You are given an \(N-sided\) polygon where vertices are numbered from 1 to \(N\) in clockwise order, You are also given an integer \(K\). You create a vertex-explosion machine that explodes vertex in polygon thereby reducing the size of the polygon. You start with vertex 2. At each step, one of the following operations on the polygon is performed:
- If \(K > 0\), then there is no effect of the vertex-explosion machine. Now, \(K\) is reduced by 1 and you move to the next available vertex at distance 2 in the clockwise direction.
- The vertex is exploded thus reducing the number of sides in the polygon by 1 and you move to the next available vertex at distance 2 in the clockwise direction from the exploded vertex.
Note: Polygon with vertex 2 and 1 exists
For example, if \(N = 4\) and \(K = 2\), then the following diagram displays an explanation.
You are required to determine the remaining last vertex.

Input format
- The first line contains \(T\) denoting the number of test cases.
- For each test case, the first line contains integers \(N\) and \(K\) denoting sides of the polygon and integer respectively.
Output format
Print an integer denoting the last-remaining vertex.
Constraints
\(1 \leq T \leq 10\)
\(3 \leq N \leq 10^{14}\)
\(1 \leq K \leq 10^{14}\)
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Login to unlock the editorial