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

using namespace std;

typedef long long ll;

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

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define Would

#define you

#define please





int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);



	int N;

	cin >> N;



	vector<pair<int, ll>> eda[100001];

	rep(i, N - 1) {

		int a, b, c;

		cin >> a >> b >> c;

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

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

	}



	ll kyori[100001] = {};



	int Q, K;

	cin >> Q >> K;



	vector<pair<int, ll>> que;

	que.push_back(make_pair(K, 0));



	while (que.size()) {

		vector<pair<int, ll>> que2;

		for (auto itr : que) {

			if (kyori[itr.first] == 0) {

				kyori[itr.first] = itr.second;

				for (auto itr2 : eda[itr.first]) {

					que2.push_back(make_pair(itr2.first, itr.second + itr2.second));

				}

			}

		}

		que = que2;

	}



	rep(i, Q) {

		int X, Y;

		cin >> X >> Y;

		co(kyori[X] + kyori[Y]);

	}



	Would you please return 0;

}

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

using namespace std;

typedef long long ll;

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

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define putchar putchar_unlocked

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], *ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}

inline void putint(ll X) {

	int keta = 0;

	char C[20];

	while (X) {

		*(C + keta) = '0' + X % 10;

		X /= 10;

		keta++;

	}

	for (int i = keta - 1; i >= 0; i--) putchar(*(C + i));

	putchar('\n');

}



int N;

ll D[100001];

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

void dfs(int A, ll B) {

	D[A] = B;

	for (auto p : E[A]) if (!(*(D + p.first))) dfs(p.first, B + p.second);

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N = getint();

	rep(i, N - 1) {

		int a = getint();

		int b = getint();

		int c = getint();

		(*(E + a)).pb(mp(b, c));

		(*(E + b)).pb(mp(a, c));

	}

	int Q = getint();

	int K = getint();

	dfs(K, 1);

	rep(i, Q) {

		int x = getint();

		int y = getint();

		putint(*(D + x) + *(D + y) - 2);

	}



	Would you please return 0;

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

using namespace std;

typedef long long ll;

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

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define Would

#define you

#define please





int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);



	int N;

	cin >> N;



	vector<pair<int, ll>> eda[100001];

	rep(i, N - 1) {

		int a, b, c;

		cin >> a >> b >> c;

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

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

	}



	ll kyori[100001] = {};



	int Q, K;

	cin >> Q >> K;



	vector<pair<int, ll>> que;

	que.push_back(make_pair(K, 0));



	while (que.size()) {

		vector<pair<int, ll>> que2;

		for (auto itr : que) {

			if (kyori[itr.first] == 0) {

				kyori[itr.first] = itr.second;

				for (auto itr2 : eda[itr.first]) {

					que2.push_back(make_pair(itr2.first, itr.second + itr2.second));

				}

			}

		}

		que = que2;

	}



	rep(i, Q) {

		int X, Y;

		cin >> X >> Y;

		co(kyori[X] + kyori[Y]);

	}



	Would you please return 0;

}

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

using namespace std;

typedef long long ll;

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

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define putchar putchar_unlocked

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], *ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}

inline void putint(ll X) {

	int keta = 0;

	char C[20];

	while (X) {

		*(C + keta) = '0' + X % 10;

		X /= 10;

		keta++;

	}

	for (int i = keta - 1; i >= 0; i--) putchar(*(C + i));

	putchar('\n');

}



int N;

ll D[100001];

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

void dfs(int A, ll B) {

	*(D + A) = B;

	for (auto itr = (*(E + A)).begin(); itr != (*(E + A)).end(); itr++) if (!(*(D + (*itr).first))) dfs((*itr).first, B + (*itr).second);

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N = getint();

	rep(i, N - 1) {

		int a = getint();

		int b = getint();

		int c = getint();

		(*(E + a)).pb(mp(b, c));

		(*(E + b)).pb(mp(a, c));

	}

	int Q = getint();

	int K = getint();

	dfs(K, 1);

	rep(i, Q) {

		int x = getint();

		int y = getint();

		putint(*(D + x) + *(D + y) - 2);

	}



	Would you please return 0;

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

using namespace std;

typedef long long ll;

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

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define Would

#define you

#define please





int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);



	int N;

	cin >> N;



	vector<pair<int, ll>> eda[100001];

	rep(i, N - 1) {

		int a, b, c;

		cin >> a >> b >> c;

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

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

	}



	ll kyori[100001] = {};



	int Q, K;

	cin >> Q >> K;



	vector<pair<int, ll>> que;

	que.push_back(make_pair(K, 0));



	while (que.size()) {

		vector<pair<int, ll>> que2;

		for (auto itr : que) {

			if (kyori[itr.first] == 0) {

				kyori[itr.first] = itr.second;

				for (auto itr2 : eda[itr.first]) {

					que2.push_back(make_pair(itr2.first, itr.second + itr2.second));

				}

			}

		}

		que = que2;

	}



	rep(i, Q) {

		int X, Y;

		cin >> X >> Y;

		co(kyori[X] + kyori[Y]);

	}



	Would you please return 0;

}

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

