N = int(eval(input()))

ablist = []

for _ in range(N):

    ab = list(map(int, input().split()))

    ablist.append(ab)

ans = 0

for a, b in ablist[::-1]:

    a += ans

    if a % b == 0:

        continue

    else:

        ans += (b - a % b)

print(ans)

N = int(eval(input()))

ABlist = [tuple(map(int, input().split())) for _ in range(N)]

ans = 0

for a, b in ABlist[::-1]:

    a += ans

    if a % b == 0:

        continue

    else:

        ans += (b - a % b)

print(ans)

N = int(eval(input()))
ablist = []
for _ in range(N):
    ab = list(map(int, input().split()))
    ablist.append(ab)
ans = 0
for a, b in ablist[::-1]:
    a += ans
    if a % b == 0:
        continue
    else:
        ans += b - a % b
print(ans)

N = int(eval(input()))
ABlist = [tuple(map(int, input().split())) for _ in range(N)]
ans = 0
for a, b in ABlist[::-1]:
    a += ans
    if a % b == 0:
        continue
    else:
        ans += b - a % b
print(ans)

-ablist = [] -for _ in range(N): -    ab = list(map(int, input().split())) -    ablist.append(ab) +ABlist = [tuple(map(int, input().split())) for _ in range(N)] -for a, b in ablist[::-1]: +for a, b in ABlist[::-1]: