gets.to_i.times do a, b, c = gets.split.map(&:to_i) min, max = [a, b, c].minmax result = if a == b && b == c if b <= 2 1 else 3 end elsif a == c if a <= 1 -1 else 1 end elsif b == min || b == max 0 elsif b <= 2 -1 else [min == 1 ? 0 : b - min + 1, max - b + 1].select(&:positive?).min end puts result end