using namespace std;

typedef long long ll;

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

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], *ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



const int dm = 1 << 17;

char dn[dm], *di = dn, dt;

inline void putcha(char c) {

	*di++ = c;

	if (di == dn + dm) { fwrite(dn, 1, dm, stdout); di = dn; }

}



inline void putint(ll X) {

	int keta = 0;

	char C[20];

	while (X) {

		*(C + keta) = '0' + X % 10;

		X /= 10;

		keta++;

	}

	if (di + 20 > dn + dm) {

		for (int i = keta - 1; i >= 0; i--) putcha(*(C + i));

		putcha('\n');

	}

	else {

		for (int i = keta - 1; i >= 0; i--) *di++ = (*(C + i));

		*di++ = '\n';

	}

}



int N;

ll D[100001];

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

void dfs(int A, ll B) {

	*(D + A) = B;

	for (auto p : *(E + A)) if (!(*(D + p.first))) dfs(p.first, B + p.second);

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N = getint();

	rep(i, N - 1) {

		int a = getint();

		int b = getint();

		int c = getint();

		(*(E + a)).pb(mp(b, c));

		(*(E + b)).pb(mp(a, c));

	}

	int Q = getint();

	int K = getint();

	dfs(K, 1);

	rep(i, Q) {

		int x = getint();

		int y = getint();

		putint(*(D + x) + *(D + y) - 2);

	}



	fwrite(dn, 1, di - dn, stdout);



	Would you please return 0;

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

using namespace std;

typedef long long ll;

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

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define Would

#define you

#define please





int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);



	int N;

	cin >> N;



	vector<pair<int, ll>> eda[100001];

	rep(i, N - 1) {

		int a, b, c;

		cin >> a >> b >> c;

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

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

	}



	ll kyori[100001] = {};



	int Q, K;

	cin >> Q >> K;



	vector<pair<int, ll>> que;

	que.push_back(make_pair(K, 0));



	while (que.size()) {

		vector<pair<int, ll>> que2;

		for (auto itr : que) {

			if (kyori[itr.first] == 0) {

				kyori[itr.first] = itr.second;

				for (auto itr2 : eda[itr.first]) {

					que2.push_back(make_pair(itr2.first, itr.second + itr2.second));

				}

			}

		}

		que = que2;

	}



	rep(i, Q) {

		int X, Y;

		cin >> X >> Y;

		co(kyori[X] + kyori[Y]);

	}



	Would you please return 0;

}

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

using namespace std;

typedef long long ll;

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

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], *ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



const int dm = 1 << 21;

char dn[dm], *di = dn, dt;

inline void putint(ll X) {

	int keta = 0;

	char C[20];

	while (X) {

		*(C + keta) = '0' + X % 10;

		X /= 10;

		keta++;

	}

	for (int i = keta - 1; i >= 0; i--) *di++ = (*(C + i));

	*di++ = '\n';

}



int N;

ll D[100001];

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

void dfs(int A, ll B) {

	*(D + A) = B;

	for (auto p : *(E + A)) if (!(*(D + p.first))) dfs(p.first, B + p.second);

}



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N = getint();

	rep(i, N - 1) {

		int a = getint();

		int b = getint();

		int c = getint();

		(*(E + a)).pb(mp(b, c));

		(*(E + b)).pb(mp(a, c));

	}

	int Q = getint();

	int K = getint();

	dfs(K, 1);

	rep(i, Q) {

		int x = getint();

		int y = getint();

		putint(*(D + x) + *(D + y) - 2);

	}



	fwrite(dn, 1, di - dn, stdout);



	Would you please return 0;

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

using namespace std;

typedef long long ll;

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

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define Would

#define you

#define please





int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);



	int N;

	cin >> N;



	vector<pair<int, ll>> eda[100001];

	rep(i, N - 1) {

		int a, b, c;

		cin >> a >> b >> c;

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

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

	}



	ll kyori[100001] = {};



	int Q, K;

	cin >> Q >> K;



	vector<pair<int, ll>> que;

	que.push_back(make_pair(K, 0));



	while (que.size()) {

		vector<pair<int, ll>> que2;

		for (auto itr : que) {

			if (kyori[itr.first] == 0) {

				kyori[itr.first] = itr.second;

				for (auto itr2 : eda[itr.first]) {

					que2.push_back(make_pair(itr2.first, itr.second + itr2.second));

				}

			}

		}

		que = que2;

	}



	rep(i, Q) {

		int X, Y;

		cin >> X >> Y;

		co(kyori[X] + kyori[Y]);

	}



	Would you please return 0;

}

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

