Infinity = 1000000

def dijkstra graph, s, d
  #initialize
  n = graph.size
  q = Array.new
  n.times do |u|
    q << [u, graph[s][u]]
  end

  while ! q.empty?
    #そこまでのコストが最も小さい節点を選択する
    q.sort!{|a, b| a[1] <=> b[1]}
    node = q.delete_at(0)
    u = node[0]
    w = node[1]
    if u == d
      return graph[s][d]
    end
    #そこからコストが更新できる節点を更新する
    n.times do |v|
      next if graph[u][v] == Infinity
      newLength = w + graph[u][v]
      if newLength < graph[s][v]
        q << [v, newLength]
        graph[s][v] = newLength
      end
    end
  end
end

loop do
  #入力値で初期化
  n,m = $stdin.gets.chomp.split(" ").map(&:to_i)
  break if (n|m) == 0
  graphMoney = Array.new(m).map{Array.new(m, Infinity)}
  graphTime = Array.new(m).map{Array.new(m, Infinity)}
  m.times do |u|
    graphMoney[u][u] = 0
    graphTime[u][u] = 0
  end
  #線路情報からグラフ生成
  n.times do
    a,b,cost,time = $stdin.gets.chomp.split(" ").map(&:to_i)
    graphMoney[a - 1][b - 1] = cost
    graphMoney[b - 1][a - 1] = cost
    graphTime[a - 1][b - 1] = time
    graphTime[b - 1][a - 1] = time
  end

  $stdin.gets.to_i.times do
    p,q,r = $stdin.gets.chomp.split(" ").map(&:to_i)
    case r
    when 0
      p dijkstra(graphMoney, p - 1, q - 1)
    when 1
      p dijkstra(graphTime, p - 1, q - 1)
    end
  end
end