#define NODE 0
#define WEIGHT 1
#define COST_IDX 0
#define TIME_IDX 1
#define P_IDX 0
#define Q_IDX 1
#define R_IDX 2
#include <iostream>
#include <unordered_map>
#include <queue>
#include <vector>
using namespace std;

int n, m, k;
unordered_map<int, unordered_map<int, int*> > weights;
int **requests;

bool read_input() {
  cin >> n >> m;
  if (n*m == 0) 
    return false;
  for (int i = 0; i < n; i++) {
    int a, b, c, t;
    cin >> a >> b >> c >> t;
    weights[a][b] = new int[2];
    weights[b][a] = new int[2];
    weights[a][b][COST_IDX] = weights[b][a][COST_IDX] = c;
    weights[a][b][TIME_IDX] = weights[b][a][TIME_IDX] = t;
  }
  cin >> k;
  requests = new int*[k];
  for (int i = 0; i < k; i++) {
    requests[i] = new int[3];
    cin >> requests[i][P_IDX] >> requests[i][Q_IDX] >> requests[i][R_IDX];
  }
  return true;
}

int resolve_request(int i_th) {
  int p, q, r;
  p = requests[i_th][P_IDX];
  q = requests[i_th][Q_IDX];
  r = requests[i_th][R_IDX];

  /* keep track of latest weight_sum of each node since values 
   * in priority queue might not be up to date (since STL prioroty 
   * queue does not support value change, ignore popped itmes
   * that does not match values in this map). */  
  unordered_map<int, int> weight_sum_map;
  
  /* starting node has weight_sum of 0 */
  weight_sum_map[p] = 0;

  /* create priority queue and push p into it as the initial element */
  auto cmp = [](int *a, int *b) { return a[WEIGHT] > b[WEIGHT]; };
  priority_queue<int*, vector<int*>, decltype(cmp)> pq(cmp); 
  int *itm = new int[2];
  itm[NODE] = p;
  itm[WEIGHT] = 0;
  pq.push(itm);

  while (!pq.empty()) {
    itm = pq.top(); 
    int weight_sum = itm[WEIGHT];
    int node = itm[NODE];

    /* release allocated mem and remove from priority queue*/
    delete[] itm;
    pq.pop();

    // if q was popped, release all allocated mem and break 
    if (node == q) {/*
      while (!pq.empty()) {
	itm = pq.top();
	delete[] itm;
	pq.pop();
	}*/
      break;
    }

    /* ignore if wight_sum value not up to date */
    if (weight_sum != weight_sum_map[node]) {
      continue;
    }
    auto connections = weights[node];
    for (auto itr = connections.begin(); itr != connections.end(); itr++) {
      int adj_node = itr->first;
      int w = itr->second[r]; // get the requested weight type r == 0(cost) or 1(time)
      int new_weight_sum = weight_sum + w;
      if (weight_sum_map.count(adj_node) == 0 || new_weight_sum < weight_sum_map[adj_node]) {
	weight_sum_map[adj_node] = new_weight_sum;
	itm = new int[2];
	itm[NODE] = adj_node;
	itm[WEIGHT] = new_weight_sum;
	pq.push(itm);
      }
    }
  }
  return weight_sum_map[q];
}

void solve() {
  for (int i = 0; i < k; i++)
    cout << resolve_request(i) << endl;
}

void cleanup() {/*
  for (int i = 0; i < k; i++) {
    delete[] requests[i];
  }
  delete[] requests;
		*/
  /*
  for (auto itr_a = weights.begin(); itr_a != weights.end(); itr_a++) {
    auto m = itr_a->second;
    for (auto itr_b = m.begin(); itr_b != m.end(); itr_b++) {
      delete[] itr_b->second;
    }
    m.clear();
    }*/
  weights.clear();
}

int main() {
  while(read_input()) {
    solve();
    cleanup();
  }
  return 0;
}

Measured memory_bytes: 33500