#include <bits/stdc++.h>
using namespace std;
void solve() {
  string s;
  cin >> s;
  int n = s.length();
  int one = 0;
  int zero = 0;
  for (char c : s) {
    one += (c == '1');
    zero += (c == '0');
  }
  if (one == n || zero == n) {
    printf("0\n");
    return;
  }
  vector<int> ap(n), bp(n), as(n), bs(n);
  ap[0] = (s[0] != '1');
  bp[0] = (s[0] != '0');
  for (int i = 1; i < n; i++) {
    ap[i] = (s[i] != '1') + ap[i - 1];
    bp[i] = (s[i] != '0') + bp[i - 1];
  }
  as[n - 1] = (s[n - 1] != '1');
  bs[n - 1] = (s[n - 1] != '0');
  for (int i = n - 2; i >= 0; i--) {
    as[i] = (s[i] != '1') + as[i + 1];
    bs[i] = (s[i] != '0') + bs[i + 1];
  }
  int ans = bs[0];
  ans = min(ans, as[0]);
  for (int i = 0; i + 1 < n; i++) {
    int p0 = ap[i] + bs[i + 1];
    int p1 = bp[i] + as[i + 1];
    ans = min(ans, p0);
    ans = min(ans, p1);
  }
  printf("%d\n", ans);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    solve();
  }
  return 0;
}
