Just Some Permutation 5

Given N and K, find the lexicographically K-th (1-indexed) smallest permutation P1, P2, ..., PN of the first N positive integers (1,2,...,N), such that the adjacent numbers are relatively prime [gcd(Pi,Pi+1) = 1, for 1 ≤ i < N] in the permutation. A permutation of N numbers A1, A2, ..., AN is lexicographically smaller than another permutation B1, B2, ..., BN if Ai < Bi for some i and Aj =Bj forallj<i. Input First line of the input contains an integer T (≤ 20), which is the number of test cases. Each of the next T lines contain two space separated integers N (1 ≤ N ≤ 28) and K (1 ≤ K ≤ 1018). Output For each test case output the case number and then N space separated integers which is the lexicograph- ically K-th smallest permutation of the first N positive integer numbers, such that adjacent numbers in the permutation are relatively prime. If there are less than K such permutations then output ‘-1’. See sample input output for exact formatting. Sample Input 3 33 42 4 20 Sample Output Case 1: 2 1 3 Case 2: 1 4 3 2 Case 3: -1