#include <iostream>
#include <algorithm> // next_permutation
#include <iomanip>
#include <cmath>
#include <vector>
#include <sstream>
#include <string>
#include <cstdio>
#include <stack>
#include <queue>
#include <list>
#include <numeric> //accumulate
#include <unordered_map> //???????????\??¢??°

using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
//#define TEST //*******************************************************************************************************************************************

//?????????????????¨??§?????\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
#ifdef TEST
    #define dout cout
#else
    stringstream dummy; //???????????°??????dout?????????????????????????????????
    #define dout dummy.str(""); dummy.clear(stringstream::goodbit); dummy //dummy?????????????????????????????????????????¨?????¢?????¢???????????????
    //???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§???????????????????????????????????? http://d.hatena.ne.jp/linden/20060427/p1
#endif


#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A) dout << (A) << " "
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)

#define dispAll(A,n) dout << #A << " = "; rep(j, 0, (n)) {disP(A[j]);} dout << endl



const int INF = 1e5-1;

int main(void) {
    
    int N; //????????°
    int M; //?????°
    int a, b, c, t;
    
    while(1) {
        cin >> M >> N;
        if( M==0 && N==0 ) break;
    
        //initialization
        int minCost[N+1][N+1]; //1~N??????????????????
        int minTime[N+1][N+1]; //1~N??????????????????
        
        rep(i,0,N+1) {
            rep(j,0,N+1) {
                minCost[i][j] = (i==j ? 0 : INF);
                minTime[i][j] = (i==j ? 0 : INF);
            }
        }

        rep(i,0,M) {
            cin >> a >> b >> c >> t; //??????a??????b?????§????????????t
            
            minCost[a][b] = minCost[b][a] = c;
            minTime[a][b] = minTime[b][a] = t;
        }
        
        
        //test display
        rep(i,0,N+1) {
            disP(i);
            dispAll(minCost[i], N+1);
        }
        dout << endl;
        
        //test display
        rep(i,0,N+1) {
            disP(i);
            dispAll(minTime[i], N+1);
        }
        dout << endl;

        
        
        //Warshall Floyd Algorithm
        rep(k,1,N+1) {
            rep(i,1,N+1) {
                rep(j,1,N+1) {
                    minCost[i][j] = min( minCost[i][j], minCost[i][k] + minCost[k][j] );
                    minTime[i][j] = min( minTime[i][j], minTime[i][k] + minTime[k][j] );
                }
            }
        }
        
        
        //test display
        rep(i,0,N+1) {
            disP(i);
            dispAll(minCost[i], N+1);
        }
        dout << endl;
        
        //test display
        rep(i,0,N+1) {
            disP(i);
            dispAll(minTime[i], N+1);
        }
        dout << endl;
        
        
        //read next dataset (about inquiry)
        int numOfQuery;
        cin >> numOfQuery;
        
        bool needTime = 0;
        
        //answer each query
        rep(i,0,numOfQuery) {
            cin >> a >> b >> needTime;
            
            cout << (needTime ? minTime[a][b] : minCost[a][b] ) << endl;
        }
        
    }
    
    return 0;
}

Measured memory_bytes: 3232