Problem:


残り物には福がある

K 個の石から、P 人が順番に１つずつ石を取るゲームがあります。P 人目が石を取った時点で、まだ石が残っていれば、また１人目から順番に１つずつ石を取っていきます。このゲームでは、最後の石を取った人が勝ちとなります。K とP が与えられたとき、何人目が勝つか判定するプログラムを作成してください。

入力

入力は以下の形式で与えられる。

N
K1 P1
K2 P2
:
KN PN

１行目にはゲームを行う回数 N (1 ≤ N ≤ 100) が与えられる。続く N 行に、i 回目のゲームにおける石の個数 Ki (2 ≤ Ki ≤ 1000) と、ゲームに参加する人数 Pi (2 ≤ Pi ≤ 1000) が与えられる。

出力

それぞれのゲームについて、何人目が勝つかを１行に出力する。

入出力例

入力例

3
10 3
2 10
4 2

 出力例

1
2
2



Sample input:
3
10 3
2 10
4 2


Sample output:
1
2
2

Submission status: Wrong Answer

Solution code:
n = input()
for i in range(n):
    a, b = map(int, raw_input().split())
    print a % b

Submission status: Wrong Answer

Solution code:
n = input()
for i in range(n):
    a, b = map(int, raw_input().split())
    if a / b == 0: print b
    else: print a % b

Submission status: Wrong Answer

Solution code:
for _ in range(int(input())):
    k, p= map(int, input().split())
    print(p if k%p else k%p)

Submission status: Wrong Answer

Solution code:
for _ in range(int(input())):
    a,b = map(int,input().split())
    print(a%b+1)

Submission status: Wrong Answer

Solution code:
for _ in range(int(input())):
    a,b = map(int,input().split())
    print(a%b)

Submission status: Accepted

Solution code:
n = int(input())
for i in range(n):
    k,p = map(int, input().split())
    ans = k%p
    print(ans if ans != 0 else p)

Submission status: Accepted

Solution code:
import sys

T = input()
for i in range(0,T):
    K,P = raw_input().split(' ')
    K = int(K)
    P = int(P)
    print ( K % P - 1 + P ) % P + 1

Submission status: Accepted

Solution code:
N = int(raw_input())
for loop in xrange(N):
    k,p = map(int,raw_input().split())
    print (k-1)%p+1

Submission status: Accepted

Solution code:
import sys
f = sys.stdin

n = int(f.readline())

for k, p in (map(int, line.split()) for line in f):
    print(k % p if k % p else p)

Submission status: Accepted

Solution code:
N = int(raw_input())
for i in range(N):
    Ki,Pi = map(int,raw_input().split())
    tmp =  Ki%Pi
    if tmp == 0:
        print Pi
    else:
        print tmp

Submission status: Accepted

Solution code:
N = int(input())
for i in range(N):
    a, b = map(int, input().split())
    print(a % b if a % b else b)

Submission status: Accepted

Solution code:
n = input()
for i in xrange(n):
    k, p = map(int, raw_input().split())
    print (k-1)%p+1

Submission status: Accepted

Solution code:
n = input()
for i in range(n):
    a, b = map(int, raw_input().split())
    if a % b == 0: print b
    else: print a % b

Submission status: Accepted

Solution code:
n = int(input())
for i in range(n):
  k, p = map(int, input().split())
  if k % p == 0:
    print(p)
  else:
    print(k % p)

Submission status: Accepted

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0296

N = int(input())

for i in range(N):
	x = input()
	K = int(x.split(" ")[0])
	P = int(x.split(" ")[1])
	if K % P == 0:
		print(str(P))
	else:
		print(str(K % P))

Submission status: Accepted

Solution code:
N = int(raw_input())
for _ in xrange(N):
  K,P = map(int, raw_input().split())
  ans = K % P
  if ans == 0:
    ans = P
  print ans

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    n = int(input())
    for i in range(n):
        games = list(map(int, input().split()))
        result = games[0] % games[1]
        print(games[1] if result == 0 else result)

Submission status: Accepted

Solution code:
n = int(input())
for i in range(n):
    k,p = map(int,input().split())
    if k % p == 0:
        print(p)
    else:
        print(k % p)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
'import sys'
'import math'
i=int(input())
while i:
    i-=1
    n,m=map(int,input().split())
    if n%m: print(n%m)
    else:print(m)

Submission status: Accepted

Solution code:
for _ in range(int(input())):
    k, p= map(int, input().split())
    print(p if k%p==0 else k%p)

Submission status: Accepted

