n,*a=list(map(int,open(0).read().split()))

#print(a)

sumcount=0

for s in range(n*2-1,-1,-2):

  hidari=a[s-1]+sumcount

#  print(hidari,a[s],sumcount)

  if hidari%a[s]==0:

    continue

  elif hidari%a[s]!=0 and hidari//a[s]==0:

    sumcount+=a[s]-hidari

  elif hidari%a[s]!=0 and hidari//a[s]!=0:

    sumcount+=(hidari//a[s]+1)*a[s]-hidari

print(sumcount)
n,*a=list(map(int,open(0).read().split()))

sumcount=0

for s in range(n*2-1,-1,-2):

  hidari=a[s-1]+sumcount

  if hidari%a[s]!=0 and hidari//a[s]==0:

    sumcount+=a[s]-hidari

  if hidari%a[s]!=0 and hidari//a[s]!=0:

    sumcount+=(hidari//a[s]+1)*a[s]-hidari

print(sumcount)
n, *a = list(map(int, open(0).read().split()))
# print(a)
sumcount = 0
for s in range(n * 2 - 1, -1, -2):
    hidari = a[s - 1] + sumcount
    #  print(hidari,a[s],sumcount)
    if hidari % a[s] == 0:
        continue
    elif hidari % a[s] != 0 and hidari // a[s] == 0:
        sumcount += a[s] - hidari
    elif hidari % a[s] != 0 and hidari // a[s] != 0:
        sumcount += (hidari // a[s] + 1) * a[s] - hidari
print(sumcount)

n, *a = list(map(int, open(0).read().split()))
sumcount = 0
for s in range(n * 2 - 1, -1, -2):
    hidari = a[s - 1] + sumcount
    if hidari % a[s] != 0 and hidari // a[s] == 0:
        sumcount += a[s] - hidari
    if hidari % a[s] != 0 and hidari // a[s] != 0:
        sumcount += (hidari // a[s] + 1) * a[s] - hidari
print(sumcount)

-# print(a) -    #  print(hidari,a[s],sumcount) -    if hidari % a[s] == 0: -        continue -    elif hidari % a[s] != 0 and hidari // a[s] == 0: +    if hidari % a[s] != 0 and hidari // a[s] == 0: -    elif hidari % a[s] != 0 and hidari // a[s] != 0: +    if hidari % a[s] != 0 and hidari // a[s] != 0: