<reponame>m-star18/atcoder<filename>submissions/chokudai_S002/b.py
# sys.stdin.readline()
import sys
input = sys.stdin.readline

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

# sys.stdin.readline()
import sys
input = sys.stdin.readline

n = int(input())
for i in range(n):
    a, b = map(int, input().split())
    print(a%b)
# sys.stdin.readline()