#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
using Graph = vector<vector<pair<int,int>>>;
int dij(int a,int b,Graph G){
  vector<ll>dist(G.size(),101640100165);
  dist[a] = 0;
  priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
  pq.push({0,a});
  while(!(pq.empty())){
    int now = pq.top().second,now_cost = pq.top().first;
    pq.pop();
    for(auto i : G[now]){
      if(dist[i.first] <= now_cost + i.second)continue;
      dist[i.first] = now_cost + i.second;
      pq.push({dist[i.first],i.first});
    }
  }
  return dist[b];
}
int main(){
  while(true){
    int n,m;
    cin >> n >> m;
    if(n == 0 && m == 0)return 0;
    Graph cost(m),time(m);
    rep(i,n){
      int a,b;
      cin >> a >> b;
      a--,b--;
      int c,t;
      cin >> c >> t;
      cost[a].push_back({b,c});
      cost[b].push_back({a,c});
      time[a].push_back({b,t});
      time[b].push_back({a,t});
    }
    int cnt;
    cin >> cnt;
    rep(i,cnt){
      int a,b,c;
      cin >> a >> b >> c;
      a--,b--;
      if(c)cout << dij(a,b,time) << endl;
      else cout << dij(a,b,cost) << endl;
    }
  }
  return 0;
}
