stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include "bits/stdc++.h"

#define MOD 1000000007

#define INF 1123456789012345

#define in std::cin

#define out std::cout

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

typedef long long int LL;

typedef std::pair<LL, LL> P;

struct edge { LL to, cost; };

 

LL N, a[112345], b[112345], c[112345], Q, K, x[112345], y[112345];

LL d[112345];

std::vector<edge>G[112345];

 

void dijkstra(LL s)

{

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

	std::fill(d, d + N + 1, INF);

	d[s] = 0;

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

	while (!que.empty())

	{

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

		LL v = p.second;

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

		rep(i, G[v].size())

		{

			edge e = G[v][i];

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

			{

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

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

			}

		}

	}

}

 

int main()

{

	in >> N;

	rep(i, N - 1)

	{

		in >> a[i] >> b[i] >> c[i];

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

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

	}

	in >> Q >> K;

	rep(i, Q) { in >> x[i] >> y[i]; }

 

	dijkstra(K);

 

	rep(i, Q) { out << d[x[i]] + d[y[i]] << std::endl; }

	return 0;

}

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

#define in std::cin

#define out std::cout

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

typedef long long int LL;

typedef std::pair<LL, LL> P;

struct edge { LL to, cost; };



const LL inf = 112345678901234;

LL N, Q, K;

std::vector<LL>dist;

std::vector<std::vector<edge>>G;



void dijkstra(LL s)

