#include <bits/stdc++.h>
using namespace std;
long long int recpow(long long int x, long long int n, long long int M) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return recpow((x * x) % M, n / 2, M);
  else
    return (x * recpow((x * x) % M, (n - 1) / 2, M)) % M;
}
bool sortbysec(
    const pair<pair<long long int, long long int>, long long int> &a,
    const pair<pair<long long int, long long int>, long long int> &b) {
  if (a.second == b.second)
    return (a.first.second < b.first.second);
  else
    return (a.second > b.second);
}
string rev(string s) {
  string t;
  for (long long int i = s.length() - 1; i >= 0; i--) {
    t.push_back(s[i]);
  }
  return t;
}
bool comp(pair<long long int, long long int> a,
          pair<long long int, long long int> b) {
  if (a.first == b.first)
    return a.second < b.second;
  else
    return a.first < b.first;
}
long long int modInverse(long long int n, long long int p) {
  return recpow(n, p - 2, p);
}
long long int nCr(long long int n, long long int r, long long int p) {
  if (r == 0) return 1;
  long long int fac[n + 1];
  fac[0] = 1;
  for (long long int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p;
  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long int n = s.length();
    long long int a0[n + 2], a1[n + 2], ans;
    long long int o = 0, z = 0;
    for (long long int i = 0; i < n; i++) {
      if (s[i] == '0')
        z++;
      else
        o++;
    }
    a0[0] = 0, a1[0] = o;
    ans = min(o, z);
    for (long long int i = 0; i < n; i++) {
      if (s[i] == '0')
        a0[i + 1] = a0[i] + 1, a1[i + 1] = a1[i];
      else
        a1[i + 1] = a1[i] - 1, a0[i + 1] = a0[i];
      ans = min(ans, min(a0[i + 1] + a1[i + 1], o - a1[i + 1] + z - a0[i + 1]));
    }
    cout << ans << "\n";
  }
  return 0;
}
