#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define FOR(i,a,b) for(int i=(a);i<(int)(b);++i)
#define rep(i,n) FOR(i,0,n)
#define ALL(x) (x).begin(), (x).end()
#define SYAMU(a) memset(a,0,sizeof(a))
#define pb push_back

const int INF = 114514810;
int n,m;
int cost[3000][3000], _time[3000][3000];

void init() {
    rep(i,3000) rep(j,3000) cost[i][j] = _time[i][j] = INF;
}

int main() {
    while(cin>>n>>m && n) {
        init();
        rep(i,m) {
            int a,b,c,t; scanf("%d%d%d%d",&a,&b,&c,&t);
            --a; --b;
            cost[a][b] = cost[b][a] = c;
            _time[a][b] = _time[b][a] = t;
        }
        
        rep(k,n) rep(i,n) rep(j,n) {
            cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
            _time[i][j] = min(_time[i][j], _time[i][k] + _time[k][j]);
        }

        int k; cin >> k;
        rep(i,k) {
            int p,q,r;
            cin>>p>>q>>r;
            if(r==0)
                cout << cost[p][q] << "\n";
            else
                cout << _time[p][q] << "\n";
        }
    }

    return 0;
}