Solution code:
N=int(input())
for i in range(N):
  K,P=map(int,input().split())
  result=K % P
  if result==0:
    print(P)
  else:
    print(result)

Submission status: Accepted

Solution code:
for i in range(int(input())):
    K, P = map(int, input().split())
    if K%P == 0:
        print(P)
    else:
        print(K%P)

Submission status: Accepted

Solution code:
# Aizu Problem 0297: The Last is the Best

import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


n = int(input())
for __ in range(n):
    K, P = [int(_) for _ in input().split()]
    K %= P
    print(K if K > 0 else P)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0297

"""
import sys
from sys import stdin
input = stdin.readline


def main(args):
    N = int(input())
    for _ in range(N):
        K, P = map(int, input().split())
        ans = K % P
        if ans == 0:
            ans = P
        print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
n = int(input())
for _ in range(n):
    K, P = map(int, input().split())
    print(P * (K % P == 0) + (K % P) * (K % P != 0))

Submission status: Accepted

Solution code:
n = int(input())
for _ in range(n):
    K, P = map(int, input().split())
    a = K - (K // P) * P
    print(P * (a == 0) + (a) * (a != 0))

Submission status: Accepted

Solution code:
n = int(input())
for _ in range(n):
    K, P = map(int, input().split())
    a = K % P
    print(P * (a == 0) + (a) * (a != 0))

Submission status: Accepted

Solution code:
for _ in range(int(input())):
    a,b = map(int,input().split())
    print((a-1)%b+1)

Submission status: Accepted

Solution code:
# AOJ 0297: The Last One is the Best
# Python3 2018.6.23 bal4u

n = int(input())
for i in range(n):
	k, p = map(int, input().split())
	k %= p
	print(p if k == 0 else k)

Submission status: Accepted

Solution code:
n = int(input())
for _ in range(n):
  k, p = map(int, input().split())
  print((k - 1) % p + 1)

Submission status: Accepted

Solution code:
n = int(input())
for i in range(n):
    k,p = map(int,(input().split()))
    if k % p == 0:
        print(p)
    else:
        print(k % p)

Submission status: Accepted

Solution code:
N = int(input())
for _ in range(N):
	K, P = map(int, input().split())
	tmp = K%P
	if tmp==0:
		print(P)
	else:
		print(tmp)

Submission status: Accepted

Solution code:
num = int(input()) #標準入力
for _ in range(num): #num回繰り返す
    a,b = map(int,input().split()) #標準入力
    if a % b == 0:print(b) #aをbで割った余りがないなら最後の人がとったことになるから最後の人の番号を出力する
    else:print(a % b) #上記でなければaをbで割った余りを出力する

Submission status: Accepted

Solution code:
n = int(input())

for _ in range(n):
    k, p = [int(i) for i in input().split()]
    print(k % p) if k % p != 0 else print(p)

Submission status: Accepted

Solution code:
n=int(input())
for i in range(n):
    a,b=map(int,input().split())
    print((a-1)%b+1)

Submission status: Accepted

Solution code:
n = int(input())
for i in range(n):
    a,b = map(int,input().split())
    if a%b == 0:
        print(b)
    else:
        print(a%b)

Submission status: Accepted

Solution code:
N = int(input())

for i in range(N):
    K,P=map(int,input().split())
    ans = K%P
    if ans == 0:
        print(P)
    else:
        print(ans)

Submission status: Accepted

Solution code:
n=int(input())
for i in range(n):
    k,p=map(int,input().split())
    k%=p
    print(p if k==0 else k)

Submission status: Accepted

Solution code:
n = int(input())
for i in range (n):
  a,b= map(int,input().split(" "))
  if a%b == 0:
    print(b)
  else:
    print(int(a%b))

Submission status: Accepted

Solution code:
N = int(input())
i = 0
while i in range(N):
    k, p = map(int, input().split())
    if k%p == 0:
        ans = p
    else:
        ans = k%p
    print(ans)
    i = i+1

Submission status: Accepted

Solution code:
def game(a,b):
  winner=(a-1)%b+1
  print(winner)
N=int(input())
for i in range(1,int(N)+1):
  K,P=map(int,input().split())
  game(K,P)

Submission status: Accepted

Solution code:
n = int(input())
for i in range(n):
    k, p = map(int, input().split())
    k %= p
    print(p if k == 0 else k)

Submission status: Accepted

Solution code:
N = int(input())
for i in range(N):
  k, p = map(int, input().split())
  if k % p > 0:
    print(k % p)
  else:
    print(p)

Submission status: Accepted

Solution code:
n = int(input())
for _ in range(n):
    k,p = map(int, input().split())
    if (k % p):
        print(k % p)
    else:
        print(p)

Submission status: Accepted

Solution code:
n = int(input())
for _ in range(n):
    k,p = map(int, input().split())
    if (k % p):
        print(k % p)
    else:
        print(p)

Submission status: Accepted

Solution code:
N = int(input())
for i in range(N):
    K,P = map(int, input().split())
    print((K-1)%P + 1)

Submission status: Accepted

Solution code:
N=int(input())
V=[0 for i in range(N)]
for i in range(N):
  k,p=map(int,input().split())
  if p>=k:
    c=k
  else:
    while True:
      b=k-p
      if b<=p:
        c=b
        break
      else:
        k=b
  V[i-1]=c

for i in range(N):
  print(V[i-1])

Submission status: Accepted

Solution code:
N = int(input())

while N>0:
  K, P = map(int, input().split())
  if K%P == 0:
    print(P)
  else:
    print(K%P) 
  N = N-1

Submission status: Accepted

Solution code:
n = int(input())
K=[]
P=[]
while len(K)<n:
  k,p = map(int, input().split())
  K.append(k)
  P.append(p)

for i in range(n):
  a = K[i]
  b = P[i]
  if a%b !=0:
    print(a%b)
  else:
    print(b)

Submission status: Accepted

Solution code:
N=int(input())
answer=[]
for i in range(N):
  K,P=map(int,input().split())
  if K<=P:
    answer.append(K)
  elif K%P==0:
    answer.append(P)
  else:
    answer.append(K%P)
for i in range(len(answer)):
  print(answer[i])

Submission status: Accepted

Solution code:
N = int(input())
for i in range(N):
    K,P = map(int, input().split())
    print((K-1)%P + 1)

Submission status: Accepted

Solution code:
n = int(input())
for _ in range(n):
  k,p = map(int, input().split())
  print( p if (k % p)==0 else (k % p))

Submission status: Accepted

Solution code:
n = int(input())
for _ in range(n):
    k,p = map(int, input().split())
    print (p if (k % p)==0 else (k % p))

Submission status: Accepted

Solution code:
n = int(input())
for _ in range(n):
    k,p = map(int, input().split())
    print (p if (k % p)==0 else (k % p))

Submission status: Accepted

Solution code:
n = int(input())
for i in range(n):
  k, p = map(int, input().split())
  if k % p > 0:
    print(k % p)
  else:
    print(p)

Submission status: Accepted

Solution code:
N = int(input())
for i in range(N) :
  K, P = map(int, input().split())
  if(K % P == 0) :
    print(P)
  else :
    print(K % P)

Submission status: Accepted

Solution code:
n = int(input())

for _ in range(n):
  a,b = map(int,input().split())
  if a % b == 0:
    print(b)
  else:
    print(a%b)

Submission status: Accepted

Solution code:
# from sys import exit
# A, B, C = [int(n) for n in input().split()]
N = int(input())
# a = [int(input()) for _ in range(N)]
# S = str(input())
# L = len(S)
# T = str(input())
# exit()
K = [0 for _ in range(N)]
P = [0 for _ in range(N)]
for i in range(N):
    K[i], P[i] = [int(n) for n in input().split()]
    ans = K[i] % P[i]
    print(ans if ans != 0 else P[i])

Submission status: Accepted

Solution code:
n=int(input())
for i in range(n):
  a,b=map(int,input().split())
  if a%b==0:
    print(b)
  else:
    print(a%b)

Submission status: Accepted

Solution code:
N = int(input())
for l in range(N):
    K,P = [int(i) for i in input().split()]
    ans = K % P
    if ans == 0:
        ans = P
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
for _ in range(n):
    k, p = map(int, input().split())
    r = k % p
    print(r if r != 0 else p)

Submission status: Accepted

Solution code:
n=int(input())
for i in range(n):
    k,p=map(int,input().split())
    print((k-1)%p+1)

Submission status: Accepted

Solution code:
N = int(input())
for i in range(N):
    K,P = map(int, input().split())
    print((K-1)%P + 1)

Submission status: Accepted

Solution code:
n = int(input())

for _ in range(n):
    k, p = map(int, input().split())
    if k % p == 0:
        print(p)
    else:
        print(k % p)

Submission status: Accepted

Solution code:
for i in range(int(input())):
    K, P = map(int, input().split())
    print(P if K%P == 0 else K%P)