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

using namespace std;

typedef pair<int,int>P;

struct E{

  long long t,c;

  bool operator<(const E&a)const{return c>a.c;}

};

vector<E>G[1<<17];

long long z[1<<17];

int n,a,b,c,q,k,x,y,i,j;

void dijkstra(){

  priority_queue<E>Q;

  for(int i=0;i<1<<17;)z[i++]=1ll<<60;

  z[k]=0;

  Q.push(E{k,0});

  while(Q.size()){

    E p=Q.top();Q.pop();

    if(z[p.t]<p.c)continue;

    for(i=0;i<G[p.t].size();++i){

      E e=G[p.t][i];

      if(z[e.t]>p.c+e.c)z[e.t]=p.c+e.c,Q.push(E{e.t,z[e.t]});

    }

  }

}

main(){

  for(cin>>n;++i<n;G[a].push_back(E{b,c}),G[b].push_back(E{a,c}))scanf("%d%d%d",&a,&b,&c);

  for(cin>>q>>k,dijkstra();j++<q;cout<<z[x]+z[y]<<endl)scanf("%d%d",&x,&y);

}


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

using namespace std;

struct E{long long t,c;};

vector<E>G[1<<17];

long long z[1<<17];

int n,a,b,c,q,i,j;

void d(int x,int p){

  for(auto &e:G[x])if(e.t-p)z[e.t]=z[x]+e.c,d(e.t,x);

}

main(){

  for(cin>>n;++i<n;G[a].push_back({b,c}),G[b].push_back({a,c}))scanf("%d%d%d",&a,&b,&c);

  for(cin>>q>>n,d(n,0);j++<q;printf("%lld\n",z[a]+z[b]))scanf("%d%d",&a,&b);

}


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

using namespace std;

typedef pair<int,int>P;

struct E{

  long long t,c;

  bool operator<(const E&a)const{return c>a.c;}

};

vector<E>G[1<<17];

long long z[1<<17];

int n,a,b,c,q,k,x,y,i,j;

void dijkstra(){

  priority_queue<E>Q;

  for(int i=0;i<1<<17;)z[i++]=1ll<<60;

  z[k]=0;

  Q.push(E{k,0});

  while(Q.size()){

    E p=Q.top();Q.pop();

    if(z[p.t]<p.c)continue;

    for(i=0;i<G[p.t].size();++i){

      E e=G[p.t][i];

      if(z[e.t]>p.c+e.c)z[e.t]=p.c+e.c,Q.push(E{e.t,z[e.t]});

    }

  }

}

main(){

  for(cin>>n;++i<n;G[a].push_back(E{b,c}),G[b].push_back(E{a,c}))scanf("%d%d%d",&a,&b,&c);

  for(cin>>q>>k,dijkstra();j++<q;printf("%lld\n",z[x]+z[y]))scanf("%d%d",&x,&y);

}


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

using namespace std;

struct E{long long t,c;};

vector<E>G[1<<17];

long long z[1<<17];

int n,a,b,c,q,i,j;

void d(int x,int p){

  for(auto &e:G[x])if(e.t-p)z[e.t]=z[x]+e.c,d(e.t,x);

}

main(){

  for(cin>>n;++i<n;G[a].push_back({b,c}),G[b].push_back({a,c}))scanf("%d%d%d",&a,&b,&c);

  for(cin>>q>>n,d(n,0);j++<q;printf("%lld\n",z[a]+z[b]))scanf("%d%d",&a,&b);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<string>

#include<cstdio>

#include<cmath>

#include<cstring>

#include<algorithm>

#include<vector>

#include<functional>

#include<iomanip>

#include<queue>

#include<cassert>

#include<tuple>

#include<set>

#include<map>

#include<list>

#include<bitset>



#define PB push_back

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

#define DWN(a)  (a).begin(),(a).end(), greater<int>()

#define rep(i, m) for (int i = 0; i < m; i++)

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



#define mod 1000000007



using namespace std;



typedef long long ll;

typedef pair<int, int> P;



const int dx[4] = { 1, 0, -1, 0 };

const int dy[4] = { 0, 1, 0, -1 };

const int INF = (int)1e9;



struct edge { int to; long long cost; };

const int MAX_V = 100010;



vector<edge> tree[MAX_V];

long long depth[MAX_V];



// v: 今いる頂点, p: v の親

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

	depth[v] = d;

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

		edge e = tree[v][i];

		if (e.to == p) { continue; }

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

	}

}



int main()

{

	int n;

	cin >> n;

	rep(i, n - 1) {

		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);

	rep(i, q) {

		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;

typedef vector<ll> vl;

typedef vector<vector<ll>> vvl;

typedef pair<ll, ll> P;

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

#define REP(i, a, b) for(ll i = a; i <= b; i++)

#define out(x) std::cout << x << '\n'

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

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

#define pb push_back

#define sz(x) ((int)(x).size())

#define chmax(x, y) x = max(x, y)

#define chmin(x, y) x = min(x, y)

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; }

const int dx[4] = { 1, 0, -1,  0 };

const int dy[4] = { 0, 1,  0, -1 };

const int INF = INT_MAX / 2;

const ll  inf = LLONG_MAX / 2;

const int mod = 1000000007;

const int MAX_N = 101010;

const long double PI = acos(-1);



using Edge = pair<int, ll>;

using Graph = vector<vector<Edge>>;



Graph G;

vector<ll> dist;



void dfs(int cur, int pre, ll sum) {

	dist[cur] = sum;

	

	for (auto to : G[cur]) {

		if (to.first != pre) {

			dfs(to.first, cur, sum + to.second);

		}

	}

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);

	cout << fixed << setprecision(10);

	/*------------------------------*/



	int n;

	cin >> n;

	

	G.resize(n);

	

	rep(i, n - 1) {

		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--;



	dist.resize(n, 0);



	dfs(k, -1, 0);



	rep(i, q) {

		int x, y;

		cin >> x >> y;

		x--; y--;

		out(dist[x] + dist[y]);

	}



	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

//#include <math.h>

#include <set>

#include<iomanip>

#include <algorithm>

#include <vector>

#include <string>

#include <map>

#include <utility>

#include <queue>

#define INF 1e9+7

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

#define NO cout<<"NO"<<endl;

#define YES cout << "YES"<<endl;

#define No cout << "No"<<endl;

#define Yes cout << "Yes"<<endl;

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

#define P pair<int,int>

#define space ' '

#define pll pair<ll,ll>

using namespace std;

typedef long long int ll;

typedef unsigned long long int ull;



//(int)'a'は97 (int)'A'は65 (int)'1'は49

//おまじない



const int maxn = 100000;



int main(){



  int n,q,k;

  vector<vector<int> > v(maxn);

  map<pair<int,int>,ll > m;

  int x[maxn],y[maxn];

  bool used[maxn]={0};

  ll dis[maxn]={0};

  queue<int> que;



  cin >> n;

  rep(i,n-1){

    int a,b;

    ll c;

    cin >> a>>b>>c;

    a--;b--;

    m[make_pair(a,b)]=m[make_pair(b,a)]=c;

    v[a].push_back(b);v[b].push_back(a);

  }



  cin >> q>>k;

  k--;

  rep(i,q){

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

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

  }



  que.push(k);

  used[k]=true;

  dis[k]=0;



  while(!que.empty()){

    int arg = que.front();que.pop();

    rep(i,v[arg].size()){

      int next = v[arg][i];

      if(!used[next]){

        dis[next]=dis[arg]+m[make_pair(arg,next)];

        used[next]=true;

        que.push(next);

      }

    }

  }



  rep(i,q){

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

  }



  return 0;

}


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

#include <map>

#include <algorithm>

#include <vector>

#include <string>

#include <utility>

#include <queue>

#define INF 1e16

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

#define NO cout<<"NO"<<endl;

#define YES cout << "YES"<<endl;

#define No cout << "No"<<endl;

#define Yes cout << "Yes"<<endl;

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

using namespace std;

typedef long long int ll;

typedef unsigned long long int ull;



const int maxn = 100000;



int n, k, q;

ll dist[maxn];

bool used[maxn];

vector<pair<int, int>> p;

vector<vector<pair<ll, ll>>> v(maxn);  //行先とコスト



int main() {



	cin >> n;

	rep(i, n - 1) {

		ll a, b, c;

		cin >> a >> b >> c;

		a--; b--;

		v[a].push_back(pair<ll, ll>(b, c));

		v[b].push_back(pair<ll, ll>(a, c));

	}



	cin >> q >> k;



	rep(i, q) {

		int a, b;

		cin >> a >> b;

		a--; b--;

		p.push_back(pair<int, int>(a, b));

	}



	fill(used, used + n, false);

	fill(dist, dist + n, INF);

	dist[k - 1] = 0; used[k - 1] = true;



	queue<int> que;

	que.push(k - 1);



	while (que.size()) {

		int t = que.front(); que.pop();

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

			if (!used[v[t][i].first]) {

				dist[v[t][i].first] = dist[t] + v[t][i].second;

				used[v[t][i].first] = true;

				que.push(v[t][i].first);

			}

		}

	}



	rep(i, p.size()) {

		cout << dist[p[i].first] + dist[p[i].second] << endl;

	}



	return 0;

}


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

using namespace std;

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

typedef long long ll;

#define dump(x) cerr<<#x<<"="<<x<<endl

using P = pair<int, 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(void) {

 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;

 }



#if 0

void dfs(int v, int p, vector<vector<P>> g, vector<ll> &dist, ll d){

    dist.at(v) = d;

    for(auto &u: g.at(v)){

        if(dist.at(u.first)!=-1) continue;

        dfs(u.first, v, g, dist, d+u.second);

    }

}



int main() {

    int n; cin >> n;

    vector<vector<P>> g(n+1);

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

    

    rep(i, n-1){

        int a, b, c; cin >> a >> b >> c;

        g.at(a).push_back(make_pair(b, c));

        g.at(b).push_back(make_pair(a, c));

    }



    int q, k; cin >> q >> k;

    dfs(k, -1, g, dist, 0);



    rep(i, q){

        int x, y; cin >> x >> y;

        cout << dist.at(x) + dist.at(y) << endl;

    }

    



    return 0;

}

#endif

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

using namespace std;

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

typedef long long ll;

#define dump(x) cerr<<#x<<"="<<x<<endl

using P = pair<int, ll>;

 

void dfs(int v, int p, vector<vector<P>> &g, vector<ll> &dist, ll d){

    dist.at(v) = d;

    for(auto &u: g.at(v)){

        if(dist.at(u.first)!=-1) continue;

        dfs(u.first, v, g, dist, d+u.second);

    }

}

 

int main() {

    int n; cin >> n;

    vector<vector<P>> g(n+1);

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

    

    rep(i, n-1){

        int a, b, c; cin >> a >> b >> c;

        g.at(a).push_back(make_pair(b, c));

        g.at(b).push_back(make_pair(a, c));

    }

 

    int q, k; cin >> q >> k;

    dfs(k, -1, g, dist, 0);

 

    rep(i, q){

        int x, y; cin >> x >> y;

        cout << dist.at(x) + dist.at(y) << endl;

    }

    

 

    return 0;

}

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

using namespace std;

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

typedef long long ll;

#define dump(x) cerr<<#x<<"="<<x<<endl

using P = pair<int, ll>;



vector<vector<P>> g(100010);

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



#if 1

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

    dist.at(v) = d;

    for(auto &u: g.at(v)){

        if(dist.at(u.first)!=-1) continue;

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

    }

}



int main() {

    int n; cin >> n;

    

    rep(i, n-1){

        int a, b, c; cin >> a >> b >> c;

        g.at(a).push_back(make_pair(b, c));

        g.at(b).push_back(make_pair(a, c));

    }



    int q, k; cin >> q >> k;

    dfs(k, -1, 0);



    rep(i, q){

        int x, y; cin >> x >> y;

        cout << dist.at(x) + dist.at(y) << endl;

    }

    



    return 0;

}

#endif

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

using namespace std;

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

typedef long long ll;

#define dump(x) cerr<<#x<<"="<<x<<endl

using P = pair<int, ll>;

 

void dfs(int v, int p, vector<vector<P>> &g, vector<ll> &dist, ll d){

    dist.at(v) = d;

    for(auto &u: g.at(v)){

        if(dist.at(u.first)!=-1) continue;

        dfs(u.first, v, g, dist, d+u.second);

    }

}

 

int main() {

    int n; cin >> n;

    vector<vector<P>> g(n+1);

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

    

    rep(i, n-1){

        int a, b, c; cin >> a >> b >> c;

        g.at(a).push_back(make_pair(b, c));

        g.at(b).push_back(make_pair(a, c));

    }

 

    int q, k; cin >> q >> k;

    dfs(k, -1, g, dist, 0);

 

    rep(i, q){

        int x, y; cin >> x >> y;

        cout << dist.at(x) + dist.at(y) << endl;

    }

    

 

    return 0;

}

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

