stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<iostream>

#include<cmath>

#include<cstring>

#include<cstdio>

#include<string>

#include<algorithm>

#include<queue>

#include<stack>

#include<vector>

using namespace std;

#define ll long long

#define inf 0x3f3f3f3f3f3f3f3f

#define mod 1000000009

#define lson l,m,rt<<1

#define rson m+1,r,rt<<1|1

#define max 100010

int n;

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

bool visit[max];

ll d[max];

void dijkstra(int k)

{

	memset(visit, false, sizeof(visit));

	memset(d, inf, sizeof(d));

	priority_queue<pair<int, int> >PQ;

	d[k] = 0;

	visit[k] = true;

	PQ.push(make_pair(0, k));

	while (!PQ.empty())

	{

		pair<int, int>f = PQ.top();

		PQ.pop();

		int u = f.second;

		visit[u] = true;

		if (d[u]<f.first*(-1))

			continue;

		for (int j = 0; j<G[u].size(); j++)

		{

			int v = G[u][j].first;

			if (visit[v] == true)

				continue;

			if (d[v]>d[u] + G[u][j].second)

			{

				d[v] = d[u] + G[u][j].second;

				PQ.push(make_pair(d[v] * (-1), v));

			}

		}

	}

}

int main()

{

	//freopen("Text.txt","r",stdin);

	int u, v, c, q, k;

	scanf("%d", &n);

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

	{

		scanf("%d%d%d", &u, &v, &c);

		G[u].push_back(make_pair(v, c));

		G[v].push_back(make_pair(u, c));

	}

	scanf("%d%d", &q, &k);

	dijkstra(k);

	for (int i = 1; i <= q; i++)

	{

		scanf("%d%d", &u, &v);

		printf("%lld\n", d[u] + d[v]);

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.25868062043245x):
// In the name of God



#include <bits/stdc++.h>



using namespace std;



const int N = 1e5+5;



long long d[N];

vector<pair<int,int>> adj[N];



int main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	int n;

	cin >> n;

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

		int a, b, c;

		cin >> a >> b >> c;

		a--;

		b--;

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

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

	}

	int q, k;

	cin >> q >> k;

	k--;

	for(int i = 0; i < n; i++) d[i] = 1e16;

	d[k] = 0;

	priority_queue<pair<long long, int>> pq;

	pq.push(make_pair(0, k));

	while(!pq.empty()) {

		int now = pq.top().second;

		pq.pop();

		for(pair<int,int> pv : adj[now]) {

			if(d[pv.first] > d[now] + pv.second) {

				d[pv.first] = d[now] + pv.second;

				pq.push(make_pair(-d[pv.first], pv.first));

			}

		}

	}

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

		int u, v;

		cin >> u >> v;

		u--;

		v--;

		cout << d[u]+d[v] << "\n";

	}

	return 0;

}
// Slower C++ solution:
#include<stdio.h>

#include<string.h>

#include<algorithm>

#include<vector>

using namespace std;

vector<int> p[100010];

int vis[100010];

long long s[100010];

void dfs(int x,long long y)

{

    int i,j;

    long long k;

    for(i=0;i<p[x].size();i=i+2)

    {

        j=p[x][i];

        k=p[x][i+1];

        if(vis[j]==0)

        {

            vis[j]=1;

            s[j]=y+k;

            dfs(j,y+k);

        }

    }

}

int main()

{

    int n,i,j,k,x,y,z,q;

    long long s1,s2,sum;

    scanf("%d",&n);

    memset(s,0,sizeof(s));

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

    {

        scanf("%d%d%d",&x,&y,&z);

        p[x].push_back(y);

        p[x].push_back(z);

        p[y].push_back(x);

        p[y].push_back(z);

    }

    scanf("%d%d",&q,&k);

    memset(vis,0,sizeof(vis));

    vis[k]=1;

    dfs(k,0);

    while(q--)

    {

        scanf("%d%d",&x,&y);

        s1=s[x];

        s2=s[y];

        sum=s1+s2;

        printf("%lld\n",sum);

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3397330404366004x):
// In the name of God



#include <bits/stdc++.h>



using namespace std;



const int N = 1e5+5;



long long d[N];

vector<pair<int,int>> adj[N];



int main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	int n;

	cin >> n;

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

		int a, b, c;

		cin >> a >> b >> c;

		a--;

		b--;

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

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

	}

	int q, k;

	cin >> q >> k;

	k--;

	for(int i = 0; i < n; i++) d[i] = 1e16;

	d[k] = 0;

	priority_queue<pair<long long, int>> pq;

	pq.push(make_pair(0, k));

	while(!pq.empty()) {

		int now = pq.top().second;

		pq.pop();

		for(pair<int,int> pv : adj[now]) {

			if(d[pv.first] > d[now] + pv.second) {

				d[pv.first] = d[now] + pv.second;

				pq.push(make_pair(-d[pv.first], pv.first));

			}

		}

	}

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

		int u, v;

		cin >> u >> v;

		u--;

		v--;

		cout << d[u]+d[v] << "\n";

	}

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

