def warshall_floyd(nodes)
  n = nodes.size
  dist = Array.new(n) { Array.new(n) { [Float::INFINITY, Float::INFINITY] } }
  pred = Array.new(n) { Array.new(n) { [-1, -1] } }
  
  n.times do |i|
    dist[i][i] = [0, 0]
    
    nodes[i].each { |to, cost, time| dist[i][to] = [cost, time]}
  end
  
  n.times do |i|
    n.times do |j|
      n.times do |k|
        2.times do |value_as_dist|
          new_len = dist[j][i][value_as_dist] + dist[i][k][value_as_dist]
          
          if new_len < dist[j][k][value_as_dist]
            dist[j][k][value_as_dist] = new_len
          end
        end
      end
    end
  end
  
  dist
end

while true
  n, m = gets.split.map(&:to_i)

  break if n + m == 0

  nodes = Array.new(m) { [] }
  memo = Array.new(m) { [nil, nil] }

  n.times do
    a, b, cost, time = gets.split.map(&:to_i)
    a -= 1
    b -= 1
    
    nodes[a] << [b, cost, time]
    nodes[b] << [a, cost, time]
  end
  
  res = warshall_floyd(nodes)
  
  gets.to_i.times do
    p_k, q_k, r_k = gets.split.map(&:to_i)
    p_k -= 1
    q_k -= 1
    
    puts res[p_k][q_k][r_k]
  end
end