using namespace std;

#define int long long



const int N = 1e5+10;

struct edge{ int to,cost; };

vector<edge> G[N];

int d[N];



void dfs(int s,int prev,int dis){

  d[s] += dis;

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

    if(G[s][i].to != prev) dfs(G[s][i].to,s,dis + G[s][i].cost);

  }

}



signed main(){



  int n; cin >> n;

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

    int a,b,c; cin >> a >> b >> c;

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

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

  }



  int q,k; cin >> q >> k;

  dfs(k,-1,0);

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

    int x,y; cin >> x >> y;

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

  }



  return 0;

}


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

using namespace std;

#ifdef LOCAL_DEBUG

  #include "LOCAL_DEBUG.hpp"

#endif

#define int long long



struct edge{ int to,cost; };

vector<int> dikstra(int s,vector<vector<edge>> G){

  typedef pair<int, int> P;

  const int INF = 1LL << 60;



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

  vector<int> d(G.size(),INF); //sからの最短距離

  d[s] = 0;

  que.push({0,s}); //{最短距離,頂点}



  while( !que.empty() ){

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

    int v = p.second;

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

    for(auto e : G[v]){

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

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

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

      }

    }

  }

  return d;

}



signed main(){



  int n, k, q; cin >> n;

  vector<vector<edge>> G(n);

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

    int a, b, c; cin >> a >> b >> c;

    a--, b--;

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

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

  }



  cin >> q >> k;

  k--;

  auto dp = dikstra(k, G);

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

    int x, y; cin >> x >> y;

    x--, y--;

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

  }



  return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
/*

    SPFA ````

*/



#include <iostream>

#include <cstring>

#include <algorithm>

#include <queue>



using namespace std;

typedef long long ll;

const int N = 1e6+10;

int n,m,k,q,x,y;

int h[N],w[N],e[N],ne[N],idx;



ll dist[N];

bool st[N];



void add(int a,int b,int c)

{

    e[idx] = b,w[idx] = c,ne[idx] = h[a],h[a] = idx++;

}



void spfa()

{

    memset(dist,0x3f,sizeof dist);

    dist[k] = 0;

    queue<int>q;

    q.push(k);

    

    st[k] = true;

    

    while(q.size())

    {

        int t = q.front();

        q.pop();

        

        st[t] = false;

        

        for(int i = h[t];i!=-1;i=ne[i])

        {

            int j = e[i];

            if(dist[j]>dist[t] + w[i])

            {

                dist[j] = dist[t] + w[i];

                if(!st[j])

                {

                    q.push(j);

                    st[j] = true;

                }

            }



        }

    }

}



int main()

{

    ios::sync_with_stdio(false);

    cin.tie(0);cout.tie(0);

    cin >>n;

    memset(h,-1,sizeof h);

    m = n-1;

    while(m--)

    {

        int a,b,c;

        cin >>a>>b>>c;

        add(a,b,c);

        add(b,a,c);

    }

    

    cin >> q>>k;

    spfa();

    while(q--)

    {

        cin >>x >>y;

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

    }

    return 0;

}

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

using namespace std;

#define int long long

vector<pair<int, int>> graph[100001];

int ans[100001];

signed main() {

    //Ввод графа и вершины start.

	int n;

	cin >> n;

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

		int a, b, c;

		cin >> a >> b >> c;

		--a, --b;

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

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

	}

	int Q, K;

	cin >> Q >> K;

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

        ans[i] = LONG_MAX;

    }

    int start = K - 1;

    ans[start] = 0;



    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;



    q.push({0, start});



    while (!q.empty()) {

        pair<int, int> c = q.top();

        q.pop();



        int dst = c.first, v = c.second;



        if (ans[v] < dst) {

            continue;

        }



        for (pair<int, int> e: graph[v]) {

            int u = e.first, len_vu = e.second;



            int n_dst = dst + len_vu;

            if (n_dst < ans[u]) {

                ans[u] = n_dst;

                q.push({n_dst, u});

            }

        }

    }

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

    	int u, v;

    	cin >> u >> v;

    	u--; v--;

    	cout << ans[u] + ans[v] << '\n';

    }

}

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

