Shubham has a binary string s. A binary string is a string containing only characters "0" and "1".

He can perform the following operation on the string any amount of times: 

  * Select an index of the string, and flip the character at that index. This means, if the character was "0", it becomes "1", and vice versa. 



A string is called good if it does not contain "010" or "101" as a subsequence — for instance, "1001" contains "101" as a subsequence, hence it is not a good string, while "1000" doesn't contain neither "010" nor "101" as subsequences, so it is a good string.

What is the minimum number of operations he will have to perform, so that the string becomes good? It can be shown that with these operations we can make any string good.

A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.

Input

The first line of the input contains a single integer t (1≤ t ≤ 100) — the number of test cases.

Each of the next t lines contains a binary string s (1 ≤ |s| ≤ 1000).

Output

For every string, output the minimum number of operations required to make it good.

Example

Input


7
001
100
101
010
0
1
001100


Output


0
0
1
1
0
0
2

Note

In test cases 1, 2, 5, 6 no operations are required since they are already good strings.

For the 3rd test case: "001" can be achieved by flipping the first character — and is one of the possible ways to get a good string.

For the 4th test case: "000" can be achieved by flipping the second character — and is one of the possible ways to get a good string.

For the 7th test case: "000000" can be achieved by flipping the third and fourth characters — and is one of the possible ways to get a good string.
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (!a || !b) return a | b;
  unsigned shift = __builtin_ctz(a | b);
  a >>= __builtin_ctz(a);
  do {
    b >>= __builtin_ctz(b);
    if (a > b) swap(a, b);
    b -= a;
  } while (b);
  return a << shift;
}
int lcm(int a, int b) { return (long long)a * b / gcd(a, b); }
const int INF = 2 * ((1 << 30) - 1) + 1;
int lsb(int i) { return i & -i; }
template <class T>
bool umin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool umax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
int binarySearch(vector<int> arr, int p, int r, int num) {
  if (p <= r) {
    int mid = (p + r) / 2;
    if (arr[mid] == num) return mid;
    if (arr[mid] > num) return binarySearch(arr, p, mid - 1, num);
    if (arr[mid] > num) return binarySearch(arr, mid + 1, r, num);
  }
  return -1;
}
long long FIRSTTRUE(function<bool(long long)> f, long long lb, long long rb) {
  while (lb < rb) {
    long long mb = (lb + rb) / 2;
    f(mb) ? rb = mb : lb = mb + 1;
  }
  return lb;
}
long long LASTTRUE(function<bool(long long)> f, long long lb, long long rb) {
  while (lb < rb) {
    long long mb = (lb + rb + 1) / 2;
    f(mb) ? lb = mb : rb = mb - 1;
  }
  return lb;
}
template <class A>
void read(vector<A>& v);
template <class A, size_t S>
void read(array<A, S>& a);
template <class T>
void read(T& x) {
  cin >> x;
}
void read(double& d) {
  string t;
  read(t);
  d = stod(t);
}
void read(long double& d) {
  string t;
  read(t);
  d = stold(t);
}
template <class H, class... T>
void read(H& h, T&... t) {
  read(h);
  read(t...);
}
template <class A>
void read(vector<A>& x) {
  for (auto& a : x) read(a);
}
template <class A, size_t S>
void read(array<A, S>& x) {
  for (auto& a : x) read(a);
}
string to_string(char c) { return string(1, c); }
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(const char* s) { return string(s); }
string to_string(string s) { return s; }
string to_string(vector<bool> v) {
  string res;
  for (int i = (0); (1) > 0 ? i < ((int)(v).size()) : i > ((int)(v).size());
       i += (1))
    res += char('0' + v[i]);
  return res;
}
template <size_t S>
string to_string(bitset<S> b) {
  string res;
  for (int i = (0); (1) > 0 ? i < (S) : i > (S); i += (1))
    res += char('0' + b[i]);
  return res;
}
template <class T>
string to_string(T v) {
  bool f = 1;
  string res;
  for (auto& x : v) {
    if (!f) res += ' ';
    f = 0;
    res += to_string(x);
  }
  return res;
}
template <class A>
void write(A x) {
  cout << to_string(x);
}
template <class H, class... T>
void write(const H& h, const T&... t) {
  write(h);
  write(t...);
}
void print() { write("\n"); }
template <class H, class... T>
void print(const H& h, const T&... t) {
  write(h);
  if (sizeof...(t)) write(' ');
  print(t...);
}
void DBG() { cerr << "]" << endl; }
template <class H, class... T>
void DBG(H h, T... t) {
  cerr << to_string(h);
  if (sizeof...(t)) cerr << ", ";
  DBG(t...);
}
template <class T>
void offset(long long o, T& x) {
  x += o;
}
template <class T>
void offset(long long o, vector<T>& x) {
  for (auto& a : x) offset(o, a);
}
template <class T, size_t S>
void offset(long long o, array<T, S>& x) {
  for (auto& a : x) offset(o, a);
}
mt19937 mt_rng(chrono::steady_clock::now().time_since_epoch().count());
long long randint(long long a, long long b) {
  return uniform_int_distribution<long long>(a, b)(mt_rng);
}
template <class T, class U>
void vti(vector<T>& v, U x, size_t n) {
  v = vector<T>(n, x);
}
template <class T, class U>
void vti(vector<T>& v, U x, size_t n, size_t m...) {
  v = vector<T>(n);
  for (auto& a : v) vti(a, x, m);
}
const int d4i[4] = {-1, 0, 1, 0}, d4j[4] = {0, 1, 0, -1};
const int d8i[8] = {-1, -1, 0, 1, 1, 1, 0, -1},
          d8j[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int mxN = 5e5 + 1, M = 1e9 + 7;
int n, m, p[mxN];
int find(int x) { return x ^ p[x] ? p[x] = find(p[x]) : x; }
bool join(int x, int y) {
  if ((x = find(x)) == (y = find(y))) return 0;
  p[x] = y;
  return 1;
}
vector<int> ans;
vector<vector<int>> adjlist;
queue<int> nodelist;
vector<bool> visited;
vector<vector<int>> weightlist;
void bfs(int start) {
  ans[start] = 0;
  nodelist.push(start);
  while (!nodelist.empty()) {
    int node = nodelist.front();
    visited[node] = true;
    for (int e : adjlist[node]) {
      if (!visited[e]) {
        nodelist.push(e);
        ans[e] = ans[node] + 1;
        visited[e] = true;
      }
    }
    nodelist.pop();
  }
}
int pmst(int start) {
  int n = adjlist.size();
  visited.resize(n);
  for (int i = (0); (1) > 0 ? i < (n) : i > (n); i += (1)) {
    visited[i] = false;
  }
  visited[start] = true;
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  for (int i = 0; i < adjlist[start].size(); i++) {
    pq.push(
        make_pair(weightlist[start][i], make_pair(start, adjlist[start][i])));
  }
  stack<int> nodes;
  int ans = 0;
  nodes.push(start);
  while (nodes.size() != n) {
    pair<int, pair<int, int>> trip = pq.top();
    pq.pop();
    int w = trip.first;
    int a = trip.second.first;
    int b = trip.second.second;
    if (visited[a] && visited[b]) {
      continue;
    }
    visited[b] = true;
    nodes.push(b);
    ans += w;
    for (int i = (0);
         (1) > 0 ? i < (adjlist[b].size()) : i > (adjlist[b].size());
         i += (1)) {
      if (!visited[adjlist[b][i]]) {
        pq.push(make_pair(weightlist[b][i], make_pair(b, adjlist[b][i])));
      }
    }
  }
  return ans;
}
void dfs(int node) {}
void update(vector<int> arr, int index, int val) {
  int n = arr.size();
  int x = index + (1 << lsb(index + 1));
  int og = arr[index];
  arr[index] = val;
  while (x < n) {
    arr[x] /= og;
    arr[x] *= val;
    x = x + (1 << lsb(x + 1));
  }
}
int query(vector<int> arr, int val) {
  int ans = 0;
  val++;
  while (val != 0) {
    ans *= arr[val - 1];
    val - (1 << lsb(val));
  }
  return ans;
}
string reverse(string s) {
  string str = "";
  for (int i = s.length() - 1; i >= 0; i--) {
    str += s.substr(i, 1);
  }
  return str;
}
string leximax(string a, string b) {
  if (a > b) {
    return a;
  }
  return b;
}
string leximin(string a, string b) {
  if (a > b) {
    return b;
  }
  return a;
}
void solve() {
  string str;
  read(str);
  int n = str.length();
  int count0 = 0;
  int count1 = 0;
  int onecounts[n + 1];
  onecounts[0] = 0;
  for (int i = 0; i < n; i++) {
    if (str.substr(i, 1) == "1") {
      count1++;
      onecounts[i + 1] = onecounts[i] + 1;
    } else {
      count0++;
      onecounts[i + 1] = onecounts[i];
    }
  }
  int ans1 = min(count0, count1);
  int newans = ans1;
  for (int i = 1; i <= n; i++) {
    newans =
        min(newans, onecounts[i] + (n - i) - (onecounts[n] - onecounts[i]));
  }
  for (int i = 1; i <= n; i++) {
    newans = min(newans, i - onecounts[i] + onecounts[n] - onecounts[i]);
  }
  write(newans, "\n");
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int tc;
  read(tc);
  for (int i = (0); (1) > 0 ? i < (tc) : i > (tc); i += (1)) {
    solve();
  }
}
