#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <functional>
 
using namespace std;
 
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ll long long
//#define scanf scanf_s
 
int cost[101][101];
int time_[101][101];
 
 
int main()
{
    int n, m;
    while (scanf("%d%d",&n,&m))
    {
        if (!n&&!m) break;
        REP(i, 101)REP(j, 101)
        {
                cost[i][j] = INF;
                time_[i][j] = INF;
        }
         
        REP(i, n)
        {
            int a, b, c, t;
			scanf("%d%d%d%d", &a, &b, &c, &t);
            a--; b--;
            cost[a][b] = cost[b][a] = c;
            time_[a][b] = time_[b][a] = t;
        }
 
        REP(i, m)REP(j, m)REP(k, m)
        {
            cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
            time_[i][j] = min(time_[i][j], time_[i][k] + time_[k][j]);
        }
        int k;
		scanf("%d", &k);
        REP(i, k)
        {
            int p, q, r;
			scanf("%d%d%d", &p, &q, &r);
            p--; q--;
			if (r == 0) printf("%d\n", cost[p][q]);
			else  printf("%d\n", time_[p][q]);
        }
    }
    return 0;
}