n = int(eval(input()))

for _ in range(n):

    K, P = list(map(int, input().split()))

    a = K - (K // P) * P

    print((P * (a == 0) + (a) * (a != 0)))
n = int(eval(input()))

for _ in range(n):

    K, P = list(map(int, input().split()))

    a = K % P

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

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

-    a = K - (K // P) * P +    a = K % P