# Original solution
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, t):
    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, t):
    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 v == t:
            return cur_dist

        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))

def main():
    while True:
        n, m = list(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 = list(map(int, input().split()))
            a -= 1
            b -= 1
            cgraph[a].append((b, cost))
            cgraph[b].append((a, cost))
            tgraph[a].append((b, time))
            tgraph[b].append((a, time))
            

        k = int(eval(input()))
        for i in range(k):
            p, q, r = list(map(int, input().split()))
            p-=1
            q-=1
            if p > q:
                p, q = q, p
            if r == 0:
                print((shortestPath1(cgraph, p, q)))
            else:
                print((shortestPath1(tgraph, p, q)))
main()

# Optimized solution
inf = 0x10101010
def solve(A,strt):
    cost = [inf]*m
    visited = [False]*m
    cost[strt] = 0
    while 1:
        min = inf
        next = -1
        visited[strt] = True
        for i in range(m):
            if visited[i]: continue
            if A[strt][i]:
                d = cost[strt] + A[strt][i]
                if d < cost[i]:
                    cost[i] = d
            if min > cost[i]:
                min = cost[i]
                next = i
        strt = next
        if next == -1: break
    return cost
     
while 1:
    n,m = list(map(int,input().split()))
    if n == 0: break
    T = [[0]*m for i in range(m)]
    C = [[0]*m for i in range(m)]
    for i in range(n):
        a,b,c,t = list(map(int,input().split()))
        T[a-1][b-1] = T[b-1][a-1] = t
        C[a-1][b-1] = C[b-1][a-1] = c
    TS = [solve(T,i) for i in range(m)]
    CS = [solve(C,i) for i in range(m)]
    for i in range(eval(input())):
        a,b,q = list(map(int,input().split()))
        if q == 0:
            print(CS[a-1][b-1])
        else:
            print(TS[a-1][b-1])