#include <cstdio>
#include <algorithm>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define INF 1 << 20
#define MAX_M 101
using namespace std;

int main(){
  int n, m, a, b, c, t, p, q, r, s;  
  int C[MAX_M][MAX_M], T[MAX_M][MAX_M];
  while(true){
	scanf("%d %d", &n, &m);
	if((n | m) == 0){
	  break;
	}
	fill((int*) C, (int*)(C + lengthof(C)), INF);
	fill((int*) T, (int*)(T + lengthof(T)), INF);
	REP(i,n){
	  scanf("%d %d %d %d", &a, &b, &c, &t);
	  C[a][b] = C[b][a] = c;
	  T[a][b] = T[b][a] = t;
	}
	REP(k,m+1){
	  REP(i,m+1){
		REP(j,m+1){
		  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]);
		}
	  }
	}
	scanf("%d", &s);
	REP(i,s){
	  scanf("%d %d %d", &p, &q, &r);
	  if(r == 0){
		printf("%d\n", C[p][q]);
	  }
	  else{
		printf("%d\n", T[p][q]);
	  }
	}
  }
  return 0;
}