#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 99999
int dijkstra(int x,int y,int p);
int D[110][110][2];/*Adjacency matrix*/
int m;
int main(){
    while(1){
        int n;
        cin >> n >> m;
        if(n == 0 && m == 0)break;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(i == j){
                    D[i][j][0] = 0;
                    D[i][j][1] = 0;
                    continue;
                }
                D[i][j][0] = INF;
                D[i][j][1] = INF;
            }
        }
         
        for(int i = 0; i < n; i++){
            int from,to,cst,tm;
            scanf("%d%d%d%d",&from,&to,&cst,&tm);
            D[from][to][0] = cst;
            D[to][from][0] = cst;
            D[from][to][1] = tm;
            D[to][from][1] = tm;
        }
        int k;
        scanf("%d",&k);
        for(int i = 0; i < k; i++){
            int from,to,x;
            cin >> from >> to >> x;
            cout << dijkstra(from,to,x) << endl;
        }
    }
    return 0;
}
int dijkstra(int x,int y,int p){
    int dist[128];
    bool done[128]={false};
    for(int i = 1; i <= m; i++){
        dist[i] = INF;
    }
     
    dist[x] = 0;
    int cost = 0;
    while(1){
        int e = -1;
        for(int i = 1; i <= m; i++){
            if(done[i] == false && (e == -1 || dist[i] < dist[e])){
                e = i;
            }
        }
        if(e == -1)break;
        done[e] = true;
        if(e == y){
            cost = dist[e];
            break;
        }
         
        for(int i = 1; i <= m; i++){
            if(p == 0)dist[i] = min(dist[i],dist[e]+D[e][i][0]);
            else dist[i] = min(dist[i],dist[e]+D[e][i][1]);
        }
         
    }
    return cost;
}