#include<cstdio>
#include<vector>
#include<algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const int INF=1<<29;

template<class T>
class binary_heap{
	const static int N_MAX=30000;
	int n;
	T a[N_MAX+1];
public:
	binary_heap():n(0){}
	const T &find_min()const{ return a[1]; }
	void insert(const T &x){
		int u=++n;
		a[u]=x;
		while(u!=1 && a[u/2]>a[u]) swap(a[u/2],a[u]), u/=2;
	}
	void delete_min(){
		a[1]=a[n--];
		for(int u=1;2*u<=n;){
			int v=2*u;
			if(2*u+1<=n && a[2*u]>a[2*u+1]) v++;
			if(a[v]<a[u]) break;
			swap(a[v],a[u]);
			u=v;
		}
	}
	bool empty()const{ return n==0; }
	int size()const{ return n; }
};

struct edge{ int v,cost1,cost2; };

int main(){
	for(int m,n;scanf("%d%d",&m,&n),n;){
		vector<edge> G[100];
		rep(i,m){
			int u,v,cost1,cost2; scanf("%d%d%d%d",&u,&v,&cost1,&cost2); u--; v--;
			G[u].push_back((edge){v,cost1,cost2});
			G[v].push_back((edge){u,cost1,cost2});
		}

		int q; scanf("%d",&q);
		while(q--){
			int s,t,type; scanf("%d%d%d",&s,&t,&type); s--; t--;

			int d[100];
			rep(u,n) d[u]=(u==s?0:INF);
			static binary_heap< pair<int,int> > H; H.insert(make_pair(0,s));
			while(!H.empty()){
				int d_now=H.find_min().first,u=H.find_min().second;
				H.delete_min();

				if(d[u]<d_now) continue;

				rep(i,G[u].size()){
					const edge &e=G[u][i];
					int d2=d_now+(type==0?e.cost1:e.cost2);
					if(d[e.v]>d2){
						d[e.v]=d2;
						H.insert(make_pair(d2,e.v));
					}
				}
			}

			printf("%d\n",d[t]);
		}
	}

	return 0;
}