#! /usr/bin/env ruby
# coding: utf-8

loop do

n,m = gets.split.map(&:to_i) 
if n+m==0 then
  break
end

matrix1 = Array.new(n+1,nil)
matrix1.length.times{ |i|
matrix1[i] = Array.new(n+1,1000000)
}
matrix2 = Array.new(n+1,nil)
matrix2.length.times{ |i|
matrix2[i] = Array.new(n+1,10000000)
}

for i in 1..n 
a,b,cost,time = gets.split.map(&:to_i)
matrix1[a][b] = matrix1[b][a] = cost
matrix2[a][b] = matrix2[b][a] = time
end

for k in 1..n
  for i in 1..n
    for j in 1..n
      matrix1[i][j] = [matrix1[i][j],matrix1[i][k]+matrix1[k][j] ].min
      matrix2[i][j] = [matrix2[i][j],matrix2[i][k]+matrix2[k][j] ].min
    end
  end
end

k = gets.to_i
for i in 1..k
  p,q,r = gets.split.map(&:to_i)
  if r == 0 then
    puts matrix1[p][q]
  else
    puts matrix2[p][q]
  end

end


end