#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()

const int MAXN = 3000, MAXM = 100, C = 0, T = 1;
const int INF = 1 << 28;
int cost[2][MAXM + 10][MAXM + 10];

int main()
{
    int n, m;
    while(cin >> n >> m && n)
    {
        int a, b, cc, ct;
        REP(MAXM)rep(j,MAXM)rep(e,2)cost[e][i][j] = INF;
        REP(n)
        {
            cin >> a >> b >> cc >> ct; a--; b--;
            cost[C][a][b] = cost[C][b][a] = min(cc, cost[C][a][b]);
            cost[T][a][b] = cost[T][b][a] = min(ct, cost[T][a][b]);
        }

        int k, p, q, r;
        cin >> k;
        int d[MAXM] = {}, used[MAXM] = {};
        rep(s,k)
        {
            cin >> p >>q >> r; p--; q--;
            memset(used,0, sizeof(used));
            REP(m) d[i] = INF;
            d[p] = 0;
            while(true)
            {
                int v = -1;
                rep(i, m)if(!used[i] && (v == -1 || d[i] < d[v])) v = i;
                if(v == -1) break;
                used[v] = true;
                rep(i,m) d[i] = min(d[i], d[v] + cost[r][v][i]);
            }
            cout << d[q] << endl;
        }
    }
    return 0;
}