ГлавнаяСборникиТурнирыРазделыФорумыУчастникиПечатьПомощьО системе

Сборники > Алгоритмы и Структуры Данных > задача:


16. Josephus Queries

Задачи сборника

• 08. B. Песня о любви
• 09. Weird Algorithm
• 10. А. Любовь «А»
• 11. Missing Number
• 12. D. Love-Hate
• 13. Repetitions
• 14. Increasing Array
• 15. Bit Strings
• 16. Josephus Queries
• 17. Number Spiral
• 18. Apartments
• 19. Ferris Wheel
• 20. Two Sets
• 21. Distinct Numbers
• 22. Creating Strings
• 23. Apple Division
• 24. Coin Piles

Обратная связь

Если у вас есть предложения или пожелания по работе Contester, посетите форум сайта www.contester.ru.

Лимит времени 2000/4000/4000/4000 мс. Лимит памяти 65000/65000/65000/65000 Кб. Сложность Альфа


Consider a game where there are n children (numbered 1,2,…,n) in a circle. During the game, every second child is removed from the circle, until there are no children left.

Your task is to process q queries of the form: "when there are n children, who is the kth child that will be removed?"

Input

The first input line has an integer q: the number of queries.

After this, there are q lines that describe the queries. Each line has two integers n and k: the number of children and the position of the child.

Output

Print q integers: the answer for each query.

Constraints
1≤q≤105
1≤k≤n≤109

Example
Input:
4
7 1
7 3
2 2
1337 1313

Output:
2
6
1


1107Для отправки решений необходимо выполнить вход.

www.contester.ru