Go: // Warshall–Floyd Algorithm

package main

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

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
					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
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
#define INF 10000000
#define MAX_V 101
struct edge{int to,cost;};
typedef pair<int,int> P;//firstは最短距離,secondは頂点番号

int V;
vector<edge> G[2][MAX_V];
int d[2][MAX_V];

void dijkstra(int s,int q){
	priority_queue<P,vector<P>,greater<P> > que;
	fill(d[q],d[q]+V,INF);
	d[q][s]=0;
	que.push(P(0,s));
	while(!que.empty()){
		P p=que.top();que.pop();//仮の最短距離が短い順に取り出す
		int v=p.second;
		if(d[q][v]<p.first)continue;
		REP(i,G[q][v].size()){
			edge e=G[q][v][i];
			if(d[q][e.to]>d[q][v]+e.cost){
				d[q][e.to]=d[q][v]+e.cost;
				que.push(P(d[q][e.to],e.to));//仮の最短距離と頂点の組を更新が行われる度に追加していく
			}
		}
	}
}

int ar[2][14];
int cp[101][101],tp[101][101];

int main(){
	int n,m;
	while(cin>>n>>m){
		if(n==0&&m==0)break;
		V=m;
		
		REP(i,2)REP(j,MAX_V)G[i][j].clear();
		REP(i,101)REP(j,101)cp[i][j]=INF,tp[i][j]=INF;
		REP(i,n){
			int at,bt,ct,tt;
			cin>>at>>bt>>ct>>tt;
			at--;bt--;
			if(at>bt)swap(at,bt);
			cp[at][bt]=min(cp[at][bt],ct);
			tp[at][bt]=min(tp[at][bt],tt);
		}
		REP(i,m)FOR(j,i+1,m){
			if(cp[i][j]!=INF){
				edge e;
				e.to=j;e.cost=cp[i][j];
				G[0][i].PB(e);
				e.to=i;
				G[0][j].PB(e);
				e.to=j;e.cost=tp[i][j];
				G[1][i].PB(e);
				e.to=i;
				G[1][j].PB(e);
			}
		}
		REP(i,m){
			dijkstra(i,0);
			dijkstra(i,1);
			REP(j,m){
				if(i!=j&&d[0][j]!=INF){
					cp[i][j]=d[0][j];
					tp[i][j]=d[1][j];
				}
			}
		}
		int k;
		cin>>k;
		REP(i,k){
			int p,q,r;
			cin>>p>>q>>r;
			p--;q--;
			if(r==0)cout<<cp[p][q]<<endl;
			else cout<<tp[p][q]<<endl;
		}
	}
	return 0;
}

/*
4
5+4*3+2/(1-1-1)=
(1+4)*(3+7)/5=
5+4-3=
(1+5)*(2+3)=
3
1+2*3/2=
1+2*(3-2)/(4+5)=
1*2+3/(2+5)=
*/

C++: #include <iostream>
#define INFINITY 1000000
int main (){
    int m,n;
    std::cin >> n >> m;
    int table_c[100][100];
    int table_j[100][100];
    for (size_t i = 1; i <= m; i++)
    {
        for (size_t j = 1; j <= m; j++)
        {
            if(i==j){
                table_c[i][j] = 0;
                table_j[i][j] = 0;
            }else{
                table_c[i][j] = INFINITY;
                table_j[i][j] = INFINITY;
            }
            
        }
        
    }
    
    for (size_t i = 1; i <= n; i++)
    {
        int a,b,cost,jikan;
        std::cin >> a >> b >> cost >> jikan;
        table_c[a][b] = cost;
        table_c[b][a] = cost;
        table_j[a][b] = jikan;
        table_j[b][a] = jikan;
    }

    for (size_t i = 1; i <= m; i++)
    {
        for (size_t j = 1; j <= m; j++)
        {   
            for (size_t k = 1; k <= m; k++)
            {
                table_c[i][j] = std::min(table_c[i][k] + table_c[k][j], table_c[i][j]);
                table_j[i][j] = std::min(table_j[i][k] + table_j[k][j], table_j[i][j]);

            }
        }
    }
    
    int k;
    std::cin >> k;
    int p[50],q[50],r[50];
    for (size_t i = 0; i < k; i++)std::cin >> p[i] >> q[i] >> r[i];
    for (size_t i = 0; i < k; i++){
        if (r[i]){
            std::cout << table_j[p[i]][q[i]] << std::endl;

        }else{
            std::cout << table_c[p[i]][q[i]] << std::endl;
        }
    }
}