N = int(eval(input()))

src = [list(map(int,input().split())) for i in range(N)]



ans = 0

for a,b in reversed(src):

    r = (ans+a) % b

    if r == 0: continue

    ans += (b-r)

print(ans)

N = int(eval(input()))

src = [tuple(map(int,input().split())) for i in range(N)]



ans = 0

for a,b in reversed(src):

    a += ans

    ans += (b-a)%b

print(ans)
N = int(eval(input()))
src = [list(map(int, input().split())) for i in range(N)]
ans = 0
for a, b in reversed(src):
    r = (ans + a) % b
    if r == 0:
        continue
    ans += b - r
print(ans)

N = int(eval(input()))
src = [tuple(map(int, input().split())) for i in range(N)]
ans = 0
for a, b in reversed(src):
    a += ans
    ans += (b - a) % b
print(ans)

-src = [list(map(int, input().split())) for i in range(N)] +src = [tuple(map(int, input().split())) for i in range(N)] -    r = (ans + a) % b -    if r == 0: -        continue -    ans += b - r +    a += ans +    ans += (b - a) % b