#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it = (c).begin(); it != (c).end(); it++)
#define X first
#define Y second

using namespace std;

typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
const int N = 101;

int cost[N][N][2];
int d[N][N][2];
int n;
bool used[N];

void dijkstra(int, int);

int main(void){
    for(int m; cin >> m >> n, n;){
        rep(i, n) rep(j, n) rep(k, 2){
            cost[i][j][k] = INF;
            d[i][j][k] = -1;
        }
        while(m--){
            int a, b, c, t; cin >> a >> b >> c >> t;
            a--, b--;
            cost[a][b][0] = c;
            cost[b][a][0] = c;
            cost[a][b][1] = t;
            cost[b][a][1] = t;
        }
        int k; cin >> k;
        while(k--){
            int p, q, r; cin >> p >> q >> r;
            p--, q--;
            if(!~d[p][q][r]) dijkstra(p, r);
            cout << d[p][q][r] << endl;
        }
    }

    return 0;
}

void dijkstra(int s, int t){
    rep(i, n) d[s][i][t] = INF;
    rep(i, n) used[i] = false;
    d[s][s][t] = 0;

    while(1){
        int v = -1;
        rep(u, n) if(!used[u] && (!~v || d[s][u][t] < d[s][v][t])) v = u;

        if(!~v) break;
        used[v] = true;

        rep(u, n) d[s][u][t] = min(d[s][u][t], d[s][v][t] + cost[v][u][t]);
    }
}