Round Table

아직 제출이 없습니다시간 제한2초메모리 제한2048 MB

문제

There are nn people, numbered from 11 to nn, sitting at a round table. Person i+1i + 1 is sitting to the right of person ii (with person 11 sitting to the right of person nn).

You have come up with a better seating arrangement, which is given as a permutation p_1p\_1, p_2p\_2, \dots, p_np\_n. More specifically, you want to change the seats of the people so that at the end person p_i+1p\_{i+1} is sitting to the right of person p_ip\_i (with person p_1p\_1 sitting to the right of person p_np\_n). Notice that for each seating arrangement there are nn 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 1xn11 ≤ x ≤ n - 1 you cannot swap person xx and person x+1x + 1 (notice that you can swap person nn and person 11). 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 tt (1t10,0001 ≤ t ≤ 10\\,000) — the number of test cases. The descriptions of the tt test cases follow.

The first line of each test case contains a single integer nn (3n200,0003 ≤ n ≤ 200\\,000) — the number of people sitting at the table.

The second line contains nn distinct integers p_1p\_1, p_2p\_2, \dots, p_np\_n (1p_in1 ≤ p\_i ≤ n, p_ip_jp\_i \ne p\_j for iji \ne j) — the desired final order of the people around the table.

The sum of the values of nn over all test cases does not exceed 200,000200\\,000.

출력

For each test case, print the minimum number of swaps necessary to achieve the desired order.