9368 - Last Man Standing   

Description

There are n people standing in a circle waiting to be executed. Starting from the first man, k - 1 people are skipped and the k-th man is executed. Then again, k - 1 people are skipped and the k-th man is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last man remains.

You are given number of people standing in the circle, and number k. You must find the number of last man, who standing in the circle.

Input

The first line contains integer T (0 < T <= 100), it is the number of tests. On each of next T lines there are 2 integers n and k ( 0 < k <= 109).
Case 1: 0 < n <= 102
Case 2: 0 < n <= 103
Case 3: 0 < n <= 104
Case 4: 0 < n <= 105

Output

For each test case out line formatter like this: "Case i: a". Where "i" is a test number, and "a" is the last man standing in the circle (see examples).

Sample Input  Download

Sample Output  Download

Tags




Discuss