Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
INF=9999999999
d=[INF for _ in range(n)]
dist=[[INF for _ in range(n)] for _ in range(n)]
linked=[[] for _ in range(n)]

for i in range(n):
    dist[i][i]=0

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

def dfs(v,p,dv):
    d[v]=dv
    for i in linked[v]:
        linked[i].remove(v)
        if(i==p):
            continue
        dfs(i,v,dv+dist[v][i])



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

dfs(k-1,-1,0)

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




Fixed submission:
import sys
sys.setrecursionlimit(10**6)
n=int(input())
INF=9999999999
d=[INF for _ in range(n)]
g=[[] for _ in range(n)]


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

def dfs(v,p,dv):
    d[v]=dv
    for gi,c in g[v]:
        if(gi==p):
            continue
        dfs(gi,v,dv+c)

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

dfs(k-1,-1,0)

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

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
INF=9999999999
d=[INF for _ in range(n)]
dist=[[INF for _ in range(n)] for _ in range(n)]
used=[False for _ in range(n)]
linked=[[] for _ in range(n)]

for i in range(n):
    dist[i][i]=0

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

def dfs(v,dv):
    d[v]=dv
    used[v]=True
    for i in linked[v]:
        linked[i].remove(v)
        if(used[i]):
            continue
        dfs(i,dv+dist[v][i])



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

dfs(k-1,0)

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




Fixed submission:
import sys
sys.setrecursionlimit(10**6)
n=int(input())
INF=9999999999
d=[INF for _ in range(n)]
g=[[] for _ in range(n)]


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

def dfs(v,p,dv):
    d[v]=dv
    for gi,c in g[v]:
        if(gi==p):
            continue
        dfs(gi,v,dv+c)

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

dfs(k-1,-1,0)

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

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
INF=9999999999
d=[INF for _ in range(n)]
dist=[[INF for _ in range(n)] for _ in range(n)]
used=[False for _ in range(n)]
linked=[[] for _ in range(n)]

for i in range(n):
    dist[i][i]=0

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

def dfs(v,dv):
    d[v]=dv
    used[v]=True
    for i in linked[v]:
        if(used[i]):
            continue
        dfs(i,dv+dist[v][i])



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

dfs(k-1,0)

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




Fixed submission:
import sys
sys.setrecursionlimit(10**6)
n=int(input())
INF=9999999999
d=[INF for _ in range(n)]
g=[[] for _ in range(n)]


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

def dfs(v,p,dv):
    d[v]=dv
    for gi,c in g[v]:
        if(gi==p):
            continue
        dfs(gi,v,dv+c)

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

dfs(k-1,-1,0)

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

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
INF=9999999999
d=[[INF for _ in range(n)] for _ in range(n)]

for i in range(n):
    d[i][i]=0

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

for a in range(n):
    for b in range(n):
        for c in range(n):
            d[b][c]=min(d[b][c],d[b][a]+d[a][c])



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




Fixed submission:
import sys
sys.setrecursionlimit(10**6)
n=int(input())
INF=9999999999
d=[INF for _ in range(n)]
g=[[] for _ in range(n)]


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

def dfs(v,p,dv):
    d[v]=dv
    for gi,c in g[v]:
        if(gi==p):
            continue
        dfs(gi,v,dv+c)

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

dfs(k-1,-1,0)

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