Buggy python submission (verdict: Runtime Error):
N = int(input())

G = [set() for i in range(N)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    G[a-1].add((b, c))
    G[b-1].add((a, c))

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

visited = [False] * N
visited[K-1] = True

distance = [-1]*N


def dfs(v, p, d):  # 現在の頂点v, vの親p, 現在の距離d
    distance[v-1] = d
    for i, c in G[v-1]:
        if visited[i-1] is False:
            if i == p:
                continue
            dfs(i, v, d+c)
    return


dfs(K, -1, 0)

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


Fixed submission:
from collections import deque

N = int(input())

G = [set() for i in range(N)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    G[a-1].add((b, c))
    G[b-1].add((a, c))

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

visited = [False] * N
visited[K-1] = True

distance = [-1]*N

que = deque([(K, 0)])
while que:
    cur, d = que.popleft()
    distance[cur-1] = d

    for nx, nd in G[cur-1]:
        if visited[nx-1] is False:
            visited[nx-1] = True
            que.append((nx, d+nd))

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

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

N = int(input())

G = set()
for _ in range(N-1):
    a, b, c = map(int, input().split())
    G.add((a, b, c))

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

visited = [False] * N
visited[K-1] = True

distance = [-1]*N

que = deque([(K, 0)])
while que:
    cur, d = que.popleft()
    distance[cur-1] = d

    for a, b, c in G:
        if a == cur and visited[b-1] is False:
            visited[b-1] = True
            que.append((b, d+c))
        elif b == cur and visited[a-1] is False:
            visited[a-1] = True
            que.append((a, d+c))

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


Fixed submission:
from collections import deque

N = int(input())

G = [set() for i in range(N)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    G[a-1].add((b, c))
    G[b-1].add((a, c))

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

visited = [False] * N
visited[K-1] = True

distance = [-1]*N

que = deque([(K, 0)])
while que:
    cur, d = que.popleft()
    distance[cur-1] = d

    for nx, nd in G[cur-1]:
        if visited[nx-1] is False:
            visited[nx-1] = True
            que.append((nx, d+nd))

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

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

N = int(input())

G = set()
for _ in range(N-1):
    a, b, c = map(int, input().split())
    G.add((a, b, c))

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

visited = [False] * N
visited[K-1] = True

distance = [-1]*N

que = deque([(K, 0)])
while que:
    cur, d = que.popleft()
    distance[cur-1] = d

    for a, b, c in G:
        if a == cur and visited[b-1] is False:
            visited[b-1] = True
            que.append((b, d+c))
        elif b == cur and visited[a-1] is False:
            visited[a-1] = True
            que.append((a, d+c))

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


Fixed submission:
from collections import deque

N = int(input())

G = [set() for i in range(N)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    G[a-1].add((b, c))
    G[b-1].add((a, c))

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

visited = [False] * N
visited[K-1] = True

distance = [-1]*N

que = deque([(K, 0)])
while que:
    cur, d = que.popleft()
    distance[cur-1] = d

    for nx, nd in G[cur-1]:
        if visited[nx-1] is False:
            visited[nx-1] = True
            que.append((nx, d+nd))

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

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

N = int(input())

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

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

visited = [False] * N
visited[K-1] = True

distance = [-1]*N

que = deque([(K-1, 0)])
while que:
    cur, d = que.popleft()
    distance[cur] = d

    for nx, nd in enumerate(G[cur]):
        if nd != -1 and visited[nx] is False:
            visited[nx] = True
            que.append((nx, d+nd))

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


Fixed submission:
from collections import deque

N = int(input())

G = [set() for i in range(N)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    G[a-1].add((b, c))
    G[b-1].add((a, c))

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

visited = [False] * N
visited[K-1] = True

distance = [-1]*N

que = deque([(K, 0)])
while que:
    cur, d = que.popleft()
    distance[cur-1] = d

    for nx, nd in G[cur-1]:
        if visited[nx-1] is False:
            visited[nx-1] = True
            que.append((nx, d+nd))

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

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

N = int(input())

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

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

visited = [False] * N
visited[K-1] = True

distance = [-1]*N

que = deque([(K-1, 0)])
while que:
    cur, d = que.popleft()
    distance[cur] = d

    for nx, nd in enumerate(G[cur]):
        if nd != -1 and visited[nx] is False:
            visited[nx] = True
            que.append((nx, d+nd))

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


Fixed submission:
from collections import deque

N = int(input())

G = [set() for i in range(N)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    G[a-1].add((b, c))
    G[b-1].add((a, c))

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

visited = [False] * N
visited[K-1] = True

distance = [-1]*N

que = deque([(K, 0)])
while que:
    cur, d = que.popleft()
    distance[cur-1] = d

    for nx, nd in G[cur-1]:
        if visited[nx-1] is False:
            visited[nx-1] = True
            que.append((nx, d+nd))

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