#include<iostream>
using namespace std;

int t[303][303];
int c[303][303];

int main() {
  for( int N, M; cin >> M >> N, (N || M); ) {
    
    for(int i = 0; i < N; i++)
      for(int j = 0; j < N; j++) {
        t[i][j] = 987654321;
        c[i][j] = 987654321;
      }

    for(int i = 0; i < N; i++) t[i][i] = c[i][i] = 0;
    
    for(int n = 0; n < M; n++) {
      int a, b, time, cost;
      cin >> a >> b >> time >> cost;
      a--, b--;
      t[a][b] = t[b][a] = time;
      c[a][b] = c[b][a] = cost;
    }
    
    for(int k = 0; k < N; k++)
      for(int i = 0; i < N; i++)
        for(int j = 0; j < N; j++) {
          t[i][j] = min(t[i][j], t[i][k] + t[k][j]);
          c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
        }
    
    
    cin >> M;
    for(int m = 0; m < M; m++) {
      int a, b, r;
      cin >> a >> b >> r;
      a--, b--;
      if( r == 0 ) 
        cout << c[a][b] << endl;
      else 
        cout << t[a][b] << endl;
    }
    
  }
}