#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF 1000000000
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int n, m;
int d[101][101][2];

int main(void){

	while(cin >> n >> m){
		if(n + m == 0)break;
		fill_n(**d, 101*101*2, 1000000);

		for(int i = 0; i < n; i++){
			int from, to, t_cost, m_cost;
			cin >> from >> to >> t_cost >> m_cost;
			d[from][to][0] = m_cost;
			d[to][from][0] = m_cost;
			d[from][to][1] = t_cost;
			d[from][to][1] = t_cost;
		}
		rep(k, n){
			rep(i, n){
				rep(j, n){
					d[i][j][0] = min(d[i][j][0], d[i][k][0] + d[k][j][0]);
					d[i][j][1] = min(d[i][j][1], d[i][k][1] + d[k][j][1]);
				}
			}
		}
		int t;
		cin >> t;
		rep(i, t){
			int a, b, r;
			cin >> a >> b >> r;
			cout << d[a][b][r] << endl;
		}
	}




	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}