N = int(eval(input()))



A = [None] * N

B = [None] * N



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

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

    A[N-i] = a

    B[N-i] = b



cnt = 0

for i in range(N):

    A_add = A[i] + cnt

    r =  A_add % B[i]

    if r == 0:

        pass

    else:

        cnt += B[i] - r

print(cnt)

import sys

N = int(eval(input()))



A = [None] * N

B = [None] * N



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

    a, b = list(map(int, sys.stdin.readline().split()))

    A[N-i] = a

    B[N-i] = b



cnt = 0

for i in range(N):

    A_add = A[i] + cnt

    r =  A_add % B[i]

    if r == 0:

        pass

    else:

        cnt += B[i] - r

print(cnt)

N = int(eval(input()))
A = [None] * N
B = [None] * N
for i in range(1, N + 1):
    a, b = list(map(int, input().split()))
    A[N - i] = a
    B[N - i] = b
cnt = 0
for i in range(N):
    A_add = A[i] + cnt
    r = A_add % B[i]
    if r == 0:
        pass
    else:
        cnt += B[i] - r
print(cnt)

import sys

N = int(eval(input()))
A = [None] * N
B = [None] * N
for i in range(1, N + 1):
    a, b = list(map(int, sys.stdin.readline().split()))
    A[N - i] = a
    B[N - i] = b
cnt = 0
for i in range(N):
    A_add = A[i] + cnt
    r = A_add % B[i]
    if r == 0:
        pass
    else:
        cnt += B[i] - r
print(cnt)

+import sys + -    a, b = list(map(int, input().split())) +    a, b = list(map(int, sys.stdin.readline().split()))