文理学院程序设计在线练习


Card

Time Limit: 5 Second(s)    Memory Limit: 32 MB

Total Submission(s): 7   Accepted Submission(s): 4
Problem Description

Xiaoxi wants to sort a hand of cards, but it is not as easy as usual.
Xiaoxi can only exchange two adjacent cards, or move one card to the start position or the end position.
The different suits should be grouped and the ranks should be sorted within each suit. But the order of the suits does not matter and within each suit, the cards may be sorted in either ascending or descending order on rank. It is allowed for some suits to be sorted in ascending order and others in descending order.
What is the smallest number of exchanges or moves required to sort a given hand of cards?

Input

The first line contains an integer T to denote the number of data cases (T<=200).
The first line of input contains an integer n, the number of cards.
The second line contains n pairwise distinct space-separated cards, each represented by two characters. The first character of a card represents the rank and is either a digit from 2 to 9 or one of the letters T, J, Q, K, and A representing Ten, Jack, Queen, King and Ace, respectively, given here in increasing order. The second character of a card is from the set {s,h,d,c} representing the suits spades♠, hearts♥, diamonds♦, and clubs♣.
1 <= n <= 52 Σn <= 2000
There is no more than 25 testcases n > 10.

Output

For each testcase, print an integer respecting your answer in one line.

Sample Input
3
4
2h 9h 3c Ah
4
Ah 3h 6h 9h
3
9s 9h 9d
Sample Output
1
1
0
Source

2019绍兴联赛