c=0

for a,b in[list(map(int,input().split()))for _ in[0]*int(eval(input()))][::-1]:c+=(b-a-c)%b

print(c)
c=0

for t in open(0).readlines()[:0:-1]:a,b=list(map(int,t.split()));c+=(-a-c)%b

print(c)
c = 0
for a, b in [list(map(int, input().split())) for _ in [0] * int(eval(input()))][::-1]:
    c += (b - a - c) % b
print(c)

c = 0
for t in open(0).readlines()[:0:-1]:
    a, b = list(map(int, t.split()))
    c += (-a - c) % b
print(c)

-for a, b in [list(map(int, input().split())) for _ in [0] * int(eval(input()))][::-1]: -    c += (b - a - c) % b +for t in open(0).readlines()[:0:-1]: +    a, b = list(map(int, t.split())) +    c += (-a - c) % b