n=int(input())

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

cnt=0

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

    a,b=ab[i][0],ab[i][1]

    if (a+cnt)%b==0:continue

    cnt+=b-(a+cnt)%b

print(cnt)
n=int(input())

a=[]

b=[]

for i in range(n):

    A,B=list(map(int,input().split()))

    a.append(A)

    b.append(B)

ans=0

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

    if (a[i]+ans)%b[i]!=0:

        ans+=b[i]-(a[i]+ans)%b[i]

print(ans)
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]
cnt = 0
for i in range(n - 1, -1, -1):
    a, b = ab[i][0], ab[i][1]
    if (a + cnt) % b == 0:
        continue
    cnt += b - (a + cnt) % b
print(cnt)

n = int(input())
a = []
b = []
for i in range(n):
    A, B = list(map(int, input().split()))
    a.append(A)
    b.append(B)
ans = 0
for i in range(n - 1, -1, -1):
    if (a[i] + ans) % b[i] != 0:
        ans += b[i] - (a[i] + ans) % b[i]
print(ans)

-ab = [list(map(int, input().split())) for _ in range(n)] -cnt = 0 +a = [] +b = [] +for i in range(n): +    A, B = list(map(int, input().split())) +    a.append(A) +    b.append(B) +ans = 0 -    a, b = ab[i][0], ab[i][1] -    if (a + cnt) % b == 0: -        continue -    cnt += b - (a + cnt) % b -print(cnt) +    if (a[i] + ans) % b[i] != 0: +        ans += b[i] - (a[i] + ans) % b[i] +print(ans)