#include <bits/stdc++.h>
using namespace std;
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)),UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define MUL(a, b) a = (a * b) % mod
#define repi(i,m,n) for(int i = m;i < n;i++)
#define drep(i,n,m) for(int i = n;i >= m;i--)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) repi(i,1,n+1)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define fi first
#define se second
typedef pair<int,int> P;
typedef pair<int, P> PP;
typedef vector<int> vi;
const int inf = 1e9+7;
const int INF = 1e18+7;
//26

int a[200][200], b[200][200];
signed main(){
	while(1){
		int n, m;
		scanf("%lld%lld", &m, &n);
		if(n == 0 and m == 0)break;
		rep(i,200)rep(j,200)a[i][j] = b[i][j] = inf;
		rep(i,200)a[i][i] = b[i][i] = 0;
		rep(i,m){
			int x, y, c, t;
			scanf("%lld%lld%lld%lld", &x, &y, &c, &t);x--;y--;
			a[x][y] = a[y][x] = min(a[x][y], c);
			b[x][y] = b[y][x] = min(b[x][y], t);
		}
		rep(i,n)rep(j,n)rep(k,n){
			a[j][k] = a[k][j]= min(a[j][k], a[j][i]+a[i][k]);
			b[j][k] = b[k][j]= min(b[j][k], b[j][i]+b[i][k]);
		}
		int k;
		scanf("%lld", &k);
		rep(i,k){
			int x, y, f;
			scanf("%lld%lld%lld", &x, &y, &f);x--;y--;
			//printf("*%lld %lld %lld*\n", x, y, a[x][y]);
			if(f == 0)printf("%lld\n", a[x][y]);
			else printf("%lld\n", b[x][y]);
		}
		/*rep(i,n){
			rep(j,n)printf("%lld ", a[i][j]);
			printf("\n");
		}*/
	}
	return 0;
}




