#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <string>
#include <queue>

const int INF = 1<<27;
using namespace std;
typedef pair<int,int> P;
string IntToString(int);
int StringToInt(string);
struct edje{
public:
  int from;
  int to;
  int cost;
  edje(int f,int t,int c){
    from = f;
    to = t;
    cost = c;
  }
};

void bellmanFord(vector<int> &map,vector<edje> &cost){
  for(int i = 0;i < map.size()-1;i++){
    for(int j = 0;j < cost.size();j++){
      map[cost[j].to] = min(map[cost[j].to],map[cost[j].from] + cost[j].cost);
    }
  }
}
void dijkstra(int s,vector<vector<P> >& cost,vector<int>& map){
  map[s] = 0;
  
  priority_queue<P,vector<P>,greater<P> > que;
  que.push(P(0,s));
  while(!que.empty()){
    P p = que.top();
    que.pop();
    int v = p.second;
    if(map[v] < p.first)continue;//
    for(int i = 0;i < cost[v].size();i++){
      if(map[cost[v][i].first] > map[v] + cost[v][i].second){
	map[cost[v][i].first] = map[v] + cost[v][i].second;
	que.push(P(map[cost[v][i].first],cost[v][i].first));
      }
    }

  }
}
int main(){
  while(1){
    int n,m;
    cin >> n >> m;
    if(n == 0 && m == 0)break;
    vector<vector<pair<int,int> > > cost(m);
    vector<vector<pair<int,int> > > time(m);
    for(int i = 0;i < n;i++){
      int a,b,c,t;
      cin >> a >> b >> c >> t;
      cost[a-1].push_back(pair<int,int>(b-1,c));
      cost[b-1].push_back(pair<int,int>(a-1,c));
      time[a-1].push_back(pair<int,int>(b-1,t));
      time[b-1].push_back(pair<int,int>(a-1,t));
    }
    int k;
    cin >> k;
    for(int i = 0;i < k;i++){
      int s,g,q;
      cin >> s >> g >> q;
      vector<vector<pair<int,int> > > tmp;
      if(q == 0) tmp = vector<vector<pair<int,int> > >(cost);
      else if(q == 1) tmp = vector<vector<pair<int,int> > >(time);
      vector<int> map(m,INF);
      //      map[s-1] = 0;
      dijkstra(s-1,tmp,map);
      cout << map[g-1] << endl;
      

    }
  }
  return 0;   
}
string IntToString(int number){
  stringstream ss;
  ss << number;
  return ss.str();
}
int StringToInt(string number){
  stringstream ss;
  ss << number;
  int numInt;
  ss >> numInt;
  return numInt;
  
}