#include<iostream>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)

const int MAXN = 301, MAXM = 101;
const int INF = 1000000 ;
const int TIME = 1, COST = 0;

int cost[2][MAXM][MAXM] = {};
int d[MAXM] = {};
bool used[MAXM] = {};

int n, m ,a, b, c, t, k;

int p, q, r;

int main()
{
    while(cin >> n >> m && n)
    {
        memset(cost, INF, sizeof(cost));

        REP(n)
        {
            cin >> a >> b >> c >> t;
            a--;
            b--;
            cost[COST][a][b] = cost[COST][b][a] = c;
            cost[TIME][a][b] = cost[TIME][b][a] = t;
        }
        cin >> k;

        rep(s,k)
        {
            cin >> p >> q >> r;
            p--;
            q--;
            memset(d, INF, sizeof(d));
            memset(used, false, sizeof(used));
            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;
}