#include <bits/stdc++.h>
using namespace std;
const long double PI = atan(1.0) * 4;
const int64_t INF = 3e18;
const int32_t LOG = 21;
const long long int MOD = 998244353;
int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  long long int tests = 1;
  cin >> tests;
  for (long long int ii = 0; ii < tests; ii++) {
    string second;
    cin >> second;
    long long int zeros = 0, ones = 0;
    for (long long int i = 0; i < second.size(); i++)
      if (second[i] == '1')
        ones++;
      else
        zeros++;
    long long int cur_zeros = 0, cur_ones = 0, rem_zeros = zeros,
                  rem_ones = ones;
    long long int ans = second.size();
    for (long long int i = 0; i < second.size(); i++) {
      if (second[i] == '0') {
        cur_zeros++;
        rem_zeros--;
      } else {
        cur_ones++;
        rem_ones--;
      }
      ans = min(ans, cur_ones + rem_zeros);
      ans = min(ans, cur_zeros + rem_ones);
    }
    cout << ans << '\n';
  }
  return 0;
}
