Candy in the box

4.4

11 votes
, Observation, Linear Search, Math, Algorithms
Problem

You have N boxes numbered 1 through N and K candies numbered 1 through K. You put the candies in the boxes in the following order:

  • first candy in the first box,
  • second candy in the second box,
  • ....... 
  • ........
  • so up to N-th candy in the Nth box,
  • the next candy in (N - 1)-th box,
  • the next candy in (N - 2)-th box
  • ........
  • .......
  • and so on up to the first box, 
  • then the next candy in the second box 
  •  ......    and so on until there is no candy left.

So you put the candies in the boxes in the following order: 1,2,3,....,N,N1,N2,....,2,1,2,3,....,N,N1,....

Find the index of the box where you put the K-th candy.

Input format

  • The first line contains denoting the number of test cases. The description of T test cases is as follows:
  • Each test case consists of a single line containing two integers N, and K.

Output format

For each test case, print the index of the box where you put the K-th candy.

Constraints

1T1052N1091K109

Sample Input
3
5 2
3 5
10 27
Sample Output
2
1
9
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the first test case, you put the first candy in the first box and the second candy in the second box.

In the second test case, you put the five candies in the boxes with numbers 1, 2, 3, 2, and 1 respectively. So you put the fifth candy in the first box,

Editor Image

?