#include <bits/stdc++.h>
using namespace std;
const int N = int(2e5) + 9;
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int n = s.size();
    int m1 = 0, m2 = 0;
    bool on = false;
    int n1 = 0, n0 = 0, N1 = 0, N0 = 0;
    for (int i = 0; i < n; i++) {
      n0 += (s[i] == '0');
      n1 += (s[i] == '1');
    }
    int ans = n;
    for (int i = 0; i < n; i++) {
      N0 += (s[i] == '0');
      N1 += (s[i] == '1');
      n0 -= (s[i] == '0');
      n1 -= (s[i] == '1');
      ans = min(ans, N0 + n1);
      ans = min(ans, N1 + n0);
    }
    cout << ans << endl;
  }
  return 0;
}
