#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin(); it!=(c).end(); it++)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pr(a) cout << (a) << endl
#define PR(a,b) cout << (a) << " " << (b) << endl;
#define F first
#define S second
typedef long long ll;
typedef pair<int,int> P;
const int MAX=1000000001;
const ll MAXL=1000000000000000001LL;
const ll mod=1000000007;
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int main() {
  int n,m;
  while(cin >> n >> m && (n||m)) {
    m++;
    int dp[m][m],dp2[m][m];
    fill(dp[0],dp[m],MAX);
    fill(dp2[0],dp2[m],MAX);
    rep(i,m) {
      dp[i][i]=0;
      dp2[i][i]=0;
    }
    rep(i,n) {
      int x,y,a,b;
      cin >> x >> y >> a >> b;
      dp[x][y]=min(dp[x][y],a);
      dp[y][x]=min(dp[y][x],a);
      dp2[x][y]=min(dp2[x][y],b);
      dp2[y][x]=min(dp2[y][x],b);
    }
    rep(k,m) {
      rep(i,m) {
	rep(j,m) {
	  dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
	  dp2[i][j]=min(dp2[i][j],dp2[i][k]+dp2[k][j]);
	}
      }
    }
    int k;
    cin >> k;
    rep(i,k) {
      int x,y,z;
      cin >> x >> y >> z;
      if(z) pr(dp2[x][y]);
      else pr(dp[x][y]);
    }
  }
  return 0;
}
