#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int c[111][111];
int t[111][111];
int main(void){
  while(1){
    int n,m;
    cin >> n >> m;

    if(!n && !m) break;

    for(int i = 1 ; i <= m; i++){ 
      fill(c[i], c[i] + m + 1, 1 << 20);
      fill(t[i], t[i] + m + 1, 1 << 20);
      c[i][i] = 0;
      t[i][i] = 0;
    }
    for(int i = 0 ; i < n; i++){
      int a,b,cost,time;
      cin >> a >> b >> cost >> time;
      c[a][b] = min(c[a][b],cost);
      c[b][a] = min(c[b][a],cost);
      t[a][b] = min(t[a][b],time);
      t[b][a] = min(t[b][a],time);
    }
    for(int k =1; k <= m; k++){
      for(int i = 1; i <= m; i++){
	for(int j = 1; j <= m; j++){
	  c[i][j] = min(c[i][j],c[i][k] + c[k][j]);
	  t[i][j] = min(t[i][j],t[i][k] + t[k][j]);
	}
      }
    }
    int q;
    cin >> q;
    for(int i = 0; i < q; i++){
      int a,b,f;
      cin >> a >> b >> f;
      if(f){
	cout << t[a][b] << endl;
      }else{
	cout << c[a][b] << endl;
      }
    }
  }
}