#include<cstdio>

using namespace std;

typedef long long ll;

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

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



const int dm = 1 << 21;

char dn[dm], * di = dn, dt;

inline void putint(ll X) {

	int keta = 0;

	char C[20];

	while (X) {

		*(C + keta) = '0' + X % 10;

		X /= 10;

		keta++;

	}

	for (int i = keta - 1; i >= 0; i--)* di++ = (*(C + i));

	*di++ = '\n';

}



int N;

ll D[100001];

int E[200001], Ed[200001], E2[200001], H[100001];

void dfs(int A, ll B) {

	*(D + A) = B;

	for (int i = H[A]; i; i = E2[i]) if (!(*(D + E[i]))) dfs(E[i], B + Ed[i]);

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint();

	int k = 1;

	rep(i, N - 1) {

		int a = getint();

		int b = getint();

		int c = getint();

		E[k] = b;

		Ed[k] = c;

		E2[k] = H[a];

		H[a] = k++;

		E[k] = a;

		Ed[k] = c;

		E2[k] = H[b];

		H[b] = k++;

	}

	int Q = getint();

	int K = getint();

	dfs(K, 1);

	rep(i, Q) {

		int x = getint();

		int y = getint();

		putint(*(D + x) + *(D + y) - 2);

	}



	fwrite_unlocked(dn, di - dn, 1, stdout);



	Would you please return 0;

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

using namespace std;

typedef long long ll;

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

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define Would

#define you

#define please





int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);



	int N;

	cin >> N;



	vector<pair<int, ll>> eda[100001];

	rep(i, N - 1) {

		int a, b, c;

		cin >> a >> b >> c;

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

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

	}



	ll kyori[100001] = {};



	int Q, K;

	cin >> Q >> K;



	vector<pair<int, ll>> que;

	que.push_back(make_pair(K, 0));



	while (que.size()) {

		vector<pair<int, ll>> que2;

		for (auto itr : que) {

			if (kyori[itr.first] == 0) {

				kyori[itr.first] = itr.second;

				for (auto itr2 : eda[itr.first]) {

					que2.push_back(make_pair(itr2.first, itr.second + itr2.second));

				}

			}

		}

		que = que2;

	}



	rep(i, Q) {

		int X, Y;

		cin >> X >> Y;

		co(kyori[X] + kyori[Y]);

	}



	Would you please return 0;

}

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

#include<cstdio>

using namespace std;

typedef long long ll;

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

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



const int dm = 1 << 21;

char dn[dm], * di = dn, dt;

inline void putint(ll X) {

	int keta = 0;

	char C[20];

	while (X) {

		*(C + keta) = '0' + X % 10;

		X /= 10;

		keta++;

	}

	for (int i = keta - 1; i >= 0; i--)* di++ = (*(C + i));

	*di++ = '\n';

}



int N;

ll D[100001];

int E[200001], Ed[200001], E2[200001], H[100001];

void dfs(int A, ll B) {

	*(D + A) = B;

	for (int i = H[A]; i; i = E2[i]) if (!(*(D + E[i]))) dfs(E[i], B + Ed[i]);

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint();

	int k = 1;

	rep(i, N - 1) {

		int a = getint();

		int b = getint();

		int c = getint();

		E[k] = b;

		Ed[k] = c;

		E2[k] = H[a];

		H[a] = k++;

		E[k] = a;

		Ed[k] = c;

		E2[k] = H[b];

		H[b] = k++;

	}

	int Q = getint();

	int K = getint();

	dfs(K, 1);

	rep(i, Q) {

		int x = getint();

		int y = getint();

		putint(*(D + x) + *(D + y) - 2);

	}



	fwrite_unlocked(dn, 1, di - dn, stdout);



	Would you please return 0;

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



using namespace std;

typedef long long ll;

typedef pair<ll, int> P;



struct edge {

  int to; ll cost;

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

};



vector<edge> G[100001];

ll d[100001];



void dijkstra(int s) {

  priority_queue<P> que;

  fill(d, d + 100001, 1e18);

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

      }

    }

  }

}