{

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

	std::fill(dist.begin(), dist.end(), inf);

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

		rep(i, G[v].size())

		{

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

{

	in >> N;

	std::vector<LL>a(N - 1), b(N - 1), c(N - 1);

	dist.resize(N + 1); G.resize(N + 1);

	rep(i, N - 1)

	{

		in >> a[i] >> b[i] >> c[i];

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

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

	}

	in >> Q >> K;

	dijkstra(K);

	std::vector<LL> x(Q), y(Q);

	rep(i, Q)

	{

		in >> x[i] >> y[i];

		out << dist[x[i]] + dist[y[i]] << std::endl;

	}

	return 0;

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

#define MOD 1000000007

#define INF 1123456789012345

#define in std::cin

#define out std::cout

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

typedef long long int LL;

typedef std::pair<LL, LL> P;

struct edge { LL to, cost; };



LL N, a[112345], b[112345], c[112345], Q, K, x[112345], y[112345];

LL d[112345];

std::vector<edge>G[112345];



void dijkstra(LL s)

{

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

	std::fill(d, d + N + 1, INF);

	d[s] = 0;

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

	while (!que.empty())

	{

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

		LL v = p.second;

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

		rep(i, G[v].size())

		{

			edge e = G[v][i];

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

			{

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

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

			}

		}

	}

}



int main()

{

	in >> N;

	rep(i, N - 1)

	{

		in >> a[i] >> b[i] >> c[i];

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

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

	}

	in >> Q >> K;

	rep(i, Q) { in >> x[i] >> y[i]; }



	dijkstra(K);



	rep(i, Q) { out << d[x[i]] + d[y[i]] << std::endl; }

	return 0;

}

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

#define in std::cin

#define out std::cout

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

typedef long long int LL;

typedef std::pair<LL, LL> P;

struct edge { LL to, cost; };



const LL inf = 1123456789012345;

std::vector<LL>dist;

std::vector<std::vector<edge>>G;

void dijkstra(LL s)

{

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

	std::fill(dist.begin(), dist.end(), inf);

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

		rep(i, G[v].size())

		{

			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, Q, K;

	in >> N;

	std::vector<LL>a(N - 1), b(N - 1), c(N - 1);

	rep(i, N - 1) in >> a[i] >> b[i] >> c[i];

	in >> Q >> K;

	std::vector<LL>x(Q), y(Q);

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



	dist.resize(N + 1);

	G.resize(N + 1);

	rep(i, N - 1)

	{

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

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

	}

	dijkstra(K);



	rep(i, Q) out << dist[x[i]] + dist[y[i]] << std::endl;

	return 0;

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

#define MOD 1000000007

#define INF 1123456789012345

#define in std::cin

#define out std::cout

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

typedef long long int LL;

typedef std::pair<LL, LL> P;

struct edge { LL to, cost; };

 

LL N, a[112345], b[112345], c[112345], Q, K, x[112345], y[112345];

LL d[112345];

std::vector<edge>G[112345];

 

void dijkstra(LL s)

{

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

	std::fill(d, d + N + 1, INF);

	d[s] = 0;

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

	while (!que.empty())

	{

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

		LL v = p.second;

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

		rep(i, G[v].size())

		{

			edge e = G[v][i];

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

			{

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

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

			}

		}

	}

}

 

int main()

{

	in >> N;

	rep(i, N - 1)

	{

		in >> a[i] >> b[i] >> c[i];

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

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

	}

	in >> Q >> K;

	rep(i, Q) { in >> x[i] >> y[i]; }

 

	dijkstra(K);

 

	rep(i, Q) { out << d[x[i]] + d[y[i]] << std::endl; }

	return 0;

}

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

#define in std::cin

#define out std::cout

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

typedef long long int LL;

typedef std::pair<LL, LL> P;

struct edge { LL to, cost; };



const LL inf = 1123456789012345;

std::vector<LL>dist;

std::vector<std::vector<edge>>G;

void dijkstra(LL s)

{

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

	std::fill(dist.begin(), dist.end(), inf);

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

		rep(i, G[v].size())

		{

			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, Q, K;

	in >> N;

	std::vector<LL>a(N - 1), b(N - 1), c(N - 1);

	rep(i, N - 1) in >> a[i] >> b[i] >> c[i];

	in >> Q >> K;

	std::vector<LL>x(Q), y(Q);

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



	dist.resize(N + 1);

	G.resize(N + 1);

	rep(i, N - 1)

	{

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

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

	}

	dijkstra(K);



	rep(i, Q) out << dist[x[i]] + dist[y[i]] << std::endl;

	return 0;

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

#define debug(x) cerr << #x << ": " << x << '\n';

using namespace std;

using ll = long long;

using P = pair<ll, int>;

const ll INF = (ll)1e17;



struct edge{

	int to, cost;

};



vector<edge> G[100010];

ll d[100010];



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

		edge e; e.to = b; e.cost = c; G[a].push_back(e);

		e.to = a; G[b].push_back(e);

	}

	int Q, K; cin >> Q >> K;



	// for(int i = 0; i < N; i++){

	// 	printf("G[%d]: ", i);

	// 	for(auto& v: G[i]){

	// 		printf("%d ", v.to);

	// 	}

	// 	printf("\n");

	// }



	queue<int> que;

	que.push(K-1);

	fill(d, d+N, -1); d[K-1] = 0;



	while(not que.empty()){

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

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

			edge e = G[v][i];

			if(d[e.to] >= 0) continue;



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

			que.push(e.to);

		}

	}



	// for(int i = 0; i < N; i++) printf("d[%d] = %lld\n", i, d[i]);



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

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

		x--; y--;

		printf("%lld\n", (ll)d[x] + d[y]);

	}



	return 0;

}


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

#define debug(x) cerr << #x << ": " << x << '\n';

using namespace std;

using ll = long long;

using P = pair<int, int>;

const int INF = (int)1e9;



// graph template

template<typename T>

class edge{

    public:

        int src, to; T cost;



        edge(int to, T cost): src(-1), to(to), cost(cost){}

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

};

template<typename T>

using Edges = vector<edge<T>>;

template<typename T>

using AdjacencyList = vector<Edges<T>>;

template<typename T>

using Matrix = vector<vector<T>>;



template<typename T>

vector<T> dijkstra(AdjacencyList<T> &g, int s){

    const auto INF = numeric_limits<T>::max();

    vector<T> dist(g.size(), INF);



    using Pr = pair<T, int>;

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

    dist[s] = 0;

    que.emplace(dist[s], s);



    while(not que.empty()){

        T cost; int v;

        tie(cost, v) = que.top(); que.pop();

        if(dist[v] < cost) continue;



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

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

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

                que.emplace(dist[e.to], e.to);

            }

        }

    }



    return dist;

}



int main(void){

    int N; 

    cin >> N;

    AdjacencyList<ll> G(N);

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

        int a, b, c;

        cin >> a >> b >> c;

        a--; b--;

        G[a].push_back(edge<ll>(b, c));

        G[b].push_back(edge<ll>(a, c));

    }



    int Q, K;

    cin >> Q >> K;

    vector<ll> dist = dijkstra(G, K-1);

    // for(int i = 0; i < N; i++) printf("dist[%d] = %lld\n", i, dist[i]);

    vector<ll> res(Q);

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

        int x, y;

        cin >> x >> y;

        x--; y--;

        res[i] = (dist[x] + dist[y]);

    }



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

        cout << res[i] << '\n';

    }



    return 0;

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

#include <cassert>

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

using namespace std;

using ll = long long;

using P = pair<int, int>;

using Graph = vector<vector<int>>;



const long long MOD = 1000000007;

const long long INF = 1LL << 60;

const int INT_INF = 1000000000;



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

int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, 1, -1, 1, 0, -1};



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

{

  ios::sync_with_stdio(false);

  cin.tie(nullptr);

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

#include <cassert>

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

using namespace std;

using ll = long long;

using P = pair<int, int>;

struct Edge

{

  int to;

  int weight;

  Edge(int t, int w) : to(t), weight(w) {}

};

using Graph = vector<vector<Edge>>;

// using Graph = vector<vector<int>>;



const long long INF = 1LL << 60;

const int INT_INF = 1000000000;



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

// int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, 1, -1, 1, 0, -1};



void dfs(Graph &G, vector<ll> &d, int v, int p = -1)

{

  for (auto u : G[v])

  {

    if (u.to == p)

      continue;



    // cerr << u.to << " " << d[v] + u.weight << endl;

    d[u.to] = min(d[u.to], d[v] + u.weight);

    dfs(G, d, u.to, v);

  }

}



int main()

{

  ios::sync_with_stdio(false);

  cin.tie(nullptr);

  int N;

  cin >> N;

  Graph G(N);

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

  {

    int a, b, c;

    cin >> a >> b >> c;

    a--, b--, c;

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

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

  }



  int Q, K;

  cin >> Q >> K;

  K--;



  vector<ll> d(N, INF);



  d[K] = 0;



  dfs(G, d, K);



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

  {

    // cerr << "a" << endl;

    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;

 

long long n;

vector<long long> v(100005);

map<long long,vector<pair<long long,long long>>> mp;

bool used[100005];

 

void dfs(long long s,long long dist){

        used[s]=true;

        v[s]=dist;



        for(auto i:mp[s]){

            if(!used[i.first])

                dfs(i.first,dist+i.second);

        }



}

 

int main(){

    cin>>n;

    long long a,b,c;

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

        cin>>a>>b>>c;

        a--,b--;

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

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

    }

    long long k,q;

    cin>>q>>k;

    k--;

    

 

    dfs(k,0);

 

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

        long long x,y;

        cin>>x>>y;

        x--,y--;

      

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

    }

}

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

using namespace std;

 

class Dijkstra {

public:

    struct edge { long long v, dist; };

 

    struct state {

        long long v, cost;

        bool operator>(const state s) const { return cost > s.cost; }

    };

 

    const long long INF = (1LL << 60);

    long long N;

    vector< vector<edge> > E;

 

    Dijkstra(long long n): N(n), E(n) {}

 

    //有効グラフの時はこっち。u→vに距離dで結ぶ

    void add_directed_edge(long long u, long long v, long long d) {

        E[u].push_back((edge) { v, d });

    }

 

    //無向グラフの時はこっち。uとvを双方向に距離dで結ぶ

    void add_undirected_edge(long long u, long long v, long long d) {

        E[u].push_back((edge) { v, d });

        E[v].push_back((edge) { u, d });

    }

 

    //Sを始点として、他の頂点への最短経路を探す

    vector<long long> shortest_path(long long S) {

        vector<long long> dp(E.size(), INF);

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

        q.push((state) { S, 0 });

        while(!q.empty()) {

            long long v = q.top().v, cost = q.top().cost;

            q.pop();

            if(dp[v] <= cost) continue;

            dp[v] = cost;

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

            long long nv = E[v][i].v, ncost = cost + E[v][i].dist;

            if(dp[nv] > ncost) q.push((state) { nv, ncost });

            }

        }

        return dp;

    }

};

 

 

