Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[]
B=[]
for i in range(n):
    a,b=input().split()
    A.append(a)
    B.append(b)
A.reverse()
B.reverse()
A.append(0)
B.append(0)
A=list(map(int,A))
B=list(map(int,B))

v=[0]
for i in range(0,n):
    cnt=0
    if A[i]%B[i]!=0:
        cnt+=(B[i]-A[i]%B[i])
        A[i]+=(B[i]-A[i]%B[i])
    v.append(cnt)
    A[i+1]+= sum(v)
print(sum(v))



Fixed submission:
n=int(input())
A=[]
B=[]
for i in range(n):
    a,b=input().split()
    A.append(a)
    B.append(b)
A.reverse()
B.reverse()
A.append(0)
B.append(0)
A=list(map(int,A))
B=list(map(int,B))

x=0
for i in range(0,n):
    cnt=0
    if A[i]%B[i]!=0:
        cnt+=(B[i]-A[i]%B[i])
        A[i]+=(B[i]-A[i]%B[i])
    x+=cnt
    A[i+1]+= x
print(x)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
tmp=[list(map(int,input().split())) for i in range(n)]
tmp.reverse()
tmp.append([0,0])
v=[0]
for i in range(0,n):
    cnt=0
    if tmp[i][0]%tmp[i][1]!=0:
        cnt+=(tmp[i][1]-tmp[i][0]%tmp[i][1])
        tmp[i][0]+=(tmp[i][1]-tmp[i][0]%tmp[i][1])
    v.append(cnt)
    tmp[i + 1][0] += sum(v)
print(sum(v))



Fixed submission:
n=int(input())
A=[]
B=[]
for i in range(n):
    a,b=input().split()
    A.append(a)
    B.append(b)
A.reverse()
B.reverse()
A.append(0)
B.append(0)
A=list(map(int,A))
B=list(map(int,B))

x=0
for i in range(0,n):
    cnt=0
    if A[i]%B[i]!=0:
        cnt+=(B[i]-A[i]%B[i])
        A[i]+=(B[i]-A[i]%B[i])
    x+=cnt
    A[i+1]+= x
print(x)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
tmp=[list(map(int,input().split())) for i in range(n)]
tmp.reverse()
tmp.append([0,0])
v=[0]
for i in range(0,n):
    cnt=0
    if tmp[i][0]%tmp[i][1]!=0:
        cnt+=(tmp[i][1]-tmp[i][0]%tmp[i][1])
        tmp[i][0]+=(tmp[i][1]-tmp[i][0]%tmp[i][1])
    v.append(cnt)
    tmp[i + 1][0] += sum(v)
print(sum(v))



Fixed submission:
n=int(input())
A=[]
B=[]
for i in range(n):
    a,b=input().split()
    A.append(a)
    B.append(b)
A.reverse()
B.reverse()
A.append(0)
B.append(0)
A=list(map(int,A))
B=list(map(int,B))

x=0
for i in range(0,n):
    cnt=0
    if A[i]%B[i]!=0:
        cnt+=(B[i]-A[i]%B[i])
        A[i]+=(B[i]-A[i]%B[i])
    x+=cnt
    A[i+1]+= x
print(x)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
tmp=[list(map(int,input().split())) for i in range(n)]
tmp.reverse()
tmp.append([0,0])
v=[0]
for i in range(0,n):
    cnt=0
    while(tmp[i][0]%tmp[i][1]!=0):
        cnt+=1
        tmp[i][0]+=1
    v.append(cnt)
    tmp[i + 1][0] += sum(v)
print(sum(v))



Fixed submission:
n=int(input())
A=[]
B=[]
for i in range(n):
    a,b=input().split()
    A.append(a)
    B.append(b)
A.reverse()
B.reverse()
A.append(0)
B.append(0)
A=list(map(int,A))
B=list(map(int,B))

x=0
for i in range(0,n):
    cnt=0
    if A[i]%B[i]!=0:
        cnt+=(B[i]-A[i]%B[i])
        A[i]+=(B[i]-A[i]%B[i])
    x+=cnt
    A[i+1]+= x
print(x)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
ans=0
tmp=[list(map(int,input().split())) for i in range(n)]

for i in reversed(range(0,n)):
    cnt=0
    if tmp[i][0]%tmp[i][1]!=0:
        while(tmp[i][0]%tmp[i][1]!=0):
            cnt+=1
            tmp[i][0]+=1
        for j in reversed(range(0,i)):
            tmp[j][0]+=cnt
    else:
        continue
    ans+=cnt

print(ans)



Fixed submission:
n=int(input())
A=[]
B=[]
for i in range(n):
    a,b=input().split()
    A.append(a)
    B.append(b)
A.reverse()
B.reverse()
A.append(0)
B.append(0)
A=list(map(int,A))
B=list(map(int,B))

x=0
for i in range(0,n):
    cnt=0
    if A[i]%B[i]!=0:
        cnt+=(B[i]-A[i]%B[i])
        A[i]+=(B[i]-A[i]%B[i])
    x+=cnt
    A[i+1]+= x
print(x)