#include <algorithm>
#include <iostream>
#include <map>

using namespace std;

#define fst first
#define snd second
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
typedef pair<int,int> pii;

int n,m;
pii cost[110][110];

const int inf=1<<27;

int main(void){
	while(cin >> n >> m,n|m){
		rep(i,110)rep(j,110) cost[i][j].fst=cost[i][j].snd=((i==j)?0:inf);
		rep(i,n){
			int f,t;
			cin >> f >> t;
			f--;t--;
			int cos,tim;
			cin >> cos >> tim;
			cost[f][t].fst=cost[t][f].fst=cos; 
			cost[f][t].snd=cost[t][f].snd=tim;
		}

		rep(k,m)rep(i,m)rep(j,m){
			cost[i][j].fst=min(cost[i][j].fst,cost[i][k].fst+cost[k][j].fst);
			cost[i][j].snd=min(cost[i][j].snd,cost[i][k].snd+cost[k][j].snd);
		}
		int q;
		cin >> q;
		rep(i,q){
			int f,t,c;
			cin >> f >> t >> c;
			f--;t--;
			if(c)
				cout << cost[f][t].snd << endl;
			else
				cout << cost[f][t].fst << endl;
		}
	}
	return 0;
}