import sys
import heapq as h

inf = sys.maxsize

# おまじない
class heapq:
    def __init__(self):
        self.q = []

    def push(self,v):
        h.heappush(self.q, v)
    
    def pop(self):
        return h.heappop(self.q)

    def is_empty(self):
        return len(self.q) == 0

# 入力:graphと始点s
# 出力:始点sから各頂点tへの距離dist
def shortestPath0(graph, s):
    n = len(graph)

    dist    = [inf for _ in range(n)]
    visited = [False for _ in range(n)]
    dist[s] = 0

    while True:
        idx, u  = -1, inf
        for i in range(n):
            if not visited[i] and dist[i] < u:
                idx = i
                u = dist[i]
        
        if u == inf: break
        
        visited[idx] = True

        for (next, w) in graph[idx]:
            newdist = w + dist[idx]
            if newdist < dist[next]:
                dist[next] = newdist
    return dist

def shortestPath1(graph, s):
    n = len(graph)

    dist    = [inf for _ in range(n)]
    visited = [False for _ in range(n)]
    
    q = heapq()
    q.push((0, s))

    while not q.is_empty():
        (cur_dist, v) = q.pop()

        if visited[v] or dist[v] <= cur_dist:
            continue

        dist[v] = cur_dist
        visited[v] = True

        for next, w in graph[v]:
            if not visited[next] and dist[v] + w < dist[next]:
                q.push((dist[v] + w, next))

    return dist

def main():
    while True:
        n, m = map(int, input().split())
        if n == 0 and m == 0:
            break
        # 隣接リスト
        tgraph = [[] for _ in range(m)]
        cgraph = [[] for _ in range(m)]
        
        for i in range(n):
            a, b, cost, time = map(int, input().split())
            a -= 1
            b -= 1
            cgraph[a].append((b, cost))
            cgraph[b].append((b, cost))
            tgraph[a].append((b, time))
            tgraph[b].append((b, time))
            
        c_dist = [shortestPath1(cgraph, s) for s in range(m)]
        t_dist = [shortestPath1(tgraph, s) for s in range(m)]

        k = int(input())
        for i in range(k):
            p, q, r = map(int, input().split())
            p-=1
            q-=1
            if r == 0:
                print(c_dist[p][q])
            else:
                print(t_dist[p][q])
main()