Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
N = int(input())

g = [[] for _ in range(N+1)]
dist = [[-1]*(N+1) for _ in range(N+1)]
for i in range(N-1):
    a, b, c = map(int, input().split())
    g[a].append([b, c])
    g[b].append([a, c])
    
Q, K = map(int, input().split())

cost = [-1] * (N+1)
cost[K] = 0
temp = [K]
while temp:
    p = temp.pop()
    for q in g[p]:
        if cost[q[0]] == -1:
            cost[q[0]] = cost[p] + q[1]
            temp.append(q[0])

for i in range(Q):
    x, y = map(int, input().split())
    print(cost[x] + cost[y])


Fixed submission:
import sys
input = sys.stdin.readline
N = int(input())

g = [[] for _ in range(N+1)]

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

cost = [-1] * (N+1)
cost[K] = 0
temp = [K]
while temp:
    p = temp.pop()
    for q in g[p]:
        if cost[q[0]] == -1:
            cost[q[0]] = cost[p] + q[1]
            temp.append(q[0])

for i in range(Q):
    x, y = map(int, input().split())
    print(cost[x] + cost[y])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
N = int(input())

g = [[] for _ in range(N+1)]
dist = [[-1]*(N+1) for _ in range(N+1)]
for i in range(N-1):
    a, b, c = map(int, input().split())
    g[a].append(b)
    g[b].append(a)
    dist[a][b] = c
    dist[b][a] = c
    
Q, K = map(int, input().split())

cost = [-1] * (N+1)
cost[K] = 0
temp = [[K, 0]]
while temp:
    p = temp.pop()
    gset = set(g[p[0]])
    gset.discard(p[1])
    for q in gset:
        cost[q] = cost[p[0]] + dist[p[0]][q]
        temp.append([q, p[0]])

for i in range(Q):
    x, y = map(int, input().split())
    print(cost[x] + cost[y])


Fixed submission:
import sys
input = sys.stdin.readline
N = int(input())

g = [[] for _ in range(N+1)]

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

cost = [-1] * (N+1)
cost[K] = 0
temp = [K]
while temp:
    p = temp.pop()
    for q in g[p]:
        if cost[q[0]] == -1:
            cost[q[0]] = cost[p] + q[1]
            temp.append(q[0])

for i in range(Q):
    x, y = map(int, input().split())
    print(cost[x] + cost[y])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

g = [[] for _ in range(N+1)]
dist = [[-1]*(N+1) for _ in range(N+1)]
for i in range(N-1):
    a, b, c = map(int, input().split())
    g[a].append(b)
    g[b].append(a)
    dist[a][b] = c
    dist[b][a] = c
    
Q, K = map(int, input().split())

cost = [-1] * (N+1)
cost[K] = 0
temp = [[K, 0]]
while temp:
    p = temp.pop()
    gset = set(g[p[0]])
    gset.discard(p[1])
    for q in gset:
        cost[q] = cost[p[0]] + dist[p[0]][q]
        temp.append([q, p[0]])

for i in range(Q):
    x, y = map(int, input().split())
    print(cost[x] + cost[y])


Fixed submission:
import sys
input = sys.stdin.readline
N = int(input())

g = [[] for _ in range(N+1)]

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

cost = [-1] * (N+1)
cost[K] = 0
temp = [K]
while temp:
    p = temp.pop()
    for q in g[p]:
        if cost[q[0]] == -1:
            cost[q[0]] = cost[p] + q[1]
            temp.append(q[0])

for i in range(Q):
    x, y = map(int, input().split())
    print(cost[x] + cost[y])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

g = [[] for _ in range(N+1)]
dist = [[-1]*(N+1) for _ in range(N+1)]
for i in range(N-1):
    a, b, c = map(int, input().split())
    g[a].append(b)
    g[b].append(a)
    dist[a][b] = c
    dist[b][a] = c
    
Q, K = map(int, input().split())

cost = [-1] * (N+1)
cost[K] = 0
temp = [K]
while temp:
    p = temp.pop()
    for q in g[p]:
        if cost[q] != -1: continue
        cost[q] = cost[p] + dist[p][q]
        temp.append(q)

for i in range(Q):
    x, y = map(int, input().split())
    print(cost[x] + cost[y])


Fixed submission:
import sys
input = sys.stdin.readline
N = int(input())

g = [[] for _ in range(N+1)]

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

cost = [-1] * (N+1)
cost[K] = 0
temp = [K]
while temp:
    p = temp.pop()
    for q in g[p]:
        if cost[q[0]] == -1:
            cost[q[0]] = cost[p] + q[1]
            temp.append(q[0])

for i in range(Q):
    x, y = map(int, input().split())
    print(cost[x] + cost[y])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
abc = [list(map(int, input().split())) for _ in range(N-1)]

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

g = [[] for _ in range(N+1)]
dist = [[-1]*(N+1) for _ in range(N+1)]
for a, b, c in abc:
    g[a].append(b)
    g[b].append(a)
    dist[a][b] = c
    dist[b][a] = c

cost = [-1] * (N+1)
cost[K] = 0
temp = [K]
while temp:
    p = temp.pop()
    for q in g[p]:
        if cost[q] != -1: continue
        cost[q] = cost[p] + dist[p][q]
        temp.append(q)

for i in range(Q):
    x, y = map(int, input().split())
    print(cost[x] + cost[y])


Fixed submission:
import sys
input = sys.stdin.readline
N = int(input())

g = [[] for _ in range(N+1)]

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

cost = [-1] * (N+1)
cost[K] = 0
temp = [K]
while temp:
    p = temp.pop()
    for q in g[p]:
        if cost[q[0]] == -1:
            cost[q[0]] = cost[p] + q[1]
            temp.append(q[0])

for i in range(Q):
    x, y = map(int, input().split())
    print(cost[x] + cost[y])

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

N = int(input())
abc = [list(map(int, input().split())) for _ in range(N-1)]

Q, K = map(int, input().split())
xy = [list(map(int, input().split())) for _ in range(Q)]

g = [[] for _ in range(N+1)]
dist = [[-1]*(N+1) for _ in range(N+1)]
for a, b, c in abc:
    g[a].append(b)
    g[b].append(a)
    dist[a][b] = c
    dist[b][a] = c

f_inf = float('inf')
cost = [f_inf] * (N+1)
cost[K] = 0
temp = deque([K])

while temp:
    p = temp.popleft()
    for q in g[p]:
        c = cost[p] + dist[p][q]
        if cost[q] >= c:
            cost[q] = c
            temp.append(q)
            
for x, y in xy:
    print(cost[x] + cost[y])


Fixed submission:
import sys
input = sys.stdin.readline
N = int(input())

g = [[] for _ in range(N+1)]

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

cost = [-1] * (N+1)
cost[K] = 0
temp = [K]
while temp:
    p = temp.pop()
    for q in g[p]:
        if cost[q[0]] == -1:
            cost[q[0]] = cost[p] + q[1]
            temp.append(q[0])

for i in range(Q):
    x, y = map(int, input().split())
    print(cost[x] + cost[y])