#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<utility>
#include<cmath>
#include<assert.h>
#include<set>
#include<map>

#define int long long
using namespace std;
#define rep(i, n) for(int i=0;i<(n);++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

map<pii, vector<pair<pii, int>>> G;
map<pii, int> d;
set<int> company[100100];//company[i]:=駅iを通る会社
int N, M;

void dijkstra() {
    priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> que;
    d[pii(0, -1)] = 0;
    que.push(make_pair(0, pii(0, -1)));
    while (!que.empty()) {
        pair<int, pii> p = que.top();
        que.pop();
        pii v = p.second;
        if (d.count(v) && d[v] < p.first)continue;

        rep(i, G[v].size()) {
            pair<pii, int> edge = G[v][i];
            if (!d.count(edge.first) || d[edge.first] > d[v] + edge.second) {
                d[edge.first] = d[v] + edge.second;
                que.push(make_pair(d[edge.first], edge.first));
            }
        }
    }
}

signed main() {
    cin >> N >> M;
    rep(i, M) {
        int p, q, c;
        cin >> p >> q >> c;
        p--;
        q--;
        G[pii(p, c)].push_back(make_pair(pii(q, c), 0));
        G[pii(q, c)].push_back(make_pair(pii(p, c), 0));
        company[p].insert(c);
        company[q].insert(c);
    }
    rep(i, N) {
        for (int c:company[i]) {
            G[pii(i, c)].push_back(make_pair(pii(i, -1), 0));
            G[pii(i, -1)].push_back(make_pair(pii(i, c), 1));
        }
    }

    dijkstra();
    if (d.count(pii(N - 1, -1)))
        cout << d[pii(N - 1, -1)] << endl;
    else cout << -1 << endl;
    return 0;
}

#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>

#define INF 1<<30-1

using namespace std;

int n;
int c[101][101];
int t[101][101];
int flg;

int washa(int st,int en,int va){
	if(flg==1){
		for(int k=1;k<=n;k++){
			for(int i=1;i<=n;i++){
				for(int j=1;j<=n;j++){
					if(c[i][j]>c[i][k]+c[k][j]){
						c[i][j]=c[i][k]+c[k][j];
					}
					if(t[i][j]>t[i][k]+t[k][j]){
						t[i][j]=t[i][k]+t[k][j];
					}
				}
			}
		}
		flg=0;
	}
	if(va==0){
		return c[st][en];
	}else{
		return t[st][en];
	}
}

int main(){
	while(1){
		int m;
		for(int i=0;i<101;i++){
			for(int j=0;j<101;j++){
				c[i][j]=t[i][j]=INF;
			}
		}
		flg=1;
		
		scanf("%d%d",&m,&n);
		if(m==0 && n==0){
			break;
		}
		for(int i=0;i<m;i++){
			int a,z,s,x;
			scanf("%d%d%d%d",&a,&z,&s,&x);
			c[a][z]=c[z][a]=s;
			t[a][z]=t[z][a]=x;
		}
		
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			int a,z,s;
			scanf("%d%d%d",&a,&z,&s);
			
			printf("%d\n",washa(a,z,s));
		}
	}
}