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())
#p is parent
#pcd is parent-child distance
d = [0 for i in range(N+1)]
def f(v,p,pcd):
    global t,d
    d[v] = pcd
    for c in t[v]:
        if c[0]!=p:
            f(c[0],v,pcd+c[1])
f(K,0,0)
for i in range(Q):
    x,y = map(int,input().split())
    print(d[x]+d[y])

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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())
D = [0]*(N+1)
def f(v,p,cost):
    global T,K,D
    for C in T[v]:
        if C[0]!=p:
            D[C[0]] = cost+C[1]
            f(C[0],v,cost+C[1])
f(K,0,0)
for i in range(Q):
    x,y = map(int,input().split())
    print(D[x]+D[y])

Buggy python submission (verdict: Runtime Error):
import sys
N = int(input())
t = [[]for i in range(N+1)]
lines = sys.stdin.readlines()
for i in range(N-1):
    a,b,c = map(int,lines[i].split())
    t[a].append([b,c])
    t[b].append([a,c])
Q,K = map(int,lines[N-1].split())
#p is parent
#pcd is parent-child distance
d = [0 for i in range(N+1)]
def f(v,p,pcd):
    global t,d
    d[v] = pcd
    for c in t[v]:
        if c[0]!=p:
            f(c[0],v,pcd+c[1])
f(K,0,0)
for i in range(Q):
    x,y = map(int,lines[N+i].split())
    print(d[x]+d[y])

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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())
D = [0]*(N+1)
def f(v,p,cost):
    global T,K,D
    for C in T[v]:
        if C[0]!=p:
            D[C[0]] = cost+C[1]
            f(C[0],v,cost+C[1])
f(K,0,0)
for i in range(Q):
    x,y = map(int,input().split())
    print(D[x]+D[y])

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())
#p is parent
#pcd is parent-child distance
d = [0 for i in range(N+1)]
def f(v,p,pcd):
    global t,d
    d[v] = pcd
    for c in t[v]:
        if c[0]!=p:
            f(c[0],v,pcd+c[1])
f(K,0,0)
for i in range(Q):
    x,y = map(int,input().split())
    print(d[x]+d[y])


Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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())
D = [0]*(N+1)
def f(v,p,cost):
    global T,K,D
    for C in T[v]:
        if C[0]!=p:
            D[C[0]] = cost+C[1]
            f(C[0],v,cost+C[1])
f(K,0,0)
for i in range(Q):
    x,y = map(int,input().split())
    print(D[x]+D[y])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
d = [[0 for i in range(N+1)] for j in range(N+1)]
for i in range(N-1):
    a,b,c = map(int,input().split())
    d[a][b] = c
    d[b][a] = c
Q,K = map(int,input().split())
#p is parent
#pcd is parent-child distance
reached = [0 for i in range(N+1)]
def f(p,pcd):
    global d,K,reached
    reached[p] = 1
    d[K][p] = pcd
    for c in range(N+1):
        if d[p][c]>0 and reached[c] == 0:
            f(c,pcd+d[p][c])
f(K,0)
for i in range(Q):
    x,y = map(int,input().split())
    print(d[K][x]+d[K][y])


Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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())
D = [0]*(N+1)
def f(v,p,cost):
    global T,K,D
    for C in T[v]:
        if C[0]!=p:
            D[C[0]] = cost+C[1]
            f(C[0],v,cost+C[1])
f(K,0,0)
for i in range(Q):
    x,y = map(int,input().split())
    print(D[x]+D[y])

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
N = int(input())
##
v = defaultdict(lambda: [])
d = [defaultdict(lambda: 0) for _ in range(N+1)]
##
for i in range(N-1):
    a,b,c = map(int,input().split())
    v[a].append(b)
    v[b].append(a)
    d[a][b] = c
    d[b][a] = c
Q,K = map(int,input().split())
##
reached = defaultdict(lambda: 0)
def f(p,pcd):
    global v,d,K,reached
    reached[p] = 1
    d[K][p] = pcd
    for c in v[p]:
        if reached[c] == 0:
            f(c, pcd+d[p][c])
f(K,0)
##
for i in range(Q):
    x,y = map(int,input().split())
    print(d[K][x]+d[K][y])

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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())
D = [0]*(N+1)
def f(v,p,cost):
    global T,K,D
    for C in T[v]:
        if C[0]!=p:
            D[C[0]] = cost+C[1]
            f(C[0],v,cost+C[1])
f(K,0,0)
for i in range(Q):
    x,y = map(int,input().split())
    print(D[x]+D[y])

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
N = int(input())
##
v = defaultdict(lambda: [])
d = defaultdict(lambda: defaultdict(lambda: 0))
##
for i in range(N-1):
    a,b,c = map(int,input().split())
    v[a].append(b)
    v[b].append(a)
    d[a][b] = c
    d[b][a] = c
Q,K = map(int,input().split())
##
reached = defaultdict(lambda: 0)
def f(p,pcd):
    global v,d,K,reached
    reached[p] = 1
    d[K][p] = pcd
    for c in v[p]:
        if reached[c] == 0:
            f(c, pcd+d[p][c])
f(K,0)
##
for i in range(Q):
    x,y = map(int,input().split())
    print(d[K][x]+d[K][y])

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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())
D = [0]*(N+1)
def f(v,p,cost):
    global T,K,D
    for C in T[v]:
        if C[0]!=p:
            D[C[0]] = cost+C[1]
            f(C[0],v,cost+C[1])
f(K,0,0)
for i in range(Q):
    x,y = map(int,input().split())
    print(D[x]+D[y])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
v = [[]for i in range(N+1)]
d = [[0 for i in range(N+1)] for j in range(N+1)]
for i in range(N-1):
    a,b,c = map(int,input().split())
    v[a].append(b)
    v[b].append(a)
    d[a][b] = c
    d[b][a] = c
Q,K = map(int,input().split())
#p is parent
#pcd is parent-child distance
reached = [0 for i in range(N+1)]
def f(p,pcd):
    global v,d,K,reached
    reached[p] = 1
    d[K][p] = pcd
    for c in v[p]:
        if reached[c] == 0:
            f(c,pcd+d[p][c])
f(K,0)
for i in range(Q):
    x,y = map(int,input().split())
    print(d[K][x]+d[K][y])

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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())
D = [0]*(N+1)
def f(v,p,cost):
    global T,K,D
    for C in T[v]:
        if C[0]!=p:
            D[C[0]] = cost+C[1]
            f(C[0],v,cost+C[1])
f(K,0,0)
for i in range(Q):
    x,y = map(int,input().split())
    print(D[x]+D[y])