#include <bits/stdc++.h>
#define rep(i,n) for(int (i) = 0; (i) < (n);(i)++)
using namespace std;

const int limit = 100010;
vector<int> graph[2][limit];

void add_edge(int from, int to) {
	graph[0][from].push_back(to);
	graph[1][to].push_back(from);
}


auto scc(int n) {
	vector<int> order(n);
	iota(begin(order), end(order), 0);
	vector<int> comp;

	rep(type, 2) {
		stack<int> stk;
		vector<int> norder;
		vector<int> itr(n, 0);

		comp.assign(n, -1);
		int idx = 0;

		/*
		rep(i, n) cerr << order[i] << " ";
		cerr << endl;
		*/

		for (auto &it : order) {
			if (comp[it] != -1) continue; 
			stk.push(it);
	
			while (!stk.empty()) {
				const int v = stk.top(); 				
				comp[v] = idx;
						
				for(int &i = itr[v]; i < graph[type][v].size();++i){
					const int v2 = graph[type][v][i];
					if (comp[v2] == -1){
						stk.push(v2);
						break;
					}
				}

				if(itr[v] == graph[type][v].size()){
					norder.push_back(v);
					stk.pop();
				}
			}
			idx++;
		}

		if (type == 1) break;
		reverse(begin(norder), end(norder));
		order = norder;
	}

	return make_tuple(order, comp);
}

vector<int> order, comp;

int main(void) {
	int n, m;
	cin >> n >> m;

	rep(i, m) {
		int a, b;
		cin >> a >> b;
		add_edge(a, b);
	}

	tie(order, comp) = scc(n);

	
	int q;
	cin >> q;
	rep(loop,q){
		int u,v;
		cin >> u >> v;
		if(comp[u]==comp[v])
			puts("1");
		else
			puts("0");
	}

	return 0;
}
#include <bits/stdc++.h>

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

const double PI = 3.14159265358979;

struct Edge{ ll cost, to; };
void solve()
{
  ll n, a, b, c;
  cin >> n;
  vector< vector<Edge> > G(n, vector<Edge>());
  for(int i = 0; i < n - 1; ++i)
  {
    cin >> a >> b >> c;
    a--; b--;
    G[a].emplace_back(Edge{c, b});
    G[b].emplace_back(Edge{c, a});
  }

  ll q, k, x, y;
  cin >> q >> k;
  k--;

  const ll INF = 1LL << 60;
  vector<ll> d(n, INF);
  d[k] = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, k)); // P(cost, start)
  while (!que.empty())
  {
    P p = que.top();
    que.pop();
    ll node = p.second;
    if (d[node] < p.first) continue;
    for (const auto& edge : G[node])
    {
        if (d[edge.to] > d[node] + edge.cost)
        {
            d[edge.to] = d[node] + edge.cost;
            que.push(P(d[edge.to], edge.to));
        }
    }
  }

  for(int i = 0; i < q; ++i)
  {
    cin >> x >> y;
    x--; y--;
    cout << d[x] + d[y] << endl;
  }

}

int main()
{
  fastio;
  solve();

  return 0;
}