Buggy python submission (verdict: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

long long d[N];
vector<pair<int, int>> g[N];

void dfs(int u, int f, long long w) {
  d[u] = w;
  for (auto &go : g[u]) {
    if (go.first != f) {
      dfs(go.first, u, w + go.second);
    }
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  int q, k;
  scanf("%d %d", &q, &k);
  dfs(k, k, 0);
  while (q--) {
    int u, v;
    scanf("%d %d", &u, &v);
    cout << d[u] + d[v] << endl;
  }
  return 0;
}


Fixed submission:
n = int(input())
g = []
d = [0] * (n + 1)
for i in range(n + 1):
    g.append([]);
for i in range(n - 1):
    u, v, w = map(int, input().split())
    g[u].append([v, w])
    g[v].append([u, w])
q, k = map(int, input().split())
def bfs(u):
    s = []
    s.append(u)
    while s:
        v = s[-1]
        s.pop()
        for go in g[v]:
            if not d[go[0]] and go[0] != u:
                d[go[0]] = d[v] + go[1]
                s.append(go[0])
bfs(k)
while q > 0:
    u, v = map(int, input().split())
    print(d[u] + d[v])
    q -= 1

Buggy python submission (verdict: Runtime Error):
n = int(input())
g = []
d = [0] * (n * 2)
for i in range(n * 2):
    g.append([]);
for i in range(n - 1):
    u, v, w = map(int, input().split())
    g[u].append([v, w])
    g[v].append([u, w])
q, k = map(int, input().split())
def dfs(u, f, w):
    d[u] = w;
    for go in g[u]:
        if go[0] != f:
            dfs(go[0], u, go[1] + w)
dfs(k, k, 0)
while q > 0:
    u, v = map(int, input().split())
    print(d[u] + d[v])
    q -= 1


Fixed submission:
n = int(input())
g = []
d = [0] * (n + 1)
for i in range(n + 1):
    g.append([]);
for i in range(n - 1):
    u, v, w = map(int, input().split())
    g[u].append([v, w])
    g[v].append([u, w])
q, k = map(int, input().split())
def bfs(u):
    s = []
    s.append(u)
    while s:
        v = s[-1]
        s.pop()
        for go in g[v]:
            if not d[go[0]] and go[0] != u:
                d[go[0]] = d[v] + go[1]
                s.append(go[0])
bfs(k)
while q > 0:
    u, v = map(int, input().split())
    print(d[u] + d[v])
    q -= 1

Buggy python submission (verdict: Runtime Error):
n = int(input())
g = []
d = [0] * (n + 1)
for i in range(n + 1):
    g.append([]);
for i in range(n - 1):
    u, v, w = map(int, input().split())
    g[u].append([v, w])
    g[v].append([u, w])
q, k = map(int, input().split())
def dfs(u, f, w):
    d[u] = w;
    for go in g[u]:
        if go[0] != f:
            dfs(go[0], u, go[1] + w)
dfs(k, k, 0)
while q > 0:
    u, v = map(int, input().split())
    print(d[u] + d[v])
    q -= 1


Fixed submission:
n = int(input())
g = []
d = [0] * (n + 1)
for i in range(n + 1):
    g.append([]);
for i in range(n - 1):
    u, v, w = map(int, input().split())
    g[u].append([v, w])
    g[v].append([u, w])
q, k = map(int, input().split())
def bfs(u):
    s = []
    s.append(u)
    while s:
        v = s[-1]
        s.pop()
        for go in g[v]:
            if not d[go[0]] and go[0] != u:
                d[go[0]] = d[v] + go[1]
                s.append(go[0])
bfs(k)
while q > 0:
    u, v = map(int, input().split())
    print(d[u] + d[v])
    q -= 1

Buggy python submission (verdict: Runtime Error):
n = int(input())
g = []
d = [0] * (n + 1)
for i in range(n + 1):
    g.append([]);
for i in range(n - 1):
    u, v, w = map(int, input().split())
    g[u].append([v, w])
    g[v].append([u, w])
q, k = map(int, input().split())
def dfs(u, f, w):
    d[u] = w;
    for go in g[u]:
        if go[0] != f:
            dfs(go[0], u, go[1] + w)
dfs(k, k, 0)
while q > 0:
    u, v = map(int, input().split())
    print(d[u] + d[v])
    q -= 1


Fixed submission:
n = int(input())
g = []
d = [0] * (n + 1)
for i in range(n + 1):
    g.append([]);
for i in range(n - 1):
    u, v, w = map(int, input().split())
    g[u].append([v, w])
    g[v].append([u, w])
q, k = map(int, input().split())
def bfs(u):
    s = []
    s.append(u)
    while s:
        v = s[-1]
        s.pop()
        for go in g[v]:
            if not d[go[0]] and go[0] != u:
                d[go[0]] = d[v] + go[1]
                s.append(go[0])
bfs(k)
while q > 0:
    u, v = map(int, input().split())
    print(d[u] + d[v])
    q -= 1