#include <algorithm>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define IREP(it,array) for(vector<int>::iterator it=array.begin(); it!=array.end(); ++it)
#define SREP(it,array) for(vector<string>::iterator it=array.begin(); it!=array.end(); ++it)

#define MP       make_pair
#define PB       push_back
#define ALL(x)   (x).begin(),(x).end()

const int INF = 1<<29;
const double EPS = 1e-9;
double zero(double d){
  return d < EPS ? 0.0 : d;
}

typedef long long LL;
typedef pair<int,int> P;


int main()
{
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(7);

  int n,m;
  while(cin >> n >> m){
    if(n==0&&m==0)
      return 0;

    vector< vector<int> > cost(m+1, vector<int>(m+1,INF)),
      time(m+1, vector<int>(m+1,INF));

    REP(i,cost.size()){
      cost[i][i] = time[i][i] = 0;
    }

    REP(i,n){
      int a,b,c,t;
      cin >> a >> b >> c >> t;
      cost[a][b] = cost[b][a] = c;
      time[a][b] = time[b][a] = t;
    }

    REP(i,cost.size()){
      REP(j,cost.size()){
	REP(k,cost.size()){
	  cost[j][k] = min( cost[j][k], cost[j][i] + cost[i][k] );
	  time[j][k] = min( time[j][k], time[j][i] + time[i][k] );
	}
      }
    }

    int k;
    cin >> k;
    REP(_tmp,k){
      int p,q,r;
      cin >> p >> q >> r;
      int res= r ? time[p][q] : cost[p][q];
      cout << res << endl;
    }
  }

  return 0;
}