N=int(eval(input()))

AB=[list(map(int,input().split())) for i in range(N)][::-1]

count=0

for i in range(N):

    AB[i][0]+=count

    if AB[i][0]%AB[i][1]!=0:

        count+=AB[i][1]-(AB[i][0]%AB[i][1])

print(count)
N=int(eval(input()))

AB=[list(map(int,input().split())) for i in range(N)]

count=0

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

    a+=count

    if a%b!=0:

        count+=b-(a%b)

print(count)
N = int(eval(input()))
AB = [list(map(int, input().split())) for i in range(N)][::-1]
count = 0
for i in range(N):
    AB[i][0] += count
    if AB[i][0] % AB[i][1] != 0:
        count += AB[i][1] - (AB[i][0] % AB[i][1])
print(count)

N = int(eval(input()))
AB = [list(map(int, input().split())) for i in range(N)]
count = 0
for a, b in AB[::-1]:
    a += count
    if a % b != 0:
        count += b - (a % b)
print(count)

-AB = [list(map(int, input().split())) for i in range(N)][::-1] +AB = [list(map(int, input().split())) for i in range(N)] -for i in range(N): -    AB[i][0] += count -    if AB[i][0] % AB[i][1] != 0: -        count += AB[i][1] - (AB[i][0] % AB[i][1]) +for a, b in AB[::-1]: +    a += count +    if a % b != 0: +        count += b - (a % b)