Number Discovery
시간 제한2초메모리 제한1024 MB
쓰이지 않은 가장 작은 k개의 수와 그 합을 계속 이어 붙여 만든 무한 수열에서 n의 위치를 구한다.
문제
Ujan needs some rest from cleaning, so he started playing with infinite sequences. He has two integers and . He creates an infinite sequence by repeating the following steps.
- Find smallest distinct positive integers that are not in . Let's call them from the smallest to the largest.
- Append and to in this order.
- Go back to the first step.
Ujan will stop procrastinating when he writes the number in the sequence . Help him find the index of in . In other words, find the integer such that . It's possible to prove that all positive integers are included in only once.
입력
The first line contains a single integer (), the number of test cases.
Each of the following lines contains two integers and (, ), the number to be found in the sequence and the parameter used to create the sequence .
출력
In each of the lines, output the answer for the corresponding test case.
힌트
In the first sample, . is the -th number here, so the answer is .
In the second sample, .