#include <bits/stdc++.h>
#define rep(i,n) for(long long i=0; i<(n); i++)
#define int long long
#define mod 1000000007
#define F first
#define S second
#define P pair<long long,long long>
#define all(a) a.begin(),a.end()
#define INF 1000000000000000000
using namespace std;
int C[110][110];
int T[110][110];
int m,n,q,f,to,t,c,r;
        
signed main(void){
    while(true){
        cin>>m>>n;
        if(m==0&&n==0)break;
        rep(i,110)rep(j,110){
            C[i][j]=INF;T[i][j]=INF;
        }
        rep(i,m){
            cin>>f>>to>>c>>t;f--;to--;
            C[f][to]=c;C[to][f]=c;
            T[f][to]=t;T[to][f]=t;
        }
        rep(k,n)rep(i,n)rep(j,n){
            C[i][j]=min(C[i][j],C[i][k]+C[k][j]);
            T[i][j]=min(T[i][j],T[i][k]+T[k][j]);
        }
        
        cin>>q;
        rep(i,q){
            cin>>f>>to>>r;f--;to--;
            if(r==0)cout<<C[f][to]<<endl;
            if(r==1)cout<<T[f][to]<<endl;
        }
        
    }
}
