Go: // Warshall–Floyd Algorithm

package main

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

var scanner = bufio.NewScanner(os.Stdin)

const INF = 2000000000

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
			c_matrix[b-1][a-1] = c
			t_matrix[a-1][b-1] = t
			t_matrix[b-1][a-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
					c_prev[i][j] = -1
				} else {
					c_dist[i][j] = c_matrix[i][j]
					c_prev[i][j] = i
				}
				if t_matrix[i][j] == 0 {
					t_dist[i][j] = INF
					t_prev[i][j] = -1
				} else {
					t_dist[i][j] = t_matrix[i][j]
					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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)

template<typename T>
void remove(vector<T>& vector, unsigned int index){
vector.erase(vector.begin() + index);
}

typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

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


int main(void){
	while(cin >> n >> m, n|m){
		fill_n(**cost, 2*101*101, 10000000);
		for(int i = 0; i < n; i++){
			int a, b,c,t;
			cin >> a >> b >> c >> t;
			cost[1][a][b] = cost[1][b][a] = c;
			cost[0][a][b] = cost[0][b][a] = t;
		}
		for(int k=0;k<n;++k)for(int i=0;i<n;++i)for(int j=0;j<n;++j){
			cost[0][i][j] = min(cost[0][i][j], cost[0][i][k] + cost[0][k][j]);
			cost[1][i][j] = min(cost[1][i][j], cost[1][i][k] + cost[1][k][j]);
		}
		int k;
		cin >> k;
		rep(i, k){
			int a, b, c;
			cin >> a >> b >> c;
			cout << cost[c%2][a][b] << endl;
		}
	}

	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}

C++: #include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin(); it!=(c).end(); it++)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pr(a) cout << (a) << endl
#define PR(a,b) cout << (a) << " " << (b) << endl;
#define F first
#define S second
typedef long long ll;
typedef pair<int,int> P;
const int MAX=1000000001;
const ll MAXL=1000000000000000001LL;
const ll mod=1000000007;
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int main() {
  int n,m;
  while(cin >> n >> m && (n||m)) {
    m++;
    int dp[m][m],dp2[m][m];
    fill(dp[0],dp[m],MAX);
    fill(dp2[0],dp2[m],MAX);
    rep(i,m) {
      dp[i][i]=0;
      dp2[i][i]=0;
    }
    rep(i,n) {
      int x,y,a,b;
      cin >> x >> y >> a >> b;
      dp[x][y]=min(dp[x][y],a);
      dp[y][x]=min(dp[y][x],a);
      dp2[x][y]=min(dp2[x][y],b);
      dp2[y][x]=min(dp2[y][x],b);
    }
    rep(k,m) {
      rep(i,m) {
	rep(j,m) {
	  dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
	  dp2[i][j]=min(dp2[i][j],dp2[i][k]+dp2[k][j]);
	}
      }
    }
    int k;
    cin >> k;
    rep(i,k) {
      int x,y,z;
      cin >> x >> y >> z;
      if(z) pr(dp2[x][y]);
      else pr(dp[x][y]);
    }
  }
  return 0;
}

C++: #include <iostream>
#include <vector>

const int INF = 1e9;
#define rep(i, b) for(int i = 0; i < b; i++)
#define repi(i, a, b) for(int i = a; i < b; i++)

using namespace std;

int main(){
    int n, m;
    while(cin >> n >> m, n || m){
        int w[128][128][2];
        rep(i,m+1)rep(j,m+1)rep(k,2) w[i][j][k] = INF;

        rep(i,n){
            int a, b, c, t;
            cin >> a >> b >> c >> t;
            w[a][b][0] = c;
            w[b][a][0] = c;
            w[a][b][1] = t;
            w[b][a][1] = t;
        }
        rep(l,2) repi(k, 1, m+1) repi(i, 1, m+1) repi(j, 1, m+1)
            w[i][j][l] = min(w[i][j][l], w[i][k][l] + w[k][j][l]);

        int k;
        cin >> k;

        rep(i,k){
            int p, q, r;
            cin >> p >> q >> r;
            cout << w[p][q][r] << endl;
        }
    }
}

C++:  #include<iostream>
 #include<stdio.h>
 #include<string>
 #include<math.h>
 #include<iomanip>
 #include<algorithm>
 #include<string.h>
 #include<cctype>
 #include<map>
 #include<set>
 #include<vector>
 #include<sstream>
 #include<stack>
 #include<queue>
 #include<deque>
 #include<functional>
 #include<utility>
 
 using namespace std;
 
 const int MAX_V=100+5;
 const int INF=100000000;

 int main()
 {
   int graph[2][MAX_V][MAX_V];
   int path,V;
   while(cin>>path>>V&&(path||V))
   {
     for(int m=0;m<2;m++)
     {
       for(int i=0;i<V;i++)
       {
         for(int j=0;j<V;j++)
         {
           if(i==j) graph[m][i][j]=0;
           else graph[m][i][j]=INF;        
         }       
       }        
     }  
     while(path--)
     {
       int p,q;
       int cost,time;
       cin>>p>>q>>cost>>time;
       graph[0][p-1][q-1]=cost;
       graph[1][p-1][q-1]=time;     
       graph[0][q-1][p-1]=cost;
       graph[1][q-1][p-1]=time;         
     }                       
     for(int m=0;m<2;m++)
     {
       for(int k=0;k<V;k++)
       {
         for(int i=0;i<V;i++)
         {
           for(int j=0;j<V;j++)
           {
             graph[m][i][j]=min(graph[m][i][j],graph[m][i][k]+graph[m][k][j]);        
           }        
         }          
       }        
     }     
     int que;
     cin>>que;
     while(que--)
     {
       int start,end,type;
       cin>>start>>end>>type;
       cout<<graph[type][start-1][end-1]<<endl;           
     }
   }
   //while(1);
   return 0;
 }