Buggy python submission (verdict: Time Limit Exceeded):
#%%
n = int(input())
a, b, c = [0] * n, [0] * n, [0] * n
for i in range(n-1):
    a[i], b[i], c[i] = map(int, input().split())

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

l = [[] for _ in range(n+1)]
for i in range(n-1):
    l[a[i]].append([b[i],c[i]])
    l[b[i]].append([a[i],c[i]])

d = [0] * (n+1)
check = [0] * (n+1)
check[k] = 1
que = [k]

while len(que) > 0:
    tmp = que.pop(0)
    for i in l[tmp]:
        if check[i[0]] == 0:
            que.append(i[0])
            check[i[0]] = 1
            d[i[0]] = d[tmp] + i[1]
        else:
            pass

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

Fixed submission:
#%%
n = int(input())
a, b, c = [0] * n, [0] * n, [0] * n
for i in range(n-1):
    a[i], b[i], c[i] = map(int, input().split())

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

l = [[] for _ in range(n+1)]
for i in range(n-1):
    l[a[i]].append([b[i],c[i]])
    l[b[i]].append([a[i],c[i]])

d = [0] * (n+1)
check = [0] * (n+1)
check[k] = 1
que = [k]
count = 1

while len(que) > 0:
    tmp = que.pop(0)
    for i in l[tmp]:
        if check[i[0]] == 0:
            que.append(i[0])
            check[i[0]] = 1
            d[i[0]] = d[tmp] + i[1]
            count += 1
        else:
            pass
    if count >= n:
        break

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

Buggy python submission (verdict: Runtime Error):
#%%
n = int(input())
a, b, c = [0] * n, [0] * n, [0] * n
for i in range(n-1):
    a[i], b[i], c[i] = map(int, input().split())

q, k = map(int, input().split())
x, y = [0] * q, [0] * q
for i in range(q):
    x[i], y[i] = map(int, input().split())

l = [[] for _ in range(n+1)]
for i in range(n-1):
    l[a[i]].append((b[i],c[i]))
    l[b[i]].append((a[i],c[i]))

d = [0] * (n+1)
check = False * (n+1)
check[k] = True
que = [k]

while len(que) > 0:
    tmp = que.pop(0)
    for i in l[tmp]:
        if check[i[0]] :
            que.append(i[0])
            check[i[0]] = True
            d[i[0]] = d[tmp] + i[1]
        else:
            pass

for i in range(q):
    ans = d[x[i]] + d[y[i]]
    print(ans)

Fixed submission:
#%%
n = int(input())
a, b, c = [0] * n, [0] * n, [0] * n
for i in range(n-1):
    a[i], b[i], c[i] = map(int, input().split())

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

l = [[] for _ in range(n+1)]
for i in range(n-1):
    l[a[i]].append([b[i],c[i]])
    l[b[i]].append([a[i],c[i]])

d = [0] * (n+1)
check = [0] * (n+1)
check[k] = 1
que = [k]
count = 1

while len(que) > 0:
    tmp = que.pop(0)
    for i in l[tmp]:
        if check[i[0]] == 0:
            que.append(i[0])
            check[i[0]] = 1
            d[i[0]] = d[tmp] + i[1]
            count += 1
        else:
            pass
    if count >= n:
        break

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

Buggy python submission (verdict: Runtime Error):
#%%
n = int(input())
a, b, c = [0] * n, [0] * n, [0] * n
for i in range(n-1):
    a[i], b[i], c[i] = map(int, input().split())

q, k = map(int, input().split())
x, y = [0] * q, [0] * q
for i in range(q):
    x[i], y[i] = map(int, input().split())

l = [[] for _ in range(n+1)]
for i in range(n-1):
    l[a[i]].append((b[i],c[i]))
    l[b[i]].append((a[i],c[i]))

d = [0] * (n+1)
check = False * (n+1)
check[k] = 1
que = [k]

while len(que) > 0:
    tmp = que.pop(0)
    for i in l[tmp]:
        if check[i[0]] :
            que.append(i[0])
            check[i[0]] = True
            d[i[0]] = d[tmp] + i[1]
        else:
            pass

for i in range(q):
    ans = d[x[i]] + d[y[i]]
    print(ans)

