#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
long long google_itr = 1;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
long long popcount(long long num) {
  num = num - ((num >> 1) & 0x55555555);
  num = (num & 0x33333333) + ((num >> 2) & 0x33333333);
  return ((num + (num >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
void virus() {
  string seq;
  cin >> seq;
  long long tot_1 = 0, tot_0 = 0;
  for (auto i : seq) {
    tot_0 += (i == '0');
    tot_1 += (i == '1');
  }
  long long till_1 = 0, till_0 = 0;
  long long min_m = min(tot_0, tot_1);
  for (auto i : seq) {
    till_0 += (i == '0');
    till_1 += (i == '1');
    tot_0 -= (i == '0');
    tot_1 -= (i == '1');
    min_m = min(min_m, till_0 + tot_1);
    min_m = min(min_m, till_1 + tot_0);
  }
  cout << min_m << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    auto start = high_resolution_clock::now();
    virus();
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<seconds>(stop - start);
  }
  return 0;
}
