#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
const long long N = 1e5 + 10;
const long double Pie = acos(-1);
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    string str;
    cin >> str;
    long long cnt_1 = 0, cnt_0 = 0;
    for (char ch : str) {
      if (ch == '0') {
        cnt_0++;
      } else {
        cnt_1++;
      }
    }
    if (cnt_1 == 0 or cnt_0 == 0) {
      cout << 0 << "\n";
      continue;
    }
    long long ans = min(cnt_1, cnt_0);
    long long seen_1 = 0, seen_0 = 0;
    for (char ch : str) {
      if (ch == '0')
        seen_0++;
      else
        seen_1++;
      ans = min(ans, seen_0 + (cnt_1 - seen_1));
    }
    seen_1 = 0, seen_0 = 0;
    for (char ch : str) {
      if (ch == '0')
        seen_0++;
      else
        seen_1++;
      ans = min(ans, seen_1 + (cnt_0 - seen_0));
    }
    cout << ans << "\n";
  }
  return 0;
}
