#include<iostream>
#include<algorithm>
using namespace std;
const int INF = 111111111;
int main(void){
  int n,m,a,b,cost,time,l,p,q,r;
  int c[105][105],t[105][105];
  for( ; ; ){
    cin >> m >> n;
    if(!m && !n) break;

    for(int i = 1;i <= n; i++){
      for(int j = 1;j <= n; j++){
	if(i == j) c[i][j] = t[i][j] = 0;
	else c[i][j] = t[i][j] = INF;
      }
    }

    for(int i = 0;i < m; i++){
      cin >> a >> b >> cost >> time;
      c[a][b] = c[b][a] = cost;
      t[a][b] = t[b][a] = time;
    }

    for(int k = 1;k <= n; k++)
      for(int i = 1;i <= n; i++)
	for(int j = 1;j <= n; j++) c[i][j] = min(c[i][j], c[i][k] + c[k][j]);

    for(int k = 1;k <= n; k++)
      for(int i = 1;i <= n; i++)
	for(int j = 1;j <= n; j++) t[i][j] = min(t[i][j], t[i][k] + t[k][j]);

    cin >> l;
    for(int i = 0;i < l; i++){
      cin >> p >> q >> r;
      cout << (r ? t[p][q] : c[p][q]) << endl;
    }
  }
}