queue<pair<int,long long> > q;

int n,qu,k;

vector<pair<int,long long> > ga[100100];

int a,b;

long long c;

long long dist[100100];

int main(){

	//freopen("input.txt","r",stdin);

	//freopen("output.txt","w",stdout);

	ios::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);

	cin>>n;

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

		cin>>a>>b>>c;

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

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

		dist[i]=1e18;

	}

	cin>>qu>>k;

	memset(dist,63,sizeof(dist));

	q.push(make_pair(k,0LL));

	dist[k]=0LL;

	while(!q.empty()){

		int x=q.front().first;

		long long di=q.front().second;

		q.pop();

		if(di!=dist[x])

			continue;

		for(int i=0;i<ga[x].size();i++){

			int y=ga[x][i].first;

			long long cst=ga[x][i].second;

			if(dist[y]>di+cst){

				dist[y]=di+cst;

				q.push(make_pair(y,dist[y]));

			}

		}

	}

	while(qu--){

		cin>>a>>b;

		cout<<dist[a]+dist[b]<<endl;

	}

	return 0;

}




// Faster C++ solution for the same problem (measured speedup: 1.2383909195876988x):
// In the name of God



#include <bits/stdc++.h>



using namespace std;



const int N = 1e5+5;



long long d[N];

vector<pair<int,int>> adj[N];



int main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	int n;

	cin >> n;

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

		int a, b, c;

		cin >> a >> b >> c;

		a--;

		b--;

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

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

	}

	int q, k;

	cin >> q >> k;

	k--;

	for(int i = 0; i < n; i++) d[i] = 1e16;

	d[k] = 0;

	priority_queue<pair<long long, int>> pq;

	pq.push(make_pair(0, k));

	while(!pq.empty()) {

		int now = pq.top().second;

		pq.pop();

		for(pair<int,int> pv : adj[now]) {

			if(d[pv.first] > d[now] + pv.second) {

				d[pv.first] = d[now] + pv.second;

				pq.push(make_pair(-d[pv.first], pv.first));

			}

		}

	}

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

		int u, v;

		cin >> u >> v;

		u--;

		v--;

		cout << d[u]+d[v] << "\n";

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <vector>

using namespace std;

using ll = long long;

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.4442169833133978x):
 #include "bits/stdc++.h"



using namespace std;



#define sp(x) cout<<setprecision(x);

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

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

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

#define inf 10000000

#define linf INT64_MAX*0.99

#define print(s) cout<<(s)<<endl

#define lint long long

#define yes "Yes"

#define no "No"



typedef pair<int, int> P;



struct edge{

    lint to, cost;

};



int main(){

    ios::sync_with_stdio(false);

    cin.tie(0);

    int n, q, k;

    cin >> n;

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

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

        lint a, b, c;

        cin >> a >> b >> c;

        --a; --b; //0インデックス

        edge e;

        e.cost = c;//入力した辺の重み

        e.to = b;//入力した辺が行く先

        G[a].push_back(e);//これらをa辺目のところに入れておく

        e.to = a;

        G[b].push_back(e);

    }

    cin >> q >> k;

    --k;

    vector<lint> d(n, -1); // 拡張店のkからの最短距離

    queue<int> que;

    que.push(k);

    d[k] = 0;

    while(!que.empty()){

        int s = que.front();//queの一番先頭（始めに入れたやつ）をｓとおく

        que.pop();//queの銭湯を削除

        for(auto &e : G[s]){// a = [1, 2, 3, 4]  for i, item in enumulate(a):

            if(e.to == s || d[e.to] != -1) continue;

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

            que.push(e.to);

        }

    }

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

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

using ll=long long;

using ld=long double;

using st=string;

using ch=char;

