Kth Permutation Sequence

Hard

Given two integers n and k, find the kth permutation sequence of numbers from 1 to n.

The numbers from 1 to n has n! unique permutations.

For n=3, the permutations in order are:

  1. “123”
  2. “132”
  3. “213”
  4. “231”
  5. “312”
  6. “321”

The 4th permutation is “231”.

Testing

Input Format

The first line contains an integer ‘T’, denoting the number of test cases.

For each test case, a line containing space-separated integers ‘n’ and ‘k’.

Output Format

For each test case, print the kth permutation in a separate line.

Sample Input

2
4 3
3 4

Expected Output

1324
231

Constraints

1 <= T <= 100
1 <= n <= 9
1 <= k <= n!

Companies
Editorial Link: Editorial