n = int(eval(input()))

a_li = []

b_li = []

for _ in range(n):

    a,b = list(map(int, input().split()))

    a_li.append(a)

    b_li.append(b)



cnt = 0

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

    if (a_li[-i]+cnt) % b_li[-i] != 0:

        cnt += b_li[-i] - ((a_li[-i]+cnt) % b_li[-i])

print(cnt)

n = int(eval(input()))

cnt = 0

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

for ab in ab_l[::-1]:

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

    a += cnt

    if a % b != 0:

        cnt += b - a%b

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

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

-a_li = [] -b_li = [] -for _ in range(n): -    a, b = list(map(int, input().split())) -    a_li.append(a) -    b_li.append(b) -for i in range(1, n + 1): -    if (a_li[-i] + cnt) % b_li[-i] != 0: -        cnt += b_li[-i] - ((a_li[-i] + cnt) % b_li[-i]) +ab_l = [list(map(int, input().split())) for _ in range(n)] +for ab in ab_l[::-1]: +    a, b = ab[0], ab[1] +    a += cnt +    if a % b != 0: +        cnt += b - a % b