int main(void) {

  int n;

  cin >> n;



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

    int a, b, c;

    cin >> a >> b >> c;

    G[a - 1].push_back(edge(b - 1, c));

    G[b - 1].push_back(edge(a - 1, c));

  }



  int Q, K;

  cin >> Q >> K;



  dijkstra(K - 1);



  vector<int> x(Q), y(Q);

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

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

  }



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

    cout << d[x[i] - 1] + d[y[i] - 1] << endl;

  }

  

  return 0;

}

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

#define int long long

#define endl '\n'

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

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

using namespace std;



int N, Q, K;

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

int dis[100010];



void dfs(int v, int pv, int d) {

    dis[v] = d;

    for (auto e : G[v]) if (e.first != pv) {

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

    }

}



void _main() {

    cin >> N;

    REP (i, N - 1) {

        int a, b, c;

        cin >> a >> b >> c;

        a--; b--;

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

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

    }

    cin >> Q >> K;

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

    REP (i, Q) {

        int x, y;

        cin >> x >> y;

        x--; y--;

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

    }

}



signed main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cout << fixed << setprecision(10);

    _main();

    return 0;

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

using namespace std;

using ll = long long;

using pii = pair<int, int>;



struct edge {

  int to, cost;

  edge() {}

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

};



const ll INF = 1LL << 50;

int N;

int a[100010], b[100010], c[100010];

int Q, K;

vector<edge> G[100010];

ll dis[100010];



void bfs() {

  fill_n((ll*)dis, 100010, INF);

  dis[K] = 0;

  queue<int> que;

  que.emplace(K);

  while (!que.empty()) {

    int cur = que.front();

    que.pop();

    for (edge &e : G[cur]) {

      if (dis[e.to] == INF) {

        dis[e.to] = dis[cur] + e.cost;

        que.emplace(e.to);

      }

    }

  }

}



int main() {

  cin.tie(0);

  ios_base::sync_with_stdio(false);

  cout << fixed << setprecision(10);

  

  cin >> N;

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

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

    a[i]--, b[i]--;

    G[a[i]].emplace_back(b[i], c[i]);

    G[b[i]].emplace_back(a[i], c[i]);

  }

  cin >> Q >> K;

  K--;

  bfs();

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

    int x, y;

    cin >> x >> y;

    x--, y--;

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

  }



  return 0;

}

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

#define int long long

#define endl '\n'

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

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

using namespace std;



int N, Q, K;

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

int dis[100010];



void dfs(int v, int pv, int d) {

    dis[v] = d;

    for (auto e : G[v]) if (e.first != pv) {

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

    }

}



void _main() {

    cin >> N;

    REP (i, N - 1) {

        int a, b, c;

        cin >> a >> b >> c;

        a--; b--;

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

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

    }

    cin >> Q >> K;

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

    REP (i, Q) {

        int x, y;

        cin >> x >> y;

        x--; y--;

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

    }

}



signed main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cout << fixed << setprecision(10);

    _main();

    return 0;

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

using namespace std;

using ll = long long;

using pii = pair<int, int>;



struct edge {

  int to, cost;

  edge() {}

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

};



const ll INF = 1LL << 50;

int N, Q, K;

int a[100010], b[100010], c[100010];

vector<edge> G[100010];

ll dis[100010];



int main() {

  cin.tie(0);

  ios_base::sync_with_stdio(false);

  cout << fixed << setprecision(10);

  

  cin >> N;

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

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

    a[i]--; b[i]--;

    G[a[i]].emplace_back(b[i], c[i]);

    G[b[i]].emplace_back(a[i], c[i]);

  }

  cin >> Q >> K;

  K--;

  fill_n((ll*)dis, 100010, INF);

  dis[K] = 0;

  queue<int> que;

  que.emplace(K);

  while (!que.empty()) {

    int cur = que.front();

    que.pop();

    for (edge &e : G[cur]) {

      if (dis[e.to] == INF) {

        dis[e.to] = dis[cur] + e.cost;

        que.emplace(e.to);

      }

    }

  }

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

    int x, y;

    cin >> x >> y;

    x--; y--;

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

  }



  return 0;

}

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

#define int long long

#define endl '\n'

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

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

using namespace std;



int N, Q, K;

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

int dis[100010];



void dfs(int v, int pv, int d) {

    dis[v] = d;

    for (auto e : G[v]) if (e.first != pv) {

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

    }

}



void _main() {

    cin >> N;

    REP (i, N - 1) {

        int a, b, c;

        cin >> a >> b >> c;

        a--; b--;

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

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

    }

    cin >> Q >> K;

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

    REP (i, Q) {

        int x, y;

        cin >> x >> y;

        x--; y--;

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

    }

}



signed main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cout << fixed << setprecision(10);

    _main();

    return 0;

}