#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;
  cin >> n >> m;

  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] = 0;
    time[i][i] = 0;
  }

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

  int k;
  cin >> k;
  REP(_tmp,k){
    int p,q,r;
    cin >> p >> q >> r;

    vector< vector<int> > data(m+1, 0);
    data = r ? time : cost;

    deque<bool> reached(m+1,0);
    vector<int> dis(m+1,INF);
    dis[p] = 0;

    while(true){
      int minn = -1;
      REP(i,reached.size()){
	if( ! reached[i] ){
	  if( minn==-1 || dis[minn] > dis[i] ){
	    minn = i;
	  }
	}
      }
      if( minn == q )
	break;

      reached[minn] = true;
      REP(i,data[minn].size()+1){
	dis[i] = min( dis[minn] + data[minn][i], dis[i] );
      }
    }

    cout << dis[q] << endl;
  }

  return 0;
}