#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};
const int MAX_N = 3010;
int n, m;
struct edge { int to, cost, time; };
vector<edge> G[MAX_N];
int solve(int start, int goal, int flag)
{
    vector<int> res(MAX_N, INF);
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(P(0, start));
    res[start] = 0;
    while(not que.empty())
    {
        int w = que.top().first, v = que.top().second;
        que.pop();
        if(res[v] < w) continue;
        for(int u = 0; u < G[v].size(); u++)
        {
            edge e = G[v][u];
            int num = (flag ? e.time : e.cost);
            if(w + num < res[e.to])
            {
                res[e.to] = w + num;
                que.push(P(res[e.to], e.to));
            }
        }
    }
    return res[goal];
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> n >> m, n | m)
    {
        for(int i = 0; i < MAX_N; i++) G[i].clear();
        for(int i = 0; i < n; i++)
        {
            int a, b, c, t; cin >> a >> b >> c >> t;
            a--, b--;
            G[a].push_back({b, c, t});
            G[b].push_back({a, c, t});
        }
        int k; cin >> k;
        for(int i = 0; i < k; i++)
        {
            int p, q, f; cin >> p >> q >> f;
            p--, q--;
            cout << solve(p, q, f) << endl;
        }
    }
    return 0;
}


