#include <bits//stdc++.h>
using namespace std;
int main(void){
    while(1){
        int a,b,c,i,j,k,f;
    cin>>b>>a;
    if(a==0&&b==0)
    break;
    int train_cost[a][a];
    int train_time[a][a];
    for(i=0;i<a;i++){
        for(j=0;j<a;j++){
            if(i==j){
                train_time[i][j]=0;
                train_cost[i][j]=0;
            }
            else{
                train_time[i][j]=INT_MAX/2;
                train_cost[i][j]=INT_MAX/2;
            }
        }
    }
    for(i=0;i<b;i++){
        int d,e;
        cin>>d>>e;
        cin>>train_cost[d-1][e-1]>>train_time[d-1][e-1];
        train_cost[e-1][d-1]=train_cost[d-1][e-1];
        train_time[e-1][d-1]=train_time[d-1][e-1];
    }
    for(k=0;k<a;k++){
        for(j=0;j<a;j++){
            for(i=0;i<a;i++){
                train_cost[i][j]=min(train_cost[i][j],train_cost[i][k]+train_cost[k][j]);
                train_time[i][j]=min(train_time[i][j],train_time[i][k]+train_time[k][j]);
            }
        }
    }
    cin>>f;
    for(i=0;i<f;i++){
        int d,e;
        cin>>d>>e>>c;

        if(c)
        cout<<train_time[d-1][e-1]<<endl;
        else
        cout<<train_cost[d-1][e-1]<<endl;
    }
    }
    return 0;
}

