#include <bits/stdc++.h>
using namespace std;
int minimalFlips(string bits) {
  int n = bits.length();
  if (n == 1) return 0;
  bool flag = true;
  for (int i = 0; i < n - 1; i++)
    if (bits[i] != bits[i + 1]) flag = false;
  if (flag == true) return 0;
  int i, zeros = 0, ones = 0, ans;
  for (i = 0; i < n; i++) {
    if (bits[i] == '0')
      zeros++;
    else
      ones++;
  }
  ans = min(zeros, ones);
  int d_zeros = 0, d_ones = 0;
  for (char i : bits) {
    if (i == '0') {
      d_zeros++;
    } else {
      d_ones++;
    }
    ans = min(ans, d_zeros + ones - d_ones);
    ans = min(ans, d_ones + zeros - d_zeros);
  }
  return ans;
}
int main() {
  int t;
  cin >> t;
  string s;
  while (t--) {
    cin >> s;
    cout << minimalFlips(s) << endl;
  }
  return 0;
}
