This page is still under construction.

Parts of this page are still being built. What you see may change.

Jump

Time limit3sMemory limit128 MB

Summary
You remove every k-th number around a circle and print the last three removed numbers for each test case.
Level

Medium6 of 10

Topics
Math, Simulation, Segment tree
Solved
No attempts yet

Problem

The natural numbers 1,2,3,…,n1, 2, 3, \dots, n are placed around a circle in increasing order. We pick numbers one at a time to build a sequence, and counting starts at number 11.

While the circle still holds numbers, count kk numbers starting from the current position and take the kk-th one. Remove it from the circle, append it to the sequence, and start the next count from the number immediately after the one just removed. The sequence built this way is called Jump(n, k). (Here 1≤n1 \le n and 1≤k1 \le k.)

The first five numbers of Jump(10, 2) are 2,4,6,8,102, 4, 6, 8, 10. The next picks are 3,7,1,9,53, 7, 1, 9, 5, so Jump(10, 2) = [2, 4, 6, 8, 10, 3, 7, 1, 9, 5]. In the same way, Jump(13, 3) = [3, 6, 9, 12, 2, 7, 11, 4, 10, 5, 1, 8, 13], Jump(13, 10) = [10, 7, 5, 4, 6, 9, 13, 8, 3, 12, 1, 11, 2], and Jump(10, 19) = [9, 10, 3, 8, 1, 6, 4, 5, 7, 2].

Given nn and kk, write a program that finds the last three numbers of Jump(n, k). For example, when n=10n = 10 and k=2k = 2, you should print 1,9,51, 9, 5. Note that Jump(1, k) = [1].

Input

The first line contains the number of test cases TT. Each of the following lines is one test case: two natural numbers nn and kk separated by a space. (5≤n≤5000005 \le n \le 500000, 2≤k≤5000002 \le k \le 500000)

Output

For each test case, print the third-to-last, second-to-last, and last numbers of Jump(n, k) on one line, separated by spaces.

Examples2

  1. Example 1

    Input
    3
    10 2
    13 10
    30000 54321
    
    Expected output
    1 9 5
    1 11 2
    10775 17638 23432
    
  2. Example 2

    Input
    3
    13 3
    10 19
    13 10
    
    Expected output
    1 8 13
    5 7 2
    1 11 2