#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INFTY=1<<29;

int main()
{
	for(int n,m;cin>>n>>m,n|m;){
		vvi c(m,vi(m,INFTY));
		rep(i,m)
			c[i][i]=0;
		vvi t=c;
		
		rep(i,n){
			int src,dst,cost,time;
			cin>>src>>dst>>cost>>time;
			src--;dst--;
			c[src][dst]=c[dst][src]=cost;
			t[src][dst]=t[dst][src]=time;
		}
		
		rep(k,m) rep(i,m) rep(j,m){
			c[i][j]=min(c[i][j],c[i][k]+c[k][j]);
			t[i][j]=min(t[i][j],t[i][k]+t[k][j]);
		}
		
		int query;
		cin>>query;
		rep(i,query){
			int src,dst,which;
			cin>>src>>dst>>which;
			src--;dst--;
			if(which)
				cout<<t[src][dst]<<endl;
			else
				cout<<c[src][dst]<<endl;
		}
	}
	
	return 0;
}