Infinity = 1000000

def floydwarshall money, time
  n = money.size
  n.times do |k|
    n.times do |u|
      #for money
      next if money[u][k] == Infinity
      n.times do |v|
        total = money[u][k] + money[k][v]
        money[u][v] = [money[u][v], total].min
      end
      #for time
      next if time[u][k] == Infinity
      n.times do |v|
        total = time[u][k] + time[k][v]
        time[u][v] = [time[u][v], total].min
      end
    end
  end
end

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

  floydwarshall distMoney, distTime

  $stdin.gets.to_i.times do
    p,q,r = $stdin.gets.chomp.split(" ").map(&:to_i)
    case r
    when 0
      p distMoney[p -1][q - 1]
    when 1
      p distTime[p -1][q - 1]
    end
  end
end