Buggy python submission (verdict: Time Limit Exceeded):
def g(x):
    if len(x)==1:
        return x,0
    y = int(x[0])+int(x[1])
    if y<=9:
        return str(y),1
    else:
        z = int(str(y)[0])+int(str(y)[1])
        return str(z),2
def f(d,c):
    if c==1:
        return str(d),0
    if c==2:
        return g(str(d)+str(d))
    if c%2==0:
        x,n = f(d,c//2)
        return g(x+x)[0],2*n+g(x+x)[1]
    else:
        x,n = f(d,(c-1)//2)
        y,m = g(x+x)
        return g(y+str(d))[0],2*n+m+g(y+str(d))[1]
M = int(input())
x = []
cnt = 0
for _ in range(M):
    d,c = map(int,input().split())
    y,n = f(d,c)
    x.append(y)
    cnt += n
while len(x)>1:
    y0 = x.pop()
    y1 = x.pop()
    y,n = g(y0+y1)
    x.append(y)
    cnt += n
print(cnt)

Fixed submission:
def g(i,j):
    if i+j<10:
        return 1,i+j
    else:
        a = i+j
        b = a%10+a//10
        return 2,b
def f(c,d):
    if d==1:
        return 0,c
    if d==2:
        return g(c,c)
    if d%2==0:
        a,b = f(c,d//2)
        x,y = g(b,b)
        return 2*a+x,y
    else:
        a,b = f(c,(d-1)//2)
        x1,y1 = g(b,b)
        x2,y2 = g(c,y1)
        return 2*a+x1+x2,y2
M = int(input())
A = [list(map(int,input().split())) for _ in range(M)]
cnt = 0
B = []
for i in range(M):
    c,d = A[i][0],A[i][1]
    a,b = f(c,d)
    cnt += a
    B.append(b)
b = B[0]
for i in range(1,M):
    a,b = g(b,B[i])
    cnt += a
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def g(x):
    if len(x)==1:
        return x,0
    y = int(x[0])+int(x[1])
    if y<=9:
        return str(y),1
    else:
        z = int(str(y)[0])+int(str(y)[1])
        return str(z),2
def f(d,c):
    if c==1:
        return str(d),0
    if c==2:
        return g(str(d)+str(d))
    if c%2==0:
        x,n = f(d,c//2)
        return g(x+x)[0],2*n+g(x+x)[1]
    else:
        x,n = f(d,(c-1)//2)
        y,m = g(x+x)
        return g(y+str(d))[0],2*n+m+g(y+str(d))[1]
M = int(input())
x = deque([])
cnt = 0
for _ in range(M):
    d,c = map(int,input().split())
    y,n = f(d,c)
    x.append(y)
    cnt += n
while len(x)>1:
    y0 = x.popleft()
    y1 = x.popleft()
    y,n = g(y0+y1)
    x.appendleft(y)
    cnt += n
print(cnt)

Fixed submission:
def g(i,j):
    if i+j<10:
        return 1,i+j
    else:
        a = i+j
        b = a%10+a//10
        return 2,b
def f(c,d):
    if d==1:
        return 0,c
    if d==2:
        return g(c,c)
    if d%2==0:
        a,b = f(c,d//2)
        x,y = g(b,b)
        return 2*a+x,y
    else:
        a,b = f(c,(d-1)//2)
        x1,y1 = g(b,b)
        x2,y2 = g(c,y1)
        return 2*a+x1+x2,y2
M = int(input())
A = [list(map(int,input().split())) for _ in range(M)]
cnt = 0
B = []
for i in range(M):
    c,d = A[i][0],A[i][1]
    a,b = f(c,d)
    cnt += a
    B.append(b)
b = B[0]
for i in range(1,M):
    a,b = g(b,B[i])
    cnt += a
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
def g(i,j):
    if i+j<10:
        return 1,i+j
    else:
        a = i+j
        b = a%10+a//10
        return 2,b
def f(c,d):
    if d==1:
        return 0,c
    if d==2:
        return g(c,c)
    if d%2==0:
        a,b = f(c,d//2)
        x,y = g(b,b)
        return 2*a+x,y
    else:
        a,b = f(c,d-1)
        x,y = g(c,b)
        return a+x,y
M = int(input())
A = [list(map(int,input().split())) for _ in range(M)]
cnt = 0
B = []
for i in range(M):
    c,d = A[i][0],A[i][1]
    a,b = f(c,d)
    cnt += a
    B.append(b)
b = B[0]
for i in range(1,M):
    a,b = g(b,B[i])
    cnt += a
print(cnt)   


Fixed submission:
def g(i,j):
    if i+j<10:
        return 1,i+j
    else:
        a = i+j
        b = a%10+a//10
        return 2,b
def f(c,d):
    if d==1:
        return 0,c
    if d==2:
        return g(c,c)
    if d%2==0:
        a,b = f(c,d//2)
        x,y = g(b,b)
        return 2*a+x,y
    else:
        a,b = f(c,(d-1)//2)
        x1,y1 = g(b,b)
        x2,y2 = g(c,y1)
        return 2*a+x1+x2,y2
M = int(input())
A = [list(map(int,input().split())) for _ in range(M)]
cnt = 0
B = []
for i in range(M):
    c,d = A[i][0],A[i][1]
    a,b = f(c,d)
    cnt += a
    B.append(b)
b = B[0]
for i in range(1,M):
    a,b = g(b,B[i])
    cnt += a
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
def g(i,j):
    if i+j<10:
        return 1,i+j
    else:
        a = i+j
        b = a%10+a//10
        return 2,b
def f(c,d):
    if d==1:
        return 0,c
    if d==2:
        return g(c,c)
    if d%2==0:
        return f(c,d//2)[0]*2+g(f(c,d//2)[1],f(c,d//2)[1])[0],g(f(c,d//2)[1],f(c,d//2)[1])[1]
    else:
        return f(c,d-1)[0]+g(c,f(c,d-1)[1])[0],g(c,f(c,d-1)[1])[1]
M = int(input())
A = [list(map(int,input().split())) for _ in range(M)]
cnt = 0
B = []
for i in range(M):
    c,d = A[i][0],A[i][1]
    a,b = f(c,d)
    cnt += a
    B.append(b)
b = B[0]
for i in range(1,M):
    a,b = g(b,B[i])
    cnt += a
print(cnt)   


Fixed submission:
def g(i,j):
    if i+j<10:
        return 1,i+j
    else:
        a = i+j
        b = a%10+a//10
        return 2,b
def f(c,d):
    if d==1:
        return 0,c
    if d==2:
        return g(c,c)
    if d%2==0:
        a,b = f(c,d//2)
        x,y = g(b,b)
        return 2*a+x,y
    else:
        a,b = f(c,(d-1)//2)
        x1,y1 = g(b,b)
        x2,y2 = g(c,y1)
        return 2*a+x1+x2,y2
M = int(input())
A = [list(map(int,input().split())) for _ in range(M)]
cnt = 0
B = []
for i in range(M):
    c,d = A[i][0],A[i][1]
    a,b = f(c,d)
    cnt += a
    B.append(b)
b = B[0]
for i in range(1,M):
    a,b = g(b,B[i])
    cnt += a
print(cnt)