Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**9)

input = sys.stdin.readline

N = int(input())
T = [[] for _ in range(N+1)]

for _ in range(N-1) :
    a,b,c = map(int,input().split())
    T[a].append([b,c])
    T[b].append([a,c])

Q,K = map(int,input().split())

dist = [None]*(N+1)

def dfs(now,d) :
    dist[now] = d
    for to,c in T[now] :
        if dist[to] is not None :
            continue
        dfs(to,d+c)

dfs(K,0)

ans = [0]*Q

for _ in range(Q) :
    u,v = map(int,input().split())
    ans[i] = dist[u]+dist[v]

print(*ans,sep="\n")


Fixed submission:
from collections import deque

N = int(input())

L = [[] for i in range(N)]

for i in range(N-1) :
    a,b,c = map(int,input().split())
    a -= 1
    b -= 1
    L[a].append([b,c])
    L[b].append([a,c])
    

Q,K = map(int,input().split())

K -= 1

DISTANCE = [float("inf") for i in range(N)]

DISTANCE[K] = 0

q = deque([K])

while q :
    from_ = q.popleft()
    for to_, weight in L[from_] :
        dist = DISTANCE[from_] + weight
        if dist < DISTANCE[to_] :
            DISTANCE[to_] = dist
            q.append(to_)
            
ans = []

for i in range(Q) :
    x,y = map(int,input().split())
    x -= 1
    y -= 1
    
    calc = DISTANCE[x] + DISTANCE[y]
    ans.append(calc)

print(*ans, sep="\n")

Buggy python submission (verdict: Runtime Error):
N = int(input())
T = [[] for i in range(N+1)]

for i in range(N-1) :
    a,b,c = map(int,input().split())
    T[a].append([b,c])
    T[b].append([a,c])

Q,K = map(int,input().split())

dist = [None]*(N+1)

def dfs(now,d) :
    dist[now] = d
    for to,c in T[now] :
        if dist[to] is not None :
            continue
        dfs(to,d+c)

dfs(K,0)

ans = []

for i in range(Q) :
    u,v = map(int,input().split())
    ans.append(dist[u]+dist[v])

print(*ans,sep="\n")


Fixed submission:
from collections import deque

N = int(input())

L = [[] for i in range(N)]

for i in range(N-1) :
    a,b,c = map(int,input().split())
    a -= 1
    b -= 1
    L[a].append([b,c])
    L[b].append([a,c])
    

Q,K = map(int,input().split())

K -= 1

DISTANCE = [float("inf") for i in range(N)]

DISTANCE[K] = 0

q = deque([K])

while q :
    from_ = q.popleft()
    for to_, weight in L[from_] :
        dist = DISTANCE[from_] + weight
        if dist < DISTANCE[to_] :
            DISTANCE[to_] = dist
            q.append(to_)
            
ans = []

for i in range(Q) :
    x,y = map(int,input().split())
    x -= 1
    y -= 1
    
    calc = DISTANCE[x] + DISTANCE[y]
    ans.append(calc)

print(*ans, sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N = int(input())

T = [[] for i in range(N)]
for i in range(N-1) :
    u,v,c = map(int,input().split())
    u -= 1
    v -= 1
    T[u].append([v,c])
    T[v].append([u,c])

Q,K = map(int,input().split())
K -= 1

L = []

for i in range(Q) :
    u,v = map(int,input().split())
    u -= 1
    v -= 1
    L.append([u,v])

dist = [0]*N

q = deque([[K,0]])

explored = [0]*N

C = []
while q :
    now,c = q.pop()
    
    if not explored[now] :
        C.append(c)
        dist[now] = sum(C)
        explored[now] = 1
    
    flg = 1
    
    while T[now] :
        to, c = T[now].pop(0)
        if explored[to] :
            continue
        
        q.append([now, 0])
        q.append([to, c])
        flg = 0
        break
    if flg :
        if len(T[now]) == 0 :
            C.pop(-1)
    
    #print("now:", now, "C:", C)
        
ans = []

for i in range(Q) :
    u,v = L[i]
    calc = dist[u] + dist[v]
    ans.append(calc)

print(*ans, sep="\n")


Fixed submission:
from collections import deque

N = int(input())

L = [[] for i in range(N)]

for i in range(N-1) :
    a,b,c = map(int,input().split())
    a -= 1
    b -= 1
    L[a].append([b,c])
    L[b].append([a,c])
    

Q,K = map(int,input().split())

K -= 1

DISTANCE = [float("inf") for i in range(N)]

DISTANCE[K] = 0

q = deque([K])

while q :
    from_ = q.popleft()
    for to_, weight in L[from_] :
        dist = DISTANCE[from_] + weight
        if dist < DISTANCE[to_] :
            DISTANCE[to_] = dist
            q.append(to_)
            
ans = []

for i in range(Q) :
    x,y = map(int,input().split())
    x -= 1
    y -= 1
    
    calc = DISTANCE[x] + DISTANCE[y]
    ans.append(calc)

print(*ans, sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N = int(input())

T = [[] for i in range(N)]
for i in range(N-1) :
    u,v,c = map(int,input().split())
    u -= 1
    v -= 1
    T[u].append([v,c])
    T[v].append([u,c])

Q,K = map(int,input().split())
K -= 1

L = []

for i in range(Q) :
    u,v = map(int,input().split())
    u -= 1
    v -= 1
    L.append([u,v])

dist = [0]*N

q = deque([[K,0]])

explored = [0]*N

C = []
while q :
    now,c = q.pop()
    
    if not explored[now] :
        C.append(c)
        dist[now] = sum(C)
        explored[now] = 1
    
    flg = 1
    
    while T[now] :
        to, c = T[now].pop(0)
        if explored[to] :
            continue
        
        q.append([now, 0])
        q.append([to, c])
        flg = 0
        break
    if flg :
        if len(T[now]) == 0 :
            C.pop(-1)
    
    #print("now:", now, "C:", C)
        
ans = []

for i in range(Q) :
    u,v = L[i]
    calc = dist[u] + dist[v]
    ans.append(calc)

print(*ans, sep="\n")


Fixed submission:
from collections import deque

N = int(input())

L = [[] for i in range(N)]

for i in range(N-1) :
    a,b,c = map(int,input().split())
    a -= 1
    b -= 1
    L[a].append([b,c])
    L[b].append([a,c])
    

Q,K = map(int,input().split())

K -= 1

DISTANCE = [float("inf") for i in range(N)]

DISTANCE[K] = 0

q = deque([K])

while q :
    from_ = q.popleft()
    for to_, weight in L[from_] :
        dist = DISTANCE[from_] + weight
        if dist < DISTANCE[to_] :
            DISTANCE[to_] = dist
            q.append(to_)
            
ans = []

for i in range(Q) :
    x,y = map(int,input().split())
    x -= 1
    y -= 1
    
    calc = DISTANCE[x] + DISTANCE[y]
    ans.append(calc)

print(*ans, sep="\n")