Timer is Running

Associated Course(s):
Geeks Classes
Geeks Classes DSA- Tutorials

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.

abnandi04 | 801 |

KshamaGupta | 560 |

mummy | 322 |

NikhilGupta0301 | 260 |

jagseersinghbe18cse | 251 |

abnandi04 | 776 |

KshamaGupta | 444 |

mummy | 235 |

srivastavad270 | 223 |

bhavyapratap56 | 206 |

blackshadows | 5362 |

Ibrahim Nash | 5244 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4748 |

Login to report an issue on this page.