#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int n = s.length();
    if (n < 3) {
      cout << 0 << endl;
      continue;
    }
    if (n == 3) {
      if (s == "101" || s == "010")
        cout << 1 << endl;
      else
        cout << 0 << endl;
      continue;
    }
    long long int zero = 0, one = 0;
    for (long long int i = 0; i < n; i++) {
      if (s[i] == '1')
        one++;
      else
        zero++;
    }
    if (one == n || zero == n) {
      cout << 0 << endl;
      continue;
    }
    long long int done = 0, dzero = 0;
    long long int min1 = INT_MAX, min2 = INT_MAX;
    for (long long int i = 0; i < n; i++) {
      if (s[i] == '1')
        done++;
      else
        dzero++;
      min1 = min(min1, (dzero + one - done));
      min2 = min(min2, (done + zero - dzero));
    }
    cout << min(min1, min2) << endl;
  }
  return 0;
}