using namespace std;



const int limit = 100010;

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

vector<edge> tree[limit];

long long depth[limit];



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

    depth[v] = d;

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

        if(e.to == p) continue;

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

    }

}



int main() {

    cin.tie(0);

    ios::sync_with_stdio(false);



    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"

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

using namespace std;

typedef long long int ll;

typedef pair<int, int> P;

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

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





struct State{

    int at;

    ll cost;

    State(int a, ll c) : at(a), cost(c){}

    bool operator>(const State& s) const{

        return cost > s.cost;

    }

};



struct Edge{

    int to;

    ll cost;

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

};



using Graph = vector<vector<Edge> >;

const ll INF = 1e15;



void dijkstra(int s, const Graph& G, vector<ll>& minc){

    minc.assign(G.size(), INF);

    priority_queue<State, vector<State>, greater<State> > pq;

    pq.emplace(s, 0);

    minc[s] = 0;

    while(!pq.empty()){

        State cur = pq.top();

        pq.pop();

        if(minc[cur.at] < cur.cost) continue;

        for(const Edge& e : G[cur.at]){

            ll cost = cur.cost + e.cost;

            if(minc[e.to] <= cost) continue;

            minc[e.to] = cost;

            pq.emplace(e.to, cost);

        }

    }

}



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    int n;

    cin >> n;

    Graph G(n);

    rep(i,n-1){

        int a, b;

        ll c;

        cin >> a >> b >> c;

        --a; --b;

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

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

    }

    int q, k;

    cin >> q >> k;

    --k;

    vector<ll> minc(n, INF);

    dijkstra(k, G, minc);

    rep(i,q){

        int x, y;

        cin >> x >> y;

        --x; --y;

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

    }

    return 0;

}

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

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

using namespace std;

typedef long long int ll;

typedef pair<int, int> P;

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

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



const int limit = 100005;

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



int n;

vector<edge> tree[limit];

vector<long long> dist;



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

    dist[v] = d;

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

        if(e.to == p) continue;

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

    }

}



vector<long long> calcDist(int s){

    dist = vector<long long>(n);

    dfs(s);

    return dist;

}



int main() {

    cin.tie(0);

    ios::sync_with_stdio(false);

    cin >> n;

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

        int a, b;

		ll 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--;

	vector<ll> distK = calcDist(k);

	rep(i,q){

		int x, y;

		cin >> x >> y;

		x--; y--;

		ll ans = dist[x] + dist[y];

		cout << ans << endl;

	}

    return 0;

}


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

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

using namespace std;

typedef long long int ll;

typedef pair<int, int> P;

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

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





struct State{

    int at;

    ll cost;

    State(int a, ll c) : at(a), cost(c){}

    bool operator>(const State& s) const{

        return cost > s.cost;

    }

};



struct Edge{

    int to;

    ll cost;

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

};



using Graph = vector<vector<Edge> >;

const ll INF = 1e15;



void dijkstra(int s, const Graph& G, vector<ll>& minc){

    minc.assign(G.size(), INF);

    priority_queue<State, vector<State>, greater<State> > pq;

    pq.emplace(s, 0);

    minc[s] = 0;

    while(!pq.empty()){

        State cur = pq.top();

        pq.pop();

        if(minc[cur.at] < cur.cost) continue;

        for(const Edge& e : G[cur.at]){

            ll cost = cur.cost + e.cost;

            if(minc[e.to] <= cost) continue;

            minc[e.to] = cost;

            pq.emplace(e.to, cost);

        }

    }

}



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    int n;

    cin >> n;

    Graph G(n);

    rep(i,n-1){

        int a, b;

        ll c;

        cin >> a >> b >> c;

        --a; --b;

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

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

    }

    int q, k;

    cin >> q >> k;

    --k;

    vector<ll> minc(n, INF);

    dijkstra(k, G, minc);

    rep(i,q){

        int x, y;

        cin >> x >> y;

        --x; --y;

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

    }

    return 0;

}

// Fastest known solution for this problem: