#include <bits/stdc++.h>
using namespace std;
long long int a, b, c, i, j, k, l, m, n, p, q, r, x, y, z, ts, mn = 10e17,
                                                               mod = 10e8 + 7;
long long int ar[250002], br[250002], xr[250002];
int main() {
  {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  };
  cin >> ts;
  while (ts--) {
    string s;
    cin >> s;
    l = s.size();
    long long int one[l + 2], zero[l + 2];
    memset(one, 0, sizeof(one));
    memset(zero, 0, sizeof(zero));
    c = 0;
    for (long long int i = 0; i <= l - 1; i++) {
      if (s[i] == '1')
        one[i + 1] = 1, c++;
      else
        zero[i + 1] = 1;
      one[i + 1] += one[i];
      zero[i + 1] += zero[i];
    }
    m = min(c, l - c);
    c = 0;
    for (long long int i = 1; i <= l; i++) {
      c = zero[i] + one[l] - one[i];
      k = one[i] + zero[l] - zero[i];
      m = min(m, min(c, k));
    }
    cout << m;
    cout << endl;
  }
  return 0;
  return 0;
  if (ts == 1) cout << "WTF";
  return 0;
}
