#include <iostream>
#include <cstdio>
#include <algorithm>
#include <array>
#include <vector>
#include <queue>
#include <set>
#include <functional>
#include <utility>

using namespace std;

class Station {
public:
  int to;
  int cost;
  int time;
  Station(){
	to = cost = time = 0;
  }
  Station( const int to, const int cost, const int time ){
	this->to = to;
	this->cost = cost;
	this->time = time;
  }
};

class mycmp
{
public:
  bool operator() ( pair<int, int>& lhs, pair<int, int>& rhs )
  {
	return lhs.second > rhs.second;
  }
};

const int COST = 0;
const int TIME = 1;

int solve( const int p, const int q, const int r,
		   const vector<vector<Station> > table ) {

  priority_queue<pair<int, int>, vector<pair<int, int> >, mycmp > que;
  que.push( make_pair( p, 0 ) );

  set<int> visited;
  
  while ( !que.empty() ) {
  	auto elem = que.top();
  	const int stat = elem.first;
  	const int cost = elem.second;
  	que.pop();
	
  	if ( stat == q ) {
  	  return cost;
  	} else if ( visited.find( stat ) == visited.end() ) {
  	  visited.insert( stat );
  	  for ( const Station s : table[stat] ) {
  		if ( r == COST ) {
  		  que.push( make_pair( s.to, cost + s.cost ) );
  		} else {
  		  que.push( make_pair( s.to, cost + s.time ) );
  		}
  	  }
  	}	
  }
  return -1;
}
  
int main() {
  
  while ( true ) {
   	int n, m;
   	cin >> n >> m;
   	if ( n == 0 && m == 0 ) break;
	vector<vector<Station> > table(m + 1);
	for ( int i = 0; i < n; i++ ) {
	  int from, to, cost, time;
	  cin >> from >> to >> cost >> time;
	  table[ from ].push_back( Station( to, cost, time ) );
	  table[ to ].push_back( Station( from, cost, time ) );
	}
	int k;
	cin >> k;
	for ( int i = 0; i < k; i++ ) {
	  int p, q, r;
	  cin >> p >> q >> r;
	  cout << solve( p, q, r, table ) << endl;
	}
  }  
}