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.
import java.util.*;
import java.io.*;
import java.math.BigInteger;
	public class Main {
		//static ArrayList<String> list=new ArrayList<>();
		
		public static void main(String[] args) throws FileNotFoundException,IOException, InterruptedException {
			Scanner s=new Scanner(System.in);
			PrintWriter pw=new PrintWriter(System.out);
			
			
			int t=s.nextInt();
			
			while (t-->0)
			{
				String x=s.next();
				int cnt0=0,cnt1=0;
				if (x.charAt(0)=='0')cnt0++;
				else cnt1++;
				for (int i=1; i<x.length();i++)
					if (x.charAt(i)=='0' && x.charAt(i-1)=='1')cnt0++;
					else if (x.charAt(i)=='1') cnt1++;
				
				//pw.println(cnt0+" "+cnt1);
				if (cnt1!=0)
				{
					if (cnt0>=2)pw.println(2);
					else pw.println(cnt0);
				}
				else
					pw.println(1);
				
			}
			
//			int n=s.nextInt();
//			int []a=s.nextIntArr(n);
//			int []diff=new int [n];
//			
//			for (int i=0; i<n;i++)
//			{	
//				int x=s.nextInt();
//				//pw.println(a[i]+" "+x);
//				diff[i]=a[i]-x;
//				
//			}
////			pw.println(cntP+" "+cnt0);
////			pw.println(tm);
//			//pw.println(Arrays.toString(diff));
//			shuffle(diff);
//			Arrays.sort(diff);
//			int cntP=0,cnt0=0;
//			TreeMap<Integer,Integer> tm=new TreeMap<>();
//			//pw.println(Arrays.toString(diff));
//			for (int i=0; i<n;i++)
//			{
//				if (diff[i]==0)cnt0++;
//				if (diff[i]>0)tm.put(diff[i],++cntP);
//			}
//			//pw.println(tm);
//			int temp=cntP;
//			cntP=cnt0+(--cntP);
//			long ans=0;
//			for (int i=0; i<n;i++)
//			{
//				if (diff[i]>0)
//				{
//					ans+=cntP--;
//				}
//				else if (diff[i]<0)
//				{
//					Integer x=tm.ceilingKey(Math.abs(diff[i])+1);
//					if (x!=null)
//						ans+=temp-tm.get(x)+1;
//				}
//			}
//			pw.println(ans);
			

		
				
			

				
				
				

		
		pw.flush();
		
		}
//		static void takeOrLeave( int i, String x){
//			
//	        if(i == n+1)
//	        {
//	        	System.out.println(x);
//	        	//list.add(x);
//				return;
//	        }
//	        
//	        String y=x;
//	        y+=i;
//	        takeOrLeave(i+1,x+i);
//	        takeOrLeave(i+1,x);
//	        
//	       
//	    }
//	static void takeOrLeave( int i, ArrayList<Integer> acc){
//				
//		        if(i == n+1)
//		        {
//		        	System.out.println(acc);
//		        	//list.add(acc);
//		        	return;
//		        }
//		        
//		      //  acc.add(i);
//		        ArrayList<Integer> temo=(ArrayList<Integer>) acc.clone();
//		        temo.add(i);
//		        takeOrLeave(i+1,temo);
//		        //acc.remove(i);
//		        takeOrLeave(i+1,acc);
//		        
//		       
//		    }
	public static void permute(char []a , int l, int r)
    {
        if (l == r)
            System.out.println(Arrays.toString(a));
        else
        {
            for (int i = l; i <= r; i++)
            {
            	a= swap(a,l,i);
                permute(a, l+1, r);
                a = swap(a,l,i);
            }
        }
    }
		public static  char[] swap(char []a, int l, int i) {
			char temp=a[l];
			a[l]=a[i];
			a[i]=temp;
			
		return a;
	}
		public static double log2(long m)
	    {
	        double result = (double)(Math.log(m) / Math.log(2));
	  
	        return result;
	    }

		public static void shuffle(int []a)
		{	
			for (int i=0; i<a.length;i++)
			{
				int randIndex=(int)(Math.random()*a.length);
				int temp=a[i];
				a[i]=a[randIndex];
				a[randIndex]=temp;
			}
			
		}
		
		
		
		
		
		}

	 class Pair implements Comparable{
		int x;
		int y;
		Pair(int x, int y)
		{
			this.x=x;
			this.y=y;
		}
		@Override
		public int compareTo(Object o) {
			Pair p=(Pair)o;
			return this.x-p.x;
		}
		
	}
	class Scanner 
	{
		StringTokenizer st;
		BufferedReader br;

		public Scanner() {
		br = new BufferedReader(new InputStreamReader(System.in));
}		
		public Scanner(String s) throws FileNotFoundException
		{
			br =new BufferedReader(new FileReader(s));
		}

		public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(s));}


		public String next() throws IOException 
		{
			while (st == null || !st.hasMoreTokens()) 
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {return Integer.parseInt(next());}
		
		public long nextLong() throws IOException {return Long.parseLong(next());}

		public String nextLine() throws IOException {return br.readLine();}
		
		public double nextDouble() throws IOException
		{
			String x = next();
			StringBuilder sb = new StringBuilder("0");
			double res = 0, f = 1;
			boolean dec = false, neg = false;
			int start = 0;
			if(x.charAt(0) == '-')
			{
				neg = true;
				start++;
			}
			for(int i = start; i < x.length(); i++)
				if(x.charAt(i) == '.')
				{
					res = Long.parseLong(sb.toString());
					sb = new StringBuilder("0");
					dec = true;
				}
				else
				{
					sb.append(x.charAt(i));
					if(dec)
						f *= 10;
				}
			res += Long.parseLong(sb.toString()) / f;
			return res * (neg?-1:1);
		}
		 public long[] nextLongArr(int n) throws IOException {
	            long[] arr = new long[n];
	            for (int i = 0; i < n; i++)
	                arr[i] = nextLong();
	            return arr;
	        }

	        public int[] nextIntArr(int n) throws IOException {
	            int[] arr = new int[n];
	            for (int i = 0; i < arr.length; i++)
	                arr[i] = nextInt();
	            return arr;
	        }
		
		public boolean ready() throws IOException {return br.ready();}


	}
	
	