#!/usr/bin/env python3 INF = 10 ** 9 def is_kado(a, b, c): try: x = sorted((a, b, c))[1] return a > 0 and b > 0 and c > 0 and a != b and b != c and c != a and (a == x or c == x) except Exception: print("DAMEDESU") def minimize_cost_a(a, b, c): if is_kado(a, b, c): return 0 if a >= c: a, c = c, a if c >= b: cost = c - (b - 1) c = b - 1 if a >= c: cost += a - (c - 1) a = c - 1 if is_kado(a, b, c): return cost else: return None def minimize_cost_b(a, b, c): if is_kado(a, b, c): return 0 if a >= c: a, c = c, a if b >= a: cost = b - (a - 1) b = a - 1 if a == c: cost += a - (c - 1) a -= 1 if is_kado(a, b, c): return cost else: return None def minimize_cost(a, b, c): cost_a = minimize_cost_a(a, b, c) if cost_a is None: cost_a = INF cost_b = minimize_cost_b(a, b, c) if cost_b is None: cost_b = INF cost = min(cost_a, cost_b) return cost if cost < INF else -1 def main(): t = int(input()) for _ in range(t): a, b, c = (int(z) for z in input().split()) cost = minimize_cost(a, b, c) print(cost) if __name__ == "__main__": main()