#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long fastexp(long long x, long long n) {
  long long result = 1;
  while (n > 0) {
    if (n % 2 == 1) result *= x;
    x *= x;
    n = n / 2;
  }
  return result;
}
long long modularExp(long long x, long long n, long long mod) {
  long long result = 1;
  while (n > 0) {
    if (n % 2 == 1) result = (result * x) % mod;
    x = ((x % mod) * (x % mod)) % mod;
    n = n / 2;
  }
  return (result) % mod;
}
bool isPrime(long long n) {
  for (long long i = 2; i * i <= n; i++)
    if (n % i == 0) return false;
  return true;
}
bool mycomp(pair<long long, long long> &a, pair<long long, long long> &b) {
  if (a.first != b.first)
    return a.first < b.first;
  else
    return a.second < b.second;
}
void solve() {
  string s;
  cin >> s;
  long long n = s.length();
  long long i;
  vector<long long> pre1(n), pre0(n), suff1(n), suff0(n);
  if (s[0] == '0')
    pre0[0] = 1;
  else
    pre1[0] = 1;
  for (i = 1; i < n; i++) {
    if (s[i] == '1') {
      pre1[i] = 1 + pre1[i - 1];
      pre0[i] = pre0[i - 1];
    } else {
      pre1[i] = pre1[i - 1];
      pre0[i] = 1 + pre0[i - 1];
    }
  }
  if (s[n - 1] == '0')
    suff0[n - 1] = 1;
  else
    suff1[n - 1] = 1;
  for (i = n - 2; i >= 0; i--) {
    if (s[i] == '1') {
      suff1[i] = 1 + suff1[i + 1];
      suff0[i] = suff0[i + 1];
    } else {
      suff1[i] = suff1[i + 1];
      suff0[i] = 1 + suff0[i + 1];
    }
  }
  long long ans = mod;
  for (i = 0; i < n - 1; i++) {
    long long x = pre1[i] + suff0[i + 1];
    long long y = pre0[i] + suff1[i + 1];
    ans = min(ans, x);
    ans = min(ans, y);
  }
  ans = min(ans, pre1[n - 1]);
  ans = min(ans, pre0[n - 1]);
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) solve();
}
