#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    if (s.size() < 3) {
      cout << 0 << '\n';
    } else {
      int n = s.size();
      vector<int> zero(s.size() + 1, 0), one(s.size() + 1, 0);
      for (int i = 1; i <= n; i++) {
        zero[i] += zero[i - 1];
        one[i] += one[i - 1];
        if (s[i - 1] == '1')
          one[i]++;
        else
          zero[i]++;
      }
      int ans = s.size();
      for (int i = 1; i <= n; i++) {
        ans = min(ans, zero[i] - zero[0] + one[n] - one[i]);
        ans = min(ans, zero[n] - zero[i] + one[i] - one[0]);
      }
      cout << ans << '\n';
    }
  }
}