Fixed submission:
#%%
n = int(input())
a, b, c = [0] * n, [0] * n, [0] * n
for i in range(n-1):
    a[i], b[i], c[i] = map(int, input().split())

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

l = [[] for _ in range(n+1)]
for i in range(n-1):
    l[a[i]].append([b[i],c[i]])
    l[b[i]].append([a[i],c[i]])

d = [0] * (n+1)
check = [0] * (n+1)
check[k] = 1
que = [k]
count = 1

while len(que) > 0:
    tmp = que.pop(0)
    for i in l[tmp]:
        if check[i[0]] == 0:
            que.append(i[0])
            check[i[0]] = 1
            d[i[0]] = d[tmp] + i[1]
            count += 1
        else:
            pass
    if count >= n:
        break

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

Buggy python submission (verdict: Time Limit Exceeded):
#%%
n = int(input())
a, b, c = [0] * n, [0] * n, [0] * n
for i in range(n-1):
    a[i], b[i], c[i] = map(int, input().split())

q, k = map(int, input().split())
x, y = [0] * q, [0] * q
for i in range(q):
    x[i], y[i] = map(int, input().split())

l = [[] for _ in range(n+1)]
for i in range(n-1):
    l[a[i]].append((b[i],c[i]))
    l[b[i]].append((a[i],c[i]))

d = [0] * (n+1)
check = [0] * (n+1)
check[k] = 1
que = [k]

while len(que) > 0:
    tmp = que.pop(0)
    for i in l[tmp]:
        if check[i[0]] == 0:
            que.append(i[0])
            check[i[0]] = 1
            d[i[0]] = d[tmp] + i[1]
        else:
            pass

for i in range(q):
    ans = d[x[i]] + d[y[i]]
    print(ans)

Fixed submission:
#%%
n = int(input())
a, b, c = [0] * n, [0] * n, [0] * n
for i in range(n-1):
    a[i], b[i], c[i] = map(int, input().split())

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

l = [[] for _ in range(n+1)]
for i in range(n-1):
    l[a[i]].append([b[i],c[i]])
    l[b[i]].append([a[i],c[i]])

d = [0] * (n+1)
check = [0] * (n+1)
check[k] = 1
que = [k]
count = 1

while len(que) > 0:
    tmp = que.pop(0)
    for i in l[tmp]:
        if check[i[0]] == 0:
            que.append(i[0])
            check[i[0]] = 1
            d[i[0]] = d[tmp] + i[1]
            count += 1
        else:
            pass
    if count >= n:
        break

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

Buggy python submission (verdict: Time Limit Exceeded):
#%%
n = int(input())
a, b, c = [0] * n, [0] * n, [0] * n
for i in range(n-1):
    a[i], b[i], c[i] = map(int, input().split())

q, k = map(int, input().split())
x, y = [0] * q, [0] * q
for i in range(q):
    x[i], y[i] = map(int, input().split())

l = [[] for _ in range(n+1)]
for i in range(n-1):
    l[a[i]].append((b[i],c[i]))
    l[b[i]].append((a[i],c[i]))

d = [0] * (n+1)
check = [0] * (n+1)
check[k] = 1
que = [k]

while len(que) > 0:
    tmp = que.pop(0)
    tmp_list = l[tmp]
    for i in tmp_list:
        if check[i[0]] == 0:
            que.append(i[0])
            check[i[0]] = 1
            d[i[0]] = d[tmp] + i[1]
        else:
            pass

for i in range(q):
    ans = d[x[i]] + d[y[i]]
    print(ans)

Fixed submission:
#%%
n = int(input())
a, b, c = [0] * n, [0] * n, [0] * n
for i in range(n-1):
    a[i], b[i], c[i] = map(int, input().split())

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

l = [[] for _ in range(n+1)]
for i in range(n-1):
    l[a[i]].append([b[i],c[i]])
    l[b[i]].append([a[i],c[i]])

d = [0] * (n+1)
check = [0] * (n+1)
check[k] = 1
que = [k]
count = 1

while len(que) > 0:
    tmp = que.pop(0)
    for i in l[tmp]:
        if check[i[0]] == 0:
            que.append(i[0])
            check[i[0]] = 1
            d[i[0]] = d[tmp] + i[1]
            count += 1
        else:
            pass
    if count >= n:
        break

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