#include <iostream>
#include <vector>
#include <utility>
#include <queue>

#define INF 100000000

using namespace std;

int dijk(vector<vector<pair<int,int> > > &graph, int start, int goal){
        int total;
        priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > que;
        que.push( make_pair(0,start) );
        vector<int> cost(graph.size(), INF);
        while( !que.empty() ){
                pair<int,int> tmp = que.top(); 
                que.pop();
                if( cost[ tmp.second ] < INF ) continue;

        //      cout << tmp.second << "  " << tmp.first << "  " << endl;
                cost[tmp.second] = tmp.first;

                if( tmp.second == goal ) break;
                for(int i=0; i < graph[tmp.second].size(); i++){
                        if( cost[ graph[tmp.second][i].first ] == INF )
                                que.push( make_pair( tmp.first + graph[tmp.second][i].second, graph[tmp.second][i].first ) );
                }
        }
        return cost[goal];
}


int main(){
        int n, m, a, b, cost, time;

        while( cin >> n >> m , n && m ){
                vector<vector<pair<int, int> > > graph(m);
                vector<vector<pair<int, int> > > gtime(m);

                for(int i=0; i<n; i++){
                        cin >> a >> b >> cost >> time;
                        --a; --b;
                        graph[a].push_back( make_pair(b, cost) );
                        graph[b].push_back( make_pair(a, cost) );
                        gtime[a].push_back( make_pair(b, time) );
                        gtime[b].push_back( make_pair(a, time) );
                }

                int k;

                cin >> k;
                for(int i=0; i<k; i++){
                        int p, q, r;
                        cin >> p >> q >> r;
                        p--; q--;
                        if( r ){
                                cout << dijk( gtime, p, q ) << endl;
                        }else{
                                cout << dijk( graph, p, q ) << endl;
                        }
                }
        }

        return 0;
}