Go: // Warshall–Floyd Algorithm

package main

import (
	"bufio"
	"os"
	"strings"
	"strconv"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

const INF = 0x7FFFFFFF

func main() {
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		n, _ := strconv.Atoi(buf[0])
		m, _ := strconv.Atoi(buf[1])
		if n == 0 && m == 0 {
			break
		}
		c_matrix := make([][]int, m)
		for i := range c_matrix {
			c_matrix[i] = make([]int, m)
		}
		t_matrix := make([][]int, m)
		for i := range t_matrix {
			t_matrix[i] = make([]int, m)
		}
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			a, _ := strconv.Atoi(buf_[0])
			b, _ := strconv.Atoi(buf_[1])
			c, _ := strconv.Atoi(buf_[2])
			t, _ := strconv.Atoi(buf_[3])
			c_matrix[a-1][b-1] = c
			t_matrix[a-1][b-1] = t
		}
		c_dist := make([][]int, m)
		c_prev := make([][]int, m)
		t_dist := make([][]int, m)
		t_prev := make([][]int, m)
		for i := 0; i < m; i++ {
			c_dist[i] = make([]int, m)
			c_prev[i] = make([]int, m)
			t_dist[i] = make([]int, m)
			t_prev[i] = make([]int, m)
		}
		for i := 0; i < m; i++ {
			for j := 0; j < m; j++ {
				if c_matrix[i][j] == 0{
					c_dist[i][j] = INF
				} else {
					c_dist[i][j] = c_matrix[i][j]
				}
				if c_matrix[i][j] == 0{
					c_prev[i][j] = -1
				} else {
					c_prev[i][j] = i
				}
				if t_matrix[i][j] == 0{
					t_dist[i][j] = INF
				} else {
					t_dist[i][j] = t_matrix[i][j]
				}
				if t_matrix[i][j] == 0{
					t_prev[i][j] = -1
				} else {
					t_prev[i][j] = i
				}
			}
			c_dist[i][i] = 0
			t_dist[i][i] = 0
		}
		for k := 0; k < m; k++ {
			for i := 0; i < m; i++ {
				for j := 0; j < m; j++ {
					c_newlen := c_dist[i][k] + c_dist[k][j]
					if c_newlen < c_dist[i][j] {
						c_dist[i][j] = c_newlen
						c_prev[i][j] = c_prev[k][j]
					}
				}
			}
		}
		for k := 0; k < m; k++ {
			for i := 0; i < m; i++ {
				for j := 0; j < m; j++ {
					t_newlen := t_dist[i][k] + t_dist[k][j]
					if t_newlen < t_dist[i][j] {
						t_dist[i][j] = t_newlen
						t_prev[i][j] = t_prev[k][j]
					}

				}
			}
		}
		scanner.Scan()
		k, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < k; i++ {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			p, _ := strconv.Atoi(buf_[0])
			q, _ := strconv.Atoi(buf_[1])
			r, _ := strconv.Atoi(buf_[2])
			switch r {
			case 0: fmt.Println(c_dist[p-1][q-1])
			case 1: fmt.Println(t_dist[p-1][q-1])
			}
		}
	}
}

C++: #include <cstdio>
#include <iostream>
#include <string>
#define INF 1002
using namespace std;

