#if 1
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>

auto& in = std::cin;
auto& out = std::cout;

int32_t N,Q,K;


#include <queue>
#include <vector>
#include <functional>
#include <utility>
#include <algorithm>
#include <iterator>

using COST_T = uint64_t;
constexpr uint32_t N_MAX = 100000;
constexpr COST_T INF = 100000100000100000;//std::numeric_limits<double>::infinity()

#if defined(_MSC_VER) && defined(_DEBUG)
//static_assert(false, "リリースでコンパイルしないと遅いよ！！");
#endif

struct edge {
	uint32_t to;
	COST_T cost;
	edge() {}
	edge(uint32_t to_, COST_T cost_)
		:to(to_), cost(cost_) {}
};
std::vector<edge> graph[N_MAX];

//ダイクストラ
COST_T D[N_MAX];
void Dijkstra(uint32_t s)
{
	using P = std::pair<COST_T, uint32_t>;//cost pos
	std::priority_queue<P, std::vector<P>, std::greater<>> que;
	std::fill(std::begin(D), std::end(D), INF);

	D[s] = 0;
	que.emplace(0, s);
	while (!que.empty())
	{
		auto p = que.top(); que.pop();
		const auto& nowpos = p.second;
		const auto& nowcost = p.first;
		if (D[nowpos] < nowcost) { continue; }

		//for (int32_t to = 0; to < N; ++to)
		//{
		//	auto cost = nowcost + graph[nowpos][to];
		//	if (cost < D[to]) {
		//		D[to] = cost;
		//		que.emplace(D[to], to);
		//	}
		//}

		for (const auto& e : graph[nowpos])
		{
			auto cost = nowcost + e.cost;
			if (cost < D[e.to]) {
				D[e.to] = cost;
				que.emplace(cost, e.to);
			}
		}

	}
}



int main()
{
	using std::endl;
	in.sync_with_stdio(false);
	out.sync_with_stdio(false);
	in.tie(nullptr);
	out.tie(nullptr);

	in >> N;
	for (int32_t i = 0; i < N-1; ++i)
	{
		int x, y,c;
		in >> x >> y; --x; --y;
		in >> c;
		graph[x].emplace_back(y, c);
		graph[y].emplace_back(x, c);

	}
	in >> Q >> K; --K;
	Dijkstra(K);
	for (int32_t i = 0; i < Q; ++i)
	{
		int x, y;
		in >> x >> y; --x; --y;
		out<<D[x] + D[y]<<endl;
	}

	return 0;
}
#endif


#if 0
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>

auto& in = std::cin;
auto& out = std::cout;

int32_t a, b, c, d;

int main()
{
	using std::endl;
	in.sync_with_stdio(false);
	out.sync_with_stdio(false);
	in.tie(nullptr);
	out.tie(nullptr);

	in >> a >> b >> c >> d;
	if (a<c) {}
	else { std::swap(a, c); std::swap(b, d); }

	out << std::max(0, std::min(d,b) - c) << endl;;


	return 0;
}
#endif






#if 0
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>

auto& in = std::cin;
auto& out = std::cout;

int64_t K;
int64_t N;
std::array<int64_t, 50> A;
int main()
{
	using std::endl;
	in.sync_with_stdio(false);
	out.sync_with_stdio(false);
	in.tie(nullptr);
	out.tie(nullptr);

	in >> N;
	for (int32_t i = 0; i < N; ++i)
	{
		in >> A[i];
	}
	std::sort(A.begin(), A.begin() + (size_t)N);
	int64_t total_operate = 0;
	for (int32_t dec = N - 1; dec >= 1; dec--)
	{
		int64_t add_count = (A[dec] - A[0]) / (N + 1);
		A[dec] = A[0] + (A[dec] - A[0]) % (N + 1);
		total_operate += add_count;
	}
	for (int32_t i = 0; i < N; ++i)
	{
		A[i] += total_operate;
	}
	std::sort(A.begin(), A.begin() + (size_t)N);

	//この時点で、最大の差がN

	if (A[N-1]<N) {
		out << total_operate << endl; return 0;
	}

	//最大がNになるように全体から引く
	total_operate += N*(A[N - 1] - N);
	if (A[N - 1] - N > 1000000) {
		total_operate -= N;
	}
	for (int32_t i = 0; i < N; ++i)
	{
		A[i] -= (A[N - 1] - N);
	}
	int64_t last_index = N - 1;
	for (;;)
	{
		++total_operate;
		A[last_index] -= N;
		for (int32_t i = 0; i < N; ++i)
		{
			if (i != last_index) {
				A[i]++;
			}
		}
		--last_index;
		if (last_index < 0) { last_index += N; }
		if (std::all_of(A.begin(), A.begin() + (size_t)N, [](int64_t v) {return v < N; })) {
			out << total_operate << endl; return 0;
		}
	}

	////値が二種類

	//int32_t big_count = std::count(A, A + N, A[last_index]);
	//total_operate += (A[last_index] - (N-big_count)) - (N - 1);
	//total_operate -= (N - big_count);
	//out << total_operate << endl; return 0;
}
#endif

Measured memory_bytes: 11656