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




namespace ConsoleApplication1 {

    

    class ClassMain {


        static int[,] pathM = new int[0,0];
        static int[,] pathT = new int[0,0];

        static int dijikM(int start, int end) {
            int len = pathM.GetLength(0);
            bool[] visited = new bool[len];
            int[] cost = Enumerable.Repeat(int.MaxValue, len).ToArray();
            cost[start] = 0;
            while(true){
                int minNode = -1;
                int minCos = int.MaxValue;
                for (int i = 0; i < len; i++) {
                    if (visited[i] == false) {
                        if (cost[i] < minCos) {
                            minCos = cost[i];
                            minNode = i;
                        }
                    }
                }
                if (minNode == -1 || minNode == end) { break; }
                visited[minNode] = true;

                for (int i = 0; i < len; i++) {
                    if (pathM[minNode, i] > 0) {
                        cost[i] = Math.Min(cost[i], cost[minNode] + pathM[minNode, i]);
                    }
                }
            }
            return cost[end];
        }
        static int dijikT(int start, int end) {
            int len = pathM.GetLength(0);
            bool[] visited = new bool[len];
            int[] cost = Enumerable.Repeat(int.MaxValue, len).ToArray();
            cost[start] = 0;

            while (true) {
                int minNode = -1;
                int minCos = int.MaxValue;
                for (int i = 0; i < len; i++) {
                    if (visited[i] == false) {
                        if (cost[i] < minCos) {
                            minCos = cost[i];
                            minNode = i;
                        }
                    }
                }
                if (minNode == -1 || minNode == end) { break; }
                visited[minNode] = true;

                for (int i = 0; i < len; i++) {
                    if (pathT[minNode, i] > 0) {
                        cost[i] = Math.Min(cost[i], cost[minNode] + pathT[minNode, i]);
                    }
                }
            }

            return cost[end];
        }


        public static void Main(String[] args) {
           string line;
           while ((line = Console.ReadLine()) != null) {
                int[] ints = line.Split(' ').Select(int.Parse).ToArray();
                int n = ints[0];
                //int n = int.Parse(line);                
                if (n == 0) { break; }
                int m = ints[1];
                pathM = new int[m + 1, m + 1];
                pathT = new int[m + 1, m + 1];

                for (int i = 0; i < n; i++) {
                    ints = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                    pathM[ints[0], ints[1]] = ints[2];
                    pathT[ints[0], ints[1]] = ints[3];
                    pathM[ints[1], ints[0]] = ints[2];
                    pathT[ints[1], ints[0]] = ints[3];
                }

                int q = int.Parse(Console.ReadLine());
                for (int i = 0; i < q; i++) {
                    ints = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                    if (ints[2] == 0) {//money
                        Console.WriteLine(dijikM(ints[0], ints[1]));
                    }
                    else {
                        Console.WriteLine(dijikT(ints[0], ints[1]));
                    }

                }
 

            }
        }

    }

    


}

Measured memory_bytes: 6484