#include <bits/stdc++.h>
using namespace std;

const int maxN = 200010;
typedef long long ll;

int vis[2][maxN];
vector<int> G[2][maxN];

queue<int> Q;
void bfs(int u, int d, int c){
	while(!Q.empty()) Q.pop();
	vis[d][u] = c; Q.push(u);
	while(!Q.empty()){
		u = Q.front(); Q.pop();
		for(int i = 0; i < G[d][u].size(); ++i){
			int v = G[d][u][i];
			if(!vis[d][v]) vis[d][v] = c, Q.push(v);
		}
	}
}

map<ll, int> mp;

int main (){
	int n, k, l, u, v, i, c0 = 0, c1 = 0;
	scanf("%d%d%d", &n, &k, &l);
	while(k--){
		scanf("%d%d", &u, &v);
		G[0][u].push_back(v);
		G[0][v].push_back(u);
	}
	while(l--){
		scanf("%d%d", &u, &v);
		G[1][u].push_back(v);
		G[1][v].push_back(u);
	}
	for(i = 1; i <= n; ++i) if(!vis[0][i]) bfs(i, 0, ++c0);
	for(i = 1; i <= n; ++i) if(!vis[1][i]) bfs(i, 1, ++c1);
	for(i = 1; i <= n; ++i) ++mp[vis[0][i] * 1ll * maxN + vis[1][i]];
	for(i = 1; i <= n; ++i){
		printf("%d", mp[vis[0][i] * 1ll * maxN + vis[1][i]]);
		if(i < n) printf(" ");
	}
	return 0;
}

#include "bits/stdc++.h"
using namespace std;

#define INF 1e9+7

int main(){
	int n,m;
	while(1){
		cin>>n>>m;
		if(n==0&&m==0)break;
		vector<vector<int> > cost(m,vector<int>(m,INF));
		vector<vector<int> > timea(m,vector<int>(m,INF));
		
		for(int i=0;i<n;i++){
			int a,b;
			cin>>a>>b;
			a--;
			b--;
			cin>>cost[a][b]>>timea[a][b];
			cost[b][a]=cost[a][b];
			timea[b][a]=timea[a][b];
		}
		
		for(int i=0;i<m;i++){
			cost[i][i]=0;
			timea[i][i]=0;
		}
		
		for(int k=0;k<m;k++){
			for(int i=0;i<m;i++){
				for(int j=0;j<m;j++){
					cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
					timea[i][j]=min(timea[i][j],timea[i][k]+timea[k][j]);
				}
			}
		}
		
		int k;
		cin>>k;
		for(int i=0;i<k;i++){
			int p,q,r;
			cin>>p>>q>>r;
			p--;
			q--;
			if(r==0)cout<<cost[p][q]<<endl;
			if(r==1)cout<<timea[p][q]<<endl;
		}
	}
	return 0;
}