#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>

using namespace std;

#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define rep(i,n) for(int i=0;i<(n);i++)
#define tr(it,container) for(typeof(container.begin()) it = container.begin(); \
                                                  it != container.end(); ++it)
#define mp(a,b) make_pair((a),(b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int,int> pii;

// →↑←↓
const int dx[] = {1,0,-1,0};
const int dy[] = {0,-1,0,1};


const double EPS = 1e-9;

struct Edge{
    int from,to,cost,time;
    Edge(int from,int to,int cost,int time)
        : from(from),to(to),cost(cost),time(time) {};
};

int main(){
    int n,m;
    cin >> n >> m;
    vector<vector<Edge> > V(m);

    for(int i=0;i<n;i++){
        int a,b,cost,time;
        cin >> a >> b >> cost >> time;
        a--;b--;
        V[a].push_back(Edge(a,b,cost,time));
        V[b].push_back(Edge(b,a,cost,time));
    }
    int k;
    cin >> k;
    for(int i=0;i<k;i++){
        int p,q,r;
        cin >> p >> q >> r;
        p--,q--;
        vector<char> visited(m,false);
        //                 cost,where
        priority_queue<pair<int,int> ,vector<pair<int,int> >, greater<pair<int,int> > > Q;
        Q.push(make_pair(0,p));
        while(!Q.empty()){
            int cost,where;
            cost = Q.top().first;
            where = Q.top().second;
            Q.pop();
            if(visited[where]) continue;
            if(where == q){
                cout << cost << endl;
                break;
            }
            visited[where] = true;
            for(int j=0;j<(int)V[where].size();j++){
                if(r == 0){
                    Q.push(make_pair(V[where][j].cost+cost,V[where][j].to));
                }else{
                    Q.push(make_pair(V[where][j].time+cost,V[where][j].to));
                }
            }
        }
    }
    return 0;
}