Ticket Counter
Easy Accuracy: 67.69% Submissions: 1707 Points: 2

N people standing in the queue of a movie ticket counter. It is a weird counter, distributes tickets to first K people and then last K people and again first K people and so on. The task is to find the last person to get the ticket.

Example: Let N = 9, K = 3, starting queue will like {1, 2, 3, 4, 5, 6, 7, 8, 9}. After the first distribution queue will look like {4, 5, 6, 7, 8, 9}. And after the second distribution queue will look like {4, 5, 6}. The last person to get the ticket will be 6.

1. The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains two space-separated integers N and K.

Output: For each test case, print the last problem which remains

1. 1 <= T <= 10

2. 1 <= K<= N <= 105


9 3
25 7


We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Ticket Counter

Output Window