#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
const int MAX_V = 101;
int d[MAX_V][MAX_V][2];
int V;

void wrshall_floyd(){
    for (int k = 0; k < V; ++k) {
        for (int i = 0; i < V; ++i) {
            for (int j = 0; j < V; ++j) {
                d[i][j][0]=min(d[i][j][0],d[i][k][0]+d[k][j][0]);
                d[i][j][1]=min(d[i][j][1],d[i][k][1]+d[k][j][1]);
            }
        }
    }
}

int main(){
    for (int i = 0; i < MAX_V; ++i) {
        for (int j = 0; j < MAX_V; ++j) {
            if(i==j){
                d[i][j][1]=d[j][i][0]=0;
            } else {
                d[i][j][1]=d[j][i][0]=100000000;
            }
        }
    }
    int n,m;
    cin>>n>>m;
    V=n;
    for (int i = 0; i < n; ++i) {
        int a,b,c,t;
        cin>>a>>b>>c>>t;
        a--;b--;
        d[a][b][0]=d[b][a][0]=c;
        d[a][b][1]=d[b][a][1]=t;
    }
    wrshall_floyd();
    int k;cin>>k;
    for (int i = 0; i < k; ++i) {
        int t,f,r;cin>>t>>f>>r;
        t--;f--;
        cout<<d[t][f][r]<<endl;
    }
}