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

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<queue>

using namespace std;

#define INF 9e18

const int maxn = 4e5 + 10;

struct road{

	int v;

	int to;

	int next;

}r[maxn];

long long dis[maxn];

int head[maxn],cnt;

bool vis[maxn];

void inti(){

	memset(head, -1, sizeof(head));

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

		dis[i] = INF;

	}

	memset(vis, false, sizeof(vis));

	cnt = 0;

}

void build(long long s, long long e, long long w){

	r[cnt].v = w;

	r[cnt].to = e;

	r[cnt].next = head[s];

	head[s] = cnt++;

}

void spfa(long long s){

	queue<int>q;

	int end, i, top;

	q.push(s);

	vis[s] = true;

	dis[s] = 0;

	while(!q.empty()){

		top = q.front();

		q.pop();

		vis[top] = false;

		for(i = head[top]; i != -1; i = r[i].next){

			end = r[i].to;

			if(dis[end] > dis[top] + r[i].v){

				dis[end] = dis[top] + r[i].v;

				if(!vis[end]){

					q.push(end);

					vis[end] = true;

				}

			}

		}

	}

}

int main()

{

	long long n, s, e, w, q, k, i, a, b;

	scanf("%lld",&n);

	inti();

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

		scanf("%lld %lld %lld", &s, &e, &w);

		build(s, e, w);

		build(e, s, w);

	}

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

	spfa(k);

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

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

		//cout << dis[a] << endl;

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

	}

}

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

#define pb push_back

#define ll long long

#define pll pair<ll,ll>

#define ff first

#define ss second

#define fastio ios_base::sync_with_stdio(false),cin.tie(NULL);

using namespace std;

vector<pll>vec[100006];

ll dist[100006];

ll INF=2000000000000000000;

ll n,a,b,c,q,k;

void dijkstra(ll node)

{

    fill(dist,dist+n+1,INF);

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

    que.push({node,0});

    while(que.size()){

        pll p=que.top();

        que.pop();

        ll u=p.ff,cost=p.ss;

        if(dist[u]<cost)continue;

        for(ll v=0;v<vec[u].size();v++){

            pll x=vec[u][v];

            if(dist[x.ff]>cost+x.ss){

                dist[x.ff]=cost+x.ss;

                que.push({x.ff,dist[x.ff]});

            }

        }

    }

}

int main()

{

    fastio

    cin>>n;

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

        cin>>a>>b>>c;

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

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

    }

    cin>>q>>k;

    dijkstra(k);

    while(q--){

        cin>>a>>b;

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

    }

    return 0;

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

#include <cstdio>

#include <string>

#include <algorithm>

#include <utility>

#include <cmath>

#include <vector>

#include <queue>

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

using namespace std;

typedef long long ll;

ll mod = 1000000007;

const int Vmax = 100010;



struct edge {

    int to;

    ll weight;

};



vector<edge> tree[Vmax];

ll cost_k[Vmax];



void dfs(int v, int parent, ll dist) {

    cost_k[v] = dist;



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

        if(tree[v][i].to == parent) {

            continue;

        }

        dfs(tree[v][i].to, v, dist + tree[v][i].weight);

    }

}



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 << cost_k[x] + cost_k[y] << endl;

    }

    

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.466686623073644x):
#include <iostream>

#include <cstdio>

#include <string>

#include <algorithm>

#include <utility>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <map>

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

using namespace std;

typedef long long ll;

using Graph = vector<vector<ll>>;

const ll mod = 1000000007;



int main() {

    int n;

    cin >> n;



    Graph to(n), cost(n);

    rep(i, n-1) {

        ll a, b, w;

        cin >> a >> b >> w;

        a--;  b--;

        to[a].push_back(b);

        to[b].push_back(a);

        cost[a].push_back(w);

        cost[b].push_back(w);

    }



    int q, K;

    cin >> q >> K;

    K--;



    vector<ll> dist(n, -1);  // dist[i] : 頂点iの根からの距離

    queue<ll> Q;



    const int root = K;  // 根は頂点K

    dist[root] = 0;

    Q.push(root);

    while(!Q.empty()) {

        

        ll v = Q.front();

        Q.pop();



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

            

            ll u = to[v][i];

            

            if(dist[u] != -1) {

                continue;

            }

            

            dist[u] = dist[v] + cost[v][i];

            Q.push(u);

        }

    }



    vector<ll> ans(q);

    rep(i, q) {

        int x, y;

        cin >> x >> y;

        x--;  y--;

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

    }



    rep(i, q) {

        cout << ans[i] << "\n";

    }



    return 0;

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

#include <cstdio>

#include <string>

#include <algorithm>

#include <utility>

#include <cmath>

#include <vector>

#include <queue>

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

using namespace std;

typedef long long ll;

ll mod = 1000000007;

const int Vmax = 100010;



struct edge {

    int to;

    ll weight;

};



vector<edge> tree[Vmax];

ll cost_k[Vmax];



