#include<iostream>
#include<queue>
#include<algorithm>
#include<vector>
#define INF 1<<28
using namespace std;

struct edge{
  int to,cost;
};

typedef pair <int, int> P;

int n,m;
vector <edge> G[500][2];
int d[500][2];

void dijkstra(int s,int k){

  priority_queue <P, vector<P>, greater<P> > que;
  for(int i=0;i<n;i++) d[i][0] = d[i][1] = INF;

  d[s][k] = 0;
  que.push( P(0, s) );

  while( !que.empty() ){
    P p = que.top(); que.pop();
    int v = p.second;

    if(d[v][k] < p.first) continue;

    for(int i=0;i<G[v][k].size();i++){
      edge e = G[v][k][i];

      if(d[e.to][k] > e.cost + d[v][k]){
	d[e.to][k] = e.cost + d[v][k];
	que.push( P(d[e.to][k],e.to) );
      }
    }
  }
}

int main(){

  int a,b,cost,time,l;

  while(true){
    cin >> n >> m;
    if(!n && !m) break;
    
    edge z;
    for(int i=0;i<n;i++){
      cin >> a >> b >> cost >> time;
      
      z.to = b, z.cost = cost;
      G[a][0].push_back(z);
      z.cost = time;
      G[a][1].push_back(z);
      
      z.to = a, z.cost = cost;
      G[b][0].push_back(z);
      z.cost = time;
      G[b][1].push_back(z);
      
    }
    
    cin >> l;
    for(;l>0;l--){
      int c;
      cin >> a >> b >> c;
      dijkstra(a,c);
      cout << d[b][c] << endl;
    }
  
  }  
}
  