stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;

typedef long long ll;

typedef pair<ll, int> P;



struct edge {

  int to; ll cost;

  edge(int t, ll c): to(t), cost(c) {}

};



vector<edge> G[100001];

ll d[100001];



void dijkstra(int s) {

  priority_queue<P> que;

  fill(d, d + 100001, 1e18);

  d[s] = 0;

  que.push(P(0, s));



  while (!que.empty()) {

    P p = que.top(); que.pop();

    int v = p.second;

    if (d[v] < p.first) continue;

    for (int i = 0; i < G[v].size(); i++) {

      edge e = G[v][i];

      if (d[e.to] > d[v] + e.cost) {

        d[e.to] = d[v] + e.cost;

        que.push(P(d[e.to], e.to));

      }

    }

  }

}



int main(void) {

  int n;

  cin >> n;



  for (int i = 0; i < n - 1; i++) {

    int a, b, c;

    cin >> a >> b >> c;

    G[a - 1].push_back(edge(b - 1, c));

    G[b - 1].push_back(edge(a - 1, c));

  }



  int Q, K;

  cin >> Q >> K;



  dijkstra(K - 1);



  vector<int> x(Q), y(Q);

  for (int i = 0; i < Q; i++) {

    cin >> x[i] >> y[i];

  }



  for (int i = 0; i < Q; i++) {

    cout << d[x[i] - 1] + d[y[i] - 1] << endl;

  }

  

  return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define int long long

#define endl '\n'

#define FOR(i, a, n) for (int i = (a); i < (n); ++i)

#define REP(i, n) FOR(i, 0, n)

using namespace std;



int N, Q, K;

vector<pair<int, int>> G[100010];

int dis[100010];



void dfs(int v, int pv, int d) {

    dis[v] = d;

    for (auto e : G[v]) if (e.first != pv) {

        dfs(e.first, v, d + e.second);

    }

}



void _main() {

    cin >> N;

    REP (i, N - 1) {

        int a, b, c;

        cin >> a >> b >> c;

        a--; b--;

        G[a].emplace_back(b, c);

        G[b].emplace_back(a, c);

    }

    cin >> Q >> K;

    dfs(K - 1, -1, 0);

    REP (i, Q) {

        int x, y;

        cin >> x >> y;

        x--; y--;

        cout << dis[x] + dis[y] << endl;

    }

}



signed main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cout << fixed << setprecision(10);

    _main();

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii = pair<int, int>;



struct edge {

  int to, cost;

  edge() {}

  edge(int to, int cost): to(to), cost(cost) {}

};



const ll INF = 1LL << 50;

int N;

int a[100010], b[100010], c[100010];

int Q, K;

vector<edge> G[100010];

ll dis[100010];



void bfs() {

  fill_n((ll*)dis, 100010, INF);

  dis[K] = 0;

  queue<int> que;

  que.emplace(K);

  while (!que.empty()) {

    int cur = que.front();

    que.pop();

    for (edge &e : G[cur]) {

      if (dis[e.to] == INF) {

        dis[e.to] = dis[cur] + e.cost;

        que.emplace(e.to);

      }

    }

  }

}



int main() {

  cin.tie(0);

  ios_base::sync_with_stdio(false);

  cout << fixed << setprecision(10);

  

  cin >> N;

  for (int i = 0; i < N - 1; i++) {

    cin >> a[i] >> b[i] >> c[i];

    a[i]--, b[i]--;

    G[a[i]].emplace_back(b[i], c[i]);

    G[b[i]].emplace_back(a[i], c[i]);

  }

  cin >> Q >> K;

  K--;

  bfs();

  for (int i = 0; i < Q; i++) {

    int x, y;

    cin >> x >> y;

    x--, y--;

    cout << dis[x] + dis[y] << endl;

  }



  return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define int long long

#define endl '\n'

#define FOR(i, a, n) for (int i = (a); i < (n); ++i)

#define REP(i, n) FOR(i, 0, n)

using namespace std;



int N, Q, K;

vector<pair<int, int>> G[100010];

int dis[100010];



void dfs(int v, int pv, int d) {

    dis[v] = d;

    for (auto e : G[v]) if (e.first != pv) {

        dfs(e.first, v, d + e.second);

    }

}



void _main() {

    cin >> N;

    REP (i, N - 1) {

        int a, b, c;

        cin >> a >> b >> c;

        a--; b--;

        G[a].emplace_back(b, c);

        G[b].emplace_back(a, c);

    }

    cin >> Q >> K;

    dfs(K - 1, -1, 0);

    REP (i, Q) {

        int x, y;

        cin >> x >> y;

        x--; y--;

        cout << dis[x] + dis[y] << endl;

    }

}



signed main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cout << fixed << setprecision(10);

    _main();

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using pii = pair<int, int>;



struct edge {

  int to, cost;

  edge() {}

  edge(int t, int c): to(t), cost(c) {}

};



const ll INF = 1LL << 50;

int N, Q, K;

int a[100010], b[100010], c[100010];

vector<edge> G[100010];

ll dis[100010];



int main() {

  cin.tie(0);

  ios_base::sync_with_stdio(false);

  cout << fixed << setprecision(10);

  

  cin >> N;

  for (int i = 0; i < N - 1; i++) {

    cin >> a[i] >> b[i] >> c[i];

    a[i]--; b[i]--;

    G[a[i]].emplace_back(b[i], c[i]);

    G[b[i]].emplace_back(a[i], c[i]);

  }

  cin >> Q >> K;

  K--;

  fill_n((ll*)dis, 100010, INF);

  dis[K] = 0;

  queue<int> que;

  que.emplace(K);

  while (!que.empty()) {

    int cur = que.front();

    que.pop();

    for (edge &e : G[cur]) {

      if (dis[e.to] == INF) {

        dis[e.to] = dis[cur] + e.cost;

        que.emplace(e.to);

      }

    }

  }

  for (int i = 0; i < Q; i++) {

    int x, y;

    cin >> x >> y;

    x--; y--;

    cout << dis[x] + dis[y] << endl;

  }



  return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define int long long

#define endl '\n'

#define FOR(i, a, n) for (int i = (a); i < (n); ++i)

#define REP(i, n) FOR(i, 0, n)

using namespace std;



int N, Q, K;

vector<pair<int, int>> G[100010];

int dis[100010];



void dfs(int v, int pv, int d) {

    dis[v] = d;

    for (auto e : G[v]) if (e.first != pv) {

        dfs(e.first, v, d + e.second);

    }

}



void _main() {

    cin >> N;

    REP (i, N - 1) {

        int a, b, c;

        cin >> a >> b >> c;

        a--; b--;

        G[a].emplace_back(b, c);

        G[b].emplace_back(a, c);

    }

    cin >> Q >> K;

    dfs(K - 1, -1, 0);

    REP (i, Q) {

        int x, y;

        cin >> x >> y;

        x--; y--;

        cout << dis[x] + dis[y] << endl;

    }

}



signed main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cout << fixed << setprecision(10);

    _main();

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#define int long long

#define uint unsigned int

#define REP(i, n) for (int i = 0; i < (n); ++i)

#define REPR(i, n) for (int i = (n); i >= 0; --i)

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)

#define FORR(i, a, b) for (int i = (a); i >= (b); --i)

#define ALL(a) (a).begin(), (a).end()

#define ALLR(a) (a).rbegin(), (a).rend()

#define SZ(a) ((int)(a).size())

#define PB(a) push_back(a)

#define EMP emplace

#define EMPB(...) emplace_back(__VA_ARGS__)

#define MP(a, b) make_pair(a, b)

#define MT(...) make_tuple(__VA_ARGS__)

#define Bit(n) (1LL << (n))

using namespace std;

using pii = pair<int, int>;

template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

const int MOD = 1000000007;

const int INF = 1LL << 60;

const double EPS = 1e-10;



struct edge {

  int from, to, cost;

  edge() {}

  edge(int t, int c) : from(-1), to(t), cost(c) {}

  edge(int f, int t, int c): from(f), to(t), cost(c) {}

};



const int MAX_V = 100010;

int N, M;

vector<edge> G[MAX_V];

int dis[MAX_V];



void dijkstra(int s) {

  fill(dis, dis+MAX_V, INF);

  using P = pair<int, int>;

  priority_queue<P, vector<P>, greater<P>> que;

  dis[s] = 0;

  que.emplace(0, s);

  while (!que.empty()) {

    P p = que.top(); que.pop();

    int v = p.second;

    if (dis[v] < p.first) continue;

    for (edge& e : G[v]) {

      if (dis[e.to] > dis[v] + e.cost) {

        dis[e.to] = dis[v] + e.cost;

        que.emplace(dis[e.to], e.to);

      }

    }

  }

}



signed main() {

  cin.tie(0);

  ios_base::sync_with_stdio(false);

  cout << fixed << setprecision(10);

  

  cin >> N;

  REP(i, N-1) {

    int a, b, c;

    cin >> a >> b >> c;

    a--; b--;

    G[a].EMPB(b, c);

    G[b].EMPB(a, c);

  }

  int Q, K;

  cin >> Q >> K;

  dijkstra(--K);

  REP(i, Q) {

    int x, y;

    cin >> x >> y;

    x--; y--;

    cout << dis[x] + dis[y] << endl;

  }



  return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define int long long

#define endl '\n'

#define FOR(i, a, n) for (int i = (a); i < (n); ++i)

#define REP(i, n) FOR(i, 0, n)

using namespace std;



int N, Q, K;

vector<pair<int, int>> G[100010];

int dis[100010];



void dfs(int v, int pv, int d) {

    dis[v] = d;

    for (auto e : G[v]) if (e.first != pv) {

        dfs(e.first, v, d + e.second);

    }

}



void _main() {

    cin >> N;

    REP (i, N - 1) {

        int a, b, c;

        cin >> a >> b >> c;

        a--; b--;

        G[a].emplace_back(b, c);

        G[b].emplace_back(a, c);

    }

    cin >> Q >> K;

    dfs(K - 1, -1, 0);

    REP (i, Q) {

        int x, y;

        cin >> x >> y;

        x--; y--;

        cout << dis[x] + dis[y] << endl;

    }

}



signed main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cout << fixed << setprecision(10);

    _main();

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int limit = 100010;

using edge = struct {int to; ll cost;};



vector<edge> tree[limit];

ll depth[limit];



void dfs(int v, int p, ll d) {

  depth[v] = d;

  for (auto &e : tree[v]) {

    if (e.to == p) {

      continue;

    }

    dfs(e.to, v, d + e.cost);

  }

}



int main() {

  int n;

  cin >> n;



  for (int i = 0; i < n - 1; i++) {

    int a, b, c;

    cin >> a >> b >> c;

    a--, b--;

    tree[a].push_back({b, c});

    tree[b].push_back({a, c});

  }



  int q, k;

  cin >> q >> k;

  k--;



  dfs(k, -1, 0);



  for (int i = 0; i < q; i++) {

    int x, y;

    cin >> x >> y;

    x--, y--;

    cout << depth[x] + depth[y] << endl;

  }

  return 0;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>



using namespace std;

typedef long long ll;



const ll INF = 1e9;



struct edge {int to; ll cost;};

vector<vector<edge> > edges;



void dfs(int v, ll d, vector<vector<pair<int ,ll> > > &G, vector<ll> &list) {

  list[v] = d;

  for (pair<int, int> p : G[v]) {

    if (list[p.first] == -1LL) {

      dfs(p.first, d + p.second, G, list);

    }

  }

}



int main() {

  int n;

  cin >> n;



  vector<vector<pair<int, ll> > > G(n);

  vector<ll> list(n, -1LL);

  for (int i = 0; i < n - 1; i++) {

    int a, b, c;

    cin >> a >> b >> c;

    a--, b--;

    G[a].push_back(make_pair(b, c));

    G[b].push_back(make_pair(a, c));

  }



  int q, k;

  cin >> q >> k;

  k--;



  dfs(k, 0, G, list);



  for (int i = 0; i < q; i++) {

    int x, y;

    cin >> x >> y;

    x--, y--;

    cout << list[x] + list[y] << endl;

  }

  return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef long long lint;

typedef pair<lint,lint> P;

#define rep(i, n) for (lint i = 0; i < (n); i++)

#define INF LLONG_MAX

#define MAX_V 100005

struct edge{lint to,cost;};

lint n;

vector<edge> vec[MAX_V];

lint d[MAX_V];

void dijkstra(lint k);

signed main(){

    cin>>n;

    rep(i,n-1){  

        lint a,b,c;

        cin>>a>>b>>c;

        a--;

        b--;

        vec[a].push_back({b,c});

        vec[b].push_back({a,c});

    }          

    lint q,k;

    cin>>q>>k;

    k--;

    dijkstra(k);

    rep(i,q){

        lint x,y;

        cin>>x>>y; 

        x--;

        y--;

         cout<<d[x]+d[y]<<endl;

    }

    return 0;

}

void dijkstra(lint k){

    priority_queue<P, vector<P>,greater<P> >que;

    fill(d,d+n,INF);

    d[k]=0;

    que.push(P(0,k));

    while(!que.empty()){

        P p=que.top();

        que.pop();

        lint v= p.second;

        if(d[v]<p.first)continue;

        for(lint i=0;i<vec[v].size();i++){

            edge temp=vec[v][i];

            if(d[temp.to]>d[v]+temp.cost){

                d[temp.to]=d[v]+temp.cost;

                que.push(P(d[temp.to],temp.to));

            }

        }

    }

}


// Optimized solution to the same problem (faster):
#include "bits/stdc++.h"

using namespace std;

const int MOD = 1e9 + 7;

typedef long long lint;

#define rep(i, n) for (lint i = 0; i < (n); ++i)

#define repi(i, k, n) for (lint i = (k); i < (lint)(n); ++i)

typedef pair<lint, lint> P;

typedef vector<lint> vi;

typedef vector<vi> vvi;

#define all(x) (x).begin(), (x).end()

#define pb push_back

#define V 100005

struct edge

{

    lint to;

    lint cost;

};

vector<vector<edge>> G;

vi d(V, LLONG_MAX);

void dijstra(lint s)

{

    priority_queue<P, vector<P>, greater<P>> que;

    d[s] = 0;

    que.push(P(d[s], s));

    while (!que.empty())

    {

        P q = que.top();

        que.pop();

        lint v = q.second;

        if (q.first > d[v])

        {

            continue;

        }

        rep(i, G[v].size())

        {

            edge e = G[v][i];

            if (d[e.to] > d[v] + e.cost)

            {

                d[e.to] = d[v] + e.cost;

                que.push(P(d[e.to], e.to));

            }

        }

    }

}

int main()

{

    lint n;

    cin >> n;

    G.resize(n);

    rep(i, n - 1)

    {

        lint a, b, c;

        cin >> a >> b >> c;

        a--;

        b--;

        G[a].push_back({b, c});

        G[b].push_back({a, c});

    }

    lint q, k;

    cin >> q >> k;

    k--;

    dijstra(k);

    rep(i, q)

    {

        lint x, y;

        cin >> x >> y;

        x--;

        y--;

        cout << d[x] + d[y] << endl;

    }

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, a, b) for (int i = a; i < b; i++)

#define all(v) v.begin(), v.end()

const int MOD = 1e9 + 7;

const int INF = 1e9;

//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122



ll n;

vector<vector<pair<ll, ll>>> e(100010);

ll dist[100010];

ll q, k;



void dfs(ll v, ll p, ll cost) {

    dist[v] = dist[p] + cost;

    rep(i, 0, (int)e[v].size()) {

        ll nextv = e[v][i].first;

        ll nextcost = e[v][i].second;

        if (dist[nextv] != -1) continue;

        dfs(nextv, v, nextcost);

    }

}



int main() {

    cin >> n;



    rep(i, 0, n - 1) {

        ll a, b, c;

        cin >> a >> b >> c;

        a--;

        b--;

        e[a].push_back(make_pair(b, c));

        e[b].push_back(make_pair(a, c));

    }



    cin >> q >> k;

    k--;



    rep(i, 0, 100010) dist[i] = -1;

    dist[k] = 0;

    dfs(k, k, 0);



    rep(i, 0, q) {

        ll x, y;

        cin >> x >> y;

        x--;

        y--;

        cout << dist[x] + dist[y] << endl;

    }



    return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, a, b) for (int i = a; i < (int)b; i++)

#define all(v) v.begin(), v.end()

const int MOD = 1e9 + 7;

const int INF = 1e9;

//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122



int main() {

    int n;

    cin >> n;

    vector<pair<int, int>> edge[n];

    rep(i, 0, n - 1) {

        int a, b, c;

        cin >> a >> b >> c;

        a--; b--;

        edge[a].push_back(make_pair(b, c));

        edge[b].push_back(make_pair(a, c));

    }



    int q, k;

    cin >> q >> k;

    k--;

    queue<int> que;

    que.push(k);

    vector<ll> dist(n, -1);

    dist[k] = 0;

    while (!que.empty()) {

        int v = que.front();

        que.pop();

        for (auto pr : edge[v]) {

            int nv = pr.first;

            int c = pr.second;

            if (dist[nv] != -1) continue;

            dist[nv] = dist[v] + c;

            que.push(nv);

        }

    }



    int x[q], y[q];

    rep(i, 0, q) {

        cin >> x[i] >> y[i];

        x[i]--; y[i]--;

    }

    rep(i, 0, q) {

        cout << dist[x[i]] + dist[y[i]] << endl;

    }

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, a, b) for (int i = a; i < b; i++)

#define all(v) v.begin(), v.end()

const int MOD = 1e9 + 7;

const int INF = 1e9;

//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122



ll n;

vector<vector<pair<ll, ll>>> e(100010);

ll dist[100010];

ll q, k;



void dfs(ll v, ll p, ll sum) {

    dist[v] = sum;

    rep(i, 0, (int)e[v].size()) {

        if (e[v][i].first == p) continue;

        dfs(e[v][i].first, v, sum + e[v][i].second);

    }

}



int main() {

    cin >> n;



    rep(i, 0, n - 1) {

        ll a, b, c;

        cin >> a >> b >> c;

        a--;

        b--;

        e[a].push_back(make_pair(b, c));

        e[b].push_back(make_pair(a, c));

    }



    cin >> q >> k;

    k--;



    rep(i, 0, 100010) dist[i] = -1;

    dist[k] = 0;

    dfs(k, k, 0);



    rep(i, 0, q) {

        ll x, y;

        cin >> x >> y;

        x--;

        y--;

        cout << dist[x] + dist[y] << endl;

    }



    return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, a, b) for (int i = a; i < (int)b; i++)

#define all(v) v.begin(), v.end()

const int MOD = 1e9 + 7;

const int INF = 1e9;

//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122



int main() {

    int n;

    cin >> n;

    vector<pair<int, int>> edge[n];

    rep(i, 0, n - 1) {

        int a, b, c;

        cin >> a >> b >> c;

        a--; b--;

        edge[a].push_back(make_pair(b, c));

        edge[b].push_back(make_pair(a, c));

    }



    int q, k;

    cin >> q >> k;

    k--;

    queue<int> que;

    que.push(k);

    vector<ll> dist(n, -1);

    dist[k] = 0;

    while (!que.empty()) {

        int v = que.front();

        que.pop();

        for (auto pr : edge[v]) {

            int nv = pr.first;

            int c = pr.second;

            if (dist[nv] != -1) continue;

            dist[nv] = dist[v] + c;

            que.push(nv);

        }

    }



    int x[q], y[q];

    rep(i, 0, q) {

        cin >> x[i] >> y[i];

        x[i]--; y[i]--;

    }

    rep(i, 0, q) {

        cout << dist[x[i]] + dist[y[i]] << endl;

    }

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, a, b) for (int i = a; i < b; i++)

#define all(v) v.begin(), v.end()

const int MOD = 1e9 + 7;

const int INF = 1e9;

//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122



vector<vector<pair<ll, ll>>> e(100010);

ll dist[100010];



void dfs(ll now, ll parent, ll sum) {

    dist[now] = sum;

    for (auto i : e[now]) {

        ll next = i.first;

        ll next_cost = i.second;

        if (next == parent) continue;

        dfs(next, now, sum + next_cost);

    }

}



int main() {

    ll n;

    cin >> n;

    rep(i, 0, n - 1) {

        ll a, b, c;

        cin >> a >> b >> c;

        a--;

        b--;

        e[a].push_back(make_pair(b, c));

        e[b].push_back(make_pair(a, c));

    }



    ll q, k;

    cin >> q >> k;

    k--;



    dfs(k, -1, 0);



    rep(i, 0, q) {

        ll x, y;

        cin >> x >> y;

        x--;

        y--;

        cout << dist[x] + dist[y] << endl;

    }



    return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, a, b) for (int i = a; i < (int)b; i++)

#define all(v) v.begin(), v.end()

const int MOD = 1e9 + 7;

const int INF = 1e9;

//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122



int main() {

    int n;

    cin >> n;

    vector<pair<int, int>> edge[n];

    rep(i, 0, n - 1) {

        int a, b, c;

        cin >> a >> b >> c;

        a--; b--;

        edge[a].push_back(make_pair(b, c));

        edge[b].push_back(make_pair(a, c));

    }



    int q, k;

    cin >> q >> k;

    k--;

    queue<int> que;

    que.push(k);

    vector<ll> dist(n, -1);

    dist[k] = 0;

    while (!que.empty()) {

        int v = que.front();

        que.pop();

        for (auto pr : edge[v]) {

            int nv = pr.first;

            int c = pr.second;

            if (dist[nv] != -1) continue;

            dist[nv] = dist[v] + c;

            que.push(nv);

        }

    }



    int x[q], y[q];

    rep(i, 0, q) {

        cin >> x[i] >> y[i];

        x[i]--; y[i]--;

    }

    rep(i, 0, q) {

        cout << dist[x[i]] + dist[y[i]] << endl;

    }

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef pair<int,int> P;

#define rep(i,N) for(ll (i)=0;(i)<(N);(i)++)

const int mod = 1000000007;

const ll INF = 100000000000100;



struct edge {

  int to, cost;

};

int n;                            //頂点数

vector<vector<edge>> graph(100010); //重み付きグラフ

vector<ll> d(100010, INF);          //最短距離

vector<bool> used(100010, false); //すでに使われたかのフラグ





void dfs(int depth, int p, ll dist){

  d[depth] = dist;

  for(auto e : graph[depth]){

    if(e.to == p) continue;

    dfs(e.to, depth, dist + e.cost);

  }

}



void dijkstra(int s){

  priority_queue<P, vector<P>, greater<P> > que;

  d[s] = 0;

  que.push(P(0, s));



  while(!que.empty()){

    P p = que.top(); que.pop();

    int v = p.second;

    if(d[v] < p.first) continue;

    for(int i = 0; i < graph[v].size(); ++i){

      edge e = graph[v][i];

      if(d[e.to] > d[v] + e.cost){

        d[e.to] = d[v] + e.cost;

        que.push(P(d[e.to], e.to));

      }

    }

  }

}



int main(){

  cin >> n;

  rep(i, n - 1){

    int a, b, c;

    cin >> a >> b >> c;

    a--; b--; 

    graph[a].push_back(edge{b, c});

    graph[b].push_back(edge{a, c});

  }



  int q, k;

  cin >> q >> k;

  k--;

  //dijkstra(k);

  dfs(k, -1, 0);

  rep(i, q){

    ll x, y;

    cin >> x >> y;

    x--; y--;

    ll ans = 0;

    ans = d[x] + d[y];

    cout << ans << endl;

    //cout << d[x[i]] + d[y[i]] << endl;;

  }

  

}

// Optimized solution to the same problem (faster):
#include<iostream>

#include<vector>

#include<string>

#include<algorithm>

#include<utility>

#include<cmath>

#include<climits>

#include<queue>

#include<stack>

#include<numeric>

#include<set>

#include<iomanip>

#include<map>

#include<type_traits>

#include<tuple>

#include<deque>

#include<cassert>

using namespace std;

typedef long long ll;

typedef pair<ll,int> P;

#define rep(i,N) for(ll (i)=0;(i)<(N);(i)++)

const int mod = 1000000007;

const int INF = 1001001001;

struct edge {

  int to, cost;

};

vector<vector<edge>> graph;

vector<ll> dist;



void dfs(int v, ll d=0, int p=-1) {

  dist[v] = d;

  for(edge u : graph[v]) {

    if(u.to == p) continue;

    dfs(u.to, d + u.cost, v);

  }

}

int main() {

  int n;

  cin >> n;

  graph.resize(n);

  dist.resize(n);

  rep(i, n - 1) {

    int a, b, c;

    cin >> a >> b >> c;

    --a; --b;

    graph[a].push_back((edge){b, c});

    graph[b].push_back((edge){a, c});

  }

  int q, k;

  cin >> q >> k;

  --k;

  dfs(k);

  vector<ll> ans;



  rep(i, q) {

    int x, y;

    cin >> x >> y;

    --x; --y;

    ans.push_back(dist[x] + dist[y]);

  }

  

  rep(i, q) cout << ans[i] << endl;

}


// Fastest known solution for this problem: