#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

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

//4方向ベクトル→↑←↓
int dx[] ={1,0,-1,0};
int dy[] ={0,-1,0,1};

int n,m;//線路の情報の数,駅の数
int dpc[101][101];
int dpt[101][101];
const int INF = 100000000;


void WarshallFloyd(){
  for(int i = 1;i <= m;i++){
    for(int j = 1;j <=m;j++){
      for(int k = 1;k <=m;k++){
      dpc[j][k] =min(dpc[j][k],dpc[j][i]+dpc[i][k]);
      dpt[j][k] =min(dpt[j][k],dpt[j][i]+dpt[i][k]);
      }
    }
  }
  return;
}

int main()
{
  while(cin >>n>>m){
    if(n ==0&&m ==0){
      break;
    }
   
    for(int i = 1;i <=m ;i++){
      for(int j = i+1;j <=m;j++){
	dpc[i][j] =INF;
	dpc[j][i] =INF;
	dpt[i][j] =INF;
	dpt[j][i] =INF;
      }
    }
    for(int i = 1;i <= m;i++){
      dpc[i][i] = 0;
      dpt[i][i] = 0;
    }
    for(int i = 0;i < n;i++){
      int from,to,cost,time;
      cin >>from>>to>>cost>>time;
      dpc[from][to] =cost;
      dpt[from][to] =time;
      dpc[to][from] =cost;
      dpt[to][from] =time;
    }
    /* for(int i = 1;i <=m;i++){
      for(int j = 1;j <=m;j++){
	cout <<"dpc["<<i<<"]["<<j<<"]"<<dpc[i][j] <<endl;
	cout <<"dpt["<<i<<"]["<<j<<"]"<<dpt[i][j] <<endl;
      }
      }*/
    WarshallFloyd();
    int k;//問い合わせの数
    cin >>k;
    for(int i = 0;i <k;i++){
      int s;
      int f;
      int r;
      cin >>s>>f>>r;
      if(r)
	cout<<dpt[s][f]<<endl;
      else
	cout<<dpc[s][f]<<endl;
    }
  }
  return 0;
}