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

const int INF = 1<<28;
using namespace std;
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){
  while(1){
    bool update = false;
    for(int j = 0;j < cost.size();j++){
      if(map[cost[j].to] > map[cost[j].from] + cost[j].cost){
	map[cost[j].to] = min(map[cost[j].to],map[cost[j].from] + cost[j].cost);
	update = true;
      }
    }
    if(!update)break;
  }
}
int main(){
  while(1){
    int n,m;
    cin >> n >> m;
    if(n == 0 && m == 0)break;
    vector<edje> cost;
    vector<edje> time;
    
    for(int i = 0;i < n;i++){
      int a,b,c,t;
      cin >> a >> b >> c >> t;
      edje cs(a-1,b-1,c);
      cost.push_back(cs);
      cs= edje(b-1,a-1,c);
      cost.push_back(cs);
      edje tm(a-1,b-1,t);
      time.push_back(tm);
      tm = edje(b-1,a-1,t);
      time.push_back(tm);
    }
    int k;
    cin >> k;
    for(int i = 0;i < k;i++){
      int s,g,q;
      cin >> s >> g >> q;
      vector<edje> tmp;
      if(q == 0) tmp = vector<edje>(cost);
      else if(q == 1) tmp = vector<edje>(time);
      vector<int> map(m,INF);
      map[s-1] = 0;
      bellmanFord(map,tmp);
      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;
  
}