Buggy python submission (verdict: Time Limit Exceeded):
from scipy.sparse.csgraph import csgraph_from_dense,dijkstra

N = int(input())
edge = [[-1]*(N+2)for i in range(N+2)]

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

G = csgraph_from_dense(edge, null_value=-1)
d = dijkstra(G,indices = K)

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

Fixed submission:
from collections import deque

N = int(input())

gr = { i:[] for i in range(N+1)}

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

sta = deque([K])
dis = [10**10 for i in range(N+1)]
dis[K] = 0
sumi = [-1 for i in range(N+1)]

while len(sta) >= 1:
  a = sta.pop()
  sumi[a] += 1
  
  for j in gr[a]:
    if sumi[ j[0] ] != -1:
      continue
    else:
      sta.append(j[0])
      dis[j[0]] = dis[a] + j[1]
    
for i in range(Q):
  x,y = map(int,input().split())
  
  print(int(dis[x] + dis[y] ))

Buggy python submission (verdict: Time Limit Exceeded):
from scipy.sparse.csgraph import csgraph_from_dense,dijkstra

N = int(input())
edge = [ [10**9+1 for j in range(N+1)] for i in range(N+1) ]

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

G = csgraph_from_dense(edge, null_value=10**9+1)
d = dijkstra(G,indices = K)

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

Fixed submission:
from collections import deque

N = int(input())

gr = { i:[] for i in range(N+1)}

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

sta = deque([K])
dis = [10**10 for i in range(N+1)]
dis[K] = 0
sumi = [-1 for i in range(N+1)]

while len(sta) >= 1:
  a = sta.pop()
  sumi[a] += 1
  
  for j in gr[a]:
    if sumi[ j[0] ] != -1:
      continue
    else:
      sta.append(j[0])
      dis[j[0]] = dis[a] + j[1]
    
for i in range(Q):
  x,y = map(int,input().split())
  
  print(int(dis[x] + dis[y] ))

Buggy python submission (verdict: Time Limit Exceeded):
from scipy.sparse.csgraph import csgraph_from_dense,dijkstra

N = int(input())
edge = [ [10**10 for j in range(N+1)] for i in range(N+1) ]

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

G = csgraph_from_dense(edge, null_value=10**10)
d = dijkstra(G,indices = K)

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

Fixed submission:
from collections import deque

N = int(input())

gr = { i:[] for i in range(N+1)}

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

sta = deque([K])
dis = [10**10 for i in range(N+1)]
dis[K] = 0
sumi = [-1 for i in range(N+1)]

while len(sta) >= 1:
  a = sta.pop()
  sumi[a] += 1
  
  for j in gr[a]:
    if sumi[ j[0] ] != -1:
      continue
    else:
      sta.append(j[0])
      dis[j[0]] = dis[a] + j[1]
    
for i in range(Q):
  x,y = map(int,input().split())
  
  print(int(dis[x] + dis[y] ))