void dfs(int v, int parent, ll dist) {

    cost_k[v] = dist;



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

        if(tree[v][i].to == parent) {

            continue;

        }

        dfs(tree[v][i].to, v, dist + tree[v][i].weight);

    }

}



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 << cost_k[x] + cost_k[y] << endl;

    }

    

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.478907416571162x):
#include <iostream>

#include <cstdio>

#include <string>

#include <algorithm>

#include <utility>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <map>

#include <tuple>

#include <numeric>

#include <functional>

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 exrep(i, a, b) for(ll i = a; i <= b; i++)

#define out(x) cout << x << endl

#define exout(x) printf("%.10f\n", x)

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

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

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

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

#define pb push_back

#define re0 return 0

const ll mod = 1000000007;

const ll INF = 1e16;

const ll MAX_N = 100010;



int main() {

    ll n;

    cin >> n;

    

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

    rep(i, n-1) {

        ll a, b, cost;

        cin >> a >> b >> cost;

        a--;  b--;

        G[a].pb(make_pair(b, cost));

        G[b].pb(make_pair(a, cost));

    }



    ll q, k;

    cin >> q >> k;

    k--;

    

    vl dist(n, -1);  // dist[v] : 頂点vの根からの距離

    queue<ll> Q;

    const ll root = k;

    dist[root] = 0;

    Q.push(root);

    while(!Q.empty()) {

        ll v = Q.front();

        Q.pop();

        for(auto p : G[v]) {

            ll u = p.first;

            ll cost = p.second; 

            if(dist[u] != -1) {

                continue;

            }

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

            Q.push(u);

        }

    }

    

    vl ans(q);

    rep(i, q) {

        ll x, y;

        cin >> x >> y;

        x--;  y--;

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

    }



    rep(i, q) {

        out(ans[i]);

    }

    

    re0;

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

#include <cstdio>

#include <string>

#include <algorithm>

#include <utility>

#include <cmath>

#include <vector>

#include <queue>

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

using namespace std;

typedef long long ll;

ll mod = 1000000007;

const int Vmax = 100010;



struct edge {

    int to;

    ll weight;

};



vector<edge> tree[Vmax];

ll cost_k[Vmax];



void dfs(int v, int parent, ll dist) {

    cost_k[v] = dist;



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

        if(tree[v][i].to == parent) {

            continue;

        }

        dfs(tree[v][i].to, v, dist + tree[v][i].weight);

    }

}



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 << cost_k[x] + cost_k[y] << endl;

    }

    

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.478907416571162x):
#include <iostream>

#include <cstdio>

#include <string>

#include <algorithm>

#include <utility>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <map>

#include <tuple>

#include <numeric>

#include <functional>

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 exrep(i, a, b) for(ll i = a; i <= b; i++)

#define out(x) cout << x << endl

#define exout(x) printf("%.10f\n", x)

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

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

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

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

#define pb push_back

#define re0 return 0

const ll mod = 1000000007;

const ll INF = 1e16;

const ll MAX_N = 100010;



int main() {

    ll n;

    cin >> n;

    

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

    rep(i, n-1) {

        ll a, b, cost;

        cin >> a >> b >> cost;

        a--;  b--;

        G[a].pb(make_pair(b, cost));

        G[b].pb(make_pair(a, cost));

    }



    ll q, k;

    cin >> q >> k;

    k--;

    

    vl dist(n, -1);  // dist[v] : 頂点vの根からの距離

    queue<ll> Q;

    const ll root = k;  // デフォルトで根は頂点0

    dist[root] = 0;

    Q.push(root);

    while(!Q.empty()) {

        ll v = Q.front();

        Q.pop();

        for(auto p : G[v]) {

            ll u = p.first;

            ll cost = p.second; 

            if(dist[u] != -1) {

                continue;

            }

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

            Q.push(u);

        }

    }



    vl ans(q);

    rep(i, q) {

        ll x, y;

        cin >> x >> y;

        x--;  y--;

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

    }



    rep(i, q) {

        out(ans[i]);

    }

    

    re0;

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

#include <string>

#include <algorithm>

#include <vector>

#include <cmath>

#include <queue>

#include <map>

#include <set>

#include <bitset>

#include <functional>

using namespace std;



typedef long long ll;



vector<pair<ll, int>> edg[100003];

ll d[100003];



int main() {

	int n; cin >> n;

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

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

		--a; --b;

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

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

	}

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

	--k;

	priority_queue<pair<ll, int> > que;

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

	que.push({ 0, k});

	while (!que.empty()) {

		ll dis = -que.top().first; int pos = que.top().second; que.pop();

		if (d[pos] >= 0) continue;

		d[pos] = dis;

		for (auto p : edg[pos]) {

			que.push({ -dis - p.first, p.second });

		}

	}

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

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

		--x; --y;

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

	}

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.475406372026866x):
#include <iostream>

#include <string>

#include <algorithm>

#include <vector>

#include <cmath>

#include <queue>

#include <map>

#include <unordered_map>

#include <set>

#include <functional>

#include <bitset>

#include <numeric>

#include <complex>

#include <iomanip>

#include <cassert>

#include <random>





