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

ans = 0
for a, b in ab[::-1]:
    a += ans
    if a % b > 0:
        ans += b - (a % b)
print(ans)

fe80e340b5a4fc7d38566cc3bc1b033e33c15b37
<mask token>

<mask token>
for a, b in ab[::-1]:
    a += ans
    if a % b > 0:
        ans += b - a % b
print(ans)

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

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

ans = 0
for a, b in ab[::-1]:
    a += ans
    if a % b > 0:
        ans += b - (a % b)
print(ans)
