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

  }

  

}

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

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

}


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

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

}


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

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

}

// Faster C++ solution for the same problem (measured speedup: 3.4688750403943414x):
#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 double PI = acos(-1);



vector<vector<pair<ll, ll>>> G;

vector<ll> dist;



void dfs(ll cur, ll pre, ll cost) {

	dist[cur] = cost;

	for (auto to : G[cur]) {

		if (to.first != pre) {

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

		}

	}

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);

	cout << fixed << setprecision(10);

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



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

	}



	ll q, k;

	cin >> q >> k;

	k--;



	dist.resize(n, 0);



	dfs(k, -1, 0);



	rep(i, q) {

		ll x, y;

		cin >> x >> y;

		x--; y--;

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

	}



	return 0;

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

}

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

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

}


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

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

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

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

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

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

}


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

}
// Slower C++ solution:
/*

    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;

}

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

    }

}