qyliang/CodeNanoFix huggingface dataset:
Buggy code:
def gcd(a,b):
	t = b
	b = a % b
	
	if b == 0:
		return t
	else:
		return gcd(t,b)
        
def lcm(a, b):
    return a * b / gcd(a, b)

n = int(input())

ans = 1
for i in range(n):
    ans = lcm(ans, int(input()))

print(ans)

Fixed code:
def gcd(a,b):
    if b == 0:
        return a
    return gcd(b, a%b)

n = int(input())
ans = 1
for i in range(n):
    t = int(input())
    ans *= t // gcd(ans, t)

print(ans)
Buggy code:
# -*- coding: utf-8 -*-

n = int(input())

def gcd(a, b):
    if b==0:
        return a
    return gcd(b, a%b)

def lcm(a,b):
    return a*b/gcd(a,b)

ans = 1
for _ in range(n):
    t = int(input())
    ans = lcm(ans, t)

print(ans)


Fixed code:
# -*- coding: utf-8 -*-

n = int(input())

def gcd(a, b):
    if b==0:
        return a
    return gcd(b, a%b)

def lcm(a,b):
    return a//gcd(a,b)*b

ans = 1
for _ in range(n):
    t = int(input())
    ans = lcm(ans, t)

print(ans)
Buggy code:
import sys
read = sys.stdin.read
from math import gcd
def main():
    n = int(input())
    t = [int(input()) for _ in range(n)]
    if n == 1:
        print(t[0])
        sys.exit()
    r = (t[0] * t[1]) // gcd(t[0], t[1])
    if n == 2:
        print(r)
        sys.exit()
    for i1 in range(2, n):
        r = (r * t[i1]) // gcd(r, t[1])
    print(r)

if __name__ == '__main__':
    main()


Fixed code:
import sys
read = sys.stdin.read
from math import gcd
def main():
    n = int(input())
    t = [int(input()) for _ in range(n)]
    if n == 1:
        print(t[0])
        sys.exit()
    r = (t[0] * t[1]) // gcd(t[0], t[1])
    if n == 2:
        print(r)
        sys.exit()
    for i1 in range(2, n):
        r = (r * t[i1]) // gcd(r, t[i1])
    print(r)

if __name__ == '__main__':
    main()
Buggy code:
import sys
sys.setrecursionlimit(100000)

N = int(input())
T = []
for i in range(N):
    T.append(int(input()))

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

lcm = 1
for i in T:
    lcm = lcm * i / gcd(lcm, i)

print(lcm)

Fixed code:
import sys
sys.setrecursionlimit(100000)

N = int(input())
T = []
for i in range(N):
    T.append(int(input()))

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

