from sys import stdin def main(): input = lambda: stdin.readline()[:-1] T = int(input()) ABC = [list(map(int, input().split())) for _ in [0] * T] for abc in ABC: ans = 0 while 1: if abc.count(1) == 2 or max(abc) < 3: print(-1) break if abc[0] != abc[2] and (abc[0] < abc[1] > abc[2] or abc[0] > abc[1] < abc[2]): print(ans) break if abc[0] == abc[2]: abc[0] -= 1 ans += 1 continue center = max(0, abc[1] - min(abc) + 1) left = max(0, abc[0] - abc[1] + 1) right = max(0, abc[2] - abc[1] + 1) if center <= left + right: abc[1] -= center ans += center else: abc[0] -= left abc[2] -= right ans += left + right if abc.count(0): print(-1) break main()