Buggy python submission (verdict: Time Limit Exceeded):
import sys, scipy
import numpy as np
from scipy.sparse import csr_matrix, isspmatrix_csr, isspmatrix_csc, isspmatrix
from scipy.sparse.csgraph._shortest_path import shortest_path
n=int(input())

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

G=csr_matrix(g)

q,k=map(int,input().split())
k-=1
d=shortest_path(G, method="D")

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


Fixed submission:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)
n=int(input())
g=[[]for _ in range(n)]
for _ in range(n-1):
    a,b,c=map(int,input().split())
    a,b=a-1,b-1
    g[a].append((b,c))
    g[b].append((a,c))


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

dist=[float("inf")]*n
dist[k]=0
def dfs(s):
    for v,c in g[s]:
        if dist[v]==float("inf"):
            dist[v]=dist[s]+c
            dfs(v)


dfs(k)

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

Buggy python submission (verdict: Runtime Error):
import sys, scipy
import numpy as np
from scipy.sparse import csr_matrix, isspmatrix_csr, isspmatrix_csc, isspmatrix
from scipy.sparse.csgraph._shortest_path import shortest_path
n=int(input())

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

G=csr_matrix(g)

q,k=map(int,input().split())
k-=1
d=shortest_path(G, method="D", indices=k)

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


Fixed submission:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)
n=int(input())
g=[[]for _ in range(n)]
for _ in range(n-1):
    a,b,c=map(int,input().split())
    a,b=a-1,b-1
    g[a].append((b,c))
    g[b].append((a,c))


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

dist=[float("inf")]*n
dist[k]=0
def dfs(s):
    for v,c in g[s]:
        if dist[v]==float("inf"):
            dist[v]=dist[s]+c
            dfs(v)


dfs(k)

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

Buggy python submission (verdict: Runtime Error):
import sys, scipy
import numpy as np
from scipy.sparse import csr_matrix, isspmatrix_csr, isspmatrix_csc, isspmatrix
from scipy.sparse.csgraph._shortest_path import shortest_path
n=int(input())

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

G=csr_matrix(g)

q,k=map(int,input().split())
k-=1
d=shortest_path(G, method="D", indices=k)

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


Fixed submission:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)
n=int(input())
g=[[]for _ in range(n)]
for _ in range(n-1):
    a,b,c=map(int,input().split())
    a,b=a-1,b-1
    g[a].append((b,c))
    g[b].append((a,c))


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

dist=[float("inf")]*n
dist[k]=0
def dfs(s):
    for v,c in g[s]:
        if dist[v]==float("inf"):
            dist[v]=dist[s]+c
            dfs(v)


dfs(k)

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

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
n=int(input())
g=[[float("inf")]*n for _ in range(n)]
for _ in range(n-1):
    a,b,c=map(int,input().split())
    a,b=a-1,b-1
    g[a][b]=c
    g[b][a]=c
for i in range(n):
    g[i][i]=0
q,k=map(int,input().split())
k-=1

#V=n E=n-1

d=[float("inf")]*n
d[k]=0
used=[0]*n

while True:
    v=-1
    for u in range(n):
        if (not used[u] and (v==-1 or d[u]<d[v])):
            v=u
    if v==-1:
        break
    used[v]=1
    for u in range(n):
        d[u]=min(d[u],d[v]+g[v][u])

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


Fixed submission:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)
n=int(input())
g=[[]for _ in range(n)]
for _ in range(n-1):
    a,b,c=map(int,input().split())
    a,b=a-1,b-1
    g[a].append((b,c))
    g[b].append((a,c))


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

dist=[float("inf")]*n
dist[k]=0
def dfs(s):
    for v,c in g[s]:
        if dist[v]==float("inf"):
            dist[v]=dist[s]+c
            dfs(v)


dfs(k)

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

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
n=int(input())
g=[[float("inf")]*n for _ in range(n)]
for _ in range(n-1):
    a,b,c=map(int,input().split())
    a,b=a-1,b-1
    g[a][b]=c
    g[b][a]=c
for i in range(n):
    g[i][i]=0
q,k=map(int,input().split())
k-=1

#V=n E=n-1

d=[float("inf")]*n
d[k]=0
used=[0]*n

while True:
    v=-1
    for u in range(n):
        if (not used[u] and (v==-1 or d[u]<d[v])):
            v=u
    if v==-1:
        break
    used[v]=1
    for u in range(n):
        d[u]=min(d[u],d[v]+g[v][u])

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


Fixed submission:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)
n=int(input())
g=[[]for _ in range(n)]
for _ in range(n-1):
    a,b,c=map(int,input().split())
    a,b=a-1,b-1
    g[a].append((b,c))
    g[b].append((a,c))


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

dist=[float("inf")]*n
dist[k]=0
def dfs(s):
    for v,c in g[s]:
        if dist[v]==float("inf"):
            dist[v]=dist[s]+c
            dfs(v)


dfs(k)

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