#!/usr/bin/ruby -Ku

#require 'pp'

loop do
  data = gets.split(nil)
  n, m = data.map{|e| e.to_i}
  
  break if n == 0 and m == 0

  INF = 2<<29

  road = Hash.new()
  road[:time] = Array.new(m+1).map!{Array.new(m+1, INF)}
  road[:cost] = Array.new(m+1).map!{Array.new(m+1, INF)}
  #road[:visit] = Array.new(m+1, false)

  # 初期化
  n.times{
    data = gets.split(nil)
    i, j, c, t = data.map{|e| e.to_i}
    road[:time][i][j] = road[:time][j][i] = t
    road[:cost][i][j] = road[:cost][j][i] = c  
  }

#  pp road

  n = gets.to_i
  n.times{
    data = gets.split(nil)
    st, gl, type = data.map{|e| e.to_i}

    type = :cost if type == 0
    type = :time if type == 1
    
    # スタートからのコストマップ作成
    costmap = Array.new(m+1, INF)
    costmap[st] = 0
    
    queue = Array.new(1, st)
    while !queue.empty?
      src = queue.shift
      (m+1).times{ |dest|
        if costmap[dest] > costmap[src] + road[type][src][dest] then
          costmap[dest] = costmap[src] + road[type][src][dest]
          queue.push(dest)
        end
      }
#      puts "queue:#{queue}"
    end
#    pp costmap
    puts costmap[gl]
  }
end