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

vertex = [{} for _ in range(N)]

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

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

d = [0 for i in range(N)]

def dfs(v1,v2):
    d[v2] = d[v1] + vertex[v1][v2]
    for v in vertex[v2]:
        if v == v1:
            continue
        dfs(v2,v)

for v in vertex[K-1].keys():
    dfs(K-1,v)

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


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

N = int(input())

vertex = [{} for _ in range(N)]

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

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

d = [0 for i in range(N)]

def dfs(v1,v2):
    d[v2] = d[v1] + vertex[v1][v2]
    for v in vertex[v2]:
        if v == v1:
            continue
        dfs(v2,v)

for v in vertex[K-1].keys():
    dfs(K-1,v)

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

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

n = int(input())

nodes = [set() for i in range(n+1)]
dis = [dict() for i in range(n+1)]
ans = [-1 for i in range(n+1)]

for _ in range(n-1):
    a,b,c = map(int,input().split())
    nodes[a].add(b)
    nodes[b].add(a)
    dis[a][b] = c
    dis[b][a] = c

#print(nodes)
#print(dis)

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

q = [K]
d = 0
ans[K] = 0
c = 0
while True:
    now = q[-1]
    nex = -1
    for x in nodes[now]:
        if ans[x] == -1:
            nex = x
            break
    if nex == -1:
        if c >= n-1:
            break
        else:
            q.pop()
            d -= dis[now][q[-1]]

    else:
        nodes[now].remove(nex)
        nodes[nex].remove(now)
        d += dis[now][nex]
        q.append(nex)
        ans[nex] = d
        c += 1

#print(ans)

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


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

N = int(input())

vertex = [{} for _ in range(N)]

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

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

d = [0 for i in range(N)]

def dfs(v1,v2):
    d[v2] = d[v1] + vertex[v1][v2]
    for v in vertex[v2]:
        if v == v1:
            continue
        dfs(v2,v)

for v in vertex[K-1].keys():
    dfs(K-1,v)

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

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

n = int(input())

nodes = [set() for i in range(n+1)]
dis = [dict() for i in range(n+1)]
ans = [-1 for i in range(n+1)]

for _ in range(n-1):
    a,b,c = map(int,input().split())
    nodes[a].add(b)
    nodes[b].add(a)
    dis[a][b] = c
    dis[b][a] = c

#print(nodes)
#print(dis)

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

q = [K]
d = 0
ans[K] = 0
c = 0
while True:
    now = q[-1]
    nex = -1
    for x in nodes[now]:
        if ans[x] == -1:
            nex = x
            break
    if nex == -1:
        if c >= n-1:
            break
        else:
            q.pop()
            d -= dis[now][q[-1]]

    else:
        nodes[now].remove(nex)
        nodes[nex].remove(now)
        d += dis[now][nex]
        q.append(nex)
        ans[nex] = d
        c += 1

#print(ans)

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


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

N = int(input())

vertex = [{} for _ in range(N)]

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

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

d = [0 for i in range(N)]

def dfs(v1,v2):
    d[v2] = d[v1] + vertex[v1][v2]
    for v in vertex[v2]:
        if v == v1:
            continue
        dfs(v2,v)

for v in vertex[K-1].keys():
    dfs(K-1,v)

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

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

n = int(input())

nodes = [set() for i in range(n+1)]
dis = [dict() for i in range(n+1)]
ans = [-1 for i in range(n+1)]

for _ in range(n-1):
    a,b,c = map(int,input().split())
    nodes[a].add(b)
    nodes[b].add(a)
    dis[a][b] = c
    dis[b][a] = c

#print(nodes)
#print(dis)

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

q = [K]
d = 0
ans[K] = 0
c = 0
while True:
    now = q[-1]
    nex = -1
    for x in nodes[now]:
        if ans[x] == -1:
            nex = x
            break
    if nex == -1:
        if c >= n-1:
            break
        else:
            q.remove(now)
            d -= dis[now][q[-1]]

    else:
        nodes[now].remove(nex)
        nodes[nex].remove(now)
        d += dis[now][nex]
        q.append(nex)
        ans[nex] = d
        c += 1

#print(ans)

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


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

N = int(input())

vertex = [{} for _ in range(N)]

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

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

d = [0 for i in range(N)]

def dfs(v1,v2):
    d[v2] = d[v1] + vertex[v1][v2]
    for v in vertex[v2]:
        if v == v1:
            continue
        dfs(v2,v)

for v in vertex[K-1].keys():
    dfs(K-1,v)

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