#include <iostream>
using namespace std;

#define STAT_MAX 100
#define LINE_MAX 3000

struct LINE {
	int to;
	int cost[2];	//0:money 1:time
};
struct STAT {
	int lineNum;
	LINE line[LINE_MAX];
};

struct YET {
	int id,depth,cost;
};
struct SEARCHED {
	int id,total;
};

int main(){
	int n,m;	//?????°
	int a,b,cost,time;	//??????
	int start,goal,flg;	//???????????????
	int i,j;
	
	int statNum;
	STAT stat[STAT_MAX];

	int result;
	int yetNum;
	YET yet[STAT_MAX],now;
	int searchedNum;
	SEARCHED searched[STAT_MAX];

	while(1){
		//?????°??\???
		cin>>n>>m;if(!n && !m) break;
		//???????????????
		statNum=m;
		for(i=0;i<statNum;i++) stat[i].lineNum=0;
		while(n--){
			cin>>a>>b>>cost>>time;
			a--;b--;
			stat[a].line[stat[a].lineNum].to=b;
			stat[a].line[stat[a].lineNum].cost[0]=cost;
			stat[a].line[stat[a].lineNum].cost[1]=time;
			stat[a].lineNum++;
			stat[b].line[stat[b].lineNum].to=b;
			stat[b].line[stat[b].lineNum].cost[0]=cost;
			stat[b].line[stat[b].lineNum].cost[1]=time;
			stat[b].lineNum++;
		}
		//????????????????????????
		cin>>n;
		while(n--){
			cin>>start>>goal>>flg;
			start--;goal--;
			result=-1;searchedNum=0;
			yetNum=1;yet[0].id=start;yet[0].depth=0;yet[0].cost=0;
			while(yetNum){
				now=yet[--yetNum];
				searched[searchedNum].id=now.id;
				searched[searchedNum].total=(searchedNum!=0)?searched[searchedNum-1].total+now.cost:now.cost;
				searchedNum++;
				if(now.id==goal){
					if(result!=-1 || searched[searchedNum].total<result) result=searched[searchedNum].total;
					if(yetNum!=0) searchedNum=yet[yetNum-1].depth;
				}else{
					m=yetNum;	//?????????????????¢?´¢?§??????°?????????
					for(i=0;i<stat[now.id].lineNum;i++){
						//??¢?´¢???????????????????????§??????
						for(j=0;j<searchedNum;j++){
							if(stat[now.id].line[i].to==searched[j].id) break;
						}
						if(j!=searchedNum) continue;
						//?§??????±???yet??????????????\
						yet[yetNum].id=stat[now.id].line[i].to;
						yet[yetNum].depth=now.depth+1;
						yet[yetNum].cost=stat[now.id].line[i].cost[flg];
						yetNum++;
					}
					//????´¢??????????????????????????????????????????
					if(m==yetNum && yetNum!=0) searchedNum=yet[yetNum-1].depth;
				}
			}
		}
		cout<<result<<endl;
	}
	return 0;
}