T = gets.to_i # /\ cost def f(a, b, c) cost = 0 if a >= b d = a - b + 1 a -= d cost += d end if c >= b d = c - b + 1 c -= d cost += d end if a == c cost += 1 a -= 1 end return Float::INFINITY if a <= 0 || c <= 0 cost end # \/ cost def g(a, b, c) cost = 0 if a == c a -= 1 cost += 1 end return Float::INFINITY if a <= 0 if a <= b d = b - a + 1 b -= d cost += d end if c <= b d = b - c + 1 b -= d cost += d end return Float::INFINITY if b <= 0 cost end T.times do a, b, c = gets.split.map(&:to_i) if a > b && c > b && a != c puts 0 elsif a < b && c < b && a != c puts 0 else v = [f(a, b, c), g(a, b, c)].min if v == Float::INFINITY puts -1 else puts v end end end