#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
void solve() {
  string s;
  cin >> s;
  int n = s.size();
  int ones = count(s.begin(), s.end(), '1');
  int zeros = n - ones;
  int cur_ones = 0, cur_zeros = 0, ans = inf;
  for (int i = 0; i < n; i++) {
    ans = min(ans, n + cur_zeros - cur_ones - zeros);
    ans = min(ans, n + cur_ones - cur_zeros - ones);
    cur_ones += s[i] - '0';
    cur_zeros += 1 - s[i] + '0';
  }
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
}
