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

public class Hello{
    public static Station[] Stations;
    public static int[][][] Rails;
    
    public static void Main(){
        Stations = new Station[Consts.MAXSTATIONS + 1];
        for(int i = 0; i < Stations.Length; i++){
            Stations[i] = new Station();
        }
        Stations[0].Checked = true;
        
        Rails = new int[Consts.MAXSTATIONS + 1][][];
        for(int i = 0; i < Rails.Length; i++){
            Rails[i] = new int[Consts.MAXSTATIONS + 1][];
            for(int j = 0; j < Consts.MAXSTATIONS + 1; j++){
                Rails[i][j] = Enumerable.Repeat(-1, 2).ToArray();
            }
        }
        
/*
PQueue pq = new PQueue();
{
Random rnd = new Random();
for(int i = 0; i < 7; i++){
pq.Enqueue(rnd.Next(100), rnd.Next(100));
System.Console.Write(i + " ");
pq.Print(" ");
}
System.Console.WriteLine();
for(int i = 0; i < 7; i++){
System.Console.Write(pq.Dequeue() + " ");
pq.Print(" ");
}
}
*/     
        
        while(true){
            // init
            for(int i = 0; i < Rails.Length; i++)
                for(int j = 0; j < Rails[i].Length; j++)
                    for(int k = 0; k < Rails[i][j].Length; k++)
                        Rails[i][j][k] = -1;
                        
            // init data input
            string[] InputMapData = Console.ReadLine().Split(' ');
            int RailNum = int.Parse(InputMapData[0]);
            int StationNum = int.Parse(InputMapData[1]);
            if(RailNum == 0 && StationNum == 0) break;

            // rails data input
            for(int i = 0; i < RailNum; i++){
                string[] InputRailData = Console.ReadLine().Split(' ');
                int FromStationID = int.Parse(InputRailData[0]);
                int ToStationID = int.Parse(InputRailData[1]);
                int Cost = int.Parse(InputRailData[2]);
                int Time = int.Parse(InputRailData[3]);
                Rails[FromStationID][ToStationID][0] = Cost;
                Rails[FromStationID][ToStationID][1] = Time;
                Rails[ToStationID][FromStationID][0] = Cost;
                Rails[ToStationID][FromStationID][1] = Time;
            }
            // querys
            int QueryNum = int.Parse(Console.ReadLine());
            for(int i = 0; i < QueryNum; i++){
                string[] InputQuery = Console.ReadLine().Split(' ');
                Console.WriteLine(
                    Solve(
                        int.Parse(InputQuery[0]),
                        int.Parse(InputQuery[1]),
                        int.Parse(InputQuery[2])
                    )
                );
            }
        }
    }
    
    public static int Solve(int StartStationID, int GoalStationID, int QueryType){
        // initial data
        int CheckStationID = 0;
        for(int i = 1; i < Stations.Length; i++){
            Stations[i].MinCost = int.MaxValue;
            Stations[i].Checked = false;
        }
        
        // first stations cost is 0 and checked
        //Stations[StartStationID].Checked = true;
        Stations[StartStationID].MinCost = 0;
        PQueue pq = new PQueue();
        pq.Enqueue(StartStationID, Stations[StartStationID].MinCost);

        int TmpTotalCost;
        while(true){
            try{
                CheckStationID = pq.Dequeue();
//System.Console.WriteLine("Dequeue " + CheckStationID);
            }
            catch(Exception e){
                if(e is ArgumentOutOfRangeException || e is InvalidOperationException)
                    break;
            }
            if(Stations[CheckStationID].Checked) continue;
            
            
            
            Stations[CheckStationID].Checked = true;
            if(CheckStationID == GoalStationID) break;
            for(int ToStationID = 1; ToStationID < Stations.Length; ToStationID++){
                if(Rails[CheckStationID][ToStationID][QueryType] < 0) continue;
                TmpTotalCost = Stations[CheckStationID].MinCost + Rails[CheckStationID][ToStationID][QueryType];
                if(TmpTotalCost < Stations[ToStationID].MinCost){
                    Stations[ToStationID].MinCost = TmpTotalCost;
//System.Console.WriteLine("  Enqueue ToStationID:" + ToStationID + " MinCost:" +  Stations[ToStationID].MinCost);
                    pq.Enqueue(ToStationID, Stations[ToStationID].MinCost);
                }
                
            }
        }
        return Stations[GoalStationID].MinCost;
    }
}

public class Station{
    public int MinCost {get;set;}
    public bool Checked {get;set;}
    public Station(){
        MinCost = int.MaxValue;
        Checked = false;
    }
}

public static class Consts{
    public static int MAXSTATIONS = 100;
}

public class PQueue{
    private List<int[]> Tree;
    public PQueue(){
        Tree = new List<int[]>();
    }
    public void Enqueue(int key, int value){
        int nIdx = Tree.Count;
        Tree.Add(new int[]{key, value});
        if(1 < Tree.Count){
            while(nIdx != 0){
                int ParentIdx = (nIdx - 1) / 2;
                if(Tree[ParentIdx][1] > Tree[nIdx][1]){
                    int[] tmp = Tree[ParentIdx]; Tree[ParentIdx] = Tree[nIdx]; Tree[nIdx] = tmp;
                    nIdx = ParentIdx;
                }
                else break;

            }
        }
    }
    public int Dequeue(){
        int Ret = 0;
        try{
            Ret = Tree[0][0];
            Tree[0] = Tree[Tree.Count - 1];
            Tree.RemoveAt(Tree.Count - 1);
        }
        catch(Exception e){
            throw e;
        }
        
        int nIdx = 0;

        while(true){
            int LChildIdx = nIdx * 2 + 1;
            int RChildIdx = LChildIdx + 1;
            int TargetIdx = 0;
            if(RChildIdx < Tree.Count){
                TargetIdx = Tree[LChildIdx][1] > Tree[RChildIdx][1] ?
                            RChildIdx :
                            LChildIdx ;
            }
            else if(LChildIdx < Tree.Count)
                TargetIdx = LChildIdx;
            else break;
                
            if(Tree[nIdx][1] > Tree[TargetIdx][1]){
                int[] tmp = Tree[TargetIdx]; Tree[TargetIdx] = Tree[nIdx]; Tree[nIdx] = tmp;
            }
            nIdx = TargetIdx;
        }
        
        return Ret;
    }
    
    public void Print(string Sepalator){
        for(int i = 0; i < Tree.Count; i++){
            System.Console.Write(Tree[i][0] + "," + Tree[i][1]);
            if(i != Tree.Count - 1) System.Console.Write(Sepalator);
        }
        System.Console.WriteLine();
    }
}

Measured memory_bytes: 10552