int main(){

    long long n;

    cin>>n;

    long long a,b,c;

    Dijkstra d(n);

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

        cin>>a>>b>>c;

        a--,b--;

        d.add_undirected_edge(a,b,c);

    }

    long long q,k;

    cin>>q>>k;

    k--;

    vector<long long> ans=d.shortest_path(k);

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

        long long x,y;

        cin>>x>>y;

        x--,y--;



        

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

    }

    

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

#include<iomanip>

#include<numeric>



using namespace std;

using ll = long long;

using ull = unsigned long long;

constexpr int mo = 1e9+7;

constexpr int  mod = mo;

constexpr int inf = 1<<30;

constexpr ll infl = 1ll<<60;

vector<pair<int,int>> E[100050];

vector<ll> dist(100050,infl);

void dfs(int u,int pre){

    for(auto v : E[u]){

        if(v.second == pre)continue;

        if(dist[v.second] > dist[u] + v.first){

            dist[v.second] = dist[u] + v.first;

            dfs(v.second,u);

        }

    }

}

int main(){

    int n;

    cin>>n;

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

        int a,b,c;

        cin>>a>>b>>c;

        a--;b--;

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

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

    }

    int q,k;

    cin>>q>>k;

    k--;

    dist[k] = 0ll;

    dfs(k,-1);

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

        int u,v;

        cin>>u>>v;

        u--;v--;

        cout << dist[u] + dist[v] << endl;

    }

    return 0;

}




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



using namespace std;

using ll = long long;

using ull = unsigned long long;

constexpr int inf = 1<<30;

constexpr int mo = 1e9+7;

constexpr ll infl = 1ll<<60;



int main(){

    cin.tie(nullptr);

    ios::sync_with_stdio(false);

    

    int n;

    cin>>n;

    vector<pair<int,int>> E[100050];

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

        int a,b;ll c;

        cin>>a>>b>>c;

        a--;b--;

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

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

    }

    int Q,k;

    cin>>Q>>k;

    k--;

    queue<int> q;

    q.push(k);

    vector<ll> dist(n,infl);

    dist[k] = 0;

    while(!q.empty()){

        auto cur = q.front();q.pop();

        for(auto & v : E[cur]){

            if(dist[cur] + v.second < dist[v.first]){

                dist[v.first] = dist[cur] + v.second;

                q.push(v.first);

            }

        }

    }

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

        int x,y;

        cin>>x>>y;

        x--;y--;

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

    }

    return 0;

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

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

using namespace std;

typedef long long ll;

typedef pair<int,int>P;



vector<P>E[200000];

ll d[200000];

int par[20][200000];



struct LCA{

public:

	vector<vector<int> > g;

	const int MAX_LOG = 22;

	vector<vector<int> > lcc;

	vector<int> dep;

