Marko has laid coloured marbles out in a row. He likes things tidy, so he wants every colour to end up in one solid block: for each colour, all marbles of that colour standing next to each other, with nothing of another colour in between.
The only move he is allowed is to pick two neighbouring marbles and swap them.
Find the smallest number of swaps that gets the row tidy. The blocks may end up in any order - all that matters is that each colour forms exactly one of them.
Input
First line of input will be a single integer - the number of testcases.
The first line of each testcase contains a single integer - the number of marbles.
The second line contains integers - the colour of each marble, in the order they are lying.
Output
For every testcase print a single line with the smallest number of swaps needed.
Example
3 7 3 4 2 3 4 2 2 5 20 1 14 10 2 13 5 5 4 4 3 5 7 6 5 4 4 6 5
3 0 21
In the first testcase three swaps are enough: swapping the third and fourth marbles gives , then the second and third gives , and finally the fourth and fifth gives . In the second every colour appears once, so the row is already tidy.
Constraints
The sum of over all testcases does not exceed
This problem is based on Marbles, problem 1215E from Codeforces Round 585, by Mike Mirzayanov and the Codeforces team. The statement here is our own.