import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines T = int(readline()) m = map(int,read().split()) ABC = zip(m,m,m) INF = 10 ** 18 def type_231(A,B,C): a = B - 1 if a > A: a = A c = a - 1 if c > C: c = C if c <= 0: return INF return (A - a) + (C - c) def type_213(A,B,C): a = C - 1 if a > A: a = A b = a - 1 if b > B: b = B if b <= 0: return INF return (A - a) + (B - b) def solve(A,B,C): x = min(type_231(A,B,C), type_231(C,B,A), type_213(A,B,C), type_213(C,B,A)) return x if x < INF else -1 answer = [solve(a,b,c) for a,b,c in ABC] print('\n'.join(map(str,answer)))