import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); for (int i = 0; i < t; i++) { String[] sa = br.readLine().split(" "); long a = Integer.parseInt(sa[0]); long b = Integer.parseInt(sa[1]); long c = Integer.parseInt(sa[2]); long[] val = new long[4]; Arrays.fill(val, Long.MAX_VALUE); if (b >= 3 && a >= 2) { val[0] = Math.max(a - (b - 1), 0) + Math.max(c - Math.min(a - 1, b - 2), 0); } if (b >= 3 && c >= 2) { val[1] = Math.max(c - (b - 1), 0) + Math.max(a - Math.min(c - 1, b - 2), 0); } if (a >= 3 && c >= 2) { val[2] = Math.max(c - (a - 1), 0) + Math.max(b - Math.min(c - 1, a - 2), 0); } if (c >= 3 && a >= 2) { val[3] = Math.max(a - (c - 1), 0) + Math.max(b - Math.min(a - 1, c - 2), 0); } long ans = Long.MAX_VALUE; for (int j = 0; j < val.length; j++) { ans = Math.min(ans, val[j]); } if (ans == Long.MAX_VALUE) { System.out.println(-1); } else { System.out.println(ans); } } br.close(); } }