Sorting

Interview

Time limit0.5sMemory limit256 MB

Summary
Given N and M, construct a permutation of 1..N for which insertion sort performs exactly M shifts, or report that it is impossible.
Level

Medium6 of 10

Topics
Greedy, Sorting, Array, Implementation
Solved
No attempts yet

Problem

Given a permutation A=[A1,A2,…,AN]A = [A_1, A_2, \dots, A_N] of size NN, the code below sorts it. A permutation of size NN is a sequence in which each integer from 11 to NN appears exactly once.

```
input: n, a[1 .. n]
cnt = 0
for j = 2 to n:
x = a[j]
i = j - 1
while i >= 1 and a[i] > x:
cnt = cnt + 1
a[i+1] = a[i]
i = i-1

a[i+1] = x

| Pseudocode |

When a permutation of length $N$ is sorted, cnt always lies between $0$ and $N \times (N-1)/2$, inclusive. Given two integers $N$ and $M$, find a permutation of length $N$ for which the value of cnt after sorting with the code above is $M$.

Input

The first line contains the number of test cases TT. Each test case is a single line containing NN and MM.

Output

For each test case, output on one line a permutation whose value of cnt is MM.

If more than one permutation satisfies the condition, print any one of them.

Constraints

  • 1≤N≤100,0001 \le N \le 100,000
  • 0≤M≤N×(N−1)/20 \le M \le N \times (N-1)/2

Examples1

  1. Example 1

    Input
    4
    5 0
    5 1
    5 5
    5 10
    
    Expected output
    1 2 3 4 5
    2 1 3 4 5
    5 2 1 3 4
    5 4 3 2 1