#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define INF 11451400



int main(){
	int lineL, statL;
	cin >> lineL >> statL;
	long leastCosts[statL][statL];
	long leastTimes[statL][statL];
	FOR(aStat,0,statL){
		FOR(bStat,0,statL){
			if(aStat==bStat){
				leastCosts[aStat][bStat] = 0;
				leastTimes[aStat][bStat] = 0;
			}else{
				leastCosts[aStat][bStat] = INF;
				leastTimes[aStat][bStat] = INF;
			}
		}
	}

	REP(lineI,lineL){
		int naStat,nbStat,nCost,nTime;
		cin >> naStat >> nbStat >> nCost >> nTime;
		naStat -=1; nbStat -=1;
		FOR(aStat,0,statL){
			FOR(bStat,0,statL){
				leastCosts[aStat][bStat] = min(leastCosts[aStat][bStat] , leastCosts[aStat][naStat]+nCost+leastCosts[nbStat][bStat]);
				leastCosts[aStat][bStat] = min(leastCosts[aStat][bStat] , leastCosts[aStat][nbStat]+nCost+leastCosts[naStat][bStat]);
				leastTimes[aStat][bStat] = min(leastTimes[aStat][bStat] , leastTimes[aStat][naStat]+nTime+leastTimes[nbStat][bStat]);
				leastTimes[aStat][bStat] = min(leastTimes[aStat][bStat] , leastTimes[aStat][nbStat]+nTime+leastTimes[naStat][bStat]);
			}
		}
	}

	int reqL;
	cin >> reqL;
	REP(reqI,reqL){
		int aStat,bStat,costOrTime;
		cin >> aStat >> bStat >> costOrTime;
		aStat-=1;bStat-=1;
		if(costOrTime==0){cout<<leastCosts[aStat][bStat]<<endl;};
		if(costOrTime==1){cout<<leastTimes[aStat][bStat]<<endl;};
	}

	return 0;
}