#include <bits/stdc++.h>
using namespace std;

bool isAlt(const string& s) {
    for (size_t i = 1; i < s.size(); ++i)
        if (s[i] == s[i - 1]) return false;
    return true;
}

int minOps(const string& s) {
    if (isAlt(s)) return 0;
    
    int c0 = 0, c1 = 0;
    for (char ch : s) ch == '0' ? c0++ : c1++;
    
    return abs(c0 - c1) > 1 ? 2 : 1;
}



int main() {
	int T;
    cin >> T;
    while (T--) {
        string S;
        cin >> S;
        cout << minOps(S) << endl;
    }
    return 0;

}
