Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
d = [0] * N
INF = float('inf')

cost = [[INF] * N for i in range(N)]
for _ in range(N - 1):
    a, b, c = map(int, input().split())
    cost[a - 1][b- 1] = c
    cost[b- 1][a - 1] = c
    
def dijkstra(s):
    used = [False] * N
    for i in range(N):
        d[i] = INF
    d[s] = 0
    
    while True:
        v = -1
        for u in range(N):
            if used[u] == False and (v == -1 or d[u] < d[v]):
                v = u
        if v == -1:
            break
        used[v] = True
        
        for u in range(N):
            d[u] = min(d[u], d[v] + cost[v][u])
            
Q, K = map(int, input().split())

dijkstra(K - 1)

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

Fixed submission:
import heapq

N = int(input())
d = [0] * N
INF = float('inf')

class Info:
    def __init__(self, to, cost):
        self.to = to
        self.cost = cost
        
G = [[] for i in range(N)]
for _ in range(N - 1):
    a, b, c = map(int, input().split())
    G[a - 1].append(Info(b - 1, c))
    G[b - 1].append(Info(a - 1, c))
    
def dijkstra(s):
    for i in range(N):
        d[i] = INF
    d[s] = 0
    h = []
    heapq.heappush(h, [0, s])
    
    while h != []:
        p = heapq.heappop(h)
        v = p[1]
        if d[v] < p[0]:
            continue
        for i in range(len(G[v])):
            e = G[v][i]
            if d[e.to] > d[v] + e.cost:
                d[e.to] = d[v] + e.cost
                heapq.heappush(h, [d[e.to], e.to])

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

dijkstra(K - 1)

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

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

N = int(input())
d = [0] * N
INF = float('inf')

class Info:
    def __init__(self, to, cost):
        self.to = to
        self.cost = cost
        
G = [[] for i in range(N)]
for _ in range(N - 1):
    a, b, c = map(int, input().split())
    G[a - 1].append(Info(b - 1, c))
    G[b - 1].append(Info(a - 1, c))
    
def dijkstra(s):
    for i in range(N):
        d[i] = INF
    d[s] = 0
    h = []
    heapq.heappush(h, [0, s])
    
    while h != []:
        p = heapq.heappop(h)
        v = p[1]
        if d[v] < p[0]:
            continue
        for i in range(len(G[v])):
            e = G[v][i]
            if d[e.to] > d[v] + e.cost:
                d[e.to] = d[v] + e.cost
                heapq.heappush(h, [d[e.to], e.to])

Q, K = map(int, input().split())
for _ in range(Q):
    ans = 0
    x, y = map(int, input().split())
    dijkstra(x - 1)
    ans += d[K - 1]
    #print(d)
    dijkstra(K - 1)
    ans += d[y - 1]
    #print(d)
    print(ans)

Fixed submission:
import heapq

N = int(input())
d = [0] * N
INF = float('inf')

class Info:
    def __init__(self, to, cost):
        self.to = to
        self.cost = cost
        
G = [[] for i in range(N)]
for _ in range(N - 1):
    a, b, c = map(int, input().split())
    G[a - 1].append(Info(b - 1, c))
    G[b - 1].append(Info(a - 1, c))
    
def dijkstra(s):
    for i in range(N):
        d[i] = INF
    d[s] = 0
    h = []
    heapq.heappush(h, [0, s])
    
    while h != []:
        p = heapq.heappop(h)
        v = p[1]
        if d[v] < p[0]:
            continue
        for i in range(len(G[v])):
            e = G[v][i]
            if d[e.to] > d[v] + e.cost:
                d[e.to] = d[v] + e.cost
                heapq.heappush(h, [d[e.to], e.to])

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

dijkstra(K - 1)

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

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
d = [0] * N
INF = float('inf')

cost = [[INF] * N for i in range(N)]
for _ in range(N - 1):
    a, b, c = map(int, input().split())
    cost[a - 1][b- 1] = c
    cost[b- 1][a - 1] = c
    
def dijkstra(s):
    used = [False] * N
    for i in range(N):
        d[i] = INF
    d[s] = 0
    
    while True:
        v = -1
        for u in range(N):
            if used[u] == False and (v == -1 or d[u] < d[v]):
                v = u
        if v == -1:
            break
        used[v] = True
        
        for u in range(N):
            d[u] = min(d[u], d[v] + cost[v][u])
            
Q, K = map(int, input().split())
for _ in range(Q):
    ans = 0
    x, y = map(int, input().split())
    dijkstra(x - 1)
    ans += d[K - 1]
    #print(d)
    dijkstra(K - 1)
    ans += d[y - 1]
    #print(d)
    print(ans)

Fixed submission:
import heapq

N = int(input())
d = [0] * N
INF = float('inf')

class Info:
    def __init__(self, to, cost):
        self.to = to
        self.cost = cost
        
G = [[] for i in range(N)]
for _ in range(N - 1):
    a, b, c = map(int, input().split())
    G[a - 1].append(Info(b - 1, c))
    G[b - 1].append(Info(a - 1, c))
    
def dijkstra(s):
    for i in range(N):
        d[i] = INF
    d[s] = 0
    h = []
    heapq.heappush(h, [0, s])
    
    while h != []:
        p = heapq.heappop(h)
        v = p[1]
        if d[v] < p[0]:
            continue
        for i in range(len(G[v])):
            e = G[v][i]
            if d[e.to] > d[v] + e.cost:
                d[e.to] = d[v] + e.cost
                heapq.heappush(h, [d[e.to], e.to])

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

dijkstra(K - 1)

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

Buggy python submission (verdict: Time Limit Exceeded):
from scipy.sparse.csgraph import floyd_warshall as wf

N = int(input())
INF = float('inf')

d = [[INF] * (N) for i in range(N)]
for i in range(N):
    d[i][i] = 0
for _ in range(N - 1):
    a, b, c = map(int, input().split())
    d[a - 1][b - 1] = c
    d[b - 1][a - 1] = c
    
wf = wf(d)

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

Fixed submission:
import heapq

N = int(input())
d = [0] * N
INF = float('inf')

class Info:
    def __init__(self, to, cost):
        self.to = to
        self.cost = cost
        
G = [[] for i in range(N)]
for _ in range(N - 1):
    a, b, c = map(int, input().split())
    G[a - 1].append(Info(b - 1, c))
    G[b - 1].append(Info(a - 1, c))
    
def dijkstra(s):
    for i in range(N):
        d[i] = INF
    d[s] = 0
    h = []
    heapq.heappush(h, [0, s])
    
    while h != []:
        p = heapq.heappop(h)
        v = p[1]
        if d[v] < p[0]:
            continue
        for i in range(len(G[v])):
            e = G[v][i]
            if d[e.to] > d[v] + e.cost:
                d[e.to] = d[v] + e.cost
                heapq.heappush(h, [d[e.to], e.to])

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

dijkstra(K - 1)

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