#include <iostream>
#include <vector>
#include <utility>
#include <queue>
#define INF 200000000
using namespace std;
typedef pair<int, int> P;
struct line {
  int to,time,cost;
};

vector<line> L[1000];
int costv[1000];
int timev[1000];

void mincost(int from,int to) {
  priority_queue<P, vector<P>, greater<P> > que;
  for(int i=0;i<1000;i++) {
    costv[i] = INF;
  }
  costv[from] = 0;
  que.push(P(0,from));
  while(!que.empty()) {
    P p = que.top(); que.pop();
    int v = p.second;
    if(costv[v] < p.first) continue;
    for(unsigned int i=0;i < L[v].size();i++) {
      line l = L[v][i];
      if(costv[l.to] > costv[v] + l.cost) {
	costv[l.to] = costv[v] + l.cost;
	que.push(P(costv[l.to], l.to));
      }
    }
  }
  cout << costv[to] << endl;
}

void mintime(int from,int to) {
  priority_queue<P, vector<P>, greater<P> > que;
  for(int i=0;i<1000;i++) {
    timev[i] = INF;
  }
  timev[from] = 0;
  que.push(P(0,from));
  while(!que.empty()) {
    P p = que.top(); que.pop();
    int v = p.second;
    if(timev[v] < p.first) continue;
    for(unsigned int i=0;i < L[v].size();i++) {
      line l = L[v][i];
      if(timev[l.to] > timev[v] + l.time) {
	timev[l.to] = timev[v] + l.time;
	que.push(P(timev[l.to], l.to));
      }
    }
  }
  cout << timev[to] << endl;
}

int main() {
  int n,m,k;
  while(cin >> n >> m) {
    if(n==0&&m==0)break;
    for(int i=0;i<1000;i++) {
      L[i].clear();
    }
    for(int i=0;i<n;i++) { //input line
      line temp;
      int f,t,ti,co;
      cin >> f >> t >> co >> ti;
      temp.to = t;
      temp.time = ti;
      temp.cost = co;
      L[f].push_back(temp);
      temp.to = f;
      L[t].push_back(temp);
    }

    cin >> k;
    for(int i=0;i<k;i++) {
      int p,q,r;
      cin >> p >> q >> r;
      if(r == 0)
	mincost(p,q);
      else
	mintime(p,q);
    }
  }
}