n=int(eval(input()))

a=[list(map(int,input().split()))for _ in range(n)]

m=0

for i in range(n):

  s=(a[-1-i][0]+m)%a[-1-i][1]

  if s!=0:m+=a[-1-i][1]-s

print(m)
n=int(eval(input()))

a=[]

b=[]

for i in range(n):

  aa,bb=list(map(int,input().split()))

  a.append(aa)

  b.append(bb)

m=0

for i in range(n-1,-1,-1):

  a[i]+=m

  if a[i]%b[i]:m+=b[i]-a[i]%b[i]

print(m)
n = int(eval(input()))
a = [list(map(int, input().split())) for _ in range(n)]
m = 0
for i in range(n):
    s = (a[-1 - i][0] + m) % a[-1 - i][1]
    if s != 0:
        m += a[-1 - i][1] - s
print(m)

n = int(eval(input()))
a = []
b = []
for i in range(n):
    aa, bb = list(map(int, input().split()))
    a.append(aa)
    b.append(bb)
m = 0
for i in range(n - 1, -1, -1):
    a[i] += m
    if a[i] % b[i]:
        m += b[i] - a[i] % b[i]
print(m)

-a = [list(map(int, input().split())) for _ in range(n)] +a = [] +b = [] +for i in range(n): +    aa, bb = list(map(int, input().split())) +    a.append(aa) +    b.append(bb) -for i in range(n): -    s = (a[-1 - i][0] + m) % a[-1 - i][1] -    if s != 0: -        m += a[-1 - i][1] - s +for i in range(n - 1, -1, -1): +    a[i] += m +    if a[i] % b[i]: +        m += b[i] - a[i] % b[i]