#include<iostream>
#include<vector>
#include<cstdio>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<cmath>
#include<deque>
#include<queue>
using namespace std;

using ll = long long;
const int INF = 1001001001;
const ll LLINF = 1001001001001001001;
const int MOD = 1000000007;

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

vector<vector<pair<int, ll>>> eda(100000 + 5); // pair には num, dist
vector<ll> dist(100000 + 5);

void makeCost(int n, int k)
{
  queue<pair<pair<int,ll>, int>> q; // pair には ((num, dist) from)
  dist[k] = 0;
  for (auto a : eda[k]) {
    q.push(make_pair(a, k));
  }

  while (!q.empty()) {
    auto now = q.front();
    q.pop();
    int number = now.first.first;
    int from = now.second;
    ll distance = now.first.second;
    dist[number] = dist[from] + distance;
    for (auto tmp : eda[number]) {
      if (tmp.first == from) continue;
      q.push(make_pair(tmp, number));
    }
  }
}

int main()
{
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    eda[a].push_back(make_pair(b, c));
    eda[b].push_back(make_pair(a, c));
  }

  int q, k;
  cin >> q >> k;

  makeCost(n, k);

  rep (i, q) {
    int x, y;
    cin >> x >> y;
    cout << dist[x] + dist[y] << endl;
  }
}


#include<iostream>
using namespace std;

int n, m, k;
int t[100][100], c[100][100];
int INF = 1<<21;

void solve(){
    for(int k = 0; k < m; k++){
        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++){
                t[i][j] = min(t[i][j], t[i][k] + t[k][j]);
                c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
            }
        }
    }
}

int main(){
    while(1){
        cin >> n >> m;

        if(n + m == 0)  break;
        
        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++){
                c[i][j] = t[i][j] = INF;
            }
        }
        
        int a, b, cost, tim;
        for(int i = 0; i < n; i++){
            cin >> a >> b >> cost >> tim;
            a--; b--;
            c[a][b] = c[b][a] = cost;
            t[a][b] = t[b][a] = tim;
        }

        solve();

        cin >> k;
        int p, q, r;
        for(int i = 0; i < k; i++){
            cin >> p >> q >> r;
            p--; q--;
            if(r == 0)          cout << c[p][q] << endl;
            else if(r == 1)     cout << t[p][q] << endl;
        }

    }
    return 0;
}
