Buggy python submission (verdict: Runtime Error):
n = int(input())
edges = [[] for _ in range(n)]
costs = [-1]*n
def dfs(current):

    for nex, cost in edges[current]:
        if costs[nex] != -1:
            continue
        costs[nex] = costs[current] + cost
        dfs(nex)

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

q, k = map(int, input().split())
k -= 1
costs[k] = 0
dfs(k)

res = []
for _ in range(q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    res.append(costs[x]+costs[y])

for ans in res:
    print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(10**9)

n = int(input())
edges = [[] for _ in range(n)]
costs = [-1]*n
def dfs(current):

    for nex, cost in edges[current]:
        if costs[nex] != -1:
            continue
        costs[nex] = costs[current] + cost
        dfs(nex)

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

q, k = map(int, input().split())
k -= 1
costs[k] = 0
dfs(k)

res = []
for _ in range(q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    res.append(costs[x]+costs[y])

for ans in res:
    print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
edges = [[] for _ in range(n)]
costs = [-1]*n
def dfs(current, d):
    costs[current] = d
    for nex in edges[current]:
        if costs[nex[0]] != -1:
            continue
        dfs(nex[0], d+nex[1])

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

q, k = map(int, input().split())
k -= 1
dfs(k, 0)

res = []
for _ in range(q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    res.append(costs[x]+costs[y])

for ans in res:
    print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(10**9)

n = int(input())
edges = [[] for _ in range(n)]
costs = [-1]*n
def dfs(current):

    for nex, cost in edges[current]:
        if costs[nex] != -1:
            continue
        costs[nex] = costs[current] + cost
        dfs(nex)

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

q, k = map(int, input().split())
k -= 1
costs[k] = 0
dfs(k)

res = []
for _ in range(q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    res.append(costs[x]+costs[y])

for ans in res:
    print(ans)

Buggy python submission (verdict: Runtime Error):
import heapq

INF = 10**15
n = int(input())
edges = [[] for _ in range(n)]
costs = [INF]*n
def dfs(current, prev, d):
    costs[current] = d
    for nex in edges[current]:
        if nex[0] == prev:
            continue
        dfs(nex[0], current, d+nex[1])

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

q, k = map(int, input().split())
k -= 1
dfs(k, -1, 0)

res = []
for _ in range(q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    res.append(costs[x]+costs[y])

for ans in res:
    print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(10**9)

n = int(input())
edges = [[] for _ in range(n)]
costs = [-1]*n
def dfs(current):

    for nex, cost in edges[current]:
        if costs[nex] != -1:
            continue
        costs[nex] = costs[current] + cost
        dfs(nex)

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

q, k = map(int, input().split())
k -= 1
costs[k] = 0
dfs(k)

res = []
for _ in range(q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    res.append(costs[x]+costs[y])

for ans in res:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

INF = 10**15
n = int(input())
dist_mat = [[INF]*n for _ in range(n)]
edges = [[] for _ in range(n)]
for i in range(n-1):
    a, b, c = map(int,input().split())
    a -= 1
    b -= 1
    dist_mat[a][b] = c
    dist_mat[b][a] = c
    edges[a].append(b)
    edges[b].append(a)
    dist_mat[i][i] = 0

q, k = map(int, input().split())
k -= 1

que = [(0, k)]
heapq.heapify(que)
costs = [INF]*n
costs[k] = 0
while len(que) > 0:
    dist, current = heapq.heappop(que)
    for edge in edges[current]:
        if costs[edge] > costs[current] + dist_mat[current][edge]:
            costs[edge] = costs[current] + dist_mat[current][edge]
            heapq.heappush(que, (costs[edge], edge))

res = []
for _ in range(q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    res.append(costs[x]+costs[y])

for ans in res:
    print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(10**9)

n = int(input())
edges = [[] for _ in range(n)]
costs = [-1]*n
def dfs(current):

    for nex, cost in edges[current]:
        if costs[nex] != -1:
            continue
        costs[nex] = costs[current] + cost
        dfs(nex)

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

q, k = map(int, input().split())
k -= 1
costs[k] = 0
dfs(k)

res = []
for _ in range(q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    res.append(costs[x]+costs[y])

for ans in res:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

INF = 10**30
n = int(input())
dist_mat = [[INF]*n for _ in range(n)]
edges = [[] for _ in range(n)]
for i in range(n-1):
    a, b, c = map(int,input().split())
    a -= 1
    b -= 1
    dist_mat[a][b] = c
    dist_mat[b][a] = c
    edges[a].append(b)
    edges[b].append(a)
    dist_mat[i][i] = 0

q, k = map(int, input().split())
k -= 1

que = [(0, k)]
heapq.heapify(que)
costs = [INF]*n
costs[k] = 0
while len(que) > 0:
    dist, current = heapq.heappop(que)
    for edge in edges[current]:
        if costs[edge] > costs[current] + dist_mat[current][edge]:
            costs[edge] = costs[current] + dist_mat[current][edge]
            heapq.heappush(que, (costs[edge], edge))

res = []
for _ in range(q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    res.append(costs[x]+costs[y])

for ans in res:
    print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(10**9)

n = int(input())
edges = [[] for _ in range(n)]
costs = [-1]*n
def dfs(current):

    for nex, cost in edges[current]:
        if costs[nex] != -1:
            continue
        costs[nex] = costs[current] + cost
        dfs(nex)

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

q, k = map(int, input().split())
k -= 1
costs[k] = 0
dfs(k)

res = []
for _ in range(q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    res.append(costs[x]+costs[y])

for ans in res:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

INF = 10**30
n = int(input())
dist_mat = [[INF]*n for _ in range(n)]
edges = [[] for _ in range(n)]
for i in range(n-1):
    a, b, c = map(int,input().split())
    a -= 1
    b -= 1
    dist_mat[a][b] = c
    dist_mat[b][a] = c
    edges[a].append(b)
    edges[b].append(a)
    dist_mat[i][i] = 0

q, k = map(int, input().split())
k -= 1

que = [(0, k)]
heapq.heapify(que)
costs = [INF]*n
costs[k] = 0
while len(que) > 0:
    dist, current = heapq.heappop(que)
    for edge in edges[current]:
        if costs[edge] > costs[current] + dist_mat[current][edge]:
            costs[edge] = costs[current] + dist_mat[current][edge]
            heapq.heappush(que, (costs[edge], edge))

res = []
for _ in range(q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    res.append(costs[x]+costs[y])

for ans in res:
    print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(10**9)

n = int(input())
edges = [[] for _ in range(n)]
costs = [-1]*n
def dfs(current):

    for nex, cost in edges[current]:
        if costs[nex] != -1:
            continue
        costs[nex] = costs[current] + cost
        dfs(nex)

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

q, k = map(int, input().split())
k -= 1
costs[k] = 0
dfs(k)

res = []
for _ in range(q):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    res.append(costs[x]+costs[y])

for ans in res:
    print(ans)