typedef pair<ll,ll> P;

typedef vector<ll> vl;

typedef vector<vl> vvl;

typedef vector<P> vP;

typedef vector<ch> vc;

typedef vector<vc> vvc;

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

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

#define ROF(i,a,b) for(ll i=a;i>=b;i--)

#define per(i,a) ROF(i,a,0)

const ll MOD=1000000007;

const ll MOD2=998244353;

const ld PI=acos(-1);

const ll INF=1e18;

st abc="abcdefghijklmnopqrstuvwxyz";

st ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

struct edge{ll to,cost;};



const ll MXN=100005;

vector<edge> G[MXN];

vector<bool> B(MXN,false);

vl ans(MXN);



void bfs(ll x,ll d){

  B[x]=true;

  ans[x]=d;

  ll k=G[x].size();

  rep(i,k){

    if(B[G[x][i].to]==false){

      bfs(G[x][i].to,G[x][i].cost+d);

    }

  }

}



  

int main() {

  cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);

  ll N;

  cin >> N;

  rep(i,N-1){

    ll a,b,c;

    cin >> a >> b >> c;

    a--;b--;

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

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

  }

  ll Q,K;

  cin >> Q >> K;

  K--;

  bfs(K,0);

  rep(i,Q){

    ll x,y;

    cin >> x >> y;

    x--;y--;

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

  }

}

// Faster C++ solution for the same problem (measured speedup: 4.429233298930024x):
#include <bits/stdc++.h>

using namespace std;



int main(){

  int n;

  scanf("%d", &n);

  vector<vector<pair<int, int>>> g(n);

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

    int a, b, c;

    scanf("%d %d %d", &a, &b, &c);

    a--; b--;

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

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

  }

  int q, k;

  scanf("%d %d", &q, &k);

  k--;

  vector<long> dist(n);

  auto dfs = [&](int from, int at, long d, const auto& dfs) -> void {

    dist[at] = d;

    for(auto i : g[at]) if(i.first != from){

      dfs(at, i.first, d + i.second, dfs);

    }

  };

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

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

    int a, b;

    scanf("%d %d", &a, &b);

    a--; b--;

    printf("%lld\n", dist[a] + dist[b]);

  }

}
// Slower C++ solution:
#include <bits/stdc++.h>

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

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

using namespace std;

typedef long long ll;

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

vector<vector<edge>> tree(100010);

vector<ll> depth(100010);



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;

	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: 4.4726651889933144x):
#include <bits/stdc++.h>

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

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

using namespace std;

typedef long long ll;



struct edge {

    ll to;

    ll cost;

};



using P = pair<ll, ll>;	// <最短距離, 頂点の番号>

void dijkstra(vector<vector<edge>> &g, vector<ll> &dist, ll s) {

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

    dist.assign(dist.size(), 1LL<<60);

    dist[s] = 0;

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

    while (!que.empty()) {

        P p = que.top();

        que.pop();

        ll v = p.second;

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

        for (ll i=0; i<g[v].size(); ++i) {

            edge e = g[v][i];

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

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

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

            }

        }

    }

}



int main() {

	ll n;

	cin >> n;

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

	rep(i, n-1) {

		ll a, b, c;

		cin >> a >> b >> c;

		a--; b--;

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

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

	}

	ll Q, k;

	cin >> Q >> k;

	k--;

	vector<ll> dist(n);

	dijkstra(g, dist, k);

	rep(q, Q) {

		ll x, y;

		cin >> x >> y;

		x--; y--;

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

		printf("%lld\n", ans);

	}

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

const int limit = 100010;

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

vector<edge> tree[limit];

long long int depth[limit];

 

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

    // v = 次の場所

	// p = 前の場所

	queue<edge> Q;

	Q.push({v,0});

    //printf("%d, %d, %lld\n", p+1, v+1, d);

	//スタート地点の枝分かれ箇所数だけ繰り返す。

	while(Q.size() != 0){

		//printf("size: %d",Q.size());

		//puts("");

		edge p = Q.front(); Q.pop();

		depth[p.to] = p.cost;

		//cout << "depth:" << p.to+1 << "=" << p.cost <<endl;

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

			if(depth[tree[p.to][i].to] == 1000000000000000 ) {

				Q.push({tree[p.to][i].to,tree[p.to][i].cost + depth[p.to]});

				//printf("push: {%d, %d}",tree[p.to][i].to+1, tree[p.to][i].cost + depth[p.to]);

				//puts("");

			}

		}

	}

}

 

