#include<bits/stdc++.h>
#define rep(i,f,n) for(ll i=(f); (i) < (n); i++)
#define repe(i,f,n) for(ll i=(f); (i) <= (n); i++)
using namespace std;
using ll = long long;

ll INF = 1LL << 60;
using G = vector<map<int, int>>;

void
dfs(int node, ll count, G &arr, vector<bool> seen, vector<long> &to_K)
{
  seen[node] = true;
  for(auto child: arr[node]){
    if(seen[child.first]) continue;
    to_K[child.first] = count + child.second;
    dfs(child.first, count + child.second, arr, seen, to_K);
  }
}

int 
main() {
  int N; cin >> N;
  G arr(N + 1);
  rep(i, 0, N - 1){
    int a, b, c; cin >> a >> b >> c;
    arr[a][b] = c;
    arr[b][a] = c;
  }
  vector<long> to_K(N + 1);

  int Q, K; cin >> Q >> K;
  vector<pair<int, int>> q(Q);
  rep(i, 0, Q){
    cin >> q[i].first;
    cin >> q[i].second;
  }

  vector<bool> seen(N + 1, false);
  //dfs(K, 0, arr, seen, to_K);

  queue<int> que;
  que.push(K);
  seen[K] = true;
  to_K[K] = 0;
  while(!que.empty()){
    int node = que.front();
    que.pop();
    for(auto child : arr[node]){
      if(seen[child.first]) continue;
      to_K[child.first] = arr[node][child.first] + to_K[node];
      seen[child.first] = true;
      que.push(child.first);
    }
  }


  rep(i, 0, Q){
    cout << to_K[q[i].first] + to_K[q[i].second] << endl;
  }

}
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int N, M;
    cin >> N >> M;

    vector<vector<int>> tree(N);
    vector<int> n_in(N);
    for (int i = 0; i < N - 1 + M; i++)
    {
        int Ai, Bi;
        cin >> Ai >> Bi;
        Ai--;
        Bi--;
        tree.at(Ai).push_back(Bi);
        n_in.at(Bi) += 1;
    }

    int r;
    for (int i = 0; i < N; i++)
    {
        if (n_in.at(i) == 0)
        {
            r = i;
            break;
        }
    }

    vector<int> l;
    queue<int> q;
    q.push(r);
    while (q.size())
    {
        int n = q.front();
        q.pop();

        l.push_back(n);
        for (auto &&e : tree.at(n))
        {
            n_in.at(e)--;
            if (n_in.at(e) == 0)
            {
                q.push(e);
            }
        }
    }

    vector<int> p(N, 0);
    for (auto &&n : l)
    {
        for (auto &&e : tree.at(n))
        {
            p.at(e) = n + 1;
        }
    }

    for (int i = 0; i < N; i++)
    {
        cout << p.at(i) << endl;
    }
}