n = gets.to_i
n.times{
    x, y, b, p = gets.chomp.split.map(&:to_i)
    amount = x*b + y*p
    amount * 0.8 if b>=5 && p>=2
    addb = [5 - b, 0].max
    addp = [2 - p, 0].max
    amount2 = (x * ( b + addb) + y * (p + addp)) * 0.8
    puts [amount.to_i, amount2.to_i].min
}