int main(){
  int i,j,k,kk,aa,bb,n,m,a,b,d,dd,c[101][101],t[101][101],f;
  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;

    for(i=0;i<=m;i++){
      for(j=0;j<=m;j++){
	if(i == j){
	  c[i][j] = 0;
	  t[i][j] = 0;
	}
	else{
	c[i][j] = INF;
	t[i][j] = INF;
	}
      }
    }

    for(i=0;i<n;i++){
      cin >> a >> b >> d >> dd;
      c[a][b] = c[b][a] = d;
      t[a][b] = t[b][a] = dd;
    }

    for(i=0;i<=m;i++){
      for(j=0;j<=m;j++){
	for(k=0;k<=m;k++){
	  if(t[i][j] > t[i][k] + t[k][j]) t[i][j] = t[i][k] + t[k][j];
	  if(c[i][j] > c[i][k] + c[k][j]) c[i][j] = c[i][k] + c[k][j];
	}
      }
    }

    cin >> kk;
    for(i=0;i<kk;i++){
      cin >> aa >> bb >> f;
      if(f) cout << t[aa][bb] << endl;
      else cout << c[aa][bb] << endl;
    }

  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#define INF 1000000000
using namespace std;

int cost[101][101];
int t_cost[101][101];
int n,m;

void warshall_floyd(){
  for(int i = 0 ; i < m ; i++ ){
    for(int j = 0 ; j < m ; j++ ){
      for(int k = 0 ; k < m ; k++ ){
	cost[i][j] = min(cost[i][j],cost[i][k] + cost[k][j]);
	t_cost[i][j] = min(t_cost[i][j],t_cost[i][k] + t_cost[k][j]);
      }
    }
  }
}

void input(){
  for(int i = 0 ; i < n ; i++ ){
    int a,b,c,d;
    cin >> a >> b >> c >> d;
    cost[a][b] = cost[b][a] = c;
    t_cost[a][b] = t_cost[b][a] = d;  
  }
}

void init(){
  for(int i = 0  ; i < m ; i++ ){
    for(int j = 0  ; j < m ; j++ ){
      if(i == j){
	cost[i][j] = 0;
	t_cost[i][j] = 0;
      }else{
	cost[i][j] = INF; 
	t_cost[i][j] = INF; 
      }
    }
  }
}

int main(){

  while(cin >> n >> m , n+m ){
    m++;
    init();
    input();
    warshall_floyd();
    int k;
    cin >> k;
    for(int i = 0 ; i < k ; i++ ){
      int from,to,r;
      cin >> from >> to >> r; 
      if(r == 0){
	cout << cost[from][to] << endl;
      } else if(r == 1) {
	cout << t_cost[from][to] << endl;
      }
    }
  }
  return 0;
}

C++: //青春の片道切符：：ワーシャルフロイド法

	/*for(int k=0;k<N;k++){//経由箇所(N<200)
		for(int i=0;i<N;i++){//スタート(N<200)
			for(int j=0;j<N;j++){//目的(N<200)
				d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
			}
		}
	}*/


#include<iostream>
#include<string>
#include<queue>
#include<utility>
#include<algorithm>
#include<cctype>
using namespace std;

long long gc[110][110];
long long gt[110][110];
int n,m;

void warshallfroid(){
	for(int k=0;k<n;k++){//経由箇所(N<200)
		for(int i=0;i<n;i++){//スタート(N<200)
			for(int j=0;j<n;j++){//目的(N<200)
				gc[i][j]=min(gc[i][j],gc[i][k]+gc[k][j]);
				gt[i][j]=min(gt[i][j],gt[i][k]+gt[k][j]);
			}
		}
	}
}



int main(){
	
	while(cin>>n>>m,n||m/*n,mが両方0のときお終わり*/){
		for(int i=0;i<m;i++){//スタート(N<200)
			for(int j=0;j<m;j++){//目的(N<200)
				gc[i][j]=9999999;//初期化
				gt[i][j]=9999999;//初期化
			}
		}
	

	
	for(int i=0;i<n;i++){
		int a,b,c,t;
		cin>>a>>b>>c>>t;
		a--,b--;
		gc[a][b]=gc[b][a]=c;
		gt[a][b]=gt[b][a]=t;
	}
	warshallfroid();
	
	int k;
	cin>>k;
	for(int i=0;i<k;i++){
	int p,q,r;
		cin>>p>>q>>r;
		p--,q--;
		if(r==0)cout<<gc[p][q]<<endl;
		else cout<<gt[p][q]<<endl;
	}
	
	}

}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

using vvpii = vector<vpii>;

void solve() {
  int n, m;
  while (cin >> n >> m, n) {
    vvpii dp(m, vpii(m));
    rep(i, m) rep(j, m) dp[i][j] = i == j ? mp(0LL, 0LL) : mp(INF, INF);
    while (n--) {
      int a, b, cost, time;
      cin >> a >> b >> cost >> time;
      a--, b--;
      dp[a][b] = dp[b][a] = mp(cost, time);
    }
    rep(k, m) rep(i, m) rep(j, m) {
      cmin(dp[i][j].fi, dp[i][k].fi + dp[k][j].fi);
      cmin(dp[i][j].se, dp[i][k].se + dp[k][j].se);
    }
    int k;
    cin >> k;
    while (k--) {
      int p, q, r;
      cin >> p >> q >> r;
      p--;
      q--;
      cout << (r ? dp[p][q].se : dp[p][q].fi) << endl;
    }
  }
}