F. Minimize Fixed Points

Codeforces Round 1034 (Div. 3) F

Problem

Call a permutation pp of length nn good if gcd(pi,i)>1\gcd(p_i, i) > 1 for all 2in2 \le i \le n. Find a good permutation with the minimum number of fixed points across all good permutations of length nn. If there are multiple such permutations, print any of them.

  • A permutation of length nn is an array that contains every integer from 11 to nn exactly once, in any order.
  • gcd(x,y)\gcd(x, y) denotes the greatest common divisor (GCD) of xx and yy.
  • A fixed point of a permutation pp is an index jj (1jn1 \le j \le n) such that pj=jp_j = j.

Input

The first line contains an integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The only line of each test case contains an integer nn (2n1052 \le n \le 10^5) — the length of the permutation.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

Output

For each test case, output on a single line an example of a good permutation of length nn with the minimum number of fixed points.

Solution

Idea

Start by initializing the permutation as the identity permutation, i.e., pi=ip_i = i for all ii. Then, try to modify it to satisfy the condition.

To achieve this, for each ii, swap pip_i with pspf(i)p_{spf(i)}, where spf(i)spf(i) is the smallest prime factor of ii. For example, if i=6i = 6, the smallest prime factor is 22, so swap p6p_6 with p2p_2. This ensures that gcd(p6,6)2\gcd(p_6, 6) \ge 2 and helps reduce the number of fixed points. The number of fixed points will reduce to the number of elements that are coprime to all other elements in the permutation.

submission link