#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main(){ cin.tie(0); ios::sync_with_stdio(0); int T; cin >> T; rep(_,T) { ll A,B,C; cin >> A >> B >> C; ll ans = 1e18; // A > B < C ll nA = A, nB = min(B, min(A, C) - 1), nC = C; if(nA == nC) { nA--; if(nA == nB) nB--; } if(nA != nC && nB > 0) { ans = min(ans, A - nA + B - nB + C - nC); } // A < B > C nA = min(A, B - 2), nB = B, nC = min(C, B - 1); if(nA > 0 && nC > 0) { ans = min(ans, A - nA + B - nB + C - nC); } nA = min(A, B - 1), nB = B, nC = min(C, B - 2); if(nA > 0 && nC > 0) { ans = min(ans, A - nA + B - nB + C - nC); } cout << (ans == ll(1e18) ? -1 : ans) << "\n"; } }