using System;
using System.Collections.Generic;
using System.Linq;

namespace AOJ.Volume2
{
    public class TravelingAlone
    {
        private static int station_cnt;
        private static int[,] price;
        private static int[,] time;

        private struct Station
        {
            public int No;
            public int Cost;
            public bool Confirm;
        }
        private static Station[] dp;

        private static int Solve(int start, int goal, int kind)
        {
            // ダイクストラ法で最短経路を求める
            dp[start].Cost = 0;
            dp[start].Confirm = true;

            int current = start;
            while (true)
            {
                int min_cost = int.MaxValue;
                int min_no = 0;
                for (int i = 1; i <= station_cnt; i++)
                {
                    if (dp[i].Confirm) { continue; }

                    int c = 0;
                    // kind == 0の場合は金額計算それ以外は時間を計算
                    if (kind == 0) { c = price[current, i]; }
                    else { c = time[current, i]; }

                    if (c > 0)
                    {
                        if (dp[i].Cost < 0) { dp[i].Cost = dp[current].Cost + c; }
                        else { dp[i].Cost = Math.Min(dp[i].Cost, dp[current].Cost + c); }
                    }

                    if (dp[i].Cost > 0)
                    {
                        if (min_cost > dp[i].Cost)
                        {
                            min_cost = dp[i].Cost;
                            min_no = dp[i].No;
                        }
                    }
                }
                // Costが最小のものを確定させてQueueに追加
                if (min_no > 0)
                {
                    dp[min_no].Confirm = true;
                    if (min_no == goal) { return dp[min_no].Cost; }
                    else { current = min_no; }
                }
                else { break; }
            }

            // ここに到達した場合は経路なし
            return -1;
        }

        public static int Main()
        {
            while (true)
            {
                var n_m = Console.ReadLine().Split(' ');
                int rail_cnt = int.Parse(n_m[0]);
                station_cnt = int.Parse(n_m[1]);
                if (rail_cnt == 0 && station_cnt == 0) { break; }

                // 金額、移動時間の情報
                price = new int[station_cnt + 1, station_cnt + 1];
                time = new int[station_cnt + 1, station_cnt + 1];
                for (int i = 0; i < rail_cnt; i++)
                {
                    var input = Console.ReadLine().Split(' ');
                    int from = int.Parse(input[0]);
                    int to = int.Parse(input[1]);
                    int p = int.Parse(input[2]);
                    int t = int.Parse(input[3]);

                    price[from, to] = p;
                    time[from, to] = t;
                    // fromとtoを逆にした情報も
                    price[to, from] = p;
                    time[to, from] = t;
                }

                // 問い合わせの数
                int q = int.Parse(Console.ReadLine());
                for (int i = 0; i < q; i++)
                {
                    var input = Console.ReadLine().Split(' ');
                    int start = int.Parse(input[0]);
                    int goal = int.Parse(input[1]);
                    int kind = int.Parse(input[2]);

                    // 最短距離計算
                    dp = new Station[station_cnt + 1];
                    for (int j = 1; j <= station_cnt; j++)
                    {
                        dp[j].No = j;
                        dp[j].Cost = -1;
                        dp[j].Confirm = false;
                    }

                    int answer = Solve(start, goal, kind);
                    Console.WriteLine(answer);
                }
            }
            return 0;
        }
    }
}

Measured memory_bytes: 9760