#include <iostream>
#include <stdlib.h>
#define inf 99999

using namespace std;

bool check(int *label, int n)
{
	for(int i = 1; i <= n; i++){
		if(label[i] == 0){
			return true;
		}
	}
	
	return false;
}

void dij(int **a, int n, int s, int t)
{
	int *label, current=s, num=1, next, *cost, min;
	
	cost = (int *)calloc(n+1, sizeof(int));
	label = (int *)calloc(n+1, sizeof(int));
	
	for(int i = 1; i <= n; i++){
		cost[i] = a[current][i];
	}
	label[current] = num++;
	
	while(check(label, n)){
		min = inf;
		for(int i = 1; i <= n; i++){
			if(min >= cost[i] && label[i] == 0){
				min = cost[i];
				next = i;
			}
		}
		label[next] = num++;
		for(int i = 1; i <= n; i++){
			if(label[i] == 0 && cost[i] > (a[next][i]+cost[next])){
				cost[i] = a[next][i] + cost[next];
			}
		}
		current = next;
	}
	cout << cost[t] << endl;
	
	free(cost);
	free(label);
	
	return ;
}

int main(int argc, char **argv)
{
	int **t, **c, n, m, a, b, p, q;
	
	while(1){
		cin >> n >> m;
		if(n == 0 && m == 0){
			break;
		}
		
		t = (int **)calloc(m+1, sizeof(int));
		c = (int **)calloc(m+1, sizeof(int));
		for(int i = 0; i <= m; i++){
			t[i] = (int *)calloc(m+1, sizeof(int));
			c[i] = (int *)calloc(m+1, sizeof(int));
		}
		
		for(int i = 0; i < n; i++){
			cin >> a >> b >> p >> q;
			c[a][b] = c[b][a] = p;
			t[a][b] = t[b][a] = q;
		}
		for(int i = 1; i <= m; i++){
			for(int j = 1; j <= m; j++){
				if(i != j && c[i][j] == 0){
					c[i][j] = inf;
				}
				if(i != j && t[i][j] == 0){
					t[i][j] = inf;
				}
			}
		}
		cin >> n;
		for(int i = 0; i < n; i++){
			cin >> a >> b >> p;
			if(p == 0){
				dij(c, m, a, b);
			} else {
				dij(t, m, a, b);
			}
		}
		
		for(int i = 0; i <= m; i++){
			free(t[i]);
			free(c[i]);
		}
		free(t);
		free(c);
	}
	
	return 0;
}
#include <bits/stdc++.h>
using namespace std;

#define iota(i,n,b,s) for(int i=int(b);i!=int((b)+(s)*(n));i+=(s))
#define range(i,n,m) iota(i,(((n)>(m))?((n)-(m)+1):((m)-(n)+1)),(n),((n)>(m)?-1:1))
#define rep(i,n) iota(i,(n),0,1)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a,b) (make_pair(a,b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;

typedef long long INTEGER;
typedef double FLOAT;

template<class S, class T>
ostream& operator<<(ostream& os, pair<S,T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template<class T>
ostream& operator<<(ostream& os, vector<T> v) {
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

const int MM = 100;
int cost[MM][MM];
int toki[MM][MM];

int main() {

entry:;
  int n, m; cin >> m >> n;
  if (n + m == 0) return 0;

  rep (i, n) {
    rep (j, n) {
      cost[i][j] = INF;
      toki[i][j] = INF;
    }
    cost[i][i] = 0;
    toki[i][i] = 0;
  }

  rep (i, m) {
    int a, b, c, t;
    cin >> a >> b >> c >> t;
    --a; --b;
    cost[a][b] = cost[b][a] = c;
    toki[a][b] = toki[b][a] = t;
  }

  rep (k, n) {
    rep (i, n) {
      rep (j, n) {
        cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
        toki[i][j] = min(toki[i][j], toki[i][k] + toki[k][j]);
      }
    }
  }

  //rep (i,n) { rep(j,n) { cout << cost[i][j] << ' '; } cout << endl; }

  int k; cin >> k;
  rep (i, k) {
    int a, b, t;
    cin >> a >> b >> t;
    --a; --b;
    if (t) {
      cout << toki[a][b] << endl;
    } else {
      cout << cost[a][b] << endl;
    }
  }

  goto entry;

  return 0;
}