A binary string is a string that consists of characters 0 and 1.

Let \operatorname{MEX} of a binary string be the smallest digit among 0, 1, or 2 that does not occur in the string. For example, \operatorname{MEX} of 001011 is 2, because 0 and 1 occur in the string at least once, \operatorname{MEX} of 1111 is 0, because 0 and 2 do not occur in the string and 0 < 2.

A binary string s is given. You should cut it into any number of substrings such that each character is in exactly one substring. It is possible to cut the string into a single substring — the whole string.

A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

What is the minimal sum of \operatorname{MEX} of all substrings pieces can be?

Input

The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.

Each test case contains a single binary string s (1 ≤ |s| ≤ 10^5).

It's guaranteed that the sum of lengths of s over all test cases does not exceed 10^5.

Output

For each test case print a single integer — the minimal sum of \operatorname{MEX} of all substrings that it is possible to get by cutting s optimally.

Example

Input


6
01
1111
01100
101
0000
01010


Output


1
0
2
1
1
2

Note

In the first test case the minimal sum is \operatorname{MEX}(0) + \operatorname{MEX}(1) = 1 + 0 = 1.

In the second test case the minimal sum is \operatorname{MEX}(1111) = 0.

In the third test case the minimal sum is \operatorname{MEX}(01100) = 2.
//package com.company;

import java.io.*;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;

public class Main {

    static boolean[] primecheck = new boolean[1000002];

    public static void main(String[] args) throws IOException {
        OutputStream outputStream = System.out;
        FastReader in = new FastReader();
        PrintWriter out = new PrintWriter(outputStream);
        PROBLEM solver = new PROBLEM();
        int t = 1;
        t = in.nextInt();
        for (int i = 0; i < t; i++) {
            solver.solve(in, out);
        }
        out.close();
    }

    static class PROBLEM {

        public void solve(FastReader in, PrintWriter out) {

           char[] s = in.nextLine().toCharArray();

           int sum = 0;
           int change = 0;
            for (int i = 1; i < s.length; i++) {
                if(s[i] != s[i-1]) change++;
            }

            if(change > 2) sum = 2;
            if(change == 2){
                if(s[0] == '0') sum = 2;
                else sum = 1;
            }
            if(change == 1) sum = 1;
            if(change == 0){
                if(s[0] == '0') sum = 1;
                else sum = 0;
            }
            out.println(sum);
//            int n = in.nextInt();
//            int[] a = in.readArray(n);
//            int l = 0;
//            HashSet<Integer> hs = new HashSet<>();
//            for (int i = 0; i < n; i++) {
//                hs.add(a[i]);
//            }
//            int[] r = new int[hs.size()+1];
//            for (int i = 0; i < hs.size(); i++) {
//                int b = in.nextInt();
//                r[b]++;
//            }
//
//            int days = 0, curResearch = 1;
//
//            for (int i = 0; i < n; i++) {
//                if(curResearch == hs.size()) break;
//                if(a[i] == 0){
//                    days++;
//                    if(r[curResearch] == 0) curResearch++;
//                    if(curResearch == hs.size()) break;
//                    while(r[curResearch] == 0) curResearch++;
//                    r[curResearch]--;
//
//                }else{
//                    if(r[a[i]] == 0){
//                        days++;
//                    }else{
//                        days++;
//                        r[a[i]]--;
//                    }
//                }
//            }
//
//            out.println(days);

//            int n = in.nextInt();
//            int[] a = in.readArray(n);
//
//            int[] l = new int[n], r = new int[n];
//            int m = 0;
//            l[0] = 1;
//
//            int sum = 0;
//            int flag = 0;
//            for (int i = 0; i < n; i++) {
//                if(a[i] != 0) flag = 1;
//            }
//
//            if(flag == 0) {
//                out.println("NO");
//                return;
//            }
//
//            for (int i = 0; i < n; i++) {
//                sum+=a[i];
//                if(sum == 0){
//                    if(i == n-1 && r[0] == 0){
//
//                        if(a[i] == 0) continue;
//                        sum = a[i];
//                        r[m++] = i;
//                        l[m] = i+1;
//                        if(i == n-1) {
//                            r[m++] = i + 1;
//                            break;
//                        }
//                    }
//                }
//
//                if(i == n-1){
//                    r[m++] = n;
//                }
//            }
//
//            out.println("YES");
//            out.println(m);
//
//            for (int i = 0; i < m; i++) {
//                out.println(l[i] + " " + r[i]);
//            }

//            int n = in.nextInt();
//            int[] a = in.readArray(n);
//
//            a = mergeSort(a);
//            boolean isValid = true;
//
//            for (int i = 0; i < n-1; i++) {
//                if(a[i+1]-a[i]>1){
//                    isValid = false;
//                    break;
//                }
//            }
//            int ans = 0;
//
//            if(isValid){
//                int m =998244353;
//                int max = a[n-1],maxL = 0, cntMaxL = 0, cntMax = 0;
//                for (int i = n-1; i >= 0 ; i--) {
//                    if(a[i] != max && maxL == 0){
//                        maxL = a[i];
//                    }else cntMax++;
//                    if(maxL != 0){
//                        if(a[i] == maxL) cntMaxL++;
//                        else break;
//                    }
//                }
//
//                if(cntMax>1){
//                    ans = factorial(n, m);
//                    ans = ((ans%m) * (n%m)) %m;
//                }else{
//                    int rem = n - cntMax-cntMaxL;
//                    int nowRem = rem;
//                    long sub = 0;
//                    while(nowRem>0){
//                        sub+= (nPr(rem, nowRem, m) * factorial(n-1-nowRem, m));
//                        nowRem--;
//                    }
//                    ans = factorial(n-1,m);
//                    ans = ((ans%m) * ((n-1)%m)) %m;
//                    ans = (int)(((ans%m) - (sub%m) + m) % m);
//                }
//            }
//            out.println(ans);
        }
    }

    static int nPr(int n, int r, int m){
        int num = factorial(n, m);
        int den = factorial(r, m);
        den*=factorial(n-r, m);

        return num/den * factorial(r, m);
    }

    static int factorial(int n, int m){
        int ans = 1;
        while(n>0){
            ans = ((ans%m)*(n*m))%m;
            n--;
        }
        return ans;
    }

    static boolean isPalindromeI(int n){

        int d = 0;
        int y = n;
        while(y>0){
            d++;
            y/=10;
        }

        int[] a = new int[d];
        for (int i = 0; i < d; i++) {
            a[i] = n%10;
            n/=10;
        }

        System.out.println(Arrays.toString(a));
        for (int i = 0; i < d / 2; i++) {
            if(a[i] != a[d-i-1]) return false;
        }
        return true;
    }

    static long gcd(long a, long b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }

    static boolean isSquare(double a) {
        boolean isSq = false;
        double b = Math.sqrt(a);
        double c = Math.sqrt(a) - Math.floor(b);

        if (c == 0) isSq = true;

        return isSq;
    }

    static int exponentMod(int A, int B, int C) {

        // Base cases
        if (A == 0)
            return 0;
        if (B == 0)
            return 1;

        // If B is even
        long y;
        if (B % 2 == 0) {
            y = exponentMod(A, B / 2, C);
            y = (y * y) % C;
        }

        // If B is odd
        else {
            y = A % C;
            y = (y * exponentMod(A, B - 1,
                    C) % C) % C;
        }

        return (int) ((y + C) % C);
    }


    static class pair implements Comparable<pair>{
        int x,y;
        pair(int xi, int yi){
            x=xi;
            y=yi;
        }
        @Override
        public int compareTo(pair other){
            if(this.x>other.x){return 1;}
            if(this.x<other.x){return -1;}
            if(this.y>other.y){return 1;}
            if(this.y<other.y){return -1;}
            return 0;
        }
    }

//    public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
//        public U x;
//        public V y;
//
//        public Pair(U x, V y) {
//            this.x = x;
//            this.y = y;
//        }
//
//        public int hashCode() {
//            return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
//        }
//
//        public boolean equals(Object o) {
//            if (this == o)
//                return true;
//            if (o == null || getClass() != o.getClass())
//                return false;
//            Pair<U, V> p = (Pair<U, V>) o;
//            return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
//        }
//
//        public int compareTo(Pair<U, V> b) {
//            int cmpU = x.compareTo(b.x);
//            return cmpU != 0 ? cmpU : y.compareTo(b.y);
//        }
//
//        public String toString() {
//            return String.format("(%s, %s)", x.toString(), y.toString());
//        }
//
//    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        char nextChar() {
            return next().charAt(0);
        }

        boolean nextBoolean() {
            return !(nextInt() == 0);
        }
        // boolean nextBoolean(){return Boolean.parseBoolean(next());}

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        public int[] readArray(int size) {
            int[] array = new int[size];
            for (int i = 0; i < size; i++) array[i] = nextInt();
            return array;
        }

    }

    private static int[] mergeSort(int[] array) {

        //array.length replaced with ctr
        int ctr = array.length;
        if (ctr <= 1) {
            return array;
        }

        int midpoint = ctr / 2;

        int[] left = new int[midpoint];
        int[] right;

        if (ctr % 2 == 0) {
            right = new int[midpoint];
        } else {
            right = new int[midpoint + 1];
        }

        for (int i = 0; i < left.length; i++) {
            left[i] = array[i];
        }

        for (int i = 0; i < right.length; i++) {
            right[i] = array[i + midpoint];
        }

        int[] result = new int[array.length];

        left = mergeSort(left);
        right = mergeSort(right);

        result = merge(left, right);

        return result;
    }

    private static int[] merge(int[] left, int[] right) {

        int[] result = new int[left.length + right.length];
        int leftPointer = 0, rightPointer = 0, resultPointer = 0;

        while (leftPointer < left.length || rightPointer < right.length) {

            if (leftPointer < left.length && rightPointer < right.length) {

                if (left[leftPointer] < right[rightPointer]) {
                    result[resultPointer++] = left[leftPointer++];
                } else {
                    result[resultPointer++] = right[rightPointer++];
                }
            } else if (leftPointer < left.length) {
                result[resultPointer++] = left[leftPointer++];
            } else {
                result[resultPointer++] = right[rightPointer++];
            }
        }
        return result;
    }

    public static void Sieve(int n) {

        Arrays.fill(primecheck, true);
        primecheck[0] = false;
        primecheck[1] = false;

        for (int i = 2; i * i < n + 1; i++) {
            if (primecheck[i]) {
                for (int j = i * 2; j < n + 1; j += i) {
                    primecheck[j] = false;
                }
            }
        }
    }
}


