There are **n** people standing in a circle (**numbered clockwise 1 to n**) waiting to be executed. The counting begins at point **1** in the circle and proceeds around the circle in a fixed direction (**clockwise**). In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given **freedom**.

Given the total number of persons **n** and a number **k** which indicates that **k-1 **persons are skipped and **k ^{th}** person is killed in circle. The task is to choose the place in the initial circle so that you are the last one remaining and so survive.

Consider

Firstly, the person at position

**Input: **

The first line of input contains a single integer **T** denoting the number of test cases. Then **T** test cases follow. The first and only line of each test case consists of two space separated positive integers denoting** n** and **k**.

**Output:**

Corresponding to each test case, in a new line, print the **safe position**.

**Constraints:**

1 ≤ **T** ≤ 200

1 ≤ **n, k** ≤ 200

**Example:**

**Input:**

3

2 1

4 2

50 10

**Output:**

2

1

36

**Explanation:
Testcase 1:** here n = 2 and k = 1, then safe position is 2 as the person at 1st position will be killed.

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

jhasaurabh312 | 138 |

Aaaaarohiiii | 136 |

Harsha_kunchala | 128 |

tushki | 124 |

RajaRajput | 117 |

ARUN_PUNDIR | 617 |

manishgupta8497 | 607 |

Abhimanyu_kumar | 559 |

KhyatiSoneji | 501 |

Aman___Jain | 496 |

blackshadows | 5335 |

Ibrahim Nash | 5223 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.