Round Table
시간 제한2초메모리 제한2048 MB
연속한 번호끼리의 교환은 금지되고 n과 1만 허용될 때, 원형 좌석을 주어진 순서로 바꾸는 최소 교환 횟수를 구한다.
문제
There are people, numbered from to , sitting at a round table. Person is sitting to the right of person (with person sitting to the right of person ).
You have come up with a better seating arrangement, which is given as a permutation , , , . More specifically, you want to change the seats of the people so that at the end person is sitting to the right of person (with person sitting to the right of person ). Notice that for each seating arrangement there are permutations that describe it (which can be obtained by rotations).
In order to achieve that, you can swap two people sitting at adjacent places; but there is a catch: for all you cannot swap person and person (notice that you can swap person and person ). What is the minimum number of swaps necessary? It can be proven that any arrangement can be achieved.
입력
Each test contains multiple test cases. The first line contains an integer () — the number of test cases. The descriptions of the test cases follow.
The first line of each test case contains a single integer () — the number of people sitting at the table.
The second line contains distinct integers , , , (, for ) — the desired final order of the people around the table.
The sum of the values of over all test cases does not exceed .
출력
For each test case, print the minimum number of swaps necessary to achieve the desired order.