#include <bits/stdc++.h>
using namespace std;
long long mod_factorial[100005];
long long mod_inv_factorial[100005];
long long binpow(long long a, long long b) {
  long long res = 1LL;
  while (b) {
    if (b & 1) {
      res = (res * a);
    }
    a = (a * a);
    b >>= 1;
  }
  return res;
}
long long binpowmod(long long a, long long b, long long p) {
  long long res = 1LL;
  while (b) {
    if (b & 1) {
      res = (res * 1LL * a) % 1000000007;
    }
    a = (a * 1LL * a) % 1000000007;
    b >>= 1;
  }
  return res;
}
long long mod_inverse(long long a) {
  return binpowmod(a, 1000000007 - 2, 1000000007);
}
void precomputefact() {
  mod_factorial[0] = 1;
  for (long long i = 1; i <= 1e5; i++) {
    mod_factorial[i] = (i * 1LL * mod_factorial[i - 1]) % 1000000007;
  }
  mod_inv_factorial[0] = 1;
  for (long long i = 1; i <= 1e5; i++) {
    mod_inv_factorial[i] =
        ((mod_inv_factorial[i - 1]) * 1LL * mod_inverse(i)) % 1000000007;
  }
}
long long modbinomial(long long n, long long k, long long p) {
  long long ans = mod_factorial[n] * mod_inv_factorial[n - k] % 1000000007 *
                  mod_inv_factorial[k] % 1000000007;
  return ans;
}
void solve() {
  string s;
  cin >> s;
  int n = s.size();
  int g1 = 0, g2 = 0;
  for (int i = 0; i < n; i++) {
    int j = i + 1;
    if (s[i] == '1') {
      g1++;
    } else {
      g2++;
    }
    while (j < n && s[i] == s[j]) {
      j++;
    }
    i = j - 1;
  }
  if (g1 <= 1 && g2 <= 1) {
    cout << 0 << "\n";
    return;
  }
  int co1 = 0, co2 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      co1++;
    } else {
      co2++;
    }
  }
  int c1 = 0, c2 = 0;
  int ans = INT_MAX;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      c1++;
    } else {
      c2++;
    }
    int val1 = co1 - c1 + c2;
    int val2 = co2 - c2 + c1;
    ans = min({ans, val1, val2});
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long testcase;
  cin >> testcase;
  while (testcase--) {
    solve();
  }
  return 0;
}