	void init2(){

		int n = g.size();

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

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

				if (lcc[i][j] == -1){

					lcc[i + 1][j] = -1;

					continue;

				}

				lcc[i + 1][j] = lcc[i][lcc[i][j]];

			}

		}

	}

	int lca(int a, int b){

		if (dep[a] < dep[b]){

			swap(a, b);

		}

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

			if (((dep[a] - dep[b]) >> i) & 1){

				a = lcc[i][a];

			}

		}

		if (a == b){

			return a;

		}

		for (int i = MAX_LOG - 1; i >= 0; i--){

			if (lcc[i][a] != lcc[i][b]){

				a = lcc[i][a];

				b = lcc[i][b];

			}

		}

		return lcc[0][a];

	}

	inline void dfs(int b, int pr = -1, int d = 0){

		for (auto &i : g[b]){

			if (i == pr)continue;

			dfs(i, b, d + 1);

		}

		dep[b] = d;

		lcc[0][b] = pr;

	}

	void init(vector<vector<int> > &tree, int root = 0){

		g = tree;

		lcc.resize(MAX_LOG, vector<int>(g.size(), 0));

		dep.resize(g.size());

		dfs(root);

		init2();

	}

};

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

	d[v]=d1;par[0][v]=p;

	for(P u:E[v]){

		if(u.second!=p)dfs(u.second,v,d1+u.first);

	}

}

LCA G;

ll dist(int x,int y){

	return d[x]+d[y]-d[G.lca(x,y)]*2;

}

int main(){

	int n;scanf("%d",&n);

	vector<vector<int>>F(n);

	rep(i,n-1){

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

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

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

	}

	G.init(F);

	dfs(0,-1,0);

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

	rep(i,q){

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

		printf("%lld\n",dist(x,k)+dist(k,y));

	}

}

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

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

using namespace std;

typedef pair<int,int>P;

typedef long long ll;



vector<P>E[100000];

ll d[100000];

void dfs(int v,int p){

	for(P u:E[v]){

		if(u.second!=p){

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

		}

	}

}

int main(){

	int n;scanf("%d",&n);

	rep(i,n-1){

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

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

	}

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

	dfs(k,-1);

	rep(i,q){

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

		printf("%lld\n",d[x]+d[y]);

	}

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

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

using namespace std;

typedef long long ll;

typedef pair<int,int>P;



vector<P>E[200000];

ll d[200000],q[200000];

int par[25][200000];



void dfs(int v,int p,ll d1,ll d2){

	d[v]=d1;q[v]=d2;par[0][v]=p;

	for(P u:E[v]){

		if(u.second!=p)dfs(u.second,v,d1+1,d2+u.first);

	}

}

int lca(int u,int v){

	if(d[u]>d[v])swap(u,v);

	rep(i,22){

		if((d[v]-d[u])>>i&1)v=par[i][v];

	}

	if(u==v)return u;

	for(int i=21;i>=0;i--){

		if(par[i][u]!=par[i][v]){

			u=par[i][u];v=par[i][v];

		}

	}

	return par[0][u];

}

ll dist(int x,int y){

	return q[x]+q[y]-q[lca(x,y)]*2;

}

int main(){

	int n;scanf("%d",&n);

	rep(i,n-1){

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

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

	}

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

	for(int i=1;i<22;i++)rep(j,n){

		if(par[i-1][j]==-1)par[i][j]=-1;

		else par[i][j]=par[i-1][par[i-1][j]];

	}

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

	rep(i,q){

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

		printf("%lld\n",dist(x,k)+dist(k,y));

	}

}

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

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

using namespace std;

typedef pair<int,int>P;

typedef long long ll;



vector<P>E[100000];

ll d[100000];

void dfs(int v,int p){

	for(P u:E[v]){

		if(u.second!=p){

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

		}

	}

}

int main(){

	int n;scanf("%d",&n);

	rep(i,n-1){

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

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

	}

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

	dfs(k,-1);

	rep(i,q){

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

		printf("%lld\n",d[x]+d[y]);

	}

}