Shubham has a binary string s. A binary string is a string containing only characters "0" and "1".

He can perform the following operation on the string any amount of times: 

  * Select an index of the string, and flip the character at that index. This means, if the character was "0", it becomes "1", and vice versa. 



A string is called good if it does not contain "010" or "101" as a subsequence — for instance, "1001" contains "101" as a subsequence, hence it is not a good string, while "1000" doesn't contain neither "010" nor "101" as subsequences, so it is a good string.

What is the minimum number of operations he will have to perform, so that the string becomes good? It can be shown that with these operations we can make any string good.

A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.

Input

The first line of the input contains a single integer t (1≤ t ≤ 100) — the number of test cases.

Each of the next t lines contains a binary string s (1 ≤ |s| ≤ 1000).

Output

For every string, output the minimum number of operations required to make it good.

Example

Input


7
001
100
101
010
0
1
001100


Output


0
0
1
1
0
0
2

Note

In test cases 1, 2, 5, 6 no operations are required since they are already good strings.

For the 3rd test case: "001" can be achieved by flipping the first character — and is one of the possible ways to get a good string.

For the 4th test case: "000" can be achieved by flipping the second character — and is one of the possible ways to get a good string.

For the 7th test case: "000000" can be achieved by flipping the third and fourth characters — and is one of the possible ways to get a good string.
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main
{	
   static long mod=(long)(1e+9 + 7);
	
   //static long mod=(long)998244353;
   static int[] sieve;
   static ArrayList<Integer> primes;
   static PrintWriter out;
   static fast s;
   static StringBuilder fans;
   
   static int x_dir[];
   static int y_dir[];

   public static void solve()
   {
   	 String str=s.nextLine();
   	 char ch[]=str.toCharArray();
   	 
   	 int n=str.length();
   	 
   	 int suf_one[]=new int[n];
   	 int suf_zero[]=new int[n];
   	 int pref_one[]=new int[n];
   	 int pref_zero[]=new int[n];
   	 
   	 if(ch[0]=='1') pref_zero[0]=1;
   	 else pref_one[0]=1;
   	 
   	 if(ch[n-1]=='1') suf_zero[n-1]=1;
   	 else suf_one[n-1]=1;
   	 
   	 for(int i=1;i<n;i++)
   		 {
   			 if(ch[i]=='1')
   				 {
   					 pref_zero[i]=pref_zero[i-1]+1;
   					 pref_one[i]=pref_one[i-1];
   				 }
   			 else
   				 {
   					 pref_zero[i]=pref_zero[i-1];
   					 pref_one[i]=pref_one[i-1]+1;
   				 }
   		 }
   	 
   	 for(int i=n-2;i>=0;i--)
   		 {
   			 if(ch[i]=='1')
   				 {
   					 suf_zero[i]=suf_zero[i+1]+1;
   					 suf_one[i]=suf_one[i+1];
   				 }
   			 else
   				 {
   					 suf_zero[i]=suf_zero[i+1];
   					 suf_one[i]=suf_one[i+1]+1;
   				 }
   		 }
   	 
   	 int min=pref_one[n-1];
   	 
   	 for(int i=0;i<n-1;i++)
   		 {
   			// System.out.println(pref_one[i]+" "+suf_zero[i+1]);
   			 min=Math.min(pref_zero[i]+suf_one[i+1], min);
   			 min=Math.min(pref_one[i]+suf_zero[i+1], min);
   		 }
   	 
   	 min=Math.min(min, pref_zero[n-1]);
   	 fans.append(min+"\n");
   }
   
   public static void main(String[] args) throws java.lang.Exception
   {	  	
	     s = new fast();
	     
	     out=new PrintWriter(System.out);
	     fans=new StringBuilder("");
		  
	     int t=s.nextInt();
	     
	     while(t>0)
	    	 {
	    		 solve();
	    		 t--;
	    	 }
	    
	    out.println(fans);
	    out.close();
   }
  
 
 
static class fast {   
	
	   private InputStream i;
		private byte[]  buf = new byte[1024];
		private int curChar;
		
		private int numChars;
		
		//Return floor log2n
		public static long log2(long bits) // returns 0 for bits=0
			{
			    int log = 0;
			    if( ( bits & 0xffff0000 ) != 0 ) { bits >>>= 16; log = 16; }
			    if( bits >= 256 ) { bits >>>= 8; log += 8; }
			    if( bits >= 16  ) { bits >>>= 4; log += 4; }
			    if( bits >= 4   ) { bits >>>= 2; log += 2; }
			    return log + ( bits >>> 1 );
			}
		
		public static boolean next_permutation(int a[])
		   {
			   int i=0,j=0;int index=-1;	   
			   int n=a.length;
			   
			   for(i=0;i<n-1;i++)
				   if(a[i]<a[i+1]) index=i;
			   
			   if(index==-1) return false;
			   i=index;
 
			  
			   for(j=i+1;j<n && a[i]<a[j];j++);
 
			   int temp=a[i];
			   a[i]=a[j-1];
			   a[j-1]=temp;
			   	   
			   for(int p=i+1,q=n-1;p<q;p++,q--)
			   {
				   temp=a[p];
				   a[p]=a[q];
				   a[q]=temp;
			   }
			   
			   return true;
		   }
		   
		public static void sieve(int size)
		{
			sieve=new int[size+1];
			
			primes=new ArrayList<Integer>();
			
			sieve[1]=1;
			
			for(int i=2;i*i<=size;i++)
			{
				if(sieve[i]==0)
				 {
					for(int j=i*i;j<size;j+=i) {sieve[j]=1;}
				 }
			}
			
			for(int i=2;i<=size;i++)
			{
				if(sieve[i]==0) primes.add(i);
			}
		}
		
		public static long pow(long a, long p){
		    long o = 1;
		    for(; p>0; p>>=1){
		        if((p&1)==1)o = mul(o, a);
		        a = mul(a, a);
		    }
		    return o;
		}
		
		public static long add(long... a){
		    long o = 0;
		    for(long x:a)o = (o+mod+x)%mod;
		    return o;
		}
		
		public static long mul(long... a){
		    long p = 1;
		    for(long x:a)p = (mod+(p*x)%mod)%mod;
		    return p;
		}
		
		public static long mod_inv(long n)
		{
			return pow(n,mod-2);
		}
		   
		public long gcd(long r,long ans)
		{
			if(r==0) return ans;
			return gcd(ans%r,r);
		}
		
		public fast() {
			this(System.in);
		}
		public fast(InputStream is) {
			i = is;
		}
		public int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = i.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}
		public String nextLine() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndOfLine(c));
			return res.toString();
		}
		public String nextString() {
     		int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}
		public long nextLong() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}
		public int nextInt() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}
		public boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}
		public boolean isEndOfLine(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}
 
	}	
}
