#include <iostream>
#include <algorithm>
using namespace std;

const int size = 101;
int G1[size][size]; // time
int G2[size][size]; // money

int main() {
  int n, m;
  while ( cin >> n >> m && n && m ) {
    for ( int i = 0; i < size; i++ ) {
      for ( int j = 0; j < size; j++ ) {
	G1[i][j] = -1;
	G2[i][j] = -1;
      }
    }
    for ( int i = 0; i < n; i++ ) {
      int a, b;
      cin >> a >> b;
      int t, c;
      cin >> c >> t;
      G1[a][b] = G1[b][a] = t;
      G2[a][b] = G2[b][a] = c;
    }
    for ( int k = 1; k <= m; k++ ) {
      for ( int i = 1; i <= m; i++ ) {
	for ( int j = 1; j <= m; j++ ) {
	  if ( G1[i][k] == -1 || G1[k][j] == -1 ) continue;
	  if ( G1[i][j] == -1 ) G1[i][j] = G1[i][k] + G1[k][j];
	  G1[i][j] = min( G1[i][j], G1[i][k] + G1[k][j] );
	}
      }
    }
    for ( int k = 1; k <= m; k++ ) {
      for ( int i = 1; i <= m; i++ ) {
	for ( int j = 1; j <= m; j++ ) {
	  if ( G2[i][k] == -1 || G2[k][j] == -1 ) continue;
	  if ( G2[i][j] == -1 ) G2[i][j] = G2[i][k] + G2[k][j];
	  G2[i][j] = min( G2[i][j], G2[i][k] + G2[k][j] );
	}
      }
    }
    {
      int k;
      cin >> k;
      for ( int i = 0; i < k; i++ ) {
	int p, q, r;
	cin >> p >> q >> r;
	cout << ( r ? G1[p][q] : G2[p][q] ) << endl; 
      }
    }
  }
  return 0;
}