#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <stack>
#include <climits>
#include <deque>
#include <bitset>
#include <cassert>
#include <ctime>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
// adjust problem by problem
const double EPS=1e-8;
const double PI=acos(-1.0);
// rep macro
#define REP(i,x,n) for(int i=x;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define RREP(i,x,n) for(int i=x;i>=(int)(n);--i)
#define rrep(i,n) RREP(i,n,0)
#ifdef __GNUC__
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
int popcount(int n){return __builtin_popcount(n);}
int popcount(ll n){return __builtin_popcountll(n);}
#endif
#ifndef __GNUC__
int popcount(unsigned int n){int cnt=0;for(int i=0;i<32;i++)if((n>>i)&1)cnt++;return cnt;}
int popcountll(unsigned ll n){int cnt=0;for(int i=0;i<64;i++)if((n>>i)&1)cnt++;return cnt;}
#endif
template<class T>int SIZE(T a){return a.size();}
template<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();}
template<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}
ll getTen(int a){return (a<=0)?1:(getTen(a-1)*10);}
bool EQ(double a,double b){return abs(a-b)<EPS;}
void fastStream(){cin.tie(0);std::ios_base::sync_with_stdio(0);}
vector<string> split(string str,char del){
  vector<string> res;
  for(int i=0,s=0;i<SIZE(str);i++){
    if(str[i]==del){if(i-s!=0)res.push_back(str.substr(s,i-s));s=i+1;}
    else if(i==SIZE(str)-1){res.push_back(str.substr(s));}
  }
  return res;
}
struct TimeWatch{
  clock_t start_,end_;
  void start(){start_=clock();}
  double stop(){return (double)(clock()-start_)/CLOCKS_PER_SEC;}
};

struct edge{
  int to;
  int cost;
  edge(int t_,int c_){
    to=t_;
    cost=c_;
  }
  edge(){}
};

const int MAX_V=100100;
int N;
vector<edge> G1[MAX_V];
int d1[MAX_V];
vector<edge> G2[MAX_V];
int d2[MAX_V];

int dijkstra(int s,int t,vector<edge> *G,int *d){
  const int INF=1<<29;
  priority_queue<pii,vector<pii>,greater<pii> > pq;
  for(int i=0;i<N;i++)d[i]=INF;
  pq.push(pii(0,s));
  d[s]=0;
  while(pq.size()){
    pii p=pq.top();pq.pop();
    const int ccost=p.first;
    const int cnode=p.second;
    if(d[cnode]<ccost)continue;
    for(int i=0;i<(int)G[cnode].size();i++){
      edge &e=G[cnode][i];
      int to=e.to;
      int ncost=ccost+e.cost;
      if(d[to]>ncost){
        d[to]=ncost;
        pq.push(pii(ncost,to));
      }
    }
  }
  return d[t];
}

int main(){

  int M;
  while(cin>>M>>N&&(N|M)){
    //cout<<N<<" "<<M<<endl;
    for(int i=0;i<N;i++)G1[i].clear(),G2[i].clear();
    for(int i=0;i<M;i++){
      int a,b,c,d;
      cin>>a>>b>>c>>d;
      a--;b--;
      G1[a].push_back(edge(b,c));
      G1[b].push_back(edge(a,c));
      G2[a].push_back(edge(b,d));
      G2[b].push_back(edge(a,d));
    }
    int Q;
    cin>>Q;
    for(int i=0;i<Q;i++){
      int a,b,c;
      cin>>a>>b>>c;
      a--;b--;
      if(c==0)cout<<dijkstra(a,b,G1,d1)<<endl;
      else cout<<dijkstra(a,b,G2,d2)<<endl;
    }
  }
  
  return 0;
}

Measured memory_bytes: 6408