#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int a[maxn];
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int ans = s.size();
    int n = s.size();
    for (int i = 0; i < n; i++) {
      a[i + 1] = a[i] + (s[i] == '1');
    }
    int t = 0;
    ans = min(a[n], n - a[n]);
    for (int i = n - 1; ~i; i--) {
      t += (s[i] == '1');
      ans = min(ans, min(a[i] + n - i - t, i - a[i] + t));
    }
    cout << ans << '\n';
  }
}
