#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <stack>
#define INF 100000000
typedef long long int lli;
using namespace std;
int dc[101][101];
int dt[101][101];

int main(){
	int n,m;
	while(cin>>n>>m&&n&&m){
		for(int i=0; i<101; ++i){
			for(int j=0; j<101; ++j){
				if(i==j){
					dc[i][j]=0;
					dt[i][j]=0;
				} else {
					dc[i][j]=INF;
					dt[i][j]=INF;
				}
			}
		}
		for(int i=0; i<n; ++i){
			int a,b,cost,time;
			cin >> a >> b >> cost >> time;
			dc[a][b]=cost;
			dc[b][a]=cost;
			dt[a][b]=time;
			dt[b][a]=time;
		}
		for(int k=1; k<=m; ++k){
			for(int i=1; i<=m; ++i){
				for(int j=1; j<=m; ++j){
					dc[i][j]=min(dc[i][j],dc[i][k]+dc[k][j]);
					dt[i][j]=min(dt[i][j],dt[i][k]+dt[k][j]);
				}
			}
		}
		int K;
		cin >> K;
		for(int i=0; i<K; ++i){
			int p,q,r;
			cin >> p >> q >> r;
			if(r==0){
				cout << dc[p][q] << "\n";
			} else {
				cout << dt[p][q] << "\n";
			}
		}
	}
	return 0;
}