int main() {

    int n; std::cin >> n;

    std::vector<std::vector<int>> edg(n);

    std::vector<std::vector<long long>> cost(n);

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

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

        a--; b--;

        edg[a].push_back(b);

        edg[b].push_back(a);

        cost[a].push_back(c);

        cost[b].push_back(c);

    }

    int q, k; std::cin >> q >> k;

    k--;

    std::vector<long long> dist(n, 1e18);

    std::priority_queue<std::pair<long long, int>> que;

    que.emplace(0, k);

    dist[k] = 0;

    while (!que.empty()) {

        int pos = que.top().second;

        que.pop();

        for (int i = 0; i < (int)edg[pos].size(); i++) {

            int u = edg[pos][i];

            long long c = cost[pos][i];

            if (dist[u] <= dist[pos] + c) continue;

            dist[u] = dist[pos] + c;

            que.emplace(-dist[u], u);

        }

    }

    while (q--) {

        int x, y; std::cin >> x >> y;

        x--; y--;

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

    }

    return 0;

}
// 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.2665797612669747x):
#include <cstdio>

#include <cmath>

#include <cstring>

#include <iostream>

#include <vector>

#include <string>

#include <algorithm>

#include <functional>

#define inf 0x7ffffff7

#define MAXN  100010

#define PII pair<int, int>

#define e 2.718281828459

typedef long long ll;

typedef unsigned long long ull;

using namespace std;

ll dis[MAXN] = { 0 };

vector<pair<ll, ll>> v[MAXN];

void dfs(ll x, ll fa)

{

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

	{

		if (v[x][i].first == fa)

			continue;

		dis[v[x][i].first] = dis[x] + v[x][i].second;

		dfs(v[x][i].first, x);

	}

}

int main()

{

	int n, x, y;

	ll d;

	cin >> n;

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

	{

		cin >> x >> y >> d;

		v[x].push_back(pair<ll, ll>(y, d));

		v[y].push_back(pair<ll, ll>(x, d));

	}

	ll q, k;

	cin >> q >> k;

	dfs(k, -1);

	while (q--)

	{

		cin >> x >> y;

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

	}

	//cin >> T;

	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.3481408444460388x):
#include <cstdio>

#include <cmath>

#include <cstring>

#include <iostream>

#include <vector>

#include <string>

#include <algorithm>

#include <functional>

#define inf 0x7ffffff7

#define MAXN  100010

#define PII pair<int, int>

#define e 2.718281828459

typedef long long ll;

typedef unsigned long long ull;

using namespace std;

ll dis[MAXN] = { 0 };

vector<pair<ll, ll>> v[MAXN];

void dfs(ll x, ll fa)

{

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

	{

		if (v[x][i].first == fa)

			continue;

		dis[v[x][i].first] = dis[x] + v[x][i].second;

		dfs(v[x][i].first, x);

	}

}

int main()

{

	int n, x, y;

	ll d;

	cin >> n;

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

	{

		cin >> x >> y >> d;

		v[x].push_back(pair<ll, ll>(y, d));

		v[y].push_back(pair<ll, ll>(x, d));

	}

	ll q, k;

	cin >> q >> k;

	dfs(k, -1);

	while (q--)

	{

		cin >> x >> y;

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

	}

	//cin >> T;

	return 0;

}
// 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.259491163274699x):
#include<iostream>

#include<sstream>

#include<fstream>

#include<string>

#include<vector>

#include<deque>

#include<queue>

#include<stack>

#include<set>

#include<map>

#include<algorithm>

#include<functional>

#include<utility>

#include<bitset>

#include<cmath>

#include<cstdlib>

#include<ctime>

#include<cstdio>

using namespace std;

int n,q,k;

long long dt[101010];

vector<pair<int,int> >e[101010];

void dfs(int x,int y)

{

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

	{

		pair<int,int>p=e[x][i];

		if(p.first==y)

		{

			continue;

		}

		dt[p.first]=dt[x]+p.second;

		dfs(p.first,x);

	}

}

int main()

{

	cin>>n;

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

	{

		int a,b,c;

		cin>>a>>b>>c;

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

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

	}

	cin>>q>>k;

	dfs(k,0);

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

	{

		int a,b;

		cin>>a>>b;

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

	}

	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.3405957779800426x):
#include<iostream>

#include<sstream>

#include<fstream>

#include<string>

#include<vector>

#include<deque>

#include<queue>

#include<stack>

#include<set>

#include<map>

#include<algorithm>

#include<functional>

#include<utility>

#include<bitset>

#include<cmath>

#include<cstdlib>

#include<ctime>

#include<cstdio>

using namespace std;

int n,q,k;

long long dt[101010];

vector<pair<int,int> >e[101010];

void dfs(int x,int y)

{

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

	{

		pair<int,int>p=e[x][i];

		if(p.first==y)

		{

			continue;

		}

		dt[p.first]=dt[x]+p.second;

		dfs(p.first,x);

	}

}

int main()

{

	cin>>n;

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

	{

		int a,b,c;

		cin>>a>>b>>c;

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

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

	}

	cin>>q>>k;

	dfs(k,0);

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

	{

		int a,b;

		cin>>a>>b;

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

	}

	return 0;

}