#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
float out(long long mid, long long h, long long c) {
  float ans = 0;
  long long p = 0;
  if (mid % 2 == 1) {
    p = 1;
    mid--;
  }
  ans = (mid / 2.0) * (h + c);
  ans += p * h;
  ans /= (float)(mid + p);
  return ans;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    string second;
    cin >> second;
    int n = second.size();
    vector<int> zero(n, 0), one(n, 0);
    int z = 0, o = 0;
    for (int i = 0; i < n; i++) {
      if (second[i] == '1') {
        o++;
      } else {
        z++;
      }
      zero[i] = z;
      one[i] = o;
    }
    int ans = min(z, o);
    for (int i = 0; i < n; i++) {
      int z1 = zero[i];
      int o1 = o - one[i];
      ans = min(ans, one[i] + z - zero[i]);
      ans = min(ans, zero[i] + o - one[i]);
    }
    cout << ans << endl;
  }
}
