/*
 * Problem     : AOJ0200
 * Title       : Traveling Alone: One-way Ticket of Youth (高校生一人旅 ~青春の片道切符編~)
 * Author      : mizo0203
 * Copyright   : Your copyright free
 * Description : ダイクストラ
 */
#include <stdio.h>
#include <utility> // pairライブラリを使用するのに必要
#include <vector>
#include <queue>
#include <algorithm>
#define MAX_V 100
#define INF 3000000
using namespace std;

struct edge {
  int to, cost, time;
};
typedef pair<int, int> P;

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

int k;
int s, q, r;

bool input() {
  int i;
  int n, a, b, cost, time;
  edge e;

  scanf("%d %d", &n, &V);
  if (!n && !V)
    return false;

  for (i = 0; i < V; i++) {
    G[i].clear();
  }
  for (i = 0; i < n; i++) {
    scanf("%d %d %d %d", &a, &b, &cost, &time);
    a--;
    b--;
    e.to = b;
    e.cost = cost;
    e.time = time;
    G[a].push_back(e);
    e.to = a;
    G[b].push_back(e);
  }
  scanf("%d", &k);

  return true;
}

bool input2() {

  if (!k)
    return false;
  k--;

  scanf("%d %d %d", &s, &q, &r);
  s--;
  q--;

  return true;
}

void dijkstra() {
  priority_queue<P, vector<P>, greater<P> > que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < (int) G[v].size(); i++) {
      edge e = G[v][i];
      if (!r) {
        if (d[e.to] > d[v] + e.cost) {
          d[e.to] = d[v] + e.cost;
          que.push(P(d[e.to], e.to));
        }
      } else {
        if (d[e.to] > d[v] + e.time) {
          d[e.to] = d[v] + e.time;
          que.push(P(d[e.to], e.to));
        }
      }
    }
  }
}

int main() {

  while (input()) {
    while (input2()) {
      dijkstra();
      printf("%d\n", d[q]);
    }
  }

  return 0;
}