#include <iostream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <vector>
#include <utility>
#include <numeric>
#include <string>
#include <set>
#include <map>
#include <tuple>
#include <queue>
#include <stack>
#define rep(i, a, n) for(int (i) = (int)(a);(i) < (int)(n);++(i))
#define repe(i, a, n) for(int (i) = (int)(a);(i) <= (int)(n);++(i))
#define repr(i, a, n) for(int (i) = (int)(a);(i) > (int)(n);--(i))
#define reper(i, a, n) for(int (i) = (int)(a);(i) >= (int)(n);--(i))
#define SIZE(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define RNG(x, n) &((x)[0]), &((x)[n])
#define SUBIF(a, b, x) ((x) ? (b) : (a))
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl;
#define no cout << "no" << endl;
#define pb push_back
#define EPS (1e-20)
#define INF 2147483647LL
#define MOD 1000000007LL
using llint = long long;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using SI = std::set<int>;
using MII = std::map<int, int>;
using TIII = std::tuple<int, int, int>;
using namespace std;


int n, m;
llint gc[110][110];
llint gt[110][110];


void warshallFloyd(){
	rep(k, 0, m)
		rep(i, 0, m)
			rep(j, 0, m)
				gc[i][j] = min(gc[i][j], gc[i][k]+gc[k][j]), gt[i][j] = min(gt[i][j], gt[i][k]+gt[k][j]);
}


int main(){
	while(cin >> n >> m, n || m){
		rep(i, 0, m){
			rep(j, 0, m){
				if(i != j) gc[i][j] = gt[i][j] = INF;
			}
		}
		rep(i, 0, n){
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			a--, b--;
			gc[a][b] = gc[b][a] = c;
			gt[a][b] = gt[b][a] = t;
		}
		warshallFloyd();
		int k;
		cin >> k;
		rep(i, 0, k){
			int p, q, r;
			cin >> p >> q >> r;
			p--, q--;
			if(!r) cout << gc[p][q] << endl;
			else cout << gt[p][q] << endl;
		}
	}

	return 0;
}
