#include <iostream>
#include <cstdio>
using namespace std;

#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)

int N,M;
int maze[110][110],tim[110][110];
int main(void){
	while(cin>>N>>M,N+M){
		rep(i,M+1)rep(j,M+1)maze[i][j] = (1<<28),tim[i][j] = (1<<28);
		while(N--){
			int a,b,c,t;
			cin>>a>>b>>c>>t;
			maze[--a][--b] = c;
			maze[b][a] = c;
			tim[a][b] = t;
			tim[b][a] = t;
		}
		rep(k,M+1){
			rep(i,M+1){
				rep(j,M+1){
					if(maze[i][j] > maze[i][k] + maze[k][j]){
						// update
						maze[i][j] = maze[i][k] + maze[k][j];
						tim[i][j] = min(tim[i][j],tim[i][k] + tim[k][j]);
					} 
				}
			}
		}
		//rep(i,M)rep(j,M)cout<<tim[i][j]<<(j==M-1?'\n':' ');
		cin>>N;
		while(N--){
			int a,b,t;
			cin>>a>>b>>t;
			--a;--b;
			cout<<(t?tim[a][b]:maze[a][b])<<endl;
		}
	}
}