#include <bits/stdc++.h>
using namespace std;
void solve() {
  string s;
  cin >> s;
  int nz = count(s.begin(), s.end(), '0');
  int no = (int)s.size() - nz;
  int ans = min(nz, no);
  int conv_z = 0, conv_o = 0;
  for (int i = 0; i < (int)s.size(); ++i) {
    conv_o += (s[i] == '0');
    nz -= (s[i] == '0');
    no -= (s[i] == '1');
    conv_z += (s[i] == '1');
    ans = min(ans, conv_z + nz);
    ans = min(ans, conv_o + no);
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