int main(void) {

	int n;

	cin >> n;

 

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

		depth[i] = 1000000000000000;

	}

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

    }

    //puts("");

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

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

            //cout << i+1 << "," << tree[i][j].to+1 << "," << tree[i][j].cost << endl;

        }

    }

    //puts("");

	int q, k;

	cin >> q >> k;

	k--;

 

	bfs(k, -2, 0);

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

		int x, y;

		cin >> x >> y;

		x--, y--;

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

        //printf("depth[%d] = %lld\ndepth[%d] = %lld\n", x, depth[x], y, depth[y]);

	}

 

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.237744458072906x):
#include <bits/stdc++.h>

using namespace std;

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

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

#define INF2 1000000000000000000

#define INF 2000000000

#define int long long int

struct edge { int to, cost; };

typedef pair<int, int> P;



int V;

vector<edge> G[100010];

int d[100010];



void dijkstra(int s) {

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

    fill(d, d + V + 1, INF2);

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

            }

        }

    }

}



signed main(void)

{

    cin >> V;

    rep(i ,V-1){

        int s, t, c;

        cin >> s >> t >> c;

        G[s].push_back({t, c});

        G[t].push_back({s, c});

    }

    int Q,K;

    cin >> Q >> K;

    dijkstra(K);

    int x[Q], y[Q];

    rep(i, Q) cin >> x[i] >> y[i];

    rep(i ,Q) cout << d[x[i]] + d[y[i]] << endl;

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> P;

typedef long long ll;

typedef long double ld;

const int inf=1e9+7;

const ll longinf=1LL<<60;

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

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

#define F first

#define S second



const int mx=100010;

const ll mod=1e9+7;



vector<vector<int>> g(mx);

vector<ll> dp(mx,-1);

map<P,int> mp;



int main(){

  int n; cin >> n;

  for(int i=1,a,b,c; i<n; i++){

    cin >> a >> b >> c;

    a--; b--;

    g[a].emplace_back(b);

    g[b].emplace_back(a);

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

  }

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



  queue<int> que; que.emplace(k);

  dp[k]=0;



  while(!que.empty()){

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

    for(int u:g[v]){

      if(dp[u] != -1){ continue; }

      dp[u] = dp[v] + mp[make_pair(u,v)];

      que.emplace(u);

    }

  }



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

      int x,y; 

      cin >> x >> y; x--; y--;

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

  }



  return 0;   

}


// Faster C++ solution for the same problem (measured speedup: 4.464826369527308x):
#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> P;

typedef long long ll;

typedef long double ld;

const int inf=1e9+7;

const ll longinf=1LL<<60;

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

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

#define F first

#define S second



const int mx=100010;

const ll mod=1e9+7;



typedef vector<vector<pair<ll,int>>> wgraph;

typedef pair<ll,int> pli;



vector<ll> bfs(wgraph v,int x){

  priority_queue<pli,vector<pli>,greater<pli>> q;

  vector<ll> dist(v.size(),longinf);

  dist[x]=0;

  q.push({0LL,x});

  

  while(q.size()){

    pli p=q.top(); q.pop();

    ll d=p.F, now=p.S;

    if(dist[now]<d){ continue; }

    for(auto to:v[now]){

      if(dist[to.S]>d+to.F){

        dist[to.S] = d+to.F;

        q.push({dist[to.S], to.S});

      }

    }

  }

  return dist;

}



