n=int(eval(input()))

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

ans=0

for i in range(n):

    #print(ab[-i-1][0]+ans,ab[-i-1][1])

    if (ab[-i-1][0]+ans)%ab[-i-1][1]!=0:

        ans+=ab[-i-1][1]-(ab[-i-1][0]+ans)%ab[-i-1][1]

    #print(ans)

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

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

ab=ab[::-1]

#print(ab)

ans=0

for a,b in ab:

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

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

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

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

+ab = ab[::-1] +# print(ab) -for i in range(n): -    # print(ab[-i-1][0]+ans,ab[-i-1][1]) -    if (ab[-i - 1][0] + ans) % ab[-i - 1][1] != 0: -        ans += ab[-i - 1][1] - (ab[-i - 1][0] + ans) % ab[-i - 1][1] -    # print(ans) +for a, b in ab: +    if (a + ans) % b != 0: +        ans += b - (a + ans) % b