#include<iostream>
#include<stack>
#include<queue>
#include<map>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) for(int i=0;i<n;i++)

const int maxn=311,maxm=110,INF = 110000;
const int COST = 0, TIME = 1;

int n,m;

struct edge
{
    unsigned int to,cost;
    edge(int to,int cost):to(to),cost(cost){};
};

typedef pair<int,int> p;

int main()
{
    while(cin >> n >> m && (n||m))
    {
        vector<edge> cost[2][maxn];
        int a,b,tc,tt;
        REP(n)
        {
            cin >> a >> b >> tc >> tt;
            cost[TIME][a].push_back(edge(b,tt));
            cost[TIME][b].push_back(edge(a,tt));
            cost[COST][a].push_back(edge(b,tc));
            cost[COST][b].push_back(edge(a,tc));
        }

        int k; cin >> k;

        int start,goll,r;
        while(k--)
        {
            cin >> start >> goll >> r;
            int d[maxm] = {};
            memset(d,INF,sizeof(d));
            d[start] = 0;

            priority_queue<p,vector<p>, greater<p> > que;
            que.push(p(0,start) );

            while(!que.empty())
            {
                p tp = que.top(); que.pop();
                int v = tp.second; //確定ノード
                //cout << v << endl;
                if(d[v] < tp.first) continue;
                REP(cost[r][v].size())
                {
                    edge e = cost[r][v][i];
                    if(d[e.to] > d[v] + e.cost)
                    {
                        d[e.to] = d[v] + e.cost;
                        que.push(p(d[e.to], e.to));
                    }
                }
            }
            cout << d[goll] << endl;
        }
    }

    return 0;
}