int main(){

  int n;

  cin >> n;

  wgraph v(n);

  rep(i,n-1){

    int x,y,c;

    cin >> x >> y >> c; x--; y--;

    v[x].push_back({c,y});

    v[y].push_back({c,x});

  }

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

  vector<ll> data=bfs(v,k);

  rep(i,q){

    int x,y;

    cin >> x >> y; x--; y--;

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

  }

  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

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

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

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

#define o(a) cout << a << endl

#define int long long

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

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

using namespace std;

typedef pair<int, int> P;

const int mod = 1000000007;

const int INF = 1e16;

struct edge{

    int to, cost;

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

};

const int V = 100010;

vector<edge> G[100010];

int d[100010];



void dijkstra(int s){

    fill(d, d + V, INF);//V, INF

    d[s] = 0;

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

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

    while(!q.empty()){

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

        int from = p.second;

        int cost = p.first;

        if(d[from] < cost) continue;

        rep(i, 0, G[from].size()){

            edge e = G[from][i];

            int next = e.to;

            int newCost = e.cost + cost;

            if(d[next] > newCost){

                d[next] = newCost;

                q.push(P(newCost, next));

            }

        }

    }

}



signed main(){

    int n, q, k;

    cin >> n;

    rep(i, 0, n - 1){

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

    dijkstra(k);

    rep(i, 0, q){

        int x, y;

        cin >> x >> y;

        x--; y--;

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

    }

}

// Faster C++ solution for the same problem (measured speedup: 1.2348805118770123x):
#include <bits/stdc++.h>

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

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

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

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

#define int long long

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

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

using namespace std;

typedef pair<int, int> P;

const int mod = 1000000007;

const int INF = 1e12;



int n, q, k;

vector<P> G[100010];

int d[100010];



void dfs(int now, int pre, int l){

    d[now] = l;

    rep(i, 0, G[now].size()){

        int next = G[now][i].first;

        if(pre == next) continue;

        dfs(next, now, l + G[now][i].second);

    }

}



signed main(){

    ios::sync_with_stdio(false);

    cin.tie(0);

    cin >> n;

    rep(i, 0, n - 1){

        int a, b, c;

        cin >> a >> b >> c;

        a--; b--;

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

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

    }

    cin >> q >> k;

    k--;

    dfs(k, -1, 0);

    rep(i, 0, q){

        int x, y;

        cin >> x >> y;

        x--; y--;

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

    }

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <cmath>

#include <iomanip>

using namespace std;



bool eq(double x,double y){

  if(abs(x-y)<0.00001*y)return true;

  else return false;

}



int main(void){

    long long q;

    double x[100], x1;

    while(cin >> q){

        if(q == -1) break;

        x[1] = q/2.0;

        for(int i = 2; ; i++){

            x[i] = x[i-1]-(x[i-1]*x[i-1]*x[i-1]-(double)q)/(3*x[i-1]*x[i-1]);

            if(eq(x[i]*x[i]*x[i],(double)q)){

                x1 = x[i]; 

                break;

            }

        }

        cout <<fixed;

        cout << setprecision(7) << x1 << endl;

    } 

    return 0;

}




// Faster C++ solution for the same problem (measured speedup: 5.085683880957442x):
#include <stdio.h>

#include <math.h>

int main(void){

    double q; 

    while(scanf("%lf", &q)!= EOF && q != -1) { 

        double x, a;       

        x=q/2;       

            while(1){

                if(fabs(x*x*x-q)<0.00001*q) break;

                x=x-((x*x*x-q)/(3*x*x));

                x = x;

            }          

            printf("%.7f\n",x);     

            }    

                

        return 0; 

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;

typedef long long LL;



const int MAXN = 123456;

LL presum[MAXN];

vector<LL> line[MAXN];

map<LL, int> mp;



LL cal(LL cur, LL mod) {

   if (cur >= mod)

      return (cur - mod) % mod;

   else

      return (mod - cur) % mod;

}



int main() {

    int N, M;

    scanf("%d %d", &N, &M);

    int idx = 0;

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

        int tmp;

        scanf("%d", &tmp);

        presum[i] = presum[i - 1] + tmp;

        presum[i] %= M;

        if (!mp.count(presum[i])) mp[presum[i]] = idx++;

        line[mp[presum[i]]].push_back(i);

    }

    LL ans = 0;

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

        LL tmp = cal(-presum[i - 1], M);

        // printf("i = %d tmp = %lld\n", i, tmp);

        if (mp.count(tmp)) {

           LL cur = mp[tmp];

           auto low = lower_bound(line[cur].begin(), line[cur].end(), i);

           ans += LL(line[cur].end() - low);

           // printf("ans = %lld\n", ans);

        }

    }

    printf("%lld\n", ans);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.1143451237638x):
#include <bits/stdc++.h>



using namespace std;

typedef long long LL;



const int MAXN = 123456;

map<LL, int> mp;



int main() {

    int N, M;

    scanf("%d %d", &N, &M);

    LL ans = 0, presum = 0;

    mp[0] = 1;

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

        int tmp;

        scanf("%d", &tmp);

        presum = (presum + tmp) % M;

        ans += mp[presum];

        mp[presum]++;

    }

    printf("%lld\n", ans);

    return 0;

}