#include <iostream>
#include <fstream>
#include <typeinfo>
#include <vector>
#include <cmath>
#include <set>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const double pi=acos(-1);
const double eps=1e-8;
const vi emp;

class Graph{
	private:
	int GV;
	vvp List;
	public:
	Graph(int v){
		GV=v;
		List=vvp(v);
	}
	void add_edge(int x,int y,int c){
		List[x].push_back(P(y,c));
		List[y].push_back(P(x,c));
	}
	int DIJ(int s,int t){
		priority_queue<P> que;
		vi d(GV,inf);
		d[s]=0;
		que.push(P(0,s));
		while(!que.empty()){
			P p=que.top();
			que.pop();
			int v=p.second;
			if(d[v]<p.first) continue;
			for(int i=0;i<List[v].size();i++){
				P e=List[v][i];
				if(d[e.first]>d[v]+e.second){
					d[e.first]=d[v]+e.second;
					que.push(P(d[e.first],e.first));
				}
			}
		}
		return d[t];
	}
};

int V,E,N;

int main(){
	while(1){
		cin>>E>>V;
		if(!V&&!E) break;
		Graph cost(V),time(V);
		int s,t,c,l;
		for(int i=0;i<E;i++){
			cin>>s>>t>>c>>l;
			s--;
			t--;
			cost.add_edge(s,t,c);
			time.add_edge(s,t,l);
		}
		cin>>N;
		bool r;
		for(int i=0;i<N;i++){
			cin>>s>>t>>r;
			s--;
			t--;
			if(r) cout<<time.DIJ(s,t)<<endl;
			else cout<<cost.DIJ(s,t)<<endl;
		}
	}
}