require 'set'

class PriorityQueue < SortedSet
  @@id = 0
  def add obj
    super([obj,@@id])
    @@id+=1
  end

  def << obj
    add obj
  end

  def push obj
    add obj
  end

  def top
    self.min[0]
  end

  def pop
    self.delete(self.min)
  end
end

class State
  attr_accessor :v, :d
  def initialize v,d
    self.v = v
    self.d = d
  end

  def <=> other
    self.d <=> other.d
  end
end

INF = 2**30

def dijkstra g, s
  dist = Array.new(g.length, INF)
  dist[s] = 0
  q = PriorityQueue.new
  q << State.new(s,0)
  while q.size > 0
    s = q.top
    q.pop
    v = s.v
    next if dist[v] < s.d
    g[v].each do |e|
      if dist[e.dst] > dist[v] + e.weight
        dist[e.dst] = dist[v] + e.weight
        q << State.new(e.dst, dist[e.dst])
      end
    end
  end
  dist
end

class Edge
  attr_accessor :src, :dst, :weight
  def initialize src, dst, weight
    self.src = src
    self.dst = dst
    self.weight = weight
  end
end

loop do
  e,v = gets.chomp.split.map(&:to_i)
  break if e==0

  g1 = Array.new(v).map{ Array.new }
  g2 = Array.new(v).map{ Array.new }

  e.times do
    a,b,cost,time = gets.chomp.split.map(&:to_i)
    a-=1
    b-=1
    g1[a] << Edge.new(a,b,cost)
    g1[b] << Edge.new(b,a,cost)
    g2[a] << Edge.new(a,b,time)
    g2[b] << Edge.new(b,a,time)
  end

  d1 = Array.new(v)
  d2 = Array.new(v)
  for i in 0...v
    d1[i] = dijkstra g1, i
    d2[i] = dijkstra g2, i
  end

  k = gets.chomp.to_i

  k.times do
    p,q,r = gets.chomp.split.map(&:to_i)
    p-=1
    q-=1
    if r==0
      puts d1[p][q]
    else
      puts d2[p][q]
    end
  end
end