INF = 99999.freeze

edges_size, nodes_size, = gets.split.map(&:to_i)
# nodes_size = gets.chomp.to_i.freeze
# edges_size = gets.chomp.to_i.freeze
edges_cost = []
edges_time = []

# nodes?????????
nodes_size.times do |i|
  nodes_size.times do |j|
    edges[i] ||= []
    edges[i][j] = INF
  end
  edges[i][i] = 0
end

# ?????????????????????
edges_size.times do |i|
  edge = gets.split(' ').map(&:to_i)
  edges[edge[0]] ||= []
  edges_cost[edge[0]][edge[1]] = edge[2]
  edges_time[edge[0]][edge[1]] = edge[3]
end

# ???????????£???????????????
def Warshall_Floyd(edges)
  nodes_size.times do |i|
    nodes_size.times do |j|
      nodes_size.times do |k|
        edges[i][j] = [edges[i][j], edges[i][k] + edges[k][j]].min
      end
    end
  end
  return edges
end

edges_cost = Warshall_Floyd(edges_cost)
edges_time = Warshall_Floyd(edges_time)

num = gets.to_i

num.times do
  hoge = gets.split(' ').map(&:to_i)
  case hoge[2]
  when 0
    puts edges_cost[hoge[0]][hoge[1]]
  when 1
    puts edges_time[hoge[0]][hoge[1]]
  end
end