import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 7)



# 右から

N = int(eval(input()))

AB = [[int(x) for x in input().split()] for _ in range(N)]



push = 0

for a,b in AB[::-1]:

    a += push

    push += (-a)%b

print(push)
import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



N = int(readline())

m = list(map(int,read().split()))

A,B = list(zip(*list(zip(m,m))))



# 末尾から



add = 0

for a,b in zip(A[::-1],B[::-1]):

    a += add

    add += (-a) % b



print(add)
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)
# 右から
N = int(eval(input()))
AB = [[int(x) for x in input().split()] for _ in range(N)]
push = 0
for a, b in AB[::-1]:
    a += push
    push += (-a) % b
print(push)

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
m = list(map(int, read().split()))
A, B = list(zip(*list(zip(m, m))))
# 末尾から
add = 0
for a, b in zip(A[::-1], B[::-1]):
    a += add
    add += (-a) % b
print(add)

-input = sys.stdin.readline -sys.setrecursionlimit(10**7) -# 右から -N = int(eval(input())) -AB = [[int(x) for x in input().split()] for _ in range(N)] -push = 0 -for a, b in AB[::-1]: -    a += push -    push += (-a) % b -print(push) +read = sys.stdin.buffer.read +readline = sys.stdin.buffer.readline +readlines = sys.stdin.buffer.readlines +N = int(readline()) +m = list(map(int, read().split())) +A, B = list(zip(*list(zip(m, m)))) +# 末尾から +add = 0 +for a, b in zip(A[::-1], B[::-1]): +    a += add +    add += (-a) % b +print(add)