stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.2406768902820888x):
#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;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 3.4784339956233685x):
#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;

}
// Slower C++ solution:
#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));

            }

        }

    }

}


// Faster C++ solution for the same problem (measured speedup: 1.9414682111894803x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 4.428199375251761x):
#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



int main() {

    ll n;

    cin >> n;

    vector<vector<pair<ll, ll>>> e(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--;



    queue<ll> que;

    que.push(k);

    ll dist[n] = {};

    bool visited[n] = {};

    visited[k] = true;

    while (!que.empty()) {

        ll now_v = que.front();

        que.pop();

        for (auto i : e[now_v]) {

            ll next_v = i.first;

            ll next_c = i.second;

            if (visited[next_v]) continue;

            que.push(next_v);

            visited[next_v] = true;

            dist[next_v] = dist[now_v] + next_c;

        }

    }



    rep(i, 0, q) {

        ll x, y;

        cin >> x >> y;

        x--;

        y--;

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

    }

    return 0;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 4.428183256704068x):
#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



int main() {

    ll n;

    cin >> n;

    vector<vector<pair<ll, ll>>> e(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--;



    queue<ll> que;

    que.push(k);

    ll dist[n] = {};

    bool visited[n] = {};

    visited[k] = true;

    while (!que.empty()) {

        ll now_v = que.front();

        que.pop();

        for (auto i : e[now_v]) {

            ll next_v = i.first;

            ll next_c = i.second;

            if (visited[next_v]) continue;

            que.push(next_v);

            visited[next_v] = true;

            dist[next_v] = dist[now_v] + next_c;

        }

    }



    rep(i, 0, q) {

        ll x, y;

        cin >> x >> y;

        x--;

        y--;

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

    }

    return 0;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 1.264261906338447x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 1.2642573044530967x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 4.428480419529512x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 4.428464299958824x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 3.5028188362926067x):
#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;

}