lcm = 1
for i in T:
    lcm = int(lcm * i // gcd(lcm, i))

print(lcm)
Buggy code:
def gcd(p, q):
    if p < q:
        p, q = q, p
    while q > 0:
        p, q = q, p%q
    return p

n = int(input())
ans = 1
for i in range(n):
  t = int(input())
  ans = a*t//gcd(a, t)
print(ans)

Fixed code:
def gcd(p, q):
    if p < q:
        p, q = q, p
    while q > 0:
        p, q = q, p%q
    return p

n = int(input())
ans = 1
for i in range(n):
  t = int(input())
  ans = ans*t//gcd(ans, t)
print(ans)
Buggy code:
N = int(input())
T = []

for _ in range(N):
	T.append(int(input()))
	
def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

ans = 1
	
for i in range(N):
	ret = ret * T[i] // gcd(ret, T[i]])
	
print(ans)

Fixed code:
N = int(input())
T = []

for _ in range(N):
	T.append(int(input()))
	
def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

ans = 1
	
for i in range(N):
	ans = ans * T[i] // gcd(ans, T[i])
	
print(ans)
Buggy code:
import networkx as nx

N = int(input())
ABC = [list(map(int, input().split())) for _ in range(N - 1)]
Q, K = map(int, input().split())
XY = [list(map(int, input().split())) for _ in range(Q)]

G = nx.Graph()

for a, b, c in ABC:
    G.add_edge(a - 1, b - 1, weight=c)

# pred, dist = nx.dijkstra_predecessor_and_distance(G, K - 1)
dist = nx.shortest_path_length(G, K - 1)

for x, y in XY:
    print(dist[x - 1] + dist[y - 1])



Fixed code:
import networkx as nx

N = int(input())
ABC = [list(map(int, input().split())) for _ in range(N - 1)]
Q, K = map(int, input().split())
XY = [list(map(int, input().split())) for _ in range(Q)]

G = nx.Graph()

for a, b, c in ABC:
    G.add_edge(a - 1, b - 1, weight=c)

pred, dist = nx.dijkstra_predecessor_and_distance(G, K - 1)

for x, y in XY:
    print(dist[x - 1] + dist[y - 1])
Buggy code:

from collections import defaultdict,deque
import sys
finput=lambda: sys.stdin.readline().strip()

def main():
  n=int(finput())
  edges=[tuple(map(int,finput().split())) for _ in range(n-1)]
  q,k=map(int,finput().split())
  xy=[tuple(map(int,finput().split())) for _ in range(q)]
  ed=defaultdict(deque)
  wt=defaultdict(int)
  for e in edges:
    ed[e[0]].append(e[1])
    ed[e[1]].append(e[0])
    wt[(e[0],e[1])]=e[2]
    wt[(e[1],e[0])]=e[2]
  stack=deque([k])
  cv=stack[0]
  dist=defaultdict(int)
  while stack:
    while ed[cv]:
      if cv!=k:
        if ed[cv][-1]==stack[-1]:
          ed[cv].pop()
          if not ed[cv]:
            break
      stack.append(cv)
      cv=ed[cv].pop()
      dist[cv]=dist[stack[-1]]+wt[(stack[-1],cv)]
    cv=stack.pop()
  print(dist)
  for que in xy:
    print(dist[que[0]]+dist[que[1]])

if __name__=='__main__':
    main()


Fixed code:
from collections import defaultdict,deque
import sys
finput=lambda: sys.stdin.readline().strip()

def main():
  n=int(finput())
  edges=[tuple(map(int,finput().split())) for _ in range(n-1)]
  q,k=map(int,finput().split())
  xy=[tuple(map(int,finput().split())) for _ in range(q)]
  ed=defaultdict(deque)
  wt=defaultdict(int)
  for e in edges:
    ed[e[0]].append(e[1])
    ed[e[1]].append(e[0])
    wt[(e[0],e[1])]=e[2]
    wt[(e[1],e[0])]=e[2]
  stack=deque([k])
  cv=stack[0]
  dist=defaultdict(int)
  while stack:
    while ed[cv]:
      if cv!=k:
        if ed[cv][-1]==stack[-1]:
          ed[cv].pop()
          if not ed[cv]:
            break
      stack.append(cv)
      cv=ed[cv].pop()
      dist[cv]=dist[stack[-1]]+wt[(stack[-1],cv)]
    cv=stack.pop()
  for que in xy:
    print(dist[que[0]]+dist[que[1]])

if __name__=='__main__':
    main()
Buggy code:
import heapq
N = int(input())
edges = []
for i in range(N-1):
    a, b, c = map(int, input().split())
    edges.append([a-1, b-1, c])
    edges.append([b-1, a-1, c])
Q, K = map(int, input().split())
K -= 1


ans_list = []
def dijkstra(edges, num_v, start):
    dist = [float('inf')] * num_v
    dist[start] = 0
    q = []
    heapq.heappush(q, [0, start])

    while len(q) > 0:
        _, u = heapq.heappop(q)
        for edge in edges:
            if dist[edge[0]] > dist[u] + edge[1]:
                dist[edge[0]] = dist[u] + edge[1]
                heapq.heappush(q, [dist[u] + edge[1], edge[0]])
    return dist
for i in range(Q):
    x, y = map(lambda n: int(n) - 1, input().split())
    x_K = dijkstra(edges, N, x)[K]
    K_y = dijkstra(edges, N, K)[y]
    ans_list.append(x_K + K_y)

for i in ans_list:
    print(i)

Fixed code:
import heapq
N = int(input())
edges = [[] for _ in range(N)]
for i in range(N-1):
    a, b, c = map(int, input().split())
    edges[a-1].append([b-1, c])
    edges[b-1].append([a-1, c])
Q, K = map(int, input().split())
K -= 1


ans_list = []
def dijkstra(edges, num_v, start):
    dist = [float('inf')] * num_v
    dist[start] = 0
    q = []
    heapq.heappush(q, [0, start])

    while len(q) > 0:
        _, u = heapq.heappop(q)
        for edge in edges[u]:
            if dist[edge[0]] > dist[u] + edge[1]:
                dist[edge[0]] = dist[u] + edge[1]
                heapq.heappush(q, [dist[u] + edge[1], edge[0]])
    return dist
base = dijkstra(edges,  N, K)
for i in range(Q):
    x, y = map(lambda n: int(n) - 1, input().split())
    x_K = base[x]
    K_y = base[y]
    ans_list.append(x_K + K_y)

for i in ans_list:
    print(i)
Buggy code:

from collections import deque
N=int(input())
T=[[] for _ in range(N)]
for i in range(N-1):
    a,b,c=map(int, input().split())
    T[a-1].append((b-1,c))
    T[b-1].append((a-1,c))

Q,K=map(int, input().split())
que=[(K-1,0)
done=[-1]*N
while que:
    t=que.pop(0)
    now,nowd=t
    done[now]=nowd
    for nex in T[now]:
        nexnode,nexd=nex
        if done[nexnode]!=-1:
            continue
        else:
            que.append((nexnode, nowd+nexd))

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

Fixed code:

from collections import deque
N=int(input())
T=[[] for _ in range(N)]
for i in range(N-1):
    a,b,c=map(int, input().split())
    T[a-1].append((b-1,c))
    T[b-1].append((a-1,c))

Q,K=map(int, input().split())
que=deque([(K-1,0)])
done=[-1]*N
while que:
    t=que.popleft()
    now,nowd=t
    done[now]=nowd
    for nex in T[now]:
        nexnode,nexd=nex
        if done[nexnode]!=-1:
            continue
        else:
            que.append((nexnode, nowd+nexd))

#print(done)
for i in range(Q):
    x,y=map(int, input().split())
    print(done[x-1]+done[y-1])
Buggy code:
from collections import deque
import sys
INF = 11414191981036436433-4

n = int(input())
g = [[] for i in range(n)]
for v 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))
    
