#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reps(i,0,n)

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1001001001;

int main()
{
	int n, m;
	while(scanf("%d%d", &n, &m), n){
		vvi C(m, vi(m, INF));
		vvi T = C;
		rep(i, n){
			int a, b, c, t;
			scanf("%d%d%d%d", &a, &b, &c, &t);
			--a;
			--b;
			C[a][b] = C[b][a] = c;
			T[a][b] = T[b][a] = t;
		}
		
		rep(k, m) rep(i, m) rep(j, m){
			C[i][j] = min(C[i][j], C[i][k]+C[k][j]);
			T[i][j] = min(T[i][j], T[i][k]+T[k][j]);
		}
		
		int k;
		scanf("%d", &k);
		rep(i, k){
			int a, b, r;
			scanf("%d%d%d", &a, &b, &r);
			--a;
			--b;
			printf("%d\n", r==0 ? C[a][b] : T[a][b]);
		}
	}
	return 0;
}