def cost(A,B): return max(A-B,0) T=int(input()) X=[0]*T inf=float("inf") for i in range(T): A,B,C=map(int,input().split()) #\/型 A1,B1,C1=A,B,C if A==C: A1-=1 B1=min(B1,min(A1,C1)-1) if min(A1,B1,C1)<=0: alpha=inf else: alpha=cost(A,A1)+cost(B,B1)+cost(C,C1) #/\型 A2,B2,C2=A,B,C A2=min(A,B-1) if A2!=B-1: C2=min(C2,B-1) else: C2=A2-1 if min(A2,B2,C2)<=0: beta=inf else: beta=cost(A,A2)+cost(B,B2)+cost(C,C2) if min(alpha,beta)==inf: X[i]=-1 else: X[i]=min(alpha,beta) print(*X,sep="\n")