#include #include #include #include #include #include #include #include using namespace std; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using ll = long long; const long long MOD = 1e9+7; int main() { int t; cin >> t; const int INF = 1e9+1; for (int _t = 0; _t < t; _t++) { int a, b, c; cin >> a >> b >> c; int min_cost = INF; if (b >= 3) { int na = a, nc = c; if (na >= b) na = b - 1; if (nc >= b) nc = b - 1; if (na == nc) { if (na == 1) { na = -INF; nc = -INF; } else na--; } chmin(min_cost, a - na + c - nc); } if (a < c) swap(a, c); int nb = b, nc = c; if (a == nc) { nc--; } if (nb >= nc) nb = nc - 1; if (1 <= nb && 1 <= nc) chmin(min_cost, b - nb + c - nc); cout << (min_cost == INF ? -1 : min_cost) << endl; } }