q,k = map(int,input().split())
k -= 1

r = [INF]*n
que = deque([k])
r[k] = 0
while que:
    v = que.popleft()
    rr = r[v]
    for w,x in G[v]:
        if r[w] == INF:
            r[w] = rr + x
            que.append(w)
            
for i in range(q):
    d,e = map(int,input().split())
    print(r[d-1] + r[e-1])

Fixed code:
from collections import deque
import sys
INF = 11414191981036436433-4

n = int(input())
g = [[] for i in range(n)]
for v 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))
    
q,k = map(int,input().split())
k -= 1

r = [INF]*n
que = deque([k])
r[k] = 0
while que:
    v = que.popleft()
    rr = r[v]
    for w,x in g[v]:
        if r[w] == INF:
            r[w] = rr + x
            que.append(w)
            
for i in range(q):
    d,e = map(int,input().split())
    print(r[d-1] + r[e-1])
Buggy code:
import sys
from collections import deque
input = sys.stdin.readline
n = int(input())
edges = [[]for _ in range(n)]
for _ in range(n-1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append((c, b))
    edges[b].append((c, a))
INF = float('inf')


def bfs(init_v):
    dist = [INF]*n
    dist[init_v] = 0
    tasks = deque([(0, init_v)])
    while tasks:
        print(tasks)
        d, v = tasks.popleft()
        for d2, v2 in edges[v]:
            if dist[v2] <= dist[v]+d2:
                continue
            dist[v2] = dist[v]+d2
            tasks.append((dist[v2], v2))
    return dist


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

Fixed code:
import sys
from collections import deque
input = sys.stdin.readline
n = int(input())
edges = [[]for _ in range(n)]
for _ in range(n-1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append((c, b))
    edges[b].append((c, a))
INF = float('inf')


def bfs(init_v):
    dist = [INF]*n
    dist[init_v] = 0
    tasks = deque([(0, init_v)])
    while tasks:
        d, v = tasks.popleft()
        for d2, v2 in edges[v]:
            if dist[v2] <= dist[v]+d2:
                continue
            dist[v2] = dist[v]+d2
            tasks.append((dist[v2], v2))
    return dist


q, k = map(int, input().split())
dist = bfs(k-1)
for _ in range(q):
    x, y = map(lambda x: int(x)-1, input().split())
    print(dist[x]+dist[y])
Buggy code:
sys.setrecursionlimit(10**5 + 5)

N = int(input())

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

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

Q, K = map(int, input().split())
xy = []
for _ in range(Q):
    xy.append(tuple(map(int, input().split())))

d = [-1 for _ in range(N+1)]


def dfs(s: int):
    for v, c in g[s]:
        if d[v] == -1:
            d[v] = d[s] + c
            dfs(v)


d[K] = 0
dfs(K)

for x, y in xy:
    print(d[x]+d[y])


Fixed code:
import sys
sys.setrecursionlimit(10**5 + 5)

N = int(input())

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

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

Q, K = map(int, input().split())
xy = []
for _ in range(Q):
    xy.append(tuple(map(int, input().split())))

d = [-1 for _ in range(N+1)]


def dfs(s: int):
    for v, c in g[s]:
        if d[v] == -1:
            d[v] = d[s] + c
            dfs(v)


d[K] = 0
dfs(K)

for x, y in xy:
    print(d[x]+d[y])
Buggy code:
sys.setrecursionlimit(10000)

N = int(input())

edges = {n: [] for n in range(N+1)}

for _ in range(N-1):
    a, b, c = map(int, input().split())

    edges[a].append((b, c))
    edges[b].append((a, c))

Q, K = map(int, input().split())
xy = []
for _ in range(Q):
    xy.append(tuple(map(int, input().split())))


d = [-1] * (N+1)
d[K] = 0


def dfs(n: int):
    for m, c in edges[n]:
        if d[m] == -1:
            d[m] = d[n] + c
            dfs(m)


dfs(K)

for x, y in xy:
    print(d[x] + d[y])


Fixed code:
import sys
sys.setrecursionlimit(100005)

N = int(input())

edges = {n: [] for n in range(N+1)}

for _ in range(N-1):
    a, b, c = map(int, input().split())

    edges[a].append((b, c))
    edges[b].append((a, c))

Q, K = map(int, input().split())
xy = []
for _ in range(Q):
    xy.append(tuple(map(int, input().split())))


d = [-1] * (N+1)
d[K] = 0


def dfs(n: int):
    for m, c in edges[n]:
        if d[m] == -1:
            d[m] = d[n] + c
            dfs(m)


dfs(K)

for x, y in xy:
    print(d[x] + d[y])
Buggy code:
from collections import deque
n = int(input())
G = [deque([]) for _ in range(n)]
for i in range(n - 1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append([b, c])
    G[b].append([a, c])
dist = [-1] * n

def dfs(v):
    cnt = 0
    stack = [[v, 0]]
    while stack:
        v = stack[-1]
        if len(G[v[0]]) > 0:
            w = G[v[0]].popleft()
            if dist[w[0]] == -1:
                dist[w[0]] = v[1] + w[1]
                stack.append(w)
        else: stack.pop()

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

Fixed code:
from collections import deque
n = int(input())
G = [deque([]) for _ in range(n)]
for i in range(n - 1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append([b, c])
    G[b].append([a, c])
dist = [-1] * n

def dfs(v):
    cnt = 0
    stack = [[v, 0]]
    while stack:
        x = stack[-1]
        if len(G[x[0]]) > 0:
            w = G[x[0]].popleft()
            if dist[w[0]] == -1:
                dist[w[0]] = w[1] + dist[x[0]]
                stack.append(w)
        else: stack.pop()

q, k = map(int, input().split())
k -= 1
dist[k] = 0
dfs(k)
for i in range(q):
    x, y = map(int, input().split())
    print(dist[x - 1] + dist[y - 1])
#print(dist)
Buggy code:
n = int(input())
graph = [[] for _ in range(n+1)]
for _ in range(n-1):
  a, b, c = map(int, input().split())
  graph[a].append((b, c))
  graph[b].append((a, c))
q, k = map(int, input().split())
xy = []
for _ in range(q):
  x, y = map(int, input().split())
  xy.append((x, y))

l = [0]*(n+1)
def DFS(node, p_node, length):
  l[node] = length
  for i, j in graph[node]:
    if i==p_node:
      continue
    DFS(i, node, length+j)
DFS(k, -1, 0)
for x, y in xy:
  print(0)

Fixed code:
import sys
n = int(input())
graph = [[] for _ in range(n)]
for _ in range(n-1):
    a, b, c = map(int, input().split())
    a-=1
    b-=1
    graph[a].append((b, c))
    graph[b].append((a, c))
q, k = map(int, input().split())
k -= 1
xy = []
for _ in range(q):
    x, y = map(int, input().split())
    x-=1
    y-=1
    xy.append((x, y))

def DFS(node, p_node, length):
    l[node] = length
    for i, j in graph[node]:
        if i==p_node:
            continue
        DFS(i, node, length+j)
sys.setrecursionlimit(2 * n)
l = [0]*n
DFS(k, -1, 0)
for x, y in xy:
    print(l[x]+l[y])
Buggy code:
N = int(input())
edge = [[] for i in range(N)]
for i in range(N-1):
    a, b, c = map(int, input().split())
    edge[a-1].append((c, b-1))
    edge[b-1].append((c, a-1))
Q, K = map(int, input().split())
query = [tuple(map(int, input().split())) for _ in range(Q)]

import heapq

def dijkstra_heap(s):
    d = [10**12] * N
    used = [False] * N
    d[s] = 0
    used[s] = True
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist, e)
    while len(edgelist):
        min_edge = heapq.heappop(edgelist)
        if used[min_edge[1]]:
            continue
        v = min_edge[1]
        d[v] = min_edge[0]
        used[v] = True
        for e in edge[v]:
            heapq.heappush(edgelist, [e[0] + d[v], e[1]])
    return d

dist = dijkstra_heap(K-1)
for q in query:
    x, y = q[0], q[1]
    x -= 1
    y -= 1
    print(dist[x] + dist[y])

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

import heapq

def dijkstra_heap(s):
    d = [10**12] * N
    used = [False] * N
    d[s] = 0
    used[s] = True
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist, e)
    while len(edgelist):
        min_edge = heapq.heappop(edgelist)
        if used[min_edge[1]]:
            continue
        v = min_edge[1]
        d[v] = min_edge[0]
        used[v] = True
        for e in edge[v]:
            heapq.heappush(edgelist, (e[0] + d[v], e[1]))
    return d

dist = dijkstra_heap(K-1)
for q in query:
    x, y = q[0], q[1]
    x -= 1
    y -= 1
    print(dist[x] + dist[y])
Buggy code:
import sys
input = sys.stdin.readline

data = int(input())
E = [[] for _ in range(data)]
for _ in range(data-1):
  a, b, c = list(map(int, input().split()))
  E[a-1].append((b-1,c))
  E[b-1].append((a-1,c))

nim, mike = list(map(int, input().split()))

queue = [mike-1]
dist = [None]*data
dist[K-1] = 0
while queue:
  queue_new = []
  for q in queue:
    for e in E[q]:
      if dist[e[0]] is not None: continue
      dist[e[0]] = dist[q] + e[1]
      queue_new.append(e[0])
  queue = queue_new

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

Fixed code:
import sys
input = sys.stdin.readline

N = int(input())
E = [[] for _ in range(N)]
for _ in range(N-1):
  a, b, c = list(map(int, input().split()))
  E[a-1].append((b-1,c))
  E[b-1].append((a-1,c))

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

queue = [K-1]
dist = [None]*N
dist[K-1] = 0
while queue:
  queue_new = []
  for q in queue:
    for e in E[q]:
      if dist[e[0]] is not None: continue
      dist[e[0]] = dist[q] + e[1]
      queue_new.append(e[0])
  queue = queue_new

for _ in range(Q):
  x, y = list(map(int, input().split()))
  print(dist[x-1]+dist[y-1])
Buggy code:
N=int(input())
abc=[tuple(map(int,input().split())) for i in range(N-1)]
g=[set() for i in range(N)]
for a,b,c in abc:
  g[a-1].add((b-1,c))
  g[b-1].add((a-1,c))
memo=[0]*N
memo[K-1]=0
Q,K=map(int,input().split())
import collections
q=collections.deque([(K-1,0)])
while(q):
  x,w = q.popleft()
  for a,b in g[x]:
    if memo[a]==-1:
      memo[a]=w+b
      q.append((a,w+b))
  
for i in range(Q):
  x,y=map(int,input().split())
  print(memo[a-1]+memo[b-1])

Fixed code:
N=int(input())
abc=[tuple(map(int,input().split())) for i in range(N-1)]
g=[set() for i in range(N)]
for a,b,c in abc:
  g[a-1].add((b-1,c))
  g[b-1].add((a-1,c))
memo=[-1]*N

Q,K=map(int,input().split())
import collections
q=collections.deque([(K-1,0)])
memo[K-1]=0
while(q):
  
  x,w = q.popleft()
  for a,b in g[x]:
    if memo[a]==-1:
      memo[a]=w+b
      q.append((a,w+b))
  
for i in range(Q):
  x,y=map(int,input().split())
  print(memo[x-1]+memo[y-1])
Buggy code:
def dijkstra(n, edges, s):
    min_dist = [float('inf')] * n
    min_dist[s] = 0
    next_v = []
    heapq.heappush(next_v, (0, s))
    path = [-1] * n
    
    while True:
        if len(next_v) == 0: break
        d, v = heapq.heappop(next_v)
        
        for u, d in edges[v]:
            if min_dist[u] > min_dist[v] + d:
                min_dist[u] = min_dist[v] + d
                heapq.heappush(next_v, (min_dist[u], u))
                path[u] = v
                
    return min_dist, path

n = int(input())
edges = [[] for _ in range(n)]

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

dist = [None] * n
for i in range(n):
    dist[i], _ = dijkstra(n, edges, i)

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

Fixed code:
import heapq
def dijkstra(n, edges, s):
    min_dist = [float('inf')] * n
    min_dist[s] = 0
    next_v = []
    heapq.heappush(next_v, (0, s))
    path = [-1] * n
    
    while True:
        if len(next_v) == 0: break
        d, v = heapq.heappop(next_v)
        
        for u, d in edges[v]:
            if min_dist[u] > min_dist[v] + d:
                min_dist[u] = min_dist[v] + d
                heapq.heappush(next_v, (min_dist[u], u))
                path[u] = v
                
    return min_dist, path

n = int(input())
edges = [[] for _ in range(n)]

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


q, k = map(int, input().split())
dist, _ = dijkstra(n, edges, k-1)

for _ in range(q):
    x, y = map(int, input().split())
    print(dist[x-1] + dist[y-1])
Buggy code:
from collections import deque
N = int(input())
edge = [[] for _ in range(N+1)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    edge[a].append([b, c])
    edge[b].append([a, c])


def BFS(x, y):
    visited = [False for _ in range(N+1)]
    next_N = [False for _ in range(N+1)]
    INF = float('inf')
    dest = [INF for _ in range(N+1)]
    d = deque()
    visited[x] = True
    dest[x] = 0
    for B, C in edge[x]:
        d.append([B, C])
        next_N[B] = True
        dest[B] = dest[x] + C
    while d:
        now = d.popleft()
        visited[now[0]] = True
        for B, C in edge[now[0]]:
            if visited[B] == False and next_N[B] == False:
                d.append([B, C])
                next_N[B] = True
                dest[B] = dest[now[0]] + C
    print(dest)
    return dest[y]

Q, K = map(int, input().split())
xy = [list(map(int, input().split())) for _ in range(Q)]
for X, Y in xy:
    print(BFS(X, K) + BFS(K, Y))


Fixed code:
from collections import deque
N = int(input())
edge = [[] for _ in range(N+1)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    edge[a].append([b, c])
    edge[b].append([a, c])


def BFS(x):
    visited = [False for _ in range(N+1)]
    next_N = [False for _ in range(N+1)]
    INF = float('inf')
    dest = [INF for _ in range(N+1)]
    d = deque()
    visited[x] = True
    dest[x] = 0
    for B, C in edge[x]:
        d.append([B, C])
        next_N[B] = True
        dest[B] = dest[x] + C
    while d:
        now = d.popleft()
        visited[now[0]] = True
        for B, C in edge[now[0]]:
            if visited[B] == False and next_N[B] == False:
                d.append([B, C])
                next_N[B] = True
                dest[B] = dest[now[0]] + C
    return dest

Q, K = map(int, input().split())
xy = [list(map(int, input().split())) for _ in range(Q)]
DEST = BFS(K)
for X, Y in xy:
    print(DEST[X] + DEST[Y])
Buggy code:
fron collections import deque
n=int(input())
g=[[] for i 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])
q,k=map(int,input().split())
qu=deque([[k-1,0]])
v=[0]*n
d=[0]*n
while len(qu)>0:
    s=qu.popleft()
    v[s[0]]=1
    for j in g[s[0]]:
        if not v[j[0]]:
            v[j[0]]=1
            d[j[0]]=s[1]+j[1]
            qu+=[[j[0],d[j[0]]]]
for i in range(q):
    x,y=map(int,input().split())
    print(d[x-1]+d[y-1])


Fixed code:
from collections import deque
n=int(input())
g=[[] for i 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])
q,k=map(int,input().split())
qu=deque([[k-1,0]])
v=[0]*n
d=[0]*n
while len(qu)>0:
    s=qu.popleft()
    v[s[0]]=1
    for j in g[s[0]]:
        if not v[j[0]]:
            v[j[0]]=1
            d[j[0]]=s[1]+j[1]
            qu+=[[j[0],d[j[0]]]]
for i in range(q):
    x,y=map(int,input().split())
    print(d[x-1]+d[y-1])