
INF = 1 << 29

def reader; gets.split(" ").map(&:to_i); end

def add_edge(tb, from, to, val)
  tb[from][to] = val
  tb[to][from] = val
end

def solve!(tb, n)
  n.times do |k|
    n.times do |i|
      n.times do |j|
        m = tb[i][k] + tb[k][j]
        tb[i][j] = m if tb[i][j] > m
      end
    end
  end
end

loop do
  n, m = reader
  break if n == 0 and m == 0
  $costs = Array.new(m) {Array.new(m, INF)}
  $times = Array.new(m) {Array.new(m, INF)}
  n.times do
    from, to, cost, time = reader
    add_edge($costs, from - 1, to - 1, cost)
    add_edge($times, from - 1, to - 1, time)
  end
  solve!($costs, m)
  solve!($times, m)
  gets.to_i.times do
    from, to, r = reader
    tb = (r == 0) ? $costs : $times
    puts tb[from - 1][to - 1]
  end
end