#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,b) loop(i,0,b)


struct edge {
    int from,to,money,time;
};
int d[101];
vector<edge> E;

int main(){
    int n,m;
    while(cin>>n>>m, n|m){
        E.resize(n);
        for(edge& e:E){
            cin>>e.from>>e.to>>e.money>>e.time;
        }
        int k; cin>>k;
        rep(i,k){
            int a,b,c; cin>>a>>b>>c;
            for(int& x:d) x=1<<30;
            d[a]=0;
            while(1){
                bool end=true;
                for(edge& e:E){
                    rep(dir,2){
                        int cost=(c==0 ? e.money : e.time);
                        if(d[e.to]>d[e.from]+cost){
                            d[e.to]=d[e.from]+cost;
                            end=false;
                        }
                        swap(e.from,e.to);
                    }
                }
                if(end) break;
            }
            cout<<d[b]<<endl;
        }
    }
}