LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion Find
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPDP problemsTree DPBitmask DPDigit DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

Marbles

HardProblem #74
Time LimitMemoryInputOutput
4 s64 MBstdinstdout

There are at most 20 colours, and the row can only be sorted one way once you have decided the order of the colours.

Marko has laid n 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 t - the number of testcases.
The first line of each testcase contains a single integer n - the number of marbles.
The second line contains n integers a1​,a2​,…,an​ - 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

Input
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
Output
3
0
21

In the first testcase three swaps are enough: swapping the third and fourth marbles gives 3,4,3,2,4,2,2, then the second and third gives 3,3,4,2,4,2,2, and finally the fourth and fifth gives 3,3,4,4,2,2,2. In the second every colour appears once, so the row is already tidy.

Constraints

1≤t≤5
2≤n≤4⋅105
1≤ai​≤20
The sum of n over all testcases does not exceed 4⋅105


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.

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit