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.
//package learning;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class NitsLocal {
    static ArrayList<String> s1;
    static boolean[] prime;
    static int n = 200001;
    
     static void sieve() {
Arrays.fill(prime	, true);
prime[0] = prime[1] = false;
for(int i = 2 ; i * i <= n ; ++i) {
if(prime[i]) {
for(int k = i * i; k<= n ; k+=i) {
prime[k] = false;
}
}
}
 
}
    public static void main(String[] args) {
        InputReader sc = new InputReader(System.in);
        
           //prime  = new boolean[n + 1];
          // sieve();
                //prime[1] = false;
              
                  
             /*    
        int n = sc.ni();
        int k = sc.ni();
        int []vis = new int[n+1];
        int []a = new int[k];
        for(int i=0;i<k;i++)
        {
            a[i] = i+1;
        }
        int j = k+1;
        int []b = new int[n+1];
        for(int i=1;i<=n;i++)
        {
            b[i] = -1;
        }
        int y = n - k +1;
        int tr = 0;
        int y1 = k+1;
        while(y-- > 0)
        {
        System.out.print("? ");
        for(int i=0;i<k;i++)
        {
            System.out.print(a[i] + " ");
        }
        
        System.out.flush();
        int pos = sc.ni();
        int a1 = sc.ni();
        b[pos] = a1;
        
        
        for(int i=0;i<k;i++)
        {
            if(a[i] == pos)
            {
                a[i] = y1;
                y1++;
            }
        }
        }
        ArrayList<Integer> a2 = new ArrayList<>();
        if(y >= k)
        {
            
        int c = 0;
        int k1 = 0;
            for(int i=1;i<=n;i++)
            {
                if(b[i] != -1)
                {
                    c++;
                    a[k1] = i;
                    a2.add(b[i]);
                    k1++;
                }
                if(c==k)
                    break;
            }
            
            Collections.sort(a2);
            System.out.print("? ");
        for(int i=0;i<k;i++)
        {
            System.out.print(a[i] + " ");
        }
        System.out.println();
        System.out.flush();
        int pos = sc.ni();
        int a1 = sc.ni();
        int ans = -1;
        for(int i=0;i<a2.size();i++)
        {
            if(a2.get(i) == a1)
            {
                ans = i+1;
                break;
            }
        }
        System.out.println("!" + " " + ans);
           System.out.flush();  
        }
        else
        {
            int k1 = 0;
            a = new int[k];
             for(int i=1;i<=n;i++)
            {
                if(b[i] != -1)
                {
                    
                    a[k1] = i;
                    a2.add(b[i]);
                    k1++;
                }
             
            }
              for(int i=1;i<=n;i++)
            {
                if(b[i] == -1)
                {
                    
                    a[k1] = i;
                    
                    k1++;
                    if(k1==k)
                        break;
                }
             
            }
              int ans = -1;
              while(true)
              {
                   System.out.print("? ");
                     for(int i=0;i<k;i++)
                   {
                         System.out.print(a[i] + " ");
                  }
                   System.out.println();
                   System.out.flush();
                   int pos = sc.ni();
                   int a1 = sc.ni();
                   int f = 0;
                   
                   if(b[pos] != -1)
                   {
                       Collections.sort(a2);
                       for(int i=0;i<a2.size();i++)
                       {
                           if(a2.get(i) == a1)
                           {
                               ans = i+1;
                               f = 1;
                               System.out.println("!" + " " + ans);
                               System.out.flush();
                               break;
                           }
                       }
                       if(f==1)
                           break;
                   }
                   else
                   {
                       b[pos] = a1;
                        a = new int[k];
                        a2.add(a1);
             for(int i=1;i<=n;i++)
            {
                if(b[i] != -1)
                {
                    
                    a[k1] = i;
                    a2.add(b[i]);
                    k1++;
                }
             
            }
              for(int i=1;i<=n;i++)
            {
                if(b[i] == -1)
                {
                    
                    a[k1] = i;
                    
                    k1++;
                    if(k1==k)
                        break;
                }
             
                       
                   }
              }
        }  
          */
             
 
    /*
          int n = sc.ni();
          int []a = sc.nia(n);
          int []b = sc.nia(n);
          Integer []d = new Integer[n];
          int []d1 = new int[n];
          for(int i=0;i<n;i++)
          {
              d[i] = (a[i] - b[i]);
             
          }
          int l = 0;
          int r = n-1;
          Arrays.sort(d);
          long res = 0;
          while(l < r)
          {
              if(d[l] + d[r] > 0)
              {
                  res += (long) (r-l);
                  r--;
              }
              else 
                  l++;
          }
          
          w.println(res);
*/
      /*
      int n = sc.ni();
     
      ArrayList<Integer> t1 = new ArrayList<>();
      int []p1 = new int[n+1];
      int []q1 = new int[n-1];
      int []q2 = new int[n-1];
      for(int i=0;i<n-1;i++)
      {
          int p = sc.ni();
          int q = sc.ni();
          t1.add(q);
          p1[p]++;
          q1[i] = p;
          q2[i] = q;
          
      }
      int res = 0;
      for(int i=0;i<t1.size();i++)
      {
          if(p1[t1.get(i)] == 0)
          {
              res = t1.get(i);
              //break;
          }
      }
      
    int y = 1;
    for(int i=0;i<n-1;i++)
    {
        if(q2[i] == res)
        {
            w.println("0");
        }
        else
        {
            w.println(y + " ");
            y++;
        }
    }
      */
 /*
   int n = sc.ni();
   int k = sc.ni();
   Integer []a = new Integer[n];
   for(int i=0;i<n;i++)
   {
       a[i] = sc.ni();
       a[i] = a[i]%k;
   }
   HashMap<Integer,Integer> hm = new HashMap<>();
   for(int i=0;i<n;i++)
   {
       if(!hm.containsKey(a[i]))
           hm.put(a[i], 1);
       else
           hm.put(a[i], hm.get(a[i]) + 1);
   }
  Arrays.sort(a);
  int z = 0;
  long res = 0;
  HashMap<Integer,Integer> v = new HashMap<>();
  for(int i=0;i<n;i++)
  {
      if(a[i] == 0)
          res++;
      else
      {
          if(a[i] == k-a[i] && !v.containsKey(a[i]))
          {
              res += (long) hm.get(a[i]) / 2;
              v.put(a[i],1);
          }
          else
          {
          if(hm.containsKey(a[i]) && hm.containsKey(k-a[i]))
          {
          int temp = a[i];
          
          if(!v.containsKey(a[i]) && !v.containsKey(k-a[i]))
          {
             res += (long) Math.min(hm.get(temp), hm.get(k-temp));
             v.put(temp,1);
             v.put(k-temp,1);
          }
          }
          }
      }
  }
  w.println(res);
*/
 /*
int []a = new int[4];
//G0
a[0] = 0;
a[1] = 1;
a[2] = 0;
a[3] = 1;
int []b = new int[4];
//g1
b[0] = 0;
b[1] = 1;
b[2] = 1;
b[3] = 0;
int [][]dp1 = new int[4][4];
int [][]dp2 = new int[4][4];
for(int i=0;i<4;i++)
{
    for(int j=0;j<4;j++)
    {
        dp1[i][j] = (a[i] | a[j]);
    }
   // w.println();
}
w.println();
for(int i=0;i<4;i++)
{
    for(int j=0;j<4;j++)
    {
        dp2[i][j] = (b[i] | b[j]);
    }
    //w.println();
}
int a1 = 0;
int b1 = 0;
int c1 = 0;
int d1 = 0;
for(int i=0;i<4;i++)
{
    for(int j=0;j<4;j++)
    {
        if(dp1[i][j] == 0 && dp2[i][j] == 0)
            a1++;
        else  if(dp1[i][j] == 1 && dp2[i][j] == 1)
            b1++;
        else  if(dp1[i][j] == 0 && dp2[i][j] == 1)
            c1++;
        else
            d1++;
       
    }
}
w.println(a1+ " " + b1 + " " + c1 +" "+ d1);

Scanner sc = new Scanner(System.in);
    int q = sc.nextInt();
    while(q-- > 0)
    {
        long x = sc.nextLong();
        long y = sc.nextLong();
        long a = (x - y)/2;  
        long a1 = 0, b1 = 0; 
        
        
        int lim     = 8; 
  
   
    int f = 0;
    
    for (int i=0; i<8*lim; i++)  
    {  
        long pi = (y & (1 << i));  
        long qi = (a & (1 << i));  
        if (pi == 0 && qi == 0)  
        {  
             
        }  
        else if (pi == 0 && qi > 0)  
        {  
            a1 = ((1 << i) | a1);  
            b1 = ((1 << i) | b1);  
        }  
        else if (pi > 0 && qi == 0)  
        {  
            a1 = ((1 << i) | a1);  
  
           
        }  
        else
        {  
            f = 1;  
            
        }  
    }  
    if(a1+b1 != x)
        f  =1;
  if(f==1)
            System.out.println("-1");
  else
  {
       if(a1 > b1)
       {
           long temp = a1;
           a1 = b1;
           b1 = temp;
       }
    System.out.println(a1 + " " + b1);
   
  }
  
  
    }
     
     */

   int t = sc.ni();
   while(t-- > 0)
   {
             
       String s = sc.rs();
       int n = s.length();
       char []c = s.toCharArray();
       int z1 = 0;
       int z2 = 0;
       int p1 = 0;
       int p2 = 0;
       int ans = 1000000000;
       for(int i=0;i<n;i++)
       {
           z2 = Math.min(z1,z2);
           if(c[i] == '0')
               z2++;
           if(c[i] == '1')
               
               z1++;
           
             p2 = Math.min(p1,p2);
           if(c[i] == '1')
               p2++;
           if(c[i] == '0')
               p1++;
           
           ans = Math.min(Math.min(z1,z2) , Math.min(p1,p2));
       }
      w.println(ans);
         
   }
        
         
       
       w.close();
       
        
    
    
} 
    
    static long nCrModPFermat(int n, int r, 
                                    long p) 
    { 
          
        // Base case 
        if (r == 0) 
            return 1; 
      
        // Fill factorial array so that we 
        // can find all factorial of r, n 
        // and n-r 
       
          
        
           
      
        return (fact[n]* modInverse(fact[r], p) % p * modInverse(fact[n-r], p) % p) % p; 
    } 
   static long []fact;
    static int []res;
    static int c = 0;
    static class Pair{
        int fir;
        int last;
        Pair(int fir,int last)
        {
            this.fir = fir;
            this.last = last;
        }
    }
    static class m1 implements Comparator<Pair>{
        public int compare(Pair a, Pair b)
        {
            int t1 = a.last - a.fir;
            int t2 = b.last - b.fir;
            if(t1 < t2)
                return 1;
            else if(t1 > t2)
                return -1;
            else
            {
                if(a.fir < b.fir)
                    return -1;
                else
                    return 1;
            }
        }
    }
   
    static long calc(long x)
    {
        double res = Math.sqrt(1 + 24*(double)x);
        res -= 1.0;
        res = res / (double) 6;
        return (long) res;
    }
   
 public static final BigInteger MOD = BigInteger.valueOf(998244353L);
    
    static BigInteger fact(int x)
    {
        BigInteger res = BigInteger.ONE;
        for(int i=2;i<=x;i++)
        {
            res = res.multiply(BigInteger.valueOf(i)).mod(MOD);
        }
        return res;
    }
   
    public static long calc(int x,int y,int z)
    {
        long res = ((long) (x-y) * (long) (x-y)) + ((long) (z-y) * (long) (z-y)) + ((long) (x-z) * (long) (x-z));
        return res;
    }
    
    public static int upperBound(ArrayList<Integer>arr, int length, int value) {
        int low = 0;
        int high = length;
        while (low < high) {
            final int mid = (low + high) / 2;
            if (value >= arr.get(mid)) {
                low = mid + 1;
            } else {
                high = mid;
            }
        }
        return low;
    }
    
     public static int lowerBound(ArrayList<Integer> arr, int length, int value) {
        int low = 0;
        int high = length;
        while (low < high) {
            final int mid = (low + high) / 2;
            //checks if the value is less than middle element of the array
            if (value <= arr.get(mid)) {
                high = mid;
            } else {
                low = mid + 1;
            }
        }
        return low;
    }
    static HashMap<Long,Integer> map;
  
          
    static class Coin{
        long a;
        long b;
        
        Coin(long a, long b)
        {
            this.a = a;
            this.b = b;
           
        }
    }
    static ArrayList<Long> fac;
    static HashSet<Long> hs;
    public static void primeFactors(long n) 
    { 
        // Print the number of 2s that divide n 
        while (n % 2 == 0) { 
            fac.add(2l);
            hs.add(2l);
            n /= 2; 
        } 
  
        // n must be odd at this point.  So we can 
        // skip one element (Note i = i +2) 
        for (long i = 3; i*i <= n; i += 2) { 
            // While i divides n, print i and divide n 
            while (n % i == 0) { 
                fac.add(i);
                hs.add(i);
                n /= i; 
            } 
        } 
  
        // This condition is to handle the case whien 
        // n is a prime number greater than 2 
        if (n > 2) 
            fac.add(n);
    } 
   static int smallestDivisor(int n)  
{  
    // if divisible by 2  
    if (n % 2 == 0)  
        return 2;  
  
    // iterate from 3 to sqrt(n)  
    for (int i = 3; i * i <= n; i += 2) {  
        if (n % i == 0)  
            return i;  
    }  
  
    return n;  
}  
 static boolean IsP(String s,int l,int r)
 {
      while(l <= r)
      {
          if(s.charAt(l) != s.charAt(r))
          {
              return false;
          }
         l++;
         r--;
      }
      return true;
 }
  
	

    
    
 static class Student{
    int id;
    int val;
     Student(int id,int val)
     {
      this.id = id;
      this.val = val;
     }
 }
   
   
   
 
     static int upperBound(ArrayList<Integer> a, int low, int high, int element){
    while(low < high){
        int middle = low + (high - low)/2;
        if(a.get(middle) >= element)
            high = middle;
        else 
            low = middle + 1;
    }
    return low;
}
   static long func(long t,long e,long h,long a, long b)
   {
       if(e*a >= t)
           return t/a;
       else
       {
           return e + Math.min(h,(t-e*a)/b);
       }
   }
         
      
   
    public static int countSetBits(int number){
        int count = 0;
        while(number>0){
            ++count;
            number &= number-1;
        }
        return count;
    }
    
     
      
 
    
   
     static long modexp(long x,long n,long M)
{
    long power = n;
    long result=1;
    x = x%M;
    while(power>0)
    {
        if(power % 2 ==1)
            result=(result * x)%M;
        x=(x*x)%M;
        power = power/2;
    }
    return result;
}
      static long modInverse(long A,long M)
{
    return modexp(A,M-2,M);
}
    static long gcd(long a,long b)
    {
        if(a==0)
            return b;
        else
            return gcd(b%a,a);
    }
    static class Temp{
        int a;
        int b;
        int c;
        int d;
      
        Temp(int a,int b,int c,int d)
        {
            this.a = a;
            this.b = b;
            this.c  =c;
            this.d = d;
            //this.d = d;
        }
    }
   
    
    
   static long sum1(int t1,int t2,int x,int []t)
   {
       int mid = (t2-t1+1)/2;
  if(t1==t2)
      return 0;
    
   else
   return sum1(t1,mid-1,x,t) + sum1(mid,t2,x,t);
    
     
   }
  
   
    static String replace(String s,int a,int n)
    {
        char []c = s.toCharArray();
        for(int i=1;i<n;i+=2)
        {
            int num = (int) (c[i] - 48);
            num += a;
            num%=10;
            c[i] = (char) (num+48);
        }
        return new String(c);
    }
    static String move(String s,int h,int n)
    {
        h%=n;
        char []c = s.toCharArray();
        char []temp = new char[n];
        for(int i=0;i<n;i++)
        {
            temp[(i+h)%n] = c[i];
        }
        return new String(temp);
    }
    
     public static int ip(String s){
return Integer.parseInt(s);
}
     static class multipliers implements Comparator<Long>{
  
         
  public int compare(Long a,Long b) {
   if(a<b)
    return 1;
   else if(b<a)
    return -1;
   else
    return 0;
  }
 }
     
     static class multipliers1 implements Comparator<Coin>{
  
  public int compare(Coin a,Coin b) {
   if(a.a < b.a)
       return 1;
   else if(a.a > b.a)
       return -1;
   else{ 
       if(a.b < b.b)
       return 1;
   else if(a.b > b.b)
       return -1;
       else
       return 0;
   }
       
  
 
     }
     }
     
// Java program to generate power set in 
// lexicographic order. 
 
    
    
    
      static class InputReader {
 
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
 
public InputReader(InputStream st) {
this.stream = st;
}
 
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
 
public int ni() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
 
public long nl() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
 
public int[] nia(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
 
public String rs() {
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 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 boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
 
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
 
}
 
 
 
 
 
   
static PrintWriter w = new PrintWriter(System.out);
                  static class Student1
    {
        int id;
        //int x;
       
       int b;
      
        //long z;
        
        Student1(int id,int b)
        {
            this.id = id;
            //this.x = x;
            //this.s = s;
            this.b = b;
          
           // this.z = z;
            
            
            
           
        }
        
    }
}