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\le t \le 100)$ — the number of test cases.

Each of the next $t$ lines contains a binary string $s$ $(1 \le |s| \le 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 $3$rd 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 $4$th 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 $7$th 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.

Difficulty: MEDIUM
Tags: String algorithms

Implementation

Starter code:

Solutions:
for _ in [0] * int(input()):
	s = input()
	ans = [s.count('0'), s.count('1')]
	f = [0, 0]
	for i in s:
		f[int(i)] += 1
		ans.append(f[0] + ans[1] - f[1])
		ans.append(f[1] + ans[0] - f[0])
	print(min(ans))


from collections import defaultdict
import sys, math
f = None
try:
	f = open('q1.input', 'r')
except IOError:
	f = sys.stdin
if 'xrange' in dir(__builtins__):
	range = xrange

def print_case_iterable(case_num, iterable):
	print('Case #{}: {}'.format(case_num, ' '.join(map(str, iterable))))

def print_case_number(case_num, iterable):
	print('Case #{}: {}'.format(case_num, iterable))

def print_iterable(A):
	print(' '.join(A))

def read_int():
	return int(f.readline().strip())

def read_int_array():
	return [int(x) for x in f.readline().strip().split(' ')]

def rns():
	a = [x for x in f.readline().split(' ')]
	return (int(a[0]), a[1].strip())

def read_string():
	return list(f.readline().strip())

def bi(x):
	return bin(x)[2:]
from collections import deque
import math
NUMBER = 10 ** 9 + 7

def factorial(n):
	M = NUMBER
	f = 1
	for i in range(1, n + 1):
		f = f * i % M
	return f

def mult(a, b):
	return a * b % NUMBER

def minus(a, b):
	return (a - b) % NUMBER

def plus(a, b):
	return (a + b) % NUMBER

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, y, x) = egcd(b % a, a)
		return (g, x - b // a * y, y)

def modinv(a):
	m = NUMBER
	(g, x, y) = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m

def choose(n, k):
	if n < k:
		assert false
	return mult(factorial(n), modinv(mult(factorial(k), factorial(n - k))))
from collections import deque, defaultdict
import heapq

def solution(s):
	zeros = [0]
	ones = [0]
	total = 0
	max_curr = 0
	for x in s:
		zeros.append(zeros[-1] + (1 if x == '0' else 0))
		ones.append(ones[-1] + (1 if x == '1' else 0))
	res = 10 ** 10
	n = len(s)
	for i in range(n + 1):
		res = min(res, ones[i] + zeros[n] - zeros[i])
		res = min(res, zeros[i] + ones[n] - ones[i])
	return res

def main():
	T = read_int()
	for i in range(T):
		s = read_string()
		x = solution(s)
		if 'xrange' not in dir(__builtins__):
			print(x)
		else:
			(print >> output, str(x))
	if 'xrange' in dir(__builtins__):
		print(output.getvalue())
		output.close()
if 'xrange' in dir(__builtins__):
	import cStringIO
	output = cStringIO.StringIO()
main()


q = int(input())
for query in range(q):
	s = input()
	n = len(s)
	MinSoFar = n
	zeri_first = 0
	zeri_second = 0
	for i in range(n):
		if s[i] == '0':
			zeri_second += 1
	for sep in range(n):
		if s[sep] == '0':
			zeri_first += 1
			zeri_second -= 1
		l_first = sep + 1
		l_second = n - l_first
		MinSoFar = min(MinSoFar, zeri_first + l_second - zeri_second, l_first - zeri_first + zeri_second)
	print(MinSoFar)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	s = input().rstrip()
	n = len(s)
	(a, b) = ([0] * n, [0] * n)
	a[0] = int(s[0])
	b[0] = 1 - a[0]
	for x in range(1, n):
		a[x] = a[x - 1]
		b[x] = b[x - 1]
		if s[x] == '1':
			a[x] += 1
		else:
			b[x] += 1
	now = min(a[-1], b[-1])
	for x in range(n - 1):
		now = min(now, a[x] + b[-1] - b[x], b[x] + a[-1] - a[x])
	print(now)


def calc(S):
	N = len(S)
	A = [0] * (N + 1)
	B = [0] * (N + 1)
	for i in range(N):
		A[i + 1] = A[i] + S[i]
	for i in range(N)[::-1]:
		B[i] = B[i + 1] + S[i]
	mi = 1 << 20
	for i in range(N + 1):
		mi = min(mi, A[i] + (N - i - B[i]), i - A[i] + B[i])
	return mi
T = int(input())
for _ in range(T):
	print(calc([int(a) for a in input()]))


t = int(input())
for _ in range(t):
	s = input()
	ans = 100000000005
	for i in range(len(s)):
		a = s[:i].count('1')
		b = s[i:].count('1')
		c = s[:i].count('0')
		d = s[i:].count('0')
		ans = min(ans, b + c, a + d)
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	flips = 0
	for bit in s:
		if bit == '0':
			flips += 1
	min_flips = flips
	for bit in s:
		flips += 1 if bit == '1' else -1
		if flips < min_flips:
			min_flips = flips
	flips = 0
	for bit in s:
		if bit == '1':
			flips += 1
	for bit in s:
		flips += 1 if bit == '0' else -1
		if flips < min_flips:
			min_flips = flips
	print(min_flips)


lines = int(input())
while lines > 0:
	lines -= 1
	s = input()
	(left_zero_to_one, left_one_to_zero, right_zero_to_one, right_one_to_zero) = ([0] * len(s), [0] * len(s), [0] * len(s), [0] * len(s))
	left_one_to_zero[0] = 1 if s[0] == '1' else 0
	left_zero_to_one[0] = 1 if s[0] == '0' else 0
	right_one_to_zero[-1] = 1 if s[-1] == '1' else 0
	right_zero_to_one[-1] = 1 if s[-1] == '0' else 0
	for i in range(1, len(s)):
		if s[i] == '0':
			left_zero_to_one[i] = left_zero_to_one[i - 1] + 1
			left_one_to_zero[i] = left_one_to_zero[i - 1]
		else:
			left_one_to_zero[i] = left_one_to_zero[i - 1] + 1
			left_zero_to_one[i] = left_zero_to_one[i - 1]
		if s[len(s) - i - 1] == '0':
			right_zero_to_one[len(s) - i - 1] = right_zero_to_one[len(s) - i] + 1
			right_one_to_zero[len(s) - i - 1] = right_one_to_zero[len(s) - i]
		else:
			right_one_to_zero[len(s) - i - 1] = right_one_to_zero[len(s) - i] + 1
			right_zero_to_one[len(s) - i - 1] = right_zero_to_one[len(s) - i]
	flip = min(left_zero_to_one[-1], left_one_to_zero[-1])
	for i in range(len(left_one_to_zero) - 1):
		flip = min(flip, left_one_to_zero[i] + right_zero_to_one[i + 1])
		flip = min(flip, left_zero_to_one[i] + right_one_to_zero[i + 1])
	print(flip)


import sys
from math import inf
input = sys.stdin.readline

def solve():
	s = input()
	n = len(s)
	if len(set(s)) == 1:
		print(0)
		return
	zeros_pref = [0] * n
	ones_pref = [0] * n
	zeros_suff = [0] * n
	ones_suff = [0] * n
	zeros_pref[0] = int(s[0] == '0')
	ones_pref[0] = int(s[0] == '1')
	zeros_suff[-1] = int(s[-1] == '0')
	ones_suff[-1] = int(s[-1] == '1')
	for i in range(1, n):
		zeros_pref[i] = zeros_pref[i - 1] + int(s[i] == '0')
		ones_pref[i] = ones_pref[i - 1] + int(s[i] == '1')
	for i in range(n - 2, -1, -1):
		zeros_suff[i] = zeros_suff[i + 1] + int(s[i] == '0')
		ones_suff[i] = ones_suff[i + 1] + int(s[i] == '1')
	(ans1, ans2) = (n, n)
	for i in range(n):
		curr_cost1 = ones_pref[i] + zeros_suff[min(n - 1, i + 1)]
		ans1 = min(ans1, curr_cost1)
		curr_cost2 = zeros_pref[i] + ones_suff[min(n - 1, i + 1)]
		ans2 = min(ans2, curr_cost2)
	print(min(ans1, ans2))

def main():
	for _ in range(int(input())):
		solve()
main()


for i in range(int(input())):
	n = input()
	l = len(n)
	c1 = []
	c2 = []
	o1 = 0
	o2 = 0
	for j in range(1, l + 1):
		if n[j - 1] == '1':
			o1 += 1
			c1.append(o1)
			c2.append(o2)
		else:
			o2 += 1
			c2.append(o2)
			c1.append(o1)
	r = 0
	li = []
	o11 = 0
	o20 = 0
	for k in range(l):
		if n[k] == '1':
			o11 += 1
		else:
			o20 += 1
		li.append(min(o20 + o1 - o11, o11 + o2 - o20))
	print(min(li))


T = int(input())
while T > 0:
	s = str(input())
	T -= 1
	c0 = 0
	c1 = 0
	cnt = 0
	if len(s) < 3:
		print('0')
		continue
	for ch in s:
		if ch == '0':
			c0 += 1
		else:
			c1 += 1
	if c0 == len(s) or c1 == len(s):
		print('0')
		continue
	cnt = 10 ** 10
	c0_ = 0
	c1_ = 0
	for i in range(len(s)):
		if s[i] == '0':
			c0_ += 1
		else:
			c1_ += 1
		cnt = min(c0_ + c1 - c1_, cnt)
		cnt = min(cnt, c1_ + c0 - c0_)
	print(cnt)


t = int(input())
for _ in range(t):
	s = list(input())
	s = [int(si) for si in s]
	cost0 = []
	cost1 = []
	c0 = 0
	c1 = 0
	for si in s:
		if si:
			c1 += 1
		else:
			c0 += 1
		cost0.append(c0)
		cost1.append(c1)
	l = len(s)
	ans = l * 2
	for i in range(l):
		ans = min(ans, cost0[i] + cost1[-1] - cost1[i])
		ans = min(ans, cost1[i] + cost0[-1] - cost0[i])
	print(ans)


kl = int(input())
for l in range(kl):
	s = input()
	n = len(s)
	ke = s.count('1')
	kn = n - ke
	kzpe = ke
	kzpn = kn
	mnpe = kzpe
	mnpn = kzpn
	for i in range(n):
		if s[i] == '1':
			kzpe -= 1
			kzpn += 1
		else:
			kzpe += 1
			kzpn -= 1
		mnpe = min(mnpe, kzpe)
		mnpn = min(mnpn, kzpn)
	print(min(mnpe, mnpn))


t = int(input())
for kek in range(t):
	s = list(map(int, input()))
	ans = sum(s)
	lol = sum(s)
	for i in range(len(s)):
		if s[i] == 1:
			lol -= 1
		else:
			lol += 1
		ans = min(ans, lol)
	lol = len(s) - sum(s)
	for i in range(len(s)):
		if s[i] == 0:
			lol -= 1
		else:
			lol += 1
		ans = min(ans, lol)
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	ans = s.count('1')
	costZ = ans
	costO = s.count('0')
	for x in s:
		if x == '1':
			costZ -= 1
			costO += 1
		else:
			costZ += 1
			costO -= 1
		ans = min(ans, min(costZ, costO))
	print(ans)


t = int(input())
for i in range(t):
	s = input()
	n = len(s)
	count_1 = [0 for j in range(n)]
	count_0 = [0 for j in range(n)]
	for j in range(len(s)):
		if s[j] == '0':
			count_0[j] = count_0[j - 1] + 1
		else:
			count_0[j] = count_0[j - 1]
		if s[j] == '1':
			count_1[j] = count_1[j - 1] + 1
		else:
			count_1[j] = count_1[j - 1]
	z1 = float('inf')
	z2 = float('inf')
	z3 = float('inf')
	z4 = float('inf')
	if count_0[0] == 1 and count_0[n - 1] == 1 or (count_0[n - 1] == 1 and s[n - 1] == '0'):
		print(0)
		continue
	elif count_1[0] == 1 and count_1[n - 1] == 1 or (count_1[n - 1] == 1 and s[n - 1] == '1'):
		print(0)
		continue
	elif count_1[n - 1] == n or count_0[n - 1] == n:
		print(0)
		continue
	else:
		for j in range(n):
			if j == 0:
				z1 = min(z1, count_0[n - j - 1])
				z2 = min(z2, count_1[n - j - 1])
			z1 = min(z1, count_0[n - 1] - count_0[j] + count_1[j])
			z2 = min(z2, count_1[n - 1] - count_1[j] + count_0[j])
	print(min(z1, z2))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(s, ans) = (input().strip(), [])
	for i in range(len(s) + 1):
		ans.append(s[i:].count('0') + s[:i].count('1'))
		ans.append(s[i:].count('1') + s[:i].count('0'))
	print(min(ans))


def count(s):
	res = [[0, 0] for _ in range(len(s))]
	for (i, c) in enumerate(s):
		res[i][0] = res[i - 1][0] + (1 if c == '0' else 0)
		res[i][1] = res[i - 1][1] + (1 if c == '1' else 0)
	return res
for _ in range(int(input())):
	s = input()
	l = [[0, 0]] + count(s)
	r = count(s[::-1])[::-1] + [[0, 0]]
	print(min((min(x[0] + y[1], x[1] + y[0]) for (x, y) in zip(l, r))))


T = int(input())
D = []
for i in range(0, T):
	A = []
	C = []
	A = list(input())
	A = [int(x) for x in A]
	numzero = A.count(0)
	numone = len(A) - numzero
	C.append(numzero)
	C.append(numone)
	x = 0
	y = 0
	for j in range(0, len(A)):
		if A[j] == 0:
			x += 1
		else:
			y += 1
		C.append(x + numone - y)
		C.append(len(A) + y - x - numone)
	D.append(min(C))
for i in D:
	print(i)


import sys
I = sys.stdin.readline
ans = ''
for _ in range(int(I())):
	s = I().strip()
	if len(s) < 3:
		ans += '0\n'
	else:
		count_0 = s.count('0')
		count_1 = s.count('1')
		a = 0
		b = 0
		cnt = 100000
		for i in s:
			if i == '0':
				a += 1
			else:
				b += 1
			cnt = min(cnt, a + count_1 - b, b + count_0 - a)
		ans += str(cnt) + '\n'
print(ans)


def get_distance(binary_str):
	distance = [[binary_str[0], 1]]
	for i in range(1, len(binary_str)):
		if binary_str[i] == distance[-1][0]:
			distance[-1][1] += 1
		else:
			distance.append([binary_str[i], 1])
	return distance

def solution_headed(distance, bit):
	s = sum([el[1] for el in distance if el[0] == bit])
	solution = float('inf')
	prev_sum = 0
	for el in distance:
		if el[0] != bit:
			solution = min(solution, prev_sum + s)
			prev_sum += el[1]
		else:
			s -= el[1]
	return min(solution, sum([el[1] for el in distance if el[0] != bit]))

def solution(binary_str):
	distance = get_distance(binary_str)
	if len(distance) <= 2:
		return 0
	zero_headed = solution_headed(distance, '0')
	one_headed = solution_headed(distance, '1')
	return min(zero_headed, one_headed)
results = []
for _ in range(int(input())):
	binary_str = input()
	results.append(str(solution(binary_str)))
print('\n'.join(results))


def inp():
	return map(int, input().split())

def solve():
	s = input()
	n = len(s)
	a = [[s[0], 1]]
	for i in range(1, n):
		if s[i] == a[-1][0]:
			a[-1][1] += 1
		else:
			a.append([s[i], 1])
	m = len(a)
	if m < 3:
		return 0
	p = [0] * m
	pp = [0] * m
	for i in range(m):
		if i == 0 or i == 1:
			p[i] = a[i][1]
		else:
			p[i] = p[i - 2] + a[i][1]
	for i in range(m - 1, -1, -1):
		if i == m - 1 or i == m - 2:
			pp[i] = a[i][1]
		else:
			pp[i] = pp[i + 2] + a[i][1]
	mm = min(pp[1], pp[2])
	for i in range(m - 2):
		x1 = p[i] + pp[i + 2]
		x2 = p[i] + (0 if i + 3 == m else pp[i + 3])
		xx = min(x1, x2)
		if xx < mm:
			mm = xx
	return mm

def main():
	t = int(input())
	while t:
		print(solve())
		t -= 1
main()


a = int(input())
for x in range(a):
	c = input()
	m = []
	for y in range(0, len(c)):
		f = 0
		f += c[:y].count('1')
		f += c[y:].count('0')
		j = 0
		j += c[:y].count('0')
		j += c[y:].count('1')
		m.append(f)
		m.append(j)
	print(min(m))


t = int(input())
while t > 0:
	str1 = list(input())
	count1 = str1.count('0')
	count2 = str1.count('1')
	if count1 >= count2:
		min1 = count1
	else:
		min1 = count2
	ones = 0
	zeroes = count1
	for i in range(len(str1)):
		if str1[i] == '0':
			zeroes -= 1
		else:
			ones += 1
		if zeroes + ones < min1:
			min1 = zeroes + ones
	ones = count2
	zeroes = 0
	for i in range(len(str1)):
		if str1[i] == '0':
			zeroes += 1
		else:
			ones -= 1
		if zeroes + ones < min1:
			min1 = zeroes + ones
	print(min(count1, count2, min1))
	t -= 1


t = int(input())
ans = 0
zero = 0
one = 0
group = 0
for i in range(t):
	A = str(input())
	zero = A.count('0')
	one = A.count('1')
	ans = min(zero, one)
	for j in range(len(A)):
		group = min(A[:j + 1].count('0') + A[j + 1:].count('1'), A[:j + 1].count('1') + A[j + 1:].count('0'))
		if ans > group:
			ans = group
	print(ans)


t = int(input())
for i in range(t):
	s = input()
	L = []
	n = len(s)
	for i in range(n):
		z1 = 0
		d0 = 0
		z0 = 0
		d1 = 0
		for j in range(i):
			if s[j] == '1':
				z0 = z0 + 1
			else:
				z1 = z1 + 1
		for j in range(i, n):
			if s[j] == '1':
				d0 = d0 + 1
			else:
				d1 = d1 + 1
		L.append(min(z1 + d0, z0 + d1))
	print(min(L))


import sys, re, math
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from heapq import heappush, heappop, heapify, nlargest, nsmallest

def STR():
	return list(input())

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST():
	return list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def sortListWithIndex(listOfTuples, idx):
	return sorted(listOfTuples, key=lambda x: x[idx])

def sortDictWithVal(passedDic):
	temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))[::-1]
	toret = {}
	for tup in temp:
		toret[tup[0]] = tup[1]
	return toret

def sortDictWithKey(passedDic):
	return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def compare(s1, s2):
	ans = 0
	for i in range(n):
		if s1[i] != s2[i]:
			ans += 1
	return ans
t = INT()
while t != 0:
	s = input()
	mn = INF
	temp = 0
	n = len(s)
	zeros = [0] * n
	ones = [0] * n
	for (i, ch) in enumerate(s):
		if ch == '0':
			zeros[i] = zeros[i - 1] + 1
			ones[i] = ones[i - 1]
		else:
			zeros[i] = zeros[i - 1]
			ones[i] = ones[i - 1] + 1
	for i in range(n):
		st = '1' * i + '0' * (n - i)
		if i == 0:
			temp = ones[n - 1]
		else:
			temp = zeros[i - 1] + ones[n - 1] - ones[max(0, i - 1)]
		mn = min(mn, temp)
	for i in range(n):
		st1 = '0' * i + '1' * (n - i)
		if i == 0:
			temp = zeros[n - 1]
		else:
			temp = ones[i - 1] + zeros[n - 1] - zeros[max(0, i - 1)]
		mn = min(mn, temp)
	print(mn)
	t -= 1


for _ in range(int(input())):
	m = input()
	c_1 = 0
	c_0 = 0
	pre_1 = [0] * (len(m) + 1)
	pre_0 = [0] * (len(m) + 1)
	for i in range(len(m)):
		if m[i] == '1':
			c_1 += 1
			pre_1[i + 1] = c_1
			pre_0[i + 1] = c_0
		else:
			c_0 += 1
			pre_1[i + 1] = c_1
			pre_0[i + 1] = c_0
	ans = min(c_0, c_1)
	for i in range(1, len(m)):
		ans = min(ans, pre_1[i] + c_0 - pre_0[i])
		ans = min(ans, pre_0[i] + c_1 - pre_1[i])
	print(ans)


for pratyush in range(int(input())):
	(s, z, o, h) = (str(input()), 0, 0, float('inf'))
	(a, b) = (s.count('0'), s.count('1'))
	for i in range(len(s)):
		w = z + (b - o)
		f = o + (a - z)
		h = min(h, w)
		h = min(h, f)
		if s[i] == '0':
			z += 1
		else:
			o += 1
	print(h)


for _ in range(int(input())):
	l = [int(j) for j in input()]
	n = len(l)
	c = 0
	mi = 10 ** 7
	(n1, n2) = ([], [])
	(o, z) = (0, 0)
	for e in l:
		if e:
			o += 1
		n1.append(o)
	for e in l[::-1]:
		if not e:
			z += 1
		n2.append(z)
	n2 = n2[::-1]
	for i in range(n):
		c = n1[i]
		if i + 1 < n:
			c += n2[i + 1]
		mi = min(mi, c)
	(m1, m2) = ([], [])
	(o, z) = (0, 0)
	for e in l:
		if not e:
			o += 1
		m1.append(o)
	for e in l[::-1]:
		if e:
			z += 1
		m2.append(z)
	m2 = m2[::-1]
	for i in range(n):
		c = m1[i]
		if i + 1 < n:
			c += m2[i + 1]
		mi = min(mi, c)
	print(mi)


for _ in range(int(input())):
	s = input()
	o = s.count('1')
	z = len(s) - o
	ones = 0
	zeros = 0
	ans = len(s)
	for i in range(len(s)):
		if s[i] == '1':
			ones += 1
		else:
			zeros += 1
		ans = min(ans, zeros + o - ones, ones + z - zeros)
	print(ans)


for _ in range(int(input())):
	s = str(input())
	one = s.count('1')
	zero = s.count('0')
	ans = min(one, zero)
	(a, b) = (0, 0)
	for i in s:
		if i == '1':
			a += 1
		else:
			b += 1
		ans = min(a + zero - b, b + one - a, ans)
	print(ans)


el = int(input())
for k in range(el):
	st = list(input())
	one = 0
	zero = 0
	for i in st:
		if i == '1':
			one = one + 1
		else:
			zero = zero + 1
	res = min(zero, one)
	curr0 = 0
	curr1 = 0
	for i in st:
		if i == '0':
			res = min(res, curr0 + (one - curr1))
			res = min(res, curr1 + (zero - curr0))
			curr0 = curr0 + 1
		else:
			res = min(res, curr0 + (one - curr1))
			res = min(res, curr1 + (zero - curr0))
			curr1 = curr1 + 1
	print(res)


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	t = int(input())
	for i in range(t):
		s = str(input())
		(prefix0, prefix1) = (0, 0)
		(suffix0, suffix1) = (0, 0)
		for j in range(len(s)):
			if s[j] == '0':
				suffix0 += 1
			else:
				suffix1 += 1
		ans = min(suffix0, suffix1)
		for j in range(len(s)):
			if s[j] == '0':
				suffix0 = suffix0 - 1
				prefix0 = prefix0 + 1
			else:
				suffix1 = suffix1 - 1
				prefix1 = prefix1 + 1
			ans = min(ans, prefix1 + suffix0)
			ans = min(ans, prefix0 + suffix1)
		print(ans)
main()


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	c = [[0, 0] for i in range(n)]
	countx = 0
	county = 0
	for (i, xy) in enumerate(zip(s, s[::-1])):
		(x, y) = xy
		c[i][0] = countx
		c[-i - 1][1] = county
		if x == '1':
			countx += 1
		if y == '1':
			county += 1
	ans = n
	for i in range(n):
		(a, b) = c[i]
		ans = min(ans, a + (n - 1 - i - b), i - a + b)
	print(ans)


import sys
from collections import deque
cases = int(input())
for case in range(cases):
	s = input()
	length = len(s)
	index = 1
	check = 0
	flag = True
	while index < length:
		if s[index - 1] != s[index]:
			check += 1
			if check == 2:
				flag = False
				break
		index += 1
	if flag is True:
		print(0)
		continue
	else:
		count = [0, 0]
		for char in s:
			if char == '1':
				count[1] += 1
			else:
				count[0] += 1
		nums_straight_0 = []
		nums_straight_1 = []
		nums_reverse_0 = []
		nums_reverse_1 = []
		if s[0] == '1':
			nums_straight_0 = [0]
			nums_straight_1 = [1]
		else:
			nums_straight_0 = [1]
			nums_straight_1 = [0]
		if s[-1] == '1':
			nums_reverse_0 = [0]
			nums_reverse_1 = [1]
		else:
			nums_reverse_0 = [1]
			nums_reverse_1 = [0]
		index_straight = 1
		while index_straight < len(s):
			if s[index_straight] == '1':
				nums_straight_0.append(nums_straight_0[-1])
				nums_straight_1.append(nums_straight_1[-1] + 1)
			else:
				nums_straight_0.append(nums_straight_0[-1] + 1)
				nums_straight_1.append(nums_straight_1[-1])
			index_straight += 1
		index_reverse = len(s) - 2
		while index_reverse >= 0:
			if s[index_reverse] == '1':
				nums_reverse_1.append(nums_reverse_1[-1] + 1)
				nums_reverse_0.append(nums_reverse_0[-1])
			else:
				nums_reverse_0.append(nums_reverse_0[-1] + 1)
				nums_reverse_1.append(nums_reverse_1[-1])
			index_reverse -= 1
		nums_reverse_0.reverse()
		nums_reverse_1.reverse()
		minimum = min(count[0], count[1])
		temp_index = 0
		sum_01 = sys.maxsize
		sum_10 = sys.maxsize
		while temp_index <= len(s) - 2:
			if s[temp_index] == '0' and s[temp_index + 1] == '1':
				sum_01 = nums_straight_1[temp_index] + nums_reverse_0[temp_index + 1]
			if s[temp_index] == '1' and s[temp_index + 1] == '0':
				sum_10 = nums_straight_0[temp_index] + nums_reverse_1[temp_index + 1]
			minimum = min(minimum, sum_01, sum_10)
			temp_index += 1
		print(minimum)


for i in range(int(input())):
	sequence = input()
	(no_of_ones, no_of_zeros) = (sequence.count('1'), sequence.count('0'))
	(ones, zeros) = (no_of_ones, no_of_zeros)
	no_of_changes = []
	for i in range(len(sequence) - 1, -1, -1):
		no_of_changes.append(min(no_of_ones + zeros - no_of_zeros, no_of_zeros + ones - no_of_ones))
		if sequence[i] == '1':
			no_of_ones -= 1
		else:
			no_of_zeros -= 1
	print(min(no_of_changes))


ipnl = lambda n: [int(input()) for _ in range(n)]
inp = lambda : int(input())
ip = lambda : [int(w) for w in input().split()]
for _ in range(inp()):
	s = input()
	n = len(s)
	c = s.count('0')
	if c == 0 or c == n:
		print(0)
		continue
	(l0, r0) = (s.index('0'), s.rfind('0'))
	(l1, r1) = (s.index('1'), s.rfind('1'))
	if r1 - l1 + 1 + r0 - l0 + 1 == n:
		print(0)
		continue
	d = n - c
	mn = min(d, c)
	(x, y) = (0, 0)
	for i in range(n - 1):
		if s[i] == '0':
			x += 1
		else:
			y += 1
		mn = min(mn, y + (c - x))
		mn = min(mn, x + (d - y))
	print(mn)


def checker(s):
	if len(s) < 3:
		return 0
	else:
		(zeros, ones) = (s.count('0'), s.count('1'))
		(cur_zeros, cur_ones) = (0, 0)
		result = min(zeros, ones)
		for elem in s:
			if elem == '1':
				cur_ones += 1
			else:
				cur_zeros += 1
			result = min(ones - cur_ones + cur_zeros, result)
			result = min(result, cur_ones + zeros - cur_zeros)
		return result
for _ in range(int(input())):
	print(checker(input()))


t = int(input())
for _ in range(t):
	a = [int(x) for x in input()]
	n = len(a)
	pref = [0 for i in range(n)]
	pref[0] = a[0]
	for i in range(1, n):
		pref[i] = pref[i - 1] + a[i]
	mini = n + 1
	for i in range(n):
		mini = min(mini, n - 1 - pref[n - 1] - i + 2 * pref[i], i - 2 * pref[i] + 1 + pref[n - 1])
	print(mini)


def subsequence_hate(n, s):
	c_0 = 0
	c_1 = 0
	for i in range(n):
		if s[i] == '0':
			c_0 += 1
		else:
			c_1 += 1
	ans = min(c_0, c_1)
	left_0 = 0
	left_1 = 0
	for i in range(n):
		if s[i] == '0':
			left_0 += 1
		else:
			left_1 += 1
		right_0 = c_0 - left_0
		right_1 = c_1 - left_1
		cost_1 = left_1 + right_0
		cost_2 = left_0 + right_1
		ans = min(ans, cost_1, cost_2)
	return ans
T = int(input())
for i in range(T):
	st = input()
	print(subsequence_hate(len(st), st))


N = int(input())
for i in range(N):
	s = input()
	Z = [0]
	O = [0]
	ans = 10 ** 9
	for j in range(1, len(s) + 1):
		if s[j - 1] == '0':
			Z.append(Z[-1] + 1)
			O.append(O[-1])
		else:
			Z.append(Z[-1])
			O.append(O[-1] + 1)
	for j in range(len(s) + 1):
		ans = min(ans, O[j] + Z[-1] - Z[j])
		ans = min(ans, Z[j] + O[-1] - O[j])
	print(ans)


t = int(input())
for test in range(t):
	s = input()
	n = len(s)
	zero = []
	one = []
	for i in range(n):
		if s[i] == '0':
			zero.append(i)
		else:
			one.append(i)
	m = len(one)
	n = len(zero)
	ans1 = s.count('1')
	ans2 = s.count('0')
	for i in range(m):
		ans1 = min(ans1, i + s[one[i]:].count('0'))
	for i in range(n):
		ans2 = min(ans2, i + s[zero[i]:].count('1'))
	print(min(ans1, ans2))


t = int(input())
for _ in range(t):
	a = [i for i in input()]
	mn = 1001
	o = [0 for i in range(len(a) + 1)]
	for i in range(1, len(o)):
		o[i] = o[i - 1] + int(a[i - 1])
	for i in range(len(a)):
		mn = min(mn, o[-1] - o[i] + i - o[i])
		mn = min(mn, o[i] + len(a) - i - (o[-1] - o[i]))
	print(mn)


import sys
from sys import setrecursionlimit
setrecursionlimit(10 ** 5)
readline = sys.stdin.readline

def process():
	a = readline().rstrip()
	(total_one, total_zero) = (0, 0)
	for i in a:
		if i == '0':
			total_zero += 1
		else:
			total_one += 1
	ans = float('+inf')
	(done_zero, done_one) = (0, 0)
	for i in a:
		if i == '0':
			done_zero += 1
		else:
			done_one += 1
		cost1 = done_zero + total_one - done_one
		cost2 = done_one + total_zero - done_zero
		ans = min(ans, cost1, cost2)
	print(ans)
for _ in range(int(readline())):
	process()


def solve():
	s = input()
	s = list(s)
	n = len(s)
	ans = 1000000
	for i in range(n):
		z = 0
		o = 0
		for j in range(n):
			if j >= i:
				if s[j] != '0':
					z += 1
				else:
					o += 1
			elif s[j] != '1':
				z += 1
			else:
				o += 1
		a = min(z, o)
		ans = min(ans, a)
	print(ans)
T = 1
T = int(input())
for _ in range(1, T + 1):
	solve()


we = int(input())
for erw in range(we):
	n = list(input())
	l = len(n)
	o = 0
	z = 0
	re = [0] * len(n)
	for i in range(l):
		if n[i] == '1':
			o += 1
		else:
			z += 1
		re[i] = [z, o]
	f = [0] * l
	mi = min(re[l - 1][1], re[l - 1][0])
	for i in range(l):
		f[i] = re[i][1] + re[l - 1][0] - re[i][0]
	s = [0] * l
	for i in range(l):
		s[i] = re[i][0] + re[l - 1][1] - re[i][1]
	ans = min(mi, min(min(s), min(f)))
	print(ans)


t = int(input(''))
while t > 0:
	answers = {}
	string = input('')
	n = len(string)
	number = int('1' * n)
	ans = n
	prefix_list = []
	count = 0
	for prefix in string:
		if prefix == '0':
			count += 1
		prefix_list.append(count)
	zeroes_count = count
	ones_count = n - count
	ans1 = zeroes_count
	ans2 = ones_count
	ans = min(zeroes_count, ones_count)
	if string in answers:
		print(answers[string])
	else:
		for y in range(1, n):
			change_count1 = prefix_list[y - 1] + (n - y - (zeroes_count - prefix_list[y - 1]))
			change_count2 = n - change_count1
			if min(change_count1, change_count2) < ans:
				ans = min(change_count1, change_count2)
		print(ans)
	answers[string] = ans
	t -= 1


from math import inf as inf
from math import *
from collections import *
import sys
input = sys.stdin.readline
t = int(input())
while t:
	t -= 1
	a = input().rstrip('\n')
	o = a.count('1')
	z = a.count('0')
	c = [[a[0], 1]]
	for i in range(1, len(a)):
		if a[i] == c[-1][0]:
			c[-1][1] += 1
			continue
		else:
			c.append([a[i], 1])
	if len(c) <= 2:
		print(0)
	else:
		if len(c) % 2:
			flag = 1
		else:
			flag = 0
		s = []
		s1 = []
		for i in range(len(c)):
			if i % 2:
				s.append(c[i][1])
			else:
				s1.append(c[i][1])
		v = sum(s1)
		v1 = sum(s)
		temp = 0
		temp1 = 0
		r = float('inf')
		if len(c) == 3:
			print(min(min(s), min(s1)))
			continue
		for i in range(len(c) - 1):
			if i % 2 == 0:
				r = min(r, v - c[i][1] + temp - temp1)
				temp1 += c[i][1]
			else:
				r = min(r, v1 - c[i][1] + temp1 - temp)
				temp += c[i][1]
		if len(c) % 2:
			r = min(r, sum(s1[:-1]))
		else:
			r = min(r, sum(s[:-1]))
		r = min(r, o, z)
		print(r)


for i in range(int(input())):
	s = input()
	min = len(s)
	for i in range(len(s)):
		lst1 = s[0:i]
		lst2 = s[i:]
		x = lst1.count('1') + lst2.count('0')
		y = lst1.count('0') + lst2.count('1')
		if x < y:
			k = x
		else:
			k = y
		if min > k:
			min = k
	print(min)


from sys import stdin, stdout

def find(arr):
	A0 = [0]
	A1 = [0]
	N = len(arr)
	for i in range(N):
		if arr[i] == 1:
			A0.append(A0[-1] + 1)
			A1.append(A1[-1])
		else:
			A0.append(A0[-1])
			A1.append(A1[-1] + 1)
	A00 = [0]
	A11 = [0]
	for i in range(N)[::-1]:
		if arr[i] == 1:
			A00.append(A00[-1] + 1)
			A11.append(A11[-1])
		else:
			A00.append(A00[-1])
			A11.append(A11[-1] + 1)
	A00.reverse()
	A11.reverse()
	res = float('inf')
	for i in range(N + 1):
		res = min([res, A0[i] + A11[i], A1[i] + A00[i]])
	return res
	print('0', A0)
	print('1', A11)
	print()
	print('1', A00)
	print('0', A1)

def main():
	for _ in range(int(stdin.readline())):
		S = list(map(int, stdin.readline().strip()))
		print(find(S))
main()


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	s = input().rstrip()
	cnt = s.count('0')
	ans = cnt
	for c in s:
		if c == '0':
			cnt -= 1
		else:
			cnt += 1
		ans = min(ans, cnt)
	cnt = s.count('1')
	for c in s:
		if c == '1':
			cnt -= 1
		else:
			cnt += 1
		ans = min(ans, cnt)
	print(ans)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def bs(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count
for t in range(int(input())):
	s = input()
	if len(s) < 3:
		print(0)
		continue
	zero = []
	one = []
	tt = 99999999999999999999
	for j in range(len(s)):
		if s[j] == '1':
			zero.append(j)
		else:
			one.append(j)
	for j in range(len(zero)):
		c = bs(one, len(one), zero[j])
		ans = c + len(zero) - 1 - j
		tt = min(tt, ans)
	for j in range(len(one)):
		c = bs(zero, len(zero), one[j])
		ans = c + len(one) - 1 - j
		tt = min(tt, ans)
	print(tt)


from typing import *

def minimum_number_of_operations(s: str) -> int:
	if len(s) == 1:
		return 0
	one_number = 0
	zero_number = 0
	for ch in s:
		if ch == '1':
			one_number += 1
		else:
			zero_number += 1
	flipped_ones = 0
	flipped_zeros = 0
	ans = min(zero_number, one_number)
	for ch in s:
		if ch == '1':
			flipped_ones += 1
		else:
			flipped_zeros += 1
		ans = min(ans, flipped_zeros + one_number - flipped_ones, flipped_ones + zero_number - flipped_zeros)
	return ans
t = int(input())
for _ in range(t):
	print(minimum_number_of_operations(input()))


import sys
input = sys.stdin.readline
import heapq
import copy
import math
import decimal

class Node:

	def _init_(self, val):
		self.data = val
		self.left = None
		self.right = None

class fen_tree:

	def __init__(self, list):
		self.array = [0] + list
		for idx in range(1, len(self.array)):
			idx2 = idx + (idx & -idx)
			if idx2 < len(self.array):
				self.array[idx2] += self.array[idx]

	def prefix_query(self, idx):
		result = 0
		while idx:
			result += self.array[idx]
			idx -= idx & -idx
		return result

	def prints(self):
		print(self.array)
		return

	def range_query(self, from_idx, to_idx):
		return self.prefix_query(to_idx) - self.prefix_query(from_idx - 1)

	def update(self, idx, add):
		while idx < len(self.array):
			self.array[idx] += add
			idx += idx & -idx

def pre_sum(arr):
	p = [0]
	for i in arr:
		p.append(p[-1] + i)
	p.pop(0)
	return p

def pre_back(arr):
	p = [0]
	for i in arr:
		p.append(p[-1] + i)
	p.pop(0)
	return p

def bin_search(arr, l, r, val):
	if arr[r] <= val:
		return r + 1
	if r - l < 2:
		if arr[l] > val:
			return l
		else:
			return r
	mid = int((l + r) / 2)
	if arr[mid] <= val:
		return bin_search(arr, mid, r, val)
	else:
		return bin_search(arr, l, mid, val)

def search_leftmost(arr, val):

	def helper(arr, l, r, val):
		print(l, r)
		if arr[l] == val:
			return l
		if r - l <= 1:
			if arr[r] == val:
				return r
			else:
				print('not found')
				return
		mid = int((r + l) / 2)
		if arr[mid] >= val:
			return helper(arr, l, mid, val)
		else:
			return helper(arr, mid, r, val)
	return helper(arr, 0, len(arr) - 1, val)

def search_rightmost(arr, val):

	def helper(arr, l, r, val):
		print(l, r)
		if arr[r] == val:
			return r
		if r - l <= 1:
			if arr[l] == val:
				return r
			else:
				print('not found')
				return
		mid = int((r + l) / 2)
		if arr[mid] > val:
			return helper(arr, l, mid, val)
		else:
			return helper(arr, mid, r, val)
	return helper(arr, 0, len(arr) - 1, val)

def preorder_postorder(root, paths, parent, left, level):
	l = 0
	queue = [root]
	while queue != []:
		node = queue[-1]
		flag = 0
		for i in paths[node]:
			if i != parent[node] and level[i] == sys.maxsize:
				parent[i] = node
				level[i] = level[node] + 1
				flag = 1
				queue.append(i)
		if flag == 0:
			left[parent[node]] += 1 + left[node]
			queue.pop()
	return [parent, left, level]

def nCr(n, r, p):
	if r == 0:
		return 1
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def pr_list(a):
	print(*a, sep=' ')

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	for test in range(tests):
		s = insr()
		r = [0]
		l = [0]
		n = len(s)
		for i in s:
			if i == '0':
				l.append(l[-1])
			else:
				l.append(l[-1] + 1)
		l.pop()
		for i in range(n - 1, -1, -1):
			if s[i] == '0':
				r.append(r[-1])
			else:
				r.append(r[-1] + 1)
		r.pop()
		r.reverse()
		ans = min(l[-1] + int(s[-1]), n - (l[-1] + int(s[-1])))
		for i in range(0, n):
			if i > 0 or i < n - 1:
				l0 = l[i]
				r1 = n - i - 1 - r[i]
				l1 = i - l[i]
				r0 = r[i]
				ans = min(ans, l0 + r1, l1 + r0)
			elif i == 0:
				ans = min(ans, r[0], n - r[0] - 1)
			else:
				ans = min(ans, l[0], n - l[0] - 1)
		print(ans)
main()


t = int(input())
for ii in range(t):
	s = input()
	o = 0
	z = 0
	do = 0
	dz = 0
	for i in range(len(s)):
		if s[i] == '1':
			o += 1
		else:
			z += 1
	m1 = len(s)
	m2 = len(s)
	for i in range(len(s)):
		if s[i] == '1':
			do += 1
		else:
			dz += 1
		m1 = min(m1, dz + o - do)
		m2 = min(m2, do + z - dz)
	print(min(m1, m2))


t = int(input())
for _ in range(t):
	s = input()
	(count_0_2, count_1_2) = (0, 0)
	for i in s:
		if i == '0':
			count_0_2 += 1
		else:
			count_1_2 += 1
	ans = len(s)
	(count_0_1, count_1_1) = (0, 0)
	for i in s:
		x = count_1_1 + count_0_2
		y = count_0_1 + count_1_2
		ans = min(ans, x, y)
		if i == '0':
			count_0_1 += 1
			count_0_2 -= 1
		else:
			count_1_1 += 1
			count_1_2 -= 1
	x = count_1_1 + count_0_2
	y = count_0_1 + count_1_2
	ans = min(ans, x, y)
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	s = input().rstrip()
	n = len(s)
	x = s.count('1')
	op1 = [n] * n
	cnt = 0
	ans = n
	for i in range(n):
		if s[i] == '1':
			cnt += 1
		op1[i] = i + 1 - cnt + x - cnt
		ans = min(ans, op1[i], n - op1[i])
	print(ans)


for _ in range(int(input())):
	str1 = list(input())
	_c1 = str1.count('1')
	_c0 = len(str1) - _c1
	l1 = []
	c1_ = 0
	c0_ = 0
	for i in range(len(str1)):
		if str1[i] == '1':
			c1_ += 1
			_c1 -= 1
			s1_0 = c0_ + _c1
			s0_1 = c1_ + _c0
			l1.append(min(s0_1, s1_0))
		else:
			c0_ += 1
			_c0 -= 1
			s1_0 = c0_ + _c1
			s0_1 = c1_ + _c0
			l1.append(min(s0_1, s1_0))
	print(min(l1))


for _ in range(int(input())):
	s = input()
	ans = 10 ** 6
	for i in range(len(s)):
		z = s[:i].count('0')
		o = s[:i].count('1')
		ones = s[i:].count('1')
		zeroes = s[i:].count('0')
		ans = min(ans, z + ones, o + zeroes)
	print(ans)


import sys
from bisect import *
from math import floor, sqrt, ceil, factorial as F, gcd, pi
from itertools import chain, combinations, permutations, accumulate
from collections import Counter, defaultdict, OrderedDict, deque
from array import array
INT_MAX = sys.maxsize
INT_MIN = -sys.maxsize - 1
mod = 1000000007
ch = 'abcdefghijklmnopqrstuvwxyz'
lcm = lambda a, b: a * b // gcd(a, b)
setbit = lambda x: bin(x)[2:].count('1')

def solve():
	T = int(sys.stdin.readline())
	for _ in range(T):
		s = str(sys.stdin.readline()).replace('\n', '')
		ans = INT_MAX
		for j in range(len(s)):
			a = s[:j].count('1')
			b = s[:j].count('0')
			c = s[j:].count('1')
			d = s[j:].count('0')
			ans = min(ans, b + c, a + d)
		print(ans)
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass
solve()


for _ in range(int(input())):
	s = input()
	count0 = 0
	count1 = 0
	ans = float('inf')
	pre = []
	pre.append((count0, count1))
	for i in s:
		if i == '0':
			count0 += 1
		else:
			count1 += 1
		pre.append((count0, count1))
	suf = []
	(num0, num1) = pre[-1]
	for i in pre:
		(count0, count1) = i
		suf.append((num0 - count0, num1 - count1))
	for i in range(len(suf)):
		ans = min(ans, min(pre[i]) + min(suf[i]))
	print(ans)


from collections import Counter
T = int(input().strip())
for t in range(T):
	a = list(map(int, input().strip()))
	hh = Counter(a)
	s = hh[1]
	ln = len(a)
	res = min(s, ln - s)
	cursum = 0
	for i in range(ln):
		cursum += a[i]
		res = min(res, i + 1 - cursum + (s - cursum), cursum + (ln - (i + 1)) - (s - cursum))
	print(res)


from math import *
for t in range(int(input())):
	s = input()
	n = len(s)
	mas = []
	for i in range(n):
		if s[i] == '1':
			mas.append(True)
		else:
			mas.append(False)
	if not True in mas or not False in mas or n < 3:
		print(0)
		continue
	pref1 = [0 for i in range(n)]
	if mas[0]:
		pref1[0] = 1
	for i in range(1, n):
		pref1[i] = pref1[i - 1]
		if mas[i]:
			pref1[i] += 1
	otv = min(pref1[-1], n - pref1[-1])
	for i in range(n):
		cntpr1 = pref1[i]
		cntpr0 = i + 1 - pref1[i]
		cntsf1 = pref1[-1] - cntpr1
		cntsf0 = n - pref1[-1] - cntpr0
		otv = min(otv, cntpr0 + cntsf1)
		otv = min(otv, cntpr1 + cntsf0)
	print(otv)


t = int(input())
for i in range(t):
	n = input()
	if len(n) <= 2:
		print(0)
	elif len(n) == 3:
		if n == '101' or n == '010':
			print(1)
		else:
			print(0)
	else:
		n = list(map(int, n))
		n = [1 if item == 1 else -1 for item in n]
		difference_dict = {}
		sum_n = sum(n)
		difference_dict[sum_n] = [-1, 0, sum_n]
		left = 0
		right = sum_n
		for idx in range(0, len(n)):
			left += n[idx]
			right -= n[idx]
			diff = abs(left) + abs(right)
			difference_dict[diff] = [idx, left, right]
		select_key = max(list(difference_dict.keys()))
		(idx, left, right) = difference_dict[select_key]
		res = (idx + 1 - abs(left)) / 2
		res += (len(n) - idx - 1 - abs(right)) // 2
		res = int(res)
		print(res)


test_cases = int(input())
for i in range(test_cases):
	bit_string = input()
	min = len(bit_string)
	for j in range(len(bit_string) + 1):
		left = bit_string[:j]
		right = bit_string[j:]
		right_len = len(bit_string) - j
		zeroes = left.count('0') + right.count('0')
		ones = left.count('1') + right.count('1')
		oz = left.count('1') + right.count('0')
		zo = left.count('0') + right.count('1')
		if len(bit_string) - max(zeroes, ones, oz, zo) < min:
			min = len(bit_string) - max(zeroes, ones, oz, zo)
	print(min)


t = int(input())
for _ in range(t):
	s = input()
	p = [(0, 0)]
	(a, b) = (0, 0)
	for x in s:
		if x == '0':
			a += 1
		else:
			b += 1
		p.append((a, b))
	n = len(s)
	ans = n
	for i in range(n + 1):
		(c, d) = (p[i][0], p[i][1])
		(e, f) = (p[n][0] - p[i][0], p[n][1] - p[i][1])
		a_c = min(max(i - c, 0) + max(0, n - i - f), max(i - d, 0) + max(n - i - e, 0))
		ans = min(ans, a_c)
	print(ans)


for _ in range(int(input())):
	l = list(map(int, input()))
	n = len(l)
	ans = l.count(1)
	ans = min(ans, l.count(0))
	a = l.count(1)
	b = l.count(0)
	c = d = 0
	for i in range(0, n):
		if l[i] == 1:
			c += 1
		else:
			d += 1
		ans = min(ans, d + a - c)
		ans = min(ans, c + b - d)
	print(ans)


for _ in range(int(input())):
	s = input()
	n = len(s)
	min1 = 10000000000000
	for i in range(n):
		z1 = n - s[:i].count('1') - s[i:].count('0')
		z2 = n - s[:i].count('0') - s[i:].count('1')
		min1 = min(min1, z1, z2)
	print(min1)


from sys import stdin, stdout
for tc in range(int(stdin.readline())):
	s = list(stdin.readline().strip())
	n = len(s)
	cnt1 = [0]
	for i in s:
		cnt1.append(cnt1[-1] + int(i))
	ans = len(s)
	for i in range(len(cnt1)):
		c1 = i - cnt1[i] + cnt1[-1] - cnt1[i]
		c2 = cnt1[i] + n - i - (cnt1[-1] - cnt1[i])
		ans = min(ans, c1, c2)
	print(ans)


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
from math import sqrt, floor, factorial, gcd, log
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations
from math import gcd
from bisect import bisect

def solution():
	t = int(input())
	for _ in range(t):
		a = input()
		o = a.count('1')
		z = a.count('0')
		ones = 0
		zeros = 0
		ans = len(a)
		for i in range(len(a)):
			if a[i] == '1':
				ones += 1
			else:
				zeros += 1
			ans = min(ans, zeros + o - ones, ones + z - zeros)
		print(ans)
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
read = lambda : list(map(int, input().strip().split(' ')))
solution()


for _ in range(int(input())):
	s = str(input())
	o = 0
	z = 0
	n = len(s)
	l1 = [0] * n
	l2 = [0] * n
	for i in range(n):
		if s[i] == '1':
			o += 1
			l1[i] = o
			l2[i] = z
		else:
			z += 1
			l1[i] = o
			l2[i] = z
	if z == 0 or o == 0:
		print(0)
	else:
		ans = 1000000000
		for i in range(1, n):
			ans = min(ans, l1[i - 1] + z - l2[i])
			ans = min(ans, l2[i - 1] + o - l1[i])
		print(ans)


t = int(input())
for _ in range(t):
	s = input()
	ans = 5000
	ans = min(ans, s.count('0'))
	ans = min(ans, s.count('1'))
	acc1 = [0]
	acc0 = [0]
	for i in range(len(s)):
		if s[i] == '1':
			acc0.append(acc0[-1])
			acc1.append(acc1[-1] + 1)
		else:
			acc0.append(acc0[-1] + 1)
			acc1.append(acc1[-1])
	for i in range(1, len(s)):
		ans = min(ans, acc0[i] + acc1[-1] - acc1[i])
		ans = min(ans, acc1[i] + acc0[-1] - acc0[i])
	print(ans)


from fractions import Fraction
import sys
import bisect
import heapq
from math import *
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
mod = 1000000007

def inp():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def graph(vertex):
	return [[] for i in range(0, vertex + 1)]

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(a, b):
	ans = 1
	while b > 0:
		if b % 2 == 1:
			ans *= a
		a *= a
		b //= 2
	return ans

def createMatrix(rowCount, colCount, dataList):
	mat = []
	for i in range(rowCount):
		rowList = []
		for j in range(colCount):
			if dataList[j] not in mat:
				rowList.append(dataList[j])
		mat.append(rowList)
	return mat

def isprime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def minimalFilps1(bits):
	n = len(bits)
	flipsFromLeft = [0 for i in range(n)]
	flipsFromRight = [0 for i in range(n)]
	flips = 0
	for i in range(0, n, 1):
		if bits[i] == '1':
			flips = flips + 1
		flipsFromLeft[i] = flips
	flips = 0
	i = n - 1
	while i >= 0:
		if bits[i] == '0':
			flips = flips + 1
		i = i - 1
		flipsFromRight[i] = flips
	minFlips1 = sys.maxsize
	for i in range(1, n, 1):
		if flipsFromLeft[i - 1] + flipsFromRight[i] < minFlips1:
			minFlips1 = flipsFromLeft[i - 1] + flipsFromRight[i]
	return minFlips1

def minimalFilps(bits):
	n = len(bits)
	flipsFromLeft = [0 for i in range(n)]
	flipsFromRight = [0 for i in range(n)]
	flips = 0
	for i in range(0, n, 1):
		if bits[i] == '0':
			flips = flips + 1
		flipsFromLeft[i] = flips
	flips = 0
	i = n - 1
	while i >= 0:
		if bits[i] == '1':
			flips = flips + 1
		i = i - 1
		flipsFromRight[i] = flips
	minFlips = sys.maxsize
	for i in range(1, n, 1):
		if flipsFromLeft[i - 1] + flipsFromRight[i] < minFlips:
			minFlips = flipsFromLeft[i - 1] + flipsFromRight[i]
	return minFlips
for i in range(int(input())):
	s = input()
	a = 0
	b = 0
	for i in range(len(s)):
		if s[i] == '1':
			a += 1
		else:
			b += 1
	e = min(a, b)
	c = 0
	d = 0
	for i in range(len(s)):
		if s[i] == '1':
			c += 1
			a -= 1
		else:
			d += 1
			b -= 1
		e = min(e, d + a)
		e = min(e, c + b)
	print(e)


t = int(input())
from itertools import accumulate
for _ in range(t):
	s = str(input())
	n = len(s)
	L0 = [0] * (n + 1)
	L1 = [0] * (n + 1)
	R0 = [0] * (n + 1)
	R1 = [0] * (n + 1)
	for i in range(n):
		if s[i] == '0':
			L0[i + 1] = 1
			R0[n - i] = 1
		else:
			L1[i + 1] = 1
			R1[n - i] = 1
	L0 = list(accumulate(L0))
	L1 = list(accumulate(L1))
	R0 = list(accumulate(R0))
	R1 = list(accumulate(R1))
	ans = 10 ** 18
	for i in range(n):
		temp = min(L1[i] - L1[0] + R0[n - 1 - i] - R0[0], L0[i] - L0[0] + R1[n - 1 - i] - R1[0])
		ans = min(ans, temp)
	print(ans)


for _ in range(int(input())):
	s = [int(c) for c in input()]
	n = len(s)
	cnt = 0
	if len(s) < 3:
		print(0)
		continue
	pref = [0]
	for i in range(n):
		if s[i] == 1:
			pref.append(pref[-1] + 1)
		else:
			pref.append(pref[-1])
	total = pref[-1]
	ans = float('inf')
	for i in range(n + 1):
		ans = min(ans, pref[i] + n - i - (total - pref[i]), i - pref[i] + total - pref[i])
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	arr = [0]
	for i in s:
		if i == '0':
			arr.append(arr[-1] + 1)
		else:
			arr.append(arr[-1])
	temp = arr[-1]
	for k in range(n):
		ans = k + 1 - arr[k + 1] + (arr[-1] - arr[k + 1])
		if ans < temp:
			temp = ans
	for k in range(n):
		ans = arr[k + 1] + (n - (k + 1) - (arr[-1] - arr[k + 1]))
		if ans < temp:
			temp = ans
	print(temp)


q = int(input())
for ii in range(q):
	s = input()
	n = len(s)
	wyn = 5298528589245892
	wyn = min(wyn, s.count('0'))
	wyn = min(wyn, s.count('1'))
	cyk = s.count('0')
	for i in range(n):
		if s[i] == '1':
			cyk += 1
		else:
			cyk -= 1
		wyn = min(wyn, cyk)
	cyk = s.count('1')
	for i in range(n):
		if s[i] == '0':
			cyk += 1
		else:
			cyk -= 1
		wyn = min(wyn, cyk)
	print(wyn)


t = int(input())
for i in range(t):
	s = input()
	f = s[0]
	x = 0
	for v in s:
		if int(v) ^ int(f) == 1:
			x = x + 1
			f = v
		if x == 2:
			break
	if x < 2:
		print(0)
	else:
		v = 0
		dp = []
		l = 0
		for h in s:
			if h == '1':
				v = v + 1
			dp.append(v)
			l = l + 1
		res = l
		for j in range(l):
			res = min(res, dp[j] + l - 1 - j - dp[l - 1] + dp[j], j + 1 - dp[j] + dp[l - 1] - dp[j])
		res = min(res, dp[-1], l - dp[-1])
		print(res)


t = int(input())
for _ in range(t):
	s = input()
	x = 0
	y = 0
	for i in s:
		if i == '0':
			x += 1
		else:
			y += 1
	a = 0
	b = 0
	ans = min(x, y)
	for i in s:
		if i == '1':
			b += 1
			y -= 1
		else:
			a += 1
			x -= 1
		ans = min(ans, a + y)
		ans = min(ans, b + x)
	print(ans)


T = int(input())
for t in range(T):
	S = input()
	Arr = []
	sm = 0
	for s in S:
		if s == '1':
			sm += 1
		Arr.append(sm)
	ans = 100000
	for i in range(len(Arr)):
		val = min(Arr[i], i + 1 - Arr[i]) + min(Arr[-1] - Arr[i], len(Arr) - i - 1 - Arr[-1] + Arr[i])
		if val < ans:
			ans = val
	print(ans)


for _ in range(int(input())):
	string = input()
	cost = 1000
	lent = len(string)
	ones = string.count('1')
	zeros = string.count('0')
	(c1, c0) = (0, 0)
	for s in string:
		if s == '1':
			c1 += 1
		else:
			c0 += 1
		cost = min(cost, c1 + zeros - c0, c0 + ones - c1)
	print(cost)


def sum(s, l, r):
	return s[r] - s[l - 1] if l > 0 else s[r]

def calc(st):
	n = len(st)
	s = [0 for i in range(n)]
	s[0] = int(st[0])
	res = 10000
	for i in range(1, n):
		s[i] = s[i - 1] + int(st[i])
	for i in range(n):
		t1 = 0 if i == 0 else min(s[i - 1], i - s[i - 1])
		t2 = 0 if i == len(st) - 1 else min(len(st) - i - sum(s, i, len(st) - 1), sum(s, i, len(st) - 1))
		res = min(res, t1 + t2)
	return res
t = int(input())
res = [calc(input()) for i in range(t)]
for i in res:
	print(i)


t = int(input())
i = 0
while i < t:
	s = input()
	count1 = s.count('1')
	count0 = len(s) - count1
	prefix0 = []
	prefix1 = []
	sufix1 = []
	sufix0 = []
	if s[0] == '1':
		prefix1.append(1)
		prefix0.append(0)
	else:
		prefix0.append(1)
		prefix1.append(0)
	sufix1.append(count1 - prefix1[0])
	sufix0.append(count0 - prefix0[0])
	for x in range(1, len(s)):
		if s[x] == '1':
			prefix1.append(prefix1[-1] + 1)
			prefix0.append(prefix0[-1])
			sufix1.append(count1 - prefix1[x])
			sufix0.append(count0 - prefix0[x])
		else:
			prefix1.append(prefix1[-1])
			prefix0.append(prefix0[-1] + 1)
			sufix1.append(count1 - prefix1[x])
			sufix0.append(count0 - prefix0[x])
	a = float('inf')
	for x in range(len(s)):
		c = abs(prefix1[x] + sufix0[x])
		d = abs(prefix0[x] + sufix1[x])
		a = min(a, c, d)
	print(a)
	i = i + 1


for xyz in range(0, int(input())):
	s = input()
	l = len(s)
	cnt1 = 0
	cnt0 = 0
	(suf0, pre0, suf1, pre1) = ([0] * l, [0] * l, [0] * l, [0] * l)
	if l == 1:
		print(0)
	else:
		for i in range(0, l):
			if s[i] == '1':
				pre1[i] += 1
				cnt1 += 1
			else:
				pre0[i] += 1
				cnt0 += 1
		for i in range(l - 1, -1, -1):
			if s[i] == '1':
				suf1[i] += 1
			else:
				suf0[i] += 1
		for i in range(1, l):
			suf0[i] += suf0[i - 1]
			suf1[i] += suf1[i - 1]
		for i in range(l - 1, 0, -1):
			pre0[i - 1] += pre0[i]
			pre1[i - 1] += pre1[i]
		a = l + 1
		for i in range(0, l - 1):
			a = min(a, suf0[i] + pre1[i + 1], suf1[i] + pre0[i + 1], cnt1, cnt0)
		print(a)


k = int(input())
import math
for i in range(k):
	string = input()
	ones = string.count('1')
	zeros = string.count('0')
	ans = math.inf
	onesTillNow = 0
	zerosTillNow = 0
	for (j, val) in enumerate(string):
		if val == '1':
			onesTillNow += 1
		elif val == '0':
			zerosTillNow += 1
		ans = min(ans, zerosTillNow + ones - onesTillNow, onesTillNow + zeros - zerosTillNow)
	print(ans)


T = int(input())
for _ in range(T):
	S = input()
	precount = [(0, 0)]
	count0 = 0
	count1 = 0
	for elt in S:
		if elt == '0':
			count0 += 1
		else:
			count1 += 1
		precount += ((count0, count1),)
	score0 = 1000
	score1 = 1000
	for i in range(len(S) + 1):
		score0 = min(score0, precount[i][1] + precount[-1][0] - precount[i][0])
		score1 = min(score1, precount[i][0] + precount[-1][1] - precount[i][1])
	print(min(score0, score1))


for _ in range(int(input())):
	s = input()
	ans = float('inf')
	for i in range(len(s)):
		ct1 = 0
		ct2 = 0
		for j in range(i + 1):
			if s[j] == '1':
				ct1 += 1
			else:
				ct2 += 1
		for j in range(i + 1, len(s)):
			if s[j] == '0':
				ct1 += 1
			else:
				ct2 += 1
		ans = min(ans, ct1, ct2)
	print(ans)


t = int(input())
for _ in range(t):
	s = list(map(int, input()))
	N = len(s)
	k = 0
	n = 0
	m = sum(s)
	d = float('inf')
	for x in s:
		k += 1
		if x == 1:
			n += 1
			m -= 1
		d = min(d, k - n + m)
		d = min(d, n + N - k - m)
	print(d)


for i in range(int(input())):
	s = input()
	a = len(s)
	n = 100000000000
	for j in range(a + 1):
		a = s[:j].count('0') + s[j:].count('1')
		b = s[:j].count('1') + s[j:].count('0')
		n = min(n, a, b)
	print(n)


from collections import defaultdict
from collections import Counter
from itertools import accumulate
import bisect
import sys
input = sys.stdin.readline

def solve(s):
	l = len(s)
	ss = list(map(int, s))
	ssum = sum(ss)
	cs = [0] + list(accumulate(ss))
	cnd1 = (j - 2 * cs[j] + ssum for j in range(l + 1))
	cnd2 = (2 * cs[j] + l - j - ssum for j in range(l + 1))
	ans = min(min(cnd1), min(cnd2))
	print(ans)

def main():
	t = int(input())
	for i in range(t):
		s = input().rstrip()
		solve(s)
main()


from sys import stdin
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	s = inp()
	zero = 0
	one = 0
	allOnes = []
	allZeros = []
	ans = 501
	for i in s:
		if i == '1':
			zero += 1
		else:
			one += 1
		allZeros.append(zero)
		allOnes.append(one)
	ans = min(ans, allZeros[-1])
	ans = min(ans, allOnes[-1])
	for i in range(len(s) - 1):
		ans = min(ans, allOnes[i] + allZeros[-1] - allZeros[i + 1])
	for i in range(len(s) - 1):
		ans = min(ans, allZeros[i] + allOnes[-1] - allOnes[i + 1])
	print(ans)


for i in range(int(input())):
	s = input()
	ch = s[0]
	now = 0
	p_0 = 0
	p_1 = 0
	for j in s:
		if j == '1':
			p_1 += 1
		else:
			p_0 += 1
	a = 0
	b = 0
	ans = 1000000000
	for j in s:
		if j == '1':
			b += 1
			p_1 -= 1
		else:
			a += 1
			p_0 -= 1
		te = b + p_0
		te1 = a + p_1
		ans = min(ans, te)
		ans = min(ans, te1)
	print(ans)


for _ in range(int(input())):
	s = input()
	a = s.count('1')
	b = s.count('0')
	(c, d, mini) = (0, 0, len(s))
	for i in range(len(s)):
		if s[i] == '1':
			c += 1
		else:
			d += 1
		mini = min(mini, d + a - c)
		mini = min(mini, c + b - d)
	print(mini)


import itertools

def solve_01(S):
	prefix = list(itertools.accumulate(map(int, S)))
	n = len(S)
	return min((prefix[switch_idx] + (n - 1 - switch_idx) - (prefix[n - 1] - prefix[switch_idx]) for switch_idx in range(n)))

def solve_10(S):
	prefix = list(itertools.accumulate(map(int, S)))
	n = len(S)
	return min((switch_idx + 1 - prefix[switch_idx] + prefix[n - 1] - prefix[min(switch_idx + 1, n - 1)] for switch_idx in range(n)))

def solve(S):
	if all((c == '1' for c in S)) or all((c == '0' for c in S)):
		return 0
	return min(sum((c == '0' for c in S)), sum((c == '1' for c in S)), solve_01(S), solve_10(S))

def main():
	T = int(input())
	for _ in range(T):
		S = input().strip()
		print(solve(S))
main()


for _ in range(int(input())):
	s = input()
	ans = min(s.count('0'), s.count('1'))
	d = {'0': 0, '1': 0}
	d2 = {'0': 0, '1': 0}
	for i in s:
		d2[i] += 1
	for i in range(len(s) - 1):
		c = s[i]
		d[c] += 1
		d2[c] -= 1
		ans = min(ans, d['0'] + d2['1'], d['1'] + d2['0'])
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	(suf0, suf1) = (0, 0)
	for i in s:
		suf0 += i == '0'
		suf1 += i == '1'
	ans = min(suf0, suf1)
	(pref0, pref1) = (0, 0)
	for i in s:
		(pref0, suf0) = (pref0 + (i == '0'), suf0 - (i == '0'))
		(pref1, suf1) = (pref1 + (i == '1'), suf1 - (i == '1'))
		ans = min(ans, pref0 + suf1, pref1 + suf0)
	print(ans)


import sys
for _ in range(int(input())):
	s = input()
	x = s.count('1')
	y = s.count('0')
	a = 0
	b = 0
	m = sys.maxsize
	for i in s:
		if i == '1':
			a += 1
		else:
			b += 1
		m = min(m, min(b + x - a, a + y - b))
	print(m)


from math import *
from copy import *
from string import *
from random import *
from sys import stdin, stdout
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
import math
import math
import time
from queue import Queue

def seive(n):
	l = [True]
	l = l * (n + 1)
	for i in range(2, int(sqrt(n)) + 1):
		if l[i] == True:
			val = i * i
			while val < len(l):
				l[val] = False
				val += i
	prime = []
	for i in range(2, len(l)):
		if l[i] == True:
			prime.append(i)
	return prime
t = int(input())
for i in range(t):
	s = str(input())
	mini = 1000000
	l1 = []
	l2 = []
	l3 = []
	l4 = []
	for j in range(len(s)):
		l3.append(0)
		l4.append(0)
	c = 0
	for j in range(len(s)):
		if s[j:j + 1] == '0':
			c += 1
		l1.append(c)
	c = 0
	for j in range(len(s)):
		if s[j:j + 1] == '1':
			c += 1
		l2.append(c)
	c = 0
	for j in range(len(s)):
		if s[len(s) - j - 1:len(s) - j] == '0':
			c += 1
		l3[len(s) - j - 1] = c
	c = 0
	for j in range(len(s)):
		if s[len(s) - j - 1:len(s) - j] == '1':
			c += 1
		l4[len(s) - j - 1] = c
	for j in range(len(s)):
		if j == len(s) - 1:
			mini = min(min(mini, l1[j]), l2[j])
		else:
			v1 = l2[j] + l3[j + 1]
			v2 = l1[j] + l4[j + 1]
			mini = min(min(v1, v2), mini)
	print(mini)


for _ in range(int(input())):
	arr = list(map(int, input().strip()))
	n = len(arr)
	oc = sum(arr)
	zc = n - oc
	cost = 1000
	c1 = 0
	c0 = 0
	for i in range(n):
		e = arr[i]
		r0 = zc - c0 - (0 if e else 1)
		r1 = oc - c1 - (1 if e else 0)
		curr = min(r0 + c0 + (0 if e else 1), r0 + c1 + (0 if e else 1), r1 + c0 + (0 if e else 1), r1 + c0 + (1 if e else 0), r0 + c1 + (1 if e else 0), r1 + c1 + (1 if e else 0))
		cost = min(cost, curr)
		c0 += 0 if e else 1
		c1 += 1 if e else 0
	print(cost)


from sys import stdin, stdout
t = int(input())
answers = []
for _ in range(t):
	s = stdin.readline().strip()
	if len(s) < 3:
		answers.append(0)
	elif len(s) == 3:
		if s == '010' or s == '101':
			answers.append(1)
		else:
			answers.append(0)
	else:
		pref = [0] * len(s)
		tot = 0
		for i in range(len(s)):
			tot += int(s[i])
			pref[i] = tot
		ans = 10 ** 4
		flip = [False] * len(s)
		for i in range(1, len(s)):
			a = i - pref[i - 1] + pref[-1] - pref[i]
			b = pref[i - 1] + len(s) - i - 1 - (pref[-1] - pref[i])
			ans = min(ans, a, b)
		answers.append(ans)
print(*answers, sep='\n')


t = int(input())
for _ in range(t):
	arr = [int(x) for x in input()]
	left0 = []
	left1 = []
	c0 = 0
	c1 = 0
	for each in arr:
		left1.append(c1)
		left0.append(c0)
		if each == 0:
			c0 += 1
		else:
			c1 += 1
	left1.append(c1)
	left0.append(c0)
	right1 = []
	right0 = []
	c0 = 0
	c1 = 0
	for i in range(len(arr) - 1, -1, -1):
		if arr[i] == 0:
			c0 += 1
		else:
			c1 += 1
		right0.append(c0)
		right1.append(c1)
	right1.reverse()
	right0.reverse()
	ans = float('inf')
	for i in range(len(arr)):
		ans = min(left0[i] + right1[i], ans, left1[i] + right0[i])
	print(ans)


def main():
	from sys import stdin
	from sys import stdout
	input = stdin.readline
	print = stdout.write
	t = int(input())
	for _ in range(t):
		s = input()[:-1]
		if len(set(list(s))) == 1:
			print('0\n')
			continue
		options = []
		counter = [s.count('0'), s.count('1')]
		rest = counter[0]
		tmp_counter = 0
		for i in range(len(s)):
			if s[i] == '0':
				rest -= 1
			else:
				options.append(rest + tmp_counter)
				tmp_counter += 1
		rest = counter[1]
		tmp_counter = 0
		for i in range(len(s)):
			if s[i] == '1':
				rest -= 1
			else:
				options.append(rest + tmp_counter)
				tmp_counter += 1
		print('{0}\n'.format(min(options)))
main()


import sys
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
for _ in range(int(ri())):
	s = ri()
	n = len(s)
	pre0 = [0] * (len(s) + 1)
	pre1 = [0] * (len(s) + 1)
	for i in range(len(s)):
		if s[i] == '1':
			pre1[i + 1] = pre1[i] + 1
			pre0[i + 1] = pre0[i]
		else:
			pre0[i + 1] = pre0[i] + 1
			pre1[i + 1] = pre1[i]
	minn = 10 ** 18
	for i in range(0, len(s) + 1):
		minn = min(minn, pre1[i] + pre0[n] - pre0[i])
	for i in range(0, len(s) + 1):
		minn = min(minn, pre0[i] + pre1[n] - pre1[i])
	print(minn)


def main():
	s = list(map(int, list(input())))
	n = len(s)
	ones_count = [0] * (n + 1)
	ones_count[1] = s[0]
	for i in range(1, n):
		ones_count[i + 1] = ones_count[i] + s[i]
	min_res = n
	for i in range(n + 1):
		ones_left = ones_count[i]
		zeros_left = i - ones_left
		ones_right = ones_count[-1] - ones_count[i]
		zeros_right = n - i - ones_right
		zeros_ones_res = ones_left + zeros_right
		ones_zeros_res = zeros_left + ones_right
		res = min(zeros_ones_res, ones_zeros_res)
		min_res = min(min_res, res)
	print(min_res)
for _ in range(int(input())):
	main()


import os
import sys
from io import BytesIO, IOBase

def iin():
	return int(input())

def lin():
	return list(map(int, input().split()))

def main():
	T = iin()
	for _ in range(T):
		s = input()
		n = len(s)
		a1 = [[0, 0] for i in range(n)]
		ch = 0
		for i in s:
			a1[ch][int(i)] += 1
			ch += 1
		a2 = [a1[i][0] for i in range(n)]
		a3 = [a1[i][1] for i in range(n)]
		ans = n
		for i in range(n):
			ans = min(ans, n - sum(a2[:i] + a3[i:]), n - sum(a3[:i] + a2[i:]))
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def solve(d):
	big = float('inf')
	small = float('-inf')
	n = len(d)
	all = set(d)
	if len(all) == 1:
		return 0
	change = n
	for i in range(n):
		b0 = 0
		b1 = 0
		for j in range(i):
			if d[j] == '1':
				b1 += 1
			else:
				b0 += 1
		a0 = 0
		a1 = 0
		acount = 0
		for j in range(i + 1, n):
			if d[j] == '1':
				a1 += 1
			else:
				a0 += 1
		one = i - b0 + a0
		two = i - b1 + a1
		change = min(change, min(one, two))
	return change

def main():
	t = int(input())
	for i in range(t):
		d = input()
		ans = solve(d)
		print(ans)
main()


for _ in range(int(input())):
	s = input()
	n = 1000000000
	a = b = 0
	(o, z) = (s.count('0'), s.count('1'))
	for i in s:
		if i == '1':
			a += 1
		else:
			b += 1
		n = min(n, a + o - b, b + z - a)
	print(n)


for i in range(int(input())):
	a = input()
	o = a.count('1')
	(z, ans) = (len(a) - o, float('inf'))
	n0 = n1 = 0
	for i in a:
		if i == '1':
			n1 += 1
		else:
			n0 += 1
		ans = min(ans, o - n1 + n0, z - n0 + n1)
	print(ans)


import sys

def solve(s):
	one_prefix = [0 if s[0] == '1' else 1]
	zero_prefix = [0 if s[0] == '0' else 1]
	for i in range(1, len(s)):
		char = s[i]
		if char == '1':
			one_prefix.append(one_prefix[-1])
			zero_prefix.append(zero_prefix[-1] + 1)
		else:
			one_prefix.append(one_prefix[-1] + 1)
			zero_prefix.append(zero_prefix[-1])
	mn_zero = float('inf')
	mn_one = float('inf')
	for i in range(len(s)):
		zero_score = zero_prefix[i]
		one_score = one_prefix[-1] - one_prefix[i]
		mn_zero = min(mn_zero, zero_prefix[i] + one_prefix[-1] - one_prefix[i])
		mn_one = min(mn_one, one_prefix[i] + zero_prefix[-1] - zero_prefix[i])
	return min(mn_one, mn_zero, one_prefix[-1], zero_prefix[-1])
t = int(input())
for _ in range(t):
	s = input()
	print(solve(s))


n = input('')
n = int(n)
strr = []
for i in range(n):
	entries = input('')
	strr.append(entries)
for j in strr:
	my_str = j
	j = list(j)
	cnt = 0
	num1 = j.count('1')
	num0 = j.count('0')
	if num1 + num0 <= 2 or num1 == 0 or num0 == 0:
		print(0)
		continue
	cnt = min(num1, num0)
	in1 = 0
	in0 = 0
	fi1 = num1
	fi0 = num0
	lp = 0
	while lp < len(j):
		if j[lp] == '1':
			cnt1 = in1 + fi0
			cnt2 = in0 + fi1 - 1
			cnt = min(cnt, min(cnt1, cnt2))
			in1 = in1 + 1
			fi1 = fi1 - 1
		else:
			cnt1 = in1 + fi0 - 1
			cnt2 = in0 + fi1
			cnt = min(cnt, min(cnt1, cnt2))
			in0 = in0 + 1
			fi0 = fi0 - 1
		lp = lp + 1
	print(cnt)


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	a = input()
	l = len(a)
	ans = l + 1
	one = a.count('1')
	zero = l - one
	for i in range(l):
		o = a[:i].count('1')
		lz = o + (zero - (i - o))
		lo = i - o + (one - o)
		ans = min(ans, lz, lo)
	print(ans)


t = int(input())
for k in range(t):
	s = input()
	if len(s) < 3:
		print('0')
		continue
	a = []
	b = []
	a.append(s[0])
	b.append(1)
	for i in range(1, len(s)):
		if s[i] == a[-1]:
			b[-1] += 1
		else:
			a.append(s[i])
			b.append(1)
	if len(a) < 3:
		print(0)
		continue
	champ = 1000000
	for r in range(0, len(a)):
		l1 = 0
		l2 = 0
		r1 = 0
		r2 = 0
		for i in range(r):
			if a[i] == '0':
				l1 += b[i]
			else:
				l2 += b[i]
		for i in range(r, len(a)):
			if a[i] == '1':
				r1 += b[i]
			else:
				r2 += b[i]
		champ = min(champ, min(l1 + r1, l2 + r2))
	print(champ)


t = int(input())
from math import *
for ab in range(t):
	kq = list(input())
	n = len(kq)
	kq = [int(x) for x in kq]
	c = kq.count(0)
	d = kq.count(1)
	ans = n
	al = 0
	bl = 0
	for ip in range(n):
		if kq[ip] == 0:
			al += 1
		else:
			bl += 1
		ans = min(ans, bl + c - al, al + d - bl)
	print(ans)


import sys, math
from collections import defaultdict as dd, deque, Counter

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1000000000.0) + 7

def main():
	for t in range(int(data())):
		s = data()
		n = len(s)
		dp1 = [0] * n
		dp2 = [0] * n
		for i in range(n):
			if s[i] == '1':
				dp1[i] = dp1[i - 1] + 1
				dp2[i] = dp2[i - 1]
			else:
				dp2[i] = dp2[i - 1] + 1
				dp1[i] = dp1[i - 1]
		m = dp1[-1]
		for i in range(n):
			m = min(m, dp1[i] + dp2[-1] - dp2[i], dp2[i] + dp1[-1] - dp1[i])
		out(m)
main()


try:
	for _ in range(int(input())):
		s = input()
		z = s.count('0')
		o = s.count('1')
		ans = 999999
		(ones, zeroes) = (0, 0)
		for i in s:
			if i == '1':
				ones += 1
			else:
				zeroes += 1
			ans = min(ans, zeroes + o - ones)
			ans = min(ans, ones + z - zeroes)
		print(ans)
except:
	pass


def fa(s):
	A = [0 for i in range(len(s))]
	for i in range(len(s)):
		if s[i] == '1':
			A[i] = A[i] + 1
		if i > 0:
			A[i] = A[i] + A[i - 1]
	v = len(s)
	tp = 0
	for i in range(len(s) - 1, -1, -1):
		if s[i] == '0':
			v = min(v, tp + A[i])
			tp = tp + 1
	return v
for _ in range(int(input())):
	s = input()
	c = ''
	for i in s:
		if i == '0':
			c = c + '1'
		else:
			c = c + '0'
	v = min(fa(s), fa(c))
	print(v)


for _ in range(int(input())):
	s = list(input())
	if s == sorted(s) or s == sorted(s, reverse=True):
		print(0)
		continue
	tot_ones = s.count('1')
	tot_zeros = len(s) - tot_ones
	curr_zeros = curr_ones = 0
	mini = float('inf')
	for i in s:
		if i == '1':
			curr_ones += 1
		else:
			curr_zeros += 1
		mini = min(mini, curr_ones + (tot_zeros - curr_zeros))
	curr_zeros = curr_ones = 0
	for i in s:
		if i == '1':
			curr_ones += 1
		else:
			curr_zeros += 1
		mini = min(mini, curr_zeros + (tot_ones - curr_ones))
	print(mini)


import sys

def I():
	return sys.stdin.readline().rstrip()
for tc in range(1, 1 + int(I())):
	s = I()
	n = len(s)
	l = [0] * n
	z = 0
	for i in range(n):
		l[i] = l[i - 1] + (1 if s[i] == '0' else 0)
		z += 1 if s[i] == '0' else 0
	ans = min(z, n - z)
	for i in range(n - 1):
		ans = min(ans, min(l[i], i + 1 - l[i]) + min(z - l[i], n - i - 1 - z + l[i]))
	print(ans)


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import bisect as bi
import heapq

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_le(a, x):
	i = bi.bisect_right(a, x)
	if i:
		return i - 1
	else:
		return -1

def main():
	try:
		l = list(input())
		(c0, c1, ma) = (l.count('0'), l.count('1'), 100)
		ma = min(c0, c1)
		if c1 != 0 and c0 == 0 or (c0 != 0 and c1 == 0):
			print(0)
		else:
			(x0, x1) = (0, 0)
			for i in l:
				if i == '1':
					x1 += 1
					c1 -= 1
				else:
					x0 += 1
					c0 -= 1
				ma = min(x1 + c0, ma)
				ma = min(c1 + x0, ma)
			print(ma)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


for _ in range(int(input())):
	s = input()
	wyn = min(s.count('1'), s.count('0'))
	for cyk_dig in '01':
		cyk = s.count(cyk_dig)
		for c in s:
			cyk += -1 if c == cyk_dig else 1
			wyn = min(wyn, cyk)
	print(wyn)


t = int(input())
for _ in range(t):
	s = input()
	r = 10 ** 9
	for i in range(len(s)):
		a = min(s[:i].count('0'), s[:i].count('1'))
		b = min(s[i:].count('0'), s[i:].count('1'))
		r = min(r, a + b)
	print(r)


for _ in range(int(input())):
	s = input()
	(z, o, tz, to, ans) = (0, 0, 0, 0, 2345678923)
	(tz, to) = (s.count('0'), s.count('1'))
	for i in range(0, len(s)):
		if s[i] == '0':
			z += 1
		elif s[i] == '1':
			o += 1
		ans = min(ans, o + tz - z, z + to - o)
	print(ans)


for item in [0] * int(input()):
	a = input()
	zero = a.count('0')
	one = a.count('1')
	p0 = 0
	p1 = 0
	s0 = zero - p0
	s1 = one - p1
	mini = float('inf')
	for item in a:
		if item == '0':
			p0 += 1
			s0 = zero - p0
		else:
			p1 += 1
			s1 = one - p1
		mini = min(p0 + s1, p1 + s0, mini)
	print(mini)


def answer(A):
	s = [0] * len(A)
	for i in range(len(A)):
		if A[i] == '1':
			s[i] = s[i - 1] + 1
		else:
			s[i] = s[i - 1]
	mini = 2147483647
	for i in range(len(A)):
		mini = min(mini, i + 1 - 2 * s[i] + s[-1], 2 * s[i] + len(A) - (i + 1) - s[-1])
	return mini
t = int(input())
for i in range(t):
	s = input()
	print(answer(s))


from sys import stdin, stdout
cin = stdin.readline
cout = stdout.write
for _ in range(int(cin())):
	s = cin()[:-1]
	n = len(s)
	ans = 10 ** 5
	for i in range(n):
		lz = rz = lo = ro = 0
		for j in range(i):
			if s[j] == '1':
				lz += 1
			else:
				lo += 1
		for j in range(i, n):
			if s[j] == '1':
				rz += 1
			else:
				ro += 1
		ans = min(ans, lz + ro, lo + rz)
	cout(str(ans) + '\n')


def lengths(s):
	output = []
	current = ''
	currentcount = 0
	for char in s:
		if char != current:
			if currentcount > 0:
				output.append(currentcount)
			current = char
			currentcount = 1
		else:
			currentcount += 1
	output.append(currentcount)
	return output

def alternatingindices(n):
	output = []
	output.append(list(range(0, n, 2)))
	output.append(list(range(1, n, 2)))
	return output

def neighboringindices(n):
	output = []
	for i in range(1, n):
		output.append(list(range(i - 1, -1, -2)) + list(range(i, n, 2)))
	return output

def skippingindices(n):
	output = []
	for i in range(3, n):
		output.append(list(range(i - 3, -1, -2)) + list(range(i, n, 2)))
	if n >= 3:
		output.append(list(range(2, n, 2)))
		output.append(list(range(n - 3, -1, -2)))
	return output

def allindices(n):
	return alternatingindices(n) + neighboringindices(n) + skippingindices(n)

def indexsum(l, indices):
	output = sum([l[i] for i in indices])
	return output

def bestsum(s):
	l = lengths(s)
	if len(l) < 3:
		return 0
	else:
		indexoptions = allindices(len(l))
		return min([indexsum(l, indices) for indices in indexoptions])
numberofcases = int(input())
for _ in range(numberofcases):
	print(bestsum(input()))


N = int(input())
for i in range(N):
	S = input()
	k = len(S)
	x = y = 0
	for i in S:
		if i == '0':
			y += 1
		else:
			x += 1
	a = b = 0
	for i in range(k):
		if S[i] == '0':
			b += 1
		else:
			a += 1
		A = a + y - b
		B = b + x - a
		if k > A:
			k = A
		if k > B:
			k = B
	print(k)


t = int(input())
for w in range(t):
	s = input()
	length = len(s)
	count = 0
	minn = 500
	for i in s:
		if i == '1':
			count += 1
	for i in range(length):
		count2 = 0
		for j in range(min(i + 1, length)):
			if s[j] == '1':
				count2 += 1
		if (i + 1) / 2 > count2:
			l = count2
		else:
			l = i + 1 - count2
		if (length - i - 1) / 2 > count - count2:
			r = count - count2
		else:
			r = length - i - 1 - count + count2
		if r + l < minn:
			minn = r + l
	print(minn)


t = int(input())
for i in range(t):
	s = '#' + input() + '#'
	array = [None] * len(s)
	(zero, one) = (0, 0)
	for j in range(len(s)):
		if s[j] == '0':
			zero += 1
		if s[j] == '1':
			one += 1
	array[0] = zero
	for j in range(1, len(s) - 1):
		array[j] = array[j - 1]
		if s[j] == '1':
			array[j] += 1
		else:
			array[j] -= 1
	mini = array[0]
	for j in range(len(s) - 1):
		mini = min(mini, array[j], len(s) - 2 - array[j])
	print(mini)


for s in [*open(0)][1:]:
	a = [0]
	for c in s[:-1]:
		a += (a[-1] + int(c),)
	print(min((min(i + a[-1] - 2 * x, 2 * x + len(s) - 1 - i - a[-1]) for (i, x) in enumerate(a))))


import math
t = int(input())
for i in range(t):
	inp = input()
	count0 = 0
	count1 = 0
	dp0 = [0]
	dp1 = [0]
	for inpp in inp:
		if inpp == '1':
			count1 += 1
		elif inpp == '0':
			count0 += 1
		dp0.append(count0)
		dp1.append(count1)
	minop = len(inp)
	for i in range(len(inp) + 1):
		l1 = dp1[i]
		l0 = dp0[i]
		r1 = dp1[-1] - dp1[i]
		r0 = dp0[-1] - dp0[i]
		minop = min(minop, l1 + r0, l0 + r1, l1 + r1, l0 + r0)
	print(minop)


from math import *
import heapq
from dataclasses import dataclass, field
from typing import Any
m = []
nn = []
t = int(input())
for i in range(t):
	m.append(input())
for i in range(t):
	s = m[i]
	n = len(s)
	q = []
	tmp = 0
	for j in range(n):
		if s[j] == '1':
			tmp += 1
			q.append(tmp)
		else:
			q.append(tmp)
	ans = n
	flag = 0
	for j in range(n):
		if q[n - 1] == n or q[n - 1] == 0:
			ans = 0
			break
		else:
			ans = min(ans, min(q[j], j + 1 - q[j]) + min(q[n - 1] - q[j], n - j - 1 - q[n - 1] + q[j]))
	print(ans)


for _ in range(int(input())):
	s = input()
	r1 = s.count('1')
	r0 = s.count('0')
	one = 0
	zero = 0
	p = []
	for i in range(len(s)):
		if s[i] == '0':
			zero += 1
			r0 -= 1
		else:
			one += 1
			r1 -= 1
		p.append(min(r1 + zero, r0 + one))
	print(min(p))


for testCases in range(int(input())):
	s = input()
	one = s.count('1')
	zero = s.count('0')
	s = [int(i) for i in s]
	ans = min(zero, one)
	(curr_zero, curr_one) = (0, 0)
	for val in s:
		if val == 1:
			curr_one += 1
		else:
			curr_zero += 1
		ans = min(ans, curr_zero + one - curr_one)
		ans = min(ans, curr_one + zero - curr_zero)
	print(ans)


for _ in range(int(input())):
	x = input()
	if x == '0' * len(x):
		print(0)
		continue
	if x == '1' * len(x):
		print(0)
		continue
	c = 0
	for i in range(1, len(x)):
		if x[i] != x[i - 1]:
			c += 1
	if c == 1:
		print(0)
		continue
	c = 10 ** 9
	for i in range(len(x)):
		(lo, lz) = (0, 0)
		(ro, rz) = (0, 0)
		for j in range(i, -1, -1):
			if x[j] == '1':
				lo += 1
			else:
				lz += 1
		for j in range(i + 1, len(x)):
			if x[j] == '1':
				ro += 1
			else:
				rz += 1
		c = min(c, lz + ro)
		c = min(c, rz + lo)
		c = min(c, rz + lz)
		c = min(c, ro + lo)
	print(c)


def solve(test):
	s = list(input())
	zeros = []
	ones = []
	for ch in s:
		if ch == '0':
			zeros.append(0)
			ones.append(1)
		else:
			zeros.append(1)
			ones.append(0)
	ones_count = 0
	ones_ans = 100000
	zeros_count = 0
	zeros_ans = 100000
	ans = 1000000
	for i in range(len(s)):
		ones_count += ones[i]
		zeros_count += zeros[i]
		a = 0
		b = 0
		for j in range(i + 1, len(s)):
			a += ones[j]
			b += zeros[j]
		ans = min(ans, ones_count + b, zeros_count + a)
	print(ans)

def main():
	test_case = int(input())
	for test in range(test_case):
		solve(test + 1)
main()


t = int(input())
for i in range(t):
	s = input()
	x = []
	ones = 0
	count = 0
	count1 = 0
	for j in range(len(s)):
		if s[j] == '1':
			ones += 1
	for j in range(len(s)):
		if s[j] == '1':
			count += 1
			x.append(j + 1 - 2 * count + ones)
			x.append(count + len(s) - ones - count1)
		else:
			count1 += 1
			x.append(count + len(s) - ones - count1)
			x.append(j + 1 - 2 * count + ones)
	print(min(x))


t = int(input())
for _ in range(t):
	s = input()
	no_0 = 0
	no_1 = 0
	for i in range(len(s)):
		if s[i] == '1':
			no_1 += 1
		else:
			no_0 += 1
	l = []
	now_0 = 0
	now_1 = 0
	for i in range(len(s)):
		if s[i] == '1':
			now_1 += 1
		else:
			now_0 += 1
		l.append(min(now_0 + no_1 - now_1, now_1 + no_0 - now_0))
	print(min(l))


def solve():
	arr = input()
	N = len(arr)
	pf = [0] * (N + 1)
	for i in range(N):
		pf[i + 1] = pf[i] + int(arr[i] == '1')

	def num_ones(fro, to):
		return pf[to] - pf[fro]

	def num_zeros(fro, to):
		return to - fro - num_ones(fro, to)
	min_cost = N + 10
	for i in range(N + 1):
		min_cost = min(min_cost, num_ones(0, i) + num_zeros(i, N))
		min_cost = min(min_cost, num_zeros(0, i) + num_ones(i, N))
	print(min_cost)
t = int(input())
for _ in range(t):
	solve()


from __future__ import division, print_function
import sys
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

def input():
	return sys.stdin.readline()

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
for _ in range(int(input())):
	a = input().strip()
	n = len(a)
	res = min(a.count('0'), a.count('1'))
	one = [0] * n
	zero = [0] * n
	zero[0] = a[0] == '0'
	one[0] = a[0] == '1'
	for i in range(1, n):
		one[i] = one[i - 1]
		zero[i] = zero[i - 1]
		if a[i] == '0':
			zero[i] += 1
		else:
			one[i] += 1
	res = n
	cnt = 0
	for i in range(n - 1, -1, -1):
		if a[i] == '1':
			cnt += 1
		res = min(res, cnt + zero[i] - 1)
	cnt = 0
	for i in range(n - 1, -1, -1):
		if a[i] == '0':
			cnt += 1
		res = min(res, cnt + one[i] - 1)
	print(res)


t = int(input())
for q in range(t):
	s = input()
	n = len(s)
	p = []
	p.append(0)
	for i in range(len(s) - 1, -1, -1):
		p.append(p[-1] + int(s[i]))
	mini = 10 ** 18
	suma = 0
	mini = min(p[n], mini)
	for i in range(n):
		suma += int(s[i])
		mini = min(mini, suma + n - i - 1 - p[n - 1 - i])
	suma = 0
	mini = min(n - p[n], mini)
	for i in range(n):
		suma += int(s[i])
		mini = min(mini, i + 1 - suma + p[n - 1 - i])
	print(mini)


from collections import Counter as C, defaultdict as D, deque as Q
from operator import itemgetter as I
from itertools import product as P
from bisect import bisect_left as BL, bisect_right as BR
from heapq import heappush as HPUSH, heappop as HPOP
from math import floor as MF, ceil as MC, gcd as MG, factorial as F, sqrt as SQRT, inf as INFINITY

def player1():
	print('')

def player2():
	print('')

def isPrime(n):
	for i in range(2, MC(SQRT(n)) + 1):
		if n % i == 0:
			return False
	return True

def findAns(s, n):
	prefix = [0] * n
	suffix = [0] * n
	if s[0] == '0':
		prefix[0] = 1
	if s[n - 1] == '1':
		suffix[n - 1] = 1
	for i in range(1, n):
		if s[i] == '0':
			prefix[i] = prefix[i - 1] + 1
		else:
			prefix[i] = prefix[i - 1]
	for i in range(n - 2, -1, -1):
		if s[i] == '1':
			suffix[i] = suffix[i + 1] + 1
		else:
			suffix[i] = suffix[i + 1]
	ans = INFINITY
	for i in range(n - 1):
		ans = min(ans, prefix[i] + suffix[i + 1])
	return ans

def main():
	for _ in range(int(input())):
		s = input()
		n = len(s)
		allzeroes = '0' * n
		allones = '1' * n
		if '1' not in s or '0' not in s:
			print(0)
			continue
		(count1, count2) = (0, 0)
		for i in range(n):
			if allzeroes[i] != s[i]:
				count1 += 1
			if allones[i] != s[i]:
				count2 += 1
		print(min(count1, count2, findAns(s, n), findAns(s[::-1], n)))
main()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	str1 = str(input())
	x = str1.count('0')
	y = str1.count('1')
	if x == 0 or y == 0:
		print(0)
		continue
	(ans, a, b) = (1000000000.0, 0, 0)
	for i in str1[:-1]:
		if i == '1':
			a += 1
		else:
			b += 1
		ans = min(ans, b + y - a, a + x - b)
	print(ans)


from itertools import accumulate

def mindiff(ones1, zeroes1, ones2, zeroes2):
	return min(zeroes1 + ones2, ones1 + zeroes2)

def solve():
	s = input()
	array = [int(x) for x in s]
	prefix = list(accumulate(array))
	totalones = sum(array)
	totalzeroes = len(array) - totalones
	minimum = 10 ** 6
	minimum = min(minimum, mindiff(0, 0, totalones, totalzeroes))
	for x in range(len(s)):
		minimum = min(minimum, mindiff(prefix[x], x + 1 - prefix[x], totalones - prefix[x], totalzeroes - (x + 1 - prefix[x])))
	return minimum
T = int(input())
for case in range(T):
	print(solve())


t = int(input())
for _ in [0] * t:
	s = input()
	print(min(map(lambda x: min(x, len(s) - x), [s[:i].count('0') + s[i:].count('1') for i in range(len(s) + 1)])))


for _ in range(int(input())):
	s = input()
	(pre1, pre0, suf1, suf0) = ([int(s[0])], [1 - int(s[0])], [int(s[-1])], [1 - int(s[-1])])
	for i in range(1, len(s)):
		pre1.append(pre1[-1] + int(s[i]))
		pre0.append(pre0[-1] + 1 - int(s[i]))
	for i in range(len(s) - 2, -1, -1):
		suf1.append(suf1[-1] + int(s[i]))
		suf0.append(suf0[-1] + 1 - int(s[i]))
	suf1.reverse()
	suf0.reverse()
	pre1.insert(0, 0)
	pre0.insert(0, 0)
	suf1.append(0)
	suf0.append(0)
	(zo, oz) = (10000.0, 10000.0)
	for i in range(len(s) + 1):
		zo = min(zo, pre0[i] + suf1[i])
		oz = min(oz, pre1[i] + suf0[i])
	print(min(oz, zo))


import string
t = int(input())
for x in range(t):
	a = input()
	n = len(a)
	num_0 = num_1 = 0
	for i in range(n):
		if a[i] == '1':
			num_1 += 1
		if a[i] == '0':
			num_0 += 1
	ans = min(num_1, num_0)
	done_1 = 0
	done_0 = 0
	for it in range(n):
		if a[it] == '1':
			num_1 -= 1
		if a[it] == '1':
			done_1 += 1
		if a[it] == '0':
			num_0 -= 1
		if a[it] == '0':
			done_0 += 1
		ans = min(ans, done_1 + num_0)
		ans = min(ans, done_0 + num_1)
	print(ans)


for i in range(int(input())):
	s = input()
	t = 10 ** 18
	for j in range(len(s)):
		a = s[:j].count('1')
		b = s[:j].count('0')
		c = s[j:].count('1')
		d = s[j:].count('0')
		t = min(t, a + d, b + c)
	print(t)


import sys
import math
from collections import defaultdict, deque
import heapq
t = int(sys.stdin.readline())
for _ in range(t):
	s = sys.stdin.readline()[:-1]
	cnt1 = 0
	n = len(s)
	ans = n
	for i in range(n):
		if s[i] == '0':
			cnt1 += 1
		cnt0 = 0
		for j in range(i + 1, n):
			if s[j] == '1':
				cnt0 += 1
		ans = min(ans, cnt1 + cnt0)
	cnt1 = 0
	for i in range(n):
		if s[i] == '1':
			cnt1 += 1
		cnt0 = 0
		for j in range(i + 1, n):
			if s[j] == '0':
				cnt0 += 1
		ans = min(ans, cnt1 + cnt0)
	print(ans)


for _ in range(int(input())):
	s = input()
	x = 100000000000
	for i in range(len(s) + 1):
		x = min(x, s[:i].count('0') + s[i:].count('1'))
		x = min(x, s[:i].count('1') + s[i:].count('0'))
	print(x)


T = int(input())
II = 0
while II < T:
	s = [int(y) for y in str(input())]
	n = len(s)
	i = 0
	ans = n
	p = sum(s)
	while i < n:
		ans = min(ans, sum(s[:i]) + n - i - sum(s[i:]))
		i += 1
	i = 0
	while i < n:
		ans = min(ans, -sum(s[:i]) + i + sum(s[i:]))
		i += 1
	print(ans)
	II += 1


for _ in range(int(input())):
	val = 10000000000000000
	ss = input()
	l = len(ss)
	a = [0] * (l + 1)
	b = [0] * (l + l)
	for i in range(1, l + 1):
		if ss[i - 1] == '0':
			a[i] = a[i - 1] + 1
			b[i] = b[i - 1]
		else:
			a[i] = a[i - 1]
			b[i] = b[i - 1] + 1
	for i in range(1, l + 1):
		x1 = i - a[i]
		x2 = i - b[i]
		x3 = i - 1 - b[i - 1]
		x4 = i - 1 - a[i - 1]
		x5 = l - i - a[l] + a[i]
		x6 = l - i - b[l] + b[i]
		x7 = l - i + 1 - a[l] + a[i - 1]
		x8 = l - i + 1 - b[l] + b[i - 1]
		val = min(val, min(x1 + x6, min(x2 + x5, min(x3 + x7, x4 + x8))))
	print(val)


for _ in range(int(input())):
	s = input()
	tot1 = s.count('1')
	tot0 = s.count('0')
	ans = len(s)
	done1 = 0
	done0 = 0
	for i in range(len(s)):
		if s[i] == '1':
			done1 += 1
		else:
			done0 += 1
		ans = min(tot1 - done1 + done0, tot0 - done0 + done1, ans)
	ans = min(tot0, tot1, ans)
	print(ans)


t = int(input())
for hh in range(0, t):
	s = input()
	n = len(s)
	count0 = [0] * 1001
	count1 = [0] * 1001
	if s[0] == '0':
		count0[0] = 1
	else:
		count1[0] = 1
	for i in range(1, n):
		if s[i] == '0':
			count0[i] = count0[i - 1] + 1
			count1[i] = count1[i - 1]
		else:
			count0[i] = count0[i - 1]
			count1[i] = count1[i - 1] + 1
	ans = 10001
	for i in range(0, n):
		leftzeroes = count0[i]
		tomade1 = i + 1 - leftzeroes
		rightones = count1[n - 1] - count1[i]
		tomade0 = n - 1 - i - rightones
		ans = min(ans, tomade0 + tomade1)
	for i in range(0, n):
		leftones = count1[i]
		tomade0 = i + 1 - leftones
		rightzeroes = count0[n - 1] - count0[i]
		tomade1 = n - 1 - i - rightzeroes
		ans = min(ans, tomade0 + tomade1)
	print(ans)


for i in range(int(input())):
	s = input()
	if s.count('1') == 1 and (s[0] == '1' or s[-1] == '1') or (s.count('0') == 1 and (s[0] == '0' or s[-1] == '0')):
		print(0)
	else:
		n = len(s)
		maxa = 123456789
		for i in range(n):
			maxa = min(maxa, s[i:].count('1') + s[:i].count('0'))
			maxa = min(maxa, s[:i].count('1') + s[i:].count('0'))
		print(maxa)


for t in range(int(input())):
	s = input()
	steps = 0
	sOneCost = 2147483647
	eOneCost = 2147483647
	n_one = s.count('1')
	n_zero = s.count('0')
	for i in range(len(s)):
		eOneCost = min(eOneCost, s.count('1', 0, i + 1) + s.count('0', i + 1, len(s)))
		sOneCost = min(sOneCost, s.count('0', 0, i + 1) + s.count('1', i + 1, len(s)))
	print(min(eOneCost, sOneCost))


from sys import stdin
for t in range(int(input())):
	s = input()
	rt = -1
	lt = -2
	ls = s[0]
	rs = s[-1]
	for i in range(len(s) - 1):
		if s[i] != s[i + 1]:
			lt = i
			break
	for i in range(len(s) - 1, 0, -1):
		if s[i] != s[i - 1]:
			rt = i
			break
	if rt - lt == 1:
		print(0)
	else:
		n = len(s)
		to1 = [0] * (n + 2)
		to0 = [0] * (n + 2)
		for i in range(len(s)):
			if s[i] == '1':
				to0[i + 1] = to0[i] + 1
				to1[i + 1] = to1[i]
			else:
				to1[i + 1] = to1[i] + 1
				to0[i + 1] = to0[i]
		_to1 = [0] * (n + 2)
		_to0 = [0] * (n + 2)
		for i in range(len(s) - 1, -1, -1):
			if s[i] == '1':
				_to0[i + 1] = _to0[i + 2] + 1
				_to1[i + 1] = _to1[i + 2]
			else:
				_to1[i + 1] = _to1[i + 2] + 1
				_to0[i + 1] = _to0[i + 2]
		ans = 99999999999999999999999999
		for i in range(n + 1):
			ans = min(ans, to0[i] + _to1[i + 1], to1[i] + _to0[i + 1])
			ans = min(ans, to0[i] + _to0[i + 1], to1[i] + _to1[i + 1])
		print(ans)


t = int(input())
for i in range(t):
	s = input()
	n = len(s)
	o = 0
	z = 0
	for j in range(n):
		if s[j] == '1':
			o += 1
		else:
			z += 1
	ans = min(o, z)
	n_o = 0
	n_z = 0
	for j in range(n):
		if s[j] == '1':
			n_o += 1
		else:
			n_z += 1
		ans = min(ans, n_o + z - n_z)
		ans = min(ans, n_z + o - n_o)
	print(ans)


for _ in range(int(input())):
	S = input()
	if S.count('1') == len(S) or S.count('0') == len(S):
		print(0)
	else:
		Prefix0 = [0]
		Prefix1 = [0]
		Suffix0 = [0]
		Suffix1 = [0]
		for i in range(len(S)):
			if S[i] == '0':
				Prefix0.append(Prefix0[-1] + 1)
				Prefix1.append(Prefix1[-1])
			else:
				Prefix0.append(Prefix0[-1])
				Prefix1.append(Prefix1[-1] + 1)
		Prefix0 = Prefix0[1:]
		Prefix1 = Prefix1[1:]
		S = S[::-1]
		for i in range(len(S)):
			if S[i] == '0':
				Suffix0.append(Suffix0[-1] + 1)
				Suffix1.append(Suffix1[-1])
			else:
				Suffix0.append(Suffix0[-1])
				Suffix1.append(Suffix1[-1] + 1)
		Suffix0 = Suffix0[:0:-1]
		Suffix1 = Suffix1[:0:-1]
		Min = 10 ** 9
		for i in range(1, len(S)):
			Min = min(Min, Prefix1[i - 1] + Suffix0[i], Prefix0[i - 1] + Suffix1[i])
		Min = min(Min, S.count('1'), S.count('0'))
		print(Min)


import sys
import math
import collections
import heapq
import queue
import itertools
import functools
import operator
import time
readline = sys.stdin.readline
IPS = lambda : readline().rstrip()
IP = lambda : int(readline().rstrip())
MP = lambda : map(int, readline().split())
LS = lambda : list(map(int, readline().split()))

def solve():
	for _ in range(IP()):
		S = IPS()
		(cnt0, cnt1) = (0, 0)
		for char in S:
			cnt0 += char == '0'
		cnt1 = len(S) - cnt0
		ans = min(cnt0, cnt1)
		(pref0, pref1) = (0, 0)
		for char in S:
			if char == '0':
				pref0 += 1
				cnt0 -= 1
			else:
				pref1 += 1
				cnt1 -= 1
			ans = min(ans, pref0 + cnt1)
			ans = min(ans, pref1 + cnt0)
		print(ans)
solve()


t = input()
for _ in range(int(t)):
	s = input()
	length = len(s)
	i = 0
	result = []
	index = 0
	while i < length:
		l = 1
		if i < length - 1 and s[i] == s[i + 1]:
			while i < length - 1 and s[i] == s[i + 1]:
				i += 1
				l += 1
		i += 1
		result.append(l)
		index += 1
	minimum = 10000
	len_result = len(result)
	if len_result < 3:
		print(0)
	else:
		only_one = sum([result[i] for i in range(len_result) if i % 2 == 0])
		only_zero = sum([result[i] for i in range(len_result) if i % 2 == 1])
		minimum = min(only_zero, only_one)
		for i in range(len_result - 1):
			one_zero = sum([result[i] for i in range(i) if i % 2 == 0]) + sum([result[i] for i in range(i + 1, len_result) if i % 2 == 1])
			zero_one = sum([result[i] for i in range(i) if i % 2 == 1]) + sum([result[i] for i in range(i + 1, len_result) if i % 2 == 0])
			minimum = min(minimum, zero_one)
			minimum = min(minimum, one_zero)
		print(minimum)


z = input
mod = 10 ** 9 + 7
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)
for _ in range(int(z())):
	l = list(map(int, z()))
	n = len(l)
	(o, ze, tz, to, ans) = (0, 0, l.count(0), l.count(1), 3455594759655)
	for i in l:
		if i == 1:
			o += 1
		else:
			ze += 1
		ans = min(ans, o + tz - ze, ze + to - o)
	print(ans)


for _ in range(int(input())):
	data = input()
	n = len(data)
	ones = data.count('1')
	zeros = data.count('0')
	result = 1000000
	(a, b) = (0, 0)
	for i in range(n):
		if data[i] == '0':
			a += 1
		else:
			b += 1
		result = min(result, a + ones - b, b + zeros - a)
	print(result)


def getdiff(l1, l2):
	ans = 0
	n = len(l1)
	for i in range(n):
		if l1[i] != l2[i]:
			ans += 1
	return ans

def solve():
	s = [int(v) for v in input()]
	n = len(s)
	cand = [0] * n
	cand1 = [0] * n
	diffwith0 = getdiff(s, cand)
	ans = diffwith0
	diff = diffwith0
	for i in range(n):
		cand[i] = 1
		if cand[i] != s[i]:
			diff += 1
		else:
			diff -= 1
		ans = min(diff, ans)
	diff = diffwith0
	for i in reversed(range(n)):
		cand1[i] = 1
		if cand1[i] != s[i]:
			diff += 1
		else:
			diff -= 1
		ans = min(diff, ans)
	print(ans)
t = int(input())
for _ in range(t):
	solve()


for _ in range(0, int(input())):
	b = input()
	count1 = 0
	count0 = 0
	k = 0
	for i in range(0, len(b)):
		if b[i] == '0':
			count0 += 1
		else:
			count1 += 1
	done_1 = 0
	done_0 = 0
	vals = []
	for i in range(0, len(b)):
		if b[i] == '0':
			done_0 += 1
		else:
			done_1 += 1
		vals.append(done_0 + count1 - done_1)
		vals.append(done_1 + count0 - done_0)
	print(min(vals))


for i in range(int(input())):
	inp = input()
	n = 1000000000
	x = y = 0
	(o, z) = (inp.count('1'), inp.count('0'))
	for j in inp:
		if j == '1':
			x += 1
		else:
			y += 1
		n = min(n, x + z - y, o - x + y)
	print(n)


import math
t = int(input())
cnt = 0
for cnt in range(t):
	s = input()
	cnt_zero = 0
	cnt_one = 0
	start_zero = 0
	start_one = 0
	end_zero = 0
	end_one = 0
	for i in s:
		if i == '0':
			cnt_zero += 1
		else:
			cnt_one += 1
	ans = min(cnt_one, cnt_zero)
	for i in range(len(s)):
		if s[i] == '0':
			start_zero += 1
		else:
			start_one += 1
		end_zero = cnt_zero - start_zero
		end_one = cnt_one - start_one
		temp_1 = start_zero + end_one
		temp_2 = start_one + end_zero
		temp = min(temp_1, temp_2)
		ans = min(ans, temp)
	print(ans)


for _ in range(int(input())):
	s = input()
	n = len(s)
	A = []
	cnt1 = 0
	for i in s:
		cnt1 += i == '1'
		A.append(cnt1)
	ans = min(cnt1, n - cnt1)
	for i in range(n):
		ans = min(ans, min(cnt1 - 2 * A[i] + i + 1, n - cnt1 + 2 * A[i] - i - 1))
	print(ans)


(z, zz) = (input, lambda : list(map(int, z().split())))
fast = z = lambda : stdin.readline().strip()
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = int(z())
for _ in range(num):
	arr = fast()
	res = float('inf')
	total1 = arr.count('1')
	total2 = arr.count('0')
	a1 = 0
	a0 = 0
	for i in arr:
		if i == '1':
			a1 += 1
		else:
			a0 += 1
		res = min(res, total1 - a1 + a0, total2 - a0 + a1)
	print(res)


import sys

def I():
	return list(map(int, input().split()))

def sieve(n):
	a = [1] * n
	for i in range(2, n):
		if a[i]:
			for j in range(i * i, n, i):
				a[j] = 0
	return a

def check(s1, s):
	c = 0
	for i in range(n):
		if s[i] != s1[i]:
			c += 1
	return c
for __ in range(int(input())):
	s = input()
	n = len(s)
	ans = n
	s1 = ['0'] * n
	s2 = ['1'] * n
	c1 = check(s1, s)
	c2 = check(s2, s)
	ans = min(ans, check(s1, s), check(s2, s))
	for i in range(n):
		if s[i] == '1':
			c1 -= 1
			c2 += 1
		else:
			c2 -= 1
			c1 += 1
		ans = min(ans, c1, c2)
	print(ans)


(N,) = list(map(int, input().split()))
for _ in range(N):
	s = input().strip()
	x = [0] * (len(s) + 1)
	y = [0] * (len(s) + 1)
	for i in range(1, len(s) + 1):
		if s[i - 1] == '1':
			x[i] = x[i - 1] + 1
			y[i] = y[i - 1]
		else:
			x[i] = x[i - 1]
			y[i] = y[i - 1] + 1
	r1 = r2 = len(s)
	for i in range(1, len(s) + 1):
		r1 = min(r1, x[i - 1] + (y[-1] - y[i]))
		r2 = min(r2, y[i - 1] + (x[-1] - x[i]))
	print(min(r1, r2))


for _ in range(int(input())):
	s = input()
	parr = [[0, 0] for i in range(len(s))]
	parr[0][int(s[0])] = 1
	sarr = [[0, 0] for i in range(len(s))]
	sarr[-1][int(s[-1])] = 1
	for i in range(1, len(s)):
		parr[i][int(s[i])] = 1
		parr[i][0] = parr[i - 1][0] + parr[i][0]
		parr[i][1] = parr[i - 1][1] + parr[i][1]
	for i in range(len(s) - 2, -1, -1):
		sarr[i][int(s[i])] = 1
		sarr[i][0] = sarr[i + 1][0] + sarr[i][0]
		sarr[i][1] = sarr[i + 1][1] + sarr[i][1]
	if len(s) < 3:
		minimum = 0
	else:
		minimum = 1000000000
	temp = [[0, 0]]
	temp.extend(parr)
	parr = temp[:]
	temp = [[0, 0]]
	temp.extend(sarr)
	sarr = temp[:]
	parr.append([0, 0])
	sarr.append([0, 0])
	for i in range(len(parr) - 1):
		minimum = min(min(parr[i][0] + sarr[i + 1][1], parr[i][1] + sarr[i + 1][0]), minimum)
	print(minimum)


MOD = 10 ** 9 + 7
INT_MAX = 10 ** 20 + 7

def INPUT():
	return list((int(i) for i in input().split()))

def LIST_1D_ARRAY(n):
	return [0 for _ in range(n)]

def LIST_2D_ARRAY(m, n):
	return [[0 for _ in range(n)] for _ in range(m)]

def power(a, x):
	res = 1
	while x > 0:
		if x & 1:
			res = res * a % MOD
		a = a * a % MOD
		x //= 2
	return res % MOD

def NCR_MOD(n, r):
	num = 1
	for i in range(n, n - r, -1):
		num = num * i % MOD
	for j in range(1, r + 1):
		num = num * power(j, MOD - 2) % MOD
	return num
import math
from collections import deque

def good(s):
	if len(s) < 3:
		return True
	if s[0] == '1':
		i = 1
		while i + 1 < len(s):
			if s[i] == '0' and s[i + 1] == '1':
				return False
			i += 1
		return True
	elif s[0] == '0':
		i = 1
		while i + 1 < len(s):
			if s[i] == '1' and s[i + 1] == '0':
				return False
			i += 1
		return True

def calc(s):
	if good(s):
		return 0
	A = LIST_1D_ARRAY(len(s) + 1)
	B = LIST_1D_ARRAY(len(s) + 1)
	for i in range(len(s)):
		if s[i] == '0':
			A[i + 1] = A[i] + 1
		else:
			A[i + 1] = A[i]
	for i in range(len(s)):
		if s[i] == '1':
			B[i + 1] = B[i] + 1
		else:
			B[i + 1] = B[i]
	ans = min(A[-1], B[-1])
	for i in range(len(s)):
		ans = min(ans, B[i] + A[-1] - A[i])
	for i in range(len(s)):
		ans = min(ans, A[i] + B[-1] - B[i])
	return ans
for i in range(int(input())):
	s = input()
	print(calc(s))


for t in range(int(input())):
	string = list(input())
	zero = string.count('0')
	one = string.count('1')
	ans = 10 ** 10
	for i in range(len(string)):
		first = string[:i]
		count_one = first.count('1')
		count_zero = len(first) - count_one
		val = min(count_zero, count_one) + min(one - count_one, zero - count_zero)
		ans = min(ans, val)
	print(ans)


for nt in range(int(input())):
	s = input()
	n = len(s)
	if '1' not in s or '0' not in s:
		print(0)
		continue
	if s[0] == '0':
		pref0 = [1]
		pref1 = [0]
	else:
		pref0 = [0]
		pref1 = [1]
	for i in range(1, n):
		if s[i] == '0':
			pref0.append(pref0[-1] + 1)
			pref1.append(pref1[-1])
		else:
			pref1.append(pref1[-1] + 1)
			pref0.append(pref0[-1])
	ans = min(pref0[-1], pref1[-1])
	for i in range(1, n):
		ans = min(ans, pref1[i - 1] + (pref0[-1] - pref0[i - 1]))
	for i in range(1, n):
		ans = min(ans, pref0[i - 1] + (pref1[-1] - pref1[i - 1]))
	print(ans)


import sys
t = int(input())
for _ in range(t):
	s = input()
	t_ones = s.count('1')
	t_zeros = len(s) - t_ones
	res = 1000000000.0
	ones = 0
	for i in range(len(s)):
		ones += int(s[i])
		zeros = i + 1 - ones
		r_ones = t_ones - ones
		r_zeros = t_zeros - zeros
		res = min(res, ones + r_zeros)
		res = min(res, zeros + r_ones)
	print(res)


for _ in range(int(input())):
	s = input()
	n = 1000000000
	a = b = 0
	ones = s.count('1')
	zeros = s.count('0')
	for ch in s:
		if ch == '1':
			a += 1
		else:
			b += 1
		n = min(n, a + zeros - b, ones - a + b)
	print(n)


t = int(input())
q = []
for _ in range(t):
	s = input()
	ans = len(s)
	zl = [0] * len(s)
	zr = [0] * len(s)
	ol = [0] * len(s)
	or_ = [0] * len(s)
	zl[0] = 1 if s[0] == '0' else 0
	ol[0] = 1 if s[0] == '1' else 0
	zr[-1] = 1 if s[-1] == '0' else 0
	or_[-1] = 1 if s[-1] == '1' else 0
	for i in range(1, len(s)):
		zl[i] = zl[i - 1] + (1 if s[i] == '0' else 0)
		ol[i] = ol[i - 1] + (1 if s[i] == '1' else 0)
	for i in range(len(s) - 2, -1, -1):
		zr[i] = zr[i + 1] + (1 if s[i] == '0' else 0)
		or_[i] = or_[i + 1] + (1 if s[i] == '1' else 0)
	for i in range(len(s) + 1):
		prefo = (0 if i == 0 else zl[i - 1]) + (0 if i == len(s) else or_[i])
		prefz = (0 if i == 0 else ol[i - 1]) + (0 if i == len(s) else zr[i])
		ans = min(ans, prefo, prefz)
	q.append(ans)
print(*q, sep='\n')


for _ in range(int(input())):
	s = input()
	n = len(s)
	ans = s.count('0')
	ans = min(ans, n - ans)
	cc = []
	c = 0
	for x in s:
		c += x == '0'
		cc.append(c)
	for (i, c) in enumerate(cc, 1):
		ans = min(ans, c + n - i - cc[-1] + c)
		ans = min(ans, i - c + cc[-1] - c)
	print(ans)


t = int(input())
while t:
	t -= 1
	s = input()
	sufix_0 = s.count('0')
	sufix_1 = s.count('1')
	prefix_0 = 0
	prefix_1 = 0
	ans = float('+inf')
	for i in s:
		if i == '0':
			prefix_0 += 1
			sufix_0 -= 1
		else:
			prefix_1 += 1
			sufix_1 -= 1
		ans = min(ans, min(prefix_1 + sufix_0, prefix_0 + sufix_1))
	print(ans)


from sys import stdin, stdout
t = int(stdin.readline().strip())
for _ in range(t):
	s = stdin.readline().strip()
	r1 = s.count('1')
	(l0, l1, r0) = (0, 0, len(s) - r1)
	ans = len(s)
	for c in s:
		ans = min(ans, l0 + r1, l1 + r0)
		if c == '1':
			(l1, r1) = (l1 + 1, r1 - 1)
		else:
			(l0, r0) = (l0 + 1, r0 - 1)
	stdout.write('{}\n'.format(ans))


def howManyFlips(s):
	if len(s) <= 1:
		return 0
	zeros = [0 for _ in range(len(s) + 1)]
	ones = [0 for _ in range(len(s) + 1)]
	for i in range(len(s)):
		if s[i] == '0':
			zeros[i + 1] = zeros[i] + 1
			ones[i + 1] = ones[i]
		else:
			zeros[i + 1] = zeros[i]
			ones[i + 1] = ones[i] + 1
	minFlips = 9999999
	for i in range(1, len(s)):
		zerosLeft = zeros[i]
		zerosRight = zeros[-1] - zeros[i]
		onesLeft = ones[i]
		onesRight = ones[-1] - ones[i]
		minFlips = min(minFlips, min(zerosLeft, onesLeft) + min(zerosRight, onesRight))
	return minFlips
t = int(input())
for _ in range(t):
	print(howManyFlips(input()))


from sys import stdin
for case in range(int(stdin.readline())):
	s = stdin.readline().strip()
	ones = s.count('1')
	zeros = s.count('0')
	if zeros == 0 or ones == 0:
		print(0)
		continue
	zeroCount = 0
	oneCount = 0
	low = len(s)
	for ch in s:
		if ch == '1':
			oneCount += 1
		else:
			zeroCount += 1
		low = min(low, zeroCount + ones - oneCount, oneCount + zeros - zeroCount)
	print(low)


import sys
from math import log2, floor, ceil, sqrt
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]
INF = 10 ** 18
MOD = 10 ** 9 + 7
for _ in range(int(ri())):
	s = ri()
	n = len(s)
	pre0 = [0] * (len(s) + 1)
	pre1 = [0] * (len(s) + 1)
	for i in range(len(s)):
		if s[i] == '1':
			pre1[i + 1] = pre1[i] + 1
			pre0[i + 1] = pre0[i]
		else:
			pre0[i + 1] = pre0[i] + 1
			pre1[i + 1] = pre1[i]
	minn = INF
	for i in range(0, len(s) + 1):
		minn = min(minn, pre1[i] + pre0[n] - pre0[i])
	for i in range(0, len(s) + 1):
		minn = min(minn, pre0[i] + pre1[n] - pre1[i])
	print(minn)


def f(s):
	ans = len(s)
	All1 = 0
	All0 = 0
	for i in s:
		if i == '0':
			All0 += 1
		else:
			All1 += 1
	ones = 0
	zeros = 0
	for i in range(len(s)):
		if s[i] == '0':
			zeros += 1
		else:
			ones += 1
		A = ones + All0 - zeros
		B = zeros + All1 - ones
		if ans > A:
			ans = A
		if ans > B:
			ans = B
	return ans
n = int(input())
for i in range(n):
	s = input()
	print(f(s))


for _ in range(int(input())):
	arr = input()
	n = len(arr)
	ans = 10000
	for i in range(n):
		c = arr[:i].count('1') + arr[i:].count('0')
		d = arr[:i].count('0') + arr[i:].count('1')
		ans = min(ans, c, d)
	print(ans)


for _ in range(int(input())):
	s = input()
	zf = s.count('0')
	of = len(s) - zf
	best = min(zf, of)
	ob = zb = 0
	for ch in s:
		if ch == '0':
			zf -= 1
			zb += 1
		else:
			of -= 1
			ob += 1
		best = min(best, zf + ob, of + zb)
	print(best)


t = int(input())
results = []
for case in range(t):
	s = input()
	n = len(s)
	ans = 10 ** 9
	for i in range(n):
		left_z = s[:i].count('0')
		right_o = s[i:].count('1')
		left_o = s[:i].count('1')
		right_z = s[i:].count('0')
		ans = min(ans, left_z + right_o, left_o + right_z)
	results.append(ans)
for result in results:
	print(result)


t = int(input())
for _ in range(t):
	s = input()
	leftones = s.count('1')
	leftzeros = len(s) - leftones
	(rightones, rightzeros) = (0, 0)
	mn = len(s)
	for i in range(len(s)):
		mn = min(leftzeros + rightones, mn)
		mn = min(leftones + rightzeros, mn)
		if s[i] == '1':
			leftones -= 1
			rightones += 1
		if s[i] == '0':
			leftzeros -= 1
			rightzeros += 1
	mn = min(leftzeros + rightones, mn)
	mn = min(leftones + rightzeros, mn)
	print(mn)


from fractions import Fraction
import sys
import bisect
import heapq
from math import *
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
mod = 1000000007

def inp():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def graph(vertex):
	return [[] for i in range(0, vertex + 1)]

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def solve():
	s = inp()
	n = len(s)
	a = []
	for i in range(0, len(s)):
		a.append(int(s[i]))
	pre = []
	for i in range(0, n):
		if i == 0:
			pre.append(a[i])
			continue
		pre.append(pre[i - 1] + a[i])
	ans = 10000
	for i in range(0, n):
		if i == 0:
			cnt1 = n - pre[n - 1]
			cnt2 = pre[n - 1]
			ans = min(ans, cnt1, cnt2)
		elif i == n - 1:
			cnt1 = pre[n - 1]
			cnt2 = n - pre[n - 1]
			cnt3 = pre[n - 2] + (1 - a[n - 1] % 2)
			cnt4 = n - 1 - pre[n - 2] + a[n - 1] % 2
			ans = min(ans, cnt1, cnt2, cnt3, cnt4)
		else:
			cnt1 = n - i - (pre[n - 1] - pre[i - 1]) + pre[i - 1]
			cnt2 = pre[n - 1] - pre[i - 1] + (i - pre[i - 1])
			ans = min(ans, cnt2, cnt1)
	print(ans)
testcase(int(inp()))


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	zero = 0
	one = 0
	for i in range(n):
		if s[i] == '1':
			one += 1
		else:
			zero += 1
	cur_zero = 0
	cur_one = 0
	ans = min(zero, one)
	for i in range(n):
		if s[i] == '1':
			cur_one += 1
		else:
			cur_zero += 1
		ans = min(ans, cur_one + zero - cur_zero, cur_zero + one - cur_one)
	print(ans)


t = int(input())
for i in range(t):
	s = str(input())
	len1 = len(s)
	a = 0
	b = 0
	x = []
	y = []
	for j in range(len1):
		if s[j] == '0':
			a += 1
			x.append(j)
		else:
			y.append(j)
			b += 1
	if a == 0 or b == 0 or x[0] > y[-1] or (y[0] > x[-1]):
		print(0)
	else:
		q = 0
		max1 = 0
		arr = []
		r = 0
		for j in range(len1):
			if s[j] == '0':
				q += 1
				arr.append(r + a - q)
				arr.append(q - 1 + b - r)
			else:
				r += 1
				arr.append(q + b - r)
				arr.append(r - 1 + a - q)
		print(min(min(arr), a, b))


def subsequence_hate(string):
	count_ones = sum((1 for c in string if c == '1'))
	count_zeros = len(string) - count_ones
	ones = 0
	zeros = 0
	onesthenzeroscost = -1
	zerosthenonescost = -1
	best = float('inf')
	for i in range(len(string)):
		if string[i] == '1':
			ones += 1
		else:
			zeros += 1
		onesthenzeroscost = zeros + count_ones - ones
		zerosthenonescost = ones + count_zeros - zeros
		best = min(best, min(onesthenzeroscost, zerosthenonescost))
	return best
for _ in range(int(input())):
	print(subsequence_hate(input()))


t = int(input())
for _ in range(t):
	s = input()
	zL = [0]
	oL = [0]
	total1 = 0
	total0 = 0
	for c in s:
		if c == '0':
			zL.append(zL[-1] + 1)
			oL.append(oL[-1])
		else:
			zL.append(zL[-1])
			oL.append(oL[-1] + 1)
	total0 = zL[-1]
	total1 = oL[-1]
	minCost = 99999999
	for i in range(0, len(s) + 1):
		minCost = min(minCost, zL[i] + (total1 - oL[i]))
		minCost = min(minCost, oL[i] + (total0 - zL[i]))
	print(minCost)


from collections import defaultdict
from sys import stdin, stdout
for _ in range(int(input())):
	st = stdin.readline().strip()
	n = len(st)
	pr0 = [0] * n
	pr1 = [0] * n
	suf0 = [0] * n
	suf1 = [0] * n
	if st[0] == '0':
		pr1[0] = 1
	else:
		pr0[0] = 1
	if st[-1] == '0':
		suf1[-1] = 1
	else:
		suf0[-1] = 1
	for i in range(1, n):
		if st[i] == '0':
			pr1[i] = pr1[i - 1] + 1
			pr0[i] = pr0[i - 1]
		else:
			pr0[i] = pr0[i - 1] + 1
			pr1[i] = pr1[i - 1]
	for i in range(n - 2, -1, -1):
		if st[i] == '0':
			suf1[i] = suf1[i + 1] + 1
			suf0[i] = suf0[i + 1]
		else:
			suf0[i] = suf0[i + 1] + 1
			suf1[i] = suf1[i + 1]
	ans = 10 ** 7
	for i in range(n):
		if st[i] == '0':
			ans = min(ans, pr0[i] + (suf1[i] - 1))
			ans = min(ans, pr1[i] + suf0[i])
		else:
			ans = min(ans, pr0[i] + suf1[i])
			ans = min(ans, pr1[i] + (suf0[i] - 1))
	print(ans)


import io
import os
from collections import Counter, defaultdict, deque

def solve(S):
	S = list(map(int, S))
	pref = [0]
	for x in S:
		pref.append(pref[-1] + x)
	N = len(S)
	best = float('inf')
	for i in range(N + 1):
		numLeft = pref[i]
		numRight = pref[N] - numLeft
		best = min(best, numLeft + (N - i - numRight), i - numLeft + numRight)
	return best
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	S = input().decode().rstrip()
	ans = solve(S)
	print(ans)


t = int(input())
for i in range(t):
	s = input()
	n = len(s)
	ans = n
	o = s.count('0')
	l = s.count('1')
	be0 = 0
	be1 = 0
	af0 = o
	af1 = l
	for i in range(n):
		if s[i] == '0':
			be0 += 1
			af0 -= 1
		else:
			be1 += 1
			af1 -= 1
		ans = min(ans, be0 + af1)
		ans = min(ans, af0 + be1)
	print(ans)


import sys
from os import path
if path.exists('C:/Users/prana/Desktop/sublime/input.txt'):
	sys.stdin = open('C:/Users/prana/Desktop/sublime/input.txt', 'r')
	sys.stdout = open('C:/Users/prana/Desktop/sublime/output.txt', 'w')

def solve():
	s = input()
	s = list(s)
	n = len(s)
	ans = 1000000
	for i in range(n):
		z = 0
		o = 0
		for j in range(n):
			if j >= i:
				if s[j] != '0':
					z += 1
				else:
					o += 1
			elif s[j] != '1':
				z += 1
			else:
				o += 1
		a = min(z, o)
		ans = min(ans, a)
	print(ans)
T = 1
T = int(input())
for _ in range(1, T + 1):
	print('Case #', _, file=sys.stderr)
	solve()


from math import *
from collections import *
from operator import itemgetter
import bisect
from heapq import *
i = lambda : input()
ii = lambda : int(input())
iia = lambda : list(map(int, input().split()))
isa = lambda : list(input().split())
I = lambda : list(map(int, input().split()))
chrIdx = lambda x: ord(x) - 96
idxChr = lambda x: chr(96 + x)
t = ii()
for _ in range(t):
	s = input()
	z = s.count('0')
	o = s.count('1')
	ans = 0
	n = len(s)
	ob = [0] * n
	oa = [0] * n
	zb = [0] * n
	za = [0] * n
	oc = [0] * n
	zc = [0] * n
	zz = 0
	oo = 0
	for i in range(0, n):
		if i > 0:
			ob[i] = oo
			zb[i] = zz
		if s[i] == '0':
			zz += 1
		else:
			oo += 1
		zc[i] = zz
		oc[i] = oo
	zz = 0
	oo = 0
	for i in range(n - 1, -1, -1):
		if i < n - 1:
			oa[i] = oo
			za[i] = zz
		if s[i] == '0':
			zz += 1
		else:
			oo += 1
	ans = min(z, o)
	om = 0
	zm = 0
	for i in range(n):
		ans = min(ans, i + 1 - zc[i] + za[i], i + 1 - oc[i] + oa[i])
	print(ans)


t = int(input())
for ii in range(t):
	s = input().rstrip()
	one = 0
	zero = 0
	dikkathai = 0
	a = []
	n = len(s)
	a.append(s[0])
	flag = 0
	j = 1
	ans = 0
	for i in s:
		if i == '1':
			if zero > 0 and one > 0:
				flag = 1
				break
			one += 1
		else:
			if zero > 0 and one > 0:
				flag = 1
				break
			zero += 1
	if flag == 0:
		print(0)
	else:
		zero = [0] * n
		one = [0] * n
		if s[n - 1] == '0':
			zero[n - 1] = 1
		else:
			one[n - 1] = 1
		for i in range(n - 2, -1, -1):
			if s[i] == '0':
				zero[i] = 1
			if s[i] == '1':
				one[i] = 1
			zero[i] += zero[i + 1]
			one[i] += one[i + 1]
		sol = float('inf')
		sol1 = float('inf')
		o = 0
		z = 0
		for i in range(n - 1):
			if s[i] == '0':
				z += 1
			else:
				o += 1
			sol = min(sol, z + one[i + 1])
			sol1 = min(sol1, o + zero[i + 1])
		print(min(sol, sol1, s.count('1'), s.count('0')))


t = int(input())
while t:
	s = input()
	ones = s.count('1')
	zeroes = s.count('0')
	c_o = 0
	c_z = 0
	ans = 99999999999
	for i in s:
		if i == '1':
			c_o += 1
		else:
			c_z += 1
		a = c_o + zeroes - c_z
		b = c_z + ones - c_o
		ans = min(ans, a, b)
	print(ans)
	t -= 1


import sys, math
for _ in range(int(input())):
	s = list(map(int, list(input())))
	cost = 0
	cost1 = 0
	mi = 10 ** 9
	n = len(s)
	for i in range(n):
		cost = 0
		cost1 = 0
		for j in range(n):
			if j <= i:
				if s[j] == 0:
					cost += 1
				else:
					cost1 += 1
			elif s[j] == 1:
				cost += 1
			else:
				cost1 += 1
		mi = min(mi, min(cost, cost1))
	print(mi)


for _ in [0] * int(input()):
	a = input()
	s = 100000000.0
	c0 = 0
	c1 = 0
	total1 = a.count('1')
	total0 = a.count('0')
	for i in range(len(a)):
		if a[i] == '1':
			c1 += 1
		else:
			c0 += 1
		s = min(s, c0 + total1 - c1, c1 + total0 - c0)
	print(s)


import math
for _ in range(int(input())):
	s = input()
	LeftSum = []
	RigthSum = [0] * len(s)
	sum0 = 0
	sum1 = 0
	for i in range(len(s)):
		if s[i] == '1':
			sum1 += 1
		else:
			sum0 += 1
		LeftSum.append([sum0, sum1])
	sum0 = 0
	sum1 = 0
	for i in range(len(s) - 1, -1, -1):
		if s[i] == '1':
			sum1 += 1
		else:
			sum0 += 1
		RigthSum[i] = [sum0, sum1]
	ans = len(s)
	for i in range(len(s) + 1):
		left = 0
		rigth = 0
		if i - 1 >= 0:
			left = min(LeftSum[i - 1])
		if i != len(s):
			rigth = min(RigthSum[i])
		if left + rigth < ans:
			ans = left + rigth
	print(ans)


t = int(input())
for i in range(t):
	string = list(input())
	if len(string) < 3:
		print(0)
	else:
		zeros = 0
		ones = 0
		min_changes = 2000
		zeros_total = string.count('0')
		ones_total = len(string) - zeros_total
		for i in range(len(string)):
			if string[i] == '0':
				zeros += 1
			else:
				ones += 1
			changes = min(ones + zeros_total - zeros, zeros + ones_total - ones)
			if changes < min_changes:
				min_changes = changes
		print(min_changes)


t = int(input())
for _ in range(t):
	s = input()
	ans = len(s) + 1
	onesLeft = [0] * len(s)
	if s[0] == '1':
		onesLeft[0] = 1
	for i in range(1, len(s)):
		if s[i] == '1':
			onesLeft[i] = 1 + onesLeft[i - 1]
		else:
			onesLeft[i] = onesLeft[i - 1]
	zerosRight = [0] * len(s)
	if s[-1] == '0':
		zerosRight[-1] = 1
	for i in range(len(s) - 2, -1, -1):
		if s[i] == '0':
			zerosRight[i] = 1 + zerosRight[i + 1]
		else:
			zerosRight[i] = zerosRight[i + 1]
	for i in range(len(s)):
		if i == len(s) - 1:
			ans = min(ans, onesLeft[i])
		else:
			ans = min(ans, onesLeft[i] + zerosRight[i + 1])
	zerosLeft = [0] * len(s)
	if s[0] == '0':
		zerosLeft[0] = 1
	for i in range(1, len(s)):
		if s[i] == '0':
			zerosLeft[i] = 1 + zerosLeft[i - 1]
		else:
			zerosLeft[i] = zerosLeft[i - 1]
	onesRight = [0] * len(s)
	if s[-1] == '1':
		onesRight[-1] = 1
	for i in range(len(s) - 2, -1, -1):
		if s[i] == '1':
			onesRight[i] = 1 + onesRight[i + 1]
		else:
			onesRight[i] = onesRight[i + 1]
	for i in range(len(s)):
		if i == len(s) - 1:
			ans = min(ans, zerosLeft[i])
		else:
			ans = min(ans, zerosLeft[i] + onesRight[i + 1])
	print(ans)


for _ in range(int(input())):
	s = input()
	ans = 10000000
	total = 0
	for i in s:
		if i == '1':
			total += 1
	l = len(s)
	ans = min(ans, total, l - total)
	preZeroes = 0
	for i in range(l):
		if s[i] == '0':
			preZeroes += 1
		preOnes = i + 1 - preZeroes
		postOnes = total - preOnes
		postZeroes = l - i - 1 - postOnes
		flip = min(preOnes + postZeroes, postOnes + preZeroes)
		ans = min(ans, flip)
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	a = [0]
	b = [0]
	for i in range(len(s)):
		if s[i] == '1':
			a.append(a[-1] + 1)
		else:
			a.append(a[-1])
		if s[-1 - i] == '0':
			b.append(b[-1] + 1)
		else:
			b.append(b[-1])
	ans = float('INF')
	for (i, j) in zip(a, b[::-1]):
		ans = min(ans, i + j, len(s) - i - j)
	print(ans)


import sys
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def read():
	tc = 0
	if tc:
		input = sys.stdin.readline
	else:
		sys.stdin = open('input1.txt', 'r')
		sys.stdout = open('output1.txt', 'w')

def solve():
	for _ in range(ii()):
		s = si()
		n = len(s)
		if n <= 2:
			print('0')
			continue
		ans = 1000000007
		z = [0] * n
		o = [0] * n
		if s[0] == '0':
			z[0] = 1
		else:
			o[0] = 1
		for i in range(1, n):
			z[i] = z[i - 1]
			o[i] = o[i - 1]
			if s[i] == '1':
				o[i] += 1
			else:
				z[i] += 1
		for i in range(1, n):
			ans = min(ans, z[i - 1] + o[n - 1] - o[i])
			ans = min(ans, o[i - 1] + z[n - 1] - z[i])
		print(ans)
solve()


t = int(input())
for _ in range(t):
	cnt = 0
	summ = 0
	s = str(input())
	n = len(s)
	for i in s:
		if i == '0':
			cnt += 1
		else:
			summ += 1
	ans = min(n - cnt, n - summ)
	(o, e) = (0, 0)
	for i in s:
		if i == '0':
			o += 1
		else:
			e += 1
		ans = min(ans, o + summ - e)
	(o, e) = (0, 0)
	for i in s:
		if i == '0':
			o += 1
		else:
			e += 1
		ans = min(ans, e + cnt - o)
	print(ans)


import os
os_input = os.read(0, int(10000000.0)).split()
os_input_pos = -1
answer_list = []

def read_s():
	global os_input_pos
	os_input_pos += 1
	return os_input[os_input_pos].decode()

def read_i():
	return int(read_s())

def write_s(v):
	answer_list.append(v)

def write_i(v):
	write_s(str(v))

def print_ans():
	os.write(1, '\n'.join(answer_list).encode())
	os.write(1, '\n'.encode())
T = read_i()
while T:
	T -= 1
	s = read_s()
	r_one = s.count('1')
	r_zero = s.count('0')
	l_one = 0
	l_zero = 0
	ans = len(s)
	for c in s:
		if c == '1':
			r_one -= 1
			l_one += 1
		else:
			r_zero -= 1
			l_zero += 1
		ans = min(ans, len(s) - max(l_one + r_zero, l_zero + r_one))
	write_i(ans)
print_ans()


t = int(input())
for i in range(t):
	s = input()
	c0 = s.count('0')
	c1 = s.count('1')
	x1 = 0
	y1 = 0
	l = []
	m = []
	for j in s:
		l.append(y1 + c0)
		m.append(x1 + c1)
		if j == '0':
			x1 += 1
			c0 -= 1
		else:
			y1 += 1
			c1 -= 1
	print(min(min(l), min(m)))


count = int(input())

def sum(l, i):
	l01 = l[i] + (len(l) - l[-1]) - (i + 1 - l[i])
	l10 = i + 1 - l[i] + (l[-1] - l[i])
	return min(l01, l10)
for i in range(count):
	line = input()
	l = [0] * len(line)
	for (i, s) in enumerate(line):
		if i == 0:
			l[0] = 1 if s == '0' else 0
		else:
			l[i] = l[i - 1] + 1 if s == '0' else l[i - 1]
	ar = [sum(l, j) for j in range(len(l))]
	print(min(ar))


for t in range(int(input())):
	s = input()
	(ones, zeros, ans) = (0, 0, float('inf'))
	for ch in s:
		if ch == '1':
			ones += 1
	zeros = len(s) - ones
	enc = 0
	for i in range(len(s)):
		enc += s[i] == '1'
		(flip0, flip1) = (enc + zeros - (i + 1 - enc), i + 1 - enc + ones - enc)
		ans = min(ans, flip0, flip1)
	print(ans)


tests = int(input())
for _ in range(tests):
	n = input()
	i = 0
	num_1 = n.count('1')
	num_0 = n.count('0')
	num1_and_remain0 = []
	num0_and_remain1 = []
	number_1 = 0
	number_0 = 0
	for i in n:
		if i == '1':
			number_1 += 1
		else:
			number_0 += 1
		num0_and_remain1.append(number_0 + num_1 - number_1)
		num1_and_remain0.append(number_1 + num_0 - number_0)
	print(min(min(num0_and_remain1), min(num1_and_remain0)))


t = int(input())
for _ in range(t):
	s = input()
	ct = float('inf')
	pref_0 = 0
	pref_1 = 0
	suffix_0 = s.count('0')
	suffix_1 = s.count('1')
	for i in range(len(s)):
		if s[i] == '0':
			pref_0 += 1
			suffix_0 -= 1
		else:
			pref_1 += 1
			suffix_1 -= 1
		ct = min(ct, min(pref_1 + suffix_0, pref_0 + suffix_1))
	print(ct)


T = int(input())

def process(x):
	n = len(x)
	pre_zero = [0 for i in range(n)]
	suf_zero = [0 for i in range(n)]
	pre_one = [0 for i in range(n)]
	suf_one = [0 for i in range(n)]
	if x[0] == '0':
		pre_zero[0] = 1
	else:
		pre_one[0] = 1
	for i in range(1, n):
		pre_zero[i] = pre_zero[i - 1]
		pre_one[i] = pre_one[i - 1]
		if x[i] == '0':
			pre_zero[i] += 1
		else:
			pre_one[i] += 1
	if x[-1] == '0':
		suf_zero[-1] = 1
	else:
		suf_one[-1] = 1
	for i in range(n - 2, -1, -1):
		suf_zero[i] = suf_zero[i + 1]
		suf_one[i] = suf_one[i + 1]
		if x[i] == '0':
			suf_zero[i] += 1
		else:
			suf_one[i] += 1
	ans = n
	for i in range(n - 1):
		ans = min([ans, pre_one[i] + suf_zero[i + 1], pre_zero[i] + suf_one[i + 1]])
	ans = min([ans, pre_one[-1], pre_zero[-1]])
	return ans
for t in range(T):
	s = input()
	n = len(s)
	if n <= 2:
		print(0)
	else:
		print(process(s))


for i in range(int(input())):
	a = input()
	if len(a) == 1:
		print(0)
	else:
		if a[0] == '1':
			d = [0, 1, 1, 1]
		else:
			d = [1, 0, 0, 0]
		for v in a[1:]:
			(p, n) = (int(v), (int(v) + 1) % 2)
			d1 = [d[0] + n, min(d[:2]) + p, min(d[2:]) + n, d[3] + p]
			d = d1
		print(min(d))


T = int(input())
for isc in range(T):
	l = list(map(int, list(input())))
	s = sum(l)
	t = len(l)
	(m1, m2, m3, m4, num1, num0) = (t - s, s, t, t, 0, 0)
	for i in range(t):
		k = l[i]
		if k == 1:
			num1 += 1
			m3 = min(m3, i + 1 - num1 + (s - num1))
			m4 = min(m4, i + 1 - num0 + (t - s - num0))
		else:
			num0 += 1
			m3 = min(m3, i + 1 - num1 + (s - num1))
			m4 = min(m4, i + 1 - num0 + (t - s - num0))
	print(min(m1, m2, m3, m4))


t = int(input())
while t:
	t += -1
	s = input()
	n = len(s)
	l1 = []
	l2 = []
	cnt = 0
	for i in range(n - 1, -1, -1):
		l1.append(cnt)
		if s[i] == '1':
			cnt -= -1
	l1.reverse()
	cnt = 0
	for i in range(n):
		l2.append(cnt)
		if s[i] == '0':
			cnt -= -1
	ans = 999999999999999
	for i in range(n):
		tmp = l1[i] + l2[i]
		if s[i] == '0':
			tmp -= -1
		ans = min(ans, tmp)
	l1 = []
	l2 = []
	cnt = 0
	for i in range(n - 1, -1, -1):
		l1.append(cnt)
		if s[i] == '0':
			cnt -= -1
	l1.reverse()
	cnt = 0
	for i in range(n):
		l2.append(cnt)
		if s[i] == '1':
			cnt -= -1
	for i in range(n):
		tmp = l1[i] + l2[i]
		if s[i] == '1':
			tmp -= -1
		ans = min(ans, tmp)
	print(ans)


tc = int(input().strip())
while tc:
	arr = list(map(int, list(input().strip())))
	n = len(arr)
	ones = sum(arr)
	zeros = n - ones
	ans = 9999
	pones = 0
	pzeros = 0
	for i in range(n):
		if arr[i] == 0:
			pzeros += 1
		else:
			pones += 1
		tans10 = pones + (zeros - pzeros)
		tans01 = pzeros + (ones - pones)
		ans = min([ans, tans01, tans10])
	print(ans)
	tc -= 1


import sys, math
Tests = int(input())
for _ in range(Tests):
	s = input()
	n = len(s)
	x = []
	y = []
	xx = []
	yy = []
	(z, o) = (0, 0)
	zero = 0
	one = 0
	for i in range(n):
		if s[i] == '0':
			zero += 1
			yy.append(zero)
			xx.append(one)
		else:
			one += 1
			yy.append(zero)
			xx.append(one)
	zero = 0
	one = 0
	for i in range(n - 1, -1, -1):
		if s[i] == '0':
			zero += 1
			y.append(zero)
			x.append(one)
		else:
			one += 1
			y.append(zero)
			x.append(one)
	x.reverse()
	y.reverse()
	ans = []
	for i in range(0, n - 1):
		ans.append(min(yy[i], xx[i]) + min(x[i + 1], y[i + 1]))
	ans.append(min(y[0], x[0]))
	print(min(ans))


t = int(input())
for _ in range(t):
	s = list(input())
	(suf0, suf1) = (0, 0)
	for i in s:
		suf0 += 1 if i == '0' else 0
		suf1 += 1 if i == '1' else 0
	(pre0, pre1) = (0, 0)
	ans = min(suf0, suf1)
	for i in s:
		pre0 += 1 if i == '0' else 0
		suf0 -= 1 if i == '0' else 0
		pre1 += 1 if i == '1' else 0
		suf1 -= 1 if i == '1' else 0
		ans = min(ans, pre1 + suf0)
		ans = min(ans, pre0 + suf1)
	print(ans)


import math
t = int(input())
for i in range(t):
	s = input()
	n = len(s)
	minimum = n
	a = [0] * n
	b = [0] * n
	current = 0
	for j in range(n):
		if s[j] == '1':
			current += 1
		a[j] = current
	current = 0
	for j in range(n):
		if s[n - 1 - j] == '0':
			current += 1
		b[n - 1 - j] = current
	for j in range(n):
		minimum = min(minimum, a[j] + b[j])
	current = 0
	for j in range(n):
		if s[j] == '0':
			current += 1
		a[j] = current
	current = 0
	for j in range(n):
		if s[n - 1 - j] == '1':
			current += 1
		b[n - 1 - j] = current
	for j in range(n):
		minimum = min(minimum, a[j] + b[j])
	print(minimum - 1)


for _ in range(int(input())):
	s = input()
	n = len(s)
	cnt = [0, 0]
	for i in s:
		cnt[int(i)] += 1
	ans = max(cnt)
	pref = cnt[:]
	for i in s:
		if i == '1':
			pref[0] += 1
			pref[1] -= 1
		if i == '0':
			pref[0] -= 1
			pref[1] += 1
		ans = min(ans, pref[0])
		ans = min(ans, pref[1])
	print(ans)


def check0(s):
	return s.count('0')

def check1(s):
	return s.count('1')
t_cases = int(input())
for t in range(t_cases):
	word = input()
	count0 = word.count('0')
	count1 = word.count('1')
	n = len(word)
	my_list = []
	for i in range(n):
		j = i + 1
		done0 = check0(word[:j])
		done1 = check1(word[:j])
		for1 = done0 + count1 - done1
		for0 = done1 + count0 - done0
		my_list.append(min(for0, for1))
	print(min(my_list))


t = int(input())
for i in range(t):
	(s, f) = (input(), 0)
	n = len(s)
	for i in s:
		if i == '0':
			f = f + 1
	m = min(n - f, f)
	if s[0] == '0':
		k = 1
	else:
		k = 0
	for i in range(1, len(s)):
		m = min(m, i - k + (f - k), k + n - i - f + k)
		if s[i] == '0':
			k = k + 1
	print(m)


t = int(input())
for i in range(t):
	s = input()
	ones = 0
	zeros = 0
	for j in s:
		if j == '1':
			ones += 1
		else:
			zeros += 1
	seen_ones = 0
	seen_zeros = 0
	ans = min(zeros, ones)
	for j in s:
		if j == '1':
			seen_ones += 1
			ones -= 1
		else:
			seen_zeros += 1
			zeros -= 1
		ans_zo = seen_ones + zeros
		ans_oz = seen_zeros + ones
		ans = min(ans, min(ans_oz, ans_zo))
	print(ans)


import os
import sys
from io import BytesIO, IOBase

def readvars():
	k = map(int, input().split())
	return k

def readlist():
	li = list(map(int, input().split()))
	return li

def anslist(li):
	ans = ' '.join([str(v) for v in li])
	return ans

def main():
	t = 1
	t = int(input())
	for xx in range(t):
		s = input()
		n = len(s)
		pref1 = [0] * n
		suf1 = [0] * n
		pref0 = [0] * n
		suf0 = [0] * n
		if s[0] == '1':
			pref1[0] = 1
		else:
			pref0[0] = 1
		if s[-1] == '1':
			suf1[-1] = 1
		else:
			suf0[-1] = 1
		for i in range(1, n):
			pre = 0
			if s[i] == '1':
				pre = 1
			pref1[i] = pref1[i - 1] + pre
			pref0[i] = pref0[i - 1] + (1 - pre)
			suf = 0
			if s[n - i - 1] == '1':
				suf = 1
			suf1[n - i - 1] = suf1[n - i] + suf
			suf0[n - i - 1] = suf0[n - i] + (1 - suf)
		minmo = min(pref1[-1], pref0[-1])
		for i in range(0, n - 1):
			minmo = min(minmo, pref0[i] + suf1[i + 1], pref1[i] + suf0[i + 1], pref1[i] + suf1[i + 1], pref0[i] + suf1[i + 1])
		print(minmo)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from sys import stdin, stdout

def main():
	t = int(stdin.readline())
	for _ in range(t):
		s = stdin.readline().strip()
		n = len(s)
		c = 0
		l = [0]
		sn = [0]
		i = -1
		for i in range(n - 1):
			if s[i] == s[i + 1]:
				c = c + 1
			else:
				l.append(c + 1)
				sn.append(s[i])
				c = 0
		l.append(c + 1)
		sn.append(s[i + 1])
		l.append(0)
		sn.append(0)
		at = s.count('1')
		bt = s.count('0')
		a = [at]
		b = [0]
		c = at
		d = 0
		for i in range(len(l)):
			if sn[i] == '1':
				c = a[-1] - l[i]
			else:
				d = b[-1] + l[i]
			a.append(c)
			b.append(d)
		a.append(0)
		b.append(bt)
		arr = 1000000000
		for i in range(0, len(l) + 2):
			arr = min(arr, a[i] + b[i])
		n = len(s)
		c = 0
		l = [0]
		sn = [0]
		i = -1
		for i in range(n - 1):
			if s[i] == s[i + 1]:
				c = c + 1
			else:
				l.append(c + 1)
				sn.append(s[i])
				c = 0
		l.append(c + 1)
		sn.append(s[i + 1])
		l.append(0)
		sn.append(0)
		at = s.count('0')
		bt = s.count('1')
		a = [at]
		b = [0]
		c = at
		d = 0
		for i in range(len(l)):
			if sn[i] == '0':
				c = a[-1] - l[i]
			else:
				d = b[-1] + l[i]
			a.append(c)
			b.append(d)
		a.append(0)
		b.append(bt)
		arr1 = 1000000000
		for i in range(0, len(l) + 2):
			arr1 = min(arr1, a[i] + b[i])
		print(min(arr, arr1))
main()


ans = []
for _ in range(int(input())):
	a = list(map(int, list(input())))
	u = []
	k = 1
	for i in range(1, len(a)):
		if a[i] == a[i - 1]:
			k += 1
		else:
			u.append(k)
			k = 1
	u.append(k)
	n = len(u)
	s1 = [0] * n
	s2 = [0] * n
	s1[0] = u[0]
	for i in range(1, n):
		s1[i] += s1[i - 1]
		s2[i] += s2[i - 1]
		if i % 2 == 0:
			s1[i] += u[i]
		else:
			s2[i] += u[i]
	ansi = len(a)
	for i in range(n):
		ansi = min(ansi, s1[i] + s2[-1] - s2[i], s2[i] + s1[-1] - s1[i])
	ans.append(ansi)
print('\n'.join(map(str, ans)))


import sys
input = sys.stdin.readline
for f in range(int(input())):
	s = input()
	n1 = 0
	n0 = 0
	for i in s:
		if i == '0':
			n0 += 1
		if i == '1':
			n1 += 1
	s = s[0:n0 + n1]
	mn = n0
	b1 = 0
	b0 = 0
	for i in s:
		if i == '0':
			n0 -= 1
		else:
			n1 -= 1
		m1 = b1 + n0
		m2 = b0 + n1
		if i == '0':
			b0 += 1
		else:
			b1 += 1
		mn = min(mn, m1, m2)
	print(mn)


import sys, bisect, string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby
rep = range

def Golf():
	(n, *t) = map(int, open(0).read().split())

def I():
	return int(input())

def S_():
	return input()

def IS():
	return input().split()

def LS():
	return [i for i in input().split()]

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def RA():
	return map(int, open(0).read().split())

def GI(V, E, ls=None, Directed=False, index=1):
	org_inp = []
	g = [[] for i in range(V)]
	FromStdin = True if ls == None else False
	for i in range(E):
		if FromStdin:
			inp = LI()
			org_inp.append(inp)
		else:
			inp = ls[i]
		if len(inp) == 2:
			(a, b) = inp
			c = 1
		else:
			(a, b, c) = inp
		if index == 1:
			a -= 1
			b -= 1
		aa = (a, c)
		bb = (b, c)
		g[a].append(bb)
		if not Directed:
			g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}, boundary=1):
	mp = [boundary] * (w + 2)
	found = {}
	for i in range(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [boundary] + [mp_def[j] for j in s] + [boundary]
	mp += [boundary] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def accum(ls):
	rt = [0]
	for i in ls:
		rt += [rt[-1] + i]
	return rt

def bit_combination(n, base=2):
	rt = []
	for tb in range(base ** n):
		s = [tb // base ** bt % base for bt in range(n)]
		rt += [s]
	return rt

def gcd(x, y):
	if y == 0:
		return x
	if x % y == 0:
		return y
	while x % y != 0:
		(x, y) = (y, x % y)
	return y

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
YN = ['YES', 'NO']
Yn = ['Yes', 'No']
mo = 10 ** 9 + 7
inf = float('inf')
FourNb = [(-1, 0), (1, 0), (0, 1), (0, -1)]
EightNb = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
compas = dict(zip('WENS', FourNb))
cursol = dict(zip('LRUD', FourNb))
l_alp = string.ascii_lowercase
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
input = lambda : sys.stdin.readline().rstrip()

class Tree:

	def __init__(self, inp_size=None, ls=None, init=True, index=0):
		self.LCA_init_stat = False
		self.ETtable = []
		if init:
			if ls == None:
				self.stdin(inp_size, index=index)
			else:
				self.size = len(ls) + 1
				(self.edges, _) = GI(self.size, self.size - 1, ls, index=index)
		return

	def stdin(self, inp_size=None, index=1):
		if inp_size == None:
			self.size = int(input())
		else:
			self.size = inp_size
		(self.edges, _) = GI(self.size, self.size - 1, index=index)
		return

	def listin(self, ls, index=0):
		self.size = len(ls) + 1
		(self.edges, _) = GI(self.size, self.size - 1, ls, index=index)
		return

	def __str__(self):
		return str(self.edges)

	def dfs(self, x, func=lambda prv, nx, dist: prv + dist, root_v=0):
		q = deque()
		q.append(x)
		v = [-1] * self.size
		v[x] = root_v
		while q:
			c = q.pop()
			for (nb, d) in self.edges[c]:
				if v[nb] == -1:
					q.append(nb)
					v[nb] = func(v[c], nb, d)
		return v

	def EulerTour(self, x):
		q = deque()
		q.append(x)
		self.depth = [None] * self.size
		self.depth[x] = 0
		self.ETtable = []
		self.ETdepth = []
		self.ETin = [-1] * self.size
		self.ETout = [-1] * self.size
		cnt = 0
		while q:
			c = q.pop()
			if c < 0:
				ce = ~c
			else:
				ce = c
				for (nb, d) in self.edges[ce]:
					if self.depth[nb] == None:
						q.append(~ce)
						q.append(nb)
						self.depth[nb] = self.depth[ce] + 1
			self.ETtable.append(ce)
			self.ETdepth.append(self.depth[ce])
			if self.ETin[ce] == -1:
				self.ETin[ce] = cnt
			else:
				self.ETout[ce] = cnt
			cnt += 1
		return

	def LCA_init(self, root):
		self.EulerTour(root)
		self.st = SparseTable(self.ETdepth, init_func=min, init_idl=inf)
		self.LCA_init_stat = True
		return

	def LCA(self, root, x, y):
		if self.LCA_init_stat == False:
			self.LCA_init(root)
		(xin, xout) = (self.ETin[x], self.ETout[x])
		(yin, yout) = (self.ETin[y], self.ETout[y])
		a = min(xin, yin)
		b = max(xout, yout, xin, yin)
		id_of_min_dep_in_et = self.st.query_id(a, b + 1)
		return self.ETtable[id_of_min_dep_in_et]

class SparseTable:

	def __init__(self, ls, init_func=min, init_idl=float('inf')):
		self.func = init_func
		self.idl = init_idl
		self.size = len(ls)
		self.N0 = self.size.bit_length()
		self.table = [ls[:]]
		self.index = [list(range(self.size))]
		self.lg = [0] * (self.size + 1)
		for i in range(2, self.size + 1):
			self.lg[i] = self.lg[i >> 1] + 1
		for i in range(self.N0):
			tmp = [self.func(self.table[i][j], self.table[i][min(j + (1 << i), self.size - 1)]) for j in range(self.size)]
			tmp_id = [self.index[i][j] if self.table[i][j] == self.func(self.table[i][j], self.table[i][min(j + (1 << i), self.size - 1)]) else self.index[i][min(j + (1 << i), self.size - 1)] for j in range(self.size)]
			self.table += [tmp]
			self.index += [tmp_id]

	def query(self, l, r):
		if r > self.size:
			r = self.size
		N = self.lg[r - l]
		return self.func(self.table[N][l], self.table[N][max(0, r - (1 << N))])

	def query_id(self, l, r):
		if r > self.size:
			r = self.size
		N = self.lg[r - l]
		(a, b) = (self.index[N][l], self.index[N][max(0, r - (1 << N))])
		if self.table[0][a] == self.func(self.table[N][l], self.table[N][max(0, r - (1 << N))]):
			b = a
		return b

	def right_bound(self, pos, x):
		k = (self.size - pos).bit_length()
		for j in range(k)[::-1]:
			nx = pos + (1 << j)
			if nx < n and self.query(pos, nx + 1) <= x:
				pos += 1 << j
		return pos

	def left_bound(self, pos, x):
		k = pos.bit_length()
		for j in range(k)[::-1]:
			nx = pos - (1 << j)
			if 0 <= nx and self.query(nx, pos + 1) <= x:
				pos -= 1 << j
		return pos

	def __str__(self):
		return str(self.table[0])

	def print(self):
		for i in self.table:
			print(*i)

def primeFactor(N):
	(i, n) = (2, N)
	ret = {}
	(d, sq) = (2, 99)
	while i <= sq:
		k = 0
		while n % i == 0:
			(n, k, ret[i]) = (n // i, k + 1, k + 1)
		if k > 0 or i == 97:
			sq = int(n ** (1 / 2) + 0.5)
		if i < 4:
			i = i * 2 - 1
		else:
			(i, d) = (i + d, d ^ 6)
	if n > 1:
		ret[n] = 1
	return ret

def PrimeNumSet(N):
	Max = int(N ** 0.5)
	seachList = [i for i in range(2, N + 1)]
	primeNum = []
	while seachList:
		if seachList[0] <= Max:
			break
		primeNum.append(seachList[0])
		tmp = seachList[0]
		seachList = [i for i in seachList if i % tmp != 0]
	primeNum.extend(seachList)
	return primeNum

def LCM(b, mo=10 ** 9 + 7):
	prs = PrimeNumSet(max(b))
	M = dict(zip(prs, [0] * len(prs)))
	for i in b:
		dc = primeFactor(i)
		for (j, k) in dc.items():
			M[j] = max(M[j], k)
	r = 1
	for (j, k) in M.items():
		if k != 0:
			r *= pow(j, k, mo)
			r %= mo
	return r

def topological_sort(G):
	n = len(G)
	rt = []
	indeg = [0] * n
	for i in range(n):
		for j in G[i]:
			indeg[j] += 1
	q = deque()
	for i in range(n):
		if indeg[i] == 0:
			q.append(i)
	while q:
		c = q.popleft()
		rt.append(c)
		for nb in G[c]:
			indeg[nb] -= 1
			if indeg[nb] == 0:
				q.append(nb)
	return rt
show_flg = False
show_flg = True
ans = 0
T = I()
for _ in range(T):
	ans = 0
	s = input()
	n = len(s)
	p = [0]
	l = [0]
	for i in s:
		if i == '0':
			p += [p[-1] + 1]
			l += [l[-1] + 0]
		else:
			p += [p[-1] + 0]
			l += [l[-1] + 1]
	ans = n
	zero = p[-1]
	one = l[-1]
	for i in range(n + 1):
		alt_01 = l[i] + zero - p[i]
		alt_10 = p[i] + one - l[i]
		alt = min(alt_01, alt_10)
		if alt < ans:
			ans = alt
	print(ans)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for t in range(int(input())):
	s = input()
	n = len(s)
	zero = [0] * n
	one = [0] * n
	o = 0
	z = 0
	if s[0] == '1':
		one[0] = 1
		o += 1
	else:
		zero[0] = 1
		z += 1
	for i in range(1, n):
		if s[i] == '0':
			zero[i] = zero[i - 1] + 1
			one[i] = one[i - 1]
			z += 1
		else:
			one[i] = one[i - 1] + 1
			zero[i] = zero[i - 1]
			o += 1
	m = n
	for i in range(n):
		if s[i] == '1':
			c1 = zero[i] + o - one[i]
			m = min(m, c1)
			c2 = one[i] - 1 + z - zero[i]
			m = min(m, c1)
		else:
			c1 = one[i] + z - zero[i]
			m = min(m, c1)
			c2 = zero[i] - 1 + o - one[i]
			m = min(m, c1)
	if n < 3:
		m = 0
	print(m)


testcases = int(input())
for testcase in range(testcases):
	strs = input()
	n = len(strs)
	revstrs = strs[::-1]
	freq = [(0, 0)] * n
	count0 = 0
	count1 = 0
	index = n
	for i in revstrs:
		index -= 1
		if i == '1':
			count1 += 1
		else:
			count0 += 1
		freq[index] = (count0, count1)
	cur0 = 0
	cur1 = 0
	suf0 = 0
	suf1 = 0
	mins = min(count0, count1)
	index = 0
	count0 = 0
	count1 = 0
	for i in strs:
		temp = freq[index]
		suf0 = temp[0]
		suf1 = temp[1]
		change10 = count0 + suf1
		change01 = count1 + suf0
		mins = min(change01, change10, mins)
		index += 1
		if i == '1':
			count1 += 1
		else:
			count0 += 1
	print(mins)


t = int(input())
while t:
	t -= 1
	s = input()
	one = [0] * len(s)
	zero = [] * len(s)
	a = []
	for i in s:
		a.append(int(i))
	swap1 = [0] * len(a)
	swap0 = [0] * len(a)
	rswap1 = [0] * len(a)
	rswap0 = [0] * len(a)
	for i in range(len(a)):
		if a[i] == 0:
			swap1[i] += 1
		if a[i] == 1:
			swap0[i] += 1
	for i in range(len(a) - 1, -1, -1):
		if a[i] == 0:
			rswap1[len(a) - i - 1] += 1
		if a[i] == 1:
			rswap0[len(a) - 1 - i] += 1
	for i in range(1, len(a)):
		swap1[i] += swap1[i - 1]
		swap0[i] += swap0[i - 1]
		rswap1[i] += rswap1[i - 1]
		rswap0[i] += rswap0[i - 1]
	mini1 = len(a)
	for i in range(len(a) - 1):
		if swap1[i] + rswap0[len(a) - i - 2] < mini1:
			mini1 = swap1[i] + rswap0[len(a) - i - 2]
	mini2 = len(a)
	for i in range(len(a) - 1):
		if swap0[i] + rswap1[len(a) - 2 - i] < mini2:
			mini2 = swap0[i] + rswap1[len(a) - 2 - i]
	if len(a) == 1:
		print(0)
	else:
		print(min(mini1, mini2, swap0[-1], swap1[-1]))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	s = input()
	n = len(s)
	if len(set(s)) == 1:
		print(0)
		continue
	zeros_pref = [0] * n
	ones_pref = [0] * n
	zeros_suff = [0] * n
	ones_suff = [0] * n
	zeros_pref[0] = int(s[0] == '0')
	ones_pref[0] = int(s[0] == '1')
	zeros_suff[-1] = int(s[-1] == '0')
	ones_suff[-1] = int(s[-1] == '1')
	for i in range(1, n):
		zeros_pref[i] = zeros_pref[i - 1] + int(s[i] == '0')
		ones_pref[i] = ones_pref[i - 1] + int(s[i] == '1')
	for i in range(n - 2, -1, -1):
		zeros_suff[i] = zeros_suff[i + 1] + int(s[i] == '0')
		ones_suff[i] = ones_suff[i + 1] + int(s[i] == '1')
	(ans1, ans2) = (n, n)
	for i in range(n):
		curr_cost1 = ones_pref[i] + zeros_suff[min(n - 1, i + 1)]
		ans1 = min(ans1, curr_cost1)
		curr_cost2 = zeros_pref[i] + ones_suff[min(n - 1, i + 1)]
		ans2 = min(ans2, curr_cost2)
	print(min(ans1, ans2))


for _ in range(int(input())):
	s = list(input())
	n = len(s)
	pre = [[0] * 2 for i in range(n + 2)]
	for i in range(n):
		if s[i] == '0':
			pre[i][0] = pre[i - 1][0] + 1
			pre[i][1] = pre[i - 1][1]
		else:
			pre[i][0] = pre[i - 1][0]
			pre[i][1] = pre[i - 1][1] + 1
	pre[n][0] = pre[n - 1][0]
	pre[n][1] = pre[n - 1][1]
	ans = 100000000000
	for i in range(n + 1):
		an1 = pre[n][1] - pre[i - 1][1] + pre[i - 1][0]
		ans = min(an1, ans, n - an1)
	print(ans)


for _ in range(int(input())):
	n = list(input())
	ll = len(n)
	if len(n) < 2:
		print(0)
		continue
	count1 = [0] * len(n)
	if n[0] == '1':
		count1[0] = 1
	for i in range(1, ll):
		count1[i] = (1 if n[i] == '1' else 0) + count1[i - 1]
	minans = count1[ll - 1]
	minans = min(minans, ll - count1[ll - 1])
	for i in range(ll):
		curr = count1[i] + (ll - i - 1 + count1[i] - count1[ll - 1])
		minans = min(minans, curr)
	for i in range(ll):
		curr = i + 1 - count1[i] + (count1[ll - 1] - count1[i])
		minans = min(minans, curr)
	print(minans)


t = int(input())
for _ in range(t):
	s = input()
	(num0, num1) = (0, 0)
	x = [[0, 0]]
	y = []
	ans = 0
	for i in range(len(s)):
		if s[i] == '0':
			num0 += 1
		else:
			num1 += 1
		x.append([num0, num1])
	ans = min(num0, num1)
	for i in range(len(s)):
		if s[i] == '0':
			num0 -= 1
		else:
			num1 -= 1
		y.append([num0, num1])
	y.append([0, 0])
	for i in range(len(x)):
		ans = min(ans, x[i][0] + y[i][1])
		ans = min(ans, x[i][1] + y[i][0])
	print(ans)


for t in range(int(input())):
	lst = list(input())
	n = len(lst)
	ans0 = [0] * n
	anspost0 = [0] * n
	ans1 = [0] * n
	anspost1 = [0] * n
	ans1[0] = int(lst[0])
	anspost0[n - 1] = 1 - int(lst[n - 1])
	ans0[0] = 1 - int(lst[0])
	anspost1[n - 1] = int(lst[n - 1])
	for i in range(1, n):
		nm = int(i)
		ans0[i] = ans0[i - 1] + 1 - int(lst[i])
		ans1[i] = ans1[i - 1] + int(lst[i])
	for i in range(n - 2, -1, -1):
		anspost0[i] = anspost0[i + 1] + 1 - int(lst[i])
		anspost1[i] = anspost1[i + 1] + int(lst[i])
	minans = min(anspost1[0], anspost0[0])
	for i in range(1, n):
		minans = min(minans, ans0[i - 1] + anspost1[i], ans1[i - 1] + anspost0[i])
	print(minans)


t = int(input())
while t != 0:
	t = t - 1
	s = input()
	sufone = 0
	sufzero = 0
	for i in range(0, len(s)):
		if s[i] == '0':
			sufzero = sufzero + 1
		else:
			sufone = sufone + 1
	prefzero = 0
	prefone = 0
	ans = 10000
	for i in range(0, len(s)):
		if s[i] == '0':
			prefzero = prefzero + 1
			sufzero = sufzero - 1
		else:
			prefone = 1 + prefone
			sufone = sufone - 1
		ans = min(ans, prefone + sufzero)
		ans = min(ans, prefzero + sufone)
	print(ans)


def solve():
	s = input()
	zeroSum = [0]
	oneSum = [0]
	for c in s:
		if c == '0':
			zeroSum.append(zeroSum[-1] + 1)
			oneSum.append(oneSum[-1])
		else:
			zeroSum.append(zeroSum[-1])
			oneSum.append(oneSum[-1] + 1)
	res = float('inf')
	for i in range(len(s)):
		(zero, one) = (zeroSum[i], oneSum[-1] - oneSum[i])
		res = min(res, zero + one, i - zero + len(s) - i - one)
	print(res)
for _ in range(int(input())):
	solve()


def mismatch(s, t):
	count = 0
	for i in range(len(s)):
		if s[i] != t[i]:
			count += 1
	return count
for I in range(int(input())):
	s = input()
	l = len(s)
	zeros = s.count('0')
	ones = s.count('1')
	zero = 0
	one = 0
	res = 100000055
	for char in s:
		if char == '0':
			zero += 1
		else:
			one += 1
		res = min(res, zero + ones - one)
		res = min(res, one + zeros - zero)
	print(res)


from sys import stdin, exit
input = stdin.readline

def i():
	return input()

def ii():
	return int(input())

def iis():
	return map(int, input().split())

def liis():
	return list(map(int, input().split()))

def print_array(a):
	print(' '.join(map(str, a)))
t = ii()
for _ in range(t):
	s = input().strip()
	qnt = []
	(ones, zeros) = (0, 0)
	for i in s:
		qnt.append([zeros, ones, 0, 0, i])
		if i == '1':
			ones += 1
		else:
			zeros += 1
	(zeros, ones) = (0, 0)
	idx = len(qnt) - 1
	for i in s[::-1]:
		qnt[idx][2] = zeros
		qnt[idx][3] = ones
		if i == '1':
			ones += 1
		else:
			zeros += 1
		idx -= 1
	mini = float('inf')
	for i in qnt:
		mini = min(mini, i[0] + i[3], i[1] + i[2])
		if i[4] == '1':
			mini = min(mini, i[0] + i[2])
		else:
			mini = min(mini, i[1] + i[3])
	print(mini)


import math
t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	zc = [0 for i in range(n)]
	oc = [0 for i in range(n)]
	ans = 0
	i = 0
	j = 0
	for i in range(n):
		if i == 0:
			if s[i] == '1':
				oc[i] = 1
			else:
				oc[i] = 0
		elif s[i] == '1':
			oc[i] = oc[i - 1] + 1
		else:
			oc[i] = oc[i - 1]
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			if s[i] == '0':
				zc[i] = 1
			else:
				zc[i] = 0
		elif s[i] == '0':
			zc[i] = zc[i + 1] + 1
		else:
			zc[i] = zc[i + 1]
	ans1 = 10 ** 17
	ans2 = 10 ** 17
	for i in range(n):
		ans1 = min(ans1, i - oc[i] + (n - i) - zc[i])
	s = s[::-1]
	for i in range(n):
		if i == 0:
			if s[i] == '1':
				oc[i] = 1
			else:
				oc[i] = 0
		elif s[i] == '1':
			oc[i] = oc[i - 1] + 1
		else:
			oc[i] = oc[i - 1]
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			if s[i] == '0':
				zc[i] = 1
			else:
				zc[i] = 0
		elif s[i] == '0':
			zc[i] = zc[i + 1] + 1
		else:
			zc[i] = zc[i + 1]
	for i in range(n):
		ans2 = min(ans2, i - oc[i] + (n - i) - zc[i])
	print(min(ans1, ans2))


for _ in range(int(input())):
	a = input().strip()
	(b, c) = ([0], [])
	(B, C) = (0, 0)
	for i in a:
		if i == '1':
			B += 1
		b.append(B)
	for i in range(len(a) - 1, -1, -1):
		if a[i] == '0':
			C += 1
		c.append(C)
	c = c[::-1]
	c.append(0)
	D = []
	for i in range(len(c)):
		D.append(b[i] + c[i])
	min1 = min(D)
	(b, c) = ([0], [])
	(B, C) = (0, 0)
	for i in a:
		if i == '0':
			B += 1
		b.append(B)
	for i in range(len(a) - 1, -1, -1):
		if a[i] == '1':
			C += 1
		c.append(C)
	c = c[::-1]
	c.append(0)
	D = []
	for i in range(len(c)):
		D.append(b[i] + c[i])
	min2 = min(D)
	print(min(min1, min2))


for s in [*open(0)][1:]:
	print(min((min(s[:i].count('0') + s[i:].count('1'), s[:i].count('1') + s[i:].count('0')) for i in range(len(s)))))


t = int(input())
for i in range(t):
	binary = input()
	dp = []
	for i in range(4):
		dp.append([0] * (len(binary) + 1))
	current = binary[0]
	i = 0
	continuous_count = 0
	j = 1
	while i < len(binary):
		while i < len(binary) and binary[i] == current:
			i += 1
			continuous_count += 1
		if current == '0':
			dp[0][j] = dp[0][j - 1]
			dp[1][j] = dp[1][j - 1] + continuous_count
			dp[2][j] = dp[2][j - 1] + continuous_count
			dp[3][j] = min(dp[3][j - 1], dp[1][j - 1])
		else:
			dp[0][j] = dp[0][j - 1] + continuous_count
			dp[1][j] = dp[1][j - 1]
			dp[2][j] = min(dp[2][j - 1], dp[0][j - 1])
			dp[3][j] = dp[3][j - 1] + continuous_count
		if i >= len(binary):
			break
		j += 1
		current = binary[i]
		continuous_count = 0
	print(min(dp[0][j], dp[1][j], dp[2][j], dp[3][j]))


for tt in range(int(input())):
	strng = input()
	zero = [strng.count('0')]
	one = [strng.count('1')]
	for i in range(len(strng)):
		if strng[i] == '0':
			zero.append(zero[-1] - 1)
			one.append(one[-1] + 1)
		else:
			zero.append(zero[-1] + 1)
			one.append(one[-1] - 1)
	print(min(min(zero), min(one)))


from collections import deque
from sys import stdin, stdout
import math
input = stdin.readline
for _ in range(int(input())):
	x = list(input().strip())
	n = len(x)
	res = 999999999
	left = [[0, 0] for i in range(n)]
	right = [[0, 0] for i in range(n)]
	for i in range(n):
		if i > 0:
			left[i][0] = left[i - 1][0]
			left[i][1] = left[i - 1][1]
		if x[i] == '1':
			left[i][0] += 1
		else:
			left[i][1] += 1
	for i in range(n - 1, -1, -1):
		if i < n - 1:
			right[i][0] = right[i + 1][0]
			right[i][1] = right[i + 1][1]
		if x[i] == '1':
			right[i][0] += 1
		else:
			right[i][1] += 1
	for i in range(n):
		first = left[i][0] + right[i][1] - 1
		second = left[i][1] + right[i][0] - 1
		res = min(res, first, second)
	print(res)


from sys import stdin, stdout
import sys
t = int(stdin.readline().strip())
for _ in range(t):
	s = list(map(int, stdin.readline().strip()))
	t1 = 0
	t0 = 0
	p1 = [0]
	p0 = [0]
	ans = sys.maxsize
	for i in s:
		p1.append(p1[-1] + i)
		p0.append(p0[-1] + (i ^ 1))
		t1 += i
		t0 += i ^ 1
	p1.pop(0)
	p0.pop(0)
	for i in range(len(s)):
		tn1 = p1[i]
		tn0 = p0[i]
		rm1 = t1 - tn1
		rm0 = t0 - tn0
		c1 = tn0 + rm1
		c2 = tn1 + rm0
		ans = min(min(ans, c1), c2)
	stdout.write(str(ans) + '\n')


t = int(input())
for _ in range(t):
	s = input()
	l = len(s)
	acum1 = [0]
	acum2 = [0]
	for i in range(l):
		if s[i] == '0':
			acum1.append(acum1[-1] + 1)
			acum2.append(acum2[-1])
		else:
			acum1.append(acum1[-1])
			acum2.append(acum2[-1] + 1)
	ans = 10 ** 18
	for i in range(l + 1):
		tmp1 = acum1[i] - acum1[0] + (acum2[l] - acum2[i])
		tmp2 = acum2[i] - acum2[0] + (acum1[l] - acum1[i])
		ans = min(ans, tmp1, tmp2)
	print(ans)


from collections import Counter
cases = int(input())
for t in range(cases):
	s = input()
	cc = Counter(s)
	ls = len(s)
	ones = [int(s[0] == '1')]
	zeros = [int(s[0] == '0')]
	for i in range(1, ls):
		ones.append(int(s[i] == '1') + ones[-1])
		zeros.append(int(s[i] == '0') + zeros[-1])
	output = 1001
	for i in range(ls):
		otill = ones[i]
		ztill = zeros[i]
		v1 = ztill + cc['1'] - otill
		v2 = otill + cc['0'] - ztill
		output = min(v1, v2, output)
	print(output)


t = int(input())

def solve(str):
	mn = 99999
	for i in range(len(str) + 1):
		to_zeros = 0
		to_ones = 0
		for j in range(len(str)):
			if j < i:
				if str[j] == '1':
					to_zeros += 1
				else:
					to_ones += 1
			elif str[j] == '1':
				to_ones += 1
			else:
				to_zeros += 1
		mn = min(mn, to_ones, to_zeros)
	return mn
while t > 0:
	str = input()
	print(solve(str))
	t -= 1


def f(s):
	a = [int(i) for i in s]
	total = 0
	t = []
	for i in a:
		total += i
		t.append(total)
	minimum = 10 ** 18
	for i in range(len(t)):
		num0 = i + 1 - t[i]
		num1 = t[i]
		other1 = t[-1] - t[i]
		other0 = len(t) - (i + 1) - other1
		m = max(num0 + other1, other0 + num1)
		minimum = min(minimum, len(t) - m)
	return minimum
t = int(input())
for i in range(t):
	s = input()
	print(f(s))


t = int(input())
for i in range(t):
	arr = list(map(int, list(input())))
	ans = 30000
	n = len(arr)
	sum1 = sum(arr)
	left_ones = 0
	for i in range(n):
		left_ones += arr[i]
		left_zeros = i + 1 - left_ones
		right_ones = sum1 - left_ones
		right_zeros = n - i - 1 - right_ones
		flips = min(left_zeros + right_ones, right_zeros + left_ones)
		ans = min(flips, ans)
	print(ans)


for _ in range(int(input())):
	n = input()
	ans = 0
	if len(n) < 3:
		ans = 0
	else:
		zero = n.count('0')
		ones = n.count('1')
		curr_ones = 0
		curr_zero = 0
		ans = min(zero, ones)
		for i in n:
			if i == '1':
				curr_ones += 1
			else:
				curr_zero += 1
			ans = min(ones - curr_ones + curr_zero, ans)
			ans = min(ans, curr_ones + zero - curr_zero)
	print(ans)


def function(s):
	total_1 = s.count(1)
	total_0 = s.count(0)
	curr_1 = 0
	curr_0 = 0
	ans = min(total_1, total_0)
	for i in s:
		if i == 1:
			curr_1 += 1
		else:
			curr_0 += 1
		ans = min(curr_1 + (total_0 - curr_0), curr_0 + (total_1 - curr_1), ans)
	print(ans)
T = int(input())
for i in range(0, T):
	s = list(map(int, input()))
	function(s)


import sys
input = lambda : sys.stdin.readline().strip('\r\n')
for _ in range(int(input())):
	s = input()
	one = 0
	zero = 0
	for i in range(len(s)):
		if s[i] == '1':
			one += 1
		else:
			zero += 1
	cur_one = 0
	cur_zero = 0
	ans = len(s)
	for i in range(len(s)):
		if s[i] == '1':
			cur_one += 1
		else:
			cur_zero += 1
		ans = min(ans, one - cur_one + cur_zero)
		ans = min(ans, zero - cur_zero + cur_one)
	print(ans)


for _ in range(int(input())):
	s = list(map(str, input()))
	n = len(s)
	cnt = 0
	ans = 10 ** 10
	f = 0
	val1 = [0 for i in range(n)]
	val2 = [0 for i in range(n)]
	for i in range(n):
		if i == 0:
			if s[i] == '0':
				val1[i] += 1
			if s[i] == '1':
				val2[i] += 1
		elif s[i] == '0':
			val1[i] = val1[i - 1] + 1
			val2[i] = val2[i - 1]
		else:
			val2[i] = val2[i - 1] + 1
			val1[i] = val1[i - 1]
	for i in range(n - 1):
		z = val2[i]
		one = val1[n - 1] - val1[i]
		ans = min(ans, z + one)
	for i in range(n - 1):
		z = val1[i]
		one = val2[n - 1] - val2[i]
		ans = min(ans, z + one)
	ans = min(ans, val2[n - 1])
	ans = min(ans, val1[n - 1])
	print(ans)


import collections as cc
I = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	s = input()
	le = len(s)
	a = s.count('1')
	b = s.count('0')
	x = y = 0
	an = 10 ** 9
	if le < 3:
		print(0)
	else:
		for i in range(le):
			if s[i] == '1':
				x += 1
			else:
				y += 1
			r1 = a - x
			r0 = b - y
			an = min(x + r0, y + r1, an)
		print(an)


def num(ar, ind1, ind2):
	return ar[ind2] - ar[ind1]
t = int(input())
for i in range(t):
	s = input()
	n = len(s)
	ans = n
	num0 = [0] * (n + 2)
	for i in range(n):
		num0[i + 1] = num0[i] + int(s[i] == '0')
	num0[-1] = num0[-2]
	for i in range(n + 1):
		num00 = num(num0, 0, i)
		num01 = i - num00
		num10 = num(num0, i, n + 1)
		num11 = n + 1 - (i + 1) - num10
		ans = min(ans, min(num10, num11) + min(num00, num01))
	print(ans)


T = int(input())
for _ in range(T):
	s = input()
	total = len(s)
	total_one = len(list(filter(lambda x: x == '1', s)))
	total_zero = total - total_one
	one = zero = 0
	res = total
	for i in range(total):
		res = min(res, total_one - one + zero, total_zero - zero + one)
		if s[i] == '1':
			one += 1
		else:
			zero += 1
	print(res)


import os
import sys
from io import BytesIO, IOBase
import math
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value(s):
	z = [0] * len(s)
	o = [0] * len(s)
	for i in range(len(s)):
		if i:
			z[i] = z[i - 1]
		if s[i] == '0':
			z[i] += 1
	for i in range(len(s) - 1, -1, -1):
		if i != len(s) - 1:
			o[i] = o[i + 1]
		if s[i] == '1':
			o[i] += 1
	ans = min(o[0], z[len(s) - 1])
	for i in range(len(s) - 1):
		ans = min(ans, o[i + 1] + z[i])
	return ans
for _ in range(int(input())):
	s = input()
	val1 = value(s)
	ns = ''
	for i in range(len(s)):
		if s[i] == '0':
			ns = ns + '1'
		else:
			ns = ns + '0'
	val2 = value(ns)
	print(min(val1, val2))


t = int(input())
for i in range(t):
	s = input()
	l0 = 0
	l1 = 0
	r0 = 0
	r1 = 0
	for j in s:
		if j == '1':
			r1 += 1
		else:
			r0 += 1
	f0 = l1 + r0
	f1 = l0 + r1
	for j in s:
		if j == '1':
			l1 += 1
			r1 -= 1
		else:
			l0 += 1
			r0 -= 1
		f0 = min(f0, l1 + r0)
		f1 = min(f1, l0 + r1)
	print(min(f0, f1))


def get_left(integral, pos):
	return integral[pos - 1] - integral[0]

def get_right(integral, pos):
	return integral[len(integral) - 1] - integral[pos]
tests = int(input())
for i in range(tests):
	line = input()
	k = len(line)
	integral = [0]
	add = 0
	for x in range(k):
		if line[x] == '1':
			add += 1
		integral.append(add)
	integral.append(add)
	min01 = k
	min10 = k
	for x in range(1, k + 1):
		res10 = x - 1 - get_left(integral, x) + get_right(integral, x)
		if res10 < min10:
			min10 = res10
		res01 = get_left(integral, x) + (k - x) - get_right(integral, x)
		if res01 < min01:
			min01 = res01
	if min01 > min10:
		print(min10)
	else:
		print(min01)


import sys
import heapq
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
for i in range(int(input())):
	s = input()
	(q, w) = (0, 0)
	for j in s:
		if j == '1':
			q += 1
	w = len(s) - q
	(l, k) = (0, 0)
	e = len(s)
	for j in s:
		if j == '1':
			k += 1
		else:
			l += 1
		e = min(e, k + w - l, l + q - k)
	print(e)


def tc():
	s = input()
	ztot = 0
	otot = 0
	for ch in s:
		if ch == '0':
			ztot += 1
		else:
			otot += 1
	ans = min(ztot, otot)
	pref0 = 0
	pref1 = 0
	for ch in s:
		if ch == '0':
			pref0 += 1
		else:
			pref1 += 1
		ans = min(ans, pref1 + ztot - pref0, pref0 + otot - pref1)
	return ans
T = int(input())
for _ in range(T):
	ans = tc()
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	ones = [0] * len(s)
	ones[0] = int(s[0])
	for x in range(1, len(s)):
		ones[x] = ones[x - 1] + int(s[x])
	best = len(s)
	if len(s) == 1:
		best = 0
	for x in range(len(s) - 1):
		flips = 0
		flips += min(ones[x], x + 1 - ones[x])
		flips += min(ones[-1] - ones[x], len(s) - (x + 1) - (ones[-1] - ones[x]))
		best = min(best, flips)
	print(best)


def check(s):
	n = len(s)
	result = n
	total_ones = 0
	total_zeroes = 0
	for i in s:
		if i == '1':
			total_ones += 1
		else:
			total_zeroes += 1
	done_ones = 0
	done_zeroes = 0
	for i in s:
		if i == '1':
			done_ones += 1
		else:
			done_zeroes += 1
		result = min(result, done_zeroes + total_ones - done_ones)
		result = min(result, done_ones + total_zeroes - done_zeroes)
	return result
t = int(input())
while t:
	s = input()
	print(check(s))
	t -= 1


import math
t = int(input())
for tt in range(t):
	s = input().strip()
	zeros = 0
	ones = 0
	for i in s:
		if i == '1':
			ones += 1
		else:
			zeros += 1
	pref0 = 0
	pref1 = 0
	ans = min(ones, zeros)
	for i in s:
		if i == '1':
			pref1 += 1
			ones -= 1
		else:
			zeros -= 1
			pref0 += 1
		ans = min(ans, pref1 + zeros)
		ans = min(ans, pref0 + ones)
	print(ans)


for t in range(int(input())):
	s = input()
	pref = [[0, 0] for i in range(len(s) + 1)]
	suff = [[0, 0] for i in range(len(s) + 1)]
	for i in range(len(s) - 1, -1, -1):
		suff[i][0] = suff[i + 1][0] + (s[i] == '0')
		suff[i][1] = suff[i + 1][1] + (s[i] == '1')
	for i in range(len(s)):
		pref[i + 1][0] = pref[i][0] + (s[i] == '0')
		pref[i + 1][1] = pref[i][1] + (s[i] == '1')
	ans = len(s)
	for i in range(len(s) + 1):
		ans = min(ans, pref[i][0] + suff[i][1], pref[i][1] + suff[i][0])
	print(ans)


from collections import Counter

def solve():
	S = input()
	C = Counter(S)
	ans = min(C['0'], C['1'])
	N = len(S)
	zerobef = 0
	onebef = 0
	zeroaf = C['0']
	oneaf = C['1']
	for s in S:
		if s == '0':
			zerobef += 1
			zeroaf -= 1
		else:
			onebef += 1
			oneaf -= 1
		now = min(zerobef + oneaf, onebef + zeroaf)
		if now < ans:
			ans = now
	return ans

def main():
	T = int(input())
	ANS = []
	for _ in range(T):
		ANS.append(solve())
	print('\n'.join(map(str, ANS)))
main()


t = int(input())
for _ in range(t):
	s = list(map(int, input()))
	total_zeros = 0
	total_ones = 0
	done_zeros = 0
	done_ones = 0
	for i in range(len(s)):
		if s[i] == 0:
			total_zeros += 1
		else:
			total_ones += 1
	ans = 1000000000
	for i in range(len(s)):
		if s[i] == 0:
			done_zeros += 1
		else:
			done_ones += 1
		ans = min(ans, done_zeros + total_ones - done_ones, done_ones + total_zeros - done_zeros)
	print(ans)


from sys import stdin, exit, setrecursionlimit
from collections import deque
from heapq import _heappop_max, heappush, heappop
from math import gcd
from math import ceil, log2
setrecursionlimit(10000000)
from collections import deque
input = stdin.readline
lmi = lambda : list(map(int, input().split()))
mi = lambda : map(int, input().split())
si = lambda : input().strip('\n')
ssi = lambda : input().strip('\n').split()
for _ in range(int(input())):
	s = si()
	psa = [0 for i in range(len(s) + 1)]
	for (idx, i) in enumerate(s):
		if i == '1':
			psa[idx + 1] += 1
		psa[idx + 1] += psa[idx]
	ans = float('inf')
	for i in range(len(s) + 1):
		a = psa[-1] - psa[i]
		b = psa[i]
		ans = min(ans, i - b + a, len(s) - i - a + b)
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	t0 = s.count('0')
	t1 = s.count('1')
	ans = min(t0, t1)
	(curr0, curr1) = (0, 0)
	for ch in s:
		if ch == '0':
			curr0 += 1
			t0 -= 1
		if ch == '1':
			curr1 += 1
			t1 -= 1
		ans = min(ans, curr1 + t0)
		ans = min(ans, curr0 + t1)
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	s = input().strip('\n')
	s1 = '010'
	s2 = '101'
	n = len(s)
	ans = float('inf')
	cz = []
	co = [0] * (n + 1)
	c = 0
	for i in range(n):
		if s[i] != '0':
			c += 1
		cz.append(c)
	c = 0
	for i in range(n - 1, -1, -1):
		if s[i] != '1':
			c += 1
		co[i] = c
	for i in range(n):
		ans = min(ans, cz[i] + co[i + 1])
	cz = []
	co = [0] * (n + 1)
	c = 0
	for i in range(n):
		if s[i] != '1':
			c += 1
		cz.append(c)
	c = 0
	for i in range(n - 1, -1, -1):
		if s[i] != '0':
			c += 1
		co[i] = c
	for i in range(n):
		ans = min(ans, cz[i] + co[i + 1])
	print(ans)


rw = int(input())
for ewqr in range(rw):
	s = input()
	n = len(s)
	p = []
	t = 0
	for i in s:
		if i == '1':
			t += 1
		p.append(t)
	minm = 1000000000000
	for i in range(n):
		d0 = i - p[i] + 1
		d1 = p[i]
		p0 = n - p[n - 1] - d0
		p1 = p[n - 1] - p[i]
		m1 = d0 + p1
		m2 = d1 + p0
		m3 = d0 + p0
		m4 = d1 + p1
		minm = min(minm, m1, m2, m3, m4)
	print(minm)


def check_good_str(bin_str):
	tot_0 = 0
	tot_1 = 0
	for i in bin_str:
		if i == '0':
			tot_0 += 1
		else:
			tot_1 += 1
	ans = min(tot_1, tot_0)
	done_0 = 0
	done_1 = 0
	for i in bin_str:
		if i == '0':
			done_0 += 1
			tot_0 -= 1
			ans = min(ans, done_1 + tot_0)
		else:
			done_1 += 1
			tot_1 -= 1
			ans = min(ans, done_0 + tot_1)
	return ans
l = []
for i in range(int(input())):
	l.append(check_good_str(input()))
print(*l, sep='\n')


t = int(input())
while t > 0:
	a = list(input())
	n1 = n0 = 0
	for i in a:
		if i == '0':
			n0 += 1
		else:
			n1 += 1
	nl1 = nl0 = 0
	nr0 = n0
	nr1 = n1
	ans = 1001
	temp = 0
	for i in a:
		if i == '0':
			nl0 += 1
			nr0 -= 1
			if nl0 + nr1 < nl1 + nr0:
				temp = nl0 + nr1
			else:
				temp = nl1 + nr0
		else:
			nl1 += 1
			nr1 -= 1
			if nl0 + nr1 < nl1 + nr0:
				temp = nl0 + nr1
			else:
				temp = nl1 + nr0
		if temp < ans:
			ans = temp
	print(ans)
	t -= 1


from sys import stdin, gettrace

def main():

	def solve():
		ss = input()
		n = len(ss)
		ocll = [0]
		for i in range(n):
			ocll.append(ocll[-1] + (1 if ss[i] == '1' else 0))
		best = n
		for i in range(n + 1):
			ocl = ocll[i]
			zcl = i - ocl
			ocr = ocll[n] - ocl
			zcr = n - i - ocr
			os = zcl + ocr
			zs = ocl + zcr
			best = min(best, os, zs)
		print(best)
	q = int(input())
	for _ in range(q):
		solve()
main()


for _ in range(int(input())):
	s = input()
	tz = s.count('0')
	to = len(s) - tz
	mini = min(tz, to)
	cz = 0
	co = 0
	for i in range(len(s)):
		if s[i] == '0':
			cz += 1
		else:
			co += 1
		leftz = tz - cz
		lefto = to - co
		fis = leftz + co
		sec = lefto + cz
		mini = min(mini, fis, sec)
	print(mini)


[t] = [int(i) for i in input().split()]
for _ in range(t):
	s = input()
	zeros = [0 for _ in range(len(s))]
	ones = [0 for _ in range(len(s))]
	if s[0] == '0':
		zeros[0] = 1
	else:
		ones[0] = 1
	for i in range(1, len(s)):
		if s[i] == '0':
			zeros[i] = zeros[i - 1] + 1
			ones[i] = ones[i - 1]
		else:
			zeros[i] = zeros[i - 1]
			ones[i] = ones[i - 1] + 1
	best = len(s)
	best = min(best, ones[len(s) - 1])
	best = min(best, zeros[len(s) - 1])
	for i in range(len(s) - 1):
		best = min(best, ones[i] + (zeros[len(s) - 1] - zeros[i]))
		best = min(best, zeros[i] + (ones[len(s) - 1] - ones[i]))
	print(best)


for _ in range(int(input())):
	s = input()
	n = len(s)
	suf = [0] * (n + 1)
	s1 = s.count('1')
	mi = 10000000000
	mi2 = 10000000000
	for i in range(1, n + 1):
		if s[i - 1] == '1':
			suf[i] += suf[i - 1] + 1
		else:
			suf[i] = suf[i - 1]
		mi = min(mi, s1 - suf[i] + i - suf[i])
		mi2 = min(mi2, suf[i] + n - i - (s1 - suf[i]))
	print(min(mi, mi2))


t = int(input())

def checkGood(s):
	for i in range(1, len(s)):
		if s[i] == '1':
			if s[:i].count('0') != 0 and s[i + 1:].count('0') != 0:
				return 0
		if s[i] == '0':
			if s[:i].count('1') != 0 and s[i + 1:].count('1') != 0:
				return 0
	return 1
for tt in range(0, t):
	arr = input().rstrip()
	ret = []
	if checkGood(arr) == 0:
		all1 = arr.count('0')
		all0 = arr.count('1')
		for i in range(0, len(arr)):
			(n0_l, n1_l) = (arr[:i].count('0'), arr[:i].count('1'))
			(n0_r, n1_r) = (arr[i:].count('0'), arr[i:].count('1'))
			n0_l_n1_r = n0_l + n1_r
			n1_l_n0_r = n1_l + n0_r
			ret.append(min(n0_l_n1_r, n1_l_n0_r))
		print(min(min(ret), all1, all0))
	else:
		print(0)


def main():
	t = int(input())
	for _ in range(t):
		s = input()
		n = len(s)
		pref0 = pref1 = 0
		preflist = [[], []]
		for i in range(n):
			if s[i] == '0':
				pref0 += 1
			preflist[0].append(pref0)
		for j in range(n - 1, -1, -1):
			if s[j] == '1':
				pref1 += 1
			preflist[1].append(pref1)
		m = min(pref0, pref1)
		for k in range(n - 1):
			total = preflist[0][k]
			total += preflist[1][n - k - 2]
			m = min(m, total)
			other = k + 1 - preflist[0][k]
			other += n - k - 1 - preflist[1][n - k - 2]
			m = min(m, other)
		print(m)
main()


def main():
	tc = int(input())
	while tc != 0:
		tc -= 1
		s = input()
		(one, zro) = (0, 0)
		for i in s:
			if i == '1':
				one += 1
			else:
				zro += 1
		a = min(one, zro)
		(o, z) = (0, 0)
		for i in s:
			if i == '1':
				o += 1
			else:
				z += 1
			a = min(a, o + zro - z, z + one - o)
		print(a)
main()


for _ in range(int(input())):
	s = input()
	pref0 = pref1 = cnt0 = cnt1 = 0
	for b in s:
		cnt0 += b == '0'
		cnt1 += b == '1'
	ans = min(cnt0, cnt1)
	for b in s:
		pref0 += b == '0'
		cnt0 -= b == '0'
		pref1 += b == '1'
		cnt1 -= b == '1'
		ans = min(ans, pref1 + cnt0, pref0 + cnt1)
	print(ans)


from sys import stdin, stdout
for i in range(int(input())):
	s = input()
	l = len(s)
	(c1, c0) = (0, 0)
	for i in s:
		if i == '0':
			c0 += 1
		else:
			c1 += 1
	ans = l
	x0 = 0
	x1 = 0
	for i in s:
		if i == '0':
			x0 += 1
		else:
			x1 += 1
		ans = min(x1 + c0 - x0, x0 + c1 - x1, ans)
	ans = min(ans, c1, c0)
	print(ans)


def solve(s):
	n = len(s)
	ans = 1000000
	if n <= 2:
		return 0
	f0 = 0
	l0 = s.count('0')
	f1 = 0
	l1 = n - l0
	for i in range(len(s)):
		if s[i] == '0':
			f0 += 1
			l0 -= 1
		if s[i] == '1':
			f1 += 1
			l1 -= 1
		ans = min(ans, f0 + l1, f1 + l0, f1 + l1, f0 + l0)
	return ans
t = int(input())
for i in range(t):
	s = input()
	print(solve(s))


num_inputs = int(input())
for c in range(num_inputs):
	string = input()
	converted_string = []
	temp_char = string[0]
	temp_length = 0
	for char in string:
		if char == temp_char:
			temp_length += 1
		else:
			temp_char = char
			converted_string.append(temp_length)
			temp_length = 1
	converted_string.append(temp_length)
	min_flips = 100000
	flip_counter_odd = 0
	flip_counter_even = 0
	back_odd = 0
	back_even = 0
	for index in range(len(converted_string)):
		if index % 2 == 0:
			back_even += converted_string[index]
		else:
			back_odd += converted_string[index]
	front_even = 0
	front_odd = 0
	for index in range(len(converted_string)):
		if index % 2 == 0:
			front_even += converted_string[index]
			back_even -= converted_string[index]
		else:
			front_odd += converted_string[index]
			back_odd -= converted_string[index]
		flip_counter_even = front_odd + back_even
		flip_counter_odd = front_even + back_odd
		if flip_counter_odd < min_flips:
			min_flips = flip_counter_odd
		if flip_counter_even < min_flips:
			min_flips = flip_counter_even
	print(min_flips)


import sys
import string
input = sys.stdin.readline
t = int(input())
for test in range(t):
	s = str(input())
	s = s.rstrip('\n')
	s_0 = [0] * len(s)
	s_1 = [0] * len(s)
	if s[0] == '0':
		s_0[0] = 1
	else:
		s_1[0] = 1
	for i in range(1, len(s)):
		if s[i] == '0':
			s_0[i] = s_0[i - 1] + 1
			s_1[i] = s_1[i - 1]
		else:
			s_1[i] = s_1[i - 1] + 1
			s_0[i] = s_0[i - 1]
	ans = s_0[len(s) - 1]
	for i in range(1, len(s)):
		l_0 = s_0[i - 1]
		l_1 = s_1[i - 1]
		r_0 = s_0[len(s) - 1] - s_0[i - 1]
		r_1 = s_1[len(s) - 1] - s_1[i - 1]
		temp = min(l_0 + r_1, l_1 + r_0)
		if temp < ans:
			ans = temp
	final = min(ans, s_0[len(s) - 1], s_1[len(s) - 1])
	print(final)


for s in [*open(0)][1:]:
	n = m = len(s) - 1
	x = s.count('1')
	for c in s[:-1]:
		m = min(m, x, n - x)
		x += 1 - 2 * int(c)
	print(m)


t = int(input())
for _ in range(t):
	s = input()
	zeros_before = []
	ones_before = []
	zeros_cnt = 0
	ones_cnt = 0
	for el in s:
		if el == '0':
			zeros_cnt += 1
		else:
			ones_cnt += 1
		zeros_before.append(zeros_cnt)
		ones_before.append(ones_cnt)
	min_dist = []
	for (z_b, o_b) in zip(zeros_before, ones_before):
		z_a = zeros_cnt - z_b
		o_a = ones_cnt - o_b
		min_dist.append(min(z_b + o_a, z_a + o_b))
	print(min(min_dist))


t = int(input())
for test in range(t):
	s = input()
	n = len(s)
	zeros = 0
	ones = 0
	for i in range(n):
		if s[i] == '0':
			zeros += 1
		else:
			ones += 1
	min_zero_one = 10 ** 9
	min_one_zero = 10 ** 9
	prev_zero = 0
	prev_one = 0
	next_zero = zeros
	next_one = ones
	for i in range(n):
		if s[i] == '0':
			prev_zero += 1
			next_zero -= 1
		else:
			prev_one += 1
			next_one -= 1
		temp_zero_one = prev_one + next_zero
		temp_one_zero = prev_zero + next_one
		min_zero_one = min(min_zero_one, temp_zero_one)
		min_one_zero = min(min_one_zero, temp_one_zero)
	print(min(min_one_zero, min_zero_one))


t = int(input())
count = 0
while count < t:
	count += 1
	string = input()
	vals = []
	currentSum = 0
	lastVal = string[0]
	for i in string:
		if i == lastVal:
			currentSum += 1
		else:
			vals.append(currentSum)
			currentSum = 1
			lastVal = i
	vals.append(currentSum)
	sums = []
	for i in range(len(vals)):
		maybe = 0
		for j in range(i, -1, -2):
			maybe += vals[j]
		maybe1 = maybe
		maybe2 = maybe
		for j in range(i + 2, len(vals), 2):
			maybe1 += vals[j]
		for j in range(i + 3, len(vals), 2):
			maybe2 += vals[j]
		sums.append(min(maybe1, maybe2))
	vals = vals[::-1]
	for i in range(len(vals)):
		maybe = 0
		for j in range(i, -1, -2):
			maybe += vals[j]
		maybe1 = maybe
		maybe2 = maybe
		for j in range(i + 2, len(vals), 2):
			maybe1 += vals[j]
		for j in range(i + 3, len(vals), 2):
			maybe2 += vals[j]
		sums.append(min(maybe1, maybe2))
	if len(vals) < 3:
		print(0)
	else:
		print(min(sums))


def solve(s):
	r = [[0] * (len(s) + 1) for i in range(2)]
	for i in range(len(s) - 1, -1, -1):
		v = ord(s[i]) - ord('0')
		r[v][i] = r[v][i + 1] + 1
		r[1 - v][i] = r[1 - v][i + 1]
	best = min(r[0][0], r[1][0])
	l = [0, 0]
	for i in range(len(s)):
		v = ord(s[i]) - ord('0')
		l[v] += 1
		best = min(best, l[0] + r[1][i + 1], l[1] + r[0][i + 1])
	return best
tests = int(input())
for test in range(tests):
	s = input()
	print(solve(s))


t = int(input())
for _ in range(t):
	s = [int(i) for i in input()]
	n = len(s)
	c = [0]
	for i in range(n):
		c.append(c[-1] + s[i])
	c = c[1:] + [0]
	ans = c[n - 1]
	for i in range(n):
		ans = min(ans, n - i - (c[n - 1] - c[i - 1]) + c[i - 1])
	ans = min(ans, n - c[n - 1])
	for i in range(n):
		ans = min(ans, c[n - 1] - c[i - 1] + i - c[i - 1])
	print(ans)


import sys
input = sys.stdin.readline

def prog():
	for _ in range(int(input())):
		s = input().strip()
		zeros = 0
		ones = 0
		for a in s:
			if a == '1':
				ones += 1
			else:
				zeros += 1
		m = zeros
		curr = zeros
		for a in s:
			if a == '1':
				curr += 1
			else:
				curr -= 1
			m = min(curr, m)
		curr = ones
		m = min(curr, m)
		for a in s:
			if a == '0':
				curr += 1
			else:
				curr -= 1
			m = min(curr, m)
		print(m)
prog()


for i in range(int(input())):
	a = input()
	ob = []
	zb = []
	os = []
	zs = []
	(z, o) = (0, 0)
	for i in range(len(a)):
		if a[i] == '0':
			z += 1
			zs.append(z)
			os.append(o)
		else:
			o += 1
			zs.append(z)
			os.append(o)
	(z, o) = (0, 0)
	for i in range(len(a) - 1, -1, -1):
		if a[i] == '0':
			z += 1
			zb.append(z)
			ob.append(o)
		else:
			o += 1
			zb.append(z)
			ob.append(o)
	ob.reverse()
	zb.reverse()
	n = len(a)
	ans = []
	for i in range(0, len(a) - 1):
		ans.append(min(zs[i], os[i]) + min(ob[i + 1], zb[i + 1]))
	ans.append(min(zb[0], ob[0]))
	print(min(ans))


from collections import Counter
for t in range(int(input())):
	s = input()
	min_change = 10000
	cnt1 = {'0': 0, '1': 0}
	cnt2 = Counter(s)
	for i in range(0, len(s)):
		if i != 0:
			if s[i - 1] == '1':
				cnt2['1'] -= 1
				cnt1['1'] += 1
			else:
				cnt2['0'] -= 1
				cnt1['0'] += 1
		change = min(cnt1['0'], cnt1['1']) + min(cnt2['1'], cnt2['0'])
		if change < min_change:
			min_change = change
	print(min_change)


t = int(input())
for k in range(t):
	s = input()
	num1 = 0
	num0 = 0
	done0 = 0
	done1 = 0
	for i in s:
		if i == '1':
			num1 = num1 + 1
		elif i == '0':
			num0 += 1
	ans = min(num1, num0)
	for i in s:
		if i == '1':
			done1 += 1
			num1 = num1 - 1
		elif i == '0':
			done0 += 1
			num0 = num0 - 1
		ans = min(ans, num1 + done0)
		ans = min(ans, num0 + done1)
	print(ans)


for _ in range(int(input())):
	s = input()
	mini = len(s)
	for i in range(len(s) + 1):
		mini = min(mini, s[:i].count('0') + s[i:].count('1'), s[:i].count('1') + s[i:].count('0'))
	print(mini)


for u in range(int(input())):
	s = input()
	n = len(s)
	l = [0] * n
	l[0] = 0
	for i in range(n):
		if i > 0:
			l[i] = l[i - 1]
		if s[i] == '0':
			l[i] += 1
	ka = min(l[n - 1], n - l[n - 1])
	for i in range(n):
		r = 0
		r = r + i + 1 - l[i]
		r += l[n - 1] - l[i]
		ka = min(ka, r)
		r = 0
		r += l[i]
		r = r + n - i - 1 - l[n - 1] + l[i]
		ka = min(ka, r)
	print(ka)


def solve():
	s = input()
	ans = min(s.count('1'), s.count('0'))
	n = len(s)
	for i in range(1, n):
		(u, v) = (s[:i], s[i:])
		ans = min(ans, u.count('1') + v.count('0'))
		ans = min(ans, u.count('0') + v.count('1'))
	print(ans)
t = int(input())
for _ in range(t):
	solve()


def solve(s):
	left0 = [0] * (len(s) + 1)
	left1 = [0] * (len(s) + 1)
	right0 = [0] * (len(s) + 1)
	right1 = [0] * (len(s) + 1)
	for i in range(len(s)):
		c = s[i]
		if c == '1':
			left0[i + 1] = left0[i] + 1
			left1[i + 1] = left1[i]
		else:
			left1[i + 1] = left1[i] + 1
			left0[i + 1] = left0[i]
	for i in reversed(range(len(s))):
		c = s[i]
		if c == '1':
			right0[i] = right0[i + 1] + 1
			right1[i] = right1[i + 1]
		else:
			right1[i] = right1[i + 1] + 1
			right0[i] = right0[i + 1]
	val = len(s)
	for i in range(len(s) + 1):
		val = min(val, left0[i] + right1[i], left1[i] + right0[i])
	return val
t = int(input())
for _ in range(t):
	s = input()
	print(solve(s))


import sys
input = sys.stdin.readline
from collections import defaultdict as dd
t = int(input())
while t:
	s = input().split()[0]
	ss = s[0]
	cou = 1
	l = []
	for i in range(1, len(s)):
		if s[i] == s[i - 1]:
			cou += 1
		else:
			l.append(cou)
			cou = 1
			ss += s[i]
	if cou:
		l.append(cou)
	n = len(ss)
	preo = [0] * n
	prez = [0] * n
	poso = [0] * n
	posz = [0] * n
	su1 = 0
	su0 = 0
	for i in range(n):
		preo[i] = su1
		prez[i] = su0
		if ss[i] == '1':
			su1 += l[i]
		else:
			su0 += l[i]
	su1 = 0
	su0 = 0
	for i in range(n - 1, -1, -1):
		poso[i] = su1
		posz[i] = su0
		if ss[i] == '1':
			su1 += l[i]
		else:
			su0 += l[i]
	a = 0
	b = 0
	aa = 0
	bb = 0
	mi = 1000000000000000000
	for i in range(n):
		mi = min(mi, preo[i] + posz[i], prez[i] + poso[i])
	print(mi)
	t -= 1


import sys, math
Tests = int(input())
for _ in range(Tests):
	s = input()
	n = len(s)
	zero = 0
	one = 0
	for i in range(n):
		if s[i] == '0':
			zero += 1
		else:
			one += 1
	ans = min(one, zero)
	donez = 0
	doneo = 0
	for i in range(n):
		if s[i] == '0':
			donez += 1
		else:
			doneo += 1
		ans = min(ans, donez + one - doneo)
		ans = min(ans, doneo + zero - donez)
	print(ans)


class Solution:

	def __init__(self):
		self.s = input()

	def solve_and_print(self):
		right_1 = self.s.count('1')
		(left_0, left_1, right_0) = (0, 0, len(self.s) - right_1)
		min_op = len(self.s)
		for c in self.s:
			min_op = min(min_op, left_0 + right_1, left_1 + right_0)
			if c == '1':
				left_1 += 1
				right_1 -= 1
			else:
				left_0 += 1
				right_0 -= 1
		print(min_op)
for _ in range(int(input())):
	Solution().solve_and_print()


def goodstring(s):
	zero = []
	k = 0
	for c in s:
		if c == '0':
			k += 1
		zero.append(k)
	zeros = zero[-1]
	a = min((2 * zero[i] - i for i in range(len(s)))) + len(s) - zeros - 1
	b = min((i - 2 * zero[i] for i in range(len(s)))) + zeros + 1
	return min(a, b)
t = int(input())
for i in range(t):
	s = input()
	print(goodstring(s))


t = int(input())

def solve(string, n):
	prefix = [[0, 0]]
	(zeros, ones) = (0, 0)
	for x in string:
		if not x:
			zeros += 1
		else:
			ones += 1
		prefix.append([zeros, ones])
	ans = min(string.count(0), string.count(1))
	for x in range(1, n):
		oneLeft = prefix[x][1]
		zeroRight = prefix[n][0] - prefix[x][0]
		ans = min(ans, oneLeft + zeroRight)
	return ans
while t:
	string = [int(x) for x in input()]
	ans1 = solve(string, len(string))
	ans2 = solve(string[::-1], len(string))
	print(min(ans1, ans2))
	t -= 1


for _ in range(int(input())):
	l = list(map(int, input()))
	ans = len(l)
	n = len(l)
	pre = [0 for t in range(n)]
	suf = [0 for ti in range(n)]
	if l[0] == 0:
		pre[0] = 1
	else:
		suf[0] = 1
	for t in range(1, n):
		pre[t] = pre[t - 1] + (not l[t])
		suf[t] = suf[t - 1] + l[t]
	for t in range(n):
		z = suf[t] + (pre[n - 1] - pre[t])
		w = pre[t] + (suf[n - 1] - suf[t])
		ans = min(ans, z, w)
	print(ans)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	s = input().rstrip()
	(res, z, o, nz, no) = (0, 0, 0, 0, 0)
	for c in s:
		if c == '0':
			z += 1
		else:
			o += 1
	res = min(z, o)
	for c in s:
		if c == '0':
			nz += 1
		else:
			no += 1
		res = min(res, no + (z - nz), nz + (o - no))
	print(res)


def solve():
	s = [int(v) for v in input()]
	diffwith0 = sum(s)
	ans = diffwith0
	n = len(s)
	for seq in (range(n), reversed(range(n))):
		diff = diffwith0
		for i in seq:
			if s[i] != 1:
				diff += 1
			else:
				diff -= 1
			ans = min(diff, ans)
	print(ans)
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	a = input()
	n = len(a)
	(v0, v1) = (0, 0)
	(s0, s1) = ([0] * n, [0] * n)
	ans = n
	for i in range(n):
		if a[i] == '1':
			v1 += 1
		else:
			v0 += 1
		s0[i] = v0
		s1[i] = v1
	for i in range(n):
		ans = min(ans, s0[i] + s1[-1] - s1[i], s1[i] + s0[-1] - s0[i])
	print(ans)


for i in range(int(input())):
	t = input()
	(num0, num1) = (t.count('0'), t.count('1'))
	(d1, d0) = (0, 0)
	minimum = len(t)
	for i in t:
		if i == '0':
			d0 += 1
		else:
			d1 += 1
		m = min(d0 + num1 - d1, d1 + num0 - d0)
		if m < minimum:
			minimum = m
	print(minimum)


t = int(input())
for _ in range(t):
	s = input()
	if len(s) <= 2:
		print(0)
		continue
	li = []
	for i in s:
		if li:
			if i == '1':
				li.append(li[-1] + 1)
			else:
				li.append(li[-1])
		elif i == '1':
			li.append(1)
		else:
			li.append(0)
	ans = 0
	for (p, i) in enumerate(s):
		ans = max(ans, li[p] + (li[-1] - li[p]))
		ans = max(ans, p + 1 - li[p] + (li[-1] - li[p]))
		ans = max(ans, li[p] + len(s) - (p + 1) - (li[-1] - li[p]))
		ans = max(ans, p + 1 - li[p] + len(s) - (p + 1) - (li[-1] - li[p]))
	print(len(s) - ans)


t = int(input())
for _ in range(t):
	s = input()
	if len(s) <= 2:
		print(0)
		continue
	if s[0] == '0':
		flg = 0
	else:
		flg = 1
	cnts = []
	cnt = 0
	for (i, c) in enumerate(s):
		if c == '0':
			if flg == 0:
				cnt += 1
			elif cnt != 0:
				cnts.append(cnt)
				cnt = 1
				flg = 0
		if c == '1':
			if flg == 1:
				cnt += 1
			elif cnt != 0:
				cnts.append(cnt)
				cnt = 1
				flg = 1
	cnts.append(cnt)
	n = len(cnts)
	if n <= 2:
		print(0)
		continue
	prefix_eve = 0
	prefix_odd = 0
	suf_eve = sum((cnts[i] for i in range(n) if i % 2 == 0))
	suf_odd = sum((cnts[i] for i in range(n) if i % 2 == 1))
	res = suf_eve + suf_odd
	for i in range(n):
		if i % 2 == 0:
			prefix_eve += cnts[i]
			suf_eve -= cnts[i]
		else:
			prefix_odd += cnts[i]
			suf_odd -= cnts[i]
		temp = min(prefix_eve + suf_odd, prefix_eve + suf_eve, prefix_odd + suf_odd, prefix_odd + suf_eve)
		res = min(temp, res)
	print(res)


for _ in range(int(input())):
	s = list(map(int, list(input())))
	ans = float('inf')
	nz = 0
	no = 0
	for i in s:
		if i == 1:
			no += 1
		else:
			nz += 1
	tz = 0
	to = 0
	n = len(s)
	for i in range(len(s)):
		if s[i] == 1:
			to += 1
		else:
			tz += 1
		ans = min(ans, n - tz - (no - to), n - to - (nz - tz))
	ans = min(ans, n - no, n - nz)
	print(ans)


from io import BytesIO, IOBase
import sys
import os

def Iint():
	return int(input())

def Ilist():
	return list(map(int, input().split()))

def Imap():
	return map(int, input().split())

def Plist(li, s=' '):
	print(s.join(map(str, li)))

def main():
	t = Iint()
	for _ in range(t):
		s = input()
		n = len(s)
		if n < 3:
			print(0)
			continue
		s0 = s.count('0')
		s1 = s.count('1')
		pfs0 = [0] * n
		ans = min(s1, s0)
		for i in range(n):
			pfs0[i] = pfs0[i - 1] + (1 if s[i] == '0' else 0)
			x = pfs0[i]
			(opt1, opt2) = (s0 + i + 1 - 2 * x, s1 - i - 1 + 2 * x)
			ans = min(ans, opt1, opt2)
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for i in range(t):
	string = input()
	n = len(string)
	conversions = 0
	conversionsMin = 1001
	for i in range(n):
		if string[i] == '0':
			conversions += 1
	conversionsMin = min(conversions, conversionsMin)
	for i in range(n):
		if string[i] == '0':
			conversions -= 1
		else:
			conversions += 1
		conversionsMin = min(conversions, conversionsMin)
	conversions = 0
	for i in range(n):
		if string[i] == '1':
			conversions += 1
	conversionsMin = min(conversions, conversionsMin)
	for i in range(n):
		if string[i] == '1':
			conversions -= 1
		else:
			conversions += 1
		conversionsMin = min(conversions, conversionsMin)
	print(conversionsMin)


def MIN(str):
	a = str.count('1')
	b = str.count('0')
	return min(a, b)

def beauty_nem(str):
	sum = MIN(str)
	for i in range(1, len(str)):
		if str[i] != str[i - 1]:
			left_min = MIN(str[:i])
			right_min = MIN(str[i:])
			sum = min(sum, left_min + right_min)
	return sum
num = eval(input())
for i in range(num):
	str = input()
	number = beauty_nem(str)
	print(number)


from math import inf, log2

class SegmentTree:

	def __init__(self, array, func=max):
		self.n = len(array)
		self.size = 2 ** (int(log2(self.n - 1)) + 1) if self.n != 1 else 1
		self.func = func
		self.default = 0 if self.func != min else inf
		self.data = [self.default] * (2 * self.size)
		self.process(array)

	def process(self, array):
		self.data[self.size:self.size + self.n] = array
		for i in range(self.size - 1, -1, -1):
			self.data[i] = self.func(self.data[2 * i], self.data[2 * i + 1])

	def query(self, alpha, omega):
		if alpha == omega:
			return self.data[alpha + self.size]
		res = self.default
		alpha += self.size
		omega += self.size + 1
		while alpha < omega:
			if alpha & 1:
				res = self.func(res, self.data[alpha])
				alpha += 1
			if omega & 1:
				omega -= 1
				res = self.func(res, self.data[omega])
			alpha >>= 1
			omega >>= 1
		return res

	def update(self, index, value):
		index += self.size
		self.data[index] = value
		index >>= 1
		while index:
			self.data[index] = self.func(self.data[2 * index], self.data[2 * index + 1])
			index >>= 1
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
for _ in range(int(input()) if True else 1):
	s = input()
	cur = int(s[0])
	flip = 0
	find = [0]
	odd = []
	even = []
	(oc, ec) = (0, 0)
	if s[0] == '0':
		ec += 1
	else:
		oc += 1
	zero = []
	one = []
	for i in range(len(s)):
		if s[i] == '0':
			zero += [1]
			one += [0]
		else:
			one += [1]
			zero += [0]
		if int(s[i]) != cur % 2:
			flip += 1
			cur += 1
	if flip <= 1:
		print('0')
		continue
	st0 = SegmentTree(zero, func=lambda a, b: a + b)
	st1 = SegmentTree(one, func=lambda a, b: a + b)
	n = len(s)
	ans = n
	for i in range(n):
		ans = min(ans, st1.query(0, i) + st0.query(i + 1, n - 1), st0.query(0, i) + st1.query(i + 1, n - 1))
	print(ans)


t = int(input())
for i in range(0, t):
	s = str(input())
	countzero = 0
	countone = 0
	zerotilli = 0
	onetilli = 0
	ans = len(s)
	for i in range(0, len(s)):
		if s[i] == '0':
			countzero += 1
		else:
			countone += 1
	for i in range(0, len(s)):
		if s[i] == '1':
			onetilli += 1
		else:
			zerotilli += 1
		ans = min(ans, countone - onetilli + zerotilli)
		ans = min(ans, countzero + onetilli - zerotilli)
	print(ans)


from sys import stdin
input = stdin.readline
import bisect
import math
for xoxo in range(1):
	for _ in range(int(input())):
		s = input()
		s = s[:len(s) - 1]
		ans = []
		if len(s) <= 2:
			ans.append(0)
		else:
			p = s.count('0')
			n = len(s)
			ans.append(p)
			ans.append(n - p)
			(l, r) = (0, n - p)
			for i in range(n):
				if s[i] == '0':
					l += 1
				else:
					r -= 1
				ans.append(l + r)
			k = s[::-1]
			(l, r) = (0, n - p)
			for i in range(n):
				if k[i] == '0':
					l += 1
				else:
					r -= 1
				ans.append(l + r)
		print(min(ans))


t = int(input())
while t:
	l1 = input()
	count = [1]
	passed = 0
	for i in range(1, len(l1)):
		if l1[i] != l1[i - 1]:
			passed = passed + 1
			count.append(0)
			count[passed] = count[passed] + 1
		else:
			count[passed] = count[passed] + 1
	l = passed + 1
	min = len(l1)
	sumo = 0
	sume = 0
	for i in range(0, l):
		if i % 2 == 0:
			sume = sume + count[i]
		else:
			sumo = sumo + count[i]
	check = sume - count[0]
	if check < min:
		min = check
	for i in range(2, l, 2):
		check = check - count[i] + count[i - 1]
		if check < min:
			min = check
	check = sumo
	if check < min:
		min = check
	for i in range(1, l, 2):
		check = check - count[i] + count[i - 1]
		if check < min:
			min = check
	print(min)
	t = t - 1


for _ in range(int(input())):
	s = input()
	ones = s.count('1')
	t_o = ones
	n = len(s)
	zeros = n - ones
	t_z = zeros
	arr = []
	for i in range(n - 1, -1, -1):
		arr.append(min(ones + t_z - zeros, zeros + t_o - ones))
		if s[i] == '1':
			ones -= 1
		else:
			zeros -= 1
	print(min(arr))


import sys
import math
import collections

def set_debug(debug_mode=False):
	if debug_mode:
		fin = open('input.txt', 'r')
		sys.stdin = fin
t = int(input())
for ti in range(1, t + 1):
	s = input()
	n = len(s)
	zero = 0
	one = 0
	for x in s:
		if x == '0':
			zero += 1
		else:
			one += 1
	left_zero = [0] * n
	right_zero = [0] * n
	left_one = [0] * n
	right_one = [0] * n
	lz = 0
	rz = zero
	lo = 0
	ro = one
	res = float('inf')
	for i in range(n):
		left_zero[i] = lz
		left_one[i] = lo
		if s[i] == '0':
			lz += 1
			rz -= 1
		else:
			lo += 1
			ro -= 1
		right_zero[i] = rz
		right_one[i] = ro
		if s[i] == '0':
			res = min(res, left_one[i] + right_one[i])
			res = min(res, left_zero[i] + right_one[i])
			res = min(res, left_one[i] + right_zero[i])
		else:
			res = min(res, left_zero[i] + right_zero[i])
			res = min(res, left_zero[i] + right_one[i])
			res = min(res, left_one[i] + right_zero[i])
	print(res)


for _ in range(int(input())):
	s = input()
	zl = [0]
	Or = [0]
	for i in range(len(s)):
		if s[i] == '0':
			zl.append(zl[-1] + 1)
		else:
			zl.append(zl[-1])
	for i in range(len(s) - 1, -1, -1):
		if s[i] == '1':
			Or.append(Or[-1] + 1)
		else:
			Or.append(Or[-1])
	ans = 10 ** 9
	Or = Or[::-1]
	for i in range(len(zl)):
		ans = min(ans, zl[i] + Or[i])
	zr = [0]
	Ol = [0]
	for i in range(len(s)):
		if s[i] == '1':
			Ol.append(Ol[-1] + 1)
		else:
			Ol.append(Ol[-1])
	for i in range(len(s) - 1, -1, -1):
		if s[i] == '0':
			zr.append(zr[-1] + 1)
		else:
			zr.append(zr[-1])
	zr = zr[::-1]
	for i in range(len(zr)):
		ans = min(ans, zr[i] + Ol[i])
	print(ans)


import sys
t = int(input())
for _ in range(t):
	s = input()
	siz = len(s)
	preL0 = list()
	preL1 = list()
	postL0 = [None] * siz
	postL1 = [None] * siz
	if s[0] == '0':
		preL0.append(1)
		preL1.append(0)
	else:
		preL0.append(0)
		preL1.append(1)
	for i in range(1, siz):
		if s[i] == '0':
			preL0.append(preL0[i - 1] + 1)
			preL1.append(preL1[i - 1])
		else:
			preL0.append(preL0[i - 1])
			preL1.append(preL1[i - 1] + 1)
	if s[siz - 1] == '0':
		postL0[siz - 1] = 1
		postL1[siz - 1] = 0
	else:
		postL0[siz - 1] = 0
		postL1[siz - 1] = 1
	for i in range(siz - 2, -1, -1):
		if s[i] == '0':
			postL0[i] = postL0[i + 1] + 1
			postL1[i] = postL1[i + 1]
		else:
			postL0[i] = postL0[i + 1]
			postL1[i] = postL1[i + 1] + 1
	min = siz
	if preL0[siz - 1] == siz or preL1[siz - 1] == siz or postL0[0] == siz or (postL1[0] == siz):
		min = 0
	for i in range(0, siz - 1):
		t1 = preL1[i] + postL0[i + 1]
		if t1 < min:
			min = t1
		t1 = preL0[i] + postL1[i + 1]
		if t1 < min:
			min = t1
		t1 = preL0[i] + postL0[i + 1]
		if t1 < min:
			min = t1
		t1 = preL1[i] + postL1[i + 1]
		if t1 < min:
			min = t1
	if siz < 3:
		min = 0
	print(min)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	s = input().strip()
	n = len(s)
	(l, r) = ([0] * n, [0] * n)
	(c, c1) = (0, 0)
	for i in range(n):
		if s[i] == '0':
			c += 1
		else:
			c1 += 1
		l[i] = (c, c1)
	(c, c1) = (0, 0)
	for i in range(n - 1, -1, -1):
		if s[i] == '0':
			c += 1
		else:
			c1 += 1
		r[i] = (c, c1)
	x = []
	r.append((0, 0))
	for i in range(0, n):
		x.append(min(l[i][1] + r[i + 1][0], l[i][0] + r[i + 1][1]))
	print(min(x))


import math, sys, bisect, heapq
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])
for _ in range(int(input())):
	s = input()
	n = len(s)
	o = s.count('1')
	z = n - o
	if n <= 2 or o == 0 or z == 0:
		print(0)
	else:
		mini = min(o, z)
		a = 0
		b = 0
		for i in range(n):
			if s[i] == '1':
				a += 1
			else:
				b += 1
			x = b + o - a
			y = a + z - b
			mini = min(mini, x, y)
		print(mini)


for _ in range(int(input())):
	data = input()
	s = 10 ** 18
	for i in range(len(data)):
		a = data[:i].count('1')
		b = data[:i].count('0')
		c = data[i:].count('1')
		d = data[i:].count('0')
		s = min(s, a + d, c + b)
	print(s)


for _ in range(int(input())):
	s = input()
	all1 = s.count('0')
	all0 = s.count('1')
	t1 = all0
	t0 = all1
	(d1, d0) = (0, 0)
	ans = min(all1, all0)
	for i in range(len(s)):
		if s[i] == '0':
			d0 += 1
		else:
			d1 += 1
		c1 = d0 + t1 - d1
		c2 = d1 + t0 - d0
		ans = min(ans, c1, c2)
	print(ans)


k = int(input())
for _ in range(k):
	s = input()
	n = len(s)
	var = []
	for i in range(1, n):
		var += [min(s[0:i].count('0'), s[0:i].count('1')) + min(s[i:n].count('0'), s[i:n].count('1'))]
	if n == 1:
		print('0')
	else:
		print(min(var))


import sys
from collections import defaultdict, deque

def solve():
	s = input()
	ans = len(s)
	n = len(s)
	o = 0
	z = 0
	for e in s:
		if e == '0':
			z += 1
		else:
			o += 1
	x = 0
	y = 0
	for i in range(n):
		if s[i] == '0':
			x += 1
			z -= 1
		else:
			y += 1
			o -= 1
		ans = min(ans, min(x + o, z + y))
	print(ans)
t = 1
t = int(input())
for case in range(1, t + 1):
	ans = solve()


import math
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	s = input().strip()
	m = [0] * len(s)
	(maxi, mini) = (0, 0)
	zero = 0
	if s[0] == '0':
		m[0] = -1
		zero += 1
	else:
		m[0] = 1
	for i in range(1, len(s)):
		if s[i] == '0':
			m[i] = m[i - 1] - 1
			zero += 1
		else:
			m[i] = m[i - 1] + 1
		if m[maxi] < m[i]:
			maxi = i
		if m[mini] > m[i]:
			mini = i
	zero_in_first_half = (maxi + 1 - m[maxi]) // 2
	one_in_first_half = maxi + 1 - zero_in_first_half
	one_in_second_half = len(s) - zero - one_in_first_half
	ans1 = zero_in_first_half + one_in_second_half
	zero_in_first_half = (mini + 1 - m[mini]) // 2
	one_in_first_half = (mini + 1 - m[mini]) // 2 + m[mini]
	zero_in_second_half = zero - zero_in_first_half
	ans2 = one_in_first_half + zero_in_second_half
	print(min(ans1, ans2))


t = int(input())
for x in range(t):
	s = input()
	zero = [0]
	one = [0]
	for i in range(len(s)):
		if s[i] == '0':
			temp = zero[-1]
			zero.append(temp + 1)
			temp = one[-1]
			one.append(temp)
		else:
			temp = one[-1]
			one.append(temp + 1)
			temp = zero[-1]
			zero.append(temp)
	ans = len(s)
	for i in range(len(s)):
		if zero[i] + one[-1] - one[i] < ans:
			ans = zero[i] + one[-1] - one[i]
	for i in range(len(s)):
		if one[i] + zero[-1] - zero[i] < ans:
			ans = one[i] + zero[-1] - zero[i]
	print(ans)


t = int(input())
for i in range(t):
	s = input()
	rs = list()
	rs.append(0)
	for c in s:
		if c == '1':
			rs.append(rs[-1] + 1)
		else:
			rs.append(rs[-1])
	m = len(s)
	for i in range(len(rs)):
		r = rs[i]
		m = min(m, r + (len(s) - rs[-1] - (i - r)))
		m = min(m, i - r + (rs[-1] - r))
	print(m)


def hate(s):
	to = 0
	tz = 0
	for x in s:
		if x == '1':
			to += 1
		else:
			tz += 1
	if to == 0 or tz == 0:
		print(0)
		return
	co = 0
	cz = 0
	mcost = None
	for z in s:
		if z == '1':
			co += 1
		else:
			cz += 1
		c = min(cz + to - co, co + tz - cz)
		if mcost == None:
			mcost = c
			continue
		if c < mcost:
			mcost = c
	print(mcost)
	return
t = int(input())
a = []
for i in range(t):
	a.append(input())
for x in a:
	hate(x)


for _ in range(int(input())):
	s = input()
	n = len(s)
	zc = [0]
	oc = [0]
	for x in s:
		if x == '0':
			zc.append(zc[-1] + 1)
			oc.append(oc[-1])
		else:
			zc.append(zc[-1])
			oc.append(oc[-1] + 1)
	fans = 1e+18
	for i in range(n + 1):
		v1 = oc[i]
		zzz = zc[-1] - zc[i]
		v1 += zzz
		fans = min(fans, v1)
		v1 = zc[i]
		ones = oc[-1] - oc[i]
		v1 += ones
		fans = min(fans, v1)
	print(fans)


n = int(input())
for _ in range(n):
	s = input()
	cnt1 = s.count('1')
	cnt0 = s.count('0')
	ans = min(cnt1, cnt0)
	p0 = 0
	p1 = 0
	for i in range(len(s)):
		if s[i] == '0':
			p0 += 1
			cnt0 -= 1
		else:
			p1 += 1
			cnt1 -= 1
		ans = min(ans, p0 + cnt1, cnt0 + p1)
	print(ans)


import sys
import os
import heapq
import math
try:
	path = './file/input.txt'
	if os.path.exists(path):
		sys.stdin = open(path, 'r')
except:
	pass
t = int(input())

def printd(value):
	pass

def case():
	s = input()
	leftzero = [0] * len(s)
	leftone = [0] * len(s)
	for index in range(len(s)):
		if index > 0:
			leftzero[index] = leftzero[index - 1]
			leftone[index] = leftone[index - 1]
			if s[index - 1] == '1':
				leftone[index] += 1
			else:
				leftzero[index] += 1
	rightzero = [0] * len(s)
	rightone = [0] * len(s)
	for index in range(len(s) - 1, -1, -1):
		if index < len(s) - 1:
			rightzero[index] = rightzero[index + 1]
			rightone[index] = rightone[index + 1]
			if s[index + 1] == '1':
				rightone[index] += 1
			else:
				rightzero[index] += 1
	result = len(s)
	for (index, c) in enumerate(s):
		result = min(result, leftone[index] + rightone[index] + int(s[index]))
		result = min(result, leftzero[index] + rightzero[index] + 1 - int(s[index]))
		result = min(result, leftzero[index] + rightone[index])
		result = min(result, leftone[index] + rightzero[index])
	print(result)
for _ in range(t):
	case()


r = int(input())
for ji in range(r):
	c = 0
	a1 = 0
	a0 = 0
	b1 = 0
	b0 = 0
	q = list(input())
	for w in q:
		if w == '0':
			b0 += 1
	b1 = len(q) - b0
	for e in q:
		if max(a0 + b1, b0 + a1) > c:
			c = max(a0 + b1, b0 + a1)
		if e == '0':
			b0 -= 1
			a0 += 1
		else:
			b1 -= 1
			a1 += 1
	print(len(q) - c)


t = int(input())
for _ in range(t):
	s = input()
	n_ones = sum([1 for char in s if char == '1'])
	n_zeros = len(s) - n_ones
	(zero_left, one_left, zero_right, one_right) = (0, 0, n_zeros, n_ones)
	min_01_border = float('inf')
	min_10_border = float('inf')
	for i in range(len(s)):
		if s[i] == '1':
			one_right = one_right - 1 if one_right > 0 else one_right
			one_left += 1
		else:
			zero_right = zero_right - 1 if zero_right > 0 else zero_right
			zero_left += 1
		min_01_border = min(one_left + zero_right, min_01_border)
		min_10_border = min(zero_left + one_right, min_10_border)
	print(min([n_ones, n_zeros, min_01_border, min_10_border]))


def solve(s):
	n = len(s)
	if n < 3:
		return 0
	dp = [0 for i in range(n)]
	if s[0] == '0':
		dp[0] = 1
	for i in range(1, n):
		if s[i] == '0':
			dp[i] += dp[i - 1] + 1
		else:
			dp[i] = dp[i - 1]
	min_so_far = 10000
	for i in range(1, n):
		min_so_far = min(min_so_far, i - dp[i - 1] + (dp[n - 1] - dp[i - 1]))
		min_so_far = min(min_so_far, dp[i - 1] + (n - i - (dp[n - 1] - dp[i - 1])))
	min_so_far = min(min_so_far, dp[n - 1])
	min_so_far = min(min_so_far, n - dp[n - 1])
	return min_so_far

def read():
	t = int(input())
	for i in range(t):
		s = input()
		ans = solve(s)
		print(ans)
read()


for _ in range(int(input())):
	s = input()
	k = len(s)
	a = s.count('0')
	b = s.count('1')
	if a == k or b == k:
		print(0)
		continue
	os = k
	zs = k
	no = 0
	nz = 0
	for i in range(k):
		if s[i] == '0':
			no += 1
		else:
			nz += 1
		os = min(os, k - i - 1 - a + 2 * no)
		zs = min(zs, k - i - 1 - b + 2 * nz)
	print(min(os, zs))


for _ in range(int(input())):
	s = input()
	n = len(s)
	l0 = [0] * (n + 1)
	l1 = [0] * (n + 1)
	c1 = s.count('1')
	c0 = s.count('0')
	if int(s[0]) == 1:
		l1[0] = 1
		l0[0] = 0
	else:
		l0[0] = 1
		l1[0] = 0
	for i in range(1, len(s)):
		if int(s[i]) == 1:
			l1[i] = l1[i - 1] + 1
			l0[i] = l0[i - 1] + 0
		else:
			l1[i] = l1[i - 1] + 0
			l0[i] = l0[i - 1] + 1
	ans = float('inf')
	for i in range(len(s)):
		ans = min(ans, l0[i] + c1 - l1[i])
		ans = min(ans, l1[i] + c0 - l0[i])
	print(ans)


from sys import stdin, stdout
from math import sin, pi, tan
for test in range(int(stdin.readline())):
	strn = stdin.readline()
	strn = strn[:-1]
	if len(strn) < 3:
		stdout.write('0\n')
	else:
		f0 = strn.find('0')
		l0 = strn.rfind('0')
		f1 = strn.find('1')
		l1 = strn.rfind('1')
		if f0 == l0 and f0 == -1 or (f1 == l1 and f1 == -1):
			stdout.write('0\n')
		elif f0 > l1 or f1 > l0:
			stdout.write('0\n')
		else:
			c0 = 0
			c1 = 0
			for i in strn:
				if i == '1':
					c1 += 1
				else:
					c0 += 1
			enc0 = 0
			enc1 = 0
			mini = 10001
			for i in range(len(strn)):
				if strn[i] == '1':
					enc1 += 1
				else:
					enc0 += 1
				mini = min(mini, enc1 + c0 - enc0, enc0 + c1 - enc1)
			stdout.write(str(mini) + '\n')


def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
entries = inp()
for i in range(entries):
	s = insr()
	res = 0
	point = 0
	l = []
	zero = 0
	one = 0
	for i in range(len(s)):
		if s[i] == '0':
			zero += 1
		else:
			one += 1
		l.append((zero, one))
	res = 1000
	for i in range(len(s)):
		cnt = 0
		cnt += l[i][1]
		cnt += l[len(s) - 1][0] - l[i][0]
		if cnt < res:
			res = cnt
		cnt = 0
		cnt += l[i][0]
		cnt += l[len(s) - 1][1] - l[i][1]
		if cnt < res:
			res = cnt
	print(res)


q = int(input())
while q:
	q -= 1
	s = input()
	(zero, one) = (0, 0)
	for w in s:
		if w == '0':
			zero += 1
		else:
			one += 1
	ans = min(zero, one)
	(zero_b, one_b) = (0, 0)
	for w in s:
		if w == '0':
			zero_b += 1
			zero -= 1
		else:
			one_b += 1
			one -= 1
		ans = min(ans, zero + one_b)
		ans = min(ans, one + zero_b)
	print(ans)


from math import *
t = int(input())
while t > 0:
	s = input()
	n = len(s)
	if n == 1 or n == 2:
		print(0)
	else:
		ans = []
		one = 0
		zero = 0
		pre1 = []
		pre0 = []
		for i in range(n):
			if s[i] == '1':
				one += 1
			else:
				zero += 1
			pre1.append(one)
			pre0.append(zero)
		for i in range(n):
			ans.append(pre1[i] + (zero - pre0[i]))
			ans.append(pre0[i] + (one - pre1[i]))
		ans.append(one)
		ans.append(zero)
		print(min(ans))
	t -= 1


for _ in range(int(input())):
	s = list(map(int, input()))
	n = len(s)
	o = s.count(1)
	z = s.count(0)
	m = min(o, z)
	out = float('inf')
	t0 = 0
	t1 = 0
	for i in range(n):
		out = min(out, o - t1 + t0, z - t0 + t1)
		if s[i] == 1:
			t1 += 1
		else:
			t0 += 1
	out = min(out, t0, t1)
	print(out)


for _ in range(int(input())):
	s = [int(x) for x in input()]
	i = 0
	n = len(s)
	arr = 2000
	p1 = s.count(1)
	p2 = s.count(0)
	while i < n:
		coun1 = s[0:i].count(1)
		arr = min(arr, coun1 + p2 - (i - coun1), i - coun1 + p1 - coun1)
		i += 1
	print(arr)


n = int(input())

def retrans(text):
	new_text = ''
	for k in text:
		if k == '0':
			new_text += '1'
		else:
			new_text += '0'
	return new_text
for _ in range(n):
	text = input()
	x = len(text)
	(a, b) = ([0] * x, [0] * x)
	d = {'1': b, '0': a}
	if text[0] == '1':
		text = retrans(text)
	d[text[0]][0] += 1
	for k in range(1, len(text)):
		a[k] = a[k - 1]
		b[k] = b[k - 1]
		d[text[k]][k] = d[text[k]][k - 1] + 1
	d = {'0': 0, '1': 0}
	k = x - 2
	count = 1
	while k > 0:
		if text[k] != text[k + 1]:
			break
		count += 1
		k -= 1
	d[text[x - 1]] = count
	i = d['0']
	j = d['1']
	all_zeros_to_ones = a[x - 1] - i
	all_ones_to_zeros = b[x - 1] - j
	res = min(all_ones_to_zeros, all_zeros_to_ones)
	for t in range(x):
		if text[t] == '1':
			all_left_ones_to_zeros_all_right_zeros_to_ones = b[t - 1] + a[x - 1] - a[t]
			all_left_zeros_to_ones_all_right_ones_to_zeros = a[t - 1] + b[x - 1] - b[t]
			res = min(res, all_left_ones_to_zeros_all_right_zeros_to_ones, all_left_zeros_to_ones_all_right_ones_to_zeros)
	print(res)


t = int(input())
for _ in range(t):
	s = input()
	(n, o) = (len(s), s.count('1'))
	(ch, c) = ([o, n - o], 0)
	for (i, x) in enumerate(s):
		if x == '1':
			c += 1
		ch.append(i + 1 - c + o - c)
	c = 0
	for (i, x) in enumerate(reversed(s)):
		if x == '1':
			c += 1
		ch.append(i + 1 - c + o - c)
	print(min(ch))


import math
from collections import Counter

def i():
	return int(input())

def lii():
	return list(map(int, input().split()))

def lis():
	return list(map(int, list(input())))
t = i()
for _ in range(t):
	s = lis()
	if len(s) > 2 and len(set(s)) > 1:
		count1 = s.count(1)
		count0 = len(s) - count1
		c0 = 0
		c1 = 0
		b = []
		a = []
		for i in range(len(s)):
			if s[i] == 0:
				c0 += 1
			else:
				c1 += 1
			b.append(c0 + count1 - c1)
			b.append(c1 + count0 - c0)
		a.append(min(b))
		a.extend([count0, count1])
		print(min(a))
	else:
		print(0)


for case in range(int(input())):
	s = input()
	n = len(s)
	ans = 0
	for i in range(n):
		if s[i] != '1':
			ans += 1
	cur = ans
	cur2 = ans
	for i in range(n):
		if s[i] == '0':
			cur -= 1
		else:
			cur += 1
		ans = min(cur, ans)
	for i in range(n - 1, -1, -1):
		if s[i] == '1':
			cur2 += 1
		else:
			cur2 -= 1
		ans = min(cur2, ans)
	print(ans)


def task(s):
	zeros = 0
	ones = 0
	ans = 1000
	for i in range(0, len(s)):
		if s[i] == '0':
			zeros += 1
		else:
			ones += 1
		one_swaps = ones
		zero_swaps = zeros
		for j in range(i + 1, len(s)):
			if s[j] == '0':
				one_swaps += 1
			else:
				zero_swaps += 1
		ans = min(ans, one_swaps, zero_swaps)
	print(ans)
t = int(input())
for i in range(0, t):
	s = input()
	task(s)


import sys

def readStr():
	return sys.stdin.readline().rstrip()

def readInts():
	return list(map(int, readStr().split(' ')))

def main(s):
	n = len(s)
	if n <= 2:
		return 0
	cnt_f = []
	cnt_b = []
	cnt0 = 0
	cnt1 = 0
	for i in range(n):
		si = s[i]
		if si == '0':
			cnt0 += 1
		else:
			cnt1 += 1
		cnt_f.append(min(cnt0, cnt1))
	cnt0 = 0
	cnt1 = 0
	for i in range(n):
		si = s[-i - 1]
		if si == '0':
			cnt0 += 1
		else:
			cnt1 += 1
		cnt_b.append(min(cnt0, cnt1))
	x = cnt_f[-1]
	for i in range(n - 1):
		if x > cnt_f[i] + cnt_b[-i - 2]:
			x = cnt_f[i] + cnt_b[-i - 2]
	return x
(t,) = readInts()
for _ in range(t):
	s = readStr()
	x = main(s)
	print(x)


import math

def solution(s):
	n = len(s)
	best = float('inf')
	for i in range(n):
		left0 = s[:i].count('0')
		left1 = s[:i].count('1')
		right0 = s[i:].count('0')
		right1 = s[i:].count('1')
		m = min(left0 + right1, left1 + right0)
		best = min(best, m)
	return best
t = int(input())
for _ in range(t):
	s = input()
	print(solution(s))


def solu(s):
	if len(s) < 3:
		return 0
	n0 = 0
	for c in s:
		if c == '0':
			n0 += 1
	n1 = len(s) - n0
	result = min(n0, n1)
	f0 = 0
	for p in range(len(s)):
		if s[p] == '0':
			f0 += 1
		currRe = min(f0, p + 1 - f0) + min(n0 - f0, n1 - p - 1 + f0)
		result = min(result, currRe)
	return result
nt = int(input())
sol = []
for i in range(nt):
	s = input()
	sol.append(solu(s))
for x in sol:
	print(x)


import sys
input = lambda : sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline

def I():
	return list(map(int, inp().split()))
from math import inf
for _ in range(I()[0]):
	s = [int(i) for i in input()]
	n = len(s)
	prev = [[0, 0]]
	aft = [[0, 0]]
	for i in range(n):
		prev.append(prev[-1][:])
		prev[-1][s[i] & 1] += 1
		cur = [0, 0]
		for j in range(i + 1, n):
			cur[s[j] & 1] += 1
		aft.append(cur[:])
	ans = n + 1
	for i in range(1, len(prev)):
		ans = min(ans, prev[i][0] + aft[i][1], prev[i][1] + aft[i][0], prev[i][0] + aft[i][0] + prev[i][1] + aft[i][1])
	print(ans)


for _ in range(int(input())):
	s = input()
	suf0 = s.count('0')
	suf1 = s.count('1')
	ans = min(suf0, suf1)
	pref0 = 0
	pref1 = 0
	for elem in s:
		pref0 += elem == '0'
		suf0 -= elem == '0'
		pref1 += elem == '1'
		suf1 -= elem == '1'
		ans = min(ans, pref0 + suf1)
		ans = min(ans, pref1 + suf0)
	print(ans)


from collections import Counter
n = int(input())
for i in range(n):
	s = input()
	s = list(s)
	ans = 1000000000
	for j in range(len(s)):
		z = 0
		o = 0
		for k in range(len(s)):
			if k >= j:
				if s[k] != '0':
					z += 1
				else:
					o += 1
			elif s[k] != '1':
				z += 1
			else:
				o += 1
		a = min(z, o)
		ans = min(ans, a)
	print(ans)


for i in range(int(input())):
	a = [int(i) for i in input()]
	turns_needed = []
	if len(a) == 1:
		print('0')
	else:
		for centre in range(len(a) - 1):
			swaps = 0
			for i in range(centre + 1):
				if a[i] == 0:
					swaps += 1
			ans_1 = min(swaps, centre + 1 - swaps)
			swaps = 0
			for i in range(centre + 1, len(a)):
				if a[i] == 0:
					swaps += 1
			ans_2 = min(swaps, len(a) - centre - 1 - swaps)
			turns_needed.append(ans_1 + ans_2)
		print(min(turns_needed))


cases = int(input())
for i in range(cases):
	s = input()
	one = s.count('1')
	zero = s.count('0')
	p1 = p2 = p3 = len(s)
	i1 = i2 = j1 = j2 = 1001
	if '1' not in s or '0' not in s:
		print(0)
		continue
	check01 = s.index('1')
	if '1' not in s[0:check01] and '0' not in s[check01:]:
		print(0)
		continue
	check10 = s.index('0')
	if '0' not in s[0:check10] and '1' not in s[check10:]:
		print(0)
		continue
	p1 = min(one, zero)
	arr1 = []
	seen1 = seen0 = 0
	done1 = done0 = 0
	for j in range(len(s)):
		ele = s[j]
		if ele == '1':
			seen1 += 1
		elif ele == '0':
			done0 += 1
		arr1.append(one - seen1 + done0)
	arr2 = []
	for k in range(len(s)):
		ele = s[k]
		if ele == '0':
			seen0 += 1
		elif ele == '1':
			done1 += 1
		arr2.append(zero - seen0 + done1)
	m1 = min(arr1)
	m2 = min(arr2)
	print(min(p1, m1, m2))


for _ in range(int(input())):
	s = [int(i) for i in input()]
	zero = []
	one = []
	o = 0
	z = 0
	for i in s:
		if i == 1:
			o += 1
		else:
			z += 1
		zero.append(z)
		one.append(o)
	n = len(s)
	ans = float('inf')
	for i in range(1, n):
		a = one[i - 1] + zero[n - 1] - zero[i - 1]
		b = zero[i - 1] + one[n - 1] - one[i - 1]
		ans = min(ans, a, b)
	ans = min(ans, zero[-1], one[-1])
	print(ans)


for _ in range(int(input())):
	s = input()
	ans = 10000
	o = s.count('1')
	z = s.count('0')
	currones = 0
	currzeros = 0
	for i in range(len(s)):
		if s[i] == '1':
			currones += 1
		else:
			currzeros += 1
		ans = min(ans, currzeros + o - currones)
		ans = min(ans, currones + z - currzeros)
	print(ans)


for _ in range(int(input())):
	s = input()
	one = s.count('1')
	zero = s.count('0')
	ans = min(zero, one)
	one2 = zero2 = 0
	for item in s:
		if item == '0':
			zero -= 1
			zero2 += 1
		else:
			one -= 1
			one2 += 1
		ans = min(ans, zero + one2)
		ans = min(ans, one + zero2)
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	mini = 1000
	for i in range(len(s) + 1):
		onesleft = s[:i].count('1')
		onesright = s[i:].count('1')
		zerosleft = s[:i].count('0')
		zerosright = s[i:].count('0')
		if onesleft + zerosright < mini:
			mini = onesleft + zerosright
		if onesright + zerosleft < mini:
			mini = onesright + zerosleft
	print(mini)


t = int(input())
for _ in range(t):
	s = list(input())
	n = len(s)
	k = s.count('0')
	m = s.count('1')
	ans = m
	for i in range(n):
		if s[i] == '0':
			k = k - 1
			m = m + 1
		else:
			k = k + 1
			m = m - 1
		ans = min(ans, min(m, k))
	print(ans)


for k in range(int(input())):
	s = input()
	x = s.count('1')
	y = s.count('0')
	xx = 0
	yy = 0
	ans = 99999
	for i in s:
		if i == '1':
			xx += 1
		else:
			yy += 1
		ans = min(ans, yy + (x - xx))
		ans = min(ans, xx + (y - yy))
	print(ans)


t = int(input())
for i in range(t):
	string = input()
	totalzero = 0
	totalone = 0
	for k in range(len(string)):
		if string[k] == '0':
			totalzero += 1
		if string[k] == '1':
			totalone += 1
	firstpartzero = 0
	secondpartzero = 0
	firstpartone = 0
	secondpartone = 0
	maxizero = 10000000000000
	maxione = 1000000000000
	for y in range(len(string)):
		if string[y] == '0':
			firstpartzero += 1
		if string[y] == '1':
			firstpartone += 1
		secondpartzero = totalzero - firstpartzero
		secondpartone = totalone - firstpartone
		maxizero = min(maxizero, secondpartone + firstpartzero)
		maxione = min(maxione, secondpartzero + firstpartone)
	print(min(maxione, maxizero))


for _ in range(int(input())):
	data = input()
	length = len(data)
	before_0_list = [0] * length
	before_1_list = [0] * length
	after_0_list = [0] * length
	after_1_list = [0] * length
	before_0 = 0
	before_1 = 0
	after_0 = 0
	after_1 = 0
	for i in range(length):
		if data[i] == '0':
			before_0 += 1
		else:
			before_1 += 1
		before_0_list[i] = before_0
		before_1_list[i] = before_1
	for i in range(length - 1, -1, -1):
		if data[i] == '0':
			after_0 += 1
		else:
			after_1 += 1
		after_0_list[i] = after_0
		after_1_list[i] = after_1
	result = length
	for i in range(length):
		now = before_1_list[i] + after_0_list[i] - 1
		result = min(result, now)
		now = before_0_list[i] + after_1_list[i] - 1
		result = min(result, now)
	print(result)


from math import ceil
for _ in range(int(input())):
	s = input()
	mx = 0
	mn = 0
	n = 0
	lmx = []
	lmn = []
	for i in s:
		mx += abs(int(i) - 1)
		mn += abs(int(i) - 0)
		lmx.append(mx)
		lmn.append(mn)
		n += 1
	ans = mx
	(zeros, ones) = (0, 0)
	for i in range(n):
		if s[i] == '1':
			zeros += 1
		else:
			ones += 1
		cur1 = zeros + lmx[-1] - lmx[i]
		cur2 = ones + lmn[-1] - lmn[i]
		ans = min(ans, cur1, cur2)
	print(ans)


import sys

def input():
	return sys.stdin.readline().rstrip()
t = int(input())
answers = []
for _ in range(t):
	s = [int(i) for i in input()]
	n = len(s)
	pre_sum = [0]
	for a in s:
		pre_sum.append(pre_sum[-1] + a)
	best = n
	for i in range(n + 1):
		number_of_ones = pre_sum[i]
		number_of_zeros = n - i - (pre_sum[n] - pre_sum[i])
		best = min(number_of_ones + number_of_zeros, best)
	for i in range(n + 1):
		number_of_zeros = i - pre_sum[i]
		number_of_ones = pre_sum[n] - pre_sum[i]
		best = min(number_of_ones + number_of_zeros, best)
	answers.append(best)
print(*answers, sep='\n')


import math
t = int(input())
for i in range(t):
	s = input()
	ans = 1000000000
	d = []
	su = 0
	for i in range(len(s)):
		su += int(s[i])
		d.append(su)
	for i in range(len(s)):
		c = 0
		if s[i] == '0':
			if i != 0:
				c += d[i - 1]
			if i != len(s) - 1:
				f = d[-1] - d[i]
				g = len(s) - i - 1
				c += g - f
			ans = min(ans, c)
		else:
			if i != 0:
				g = d[i - 1]
				c += i - g
			if i != len(s) - 1:
				c += d[-1] - d[i]
			ans = min(ans, c)
	print(ans)


t = int(input())
for _ in range(t):
	s = [int(x) for x in list(input())]
	n = len(s)
	if n == 1 or n == 2:
		print(0)
		continue
	nuly_zlava = [None for _ in range(n)]
	jedn_zlava = [None for _ in range(n)]
	nuly_sprava = [None for _ in range(n)]
	jedn_sprava = [None for _ in range(n)]
	pocet_nul = 0
	pocet_jedn = 0
	for i in range(n):
		if s[i] == 0:
			pocet_nul += 1
			nuly_zlava[i] = pocet_nul
			jedn_zlava[i] = pocet_jedn
		else:
			pocet_jedn += 1
			jedn_zlava[i] = pocet_jedn
			nuly_zlava[i] = pocet_nul
	pocet_nul = 0
	pocet_jedn = 0
	for i in range(n):
		if s[n - 1 - i] == 0:
			pocet_nul += 1
			nuly_sprava[n - i - 1] = pocet_nul
			jedn_sprava[n - i - 1] = pocet_jedn
		else:
			pocet_jedn += 1
			jedn_sprava[n - i - 1] = pocet_jedn
			nuly_sprava[n - i - 1] = pocet_nul
	najmenej = 1000
	for i in range(n - 1):
		prva = nuly_zlava[i] + nuly_sprava[i + 1]
		druha = nuly_zlava[i] + jedn_sprava[i + 1]
		tretia = jedn_zlava[i] + nuly_sprava[i + 1]
		stvrta = jedn_zlava[i] + jedn_sprava[i + 1]
		najmenej = min(najmenej, prva, druha, tretia, stvrta)
	print(najmenej)


from sys import stdin as cin
from itertools import accumulate
lmap = lambda f, it: list(map(f, it))
laccumulate = lambda it, *f: list(accumulate(it, *f))

def min_changes0(s):
	zeros = [0] + laccumulate(s)
	ones = list(reversed(laccumulate(map(lambda x: 1 - x, reversed(s))))) + [0]
	return min(map(sum, zip(ones, zeros)))

def min_changes(s):
	if len(s) < 3:
		return 0
	changes0 = min_changes0(s)
	changes1 = min_changes0([1 - x for x in s])
	return min(changes0, changes1)

def main():
	t = int(next(cin).strip())
	for i in range(t):
		s = lmap(int, next(cin).strip())
		print(min_changes(s))
main()


import sys
import math
try:
	sys.stdin = open('Input.txt', 'r')
	sys.stdout = open('Output.txt', 'w')
except:
	pass
for testCases in range(int(input())):
	s = input()
	dp = [s.count('0')]
	for i in range(len(s)):
		if s[i] == '0':
			dp.append(dp[-1] - 1)
		else:
			dp.append(dp[-1] + 1)
	onedp = [s.count('1')]
	for i in range(len(s)):
		if s[i] == '1':
			onedp.append(onedp[-1] - 1)
		else:
			onedp.append(onedp[-1] + 1)
	print(min(min(dp), min(onedp)))


t = int(input())
for ii in range(t):
	s = input()
	c0 = s.count('0')
	c1 = s.count('1')
	if c0 == 0 or c1 == 0:
		print(0)
	else:
		curcnt0 = curcnt1 = 0
		ans = 100000001
		for i in range(len(s)):
			if s[i] == '0':
				curcnt0 += 1
			else:
				curcnt1 += 1
			ans = min(ans, curcnt1 + c0 - curcnt0, curcnt0 + c1 - curcnt1)
		print(ans)


testcases = int(input())
for case in range(testcases):
	arr = str(input())
	ones = 0
	zeros = 0
	for i in arr:
		if i == '0':
			zeros += 1
		else:
			ones += 1
	ones_occured = 0
	zeros_occured = 0
	ans = []
	for i in arr:
		if i == '0':
			ans0 = 0
			ans1 = 0
			ans0 += ones_occured + (zeros - zeros_occured)
			ans1 += zeros_occured + (ones - ones_occured)
			ans.append(ans0)
			ans.append(ans1)
			zeros_occured += 1
		else:
			ans0 = 0
			ans1 = 0
			ans0 += ones_occured + (zeros - zeros_occured)
			ans1 += zeros_occured + (ones - ones_occured)
			ans.append(ans0)
			ans.append(ans1)
			ones_occured += 1
	print(min(ans))


n = int(input())

def solution(str):
	ret = float('inf')
	dp = [0] * (len(str) + 1)
	for (i, ch) in enumerate(str):
		dp[i] = dp[i - 1] + (1 if ch == '1' else 0)
	for i in range(len(str)):
		sofar = dp[i - 1]
		after = dp[-2] - dp[i - 1]
		ret = min(ret, sofar + (len(str) - i) - after)
		ret = min(ret, i - sofar + after)
	return ret
for _ in range(n):
	print(solution(input()))


import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline

def geta(fn=lambda s: s.decode()):
	return map(fn, readline().split())

def gete(fn=lambda s: s.decode()):
	return fn(readline().rstrip())

def main():
	for _ in range(gete(int)):
		s = gete()
		n = len(s)
		ans = n
		for i in range(n + 1):
			ans = min(ans, s[:i].count('0') + s[i:].count('1'), s[:i].count('1') + s[i:].count('0'))
		print(ans)
main()


t = int(input())

def count_ones(s):
	return sum((x == '1' for x in s))

def count_zeroes(s):
	return sum((x == '0' for x in s))
for case in range(t):
	s = input()
	ans = len(s)
	total_ones = count_ones(s)
	total_zeroes = count_zeroes(s)
	left_ones = 0
	left_zeroes = 0
	for sep in range(len(s) + 1):
		if sep > 0:
			if s[sep - 1] == '0':
				left_zeroes += 1
			else:
				left_ones += 1
		ans = min(ans, left_zeroes + (total_ones - left_ones), left_ones + (total_zeroes - left_zeroes))
	print(ans)


test = int(input())
for _ in range(test):
	s = input()
	s0 = 0
	s1 = 0
	for i in s:
		s0 += int(i == '0')
		s1 += int(i == '1')
	p0 = 0
	p1 = 0
	ans = min(s0, s1)
	for i in s:
		p0 += int(i == '0')
		p1 += int(i == '1')
		s0 -= int(i == '0')
		s1 -= int(i == '1')
		ans = min(ans, p0 + s1, p1 + s0)
	print(ans)


T = int(input())
for i in range(T):
	s = input()
	l = []
	ans = 99999999999
	n = len(s)
	for i in range(n):
		c = s[i]
		ls = s[:i]
		rs = s[i + 1:]
		lsz = ls.count('0')
		lso = ls.count('1')
		rsz = rs.count('0')
		rso = rs.count('1')
		ans = min(ans, lsz + rso, lso + rsz)
	print(ans)


for _ in range(int(input())):
	m = input()
	(c1, c0) = (0, 0)
	P1 = [0] * len(m)
	P0 = [0] * len(m)
	for i in range(len(m)):
		if m[i] == '1':
			c1 += 1
			P1[i] = c1
			P0[i] = c0
		else:
			c0 += 1
			P1[i] = c1
			P0[i] = c0
	ans = min(c0, c1)
	for i in range(len(m)):
		ans = min(ans, P1[i] + c0 - P0[i])
		ans = min(ans, P0[i] + c1 - P1[i])
	print(ans)


import sys
input = lambda : sys.stdin.readline().rstrip()
for _ in range(int(input())):
	s = input()
	p1 = []
	p2 = []
	cnt = 0
	for i in s:
		if i == '1':
			cnt += 1
		p1.append(cnt)
	cnt2 = 0
	for i in range(len(s)):
		if s[-i - 1] == '0':
			cnt2 += 1
		p2.append(cnt2)
	p2.reverse()
	a = 10 ** 6 + 1
	for i in range(len(p1) - 1):
		a = min(a, p1[i] + p2[i + 1], len(p1) - (p1[i] + p2[i + 1]))
	print(min(p2[0], a, p1[-1]))


from sys import *

def solve(s):
	n = len(s)
	a = [0] * n
	b = [0] * n
	rc = 0
	for i in range(n):
		if s[i] == '1':
			rc += 1
		a[i] = rc
	rc = 0
	for i in range(n - 1, -1, -1):
		b[i] = rc
		if s[i] == '0':
			rc += 1
	c = [0] * n
	for i in range(n):
		c[i] = a[i] + b[i]
	m1 = min(c)
	a = [0] * n
	b = [0] * n
	rc = 0
	for i in range(n):
		if s[i] == '0':
			rc += 1
		a[i] = rc
	rc = 0
	for i in range(n - 1, -1, -1):
		b[i] = rc
		if s[i] == '1':
			rc += 1
	c = [0] * n
	for i in range(n):
		c[i] = a[i] + b[i]
	m2 = min(c)
	return min(m2, m1)
	pass
test = 1
test = int(input())
for t in range(0, test):
	s = input()
	ans = solve(s)
	print(ans)


import math
for _ in range(int(input())):
	s = input()
	s = list(s)
	c0 = s.count('0')
	c1 = s.count('1')
	ans = min(c0, c1)
	if '0' not in s or '1' not in s:
		print(0)
	else:
		d0 = 0
		d1 = 0
		for i in s:
			if i == '1':
				d1 += 1
				c1 -= 1
			else:
				d0 += 1
				c0 -= 1
			ans = min(ans, d1 + c0, d0 + c1)
		print(ans)


import math
import os
import random
import re
import sys

def subsequence_hate(s):
	zc = len(list(filter(lambda c: c == '0', s)))
	nc = len(s) - zc
	minCount = min(zc, nc)
	lz = 0
	for (i, c) in enumerate(s):
		ln = i - lz
		(rz, rn) = (zc - lz, nc - ln)
		count = min(lz + rn, ln + rz)
		minCount = min(minCount, count)
		lz += 1 if c == '0' else 0
	return minCount
t = int(input())
for i in range(t):
	s = input().rstrip()
	result = subsequence_hate(s)
	print(result)


for i in range(int(input())):
	b = input()
	best = 10 ** 10
	for j in range(len(b)):
		zb = b[j:].count('0')
		ob = b[j:].count('1')
		za = b[:j].count('0')
		oa = b[:j].count('1')
		best = min(best, zb + oa, ob + za)
	print(best)


t = int(input())
for _ in range(t):
	s = str(input())
	numb_one = s.count('1')
	numb_zero = s.count('0')
	complete_one = 0
	complete_zero = 0
	var1 = 0
	var2 = 0
	M = 10 ** 9
	for i in range(len(s)):
		if s[i] == '1':
			complete_one += 1
		else:
			complete_zero += 1
		M = min(M, complete_zero + numb_one - complete_one, complete_one + numb_zero - complete_zero)
	print(M)


for t in range(int(input())):
	s = input()
	n = len(s)
	done_zero = 0
	done_one = 0
	num_zero = list(s).count('0')
	num_one = list(s).count('1')
	m = 1001
	for i in range(n):
		if s[i] == '0':
			done_zero += 1
		else:
			done_one += 1
		m = min(m, done_zero + num_one - done_one, done_one + num_zero - done_zero)
	m = min(m, num_one, num_zero)
	print(m)


t = int(input())
for _ in range(t):
	s = input()
	(res, total, d) = (len(s), 0, {})
	for i in range(len(s)):
		total += int(s[i])
		d[i] = total
	for i in range(len(s)):
		res = min(res, min(i + 1 - d[i], d[i]) + min(total - d[i], len(s) - 1 - i - total + d[i]))
	print(res)


t = int(input())
for i in range(t):
	binstr = list(input())
	nrzero = 0
	nrone = 0
	cost = len(binstr)
	for bit in binstr:
		if bit == '0':
			nrzero += 1
		else:
			nrone += 1
	lclzero = 0
	lclone = 0
	for bit in binstr:
		if bit == '0':
			lclzero += 1
		else:
			lclone += 1
		if lclzero + nrone - lclone < cost:
			cost = lclzero + nrone - lclone
		if lclone + nrzero - lclzero < cost:
			cost = lclone + nrzero - lclzero
	print(cost)


t = int(input())
while t > 0:
	s = input()
	ans = 1000
	io = [0 for x in range(len(s) + 1)]
	eo = [0 for x in range(len(s) + 1)]
	iz = [0 for x in range(len(s) + 1)]
	ez = [0 for x in range(len(s) + 1)]
	z = 0
	o = 0
	for i in range(1, len(s) + 1):
		if s[i - 1] == '0':
			z += 1
		else:
			o += 1
		io[i] = o
		iz[i] = z
	z = 0
	o = 0
	for i in range(1, len(s) + 1):
		if s[len(s) - i] == '0':
			z += 1
		else:
			o += 1
		eo[i] = o
		ez[i] = z
	for i in range(len(s) + 1):
		if io[i] + ez[len(s) - i] < ans:
			ans = io[i] + ez[len(s) - i]
		if iz[i] + eo[len(s) - i] < ans:
			ans = iz[i] + eo[len(s) - i]
	print(ans)
	t -= 1


from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
for __ in range(int(input())):
	st = input()
	ans = inf
	n = len(st)
	p = [0] * n
	s = [0] * n
	for i in range(n):
		if i == 0:
			if st[i] == '1':
				p[i] += 1
		else:
			p[i] = p[i - 1]
			if st[i] == '1':
				p[i] += 1
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			if st[i] == '0':
				s[i] += 1
		else:
			s[i] = s[i + 1]
			if st[i] == '0':
				s[i] += 1
	for i in range(0, n - 1):
		ans = min(ans, p[i] + s[i + 1])
	ans = min(s[0], p[n - 1], ans)
	p = [0] * n
	s = [0] * n
	for i in range(n):
		if i == 0:
			if st[i] == '0':
				p[i] += 1
		else:
			p[i] = p[i - 1]
			if st[i] == '0':
				p[i] += 1
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			if st[i] == '1':
				s[i] += 1
		else:
			s[i] = s[i + 1]
			if st[i] == '1':
				s[i] += 1
	for i in range(0, n - 1):
		ans = min(ans, p[i] + s[i + 1])
	ans = min(s[0], p[n - 1], ans)
	print(ans)


(t, r01, r10) = (int(input()), 0, 0)
while t:
	t -= 1
	(s, r01, r10) = (input(), 100000, 100000)
	(c0, c1) = (0, sum((1 for i in range(len(s)) if s[i] == '1')))
	res = min(c1, len(s) - c1)
	for i in range(len(s)):
		r01 = min(i - c0 + len(s) - i - c1, r01)
		if s[i] == '0':
			c0 += 1
		else:
			c1 -= 1
	(c0, c1) = (sum((1 for i in range(len(s)) if s[i] == '0')), 0)
	for i in range(len(s)):
		r10 = min(i - c1 + len(s) - i - c0, r10)
		if s[i] == '1':
			c1 += 1
		else:
			c0 -= 1
	res = min(r01, r10, res)
	print(res)


for _ in range(int(input())):
	s = input()
	n = len(s)
	res = n
	n0 = [0]
	for i in range(n):
		if s[i] == '0':
			n0.append(n0[-1] + 1)
		else:
			n0.append(n0[-1])
	for i in range(1, n + 1):
		res = min(res, i - n0[i] + n0[n] - n0[i], n0[i] + n - i - (n0[n] - n0[i]))
	print(res)


for _ in range(int(input())):
	s = input()
	n = len(s)
	c = 0
	cc = s.count('1')
	m = 1000
	for i in range(n):
		c += int(s[i])
		cc -= int(s[i])
		if i + 1 - c + cc < m:
			m = i + 1 - c + cc
		if c + n - i - 1 - cc < m:
			m = c + n - i - 1 - cc
		if i + 1 - c + n - i - 1 - cc < m:
			i + 1 - c + n - i - 1 - cc
		if c + cc < m:
			c + n - i - 1 - cc
	print(m)


t = int(input())
for _ in range(t):
	s = str(input())
	if len(s) < 3:
		print(0)
		continue
	n = len(s)
	zeros = s.count('0')
	d0 = dict()
	d1 = dict()
	curr_zerous = 0
	curr_ones = 0
	for i in range(n):
		if s[i] == '0':
			curr_zerous += 1
		else:
			curr_ones += 1
		d0[i] = zeros - curr_zerous
		d1[i] = n - zeros - curr_ones
	ans = n
	changes0 = 0
	changes1 = 0
	for i in range(n):
		if s[i] == '1':
			changes1 += 1
		else:
			changes0 += 1
		ans = min(ans, changes1 + d0[i], changes0 + d1[i])
	print(ans)


import sys
from collections import defaultdict as dd
from collections import deque
from fractions import Fraction as f

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
from math import *
import copy
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

def li():
	return [int(x) for x in input().split()]

def fi():
	return int(input())

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def bo(i):
	return ord(i) - ord('a')
from copy import *
from bisect import *
t = fi()
while t > 0:
	t -= 1
	s = si()
	n = len(s)
	z = [0 for i in range(n)]
	o = [0 for i in range(n)]
	for i in range(n):
		z[i] = z[i - 1] + int(s[i] == '0')
		o[i] = o[i - 1] + int(s[i] == '1')
	mini = n
	for i in range(n):
		mini = min([mini, o[i] + z[-1] - z[i], z[i] + o[-1] - o[i]])
	print(mini)


def answer(s):
	if len(s) < 3:
		return 0
	if s.count('0') == 0:
		return 0
	if s.count('1') == 0:
		return 0
	num_switches = 0
	for i in range(1, len(s)):
		if s[i] != s[i - 1]:
			num_switches += 1
	if num_switches <= 1:
		return 0
	cnt = 0
	for i in range(0, len(s) - 2):
		if s[i:i + 3] == '101':
			s[i + 1] = '1'
			num_switches -= 2
			cnt += 1
		elif s[i:i + 3] == '010':
			s[i + 1] = '0'
			num_switches -= 2
			cnt += 1
	if num_switches <= 1:
		return cnt
	if s[0] != s[1]:
		s[0] = s[1]
		num_switches -= 1
		cnt += 1
	if num_switches <= 1:
		return cnt
	if s[-1] != s[-2]:
		s[-1] = s[-2]
		num_switches -= 1
		cnt += 1
	if num_switches <= 1:
		return cnt
	return

def answer2(s):
	if len(s) < 3:
		return 0
	t0 = s.count('0')
	t1 = s.count('1')
	if t0 == 0:
		return 0
	if t1 == 0:
		return 0
	d0 = 0
	d1 = 0
	min_cost = 10000
	for i in range(len(s)):
		if s[i] == '0':
			d0 += 1
		else:
			d1 += 1
		costb4i = min(d0, d1)
		costafi = min(t0 - d0, t1 - d1)
		if costb4i + costafi < min_cost:
			min_cost = costb4i + costafi
	return min_cost

def main():
	t = int(input())
	while t:
		s = input()
		print(answer2(s))
		t -= 1
	return
main()


t = int(input())
while t != 0:
	n = input()
	upto_zer = []
	upto_one = []
	ze = 0
	on = 0
	for i in range(len(n)):
		if n[i] == '0':
			ze += 1
		else:
			on += 1
		upto_zer.append(ze)
		upto_one.append(on)
	final_list = list()
	for i in range(len(n)):
		p = upto_zer[i] + on - upto_one[i]
		q = upto_one[i] + ze - upto_zer[i]
		final_list.append(min(p, q))
	print(min(final_list))
	t -= 1


def fn():
	s = input()
	n = len(s)
	z = s.count('0')
	o = n - z
	if z == 0 or o == 0:
		return 0
	(pz, po, m) = (0, 0, 10 ** 9)
	for i in range(n):
		if s[i] == '0':
			pz += 1
		else:
			po += 1
		c = min(pz + (o - po), po + (z - pz))
		m = min(c, m)
	return m
for _ in range(int(input())):
	print(fn())


import math
import sys
from sys import stdin
input = sys.stdin.readline
for _ in range(int(input())):
	s = input()
	c1 = s.count('1')
	c0 = s.count('0')
	dp = []
	temp1 = 0
	temp0 = 0
	for i in range(len(s)):
		if s[i] == '0':
			l = min(temp1 + c0 - temp0, c1 - temp1 + temp0)
			dp.append(l)
			temp0 += 1
		else:
			dp.append(min(temp0 + c1 - temp1, c0 - temp0 + temp1))
			temp1 += 1
	print(min(dp))


t = int(input())
for test in range(t):
	s = input()
	n = len(s)
	num_zeroes = num_ones = 0
	for i in range(n):
		if s[i] == '0':
			num_zeroes += 1
		else:
			num_ones += 1
	done_zeroes = done_ones = 0
	ans = min(num_zeroes, num_ones)
	for i in range(n):
		if s[i] == '0':
			done_zeroes += 1
		else:
			done_ones += 1
		ans = min(done_ones + num_zeroes - done_zeroes, ans)
		ans = min(done_zeroes + num_ones - done_ones, ans)
	print(ans)


for _ in range(int(input())):
	s = input()
	zero_prefix = [0] * len(s)
	one_prefix = [0] * len(s)
	c = []
	if s[0] == '0':
		zero_prefix[0] = 1
	if s[0] == '1':
		one_prefix[0] = 1
	for i in range(1, len(s)):
		if s[i] == '1':
			one_prefix[i] = one_prefix[i - 1] + 1
			zero_prefix[i] = zero_prefix[i - 1]
		if s[i] == '0':
			zero_prefix[i] = zero_prefix[i - 1] + 1
			one_prefix[i] = one_prefix[i - 1]
	for i in range(len(s)):
		c.append(one_prefix[len(s) - 1] - one_prefix[i] + zero_prefix[i])
		c.append(zero_prefix[len(s) - 1] - zero_prefix[i] + one_prefix[i])
	print(min(c))


import sys
import math
from collections import defaultdict

def solve(test):
	s = input()
	n = len(s)
	(ones, zeros) = (0, 0)
	for c in s:
		if c == '1':
			ones += 1
		else:
			zeros += 1
	if ones == n or zeros == n:
		print(0)
		return
	(ones_so_far, zeros_so_far) = (0, 0)
	ans = sys.maxsize
	for i in range(n):
		if s[i] == '1':
			ones_so_far += 1
		else:
			zeros_so_far += 1
		pre_ones = zeros_so_far + ones - ones_so_far
		post_ones = ones_so_far + zeros - zeros_so_far
		ans = min(ans, pre_ones, post_ones)
	print(ans)
test_cases = int(input())
for t in range(1, test_cases + 1):
	solve(t)


def solve():
	s = input()
	zeros = 0
	ones = 0
	for char in s:
		zeros += char == '0'
		ones += char == '1'
	ans = 1000000
	left_ones = 0
	left_zeros = 0
	right_zeros = zeros
	right_ones = ones
	if len(s) == 1:
		print(0)
		return 0
	for char in s:
		is_zero = char == '0'
		if is_zero:
			left_zeros += 1
			right_zeros -= 1
		else:
			left_ones += 1
			right_ones -= 1
		zero_one_cost = left_ones + right_zeros
		ans = min(ans, zero_one_cost)
		one_zero_cost = left_zeros + right_ones
		ans = min(ans, one_zero_cost)
	print(ans)
t = int(input())
for _ in range(t):
	solve()


def rs():
	return input().strip()

def ri():
	return int(input())

def ria():
	return list(map(int, input().split()))

def ia_to_s(a):
	return ' '.join([str(s) for s in a])

def solve(s):
	n = len(s)
	s = [int(si) for si in s]
	ones = sum(s)
	zeros = n - ones
	ans = n
	ones_i = 0
	zeros_i = 0
	for i in range(0, n):
		if s[i] == 1:
			ones_i += 1
		if s[i] == 0:
			zeros_i += 1
		m1 = zeros_i + ones - ones_i
		m2 = ones_i + zeros - zeros_i
		ans = min(ans, m1, m2)
	return ans

def main():
	for _ in range(ri()):
		s = rs()
		print(solve(s))
main()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	L = list(input())
	L.pop()
	ans = float('inf')
	N = len(L)
	if '1' not in L or '0' not in L:
		print(0)
		continue
	prefix = [0] * N
	for i in range(N):
		if L[i] == '0':
			prefix[i] = 1
	for i in range(1, N):
		prefix[i] += prefix[i - 1]
	S = prefix[N - 1]
	for i in range(N):
		n1 = i + 1 - prefix[i]
		n0 = prefix[i]
		ans = min(ans, n1 + S - n0)
		ans = min(ans, n0 + (N - (i + 1)) - (S - n0))
	print(ans)


t = int(input())
for round in range(t):
	s = input()
	n = len(s)
	l = []
	i = 0
	while True:
		cnt = 1
		while i < n - 1 and s[i] == s[i + 1]:
			cnt += 1
			i += 1
		l.append(cnt)
		if i == n - 1:
			break
		i += 1
	if len(l) == 1:
		print(0)
		continue
	dp = [0 for i in range(len(l))]
	dp[0] = sum(l[2::2])
	dp[1] = l[0] + sum(l[3::2])
	for i in range(2, len(l)):
		dp[i] = dp[i - 2] + l[i - 1] - l[i]
	print(min(dp))


t = int(input())
for _ in range(t):
	s = input()
	maxi = 0
	c1 = s.count('1')
	c0 = s.count('0')
	l1 = 0
	l0 = 0
	r1 = c1
	r0 = c0
	maxi = 0
	arr = [0 for i in range(len(s) + 1)]
	for i in range(len(s)):
		if s[i] == '1':
			l1 += 1
			r1 -= 1
			maxi = max(maxi, l1 + r0, l0 + r1)
		else:
			l0 += 1
			r0 -= 1
			maxi = max(maxi, l1 + r0, l0 + r1)
	print(len(s) - maxi)


test = int(input())
while test > 0:
	test -= 1
	s = input()
	n = len(s)
	ans = 10000
	for i in range(n):
		c = s[:i].count('1') + s[i:].count('0')
		d = s[:i].count('0') + s[i:].count('1')
		ans = min(ans, c, d)
	print(ans)


t = int(input())
for test in range(t):
	s = input()
	min_ways = 10000
	n = len(s)
	ways = 0
	for i in range(n):
		if s[i] == '1':
			ways += 1
	min_ways = min(min_ways, ways)
	for i in range(n):
		if s[i] == '1':
			ways -= 1
		else:
			ways += 1
		min_ways = min(min_ways, ways)
	ways = 0
	for i in range(n):
		if s[i] == '0':
			ways += 1
	min_ways = min(min_ways, ways)
	for i in range(n):
		if s[i] == '0':
			ways -= 1
		else:
			ways += 1
		min_ways = min(min_ways, ways)
	print(min_ways)


T = int(input())
for _ in range(T):
	s = input()
	ans = len(s)
	tot_zeros = sum([c == '0' for c in s])
	tot_ones = sum([c == '1' for c in s])
	curr_zeros = 0
	curr_ones = 0
	for i in range(len(s)):
		ans = min(ans, min(curr_ones, curr_zeros) + min(tot_ones - curr_ones, tot_zeros - curr_zeros))
		if s[i] == '0':
			curr_zeros += 1
		else:
			curr_ones += 1
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	count_z = count_o = 0
	for ch in s:
		if ch == '1':
			count_o += 1
		else:
			count_z += 1
	if count_z == 0 or count_o == 0:
		print(0)
	else:
		min_val = len(s)
		yet_z = yet_o = 0
		for ch in s:
			if ch == '1':
				yet_o += 1
			else:
				yet_z += 1
			min_val = min(min_val, yet_o + count_z - yet_z, yet_z + count_o - yet_o)
		print(min_val)


T = int(input())
for _ in range(T):
	Str = list(input())
	Sum = 0
	oneP = []
	zeroP = []
	for i in range(len(Str)):
		if Str[i] == '1':
			Sum += 1
		oneP.append(Sum)
		zeroP.append(i + 1 - Sum)
	Ans = 10000000000000000
	for i in range(len(Str)):
		A = oneP[i]
		B = oneP[len(Str) - 1] - oneP[i]
		C = zeroP[i - 1]
		D = zeroP[len(Str) - 1] - zeroP[i]
		Ans = min(C + B, A + D, Ans)
	print(Ans)


for q in range(int(input())):
	s = input()
	count_0 = 0
	count_1 = 0
	for i in range(len(s)):
		if s[i] == '0':
			count_0 += 1
		else:
			count_1 += 1
	now_0 = 0
	now_1 = 0
	min_operations = len(s)
	for i in range(len(s)):
		if s[i] == '0':
			now_0 += 1
		else:
			now_1 += 1
		min_operations = min(min_operations, i + 1 - now_0 + len(s) - i - 1 - (count_1 - now_1), i + 1 - now_1 + len(s) - i - 1 - (count_0 - now_0))
	print(min_operations)


import time

def subseq_hate(array):
	min_ops = None
	n = len(array)
	for i in range(len(array)):
		lower = array[:i]
		upper = array[i:]
		lower1 = sum(lower)
		upper1 = sum(upper)
		lower0 = i - lower1
		upper0 = n - i - upper1
		new_ops = min(lower0 + upper1, lower1 + upper0)
		if min_ops == None or new_ops < min_ops:
			min_ops = new_ops
			if min_ops == 0:
				return min_ops
	return min_ops
test_cases = int(input())
for i in range(test_cases):
	array = [int(i) for i in input()]
	print(subseq_hate(array))


from itertools import accumulate
for _ in range(int(input())):
	xs = list(map(int, input()))
	c1 = len(list(filter(lambda x: x == 1, xs)))
	c0 = len(xs) - c1
	d1 = accumulate(xs)
	d0 = accumulate(map(lambda x: not x, xs))
	m = len(xs)
	for (i, j) in zip(d0, d1):
		m = min([i + c1 - j, j + c0 - i, m])
	print(m)


import math
import os
import random
import re
import sys
t = int(input())
while t > 0:
	t -= 1
	s = list(map(int, list(input())))
	zeros = [0] * len(s)
	ones = [0] * len(s)
	if s[0] == 0:
		zeros[0] = 1
	else:
		ones[0] = 1
	if len(s) == 0:
		print('0')
		continue
	for i in range(1, len(s)):
		if s[i] == 0:
			zeros[i] = 1 + zeros[i - 1]
			ones[i] = ones[i - 1]
		else:
			zeros[i] = zeros[i - 1]
			ones[i] = 1 + ones[i - 1]
	mi = 10 ** 10
	n = len(s) - 1
	for i in range(1, len(s)):
		cost_10 = zeros[i - 1] + (ones[n] - ones[i])
		if cost_10 < mi:
			mi = cost_10
		cost_01 = ones[i - 1] + (zeros[n] - zeros[i])
		if cost_01 < mi:
			mi = cost_01
	if mi == 10 ** 10:
		mi = 0
	print(mi)


def solve(s):
	(t0, t1) = (0, 0)
	for ch in s:
		if ch == '0':
			t0 += 1
		else:
			t1 += 1
	ans = min(t0, t1)
	(n0, n1) = (0, 0)
	for ch in s:
		if ch == '0':
			n0 += 1
			t0 = t0 - 1 if t0 > 0 else 0
			ans = min(ans, n1 + t0)
		else:
			n1 += 1
			t1 = t1 - 1 if t1 > 0 else 0
			ans = min(ans, n0 + t1)
	return ans
t = int(input())
for _ in range(t):
	s = input()
	print(solve(s))


import os, sys, heapq as h, time
from io import BytesIO, IOBase
from types import GeneratorType
from bisect import bisect_left, bisect_right
from collections import defaultdict as dd, deque as dq, Counter as dc
import math, string
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def getInt():
	return int(input())

def getStrs():
	return input().split()

def getInts():
	return list(map(int, input().split()))

def getStr():
	return input()

def listStr():
	return list(input())

def getMat(n):
	return [getInts() for _ in range(n)]

def isInt(s):
	return '0' <= s[0] <= '9'
MOD = 10 ** 9 + 7

def solve():
	S = [int(s) for s in input()]
	N = len(S)
	ans = 10 ** 18
	for i in range(N + 1):
		for op in range(2):
			tmp = 0
			for j in range(N):
				if j < i:
					tmp += S[j] ^ op
				else:
					tmp += S[j] ^ op ^ 1
			ans = min(ans, tmp)
	return ans
for _ in range(getInt()):
	print(solve())


from collections import defaultdict
itl = lambda : list(map(int, input().strip().split()))

def solve():
	s = input().strip()
	N = len(s)
	zs = [0] * (N + 1)
	os = [0] * (N + 1)
	for i in range(N):
		zs[i + 1] += zs[i]
		os[i + 1] += os[i]
		if s[i] == '0':
			zs[i + 1] += 1
		else:
			os[i + 1] += 1
	ans = N
	z = 0
	o = 0
	for i in range(N - 1, -1, -1):
		if s[i] == '0':
			z += 1
		else:
			o += 1
		ans = min(ans, min(o, z) + min(os[i], zs[i]))
	return ans
T = int(input())
for _ in range(T):
	print(solve())


t = int(input())
for _ in range(t):
	s = input()
	count_0 = 0
	count_1 = 0
	for c in s:
		if c == '0':
			count_0 += 1
		if c == '1':
			count_1 += 1
	min_change = len(s)
	current_0 = 0
	current_1 = 0
	for c in s:
		if c == '0':
			current_0 += 1
		if c == '1':
			current_1 += 1
		current_change = min(current_0, current_1) + min(abs(count_0 - current_0), abs(count_1 - current_1))
		if current_change < min_change:
			min_change = current_change
	print(min_change)


import sys
input = sys.stdin.readline
T = int(input())
for testcase in range(T):
	s = list(input())
	while not s[-1] in ['0', '1']:
		s.pop()
	n = len(s)
	whi = [0] * n
	for i in range(n):
		if s[i] == '0':
			whi[i] = whi[i - 1] + 1
		else:
			whi[i] = whi[i - 1]
	res = min(whi[-1], n - whi[-1])
	for i in range(n):
		res = min(res, i + 1 - whi[i] + (whi[-1] - whi[i]), whi[i] + (n - 1 - i) - (whi[-1] - whi[i]))
	print(res)


for _ in range(int(input())):
	s = input()
	cnt0 = s.count('0')
	cnt1 = s.count('1')
	l = [cnt0, cnt1]
	i = 0
	d0 = 0
	d1 = 0
	while i < len(s):
		x = s[i]
		if x == '0':
			d0 = d0 + 1
		else:
			d1 = d1 + 1
		o = d0 + cnt1 - d1
		t = d1 + cnt0 - d0
		l.append(o)
		l.append(t)
		i = i + 1
	print(min(l))


for _ in range(int(input())):
	s = input()
	n = len(s)
	s1 = s.count('1')
	c = 0
	mi = 10000000000
	for i in range(1, n + 1):
		if s[i - 1] == '1':
			c += 1
		mi = min(mi, s1 - c + i - c, c + n - i - (s1 - c))
	print(mi)


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	a = b = c = d = 0
	ans = float('inf')
	for i in range(n):
		if s[i] == '0':
			c += 1
		else:
			d += 1
	for i in range(n):
		ans = min(ans, a + d, b + c)
		if s[i] == '0':
			a += 1
			c -= 1
		else:
			b += 1
			d -= 1
	print(ans)


import sys
for _ in range(int(input())):
	s = input()
	n = len(s)
	ans = sys.maxsize
	l = [[0, 0] for i in range(n)]
	r = [[0, 0] for i in range(n)]
	for i in range(n):
		if i == 0:
			if s[i] == '0':
				l[i][0] += 1
			else:
				l[i][1] += 1
		else:
			l[i][0] = l[i - 1][0]
			l[i][1] = l[i - 1][1]
			if s[i] == '0':
				l[i][0] += 1
			else:
				l[i][1] += 1
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			if s[i] == '0':
				r[i][0] += 1
			else:
				r[i][1] += 1
		else:
			r[i][0] = r[i + 1][0]
			r[i][1] = r[i + 1][1]
			if s[i] == '0':
				r[i][0] += 1
			else:
				r[i][1] += 1
	for i in range(n):
		ans = min(ans, l[i][1] + r[i][0] - 1, l[i][0] - 1 + r[i][1])
	print(ans)


t = int(input())
for i in range(t):
	s = input()
	l1 = [0] * (len(s) + 1)
	l2 = [0] * (len(s) + 1)
	for j in range(len(s)):
		if s[j] == '0':
			l1[j + 1] = l1[j] + 1
			l2[j + 1] = l2[j]
		else:
			l2[j + 1] = l2[j] + 1
			l1[j + 1] = l1[j]
	n0 = l1[len(l1) - 1]
	n1 = l2[len(l2) - 1]
	ans = len(s)
	for j in range(len(s) + 1):
		ans = min(l1[j] + n1 - l2[j], ans)
		ans = min(l2[j] + n0 - l1[j], ans)
	print(ans)


def changes(s, s1):
	count = 0
	for i in range(len(s)):
		if s[i] != s1[i]:
			count = count + 1
	return count
t = int(input())
for i in range(t):
	s = input()
	zero_count = 0
	zero_counts = []
	for j in s:
		if j == '0':
			zero_count = zero_count + 1
		zero_counts.append(zero_count)
	total_zeros = s.count('0')
	m = []
	for j in range(len(s)):
		se = j
		left_zero_count = zero_counts[se]
		right_zero_count = total_zeros - left_zero_count
		left_one_count = se + 1 - left_zero_count
		right_one_count = len(s) - se - 1 - right_zero_count
		new_s_1_0 = left_zero_count + right_one_count
		new_s_0_1 = left_one_count + right_zero_count
		m.append(min(new_s_1_0, new_s_0_1))
	print(min(m))


import sys
buff_readline = sys.stdin.readline
readline = sys.stdin.readline
INF = 2 ** 62 - 1

def read_int():
	return int(buff_readline())

def read_int_n():
	return list(map(int, buff_readline().split()))

def read_float():
	return float(buff_readline())

def read_float_n():
	return list(map(float, buff_readline().split()))

def read_str():
	return readline().strip()

def read_str_n():
	return readline().strip().split()

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

def slv(S):
	ans = INF
	ans = min(ans, S.count('0'))
	ans = min(ans, S.count('1'))
	os = [0]
	zs = [0]
	for c in S:
		if c == '0':
			os.append(os[-1])
			zs.append(zs[-1] + 1)
		else:
			os.append(os[-1] + 1)
			zs.append(zs[-1])
	for i in range(len(S)):
		ans = min(ans, os[i + 1] - os[0] + (zs[-1] - zs[i]) - 1)
		ans = min(ans, zs[i + 1] - zs[0] + (os[-1] - os[i]) - 1)
	return ans

def main():
	T = read_int()
	for _ in range(T):
		S = read_str()
		print(slv(S))
main()


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]

def main():
	for _ in range(II()):
		aa = list(map(int, list(SI())))
		n = len(aa)
		ll = [0] * (n + 1)
		rr = [0] * (n + 1)
		for (i, a) in enumerate(aa):
			ll[i + 1] = ll[i] + a
		for i in range(n - 1, -1, -1):
			rr[i] = rr[i + 1] + aa[i]
		ans = 10 ** 9
		for i in range(n + 1):
			ans = min(ans, ll[i] + n - i - rr[i])
			ans = min(ans, i - ll[i] + rr[i])
		print(ans)
main()


import math
import sys
import collections
from collections import defaultdict
from sys import stdin, stdout
sys.setrecursionlimit(10 ** 9)
for _ in range(int(input())):
	s = input()
	s = '*' + s + '*'
	n = len(s)
	zeros = [0] * n
	ones = [0] * n
	for i in range(1, n):
		if s[i] == '0':
			zeros[i] = zeros[i - 1] + 1
		else:
			zeros[i] = zeros[i - 1]
	for i in range(n - 2, -1, -1):
		if s[i] == '1':
			ones[i] = ones[i + 1] + 1
		else:
			ones[i] = ones[i + 1]
	ans = float('inf')
	for i in range(n - 1):
		ans = min(ans, ones[i + 1] + zeros[i])
	zeros = [0] * n
	ones = [0] * n
	for i in range(1, n):
		if s[i] == '1':
			zeros[i] = zeros[i - 1] + 1
		else:
			zeros[i] = zeros[i - 1]
	for i in range(n - 2, -1, -1):
		if s[i] == '0':
			ones[i] = ones[i + 1] + 1
		else:
			ones[i] = ones[i + 1]
	for i in range(n - 1):
		ans = min(ans, ones[i + 1] + zeros[i])
	print(ans)


n = int(input())
answers = [] * 0
for i in range(n):
	test = input()
	possible = [] * 0
	if len(test) < 3:
		possible.append(0)
	for j in range(len(test)):
		possible.append(test[:j].count('0') + test[j:].count('1'))
		possible.append(test[:j].count('1') + test[j:].count('0'))
	answers.append(min(possible))
for answer in answers:
	print(answer)


T = int(input())
for _ in range(T):
	S = input()
	ch = [0]
	for (i, (a, b)) in enumerate(zip(S, S[1:])):
		if a != b:
			ch.append(i + 1)
	ch.append(len(S))
	if len(ch) <= 3:
		print(0)
		continue
	zeros = [0]
	ones = [0]
	for c in S:
		zeros.append(zeros[-1] + int(c == '0'))
		ones.append(ones[-1] + int(c == '1'))
	ans = min(zeros[-1], ones[-1])
	for (a, b, c) in zip(ch, ch[1:], ch[2:]):
		if S[a] == '0':
			ans = min(ans, ones[a] + zeros[-1] - zeros[c])
		else:
			ans = min(ans, zeros[a] + ones[-1] - ones[c])
	print(ans)


for _ in range(int(input())):
	a = list(map(int, list(input())))
	n = len(a)
	pre = [0] * n
	suf = [0] * n
	for i in range(1, n):
		if a[i - 1] == 1:
			pre[i] = pre[i - 1] + 1
		else:
			pre[i] = pre[i - 1]
	for i in range(n - 2, -1, -1):
		if a[i + 1] == 0:
			suf[i] = suf[i + 1] + 1
		else:
			suf[i] = suf[i + 1]
	m = n
	for i in range(n):
		m = min(m, pre[i] + suf[i])
	pre = [0] * n
	suf = [0] * n
	for i in range(1, n):
		if a[i - 1] == 0:
			pre[i] = pre[i - 1] + 1
		else:
			pre[i] = pre[i - 1]
	for i in range(n - 2, -1, -1):
		if a[i + 1] == 1:
			suf[i] = suf[i + 1] + 1
		else:
			suf[i] = suf[i + 1]
	for i in range(n):
		m = min(m, pre[i] + suf[i])
	print(m)


import sys
input = sys.stdin.readline
from collections import Counter
t = int(input())
for tests in range(t):
	t = input().strip()
	LEN = len(t)
	L0 = [0]
	L1 = [0]
	for s in t:
		if s == '0':
			L0.append(L0[-1] + 1)
			L1.append(L1[-1])
		else:
			L0.append(L0[-1])
			L1.append(L1[-1] + 1)
	R0 = [0]
	R1 = [0]
	for s in t[::-1]:
		if s == '0':
			R0.append(R0[-1] + 1)
			R1.append(R1[-1])
		else:
			R0.append(R0[-1])
			R1.append(R1[-1] + 1)
	R0 = R0[::-1]
	R1 = R1[::-1]
	ANS = 1 << 30
	for i in range(LEN + 1):
		ANS = min(ANS, L1[i] + R0[i])
		ANS = min(ANS, R1[i] + L0[i])
	print(ANS)


t = int(input())
for _ in range(t):
	s = input()
	cnt = 0
	beg0 = [0] * len(s)
	beg1 = [0] * len(s)
	for (i, c) in enumerate(s):
		if i > 0:
			beg0[i] = beg0[i - 1]
			beg1[i] = beg1[i - 1]
		if c == '0':
			beg0[i] += 1
		if c == '1':
			beg1[i] += 1
	end0 = [0] * len(s)
	end1 = [0] * len(s)
	for i in range(len(s) - 1, -1, -1):
		c = s[i]
		if i != len(s) - 1:
			end0[i] = end0[i + 1]
			end1[i] = end1[i + 1]
		if c == '0':
			end0[i] += 1
		if c == '1':
			end1[i] += 1
	cnt = min(end0[0], end1[0])
	for i in range(len(s) - 1):
		cnt_n = min(beg0[i] + end1[i + 1], beg1[i] + end0[i + 1])
		cnt = min(cnt, cnt_n)
	print(cnt)


from itertools import groupby

def solve(xs):
	sl0 = sl1 = 0
	s = sum(xs)
	res = s
	sr0 = sum(xs[::2])
	sr1 = sum(xs[1::2])
	for (i, x) in enumerate(xs):
		res = min(res, sl0 + sr1)
		res = min(res, sl1 + sr0)
		if i % 2 == 0:
			sl0 += x
			sr0 -= x
		else:
			sl1 += x
			sr1 -= x
	return res
for _ in range(int(input())):
	s = input()
	groups = [len(list(g)) for (k, g) in groupby(s)]
	print(solve(groups))


import string
t = int(input())
for _ in range(t):
	s = input()
	a = list(s[0:3])
	res = 1000000000
	n = len(s)
	(res0, res1) = (0, 0)
	for i in range(n):
		if s[i] == '0':
			res0 += 1
		else:
			res1 += 1
	(cnt0, cnt1) = (0, 0)
	res = min(res1, res0)
	for i in range(n):
		if s[i] == '0':
			cnt0 += 1
		else:
			cnt1 += 1
		res = min(res, cnt1 + res0 - cnt0, cnt0 + res1 - cnt1)
	print(res)


def solve(arr):
	n = len(arr)
	lis0 = []
	lis1 = []
	one = 0
	zero = 0
	for item in arr:
		if item == 0:
			zero += 1
		else:
			one += 1
		lis0.append(zero)
		lis1.append(one)
	cost = n
	for i in range(n):
		left = lis1[i] + zero - lis0[i]
		right = lis0[i] + one - lis1[i]
		cost = min(cost, left, right)
	print(cost)
t = int(input())
for _ in range(t):
	arr = list(map(int, list(input())))
	solve(arr)


def solve1(s):
	n = len(s)
	left = [0] * n
	right = [0] * n
	c = 0
	for i in range(n):
		if s[i] == '1':
			c += 1
		left[i] = c
	c = 0
	for i in range(n - 1, -1, -1):
		if s[i] == '0':
			c += 1
		right[i] = c
	ans = 1000000000000
	for i in range(1, n):
		ans = min(ans, left[i - 1] + right[i])
	return ans

def solve2(s):
	n = len(s)
	left = [0] * n
	right = [0] * n
	c = 0
	for i in range(n):
		if s[i] == '0':
			c += 1
		left[i] = c
	c = 0
	for i in range(n - 1, -1, -1):
		if s[i] == '1':
			c += 1
		right[i] = c
	ans = 1000000000000
	for i in range(1, n):
		ans = min(ans, left[i - 1] + right[i])
	return ans
for _ in range(int(input())):
	s = list(input())
	(c1, c2) = (0, 0)
	for i in range(len(s)):
		if s[i] == '1':
			c1 += 1
		else:
			c2 += 1
	temp1 = solve1(s)
	temp2 = solve2(s)
	print(min(temp1, temp2, c1, c2))


a = input()
for x in range(int(a)):
	b = input()
	length = len(b)
	ans = length
	q = 0
	q1 = 0
	for i in range(length):
		tag = '0' * i + '1' * (length - i)
		tag1 = '1' * i + '0' * (length - i)
		if i == 0:
			for j in range(length):
				if tag[j] != b[j]:
					q += 1
				if tag1[j] != b[j]:
					q1 += 1
		else:
			if tag[i - 1] == b[i - 1]:
				q -= 1
			else:
				q += 1
			if tag1[i - 1] == b[i - 1]:
				q1 -= 1
			else:
				q1 += 1
		ans = min(q, q1, ans)
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	k1 = [0] * len(s)
	k2 = [0] * len(s)
	if s[0] == '1':
		k1[0] = 1
	for i in range(1, len(s)):
		if s[i] == '1':
			k1[i] = k1[i - 1] + 1
		else:
			k1[i] = k1[i - 1]
	for i in range(len(s) - 2, -1, -1):
		if s[i + 1] == '0':
			k2[i] = k2[i + 1] + 1
		else:
			k2[i] = k2[i + 1]
	mnch = 999999
	for i in range(len(s)):
		if k1[i] + k2[i] < mnch:
			mnch = k1[i] + k2[i]
		if len(s) - k1[i] - k2[i] < mnch:
			mnch = len(s) - k1[i] - k2[i]
	print(mnch)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	s = input().strip()
	n = len(s)
	tot0 = 0
	tot1 = 0
	for c in s:
		if c == '0':
			tot0 += 1
		else:
			tot1 += 1
	best = min(tot0, tot1)
	cur0 = 0
	cur1 = 0
	for c in s:
		if c == '0':
			cur0 += 1
		else:
			cur1 += 1
		best = min((best, cur0 + (tot1 - cur1), cur1 + (tot0 - cur0)))
	print(best)


for _ in range(int(input())):
	a = list(input())
	b = []
	zeros = 0
	ones = 0
	for i in a:
		if i == '0':
			zeros += 1
		else:
			ones += 1
		if len(b) == 0:
			b.append([i, 1, zeros, ones])
		elif b[-1][0] == i:
			b[-1] = [b[-1][0], b[-1][1] + 1, zeros, ones]
		else:
			b.append([i, 1, zeros, ones])
	if len(b) <= 2:
		print(0)
	else:
		minVal = min(zeros, ones)
		tot = zeros + ones
		for i in range(1, len(b)):
			if b[i][0] == '1':
				minVal = min(minVal, b[i - 1][3] + zeros - b[i][2])
			else:
				minVal = min(minVal, ones - b[i][3] + b[i - 1][2])
		print(minVal)


t = int(input())
for x in range(t):
	s = input()
	min_op = 1000
	for a in range(len(s)):
		number_op = min(s[a:].count('0') + s[:a].count('1'), s[a:].count('1') + s[:a].count('0'))
		if min_op > number_op:
			min_op = number_op
	print(min_op)


t = int(input())
for _ in range(t):
	string = list(input())
	store = []
	count_0 = 0
	count_1 = 0
	for i in range(len(string)):
		if string[i] == '0':
			count_0 += 1
		else:
			count_1 += 1
		store.append((count_0, count_1))
	prefix = []
	for i in range(len(string)):
		if string[i] == '0':
			count_0 -= 1
		else:
			count_1 -= 1
		prefix.append((count_0, count_1))
	ans = float('inf')
	for i in range(len(string)):
		k = min(store[i][0] + prefix[i][1], prefix[i][0] + store[i][1])
		ans = min(ans, k)
	print(ans)


T = int(input())
for _ in range(T):
	s = input()
	cnt1 = 0
	for c in s:
		if c == '1':
			cnt1 += 1
	tm = 0
	ans = len(s)
	for i in range(len(s)):
		if s[i] == '1':
			tm += 1
		ans = min(ans, cnt1 - tm + i + 1 - tm, len(s) - i - 1 - cnt1 + tm + tm)
	print(ans)


for _ in range(int(input())):
	lis = input()
	zero = 0
	um = 0
	for i in lis:
		zero += 1 - int(i)
		um += int(i)
	ans = zero + um
	cont = zero
	for i in lis:
		if i == '0':
			cont -= 1
		elif i == '1':
			cont += 1
		if cont < ans:
			ans = cont
	for i in lis:
		if i == '0':
			cont += 1
		elif i == '1':
			cont -= 1
		if cont < ans:
			ans = cont
	print(ans)


t = int(input())
for i in range(t):
	n = list(input())
	one = n.count('1')
	zero = n.count('0')
	ans = min(one, zero)
	cone = 0
	czero = 0
	for i in range(len(n)):
		if n[i] == '0':
			czero += 1
			zero -= 1
		else:
			cone += 1
			one -= 1
		ans = min(ans, cone + zero)
		ans = min(ans, czero + one)
	print(ans)


import sys

def minop(s, n):
	one = 0
	zero = 0
	mini = 9999
	for i in s:
		if i == '0':
			zero += 1
		else:
			one += 1
	d1 = 0
	d0 = 0
	for i in range(n):
		mini = min(mini, d1 + zero - d0, d0 + one - d1)
		if s[i] == '1':
			d1 += 1
		else:
			d0 += 1
	return mini
t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	print(minop(s, n))


import sys
readline = sys.stdin.readline
ns = lambda : readline().rstrip()
ni = lambda : int(readline().rstrip())
nm = lambda : map(int, readline().split())
nl = lambda : list(map(int, readline().split()))

def solve():
	s = list(map(int, list(ns())))
	n = len(s)
	g = sum(s)
	c = 0
	ans = min(g, n - g)
	for i in range(n):
		c += s[i]
		ans = min(ans, c + n - 1 - i - g + c, i + 1 - c + g - c)
	print(ans)
	return
T = int(input())
for _ in range(T):
	solve()


for p in range(int(input())):
	st = input()
	n = len(st)
	ans = 0
	o = st.count('1')
	z = st.count('0')
	c1 = 0
	c0 = 0
	ans = 10 ** 9
	for j in st:
		if j == '1':
			c1 += 1
		else:
			c0 += 1
		ans = min(ans, c1 + z - c0)
		ans = min(ans, c0 + o - c1)
	print(ans)


import math
import os
import random
import re
import sys

def subsequence_hate(s):
	ds = {0: [0, 0]}
	prev = s[0]
	zero_count = 0
	for (i, c) in enumerate(s):
		if c != prev:
			ds[i] = [zero_count, i - zero_count]
			prev = c
		zero_count += 1 if c == '0' else 0
	ds[len(s)] = [zero_count, len(s) - zero_count]
	if len(ds) <= 3:
		return 0
	r = s[::-1]
	dr = {len(s): [0, 0]}
	prev = r[-1]
	zero_count = 0
	for (i, c) in enumerate(r):
		if c != prev:
			dr[len(s) - i] = [zero_count, i - zero_count]
			prev = c
		zero_count += 1 if c == '0' else 0
	dr[0] = [zero_count, len(s) - zero_count]
	minCount = float('inf')
	for dsi in ds.items():
		(pos, l_item) = dsi
		(l_zeros, l_ones) = l_item
		r_item = dr[pos]
		(r_zeros, r_ones) = r_item
		count = min(l_zeros + r_ones, l_ones + r_zeros)
		minCount = min(minCount, count)
	return minCount
t = int(input())
for i in range(t):
	s = input().rstrip()
	result = subsequence_hate(s)
	print(result)


import sys
input = sys.stdin.readline
from collections import Counter

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def num_moves(s):
	counter = Counter(s)
	num_ones = counter['1']
	num_zeros = counter['0']
	cur_ones = 0
	cur_zeros = 0
	res = float('inf')
	for ch in s:
		zero_one = cur_ones + (num_zeros - cur_zeros)
		if ch == '0':
			zero_one -= 1
		one_zero = cur_zeros + (num_ones - cur_ones)
		if ch == '1':
			one_zero -= 1
		res = min([zero_one, one_zero, res])
		if ch == '0':
			cur_zeros += 1
		else:
			cur_ones += 1
	return res
num_tests = inp()
for _ in range(num_tests):
	s = input()
	print(num_moves(s))


t = int(input())

def get(a):
	ans = len(a)
	count = 0
	for i in a:
		if i == 0:
			count += 1
	ans = min(ans, count)
	count = 0
	for i in a:
		if i == 1:
			count += 1
	ans = min(ans, count)
	count = 0
	pre = []
	pos = []
	count = 0
	for i in a:
		if i == 1:
			count += 1
		pre.append(count)
		pos.append(0)
	count = 0
	for i in range(len(a) - 1, -1, -1):
		if a[i] == 0:
			count += 1
		pos[i] = count
	tans = ans
	for i in range(1, len(a)):
		tans = min(tans, pre[i - 1] + pos[i])
	ans = min(tans, ans)
	tans = ans
	count = 0
	pre = []
	pos = []
	count = 0
	for i in a:
		if i == 0:
			count += 1
		pre.append(count)
		pos.append(0)
	count = 0
	for i in range(len(a) - 1, -1, -1):
		if a[i] == 1:
			count += 1
		pos[i] = count
	tans = ans
	for i in range(1, len(a)):
		tans = min(tans, pre[i - 1] + pos[i])
	ans = min(tans, ans)
	return ans
for _ in range(t):
	s = input()
	a = [int(i) for i in s]
	ans = get(a)
	ans = min(ans, get(a[::-1]))
	print(ans)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, functools

def solve(line):
	s = line
	n = len(s)
	tot = sum((i == '1' for i in s))
	cn = 0
	ans = min(tot, n - tot)
	for (i, c) in enumerate(s):
		cn += c == '1'
		ans = min(ans, i + 1 - cn + tot - cn, cn + n - tot - (i + 1 - cn))
	return ans
T = int(sys.stdin.readline())
t = 0
while True:
	line = sys.stdin.readline().rstrip()
	if not line:
		break
	print(solve(line))
	t += 1


for t in range(int(input())):
	s = input()
	minumum = 1000
	initial = 0
	for i in range(len(s)):
		if s[i] == '0':
			temp_s = s[i:]
			count = temp_s.count('1')
			if count + initial < minumum:
				minumum = count + initial
			initial += 1
	initial = 0
	for i in range(len(s)):
		if s[i] == '1':
			temp_s = s[i:]
			count = temp_s.count('0')
			if count + initial < minumum:
				minumum = count + initial
			initial += 1
	zero = 0
	one = 0
	for i in range(len(s)):
		if s[i] == '0':
			zero += 1
		elif s[i] == '1':
			one += 1
	if zero == 0 or one == 0:
		minumum = 0
	print(minumum)


for i in range(int(input())):
	t = list(input())
	if len(set(t)) == 1:
		print(0)
	else:
		preone = []
		prezero = []
		if t[0] == '0':
			prezero.append(1)
			preone.append(0)
		else:
			preone.append(1)
			prezero.append(0)
		n = len(t)
		for j in range(1, n):
			if t[j] == '0':
				prezero.append(prezero[-1] + 1)
				preone.append(preone[-1] + 0)
			else:
				prezero.append(prezero[-1] + 0)
				preone.append(preone[-1] + 1)
		postone = []
		postzero = []
		if t[-1] == '0':
			postzero.append(1)
			postone.append(0)
		else:
			postone.append(1)
			postzero.append(0)
		n = len(t)
		for j in range(n - 2, -1, -1):
			if t[j] == '0':
				postzero.append(postzero[-1] + 1)
				postone.append(postone[-1] + 0)
			else:
				postzero.append(postzero[-1] + 0)
				postone.append(postone[-1] + 1)
		postone = postone[::-1]
		postzero = postzero[::-1]
		ans = min(t.count('1'), t.count('0'))
		for j in range(n - 1):
			ans = min(prezero[j] + postone[j + 1], ans)
			ans = min(preone[j] + postzero[j + 1], ans)
		print(ans)


import sys
import string
input = sys.stdin.readline

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
for i in range(inin()):
	s = sin()
	l = s.strip()
	n = len(l)
	c1 = l.count('1')
	c0 = n - c1
	if c1 != 0:
		fo = l.index('1')
		lo = n - l[::-1].index('1') - 1
	if c0 != 0:
		fz = l.index('0')
		lz = n - l[::-1].index('0') - 1
	if n <= 2 or c1 == 0 or c0 == 0 or (fo > lz) or (fz > lo):
		print(0)
	elif n == 3:
		il = int(s, 2)
		if il == 2 or il == 5:
			print(1)
		else:
			print(0)
	else:
		l1 = [0] * n
		l1[0] = int(l[0])
		ans = min(c1, c0)
		for i in range(1, n):
			if l[i] == '1':
				l1[i] = l1[i - 1] + 1
			else:
				l1[i] = l1[i - 1]
		for i in range(1, n):
			temp = min(i - l1[i - 1] + c1 - l1[i - 1], n - i - c1 + l1[i - 1] + l1[i - 1])
			if temp < ans:
				ans = temp
		print(ans)


for _ in range(int(input())):
	l = list(input())
	n = len(l)
	zero = 0
	one = 0
	mini = 10 ** 6
	for i in range(n):
		if l[i] == '0':
			zero += 1
		else:
			one += 1
	val = 0
	val1 = 0
	for i in range(n):
		mini = min(mini, one + val)
		mini = min(mini, zero + val1)
		if l[i] == '0':
			val += 1
			zero -= 1
		else:
			val1 += 1
			one -= 1
		mini = min(mini, one + val)
		mini = min(mini, zero + val1)
	print(mini)


import sys
import math
import collections
from sys import stdin, stdout, setrecursionlimit
import bisect as bs
setrecursionlimit(2 ** 20)
M = 10 ** 9 + 7
T = int(stdin.readline())
for _ in range(T):
	s = stdin.readline().strip('\n')
	n = len(s)
	o = [0] * (n + 1)
	z = [0] * (n + 1)
	for i in range(n):
		if s[i] == '1':
			o[i + 1] = o[i] + 1
			z[i + 1] = z[i]
		else:
			o[i + 1] = o[i]
			z[i + 1] = z[i] + 1
	mn = o[-1]
	mn = min(mn, z[-1])
	for i in range(1, n + 1, 1):
		mn = min(mn, o[i] + z[-1] - z[i])
		mn = min(mn, z[i] + o[-1] - o[i])
	print(mn)


from sys import stdin
T = int(stdin.readline().strip())
for caso in range(T):
	s = stdin.readline().strip()
	z = [0 for i in range(len(s))]
	ons = [0 for i in range(len(s))]
	for i in range(len(s)):
		if s[i] == '0':
			z[i] += 1
		else:
			ons[i] += 1
		if i != 0:
			z[i] += z[i - 1]
			ons[i] += ons[i - 1]
	ans = min(z[-1], ons[-1])
	for i in range(len(s)):
		x = ons[i] + z[-1] - z[i]
		ans = min(ans, x)
	for i in range(len(s)):
		x = z[i] + ons[-1] - ons[i]
		ans = min(ans, x)
	print(ans)


from collections import Counter
from collections import defaultdict
from collections import deque
import math
import heapq
import sys
input = sys.stdin.readline
from bisect import *
rs = lambda : input().strip()
ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rls = lambda : list(map(str, input().split()))
t = int(input())
for _ in range(0, t):
	a = input().strip()
	s = a
	mn = 999999
	n = len(a)
	onef = [0] * n
	oneb = [0] * n
	zerof = [0] * n
	zerob = [0] * n
	for i in range(0, len(a)):
		if a[i] != '0':
			if i == 0:
				zerof[i] = zerof[i] + 1
			else:
				zerof[i] = zerof[i - 1] + 1
		elif i == 0:
			continue
		else:
			zerof[i] = zerof[i - 1]
	for i in range(0, len(s)):
		if a[i] != '1':
			if i == 0:
				onef[i] = onef[i] + 1
			else:
				onef[i] = onef[i - 1] + 1
		elif i == 0:
			continue
		else:
			onef[i] = onef[i - 1]
	s = s[::-1]
	a = a[::-1]
	for i in range(0, len(s)):
		if a[i] != '0':
			if i == 0:
				zerob[i] = zerob[i] + 1
			else:
				zerob[i] = zerob[i - 1] + 1
		elif i == 0:
			continue
		else:
			zerob[i] = zerob[i - 1]
	for i in range(0, len(s)):
		if a[i] != '1':
			if i == 0:
				oneb[i] = oneb[i] + 1
			else:
				oneb[i] = oneb[i - 1] + 1
		elif i == 0:
			continue
		else:
			oneb[i] = oneb[i - 1]
	oneb = oneb[::-1]
	zerob = zerob[::-1]
	mn = 999999
	for i in range(0, n - 1):
		mn = min(mn, onef[i] + zerob[i + 1])
		mn = min(mn, zerof[i] + oneb[i + 1])
	print(min(mn, oneb[0], zerob[0], onef[-1], zerof[-1]))


n = int(input())
for y in range(n):
	a = list(map(int, input()))
	sm = sum(a)
	cnt1 = sm
	cnt0 = len(a) - sm
	cur0 = 0
	cur1 = 0
	rem0 = 0
	rem1 = 0
	ans = len(a)
	for i in a:
		if i == 0:
			cur0 += 1
		else:
			cur1 += 1
		rem1 = cnt1 - cur1
		rem0 = cnt0 - cur0
		ans1 = min(cur0 + rem1, cur1 + rem0)
		if ans > ans1:
			ans = ans1
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	leftZ = [0] * len(s)
	leftO = [0] * len(s)
	rightO = [0] * len(s)
	rightZ = [0] * len(s)
	for i in range(1, len(s)):
		leftZ[i] = leftZ[i - 1] + (1 if s[i - 1] == '0' else 0)
		leftO[i] = leftO[i - 1] + (0 if s[i - 1] == '0' else 1)
	for i in range(len(s) - 2, -1, -1):
		rightZ[i] = rightZ[i + 1] + (1 if s[i + 1] == '0' else 0)
		rightO[i] = rightO[i + 1] + (0 if s[i + 1] == '0' else 1)
	leftZero = []
	rightZero = []
	for i in range(len(s)):
		leftZeroAns = rightZ[i] + leftO[i] + (1 if s[i] == '1' else 0)
		leftZero.append(leftZeroAns)
		rightZeroAns = rightO[i] + leftZ[i] + (1 if s[i] == '0' else 0)
		rightZero.append(rightZeroAns)
	allZero = rightZ[0] + (1 if s[0] == '0' else 0)
	allOnes = rightO[0] + (1 if s[i] == '1' else 0)
	print(min(min(leftZero), min(rightZero), allZero, allOnes))


t = int(input())
for i in range(t):
	s = input()
	c0 = 0
	c1 = 0
	d0 = 0
	d1 = 0
	x = len(s)
	y = len(s)
	for i in s:
		if i == '1':
			c1 += 1
		if i == '0':
			c0 += 1
	for i in s:
		if i == '0':
			d0 += 1
		if i == '1':
			d1 += 1
		x = min(x, d0 + c1 - d1)
		y = min(y, d1 + c0 - d0)
		z = min(x, y)
	print(z)


for _ in ' ' * int(input()):
	s = input()
	(c0, c1, p0, p1) = (s.count('0'), s.count('1'), 0, 0)
	a = min(c0, c1)
	for s in s:
		s = s == '0'
		p0 += s
		c0 -= s
		p1 += 1 - s
		c1 -= 1 - s
		a = min(a, c0 + p1, c1 + p0)
	print(a)


t = int(input())
while t:
	t -= 1
	s = input()
	if len(s) >= 3:
		cnt = 0
		cot = 0
		one = []
		ans = 1001
		for i in range(len(s)):
			if s[i] == '1':
				cnt += 1
				one.append(cnt)
			else:
				cot += 1
				one.append(cnt)
		for i in range(len(s)):
			ans = min(ans, i - one[i] + cnt - one[i] + 1)
			ans = min(ans, one[i] + (len(s) - i) - (cnt - one[i]) - 1)
		print(ans)
	else:
		print(0)


import sys
input = sys.stdin.readline
MOD = 10 ** 9 + 7
for t in range(int(input())):
	data = list(map(int, input().strip()))
	n = len(data)
	(a, b) = ([], [])
	s = 0
	for i in data:
		if i:
			s += 1
		a.append(s)
	s = 0
	for i in data[::-1]:
		if not i:
			s += 1
		b.append(s)
	b = b[::-1]
	ans = 10 ** 10
	for i in range(n):
		c = a[i]
		if i + 1 < n:
			c += b[i + 1]
		ans = min(ans, c)
	(a, b) = ([], [])
	s = 0
	for i in data:
		if not i:
			s += 1
		a.append(s)
	s = 0
	for i in data[::-1]:
		if i:
			s += 1
		b.append(s)
	b = b[::-1]
	for i in range(n):
		c = a[i]
		if i + 1 < n:
			c += b[i + 1]
		ans = min(ans, c)
	print(ans)


for _ in range(int(input())):
	s = input()
	pre = [int(s[0])]
	suf = [int(s[-1]) == 0]
	n = len(s)
	for i in range(1, n):
		if s[i] == '1':
			pre.append(pre[-1] + 1)
		else:
			pre.append(pre[-1])
		if s[n - i - 1] == '0':
			suf.insert(0, suf[0] + 1)
		else:
			suf.insert(0, suf[0])
	m = 1000
	for i in range(n):
		a = pre[i]
		b = suf[i]
		if s[i] == '0':
			b -= 1
		m = min(m, a + b)
		a = i + 1 - a
		b = n - (i + 1) - b
		m = min(m, a + b)
	print(m)


t = int(input())
for Adun in range(t):
	s = input()
	ones_right = 0
	zeroes_right = 0
	ones_left = 0
	zeroes_left = 0
	for i in s:
		if i == '1':
			ones_right += 1
		else:
			zeroes_right += 1
	answer = len(s)
	answer = min(answer, ones_right, zeroes_right)
	for i in s:
		if i == '1':
			ones_right -= 1
			ones_left += 1
		else:
			zeroes_right -= 1
			zeroes_left += 1
		answer = min(answer, ones_left + zeroes_right, zeroes_left + ones_right)
	print(answer)


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	a = [int(i) for i in input() if i != '\n']
	(opref, osuff) = ([0], [sum(a)])
	(zpref, zsuff) = ([0], [len(a) - sum(a)])
	for i in a:
		if i == 1:
			opref.append(opref[-1] + 1)
			osuff.append(osuff[-1] - 1)
			zpref.append(zpref[-1])
			zsuff.append(zsuff[-1])
		else:
			zpref.append(zpref[-1] + 1)
			zsuff.append(zsuff[-1] - 1)
			opref.append(opref[-1])
			osuff.append(osuff[-1])
	zpref = zpref[1:]
	zsuff = zsuff[:len(zsuff) - 1]
	opref = opref[1:]
	osuff = osuff[:len(osuff) - 1]
	if sum(a) == len(a) or sum(a) == 0:
		print(0)
		continue
	else:
		ans = min(opref[-1], zpref[-1])
		for i in range(len(a) - 1):
			ans = min(ans, opref[i] + zsuff[i + 1], zpref[i] + osuff[i + 1])
		sys.stdout.write(str(ans) + '\n')


t = int(input())
for _ in range(t):
	c = list(input())
	zn = c.count('0')
	zp = 0
	l = len(c)
	on = l - zn
	op = 0
	a = l
	for i in range(l):
		if c[i] == '0':
			zp += 1
			zn -= 1
		else:
			op += 1
			on -= 1
		a = min(a, zp + on, op + zn)
	print(a)


from sys import stdin, stdout
import math, queue, heapq
fastinput = stdin.readline
fastout = stdout.write
t = int(fastinput())
while t:
	t -= 1
	s = input()
	a = s.count('1')
	n = len(s)
	b = n - a
	ans = n
	cnt1 = 0
	cnt0 = 0
	for i in s:
		if i == '1':
			cnt1 += 1
		else:
			cnt0 += 1
		ans = min(ans, cnt1 + (b - cnt0), cnt0 + (a - cnt1))
	print(ans)


import bisect
import sys
import math
input = sys.stdin.readline
from collections import Counter
from collections import defaultdict

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def ins():
	return input().rstrip('\n')

def solve(case):
	st = ins()
	c = Counter()
	for ch in st:
		c[ch] += 1
	cum = Counter()
	res = 1000000000
	for ch in st + '_':
		make01 = cum['1'] + (c['0'] - cum['0'])
		make10 = cum['0'] + (c['1'] - cum['1'])
		res = min(res, make01, make10)
		cum[ch] += 1
	return res
if len(sys.argv) > 1 and sys.argv[1].startswith('input'):
	f = open('./' + sys.argv[1], 'r')
	input = f.readline
T = inp()
for i in range(T):
	res = solve(i + 1)
	print(str(res))


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7
for _ in range(INT()):
	S = input()
	N = len(S)
	acc = [0] * (N + 1)
	for i in range(N):
		acc[i + 1] = acc[i] + (S[i] == '1')
	ans = INF
	for i in range(N + 1):
		lcnt1 = acc[i] - acc[0]
		lcnt0 = i - lcnt1
		rcnt1 = acc[N] - acc[i]
		rcnt0 = N - i - rcnt1
		ans = min(ans, lcnt0 + rcnt1, lcnt1 + rcnt0)
	print(ans)


T = int(input())

def solve():
	S = input()
	total_0 = 0
	total_1 = 0
	for i in range(len(S)):
		if S[i] == '0':
			total_0 += 1
		else:
			total_1 += 1
	ans = len(S)
	(count_0, count_1) = (0, 0)
	for i in range(len(S)):
		ans01 = count_1 + (total_0 - count_0)
		ans10 = count_0 + (total_1 - count_1)
		if S[i] == '0':
			count_0 += 1
		else:
			count_1 += 1
		ans = min(ans, ans01, ans10)
	print(ans)
for _ in range(T):
	solve()


t = int(input())
for i in range(t):
	st = input()
	z = 0
	for j in st:
		if j == '0':
			z += 1
	arr1 = [z]
	for j in st:
		if j == '0':
			arr1.append(arr1[-1] - 1)
		else:
			arr1.append(arr1[-1] + 1)
	arr2 = [len(st) - z]
	for j in st:
		if j == '1':
			arr2.append(arr2[-1] - 1)
		else:
			arr2.append(arr2[-1] + 1)
	print(min(arr1 + arr2))


from sys import stdin
input = stdin.readline

def main():
	test = int(input())
	for t in range(test):
		s = input().strip()
		z = s.count('0')
		o = s.count('1')
		ze = 0
		on = 0
		ans = len(s)
		for i in range(len(s)):
			if s[i] == '0':
				ze += 1
			else:
				on += 1
			ans = min(ans, on + z - ze)
			ans = min(ans, ze + o - on)
		print(ans)
main()


t = int(input())

def zeros(seg):
	count = 0
	for char in seg:
		if char == '0':
			count += 1
	return count
for iteration in range(t):
	s = input()
	l = len(s)
	z1 = 0
	z2 = zeros(s)
	ans = min(z2, l - z2)
	for i in range(1, l):
		if s[i - 1] == '0':
			z1 += 1
			z2 -= 1
		zo = i - z1 + z2
		oz = z1 + (l - i - z2)
		ans = min(ans, zo, oz)
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	ans = len(s)
	for x in range(len(s)):
		ans = min(ans, s[:x].count('0') + s[x + 1:].count('1'))
		ans = min(ans, s[:x].count('1') + s[x + 1:].count('0'))
	print(ans)


from sys import stdin, stdout, setrecursionlimit
input = stdin.readline
for _ in range(int(input())):
	s = input()
	n = len(s)
	cou = 0
	for i in range(n):
		if s[i] == '0':
			cou += 1
	x = cou
	ans = cou
	for i in range(n):
		if s[i] == '0':
			cou -= 1
		elif s[i] == '1':
			cou += 1
		ans = min(cou, ans)
	cou = x
	for i in range(n - 1, -1, -1):
		if s[i] == '0':
			cou -= 1
		elif s[i] == '1':
			cou += 1
		ans = min(cou, ans)
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	c1 = s.count('1')
	c0 = s.count('0')
	count0 = []
	current = 0
	for x in s:
		current += x == '0'
		count0.append(current)
	best = min(c1, c0)
	for x in range(len(s)):
		zeros_left = count0[x]
		zeros_right = c0 - zeros_left
		ones_left = x - zeros_left + 1
		ones_right = c1 - ones_left
		var1 = zeros_right + ones_left
		var2 = zeros_left + ones_right
		best = min(best, var1, var2)
	print(best)


t = int(input())
for test in range(t):
	s = input()
	n = len(s)
	oneleft = [0] * n
	oneright = [0] * n
	zeroleft = [0] * n
	zeroright = [0] * n
	if s[0] == '1':
		zeroleft[0] = 1
	else:
		oneleft[0] = 1
	if s[-1] == '1':
		zeroright[-1] = 1
	else:
		oneright[-1] = 1
	for i in range(1, n):
		if s[i] == '1':
			zeroleft[i] = zeroleft[i - 1] + 1
			oneleft[i] = oneleft[i - 1]
		else:
			zeroleft[i] = zeroleft[i - 1]
			oneleft[i] = oneleft[i - 1] + 1
	for i in range(n - 2, -1, -1):
		if s[i] == '1':
			zeroright[i] = zeroright[i + 1] + 1
			oneright[i] = oneright[i + 1]
		else:
			zeroright[i] = zeroright[i + 1]
			oneright[i] = oneright[i + 1] + 1
	m = min(zeroright[0], oneright[0])
	for i in range(n - 1):
		m = min(m, oneleft[i] + zeroright[i + 1], zeroleft[i] + oneright[i + 1])
	m = min(m, zeroleft[-1], oneleft[-1])
	print(m)


for i in range(int(input())):
	s = input()
	n = len(s)
	(ones, zeros) = (s.count('1'), s.count('0'))
	(o, z) = (ones, zeros)
	arr = []
	for i in range(n - 1, -1, -1):
		arr.append(min(ones + z - zeros, zeros + o - ones))
		if s[i] == '1':
			ones -= 1
		else:
			zeros -= 1
	print(min(arr))


import sys

def gcd(a, b):
	if a > b:
		return gcd(b, a)
	if a == 0:
		return b
	return gcd(b % a, a)
pprint = lambda s: print(' '.join(map(str, s)))
input = lambda : sys.stdin.readline().strip()
ipnut = input
for i in range(int(input())):
	a = list(map(int, input()))
	n = len(a)
	dp0 = [0] * n
	dp1 = [0] * n
	for i in range(n):
		dp0[i] += a[i]
		dp1[i] += (a[i] + 1) % 2
		if i != n - 1:
			dp0[i + 1] += dp0[i]
			dp1[i + 1] += dp1[i]
	a.reverse()
	dpr0 = [0] * n
	dpr1 = [0] * n
	for i in range(n):
		dpr0[i] += a[i]
		dpr1[i] += (a[i] + 1) % 2
		if i != n - 1:
			dpr0[i + 1] += dpr0[i]
			dpr1[i + 1] += dpr1[i]
	dpr0.reverse()
	dpr1.reverse()
	ans = min(dp0[-1], dp1[-1])
	for i in range(n - 1):
		ans = min(dp1[i] + dpr0[i + 1], dp0[i] + dpr1[i + 1], ans)
	print(ans)


for ii in range(int(input())):
	s = input()
	zerobefore = 0
	onebefore = 0
	oneafter = s.count('1')
	zeroafter = s.count('0')
	cnt = 10 ** 9
	for i in s:
		if i == '0':
			zerobefore += 1
			zeroafter -= 1
		else:
			onebefore += 1
			oneafter -= 1
		cnt = min(cnt, onebefore + zeroafter, zerobefore + oneafter)
	print(cnt)


for t in range(int(input())):
	s = input()
	zeroes = []
	ones = []
	x = 0
	y = 0
	for c in s:
		if c == '0':
			x += 1
		else:
			y += 1
		zeroes += [x]
		ones += [y]
	zeroes_total = zeroes[-1]
	ones_total = ones[-1]
	zeroes_after = list(map(lambda z: zeroes_total - z, zeroes))
	ones_after = list(map(lambda z: ones_total - z, ones))
	solution = len(s)
	for i in range(len(s)):
		x = ones[i] + zeroes_after[i]
		y = zeroes[i] + ones_after[i]
		solution = min([x, y, solution])
	print(solution)


def solve():
	s = input()
	zeros = []
	if s[0] == '0':
		zeros.append(1)
	else:
		zeros.append(0)
	n = len(s)
	for i in range(1, n):
		zeros.append(zeros[-1])
		if s[i] == '0':
			zeros[i] += 1
	ans = n - 1
	for i in range(0, n):
		ans = min(ans, min(n - (zeros[-1] - zeros[i]) - (i - zeros[i] + 1), n - zeros[i] - (n - i - 1 - zeros[-1] + zeros[i])))
	ans = min(ans, min(zeros[-1], n - zeros[-1]))
	print(ans)

def main():
	t = int(input())
	for i in range(0, t):
		solve()
main()


for t in range(int(input())):
	a = list(map(int, input()))
	n = len(a)
	if n < 3:
		print('0')
	else:
		s = sum(a)
		sl = 0
		m = min(s, n - s)
		for i in range(n):
			if a[i] == 1:
				sl += 1
			if m > min(i + 1 - sl + s - sl, sl + (n - i - 1) - (s - sl)):
				m = min(i + 1 - sl + s - sl, sl + (n - i - 1) - (s - sl))
		print(m)


T = int(input())
for t in range(T):
	s = input()
	(p0, p1) = ([0], [0])
	for c in s:
		if c == '1':
			p1.append(p1[-1] + 1)
			p0.append(p0[-1])
		else:
			p0.append(p0[-1] + 1)
			p1.append(p1[-1])
	(s0, s1) = ([0], [0])
	for c in s[::-1]:
		if c == '1':
			s1.append(s1[-1] + 1)
			s0.append(s0[-1])
		else:
			s0.append(s0[-1] + 1)
			s1.append(s1[-1])
	s0 = s0[::-1]
	s1 = s1[::-1]
	answer = len(s)
	for i in range(len(s) + 1):
		answer = min(answer, p0[i] + s1[i], p1[i] + s0[i])
	print(answer)


import sys
input = sys.stdin.readline
f = lambda : list(map(int, input().split()))
res = []
for _ in range(int(input())):
	s = input().strip('\n')
	r0 = s.count('0')
	r1 = s.count('1')
	l0 = l1 = 0
	ans = min(r0, r1)
	for c in s:
		if c == '0':
			l0 += 1
			r0 -= 1
		else:
			l1 += 1
			r1 -= 1
		ans = min(ans, l1 + r0, l0 + r1)
	res.append(ans)
print('\n'.join(map(str, res)))


try:
	t = int(input())
	while t:
		arr = list(map(int, list(input())))
		tot_ones = arr.count(1)
		tot_zeros = arr.count(0)
		ans = min(tot_zeros, tot_ones)
		(done_ones, done_zeros) = (0, 0)
		for (i, ele) in enumerate(arr):
			if ele == 0:
				done_zeros += 1
			else:
				done_ones += 1
			zero_ones = done_ones + (tot_zeros - done_zeros)
			one_zeros = done_zeros + (tot_ones - done_ones)
			ans = min(ans, zero_ones, one_zeros)
		print(ans)
		t -= 1
except:
	pass


t = int(input())
ans = []
for i in range(t):
	s = input()
	c1 = 0
	c2 = 0
	for j in s:
		if j == '1':
			c1 += 1
		else:
			c2 += 1
	v1 = 0
	v2 = 0
	min1 = min(c1, c2)
	for j in s:
		if j == '1':
			v1 += 1
		else:
			v2 += 1
		min1 = min(v1 + c2 - v2, v2 + c1 - v1, min1)
	ans.append(min1)
for i in ans:
	print(i)


import math
r = int(input())
for i in range(r):
	s = input()
	p = []
	p1 = []
	cnt = 0
	for i in range(len(s)):
		if s[i] == '1':
			cnt += 1
		p.append(cnt)
	s = s[::-1]
	cnt = 0
	for i in range(len(s)):
		if s[i] == '1':
			cnt += 1
		p1.append(cnt)
	p1 = p1[::-1]
	s = s[::-1]
	ans = len(s)
	for i in range(len(s) - 1):
		ans = min(ans, i + 1 - p[i] + p1[i + 1], p[i] + (len(s) - i - 1 - p1[i + 1]))
	ans = min(ans, len(s) - p[-1], p[-1])
	print(ans)


t = int(input())
for _ in range(t):
	s = list(input())
	n = len(s)
	a = s[0].count('1') + s[1:].count('0')
	b = s[0].count('0') + s[1:].count('1')
	res = min([a, b])
	for i in range(1, n):
		if s[i] == '0':
			a -= 1
			b += 1
		else:
			a += 1
			b -= 1
		res = min([res, a, b])
	print(res)


from sys import stdin

def solve():
	s = stdin.readline().strip()
	n = len(s)
	cnt = s.count('1')
	cnt2 = 0
	ans = min(cnt, s.count('0'))
	for i in range(n):
		ans = min(ans, cnt + cnt2)
		ans = min(ans, n - i - cnt + (i - cnt2))
		if s[i] == '1':
			cnt -= 1
		else:
			cnt2 += 1
	print(ans)
tt = int(stdin.readline())
for _ in range(tt):
	solve()


def gen_list(s, c):
	rt = 0
	yield rt
	for a in s:
		rt += a == c
		yield rt

def solve(s):
	left1 = list(gen_list(s, '1'))
	left0 = list(gen_list(s, '0'))
	right1 = list(reversed(list(gen_list(reversed(s), '1'))))
	right0 = list(reversed(list(gen_list(reversed(s), '0'))))
	return min([min(left1[i] + right0[i], left0[i] + right1[i]) for i in range(len(s) + 1)])
cases = int(input().strip())
for _ in range(cases):
	s = input().strip()
	print(solve(s))


for _ in [0] * int(input()):
	a = input()
	s = 100000000.0
	for i in range(len(a)):
		s = min(s, a[:i].count('0') + a[i:].count('1'), a[:i].count('1') + a[i:].count('0'))
	print(s)


t = int(input())
for i in range(t):
	s = input()
	num_one = s.count('1')
	num_zero = s.count('0')
	done_one = 0
	done_zero = 0
	cost_1100 = []
	cost_0011 = []
	if s == '101' or s == '010':
		print('1')
	elif len(s) < 4:
		print('0')
	else:
		for i in range(len(s)):
			if s[i] == '1':
				done_one += 1
			else:
				done_zero += 1
			cost_1100.insert(-1, done_zero + num_one - done_one)
			cost_0011.insert(-1, done_one + num_zero - done_zero)
		a = min(cost_1100)
		b = min(cost_0011)
		if a <= b:
			print(a)
		else:
			print(b)


for _ in range(int(input())):
	n = input()
	zc = oc = i = 0
	flag = n[0]
	fd = [0] * (len(n) + 1)
	bd = [0] * (len(n) + 1)
	ct = 0
	for i in range(len(n)):
		if n[i] == '0':
			ct += 1
		fd[i + 1] = ct
	ct = 0
	for i in range(len(n) - 1, -1, -1):
		if n[i] == '1':
			ct += 1
		bd[i] = ct
	su = len(n)
	for i in range(len(n) + 1):
		su = min(su, fd[i] + bd[i])
	fd = [0] * (len(n) + 1)
	bd = [0] * (len(n) + 1)
	ct = 0
	for i in range(len(n)):
		if n[i] == '1':
			ct += 1
		fd[i + 1] = ct
	ct = 0
	for i in range(len(n) - 1, -1, -1):
		if n[i] == '0':
			ct += 1
		bd[i] = ct
	su2 = len(n)
	for i in range(len(n) + 1):
		su2 = min(su2, fd[i] + bd[i])
	print(min(su2, su))


for i in range(int(input())):
	string = input()
	num_ones = string.count('1')
	num_zeros = string.count('0')
	ans = min(num_ones, num_zeros)
	one_done = 0
	zero_done = 0
	for i in range(len(string)):
		if string[i] == '0':
			zero_done += 1
			num_zeros -= 1
		else:
			one_done += 1
			num_ones -= 1
		ans = min(ans, one_done + num_zeros)
		ans = min(ans, zero_done + num_ones)
	print(ans)


for g in range(int(input())):
	a = input()
	tot0 = tot1 = 0
	for i in range(len(a)):
		if a[i] == '0':
			tot0 += 1
		else:
			tot1 += 1
	pref0 = pref1 = 0
	ans = min(tot0, tot1)
	for k in range(len(a)):
		if a[k] == '0':
			pref0 += 1
			tot0 -= 1
		if a[k] == '1':
			pref1 += 1
			tot1 -= 1
		ans = min(ans, pref1 + tot0)
		ans = min(ans, pref0 + tot1)
	print(ans)


for cas in range(int(input())):
	s = input()
	one = zero = 0
	for i in range(len(s)):
		if s[i] == '1':
			one += 1
		else:
			zero += 1
	ans = min(one, zero)
	o = z = 0
	for i in range(len(s)):
		if s[i] == '1':
			o += 1
		else:
			z += 1
		ans = min(ans, o + zero - z)
		ans = min(ans, z + one - o)
	print(ans)


T = int(input())
for _ in range(0, T):
	s = input()
	A = list(s)
	c1 = 0
	c2 = 0
	z = 0
	o = 0
	c = 0
	for i in range(0, len(A)):
		if A[i] == '0':
			z += 1
		else:
			o += 1
	c = min(z, o)
	for i in range(0, len(A)):
		if A[i] == '0':
			z -= 1
			c1 += 1
		else:
			o -= 1
			c2 += 1
		c = min(c, c2 + z)
		c = min(c, c1 + o)
	print(c)


import sys
input = sys.stdin.readline
import math

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input().strip()
	return list(s[:len(s)])

def invr():
	return map(int, input().strip().split())

def from_file(f):
	return f.readline

def solution(s):
	n = len(s)
	ones = [0] * len(s)
	for (i, c) in enumerate(s):
		if c == '1':
			ones[i] += 1
		if i > 0:
			ones[i] += ones[i - 1]
	all_ones = ones[n - 1]
	all_zeros = n - all_ones
	res = float('inf')
	for i in range(n):
		no = ones[i]
		nz = all_zeros - (1 + i - ones[i])
		res = min(res, no + nz)
	for i in range(n):
		nz = 1 + i - ones[i]
		no = all_ones - ones[i]
		res = min(res, no + nz)
	return res
t = inp()
for _ in range(t):
	s = input().strip()
	print(solution(s))


t = int(input())
for s in range(t):
	array = []
	string = input()
	for i in string:
		array.append(int(i))
	n = len(array)
	shiv = [0]
	add = 0
	for i in array:
		if i == 1:
			add += 1
		shiv.append(add)
	hanu = [0]
	add = 0
	for i in array:
		if i == 0:
			add += 1
		hanu.append(add)
	rama = [0 for i in range(n + 1)]
	i = n
	add = 0
	while i >= 1:
		if array[i - 1] == 0:
			add += 1
		rama[i] = add
		i -= 1
	lax = [0 for i in range(n + 1)]
	(i, add) = (n, 0)
	while i >= 1:
		if array[i - 1] == 1:
			add += 1
		lax[i] = add
		i -= 1
	mini = 9999999
	a = [shiv[-1]]
	(i, h) = (0, shiv[-1])
	while i < n:
		if array[i] == 1:
			a.append(h - shiv[i + 1])
		else:
			a.append(h - shiv[i + 1] + 1)
		i += 1
	h = rama[1]
	b = []
	i = n
	while i >= 1:
		b.append(h - rama[i])
		i -= 1
	b.append(0)
	i = 0
	while i <= n:
		mini = min(mini, a[i] + b[n - i])
		i += 1
	maxi = 9999999
	a = [hanu[-1]]
	(i, h) = (0, hanu[-1])
	while i < n:
		if array[i] == 0:
			a.append(h - hanu[i + 1])
		else:
			a.append(h - hanu[i + 1] + 1)
		i += 1
	h = lax[1]
	b = []
	i = n
	while i >= 1:
		b.append(h - lax[i])
		i -= 1
	b.append(0)
	i = 0
	while i <= n:
		maxi = min(maxi, a[i] + b[n - i])
		i += 1
	print(min(mini, maxi))


a = int(input())
for i in range(a):
	s = input()
	ans = []
	count = 1
	for i in range(1, len(s)):
		if s[i] == s[i - 1]:
			count += 1
		else:
			ans.append(count)
			count = 1
	ans.append(count)
	if len(ans) <= 2:
		print(0)
		continue
	rem1 = []
	t1 = sum(ans[0::2])
	t2 = sum(ans[1::2])
	rem = [t1 - ans[0]]
	c1 = 0
	c2 = 0
	for i in range(1, len(ans)):
		if i % 2 == 1:
			c1 += ans[i - 1]
			rem.append(min(c2 + t1 - c1, t2 - c2 - ans[i] + c1))
		else:
			c2 += ans[i - 1]
			rem.append(min(c1 + t2 - c2, c2 + t1 - c1 - ans[i]))
	print(min(rem))


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	s = sys.stdin.readline().strip()
	length = len(s)
	(left1, left0, right1, right0) = ([0] * length, [0] * length, [0] * length, [0] * length)
	if s[0] == '1':
		left1[0] = 1
	else:
		left0[0] = 1
	for i in range(1, length):
		if s[i] == '1':
			left0[i] = left0[i - 1]
			left1[i] = left1[i - 1] + 1
		elif s[i] == '0':
			left0[i] = left0[i - 1] + 1
			left1[i] = left1[i - 1]
	if s[-1] == '1':
		right1[-1] = 1
	else:
		right0[-1] = 1
	for i in range(length - 2, -1, -1):
		if s[i] == '1':
			right0[i] = right0[i + 1]
			right1[i] = right1[i + 1] + 1
		else:
			right0[i] = right0[i + 1] + 1
			right1[i] = right1[i + 1]
	ans = min(left0[-1], left1[-1])
	for i in range(length - 1):
		ans = min(ans, left0[i] + right1[i + 1], left1[i] + right0[i + 1])
	if length == 1:
		ans = 0
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	(zeros, ones) = (0, 0)
	for ch in s:
		if ch == '0':
			zeros += 1
		else:
			ones += 1
	sol = min(zeros, ones)
	(pref_zeros, pref_ones) = (0, 0)
	for ch in s:
		if ch == '0':
			pref_zeros += 1
			zeros -= 1
		else:
			pref_ones += 1
			ones -= 1
		sol = min(sol, pref_zeros + ones)
		sol = min(sol, pref_ones + zeros)
	print(sol)


import sys
input = sys.stdin.readline
from sys import stdin, stdout

def main():
	for _ in range(int(input())):
		s = input().strip()
		n = len(s)
		count0 = [0] * (n + 1)
		count1 = [0] * (n + 1)
		c = 0
		c0 = 0
		for i in range(n):
			if s[i] == '1':
				c += 1
			else:
				c0 += 1
			count1[i] = c
			count0[i] = c0
		ans = 10 ** 9 + 7
		for i in range(n):
			ans = min(ans, count0[n - 1] - count0[i - 1] + count1[i - 1])
			ans = min(ans, count1[n - 1] - count1[i - 1] + count0[i - 1])
		print(ans)
main()


def readintlst():
	return list(map(int, input().split(' ')))

def readn():
	return int(input())
for _ in range(readn()):
	S = input()
	n = len(S)
	ans = 0
	if n >= 3:
		a00 = a11 = 0
		a10 = a01 = float('inf')
		a00 = S.count('1')
		a11 = S.count('0')
		for i in range(1, n):
			a01 = min(a01, S[:i].count('1') + S[i:].count('0'))
			a10 = min(a10, S[:i].count('0') + S[i:].count('1'))
		ans = min(a00, a11, a01, a10)
	print(ans)


import os
import sys
import io
import math
for t in range(int(input())):
	s = input()
	pos0 = []
	pos1 = []
	for i in range(len(s)):
		if s[i] == '0':
			pos0.append(i)
		else:
			pos1.append(i)
	n0 = len(pos0)
	n1 = len(pos1)
	answer0 = float('inf')
	count = 0
	for i in pos0:
		temp = 0
		for j in pos1:
			if j < i:
				temp += 1
			else:
				break
		temp += n0 - count - 1
		count += 1
		answer0 = min(temp, answer0)
	answer1 = float('inf')
	count = 0
	for i in pos1:
		temp = 0
		for j in pos0:
			if j < i:
				temp += 1
			else:
				break
		temp += n1 - count - 1
		count += 1
		answer1 = min(temp, answer1)
	print(min(answer0, answer1))


t = int(input())
for _ in range(t):
	s = [int(i) for i in input()]
	n = len(s)
	ans = 1e+18
	ct0 = []
	ct1 = []
	ct = 0
	for i in range(n):
		if s[i] == 0:
			ct += 1
		ct0.append(ct)
	ct = 0
	for i in range(n):
		if s[i] == 1:
			ct += 1
		ct1.append(ct)
	for i in range(n):
		c1 = ct0[i] + ct1[-1] - ct1[i]
		c2 = ct1[i] + ct0[-1] - ct0[i]
		ans = min(ans, c1, c2)
	print(ans)


from sys import stdin
from math import sqrt, gcd
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
for _ in range(I()):
	s = list(S())
	v = []
	l = l1 = len(s)
	z1 = z = s.count('0')
	o1 = o = s.count('1')
	d = 0
	for i in s:
		if i != '1':
			d += 1
			z -= 1
		l -= 1
		v += (abs(l - z) + d,)
	d = 0
	z = z1
	l = l1
	for i in s[::-1]:
		if i != '1':
			d += 1
			z -= 1
		l -= 1
		v += (abs(l - z) + d,)
	d = 0
	l = l1
	for i in s:
		if i != '0':
			d += 1
			o -= 1
		l -= 1
		v += (abs(l - o) + d,)
	d = 0
	l = l1
	o = o1
	for i in s[::-1]:
		if i != '0':
			d += 1
			o -= 1
		l -= 1
		v += (abs(l - o) + d,)
	print(min([min(v), s.count('1'), s.count('0')]))


import math as mt
import sys, string
input = sys.stdin.readline
from collections import defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())

def solve():
	s = input().strip()
	s = list(s)
	x = [0]
	y = [0]
	p = [0]
	q = [0]
	for i in range(len(s)):
		if s[i] == '0':
			x.append(x[-1] + 1)
			y.append(y[-1])
		else:
			x.append(x[-1])
			y.append(y[-1] + 1)
	for i in range(len(s) - 1, -1, -1):
		if s[i] == '0':
			p.append(p[-1] + 1)
			q.append(q[-1])
		else:
			p.append(p[-1])
			q.append(q[-1] + 1)
	x = x[1:]
	y = y[1:]
	p = p[-1::-1]
	q = q[-1::-1]
	p = p[:-1]
	q = q[:-1]
	ans = 10 ** 10
	if len(s) <= 2:
		print(0)
		return
	for i in range(1, len(s) - 1):
		if s[i] == '0':
			ans = min(ans, y[i - 1] + q[i + 1])
			ans = min(ans, y[i - 1] + p[i + 1])
			ans = min(ans, x[i - 1] + q[i + 1])
		else:
			ans = min(ans, y[i - 1] + p[i + 1])
			ans = min(ans, x[i - 1] + p[i + 1])
			ans = min(ans, x[i - 1] + q[i + 1])
	print(ans)
t = I()
for _ in range(t):
	solve()


import sys
import string
input = sys.stdin.readline
import math
from decimal import Decimal
t = int(input())
for i in range(t):
	n = input().rstrip()
	help1 = []
	help2 = []
	count = 0
	count2 = 0
	for char in n:
		if char == '0':
			count += 1
			help1.append(count)
			help2.append(count2)
		if char == '1':
			count2 += 1
			help1.append(count)
			help2.append(count2)
	best = 999999
	for j in range(len(n)):
		e = help2[j] + (help1[-1] - help1[j])
		f = help1[j] + (help2[-1] - help2[j])
		best = min(best, min(e, f))
	a = n[1:].count('0')
	b = n[1:].count('1')
	c = n[:-1].count('0')
	d = n[:-1].count('1')
	kkk = min(best, a, b, c, d)
	n = n[::-1]
	help1 = []
	help2 = []
	count = 0
	count2 = 0
	for char in n:
		if char == '0':
			count += 1
			help1.append(count)
			help2.append(count2)
		if char == '1':
			count2 += 1
			help1.append(count)
			help2.append(count2)
	best = 999999
	for j in range(len(n)):
		e = help2[j] + (help1[-1] - help1[j])
		f = help1[j] + (help2[-1] - help2[j])
		best = min(best, min(e, f))
	a = n[1:].count('0')
	b = n[1:].count('1')
	c = n[:-1].count('0')
	d = n[:-1].count('1')
	vvv = min(best, a, b, c, d)
	print(min(kkk, vvv))


t = int(input())
for _ in range(t):
	s = input()

	def solve(s):
		n = len(s)
		a = [0] * (n + 1)
		b = [0] * (n + 1)
		for i in range(n):
			a[i + 1] = a[i] + (s[i] == '1')
		for i in range(n - 1, -1, -1):
			b[i] = b[i + 1] + (s[i] == '0')
		return min((a[i] + b[i] for i in range(n + 1)))
	print(min(solve(s), solve(s[::-1])))


t = int(input())
for _ in range(t):
	s = input()
	(total0, total1) = (0, 0)
	for char in s:
		if char == '0':
			total0 += 1
		else:
			total1 += 1
	ans = min(total0, total1)
	(no0, no1) = (0, 0)
	for j in range(len(s)):
		no0 += s[j] == '0'
		no1 += s[j] == '1'
		ans = min(ans, no1 + total0 - no0)
		ans = min(ans, no0 + total1 - no1)
		s
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	oc = [0] * len(s)
	zc = [0] * len(s)
	if s[0] == '1':
		oc[0] = 1
	else:
		zc[0] = 1
	for i in range(1, len(s)):
		if s[i] == '1':
			oc[i] = oc[i - 1] + 1
			zc[i] = zc[i - 1]
		else:
			zc[i] = zc[i - 1] + 1
			oc[i] = oc[i - 1]
	ans = len(s)
	for i in range(len(s)):
		ans = min(ans, i + 1 - oc[i] + oc[-1] - oc[i])
	for i in range(len(s)):
		ans = min(ans, i + 1 - zc[i] + zc[-1] - zc[i])
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	s = list(s)
	poss = []
	for i in range(len(s)):
		if i == 0:
			if s[i] == '0':
				poss.append([1, 0])
			else:
				poss.append([0, 1])
		else:
			poss.append([j for j in poss[-1]])
			if s[i] == '0':
				poss[i][0] += 1
			else:
				poss[i][1] += 1
	tot0 = poss[-1][0]
	tot1 = poss[-1][1]
	ans = []
	for i in range(len(s)):
		ans.append(tot0 - poss[i][0] + poss[i][1])
		ans.append(tot1 - poss[i][1] + poss[i][0])
	ans.append(tot0)
	ans.append(tot1)
	print(min(ans))


for _ in range(int(input())):
	s = input()
	if '0' not in s or '1' not in s:
		print(0)
	else:
		ans = float('inf')
		for i in range(len(s)):
			ans = min(ans, min(s[i:].count('1') + s[:i].count('0'), s[i:].count('0') + s[:i].count('1')))
		print(ans)


t = int(input())
for _ in range(t):
	s = input()
	l = len(s)
	pref0 = [0] + [0 for i in range(len(s))]
	pref1 = [0] + [0 for i in range(len(s))]
	for i in range(1, len(s) + 1):
		if s[i - 1] == '1':
			pref1[i] = 1 + pref1[i - 1]
			pref0[i] = pref0[i - 1]
		else:
			pref1[i] = pref1[i - 1]
			pref0[i] = 1 + pref0[i - 1]
	s = 'X' + s
	i = 1
	m = 10 ** 4
	while i <= l:
		o = pref1[i] - pref1[0]
		z = pref0[l] - pref0[i]
		if z + o < m:
			m = z + o
		i += 1
	i = 1
	M = 10 ** 4
	while i <= l:
		o = pref1[l] - pref1[i]
		z = pref0[i] - pref0[0]
		if z + o < M:
			M = z + o
		i += 1
	print(min(pref1[l], pref0[l], m, M))


T = int(input())
for _ in range(T):
	s = input()
	n = len(s)
	cm = [0] * (n + 1)
	for i in range(n):
		cm[i + 1] = cm[i]
		if s[i] == '1':
			cm[i + 1] += 1
	ans = 10 ** 5
	lo = 0
	ro = 0
	for i in range(n):
		lo = cm[i]
		ro = cm[n] - cm[i + 1]
		ans = min(ans, ro + (i + 1) - lo)
		ans = min(ans, lo + (n - i) - ro)
	print(ans - 1)


def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	s = input().strip()
	n = len(s)
	nirvana1 = [0] * n
	nirvana0 = [0] * n
	for i in range(n):
		if i:
			nirvana1[i] = nirvana1[i - 1]
			nirvana0[i] = nirvana0[i - 1]
		if s[i] == '0':
			nirvana0[i] += 1
		else:
			nirvana1[i] += 1
	oasis1 = [0] * n
	oasis0 = [0] * n
	for i in range(n - 1, -1, -1):
		if i != n - 1:
			oasis1[i] = oasis1[i + 1]
			oasis0[i] = oasis0[i + 1]
		if s[i] == '0':
			oasis0[i] += 1
		else:
			oasis1[i] += 1
	ans = int(1e+18)
	for i in range(n - 1):
		ans = min(ans, min(nirvana1[i] + oasis0[i + 1], nirvana0[i] + oasis1[i + 1]))
	ans = min(ans, min(nirvana1[n - 1], nirvana0[n - 1]))
	print(ans)


for t in range(int(input())):
	s = input()
	zero_lft = [0]
	one_lft = [0]
	one_rgt = [0]
	zero_rgt = [0]
	for i in s:
		if i == '0':
			zero_lft.append(zero_lft[-1] + 1)
			one_lft.append(one_lft[-1])
		else:
			one_lft.append(one_lft[-1] + 1)
			zero_lft.append(zero_lft[-1])
	for i in s[::-1]:
		if i == '0':
			zero_rgt.append(zero_rgt[-1] + 1)
			one_rgt.append(one_rgt[-1])
		else:
			one_rgt.append(one_rgt[-1] + 1)
			zero_rgt.append(zero_rgt[-1])
	ans = 1000000000.0
	for i in range(len(s)):
		ans = min(ans, zero_lft[i] + one_rgt[len(s) - i], one_lft[i] + zero_rgt[len(s) - i])
	print(ans)


import sys
input = sys.stdin.readline
Q = int(input())
Ss = [list(input().rstrip()) for _ in range(Q)]
for S in Ss:
	L = len(S)
	dp = [[0, 0] for _ in range(L + 1)]
	fla = 0
	for i in range(L):
		if S[i] == '0':
			dp[i + 1][0] = dp[i][0] + 1
			dp[i + 1][1] = dp[i][1]
		else:
			dp[i + 1][0] = dp[i][0]
			dp[i + 1][1] = dp[i][1] + 1
		if i < L - 1 and S[i] != S[i + 1]:
			fla += 1
	if fla < 2:
		ans = 0
	else:
		ans = min(dp[L][0], dp[L][1])
		for i in range(L - 1):
			if S[i] == '0' and S[i + 1] == '1':
				ans = min(ans, dp[i + 1][1] + (dp[L][0] - dp[i + 2][0]))
			elif S[i] == '1' and S[i + 1] == '0':
				ans = min(ans, dp[i + 1][0] + (dp[L][1] - dp[i + 2][1]))
	print(ans)


t = int(input())
for i in range(t):
	s = input()
	n = len(s)
	r = s[::-1]
	count = 0
	count1 = 0
	if s.find('0') == -1 or s.find('0') == -1:
		print(0)
		continue
	else:
		l = []
		for j in range(n + 1):
			a = s.count('0', 0, j)
			b = s.count('1', j, n)
			c = s.count('1', 0, j)
			d = s.count('0', j, n)
			l.append(a + b)
			l.append(c + d)
		print(min(l))


def calc(s, b, j, mi, l):
	for i in s:
		if i == '0':
			r = len(s) - b - j
			va = l + r
			if va < mi:
				mi = va
		else:
			l += 1
			b -= 1
			r = len(s) - b - j
			va = l + r
			if va < mi:
				mi = va
		j += 1
	return mi
t = int(input())
for tc in range(t):
	s = input()
	b = s.count('1')
	b1 = b
	s1 = ''
	for i in range(len(s) - 1, -1, -1):
		s1 += s[i]
	mi = 10000000
	le = len(s) - b
	if le < mi:
		mi = le
	j = 1
	l = 0
	ret = calc(s, b, j, mi, l)
	mi = ret
	b = b1
	le = len(s) - b
	if le < mi:
		mi = le
	j = 1
	l = 0
	r1 = calc(s1, b, j, mi, l)
	print(r1)


mod = int(1000000000.0) + 7

def sp(n):
	if n == 1:
		return input()
	else:
		return map(str, input().split())

def np(n):
	if n == 1:
		return int(input())
	else:
		return map(int, input().split())
for _ in range(0, int(input())):
	s = sp(1)
	n = len(s)
	l = [0] * n
	l[0] = [int(s[0] == '1'), int(s[0] == '0')]
	for i in range(1, n):
		l[i] = [l[i - 1][0] + (s[i] == '1'), l[i - 1][1] + (s[i] == '0')]
	m = l[-1][0]
	m2 = l[-1][1]
	if n < 3:
		print(0)
	else:
		ans = min(n - m, m)
		for i in range(0, n):
			x = [l[i][0] + m2 - l[i][1], l[i][1] + m - l[i][0]]
			ans = min(ans, min(x))
		print(ans)


def shrey():
	s = input()
	s = list(s)
	l = len(s)
	ans = 10000000
	for i in range(l):
		c1 = 0
		c2 = 0
		for j in range(l):
			if j >= i:
				if s[j] != '0':
					c1 += 1
				else:
					c2 += 1
			elif s[j] != '1':
				c1 += 1
			else:
				c2 += 1
		a = min(c1, c2)
		ans = min(ans, a)
	print(ans)
T = int(input())
for _ in range(T):
	shrey()


for t in range(int(input())):
	string = input()
	(dlug_pod, poprz) = (0, string[0])
	ciagi = []
	for x in string:
		if x == poprz:
			dlug_pod += 1
		else:
			ciagi.append(dlug_pod)
			dlug_pod = 1
			poprz = x
	ciagi.append(dlug_pod)
	mini = 10 ** 10
	for i in range(len(ciagi)):
		licznik = 0
		for j in range(i + 2, len(ciagi), 2):
			licznik += ciagi[j]
		for j in range(i - 1, -1, -2):
			licznik += ciagi[j]
		mini = min(mini, licznik)
	print(mini)


for _ in range(int(input())):
	string = input()
	ans = min(string.count('1'), string.count('0'))
	for i in range(1, len(string)):
		(first, second) = (string[:i], string[i:])
		ans = min(ans, first.count('1') + second.count('0'))
		ans = min(ans, first.count('0') + second.count('1'))
	print(ans)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def main():
	t = I()
	rr = []
	for _ in range(t):
		s = S()
		t = [(0, 0)]
		u = [(0, 0)]
		for c in s:
			if c == '0':
				t.append((t[-1][0] + 1, t[-1][1]))
			else:
				t.append((t[-1][0], t[-1][1] + 1))
		for c in s[::-1]:
			if c == '0':
				u.append((u[-1][0] + 1, u[-1][1]))
			else:
				u.append((u[-1][0], u[-1][1] + 1))
		r = inf
		for i in range(len(s)):
			r = min(r, t[i][0] + u[-i - 1][1])
			r = min(r, t[i][1] + u[-i - 1][0])
		rr.append(r)
	return JA(rr, '\n')
print(main())


for _ in range(0, int(input())):
	s = input()
	if len(s) < 3:
		print(0)
		continue
	min = 0
	for i in s:
		if i == '1':
			min += 1
	sum = min
	for j in s:
		if j == '1':
			sum -= 1
		else:
			sum += 1
		if sum < min:
			min = sum
	for j in s:
		if j == '0':
			sum -= 1
		else:
			sum += 1
		if sum < min:
			min = sum
	print(min)


def solve(s):
	s = [1 if i == '1' else 0 for i in s]
	su = sum(s)
	n = len(s)
	l = 0
	ans = float('inf')
	for (i, v) in enumerate(s):
		l += v
		r = su - l
		cl = i + 1
		cr = n - cl
		ans = min(ans, min(l, cl - l) + min(r, cr - r))
	return ans

def main():
	inp = lambda : [int(x) for x in input().split()]
	inn = lambda : int(input())
	t = inn()
	for _ in range(t):
		s = input()
		print(solve(s))
main()


t = int(input())
for i in range(t):
	s = input()
	c = []
	for j in s:
		c.append(int(j))
	a = []
	b = []
	for j in range(len(c)):
		if c[j] == 1:
			a.append(j)
	c.reverse()
	for j in range(len(c)):
		if c[j] == 1:
			b.append(j)
	q = 0
	j = 0
	r = len(a)
	l = -1
	k = r
	while j < r:
		q += a[j] - l - 1
		k = min(k, q + r - 1 - j)
		l = a[j]
		j += 1
	a.reverse()
	p = 0
	j = 0
	r = len(b)
	l = -1
	m = r
	while j < r:
		p += b[j] - l - 1
		m = min(m, p + r - 1 - j)
		l = b[j]
		j += 1
	print(min(k, m))


for _ in range(int(input())):
	s = input()
	n = len(s)
	p = [0]
	for c in s:
		p.append(int(c) + p[-1])
	best = n
	for i in range(1, n + 1):
		prez = p[i]
		preo = i - prez
		suffz = p[n] - p[i]
		suffo = n - i - suffz
		best = min(best, prez + suffo, preo + suffz, prez + suffz, preo + suffo)
	print(best)


def main():
	t = int(input())
	for i in range(t):
		s = input()
		n = int(len(s))
		last_zero = [0 for i in range(n)]
		first_zero = [0 for i in range(n)]
		for i in range(n):
			if i != 0:
				first_zero[i] = first_zero[i - 1]
			if s[i] == '0':
				first_zero[i] += 1
		for i in range(n - 1, -1, -1):
			if i != n - 1:
				last_zero[i] = last_zero[i + 1]
			if s[i] == '0':
				last_zero[i] += 1
		answer = n
		if n == 1:
			answer = 0
		for i in range(n - 1):
			answer = min(answer, first_zero[i] + n - i - 1 - last_zero[i + 1])
			answer = min(answer, i + 1 - first_zero[i] + last_zero[i + 1])
			answer = min(answer, first_zero[i] + last_zero[i + 1])
			answer = min(answer, i + 1 - first_zero[i] + n - 1 - i - last_zero[i + 1])
		print(answer)
main()


for _ in range(int(input())):
	string = input()
	ma = 10 ** 18
	for i in range(len(string)):
		a = string[:i].count('0')
		b = string[i:].count('1')
		c = string[:i].count('1')
		d = string[i:].count('0')
		ma = min(ma, a + b, c + d)
	print(ma)


t = int(input())
for i in range(t):
	a = list(input())
	if len(a) <= 2:
		print(0)
	else:
		a.append('a')
		b = []
		count = 0
		now = a[0]
		for j in range(len(a)):
			if a[j] == now:
				count += 1
			else:
				b.append(count)
				count = 1
				now = a[j]
		if len(b) <= 2:
			print(0)
		else:
			c = []
			for j in range(len(b) - 1):
				a1 = 0
				a2 = 0
				b1 = 0
				b2 = 0
				for x in range(0, j + 1):
					if x % 2 == 0:
						a1 += b[x]
					else:
						a2 += b[x]
				for x in range(j + 1, len(b)):
					if x % 2 == 0:
						b1 += b[x]
					else:
						b2 += b[x]
				c.append(a1 + b1)
				c.append(a1 + b2)
				c.append(a2 + b1)
				c.append(a2 + b2)
			print(min(c))


from sys import stdin, stdout

def main():
	t = int(stdin.readline())
	for _ in range(t):
		s = stdin.readline().strip()
		l = len(s)
		zero_only = [10000] * (l + 1)
		one_only = [10000] * (l + 1)
		zto = [10000] * (l + 1)
		otz = [10000] * (l + 1)
		zero_only[0] = 0
		one_only[0] = 0
		zto[0] = 0
		otz[0] = 0
		for (i, x) in enumerate(s):
			if x == '0':
				zto[i + 1] = min(zto[i + 1], zto[i] + 1, zero_only[i] + 1)
				one_only[i + 1] = min(one_only[i + 1], one_only[i] + 1)
				zero_only[i + 1] = zero_only[i]
				otz[i + 1] = min(one_only[i], otz[i])
			else:
				otz[i + 1] = min(otz[i + 1], otz[i] + 1, one_only[i] + 1)
				zero_only[i + 1] = min(zero_only[i + 1], zero_only[i] + 1)
				one_only[i + 1] = one_only[i]
				zto[i + 1] = min(zero_only[i], zto[i])
		ans = min(zero_only[l], one_only[l], zto[l], otz[l])
		stdout.write(str(ans) + '\n')
main()


for _ in range(int(input())):
	A = input()
	n = len(A)
	X = []
	if n == 1 or n == 2:
		print(0)
	else:
		for _ in range(n):
			(a, b) = (0, 0)
			(c, d) = (0, 0)
			for x in range(_ + 1):
				if A[x] == '1':
					a += 1
				else:
					b += 1
			for y in range(_ + 1, n):
				if A[y] == '1':
					c += 1
				else:
					d += 1
			X.append(min(a + d, c + b))
		print(min(X))


t = int(input())
for test in range(t):
	s = input()
	arra = [0 for i in range(len(s) + 2)]
	arrb = [0 for i in range(len(s) + 2)]
	for i in range(len(s)):
		arra[i + 1] = arra[i]
		if s[i] == '0':
			arra[i + 1] += 1
	for i in range(len(s)):
		arrb[i + 1] = arrb[i]
		if s[i] == '1':
			arrb[i + 1] += 1
	arra[len(s) + 1] = arra[len(s)]
	arrb[len(s) + 1] = arrb[len(s)]
	ans = 99999999999999999
	for i in range(1, len(s) + 1):
		ans = min(arra[i] + arrb[len(s) + 1] - arrb[i], ans)
		ans = min(arrb[i] + arra[len(s) + 1] - arra[i], ans)
	print(ans)


t = int(input())
for i in range(t):
	a = input()
	arr = list(map(int, [i for i in a]))
	c0 = 0
	c1 = 0
	for i in arr:
		if i == 0:
			c0 += 1
		else:
			c1 += 1
	d0 = 0
	d1 = 0
	ans = len(arr)
	for i in range(len(arr)):
		if arr[i] == 0:
			d0 += 1
		else:
			d1 += 1
		ans = min(ans, d0 + c1 - d1, d1 + c0 - d0)
	print(ans)


t = int(input())
for i in range(t):
	s = input()
	n = len(s)
	c1 = [0] * (n + 1)
	c0 = [0] * (n + 1)
	for i in range(n):
		if s[i] == '1':
			c1[i + 1] = c1[i] + 1
			c0[i + 1] = c0[i]
		else:
			c1[i + 1] = c1[i]
			c0[i + 1] = c0[i] + 1
	ans = c1[-1]
	mn = min(ans, c0[-1])
	for i in range(1, n + 1, 1):
		mn = min(mn, c1[i] + c0[-1] - c0[i])
		mn = min(mn, c0[i] + c1[-1] - c1[i])
	print(mn)


for i in range(int(input())):
	string = input()
	m = []
	for x in range(len(string)):
		m.append(min(string[:x + 1].count('1') + string[x + 1:].count('0'), string[:x + 1].count('0') + string[x + 1:].count('1')))
	print(min(m))


import math
import sys
input = sys.stdin.readline
t = int(input())
for cs in range(t):
	s = input().strip()
	n = len(s)
	cnt = [0, 0]
	pre = [[0] * n, [0] * n]
	suf = [[0] * n, [0] * n]
	for i in range(n):
		c = ord(s[i]) - ord('0')
		cnt[c] += 1
		(pre[0][i], pre[1][i]) = (cnt[0], cnt[1])
	cnt = [0, 0]
	for i in range(n - 1, -1, -1):
		c = ord(s[i]) - ord('0')
		cnt[c] += 1
		(suf[0][i], suf[1][i]) = (cnt[0], cnt[1])
	ans = min(pre[0][n - 1], pre[1][n - 1])
	for i in range(n - 1):
		ans = min(ans, pre[0][i] + suf[1][i + 1])
		ans = min(ans, pre[1][i] + suf[0][i + 1])
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for t1 in range(t):
	s = input()
	s = s.strip()
	l = [s[0]]
	for i in range(1, len(s)):
		if s[i] != s[i - 1]:
			l.append(s[i])
	count = len(s)
	if len(l) >= 3:
		zero = 0
		one = 0
		for i in range(len(s)):
			if s[i] == '0':
				zero += 1
			else:
				one += 1
		count = min(zero, one)
		a1 = []
		a2 = []
		c1 = 0
		c2 = 0
		for i in range(len(s)):
			if s[i] == '0':
				c1 += 1
			else:
				c2 += 1
			a1.append(c1)
			a2.append(c2)
		n = len(s)
		for i in range(n):
			extra = 0
			extra += a1[i]
			extra += a2[n - 1] - a2[i]
			count = min(extra, count)
			extra = 0
			extra += a2[i]
			extra += a1[n - 1] - a1[i]
			count = min(extra, count)
		print(count)
	else:
		print(0)


t = int(input())
for _ in range(t):
	s = input()
	total_ones = 0
	total_zeros = 0
	done_ones = 0
	done_zeros = 0
	min1 = float('inf')
	min2 = float('inf')
	for i in s:
		if i == '0':
			total_zeros += 1
		else:
			total_ones += 1
	for i in s:
		if i == '0':
			done_zeros += 1
		else:
			done_ones += 1
		temp1 = done_ones + total_zeros - done_zeros
		if min1 > temp1:
			min1 = temp1
		temp2 = done_zeros + total_ones - done_ones
		if min2 > temp2:
			min2 = temp2
	print(min(min1, min2))


for _ in range(int(input())):
	s = str(input())
	ans = 1000
	n = len(s)
	if n <= 2:
		print(0)
		continue
	t = [0 for _ in range(n + 1)]
	for i in range(1, n + 1):
		if s[i - 1] == '0':
			t[i] = t[i - 1] + 1
		else:
			t[i] = t[i - 1]
	z = t[-1]
	o = n - z
	ans = 1000
	for i in range(1, n + 1):
		tmp = min(i - t[i] + z - t[i], t[i] + o - i + t[i])
		ans = min(ans, tmp)
	print(min(ans, o, z))


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
from math import sqrt, floor, factorial, gcd
from collections import deque, Counter, defaultdict
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from collections import deque
input = lambda : sys.stdin.readline().rstrip('\r\n')
read = lambda : list(map(int, input().split(' ')))

def func(arr):
	return sum([arr[i] for i in range(0, len(arr), 2)])

def solve():
	for _ in range(int(input())):
		s = input()
		suf = [0, 0]
		ans = float('inf')
		for i in s:
			suf[int(i) % 2] += 1
		(suf0, suf1) = suf
		ans += min(suf0, suf1)
		(pref0, pref1) = (0, 0)
		for i in range(len(s)):
			pref0 += '0' == s[i]
			suf0 -= '0' == s[i]
			pref1 += '1' == s[i]
			suf1 -= '1' == s[i]
			ans = min(ans, pref0 + suf1, pref1 + suf0)
		print(ans)
solve()


t = int(input())
for _ in range(t):
	s = input()
	no = 0
	nz = 0
	no = sum([int(i) for i in s])
	nz = len(s) - no
	done = [0, 0]
	cost = 1000
	for i in s:
		n = int(i)
		done[n] += 1
		cost1 = done[0] + no - done[1]
		cost2 = done[1] + nz - done[0]
		cost_final = min(cost1, cost2)
		cost = min(cost, cost_final)
	print(cost)


t = int(input())
while t != 0:
	t -= 1
	s = input()
	(pr0, pr1, sf0, sf1) = (0, 0, 0, 0)
	for i in s:
		if i == '0':
			sf0 += 1
		else:
			sf1 += 1
	ans = min(sf0, sf1)
	for i in s:
		if i == '0':
			pr0 += 1
			sf0 -= 1
		else:
			pr1 += 1
			sf1 -= 1
		ans = min(ans, pr0 + sf1)
		ans = min(ans, pr1 + sf0)
	print(ans)


T = int(input())

def cost_01(N, prefix_0, prefix_1, suffix_0, suffix_1):
	ans = float('inf')
	for i in range(N + 1):
		if i > 0:
			front_cost = prefix_1[i - 1]
		else:
			front_cost = 0
		if i < N:
			back_cost = suffix_0[i]
		else:
			back_cost = 0
		ans = min(ans, front_cost + back_cost)
	return ans

def cost_10(N, prefix_0, prefix_1, suffix_0, suffix_1):
	ans = float('inf')
	for i in range(N + 1):
		if i > 0:
			front_cost = prefix_0[i - 1]
		else:
			front_cost = 0
		if i < N:
			back_cost = suffix_1[i]
		else:
			back_cost = 0
		ans = min(ans, front_cost + back_cost)
	return ans

def ans(s):
	if not ('01' in s and '10' in s):
		return 0
	N = len(s)
	if s[0] == '0':
		prefix_0 = [1]
		prefix_1 = [0]
	else:
		prefix_0 = [0]
		prefix_1 = [1]
	for i in s[1:]:
		if i == '0':
			prefix_0 += [prefix_0[-1] + 1]
			prefix_1 += [prefix_1[-1] + 0]
		else:
			prefix_0 += [prefix_0[-1] + 0]
			prefix_1 += [prefix_1[-1] + 1]
	s = s[::-1]
	if s[0] == '0':
		suffix_0 = [1]
		suffix_1 = [0]
	else:
		suffix_0 = [0]
		suffix_1 = [1]
	for i in s[1:]:
		if i == '0':
			suffix_0 += [suffix_0[-1] + 1]
			suffix_1 += [suffix_1[-1] + 0]
		else:
			suffix_0 += [suffix_0[-1] + 0]
			suffix_1 += [suffix_1[-1] + 1]
	s = s[::-1]
	suffix_0 = suffix_0[::-1]
	suffix_1 = suffix_1[::-1]
	return min(cost_01(N, prefix_0, prefix_1, suffix_0, suffix_1), cost_10(N, prefix_0, prefix_1, suffix_0, suffix_1))
for t in range(T):
	s = input()
	print(ans(s))


import sys
readline = sys.stdin.readline
T = int(readline())
Ans = [None] * T
for qu in range(T):
	S = list(map(int, readline().strip()))
	N = len(S)
	so = S.count(1)
	ans = min(so, N - so)
	table = S[:]
	for i in range(1, N):
		table[i] += table[i - 1]
	for i in range(N):
		one = table[i]
		zero = i + 1 - one
		no = so - one
		nz = N - (i + 1) - no
		ans = min(ans, no + zero, one + nz)
	Ans[qu] = ans
print('\n'.join(map(str, Ans)))


t = int(input())
for ti in range(t):
	s = input()
	test = []
	ans = 10000
	difference = 0
	for i in range(len(s)):
		test.append('0')
	for i in range(len(s)):
		if s[i] != test[i]:
			difference += 1
	diff = difference
	for j in range(len(s)):
		test[j] = '1'
		if test[j] == s[j]:
			diff -= 1
		else:
			diff += 1
		difference = diff
		ans = min(ans, difference)
	for j in range(len(s)):
		test[j] = '0'
		if test[j] == s[j]:
			diff -= 1
		else:
			diff += 1
		difference = diff
		ans = min(ans, difference)
	print(ans)


def calc_answer(d_vec):
	ans = sum(d_vec[len(d_vec) - 1:0:-2])
	for i in range(len(d_vec) // 2):
		i_ans = sum(d_vec[len(d_vec) - 2:2 * i:-2]) + sum(d_vec[1:2 * i:2])
		ans = min(ans, i_ans)
	return ans

def process_test_case(i_t):
	s = input()
	d_vec = []
	c = '2'
	i_group = 0
	for symbol in s:
		if symbol != c:
			if i_group > 0:
				d_vec.append(i_group)
			c = symbol
			i_group = 1
		else:
			i_group += 1
	if i_group > 0:
		d_vec.append(i_group)
	if len(d_vec) % 2 == 1:
		d_vec.append(0)
	if len(d_vec) == 2:
		print(0)
	else:
		ans = calc_answer(d_vec)
		d_vec = [0] + d_vec + [0]
		ans = min(ans, calc_answer(d_vec))
		print(ans)
	return None
n_tests = int(input())
for i_test in range(1, n_tests + 1):
	process_test_case(i_test)


t = int(input())
for i in range(t):
	s = input()
	n = len(s)
	j = 0
	l = []
	l1 = []
	while j < n:
		if s[j] == '0':
			a = ''
			l1.append(0)
			while s[j] == '0':
				a += '0'
				j += 1
				if j == n:
					break
			l.append(a)
		else:
			a = ''
			while s[j] == '1':
				a += '1'
				j += 1
				if j == n:
					break
			l1.append(1)
			l.append(a)
	if len(l) <= 2:
		print(0)
	else:
		c = 0
		d = 0
		l2 = [0] * len(l)
		l3 = [0] * len(l)
		(a, b) = (0, 0)
		for j in range(len(l) - 1, -1, -1):
			if l1[j] == 0:
				c += len(l[j])
			else:
				d += len(l[j])
			l2[j] = c
			l3[j] = d
		ans = n
		for j in range(len(l)):
			ans = min(ans, a + l3[j], b + l2[j])
			if l1[j] == 0:
				a += len(l[j])
			else:
				b += len(l[j])
		print(ans)


for _ in range(int(input())):
	s = input()
	count1 = s.count('1')
	count0 = s.count('0')
	lst = []
	x = 0
	y = 0
	for i in range(len(s)):
		if s[i] == '0':
			l = min(x + count0 - y, count1 - x + y)
			lst.append(l)
			y += 1
		else:
			lst.append(min(y + count1 - x, count0 - y + x))
			x += 1
	print(min(lst))


import sys, math, itertools, collections, copy
input = sys.stdin.readline

def solve():
	s = input()
	s = s[:-1]
	N = len(s)
	ans = float('inf')
	cnt = 0
	for i in range(N):
		if s[i] == '0':
			cnt += 1
	ans = min(ans, cnt)
	cnt = 0
	for i in range(N):
		if s[i] == '1':
			cnt += 1
	ans = min(ans, cnt)
	cnt = 0
	for i in range(N):
		cur = cnt
		for j in range(i + 1, N):
			if s[j] == '0':
				cur += 1
		ans = min(ans, cur)
		if s[i] == '1':
			cnt += 1
	cnt = 0
	for i in range(N):
		cur = cnt
		for j in range(i + 1, N):
			if s[j] == '1':
				cur += 1
		ans = min(ans, cur)
		if s[i] == '0':
			cnt += 1
	ans = min(ans, cnt)
	print(ans)
for _ in range(int(input())):
	solve()


T = int(input())
for t in range(T):
	s = input()
	flip = sum([1 if k == '0' else 0 for k in s])
	result = flip
	for i in range(len(s)):
		if s[i] == '0':
			flip -= 1
		else:
			flip += 1
		result = min(result, flip)
	flip = sum([1 if k == '1' else 0 for k in s])
	result = min(result, flip)
	for i in range(len(s)):
		if s[i] == '1':
			flip -= 1
		else:
			flip += 1
		result = min(result, flip)
	print(result)


for _ in range(int(input())):
	lst = list(input())
	n = len(lst)
	c = 0
	a = 0
	a1 = 0
	lstc = list()
	lstc1 = list()
	for i in range(n):
		if lst[i] == '0':
			a += 1
			lstc.append(a)
			lstc1.append(a1)
		else:
			a1 += 1
			lstc1.append(a1)
			lstc.append(a)
	for i in range(n - 1, -1, -1):
		c3 = i + 1 - lstc[i] + n - (i + 1) - (lstc1[n - 1] - lstc1[i])
		c4 = i + 1 - lstc1[i] + n - (i + 1) - (lstc[n - 1] - lstc[i])
		if i == n - 1:
			c = min(c3, c4)
		elif min(c3, c4) < c:
			c = min(c3, c4)
	print(c)


for _ in range(int(input())):
	s = input()
	n = len(s)
	cur_ans = sum(map(int, s))
	ans = min(cur_ans, n - cur_ans)
	for i in range(n):
		if s[i] == '1':
			cur_ans -= 1
			ans = min(ans, cur_ans)
		else:
			cur_ans += 1
			ans = min(ans, n - cur_ans)
	print(ans)


T = int(input())
for _ in range(T):
	s = input()
	one = [0]
	zero = [0]
	for (i, c) in enumerate(s):
		if c == '0':
			zero.append(zero[-1] + 1)
			one.append(one[-1])
		else:
			one.append(one[-1] + 1)
			zero.append(zero[-1])
	n = len(s)
	ans = n
	for i in range(1, n + 1):
		ans = min(ans, zero[i] + one[n] - one[i])
		ans = min(ans, one[i] + zero[n] - zero[i])
	print(ans)


from sys import stdin
A = int(stdin.readline())
for k in range(0, A):
	B = stdin.readline()
	bb = len(B) - 1
	count = 0
	for u in range(0, bb):
		count += int(B[u])
	MIN = bb
	sum = 0
	for t in range(0, bb):
		CON1 = sum
		CON2 = bb - t - (count - sum)
		MIN = min(MIN, CON1 + CON2)
		CON3 = t - sum
		CON4 = count - sum
		MIN = min(MIN, CON3 + CON4)
		sum += int(B[t])
	print(MIN)


from collections import defaultdict
t = int(input())
for _ in range(t):
	s = input()
	if len(s) <= 1:
		print(0)
		continue
	till = [[0, 0] for x in range(len(s))]
	after = [[0, 0] for x in range(len(s) + 1)]
	for i in range(len(s)):
		till[i][0] = till[i - 1][0]
		till[i][1] = till[i - 1][1]
		if s[i] == '0':
			till[i][0] += 1
		else:
			till[i][1] += 1
	for i in range(len(s) - 1, 0, -1):
		after[i - 1][0] = after[i][0]
		after[i - 1][1] = after[i][1]
		if s[i] == '0':
			after[i - 1][0] += 1
		else:
			after[i - 1][1] += 1
	fin = 1000
	for i in range(len(s)):
		fin = min(fin, till[i][0] + after[i][1], till[i][1] + after[i][0])
	print(fin)


for i in range(int(input())):
	n = input()
	z = n.count('0')
	o = n.count('1')
	a = min(o, z)
	for i in range(len(n)):
		Z = n[:i + 1].count('0')
		O = o - n[:i + 1].count('1')
		op = Z + O
		a = min(a, op)
		O = n[:i + 1].count('1')
		Z = z - n[:i + 1].count('0')
		op = Z + O
		a = min(a, op)
	print(a)


for _ in range(int(input())):
	s = input()
	(z, z1) = (0, 0)
	l = len(s)
	for i in s:
		if i == '0':
			z += 1
	ans = min(z, l - z)
	for i in range(l):
		if s[i] == '0':
			z1 += 1
		ans = min(ans, i + 1 - z1 + z - z1)
		ans = min(ans, z1 + (l - i - 1) - (z - z1))
	print(ans)


from sys import stdin, stdout
import math
from collections import Counter, deque
L = lambda : list(map(int, stdin.readline().strip().split()))
M = lambda : map(int, stdin.readline().strip().split())
I = lambda : int(stdin.readline().strip())
IN = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()
mod = 1000000007

def s(a):
	print(' '.join(list(map(str, a))))
for i in range(I()):
	s = IN()
	x = len(s)
	m = x
	for i in range(x + 1):
		a = s[:i]
		b = s[i:]
		m = min(a.count('0') + b.count('1'), m)
		m = min(a.count('1') + b.count('0'), m)
	print(m)


t = int(input())
for _ in range(t):
	s = input()
	f1 = f2 = f3 = f4 = f5 = f6 = 0
	for i in range(len(s)):
		if s[i] == '1':
			f1 = 1
		else:
			f2 = 1
		if f1 == 1 and s[i] == '0':
			f3 = 1
		elif f2 == 1 and s[i] == '1':
			f4 = 1
		if f3 == 1 and s[i] == '1':
			f5 = 1
		elif f4 == 1 and s[i] == '0':
			f6 = 1
	if max(f1, f2) > 0:
		c = s[0]
		ct = 1
		a = []
		for i in range(1, len(s)):
			if s[i] != c:
				a.append((c, ct))
				c = s[i]
				ct = 1
			else:
				ct += 1
		a.append((c, ct))
		ec = 0
		oc = 0
		res = 0
		i = 0
		if len(a) == 3:
			res = min(a, key=lambda x: x[1])[1]
			print(res)
		else:
			a0 = 0
			a1 = 0
			for i in a:
				if i[0] == '1':
					a1 += i[1]
				else:
					a0 += i[1]
			b0 = 0
			b1 = 0
			res = 1002
			for i in a:
				if i[0] == '1':
					b1 += i[1]
					a1 -= i[1]
				else:
					b0 += i[1]
					a0 -= i[1]
				res = min(res, a0 + b1, a1 + b0)
			print(res)
	else:
		print(0)


import sys, random, math
from bisect import bisect_left, bisect_right
input = sys.stdin.readline

def main():
	inf = 10 ** 20
	t = int(input())
	for _ in range(1, t + 1):
		s = input().strip()
		(ps0, ps1) = ([0], [0])
		for i in range(len(s)):
			if s[i] == '0':
				ps0.append(ps0[-1] + 1)
				ps1.append(ps1[-1])
			else:
				ps0.append(ps0[-1])
				ps1.append(ps1[-1] + 1)
		ans = inf
		for i in range(len(s)):
			ans = min(ans, ps0[i] - ps0[0] + (ps1[len(s)] - ps1[i]))
			ans = min(ans, ps1[i] - ps1[0] + (ps0[len(s)] - ps0[i]))
		print(ans)
main()


from sys import maxsize
from itertools import groupby
for _ in range(int(input())):
	s = input()
	zeros = s.count('0')
	ones = s.count('1')
	result = []
	for (key, group) in groupby(s):
		result.append(key)
	if len(result) == 2:
		print(0)
	elif len(s) <= 2:
		print(0)
	elif len(s) == 3:
		if s == '101':
			print(1)
		elif s == '010':
			print(1)
		else:
			print(0)
	elif s.count('0') == 0:
		print(0)
	elif s.count('1') == 0:
		print(0)
	else:
		ans = maxsize
		s += 'x'
		tmppos = -1
		pos = [0, 0]
		left0 = 0
		left1 = 0
		right0 = 0
		right1 = 0
		c = 0
		zero = 0
		one = 0
		tmp = 0
		tmpone = 0
		start = s.index('1')
		for i in range(start):
			if s[i] == '1':
				one += 1
			else:
				zero += 1
		for i in range(start, len(s)):
			if s[i] == '1':
				if tmp == 0:
					pos[0] = i
				tmp += 1
				tmpone += 1
			else:
				pos[1] = i
				if tmpone > 0:
					remzero = zeros - zero
					remone = ones - one - (pos[1] - pos[0])
					a1 = one + remzero
					a2 = zero + remone
					a3 = zero + remzero
					a4 = one + remone + pos[1] - pos[0]
					c = min(a1, a2, a3, a4)
					ans = min(ans, c)
				zero += 1
				tmp = 0
				one += tmpone
				tmpone = 0
		print(ans)


from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
	s = stdin.readline().strip()
	(l, r) = ([0] * len(s), [0] * len(s))
	for i in range(len(s)):
		if i > 0:
			l[i] = l[i - 1]
		if s[i] == '0':
			l[i] += 1
	for i in range(len(s) - 1, -1, -1):
		if i < len(s) - 1:
			r[i] = r[i + 1]
		if s[i] == '0':
			r[i] += 1
	ans = len(s)
	for i in range(len(s)):
		if i == 0:
			lans = 0
		else:
			lans = min(i - l[i - 1], l[i - 1])
		if i == len(s) - 1:
			rans = 0
		else:
			rans = min(len(s) - i - r[i], r[i])
		ans = min(lans + rans, ans)
	print(ans)


def strLine():
	return input().split()

def lotInt():
	return map(int, input().split())

def readInt():
	return int(input())

def readStr():
	return input()

def readArr():
	arr = input()
	return [int(n) for n in arr.split()]
dp = [[0 for i in range(10)] for j in range(1010)]

def __main():
	t = readInt()
	for case in range(t):
		t -= 1
		str = input()
		n = len(str)
		for i in range(n):
			if str[i] == '1':
				i += 1
				dp[i][0] = dp[i - 1][0] + 1
				dp[i][1] = dp[i - 1][1]
				dp[i][2] = min(dp[i - 1][0], dp[i - 1][2])
				dp[i][3] = min(dp[i - 1][1] + 1, dp[i - 1][3] + 1)
				i -= 1
			else:
				i += 1
				dp[i][0] = dp[i - 1][0]
				dp[i][1] = dp[i - 1][1] + 1
				dp[i][2] = min(dp[i - 1][0] + 1, dp[i - 1][2] + 1)
				dp[i][3] = min(dp[i - 1][1], dp[i - 1][3])
				i -= 1
		ans = dp[n][0]
		for i in range(1, 4):
			ans = min(ans, dp[n][i])
		print(ans)
__main()


from io import BytesIO, IOBase
import os
import sys
from typing import *
from math import *
from itertools import *
from collections import *

def solve():
	s = input()
	total0 = 0
	total1 = 0
	for i in s:
		if i == '1':
			total1 += 1
		else:
			total0 += 1
	done0 = 0
	done1 = 0
	ans = sys.maxsize
	for i in s:
		if i == '0':
			done0 += 1
		else:
			done1 += 1
		ans = min(ans, done1 + total0 - done0)
		ans = min(ans, done0 + total1 - done1)
	pr(ans)

def main():
	T = inp()
	for _ in range(T):
		solve()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input() -> str:
	return sys.stdin.readline().rstrip('\r\n')

def li():
	return list(sys.stdin.readline().split())

def mp():
	return list(map(int, sys.stdin.readline().split()))

def inp() -> int:
	return int(input())

def pr(*args, **kwargs) -> None:
	for arg in args:
		sys.stdout.write(str(arg) + '\n')
	try:
		if kwargs['flush'] is True:
			sys.stdout.flush()
	except KeyError:
		pass

def prl(*args, **kwargs) -> None:
	for arg in args:
		sys.stdout.write(str(arg) + ' ')
	try:
		if kwargs['flush'] is True:
			sys.stdout.flush()
	except KeyError:
		pass
main()


for _ in range(int(input())):
	s = input()
	n = len(s)
	cnt1 = [0 for i in range(n + 1)]
	cnt0 = [0 for i in range(n + 1)]
	for i in range(n):
		if s[i] == '1':
			cnt1[i + 1] = cnt1[i] + 1
			cnt0[i + 1] = cnt0[i]
		else:
			cnt1[i + 1] = cnt1[i]
			cnt0[i + 1] = cnt0[i] + 1
	f_ans = min(cnt1[n], cnt0[n])
	for i in range(n):
		ans = cnt0[i + 1] + cnt1[n] - cnt1[i + 1]
		f_ans = min(ans, f_ans)
	for i in range(n):
		ans = cnt1[i + 1] + cnt0[n] - cnt0[i + 1]
		f_ans = min(ans, f_ans)
	print(f_ans)


def read_int():
	return int(input())

def read_ints():
	return map(int, input().split(' '))
t = read_int()
for case_num in range(t):
	s = input()
	n = len(s)
	one = [0 for i in range(n + 1)]
	zero = [0 for i in range(n + 1)]
	for i in range(n):
		one[i + 1] = one[i] + (1 if s[i] == '1' else 0)
		zero[i + 1] = zero[i] + (1 if s[i] == '0' else 0)
	ans = n
	for i in range(n + 1):
		cost = min(one[i] + zero[n] - zero[i], zero[i] + one[n] - one[i])
		ans = min(ans, cost)
	print(ans)


from sys import stdin, stdout
import sys
import math

def get_int():
	return int(stdin.readline().strip())

def get_ints():
	return map(int, stdin.readline().strip().split())

def get_array():
	return list(map(int, stdin.readline().strip().split()))

def get_string():
	return stdin.readline().strip()

def op(c):
	return stdout.write(c)
for _ in range(int(stdin.readline())):
	s = get_string()
	n = len(s)
	zs = [0] * (n + 1)
	os = [0] * (n + 1)
	for i in range(len(s) - 1, -1, -1):
		if s[i] == '1':
			os[i] += os[i + 1] + 1
			zs[i] += zs[i + 1]
		else:
			os[i] += os[i + 1]
			zs[i] += zs[i + 1] + 1
	curr_one = 0
	cur_zero = 0
	ans = min(zs[0], os[0])
	for i in range(len(s)):
		if s[i] == '1':
			curr_one += 1
		else:
			cur_zero += 1
		ans = min(ans, curr_one + zs[i + 1])
		ans = min(ans, cur_zero + os[i + 1])
	print(ans)


from os import path
import sys, time
from math import ceil, floor, gcd, log, log2, factorial
from collections import defaultdict, Counter, OrderedDict, deque
from itertools import combinations, accumulate
from heapq import *
from functools import reduce
from operator import mul
maxx = float('inf')
I = lambda : int(sys.stdin.buffer.readline())
tup = lambda : map(int, sys.stdin.buffer.readline().split())
lint = lambda : [int(x) for x in sys.stdin.buffer.readline().split()]
S = lambda : sys.stdin.readline().strip('\n')
grid = lambda r: [lint() for i in range(r)]
stpr = lambda x: sys.stdout.write(f'{x}' + '\n')
star = lambda x: print(' '.join(map(str, x)))
localsys = 0
start_time = time.time()
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for _ in range(I()):
	s = S()
	(ones, zeroes) = (0, 0)
	for i in s:
		if i == '1':
			ones += 1
		else:
			zeroes += 1
	(ans, to, tz) = (maxx, 0, 0)
	for i in s:
		if i == '1':
			to += 1
		else:
			tz += 1
		t1 = tz + (ones - to)
		t2 = to + (zeroes - tz)
		ans = min(ans, t1, t2)
	print(ans)
if localsys:
	print('\n\nTime Elased :', time.time() - start_time, 'seconds')


for _ in range(int(input())):
	s = input()
	sum = 0
	for i in s:
		if i == '1':
			sum += 1
	if sum == 0 or sum == len(s):
		print(0)
	else:
		ans_now = 100000000000000
		l = 0
		r = sum
		for i in range(len(s)):
			if s[i] == '1':
				l += 1
				r -= 1
			ans_now = min(i + 1 - l + r, ans_now)
			ans_now = min(l + len(s) - i - 1 - r, ans_now)
		print(ans_now)


import sys
input = sys.stdin.readline
t = int(input())
while t > 0:
	t -= 1
	s = input()
	ones = s.count('1')
	zeros = s.count('0')
	ans = float('inf')
	till0 = 0
	till1 = 0
	for i in s:
		if i == '0':
			till0 += 1
		elif i == '1':
			till1 += 1
		ans = min(ans, till1 + zeros - till0)
		ans = min(ans, till0 + ones - till1)
	print(ans)


from sys import stdin
for _ in range(int(stdin.readline())):
	s = stdin.readline().strip()
	il = []
	i = 0
	while i != len(s):
		co = 0
		while i != len(s) and s[i] == '1':
			co += 1
			i += 1
		if co != 0:
			il.append(co)
		co = 0
		while i != len(s) and s[i] == '0':
			co += 1
			i += 1
		if co != 0:
			il.append(co)
	if len(il) <= 2:
		print(0)
	else:
		tot_ze = s.count('0')
		tot_on = len(s) - tot_ze
		ze = on = 0
		mini = 10 ** 5
		for i in range(len(s)):
			on += int(s[i])
			ze += int(s[i]) ^ 1
			mini = min(mini, on + tot_ze - ze, ze + tot_on - on)
		print(mini)


def solve(s):
	ones = 0
	for c in s:
		ones += 1 if c == '1' else 0
	curr1 = 0
	curr0 = ones
	sol = curr0
	for i in range(len(s) - 1, -1, -1):
		if s[i] == '0':
			curr1 += 1
		else:
			curr0 -= 1
		sol = min(sol, curr0 + curr1)
	curr1 = len(s) - ones
	curr0 = 0
	sol = min(sol, curr1)
	for i in range(len(s) - 1, -1, -1):
		if s[i] == '1':
			curr0 += 1
		else:
			curr1 -= 1
		sol = min(sol, curr0 + curr1)
	return sol
t = int(input())
for _ in range(t):
	s = input()
	print(solve(s))


for _ in range(int(input())):
	s = input()
	o = s.count('0')
	e = s.count('1')
	to = 0
	te = 0
	ans = min([e, o])
	for i in range(len(s)):
		if s[i] == '0':
			to += 1
		else:
			te += 1
		ans = min(ans, to + e - te)
		ans = min(ans, te + o - to)
	print(ans)


from sys import stdin, stdout
int_in = lambda : int(stdin.readline())
arr_in = lambda : [int(x) for x in stdin.readline().split()]
mat_in = lambda rows: [arr_in() for y in range(rows)]
str_in = lambda : stdin.readline().strip()
out = lambda o: stdout.write('{}\n'.format(o))
arr_out = lambda o: out(' '.join(map(str, o)))
bool_out = lambda o: out('YES' if o else 'NO')
tests = lambda : range(1, int_in() + 1)
case_out = lambda i, o: out('Case #{}: {}'.format(i, o))

def solve(s):
	if len(s) < 3:
		return 0
	ones_left = 0
	ones_right = s.count(1)
	result = float('inf')
	for i in range(len(s)):
		result = min(result, i - ones_left + ones_right, ones_left + len(s) - i - ones_right)
		if s[i] == 1:
			ones_left += 1
			ones_right -= 1
	return result
for i in tests():
	s = [int(x) for x in str_in()]
	out(solve(s))


def f(border):
	if border == 0 or border == len(s):
		return min(ones[-1], zeros[-1])
	return min(ones[border - 1] + zeros[-1] - zeros[border - 1], zeros[border - 1] + ones[-1] - ones[border - 1])
for _ in range(int(input())):
	s = list(map(int, input()))
	answer = float('inf')
	ones = [int(s[0] == 1)]
	for i in range(1, len(s)):
		ones.append(ones[-1] + (s[i] == 1))
	zeros = [i + 1 - ones[i] for i in range(len(s))]
	print(min((f(i) for i in range(len(s) + 1))))


from sys import stdin, setrecursionlimit, stdout
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline().rstrip()

def lsi():
	return list(si())
res = ['YES', 'NO']
test_case = ii()
while test_case:
	test_case -= 1
	a = lsi()
	n = len(a)
	co = []
	cz = []
	o = z = 0
	for i in a:
		if int(i):
			o += 1
		else:
			z += 1
		co.append(o)
		cz.append(z)
	ans = min(o, z)
	x = 0
	for i in range(n):
		if a[i] == '0':
			x += 1
			chng = co[-1] - co[i]
			ans = min(ans, chng + x - 1)
	x = 0
	for i in range(n):
		if a[i] == '1':
			x += 1
			chng = cz[-1] - cz[i]
			ans = min(ans, chng + x - 1)
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	ones = []
	c = 0
	for s_i in s:
		if s_i == '1':
			c += 1
		ones.append(c)
	total_ones = c
	total_zeroes = len(s) - total_ones
	min_n = len(s)
	for i in range(len(s)):
		ones_before = ones[i]
		zeroes_before = i + 1 - ones_before
		ones_after = total_ones - ones[i]
		zeroes_after = len(s) - (i + 1) - ones_after
		min_n = min(min_n, zeroes_before + ones_after)
		min_n = min(min_n, ones_before + zeroes_after)
	print(min_n)


import getopt
import sys
import math
onlyCase = -1
verbose = False
try:
	(opts, args) = getopt.getopt(sys.argv[1:], 'vc:', ['case='])
except getopt.GetoptError as err:
	print(str(err))
	sys.exit(2)
for (o, a) in opts:
	if o in ('-c', '--case'):
		onlyCase = int(a)
	elif o == '-v':
		verbose = True
	else:
		sys.exit()
ncases = int(input())
for c in range(1, ncases + 1):
	B = list(map(int, input()))
	if onlyCase != -1 and onlyCase != c:
		continue
	if verbose:
		print(B)
	A = [[0, 0, 0, 0] for x in range(len(B))]
	for (i, e) in enumerate(B):
		if i == 0:
			A[i][e] = 1
		else:
			A[i][e] = A[i - 1][e] + 1
			A[i][1 - e] = A[i - 1][1 - e]
	k = len(B) - 1
	for (i, e) in enumerate(B[::-1]):
		if i == 0:
			A[k - i][e + 2] = 1
		else:
			A[k - i][e + 2] = A[k + 1 - i][e + 2] + 1
			A[k - i][3 - e] = A[k + 1 - i][3 - e]
	if verbose:
		print(A)
	best = k
	bestp = -1
	for (i, e) in enumerate(B):
		if i == 0 or B[i - 1] == B[i]:
			continue
		x = A[i][e] - 1 + A[i][3 - e]
		if x < best:
			best = x
			bestp = i
	if verbose:
		print('best=%d bestp=%d' % (best, bestp))
	simplebest = min(A[0][2], A[0][3])
	print(min(best, simplebest))


for i in range(int(input())):
	s = input()
	a = s.count('1')
	b = s.count('0')
	e = 1000
	for i in range(len(s)):
		q = s[i:].count('1') + (b - s[i:].count('0'))
		e = min(e, q)
		r = s[i:].count('0') + (a - s[i:].count('1'))
		e = min(e, r)
	print(e)


for _ in range(int(input())):
	s = input()
	num_ones = s.count('1')
	num_zeroes = s.count('0')
	cost = 1001
	done_zeroes = 0
	done_ones = 0
	for i in range(num_ones + num_zeroes):
		if s[i] == '0':
			done_zeroes += 1
		else:
			done_ones += 1
		cost = min(cost, done_zeroes + num_ones - done_ones, done_ones + num_zeroes - done_zeroes)
	print(cost)


t = int(input())
for i in range(t):
	s = input()
	n = len(s)
	ans = n
	cnt = 0
	a = []
	for i in range(n):
		if s[i] == '1':
			cnt += 1
		a.append(cnt)
	for i in range(n):
		ans = min(ans, a[i] + n - (a[-1] - a[i]) - i - 1)
		ans = min(ans, i + 1 - a[i] + (a[-1] - a[i]))
	print(ans)


import sys
import math
import heapq
import collections

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputmatrix(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d
t = int(input())
for q in range(t):
	s = inputstring()
	ones = 0
	n = len(s)
	cnt = [0] * n
	for i in range(n):
		if s[i] == '1':
			ones += 1
		cnt[i] = ones
	ans = 1000000000.0
	for i in range(n):
		cur1 = cnt[i] + (n - i - 1) - (ones - cnt[i])
		cur2 = i + 1 - cnt[i] + (ones - cnt[i])
		ans = min(ans, min(cur1, cur2))
	print(ans)


import bisect
import os
import gc
import sys
from io import BytesIO, IOBase
from collections import Counter
from collections import deque
import heapq
import math
import statistics

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

def sain():
	return input().split()

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	s = set()
	for p in range(2, n + 1):
		if prime[p]:
			s.add(p)
	return s

def readTree(n, m):
	adj = [deque([]) for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = ain()
		adj[u].append(v)
		adj[v].append(u)
	return adj

def main():
	for _ in range(iin()):
		s = sin()
		no = 0
		nz = 0
		for i in s:
			if i == '1':
				no += 1
			else:
				nz += 1
		prefz = 0
		prefo = 0
		ans = min(no, nz)
		for i in s:
			if i == '0':
				prefz += 1
				nz -= 1
			else:
				prefo += 1
				no -= 1
			ans = min(ans, prefz + no)
			ans = min(ans, prefo + nz)
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
if os.getcwd() == 'D:\\code':
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input())
ans = []
for x in range(n):
	c = 0
	s = input()
	count = len(s) + 1
	s0 = s.count('0')
	s1 = s.count('1')
	c0 = c1 = 0
	for i in range(len(s)):
		if s[i] == '0':
			c0 += 1
		elif s[i] == '1':
			c1 += 1
		count = min(count, c0 + s1 - c1)
		count = min(count, c1 + s0 - c0)
	ans.append(count)
for x in ans:
	print(x)


t = int(input())
for _ in range(t):
	s = input()
	zero = s.count('0')
	ones = s.count('1')
	l = zero + ones
	ans = 10 ** 9 + 7
	o = 0
	z = 0
	for i in range(l):
		if s[i] == '0':
			z += 1
		else:
			o += 1
		ans1 = o + zero - z
		ans2 = z + ones - o
		ans = min(ans1, ans2, ans)
	print(ans)


t = int(input())
for you in range(t):
	s = input()
	n = len(s)
	pref = [0 for i in range(n)]
	count = 0
	for i in range(n - 1, -1, -1):
		if s[i] == '1':
			count += 1
		pref[i] = count
	total = 0
	for i in range(n):
		if s[i] == '1':
			total += 1
	mina = []
	for i in range(n):
		mina.append(total - pref[i] + n - i - pref[i])
	for i in range(n):
		mina.append(i - total + pref[i] + pref[i])
	print(min(mina))


t = int(input())
for i in range(t):
	s = input()
	n = len(s)
	LD0 = [0] * n
	LD1 = [0] * n
	LG0 = [0] * n
	LG1 = [0] * n
	if s[0] == '0':
		LG0[0] = 0
		LG1[0] = 1
	if s[0] == '1':
		LG0[0] = 1
		LG1[0] = 0
	for i in range(1, n):
		if s[i] == '0':
			LG0[i] += LG0[i - 1]
			LG1[i] += LG1[i - 1] + 1
		if s[i] == '1':
			LG0[i] += LG0[i - 1] + 1
			LG1[i] += LG1[i - 1]
	if s[n - 1] == '0':
		LD0[n - 1] = 0
		LD1[n - 1] = 1
	if s[n - 1] == '1':
		LD0[n - 1] = 1
		LD1[n - 1] = 0
	for i in range(n - 2, -1, -1):
		if s[i] == '0':
			LD0[i] += LD0[i + 1]
			LD1[i] += LD1[i + 1] + 1
		if s[i] == '1':
			LD0[i] += LD0[i + 1] + 1
			LD1[i] += LD1[i + 1]
	minn = min(LG0[n - 1], LG1[n - 1])
	for i in range(n - 1):
		minn = min(minn, LG0[i] + LD1[i + 1])
		minn = min(minn, LG1[i] + LD0[i + 1])
	print(minn)


for _ in range(int(input())):
	n = input()
	a = len(n)
	one = 0
	zero = 0
	for i in range(a):
		if n[i] == '0':
			zero += 1
		else:
			one += 1
	ans = min(zero, one)
	z = 0
	o = 0
	for i in range(a):
		if n[i] == '0':
			z += 1
			zero -= 1
		else:
			o += 1
			one -= 1
		ans = min(ans, o + zero, one + z)
	print(ans)


def main():
	for _ in range(int(input())):
		(s, r) = (input(), 0)
		(n, zf) = (len(s), s.count('0'))
		(of, best) = (n - zf, n)
		ob = zb = 0
		for ch in s:
			if ch == '0':
				zf -= 1
				zb += 1
			else:
				of -= 1
				ob += 1
			best = min(best, zf + zb, zf + ob, of + zb, of + ob)
		print(best)
main()


import math
import time
from collections import defaultdict, deque
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	totalz = s.count('0')
	ans = math.inf
	currz = 0
	for i in range(n):
		if s[i] == '0':
			currz += 1
		temp = currz + (n - i - 1 - totalz + currz)
		temp2 = i + 1 - currz + (totalz - currz)
		ans = min(ans, temp, temp2)
	print(ans)


def fun(p, s):
	ans = 0
	for i in range(len(s)):
		if p[i] != s[i]:
			ans += 1
	return ans
t = int(input())
for _ in range(t):
	s = input()
	ones = 0
	zeroes = 0
	l = [(0, 0)]
	n = len(s)
	for i in range(n):
		if s[i] == '0':
			zeroes += 1
		else:
			ones += 1
		l.append((zeroes, ones))
	ans = 1000000000000000000
	for i in range(0, n + 1):
		ans = min(ans, l[i][0] + ones - l[i][1])
		ans = min(ans, l[i][1] + zeroes - l[i][0])
	print(ans)


def sol(arr):
	n = len(arr)
	rec = 10 ** 10
	pref = [0] * (n + 1)
	all = sum(arr)
	for i in range(1, n + 1):
		pref[i] = pref[i - 1] + arr[i - 1]
	for mb in range(len(arr) + 1):
		rec = min(rec, pref[mb] + (n - mb - all + pref[mb]))
	return rec
for i in range(int(input())):
	arr = [int(s) for s in input()]
	print(min(sol(arr), sol(arr[::-1])))


from sys import stdin, stdout
from math import *
from heapq import *
from collections import *

def main():
	ntest = int(stdin.readline())
	for testcase in range(ntest):
		s = stdin.readline().strip()
		a = [int(c) for c in s]
		n = len(a)
		res = n
		sm = [0]
		for i in range(n):
			sm.append(sm[-1] + a[i])
		for i in range(1, n + 1):
			l0 = i - sm[i]
			l1 = sm[i]
			r1 = sm[n] - sm[i]
			r0 = n - i - r1
			res = min(res, min(l0, l1) + min(r0, r1))
		stdout.write('%d\n' % res)
	return 0
main()


import math
m = 1000000007

def fact(n):
	ans = 1
	for i in range(1, n + 1):
		ans = ans % m * (i % m) % m
	return ans

def power_2(n):
	ans = 1
	for i in range(n):
		ans = ans % m * 2 % m
	return ans
for z in range(int(input())):
	s = input()
	n = len(s)
	(i, cnt1, cnt2) = (n - 1, 0, 0)
	(dic1, dic2) = ({}, {})
	dic1[n] = 0
	dic2[n] = 0
	while i >= 0:
		if s[i] == '1':
			cnt1 += 1
		else:
			cnt2 += 1
		dic1[i] = cnt1
		dic2[i] = cnt2
		i -= 1
	ans = []
	cnt = 0
	for i in range(0, n):
		if s[i] == '1':
			cnt += 1
		ans.append(n - i - 1 - dic1[i + 1] + cnt)
	cnt = 0
	for i in range(0, n):
		if s[i] == '0':
			cnt += 1
		ans.append(n - i - 1 - dic2[i + 1] + cnt)
	print(min(ans))


for _ in range(int(input())):
	s = input()
	n = len(s)
	s0 = s.count('0')
	s1 = s.count('1')
	ans = min(s0, s1)
	p0 = p1 = 0
	for i in range(n):
		if s[i] == '0':
			p0 += 1
			s0 -= 1
		if s[i] == '1':
			p1 += 1
			s1 -= 1
		ans = min(ans, p0 + s1, p1 + s0)
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	zeros = 0
	ones = 0
	temp_zeros = 0
	temp_ones = 0
	for i in range(len(s)):
		char = s[i]
		if char == '0':
			zeros += 1
		else:
			ones += 1
	res = min(zeros, ones)
	for i in range(len(s)):
		if s[i] == '0':
			temp_zeros += 1
		else:
			temp_ones += 1
		res = min(res, temp_ones + (zeros - temp_zeros))
		res = min(res, temp_zeros + (ones - temp_ones))
	print(res)


t = int(input())
for _ in range(t):
	s = input()
	(l0, l1, r0, r1) = ([0] * len(s), [0] * len(s), [0] * len(s), [0] * len(s))
	for i in range(len(s)):
		if s[i] == '0':
			l0[i] = l0[i - 1]
			l1[i] = l1[i - 1] + 1
		else:
			l0[i] = l0[i - 1] + 1
			l1[i] = l1[i - 1]
	for i in range(len(s) - 1, -1, -1):
		if i == len(s) - 1:
			if s[i] == '0':
				r0[i] = 0
				r1[i] = 1
			else:
				r0[i] = 1
				r1[i] = 0
			continue
		if s[i] == '0':
			r0[i] = r0[i + 1]
			r1[i] = r1[i + 1] + 1
		else:
			r0[i] = r0[i + 1] + 1
			r1[i] = r1[i + 1]
	ans = min(r0[0], r1[0], l0[-1], l1[-1])
	for i in range(len(s) - 1):
		ans = min(ans, l0[i] + r1[i + 1])
		ans = min(ans, l1[i] + r0[i + 1])
	print(ans)


for i in range(int(input())):
	s = input()
	n = len(s)
	m = min(n - s.count('0'), s.count('0'))
	v1 = 0
	v2 = 0
	m1 = n
	m2 = n
	for i in range(n):
		if s[i] == '1':
			v2 += 1
		else:
			v1 += 1
		m1 = min(m1, v1 + s[i + 1:].count('1'))
		m2 = min(m2, v2 + s[i + 1:].count('0'))
	print(min(m, m1, m2))


for ttt in range(int(input())):
	s = input()
	zero = s.count('0')
	one = s.count('1')
	ans = min(zero, one)
	zz = 0
	oo = 0
	n = len(s)
	for i in range(n):
		if s[i] == '0':
			zz += 1
			zero -= 1
		if s[i] == '1':
			oo += 1
			one -= 1
		ans = min(ans, oo + zero)
		ans = min(ans, zz + one)
	print(ans)


def solve():
	s = input()
	n = len(s)
	if n == 1:
		print(0)
		return
	found = False
	(m1, m2) = (9999999999999, 9999999999999999999)
	pref = [0] * n
	for i in range(n):
		if s[i] == '1':
			pref[i] = 1
	for i in range(1, n):
		pref[i] = pref[i - 1] + pref[i]
	for i in range(n):
		if s[i] == '1':
			found = True
			if i == 0:
				m1 = min(m1, n - pref[n - 1])
				m2 = min(m2, pref[n - 1] - 1)
			elif i == n - 1:
				m1 = min(m1, pref[n - 2])
				m2 = min(m2, n - pref[n - 1])
			else:
				m1 = min(m1, pref[i - 1] + (n - i - 1 - pref[n - 1] + pref[i]))
				m2 = min(m2, pref[n - 1] - pref[i] + (i - pref[i - 1]))
	if found == False:
		(m1, m2) = (0, 0)
	m = min(m1, m2)
	m = min(m, pref[n - 1])
	print(m)
t = int(input())
for i in range(t):
	solve()


list_ans = []
for _ in range(int(input())):
	s = input()
	z = s.count('0')
	o = s.count('1')
	zeros = ones = 0
	ans = 10000
	for i in range(len(s)):
		if s[i] == '1':
			ones += 1
		else:
			zeros += 1
		ans = min(ans, ones + z - zeros, zeros + o - ones)
	list_ans.append(ans)
for item in list_ans:
	print(item)


for _ in range(int(input())):
	s = input()
	n = len(s)
	zeroes_count = []
	if s[0] == '0':
		zeroes_count.append(1)
	else:
		zeroes_count.append(0)
	for i in range(1, n):
		if s[i] == '0':
			zeroes_count.append(zeroes_count[-1] + 1)
		else:
			zeroes_count.append(zeroes_count[-1])
	min_swaps = float('inf')
	for i in range(n):
		min_swaps = min(min_swaps, i - zeroes_count[i] + 1 + (zeroes_count[-1] - zeroes_count[i]), zeroes_count[i] + (n - i - 1 - zeroes_count[-1] + zeroes_count[i]))
	print(min_swaps)


import sys
input = sys.stdin.readline

def intInput():
	return int(input())

def intListInput():
	return list(map(int, input().split()))

def strInput():
	return input()
from itertools import groupby
import itertools

def isGood(s):
	groups = [(k, len(list(g))) for (k, g) in itertools.groupby(s)]
	return len(groups) <= 2

def brute(s):
	n = len(s)
	binStrs = [''.join(p) for p in itertools.product('10', repeat=n)]
	result = float('inf')
	for binStr in binStrs:
		if isGood(binStr):
			numDiffs = sum((1 for (a, b) in zip(binStr, s) if a != b))
			result = min(result, numDiffs)
	return result
from itertools import groupby

def soln(s):
	numZeros = [0]
	numOnes = [0]
	for (i, ch) in enumerate(s):
		currZeros = numZeros[-1]
		if ch == '0':
			currZeros += 1
		numZeros.append(currZeros)
		currOnes = numOnes[-1]
		if ch == '1':
			currOnes += 1
		numOnes.append(currOnes)
	result = float('inf')
	for i in range(len(s)):
		firstNeeded = numZeros[i] + (numOnes[-1] - numOnes[i])
		secondNeeded = numOnes[i] + (numZeros[-1] - numZeros[i])
		result = min(result, firstNeeded, secondNeeded)
	return result

def main():
	t = intInput()
	for _ in range(t):
		s = strInput()
		result = soln(s)
		print(result)

def randomTest():
	import random
	T = 100
	for i in range(1, T + 1):
		n = random.randint(1, 18)
		binStrs = [''.join(p) for p in itertools.product('10', repeat=n)]
		s = random.choice(binStrs)
		b = brute(s)
		f = soln(s)
		try:
			assert b == f
		except:
			print('Broken: ')
			print(b, f)
			return
main()


t = int(input())

def f(s):
	onesl = 0
	onesr = s.count('1')
	l = len(s)
	mi = l - onesr
	for i in range(l):
		if s[i] == '1':
			onesr -= 1
			onesl += 1
		c = onesl + (l - i - 1 - onesr)
		mi = min(c, mi)
	return mi
for _ in range(t):
	s = input()
	s2 = ''.join(('1' if c == '0' else '0' for c in s))
	print(min(f(s), f(s2)))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	s = input().strip()
	r = [[0, 0] for _ in range(len(s) + 1)]
	if len(s) < 3:
		print(0)
		continue
	for (i, c) in enumerate(s):
		if c == '0':
			r[i][0] = r[i - 1][0] + 1
			r[i][1] = r[i - 1][1]
		else:
			r[i][1] = r[i - 1][1] + 1
			r[i][0] = r[i - 1][0]
	r.pop()
	ans = len(s)
	for (i, v) in enumerate(r):
		v1 = [0, 0]
		v1[0] = r[-1][0] - v[0]
		v1[1] = r[-1][1] - v[1]
		ans = min(v[0] + v1[1], v[1] + v1[0], ans)
	print(ans)


import os
import sys
from io import BytesIO, IOBase
import math

def inputIntArray():
	return list(map(int, input().rstrip().split()))

def inputArray():
	return input().rstrip().split()

def inputVars():
	return map(int, input().rstrip().split())

def inputNum():
	return int(input())

def main():
	for _ in range(inputNum()):
		binNum = input()
		total1 = 0
		total0 = 0
		cost = float('inf')
		for num in binNum:
			if num == '1':
				total1 += 1
			else:
				total0 += 1
		curr0 = 0
		curr1 = 0
		for pos in range(1, len(binNum) + 1):
			if binNum[pos - 1] == '0':
				curr0 += 1
			else:
				curr1 += 1
			cost = min(cost, curr0 + total1 - curr1, curr1 + total0 - curr0)
		print(cost)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	res = 10000
	for i in range(n + 1):
		p = s[:i]
		q = s[i:]
		res = min(res, p.count('0') + q.count('1'))
		res = min(res, p.count('1') + q.count('0'))
	print(res)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans

def sieve_of_eratosthenes(n):
	ans = []
	arr = [1] * (n + 1)
	(arr[0], arr[1], i) = (0, 0, 2)
	while i * i <= n:
		if arr[i] == 1:
			j = i + i
			while j <= n:
				arr[j] = 0
				j += i
		i += 1
	for k in range(n):
		if arr[k] == 1:
			ans.append(k)
	return ans
from math import *
for _ in range(int(input())):
	string = input()
	n = len(string)
	(c1, c0) = (0, 0)
	for i in range(n):
		c1 += string[i] == '1'
		c0 += string[i] == '0'
	ans = min(c1, c0)
	(c1e, c0e) = (0, 0)
	for i in range(n):
		c1e += string[i] == '1'
		c0e += string[i] == '0'
		cnt1 = c0e + c1 - c1e
		cnt2 = c1e + c0 - c0e
		ans = min(ans, cnt2, cnt1)
	print(ans)


import math
t = int(input())
for _ in range(t):
	s = input()
	num_zero = 0
	num_one = 0
	for i in range(len(s)):
		if s[i] == '0':
			num_zero += 1
		else:
			num_one += 1
	done_zero = 0
	done_one = 0
	ans = len(s)
	for i in range(len(s)):
		if s[i] == '0':
			done_zero += 1
		else:
			done_one += 1
		ans = min(ans, num_zero + done_one - done_zero)
		ans = min(ans, num_one + done_zero - done_one)
	print(ans)


t = int(input())
for _ in range(t):
	data = list(input())
	zeros = data.count('0')
	ones = data.count('1')
	result = 10000
	(a, b) = (0, 0)
	for i in range(len(data)):
		if data[i] == '0':
			a += 1
		elif data[i] == '1':
			b += 1
		result = min(result, a + ones - b, b + zeros - a)
	print(result)


for _ in range(int(input())):
	a = list(map(int, input()))
	o = sum(a)
	x = 0
	n = len(a)
	ans = min(o, n - o)
	for i in range(n):
		x += a[i]
		t = x + (n - i - 1 - (o - x))
		ans = min(ans, t, n - t)
	print(ans)


n = int(input())
for i in range(n):
	j = input()
	if list(j) == sorted(j) or list(j) == sorted(j)[::-1]:
		print(0)
	else:
		(x, y) = (j.count('1'), j.count('0'))
		(a, b, c, d) = (0, y, 0, x)
		new = [y, x]
		for k in j:
			if k == '1':
				a += 1
				d -= 1
			else:
				b -= 1
				c += 1
			new.extend([a + b, c + d])
		print(min(new))


def solution():
	for _ in range(int(input())):
		s = input()
		ans = 2000000000.0
		cnt0 = cnt1 = d0 = d1 = 0
		n = len(s)
		for i in range(n):
			if s[i] == '0':
				cnt0 += 1
			else:
				cnt1 += 1
		for i in range(n):
			if s[i] == '0':
				d0 += 1
			else:
				d1 += 1
			x = d0 + cnt1 - d1
			y = d1 + cnt0 - d0
			z = min(x, y)
			ans = min(z, ans)
		print(ans)
solution()


n = int(input())

def retrans(text):
	new_text = ''
	for k in text:
		if k == '0':
			new_text += '1'
		else:
			new_text += '0'
	return new_text
for _ in range(n):
	(a, b) = ([], [])
	text = input()
	if text[0] == '1':
		text = retrans(text)
	if text[0] == '0':
		a.append(1)
		b.append(0)
	else:
		a.append(0)
		b.append(1)
	for x in range(1, len(text)):
		if text[x] == '0':
			a.append(a[x - 1] + 1)
			b.append(b[x - 1])
		else:
			a.append(a[x - 1])
			b.append(b[x - 1] + 1)
	x = len(text)
	(i, j) = (0, 0)
	k = x - 1
	while k > 0:
		if text[k] == '0':
			i += 1
		else:
			break
		k -= 1
	k = x - 1
	while k > 0:
		if text[k] == '1':
			j += 1
		else:
			break
		k -= 1
	res = 9999999999
	if i == a[x - 1] or j == b[x - 1]:
		res = 0
	else:
		for t in range(x):
			if text[t] == '1':
				all_ones_to_zeros = b[x - 1] - j
				all_zeros_to_ones = a[x - 1] - i
				all_left_ones_to_zeros_all_right_zeros_to_ones = b[t - 1] + a[x - 1] - a[t]
				all_left_zeros_to_ones_all_right_ones_to_zeros = a[t - 1] + b[x - 1] - b[t]
				res = min(res, all_ones_to_zeros, all_zeros_to_ones, all_left_ones_to_zeros_all_right_zeros_to_ones, all_left_zeros_to_ones_all_right_ones_to_zeros)
	print(res)


T = int(input())
for i in range(T):
	s = list(input())
	count_1 = s.count('1')
	count_0 = s.count('0')
	count = 1001
	upto_ones = 0
	upto_zeros = 0
	for i in s:
		if i == '0':
			upto_zeros += 1
		else:
			upto_ones += 1
		count = min(count, count_1 + upto_zeros - upto_ones, count_0 + upto_ones - upto_zeros)
	print(count)


def read_list():
	return list(map(int, input().split(' ')))

def print_list(l):
	print(' '.join(map(str, l)))
N = int(input())
for _ in range(N):
	s = input()
	ls = len(s)
	c0 = s.count('0')
	c1 = ls - c0
	res = min(c1, c0)
	if res == 0:
		print(0)
	else:
		(n0, n1) = (0, 0)
		for i in range(ls):
			if s[i] == '0':
				n0 += 1
			else:
				n1 += 1
			res = min(res, n1 + c0 - n0, n0 + c1 - n1)
		print(res)


for _ in range(int(input())):
	s = list(map(int, list(input())))
	best = 10001
	dp = [0]
	for v in s:
		dp.append(dp[-1] + int(v == 1))
	for (i, v) in enumerate(s):
		i += 1
		lone = dp[i]
		lzero = i - dp[i]
		rone = dp[-1] - dp[i]
		rzero = len(s) - i - rone
		bleft = min(lone, lzero)
		bright = min(rone, rzero)
		best = min(best, bleft + bright)
	print(best)


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
from queue import PriorityQueue
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
import sys
for i in range(10 ** 5):
	pass
t = int(input())
for _ in range(t):
	cnt = 0
	summ = 0
	s = str(input())
	n = len(s)
	for i in s:
		if i == '0':
			cnt += 1
		else:
			summ += 1
	ans = min(n - cnt, n - summ)
	(o, e) = (0, 0)
	for i in s:
		if i == '0':
			o += 1
		else:
			e += 1
		ans = min(ans, o + summ - e)
	(o, e) = (0, 0)
	for i in s:
		if i == '0':
			o += 1
		else:
			e += 1
		ans = min(ans, e + cnt - o)
	print(ans)


t = int(input())
for i in range(0, t):
	s = input()
	if len(s) <= 2:
		print(0)
	else:
		n = len(s)
		p = s[0]
		c1 = 0
		c2 = 0
		l1 = []
		l2 = []
		for j in range(0, n):
			l1.append(0)
			l2.append(0)
		for j in range(0, n):
			if s[j] == '0':
				c1 += 1
			else:
				c2 += 1
			l1[j] = c1
			l2[j] = c2
		ans = min(c1, c2)
		if ans != 0:
			for j in range(1, n):
				c1 = 0
				c2 = 0
				c1 += l2[j - 1]
				c1 += l1[n - 1] - l1[j - 1]
				c2 += l1[j - 1]
				c2 += l2[n - 1] - l2[j - 1]
				ans = min(ans, c1, c2)
				if ans == 0:
					break
		print(ans)


t = int(input())
for cases in range(t):
	s = input()
	if len(s) < 3:
		print(0)
	elif len(s) == 3:
		print(1) if s == '101' or s == '010' else print(0)
	else:
		zeros = 0
		ones = 0
		for i in s:
			if i == '0':
				zeros += 1
			else:
				ones += 1
		count = 1001
		z = 0
		o = 0
		for i in s:
			if i == '0':
				z += 1
			else:
				o += 1
			count = min(min(z + ones - o, o + zeros - z), count)
		print(count)


rr = lambda : input().strip()
T = int(rr())
for _ in range(T):
	s = rr()
	s = list(s)
	n = len(s)
	all0 = all1 = 0
	for i in range(n):
		all0 += s[i] == '0'
		all1 += s[i] == '1'
	ans = min(all1, all0)
	done1 = done0 = 0
	for i in s:
		if i == '1':
			done1 += 1
			all1 -= 1
		else:
			done0 += 1
			all0 -= 1
		ans = min(ans, done1 + all0)
		ans = min(ans, done0 + all1)
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	zero = s.count('0')
	one = s.count('1')
	(z, o) = (0, 0)
	ans = len(s)
	for c in s:
		if c == '0':
			z += 1
		else:
			o += 1
		ans = min(ans, z + (one - o))
		ans = min(ans, o + (zero - z))
	print(ans)


from sys import stdin, stdout
from collections import Counter
read = stdin.readline

def puts(x, end='\n'):
	stdout.write(x + end)

def read_int():
	return int(read())

def list_in():
	return [i for i in read().split()]

def int_list_in():
	return [int(i) for i in read().split()]

def solve():
	s = read().strip()
	min_change = 10000
	cnt1 = {'0': 0, '1': 0}
	cnt2 = Counter(s)
	for i in range(len(s)):
		if i != 0:
			if s[i - 1] == '1':
				cnt2['1'] -= 1
				cnt1['1'] += 1
			else:
				cnt2['0'] -= 1
				cnt1['0'] += 1
		change = min(cnt1['0'], cnt1['1']) + min(cnt2['1'], cnt2['0'])
		if change < min_change:
			min_change = change
	print(min_change)
for i in range(read_int()):
	solve()


from sys import stdin
from math import ceil
for _ in range(int(stdin.readline())):
	s = stdin.readline().strip()
	m = s.count('0')
	ans = m
	(z, o) = (0, 0)
	for i in range(len(s)):
		if s[i] != '0':
			o += 1
		else:
			z += 1
		k = o + m - z
		ans = min(ans, k)
	m = s.count('1')
	ans1 = m
	(z, o) = (0, 0)
	for i in range(len(s)):
		if s[i] != '0':
			o += 1
		else:
			z += 1
		k = z + m - o
		ans1 = min(ans1, k)
	print(min(ans, ans1))


import sys
import math
t = int(input())
while t:
	s = sys.stdin.readline().strip()
	num_ones = 0
	num_zeros = 0
	for e in s:
		if e == '0':
			num_zeros += 1
		else:
			num_ones += 1
	ans = math.inf
	done_zeros = 0
	done_ones = 0
	for e in s:
		if e == '0':
			done_zeros += 1
		else:
			done_ones += 1
		ans = min(ans, done_zeros + num_ones - done_ones, done_ones + num_zeros - done_zeros)
	print(ans)
	t -= 1


def f(s: str) -> int:
	acc = [0]
	for ch in s:
		if ch == '1':
			acc.append(acc[-1] + 1)
		else:
			acc.append(acc[-1])
	best = n = len(s)
	for i in range(1, n + 1):
		best = min(best, i + acc[n] - 2 * acc[i])
		best = min(best, 2 * acc[i] + n - i - acc[n])
	return best
n = int(input())
for i in range(n):
	print(f(input()))


import sys

def solve(s):
	one_prefix = [0 if s[0] == '1' else 1]
	zero_prefix = [0 if s[0] == '0' else 1]
	for i in range(1, len(s)):
		char = s[i]
		if char == '1':
			one_prefix.append(one_prefix[-1])
			zero_prefix.append(zero_prefix[-1] + 1)
		else:
			one_prefix.append(one_prefix[-1] + 1)
			zero_prefix.append(zero_prefix[-1])
	zero_first = []
	one_first = []
	for i in range(len(s)):
		zero_score = zero_prefix[i]
		one_score = one_prefix[-1] - one_prefix[i]
		zero_first.append(zero_prefix[i] + one_prefix[-1] - one_prefix[i])
		one_first.append(one_prefix[i] + zero_prefix[-1] - zero_prefix[i])
	return min(min(zero_first), min(one_first), one_prefix[-1], zero_prefix[-1])
t = int(input())
for _ in range(t):
	s = input()
	print(solve(s))


import sys
import bisect

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def MI():
	return map(int, sys.stdin.readline().split())

def SI():
	return sys.stdin.readline().strip()
t = int(input())
for q in range(t):
	a = input()
	z = []
	o = []
	zc = 0
	oc = 0
	for i in a:
		if i == '1':
			oc += 1
		else:
			zc += 1
		z.append(zc)
		o.append(oc)
	mi = a[1:len(a) - 1].count('1')
	if a[0] == a[-1] == '1':
		mi += 1
	mi = min(mi, z[-1], o[-1])
	for i in range(len(a)):
		difz = [z[-1] - z[i], z[i]]
		difo = [o[-1] - o[i], o[i]]
		mi = min(mi, difz[0] + difo[1], difz[1] + difo[0])
	print(mi)


t = int(input())
while t:
	t -= 1
	s = input()
	(ones, zeros) = (0, 0)
	for val in s:
		if val is '1':
			ones += 1
		else:
			zeros += 1
	(ones_seen, zeros_seen) = (0, 0)
	ans = len(s)
	for val in s:
		if val is '1':
			ones_seen += 1
		else:
			zeros_seen += 1
		ones_left = ones - ones_seen
		zeros_left = zeros - zeros_seen
		ans = min(ans, zeros_left + ones_seen)
		ans = min(ans, zeros_seen + ones_left)
	print(ans)


from collections import Counter, defaultdict, deque
from math import *
import sys
input = sys.stdin.readline

def solve():
	a = input().strip()
	n = len(a)
	pref = []
	s = 0
	for el in a:
		s += int(el)
		pref.append(s)
	ans = min(n - pref[-1], pref[-1])
	for i in range(n - 1):
		ans = min(ans, pref[i] + (n - i - 1 - (pref[-1] - pref[i + 1] + 1)), i + 1 - pref[i] + (pref[-1] - pref[i + 1]))
	print(ans)
tt = int(input())
for test in range(tt):
	solve()


for _ in range(int(input())):
	s = input()
	n = len(s)
	ones = s.count('1')
	zeros = s.count('0')
	o = [0] * n
	z = [0] * n
	if s[0] == '1':
		o[0] += 1
	else:
		z[0] += 1
	for i in range(1, n):
		o[i] = o[i - 1]
		z[i] = z[i - 1]
		if s[i] == '1':
			o[i] += 1
		else:
			z[i] += 1
	ans = float('inf')
	for i in range(n):
		if s[i] == '0':
			temp = z[i] - 1
			temp2 = ones - o[i]
			ans = min(ans, temp + temp2)
			temp = zeros - z[i]
			temp2 = o[i]
			ans = min(ans, temp + temp2)
		else:
			temp = o[i] - 1
			temp2 = zeros - z[i]
			ans = min(ans, temp + temp2)
			temp = ones - o[i]
			temp2 = z[i]
			ans = min(ans, temp + temp2)
	print(ans)


t = int(input())
while t:
	t = t - 1
	s = input()
	p = []
	n = 0
	m = 0
	for i in s:
		if i == '1':
			n += 1
		else:
			m += 1
		p.append([n, m])
	out = len(s) + 1
	for i in range(len(s)):
		x = p[i][0]
		y = p[i][1]
		out = min(out, x + (m - y))
		out = min(out, y + (n - x))
		out = min(out, y + (m - y))
		out = min(out, x + (n - x))
	print(out)


def solve():
	s = input()
	n = len(s)
	zeros = s.count('0')
	ones = s.count('1')
	result = n
	tmp = 0
	for i in range(n):
		if s[i] == '0':
			tmp += 1
		result = min(result, tmp + ones - (i + 1 - tmp), i + 1 - tmp + zeros - tmp)
	print(result)
for _ in range(int(input())):
	solve()


t = int(input())
chars = ['0', '1']
for _ in range(t):
	s = input()
	cur = 0
	i = 0
	score = 0
	lst = []
	while i < len(s):
		if s[i] == chars[cur]:
			score += 1
		else:
			lst.append(score)
			cur = 1 - cur
			score = 1
		i += 1
	lst.append(score)
	if len(lst) <= 2 or (len(lst) == 3 and lst[0] == 0):
		print(0)
		continue
	for i in range(2, len(lst)):
		lst[i] = lst[i] + lst[i - 2]
	if len(lst) % 2 == 0:
		sums = [lst[-2], lst[-1]]
	else:
		sums = [lst[-1], lst[-2]]
	mx = max(sums)
	for i in range(len(lst)):
		left = lst[i]
		if i > 0:
			right = sums[(i + 1) % 2] - lst[i - 1]
		else:
			right = sums[1]
		if left + right > mx:
			mx = left + right
	print(len(s) - mx)


import sys
from collections import defaultdict
input = sys.stdin.readline

def print_result(l):
	return print(' '.join(map(str, l)))

def timer(function):
	import time

	def wrapper(*args, **kwargs):
		t1 = time.time()
		val = function()
		t2 = time.time()
		print('Executed \\{}\\ in {:.2f} seconds.'.format(function.__name__, t2 - t1))
		return val
	return wrapper

def solve():
	s = input().split('\n')[0]
	(pz, po) = (0, 0)
	(prefix_zero, prefix_one) = ([], [])
	for i in range(len(s)):
		if s[i] == '1':
			pz += 1
		else:
			po += 1
		prefix_zero.append(po)
		prefix_one.append(pz)
	res = min(prefix_zero[len(s) - 1], prefix_one[len(s) - 1])
	for i in range(len(s)):
		for j in range(i):
			res = min(prefix_zero[j] + (prefix_one[len(s) - 1] - prefix_one[j]), prefix_one[j] + (prefix_zero[len(s) - 1] - prefix_zero[j]), res)
	print(res)
t = int(input())
for _ in range(t):
	solve()


import os
import sys
if os.path.exists('/mnt/c/Users/Square/square/codeforces'):
	f = iter(open('B.txt').readlines())

	def input():
		return next(f).strip()
else:
	input = lambda : sys.stdin.readline().strip()
fprint = lambda *args: print(*args, flush=True)
import itertools
t = int(input())
for _ in range(t):
	s = input()
	ones = [0]
	for i in s:
		if i == '1':
			ones.append(ones[-1] + 1)
		else:
			ones.append(ones[-1])
	zeros = [0]
	for i in reversed(s):
		if i == '0':
			zeros.append(zeros[-1] + 1)
		else:
			zeros.append(zeros[-1])
	zeros = list(reversed(zeros))
	n = len(s)
	res = []
	for (k, (l, r)) in enumerate(zip(ones, zeros)):
		res.append(min(l + r, n - l - r))
	print(min(res))


for _ in range(int(input())):
	s = input()
	(a, b) = ([0], [0])
	for v in s:
		if v == '0':
			a.append(a[-1] + 1)
			b.append(b[-1])
		else:
			a.append(a[-1])
			b.append(b[-1] + 1)
	(aa, bb) = (a[-1], b[-1])
	r = aa
	for (u, v) in zip(a, b):
		r = min(r, u + (bb - v), aa - u + v)
	print(r)


import math
for _ in range(int(input())):
	s = list(input())
	(c0, c1, n) = (s.count('0'), s.count('1'), len(s))
	(zero, one) = (c0, 0)
	mn = 1000000000.0
	for i in range(n):
		if s[n - i - 1] == '0':
			zero -= 1
		else:
			mn = min(mn, zero + one)
			one += 1
	(zero, one) = (0, c1)
	for i in range(n):
		if s[n - i - 1] == '1':
			one -= 1
		else:
			mn = min(zero + one, mn)
			zero += 1
	print(mn)


from collections import Counter
T = int(input())
for t in range(T):
	s = input()
	n = len(s)
	c = Counter(s)
	b0 = 0
	b1 = 0
	a0 = c.get('0') if c.get('0') else 0
	a1 = c.get('1') if c.get('1') else 0
	all0 = a1
	all1 = a0
	ans = 100000
	ans = min(ans, all0, all1)
	for i in range(n):
		if s[i] == '0':
			b0 += 1
			a0 -= 1
			all0 = b0 + a1
			all1 = b1 + a0
		elif s[i] == '1':
			b1 += 1
			a1 -= 1
			all0 = b0 + a1
			all1 = b1 + a0
		ans = min(ans, all0, all1)
	print(ans)


import sys
input = sys.stdin.readline
from collections import *
t = int(input())
for _ in range(t):
	s = input()[:-1]
	n = len(s)
	acc = [0]
	for si in s:
		acc.append(acc[-1] + (1 if si == '0' else 0))
	ans = 10 ** 18
	for i in range(n + 1):
		ans = min(ans, i - acc[i] + acc[n] - acc[i])
		ans = min(ans, acc[i] + n - i - (acc[n] - acc[i]))
	print(ans)


for _ in range(int(input())):
	A = str(input())
	x = 0
	for i in range(len(A)):
		if A[i] == '1':
			x += 1
	minn = x
	xx = x
	for i in range(len(A)):
		if A[i] == '1':
			xx -= 1
		else:
			xx += 1
		minn = min(minn, xx)
	xx = len(A) - x
	minn = min(minn, len(A) - x)
	for i in range(len(A)):
		if A[i] == '0':
			xx -= 1
		else:
			xx += 1
		minn = min(minn, xx)
	print(minn)


for _ in range(int(input())):
	s = list(input())
	prefixZero = [0 for i in range(len(s))]
	for i in range(len(s)):
		prefixZero[i] = prefixZero[i - 1]
		if s[i] == '0':
			prefixZero[i] += 1
	prefixOne = [i + 1 - prefixZero[i] for i in range(len(s))]
	ans = len(s)
	for i in range(len(s)):
		ans = min(ans, prefixZero[i] + prefixOne[-1] - prefixOne[i])
		ans = min(ans, prefixOne[i] + prefixZero[-1] - prefixZero[i])
	print(ans)


for T in range(int(input())):
	s = input()
	n = len(s)
	l = [0] * n
	if s[0] == '1':
		l[0] = 1
	for i in range(1, n):
		if s[i] == '1':
			l[i] = l[i - 1] + 1
		else:
			l[i] = l[i - 1]
	r = [0] * n
	if s[n - 1] == '1':
		r[n - 1] = 1
	for i in range(n - 2, -1, -1):
		if s[i] == '1':
			r[i] = r[i + 1] + 1
		else:
			r[i] = r[i + 1]
	ans = 1000000000.0
	for i in range(n):
		ans = min(ans, i + 1 - l[i] + r[i] - 1)
	for i in range(n):
		ans = min(ans, l[i] + n - i - r[i] - 1)
	print(ans)


import math
from collections import defaultdict
t = int(input())
for _ in range(t):
	s = input()
	ones = zeros = 0
	ans = 100000
	pre_arr = [0] * (len(s) + 1)
	p_arr = [0] * (len(s) + 1)
	for i in range(len(s) - 1, -1, -1):
		if s[i] == '0':
			pre_arr[i] = pre_arr[i + 1] + 1
			p_arr[i] = p_arr[i + 1]
		else:
			pre_arr[i] = pre_arr[i + 1]
			p_arr[i] = p_arr[i + 1] + 1
	for i in range(len(s)):
		if s[i] == '0':
			zeros += 1
		else:
			ones += 1
		ans = min(ans, ones + pre_arr[i + 1])
	ones = zeros = 0
	for i in range(len(s)):
		if s[i] == '1':
			ones += 1
		else:
			zeros += 1
		ans = min(ans, zeros + p_arr[i + 1])
	print(ans)


for lo in range(int(input())):
	st = input()
	n = len(st)
	ls = [0 for i in range(n)]
	if st[0] == '1':
		ls[0] = 1
	for i in range(1, n):
		c = 0
		if st[i] == '1':
			c = 1
		ls[i] = ls[i - 1] + c
	k = ls[n - 1]
	mn = min(k, n - k)
	for i in range(n):
		x = k - ls[i]
		y = min(ls[i] + (n - i - 1 - x), i + 1 - ls[i] + x)
		mn = min(mn, y)
	print(mn)


def solve(s):
	c = [0] * (len(s) + 1)
	for i in range(len(s)):
		c[i + 1] = c[i] + int(s[i])
	best = float('inf')
	for i in range(len(s)):
		best = min(best, c[i] + len(s) - i - (c[-1] - c[i]))
	return best
for _ in range(int(input())):
	s = input()
	s2 = [None] * len(s)
	for i in range(len(s)):
		if s[i] == '0':
			s2[i] = '1'
		else:
			s2[i] = '0'
	print(min(solve(s), solve(s2)))


for T in range(int(input())):
	t = input()
	n = len(t)
	count = 0
	for i in range(n):
		if t[i] == '1':
			count += 1
	ans = min(n, count)
	ans = min(ans, n - count)
	val = 0
	for i in range(n):
		if t[i] == '1':
			val += 1
		ans = min(ans, val + (n - i - 1 - (count - val)))
		ans = min(ans, i + 1 - val + count - val)
	print(ans)


for _ in range(int(input())):
	s = input()
	zeros = s.count('0')
	ones = len(s) - zeros
	left0 = 0
	best = min(zeros, ones)
	for i in range(len(s)):
		if s[i] == '0':
			left0 += 1
		left1 = i - left0 + 1
		best = min(best, min(left1 + (zeros - left0), left0 + (ones - left1)))
	print(best)


for _ in range(int(input())):
	s = input()
	i = 0
	m = len(s)
	while i < len(s):
		if s[i] == '1':
			j = i + 1
			while j < len(s) and s[j] == '1':
				j += 1
			m = min(m, s[0:i].count('1') + s[j:len(s)].count('0'), s[0:i].count('0') + s[j:len(s)].count('1'))
			i = j
		else:
			i += 1
	if '1' in s:
		print(min(m, s.count('1')))
	else:
		print(0)


import sys
max_int = 1000000001
min_int = -max_int
t = int(input())
for _t in range(t):
	s = input()
	prev = ''
	c = 0
	a = []
	for l in s:
		if l == prev:
			c += 1
		else:
			if prev == '1':
				a.append(c)
			else:
				a.append(-c)
			c = 1
			prev = l
	if prev == '1':
		a.append(c)
	else:
		a.append(-c)
	a = a[1:]
	lena = len(a)
	s1 = [0] * lena
	s1r = [0] * lena
	s2 = [0] * lena
	s2r = [0] * lena
	s1[0] = 0 if a[0] > 0 else -a[0]
	s2[0] = 0 if a[0] < 0 else a[0]
	for i in range(1, lena):
		s1[i] = s1[i - 1] - (a[i] if a[i] < 0 else 0)
		s2[i] = s2[i - 1] + (a[i] if a[i] > 0 else 0)
	s1r[lena - 1] = 0 if a[lena - 1] > 0 else -a[lena - 1]
	s2r[lena - 1] = 0 if a[lena - 1] < 0 else a[lena - 1]
	for i in range(lena - 2, -1, -1):
		s1r[i] = s1r[i + 1] - (a[i] if a[i] < 0 else 0)
		s2r[i] = s2r[i + 1] + (a[i] if a[i] > 0 else 0)
	ans = min(s1[-1], s2[-1], s1r[0], s2r[0])
	for i in range(0, lena - 1):
		ans = min(ans, s1[i] + s2r[i + 1], s2[i] + s1r[i + 1])
	print(ans)


from collections import defaultdict as dd
import math
import sys
input = sys.stdin.readline

def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return map(int, input().split())

def lm():
	return list(map(int, input().split()))
q = nn()
for _ in range(q):
	s = input()
	n = len(s)
	max0 = 0
	max1 = 0
	max01 = 0
	max10 = 0
	for i in range(n - 1):
		let = s[i]
		if let == '0':
			max1 += 1
			max01 = min(max0, max01 + 1)
		else:
			max0 += 1
			max10 = min(max1, max10 + 1)
	print(min(max01, max10))


R1 = lambda : list(map(int, input().split()))
R2 = lambda : int(input())
t = R2()
for _ in range(t):
	S = list(map(int, input().strip()))
	s_ones = s_zeros = 0
	p_ones = p_zeros = 0
	res = 0
	for s in S:
		if s == 1:
			s_ones += 1
		else:
			s_zeros += 1
	res = min(s_ones, s_zeros)
	for s in S:
		if s == 0:
			p_zeros += 1
			s_zeros -= 1
		else:
			p_ones += 1
			s_ones -= 1
		res = min(min(res, s_zeros + p_ones), p_zeros + s_ones)
	print(res)


for tt in range(int(input())):
	s = input()
	n = len(s)
	pref = [0 for i in range(n)]
	suff = [0 for i in range(n)]
	for i in range(n):
		if i == 0:
			if s[i] == '0':
				pref[i] = 1
			if s[n - 1 - i] == '0':
				suff[n - i - 1] = 1
			continue
		pref[i] = pref[i - 1]
		suff[n - 1 - i] = suff[n - i]
		if s[i] == '0':
			pref[i] += 1
		if s[n - i - 1] == '0':
			suff[n - i - 1] += 1
	ans = min(suff[0], n - suff[0])
	for i in range(n - 1):
		val = min(pref[i] + n - (i + 1) - suff[i + 1], i + 1 - pref[i] + suff[i + 1])
		ans = min(ans, val)
	print(ans)


t = int(input())
for i in range(t):
	b = str(input())
	ans = 10001
	for i in range(len(b)):
		k = b[0:i].count('1') + b[i + 1:len(b)].count('0')
		s = b[0:i].count('0') + b[i + 1:len(b)].count('1')
		p = min(k, s)
		if p < ans:
			ans = p
	print(ans)


for _ in range(int(input())):
	s = input()
	ones = s.count('1')
	zeros = s.count('0')
	zeroarray = []
	leftzero = []
	leftone = []
	onearray = []
	o = 0
	z = 0
	for i in s:
		if i == '1':
			o += 1
			onearray.append(o)
			leftone.append(ones - o)
			zeroarray.append(z)
			leftzero.append(zeros - z)
		else:
			z += 1
			onearray.append(o)
			leftone.append(ones - o)
			zeroarray.append(z)
			leftzero.append(zeros - z)
	minforone = 10 ** 9
	minforzero = 10 ** 9
	temp = 0
	for i in range(len(s)):
		temp = zeroarray[i] + leftone[i]
		if minforone > temp:
			minforone = temp
	for i in range(len(s)):
		temp = onearray[i] + leftzero[i]
		if minforzero > temp:
			minforzero = temp
	print(min(minforzero, minforone))


t = int(input())
Ans = []
for _ in range(0, t):
	s = input()
	x1 = 0
	y1 = 0
	x2 = 0
	y2 = 0
	for i in range(0, len(s)):
		if s[i] == '1':
			x1 += 1
		else:
			y1 += 1
	k = min(x1, y1)
	for i in range(0, len(s)):
		if s[i] == '1':
			x2 += 1
		else:
			y2 += 1
		k = min(k, x1 - x2 + y2, y1 + x2 - y2)
	Ans.append(k)
print(*Ans, sep='\n', end='')


def solve(s):

	def recur(i, s, temp):
		if temp in ['010', '101']:
			return 99999999
		if i == len(s):
			return 0
		if (i, temp) in memo:
			return memo[i, temp]
		(x, y) = (99999999, 99999999)
		if not temp:
			x = recur(i + 1, s, s[i])
			y = 1 + recur(i + 1, s, str(int(s[i]) ^ 1))
		elif s[i] == temp[-1]:
			x = recur(i + 1, s, temp)
			y = 1 + recur(i + 1, s, temp + str(int(s[i]) ^ 1))
		else:
			x = 1 + recur(i + 1, s, temp)
			y = recur(i + 1, s, temp + s[i])
		memo[i, temp] = min(x, y)
		return memo[i, temp]
	memo = {}
	ret = recur(0, s, '')
	return str(ret)
t = int(input())
for _ in range(t):
	s = str(input())
	print('' + solve(s))


import collections as cc
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	s = input()
	n = len(s)
	a = s.count('1')
	b = s.count('0')
	x = 0
	y = 0
	ans = 10 ** 9
	if n < 3:
		print(0)
	else:
		for i in range(n):
			if s[i] == '1':
				x += 1
			else:
				y += 1
			t = i + 1
			r1 = a - x
			r0 = b - y
			ans = min(x + r0, y + r1, ans)
		print(ans)


from collections import Counter
import math
import sys
from bisect import bisect, bisect_left, bisect_right

def input():
	return sys.stdin.readline().strip()

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def mod():
	return 10 ** 9 + 7
for _ in range(INT()):
	s = input()
	zero = s.count('0')
	one = len(s) - zero
	ans = min(zero, one)
	z_left = 0
	for i in range(len(s)):
		if s[i] == '0':
			z_left += 1
		o_left = i - z_left + 1
		ans = min(ans, min(z_left + (one - o_left), o_left + (zero - z_left)))
	print(ans)


for _ in range(int(input())):
	s = list(input())
	if len(s) <= 2:
		print(0)
	else:
		l = []
		n = len(s)
		ans = s.count('0')
		a = n - ans
		p = ans
		for i in range(n):
			if s[i] == '0':
				p -= 1
			else:
				p += 1
			ans = min(p, ans)
		p = a
		for i in range(n):
			if s[i] == '1':
				p -= 1
			else:
				p += 1
			ans = min(p, ans)
		print(ans)


t = int(input())
for test in range(t):
	s = input()
	arr = list(s)
	size = len(arr)
	left = []
	right = []
	zc = 0
	for i in arr:
		left.append(zc)
		if i == '1':
			zc += 1
	rarr = arr[::-1]
	zc = 0
	for i in rarr:
		right.append(zc)
		if i == '0':
			zc += 1
	right = right[::-1]
	final = []
	for i in range(size):
		final.append(left[i] + right[i])
	min1 = min(final)
	zc = 0
	left = []
	right = []
	for i in arr:
		left.append(zc)
		if i == '0':
			zc += 1
	rarr = arr[::-1]
	zc = 0
	for i in rarr:
		right.append(zc)
		if i == '1':
			zc += 1
	right = right[::-1]
	final = []
	for i in range(size):
		final.append(left[i] + right[i])
	min2 = min(final)
	print(min(min1, min2))


for _ in range(int(input())):
	t = 0
	lst = [i for i in input()]
	oc = lst.count('1')
	zc = lst.count('0')
	ans = min(zc, oc)
	(pre1, pre0) = (0, 0)
	for i in lst:
		pre0 += i == '0'
		zc -= i == '0'
		pre1 += i == '1'
		oc -= i == '1'
		ans = min(ans, pre1 + zc)
		ans = min(ans, pre0 + oc)
	print(ans)


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def solve():
	a = input().strip()
	(p, q, r, s) = (0, 0, 0, 0)
	for i in a:
		if i == '1':
			p += 1
		else:
			q += 1
	ans = min(p, q)
	for i in a:
		if i == '1':
			r += 1
			p -= 1
		else:
			s += 1
			q -= 1
		ans = min(ans, s + p)
		ans = min(ans, r + q)
	print(ans)
for _ in range(I()):
	solve()


for t in range(int(input())):
	s = input()
	c1 = s.count('1')
	c0 = s.count('0')
	l1 = 0
	l0 = 0
	ans = 0
	for i in s:
		if i == '1':
			l1 += 1
			c1 -= 1
			ans = max(ans, l1 + c0, l0 + c1)
		else:
			l0 += 1
			c0 -= 1
			ans = max(ans, l1 + c0, l0 + c1)
	print(len(s) - ans)


t = int(input())
for _ in range(t):
	s = list(input())
	n = len(s)
	ans = min(s.count('0'), s.count('1'))
	zero = 0
	one = 0
	ones = s.count('1')
	zeroes = s.count('0')
	for i in range(n):
		if s[i] == '0':
			zero += 1
		else:
			one += 1
		ans = min(ans, one + zeroes - zero, zero + ones - one)
	zero = 0
	one = 0
	for i in range(n - 1, -1, -1):
		if s[i] == '0':
			zero += 1
		else:
			one += 1
		ans = min(ans, one + zeroes - zero, zero + ones - one)
	print(ans)


import sys
input = sys.stdin.readline
from math import ceil, floor

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	s = input().strip()
	n = len(s)
	zero = []
	one = []
	z = o = 0
	for i in s:
		if i == '1':
			o += 1
		else:
			z += 1
		zero.append(z)
		one.append(o)
	ans = min(o, z)
	for i in range(n):
		a = one[i] + (z - zero[i])
		b = zero[i] + (o - one[i])
		ans = min(ans, a, b)
	print(ans)


from sys import stdin
for _ in range(int(stdin.readline())):
	string = input()
	arr = []
	N = [0, 0]
	(k, p) = (string.count('1'), string.count('0'))
	for i in string:
		N[int(i)] += 1
		arr.append(N[0] + k - N[1])
		arr.append(N[1] + p - N[0])
	print(min(arr))


t = int(input())
for _ in range(t):
	s = input()
	if s.count('0') == 0 or s.count('1') == 0:
		print(0)
	else:
		n = len(s)
		lz = []
		rz = []
		lo = []
		ro = []
		if s[0] == '0':
			lz.append(1)
			lo.append(0)
		elif s[0] == '1':
			lz.append(0)
			lo.append(1)
		if s[-1] == '0':
			rz.append(1)
			ro.append(0)
		elif s[-1] == '1':
			rz.append(0)
			ro.append(1)
		for i in range(1, n):
			if s[i] == '0':
				lz.append(lz[-1] + 1)
				lo.append(lo[-1])
			elif s[i] == '1':
				lo.append(lo[-1] + 1)
				lz.append(lz[-1])
			if s[n - i - 1] == '0':
				rz.append(rz[-1] + 1)
				ro.append(ro[-1])
			elif s[n - i - 1] == '1':
				rz.append(rz[-1])
				ro.append(ro[-1] + 1)
		ro = list(reversed(ro))
		rz = list(reversed(rz))
		ans = float('inf')
		for i in range(n):
			ans = min(ans, n - (ro[i] + lz[i]), n - (lo[i] + rz[i]))
		print(ans)


for _ in range(int(input())):
	s = input()
	n = len(s)
	(c0, c1) = (s.count('1'), s.count('1'))
	ans = min(c0, c1)
	for i in range(n):
		if s[i] == '0':
			c0 += 1
		else:
			c0 -= 1
		ans = min(ans, c0)
	for i in range(n - 1, -1, -1):
		if s[i] == '0':
			c1 += 1
		else:
			c1 -= 1
		ans = min(ans, c1)
	print(ans)


import sys

def read():
	return sys.stdin.readline().strip()

def write(a):
	sys.stdout.write(a)

def dist(s, t, n):
	k = 0
	for i in range(n):
		if s[i] != t[i]:
			k += 1
	return k

def solve(s):
	n = len(s)
	(f1, f2) = (dist(s, '0' * n, n), dist(s, '1' * n, n))
	(b1, b2) = (f1, f2)
	ans = min(f1, f2)
	for i in range(n):
		if s[i] == '1':
			f2 += 1
			b2 += 1
			f1 -= 1
			b1 -= 1
		else:
			f1 += 1
			b1 += 1
			f2 -= 1
			b2 -= 1
		ans = min(ans, f1, f2, b1, b2)
	return ans

def bruh():
	t = int(read())
	ans = [0] * t
	for i in range(t):
		ans[i] = solve(input())
	write('\n'.join(map(str, ans)))
bruh()


for _ in range(int(input())):
	a = list(input())
	ones = []
	zeros = []
	c0 = 0
	c1 = 0
	for i in range(len(a)):
		if a[i] == '1':
			c1 += 1
		else:
			c0 += 1
		ones.append(c1)
		zeros.append(c0)
	t0 = zeros[-1]
	t1 = ones[-1]
	m = 1000
	for i in range(len(a)):
		bma = max(ones[i], zeros[i])
		bmi = min(ones[i], zeros[i])
		fma = max(t0 - zeros[i], t1 - ones[i])
		fmi = min(t0 - zeros[i], t1 - ones[i])
		if bmi + fmi < m:
			m = bmi + fmi
	print(m)


def main():
	string = str(input())
	n = len(string)
	cnt1 = 0
	cnt0 = 0
	for i in range(n):
		if string[i] == '0':
			cnt0 += 1
		else:
			cnt1 += 1
	done0 = 0
	done1 = 0
	ans = min(cnt1, cnt0)
	for i in range(n):
		if string[i] == '0':
			done0 += 1
		if string[i] == '1':
			done1 += 1
		ans = min(done1 + cnt0 - done0, ans)
		ans = min(ans, done0 + cnt1 - done1)
	print(max(0, ans))
	return

def test():
	t = int(input())
	while t:
		main()
		t -= 1
test()


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	one = [0] * n
	zero = [0] * n
	z = 0
	o = 0
	for i in range(n):
		if s[i] == '0':
			z += 1
		else:
			o += 1
		one[i] = o
		zero[i] = z
	sone = [0] * n
	szero = [0] * n
	i = n - 1
	z = 0
	o = 0
	while i >= 0:
		if s[i] == '0':
			z += 1
		else:
			o += 1
		sone[i] = o
		szero[i] = z
		i -= 1
	ans = n
	for i in range(n):
		if s[i] == '1':
			ans = min(ans, max(0, zero[i] + sone[i] - 1))
		else:
			ans = min(ans, max(0, zero[i] + sone[i]))
		if s[i] == '0':
			ans = min(ans, max(0, one[i] + szero[i] - 1))
		else:
			ans = min(ans, max(0, one[i] + szero[i]))
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	(pref0, pref1, suff0, suff1) = (0, 0, 0, 0)
	best = 0
	for i in s:
		if i == '0':
			suff0 += 1
		else:
			suff1 += 1
	best = max(suff0, suff1)
	for i in range(n - 1):
		if s[i] == '0':
			suff0 -= 1
			pref0 += 1
		else:
			suff1 -= 1
			pref1 += 1
		best = max(best, pref0 + suff1, pref1 + suff0)
	print(n - best)


import queue
import math
t = int(input())
while t:
	s = input()
	(p1, p0) = (0, 0)
	(s1, s0) = (0, 0)
	for c in s:
		s1 += c == '1'
		s0 += c == '0'
	ans = 100000
	for c in s:
		ans = min(ans, p1 + s0, p0 + s1)
		p1 += c == '1'
		p0 += c == '0'
		s1 -= c == '1'
		s0 -= c == '0'
	print(ans)
	t -= 1


from math import *

def r1(t):
	return t(input().strip())

def r2(t):
	return [t(i) for i in input().strip().split()]

def r3(t):
	return [t(i) for i in input().strip()]
for _ in range(int(input())):
	t = r3(int)
	ps = 0
	ss = sum(t)
	n = len(t)
	ans = n
	for i in range(0, n):
		if i > 0:
			ss -= t[i - 1]
			ps += t[i - 1]
		res = min(i - ps + ss, ps + n - i - ss)
		ans = min(res, ans)
	print(ans)


import sys, io, os, time
ONLINE_JUDGE = __debug__
if not ONLINE_JUDGE:
	sys.stdin = open('../input.txt', 'r')
	sys.stdout = open('../output.txt', 'w')
start_time = time.time()
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect, math, heapq
alphabet = 'abcdefghijklmnopqrstuvwxyz'
for ii in range(int(input())):
	s = input()
	ones = 0
	zeros = 0
	for i in s:
		if i == '1':
			ones += 1
		else:
			zeros += 1
	d1 = 0
	d2 = 0
	ans = min(ones, zeros)
	for i in range(len(s)):
		if s[i] == '1':
			d1 += 1
			ones -= 1
		else:
			d2 += 1
			zeros -= 1
		ans = min(ans, d1 + zeros)
		ans = min(ans, d2 + ones)
	print(ans)
sys.stdout.close()


from itertools import groupby
import collections
n = int(input())

def solve(s):
	blocks = collections.deque([''.join(g) for (_, g) in groupby(s)])
	ops = float('inf')
	ones = [0]
	zeroes = [0]
	for b in blocks:
		if b[0] == '1':
			ones.append(ones[-1] + len(b))
			zeroes.append(zeroes[-1])
		else:
			ones.append(ones[-1])
			zeroes.append(zeroes[-1] + len(b))
	total1 = ones[-1]
	total0 = zeroes[-1]
	for (o, z) in zip(ones, zeroes):
		cost = z + total1 - o
		ops = min(cost, ops)
		cost = o + total0 - z
		ops = min(cost, ops)
	return ops
for _ in range(n):
	s = input()
	print(solve(s))


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def get_min_moves(s):
	(num_ones, num_zeros) = (0, 0)
	for ch in s:
		if ch == '1':
			num_ones += 1
		elif ch == '0':
			num_zeros += 1
	res = len(s)
	cur_ones = 0
	cur_zeros = 0
	for ch in s:
		left_moves = cur_ones
		right_moves = num_zeros - cur_zeros - (ch == '0')
		res = min(res, left_moves + right_moves)
		left_moves = cur_zeros
		right_moves = num_ones - cur_ones - (ch == '1')
		res = min(res, left_moves + right_moves)
		if ch == '0':
			cur_zeros += 1
		else:
			cur_ones += 1
	return res
num_tests = inp()
for _ in range(num_tests):
	s = input()
	print(get_min_moves(s))


for _ in range(int(input())):
	st = input()
	li = []
	s1 = 0
	for i in st:
		if i == '1':
			s1 += 1
		li.append(s1)
	le = len(st)
	ans = min(s1, le - s1)
	for i in range(le):
		x = li[i]
		ans = min(ans, s1 - 2 * x + i + 1, le - s1 + 2 * x - (i + 1))
	print(ans)


import atexit
import io
import sys
import math
from collections import defaultdict, Counter
t = int(input())
for i in range(t):
	s = input()
	l = len(s)
	count0 = s.count('0')
	count1 = s.count('1')
	ans = min(count1, count0)
	pref1 = [0] * l
	pref1[0] = int(s[0])
	suff0 = [0] * l
	suff0[l - 1] = int(s[l - 1]) ^ 1
	for j in range(1, l):
		if s[j] == '1':
			pref1[j] = 1
		pref1[j] += pref1[j - 1]
	for j in range(l - 2, -1, -1):
		if s[j] == '0':
			suff0[j] = 1
		suff0[j] += suff0[j + 1]
	for j in range(l - 1):
		s1 = pref1[j] + suff0[j + 1]
		s2 = j - pref1[j] + 1 + (l - j - 1 - suff0[j + 1])
		ans = min(ans, s1, s2)
	print(ans)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(input())):
	s = list(input())
	A = [0 for i in range(len(s) + 1)]
	B = A[:]
	for i in range(len(s)):
		if s[i] == '1':
			A[i + 1] = A[i] + 1
		else:
			A[i + 1] = A[i]
	for i in range(-1, -len(s) - 1, -1):
		if s[i] == '0':
			B[i - 1] = B[i] + 1
		else:
			B[i - 1] = B[i]
	ans = len(s)
	for i in range(len(s)):
		ans = min(A[i] + B[i], ans)
	A = [0 for i in range(len(s) + 1)]
	B = A[:]
	for i in range(len(s)):
		if s[i] == '0':
			A[i + 1] = A[i] + 1
		else:
			A[i + 1] = A[i]
	for i in range(-1, -len(s) - 1, -1):
		if s[i] == '1':
			B[i - 1] = B[i] + 1
		else:
			B[i - 1] = B[i]
	ans1 = len(s)
	for i in range(len(s)):
		ans1 = min(A[i] + B[i], ans1)
	print(min(ans, ans1))


def solve_case():
	a = input()
	sequences = []
	ls = 1
	for i in range(1, len(a)):
		if a[i] == a[i - 1]:
			ls += 1
			continue
		else:
			sequences.append(ls)
			ls = 1
	sequences.append(ls)
	ans = sum(sequences)
	ans = min(ans, sum(sequences[::2]), sum(sequences[1::2]))
	for i in range(0, len(sequences), 2):
		ans = min(ans, sum(sequences[1:i:2]) + sum(sequences[i + 2::2]), sum(sequences[:i:2]) + sum(sequences[i + 1::2]))
	return ans

def main():
	tests = int(input())
	for t in range(tests):
		print(solve_case())
main()


t = int(input())
while t > 0:
	t -= 1
	s = input()
	ones = 0
	zeros = 0
	for i in s:
		if i == '1':
			ones += 1
		else:
			zeros += 1
	ans = min(ones, zeros)
	d1 = 0
	d0 = 0
	for i in s:
		if i == '1':
			d1 += 1
			ones -= 1
		else:
			d0 += 1
			zeros -= 1
		ans = min(ans, d1 + zeros)
		ans = min(ans, d0 + ones)
	print(ans)


import math
from collections import defaultdict, Counter, deque
INF = float('inf')

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def primeFactor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i ** 2 <= n:
		if n % i == 0:
			return i
		i += 1
	return n

def main():
	s = list(input())
	n = len(s)
	if s[0] == '0':
		for i in range(n):
			if s[i] == '1':
				s[i] = '0'
			else:
				s[i] = '1'
	count_one = 0
	count_zero = 0
	for i in range(n):
		if s[i] == '1':
			count_one += 1
		else:
			count_zero += 1
	m = min(count_zero, count_one)
	o = 0
	z = 0
	for i in range(n):
		if s[i] == '1':
			o += 1
		else:
			z += 1
		a1 = count_one - o + z
		a2 = count_zero - z + o
		m = min(m, a1, a2)
	print(m)
t = int(input())
for _ in range(t):
	main()


from __future__ import division, print_function
from sys import stdin, stdout
import bisect
import math
import heapq
i_m = 9223372036854775807

def inin():
	return int(input())

def stin():
	return input()

def spin():
	return map(int, stin().split())

def lin():
	return list(map(int, stin().split()))

def modinv(n, p):
	return pow(n, p - 2, p)

def GCD(x, y):
	x = abs(x)
	y = abs(y)
	if min(x, y) == 0:
		return max(x, y)
	while y:
		(x, y) = (y, x % y)
	return x

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l
prime = []

def SieveOfEratosthenes(n):
	global prime
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	f = []
	for p in range(2, n):
		if prime[p]:
			f.append(p)
	return f
q = []

def dfs(n, d, v, c):
	global q
	v[n] = 1
	x = d[n]
	q.append(n)
	j = c
	for i in x:
		if i not in v:
			f = dfs(i, d, v, c + 1)
			j = max(j, f)
	return j
for i in range(int(input())):
	s = input()
	t = 10 ** 18
	for j in range(len(s)):
		a = s[:j].count('1')
		b = s[:j].count('0')
		c = s[j:].count('1')
		d = s[j:].count('0')
		t = min(t, b + c, a + d)
	print(t)


T = int(input())
for _ in range(T):
	s = input()
	n = len(s)
	res = 10 ** 100
	for i in range(n + 1):
		(p, q) = (s[:i], s[i:])
		res = min(res, p.count('0') + q.count('1'))
		res = min(res, p.count('1') + q.count('0'))
	print(res)


import sys
from functools import reduce
read = sys.stdin.buffer.readline
mi = lambda : map(int, read().split())
cin = lambda : int(read())
for _ in range(cin()):
	s = input()
	(no, nz) = (0, 0)
	for el in s:
		if el == '0':
			nz += 1
		else:
			no += 1
	ans = min(no, nz)
	(tnz, tno) = (0, 0)
	for el in s:
		if el == '0':
			tnz += 1
		else:
			tno += 1
		ans = min(ans, tno + nz - tnz)
		ans = min(ans, tnz + no - tno)
	print(ans)


for i in range(int(input())):
	s = [int(j) for j in input()]
	no_1 = s.count(1)
	no_0 = s.count(0)
	temp = [0, 0]
	M = len(s)
	for j in s:
		temp[j] += 1
		M = min(M, min(temp[0] + no_1 - temp[1], temp[1] + no_0 - temp[0]))
	print(M)


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	if n < 3:
		print(0)
	else:
		p = [0] * (n + 1)
		q = [0] * (n + 1)
		for i in range(n):
			if s[i] == '1':
				p[i + 1] = p[i] + 1
				q[i + 1] = q[i]
			else:
				q[i + 1] = q[i] + 1
				p[i + 1] = p[i]
		a = p[-1]
		b = q[-1]
		c = 1000000.0
		for i in range(1, n + 1):
			c = min(c, q[i] + a - p[i], p[i] + b - q[i])
		print(c)


for t in range(int(input())):
	s = input()
	s = list(map(int, list(s)))
	n = len(s)
	partial_sum = [0 for i in range(n)]
	partial_sum[0] = s[0]
	for i in range(1, n):
		partial_sum[i] = partial_sum[i - 1] + s[i]
	ans = n
	ans_1 = n
	ans_0 = n
	for p in range(n):
		ans_a = p + 1 - partial_sum[p] + (partial_sum[n - 1] - partial_sum[p])
		ans_b = partial_sum[p] + (n - p - 1) - (partial_sum[n - 1] - partial_sum[p])
		ans_1 = min(ans_1, ans_a)
		ans_0 = min(ans_0, ans_b)
		ans = min(ans, min(ans_1, ans_0))
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	totalnum0 = 0
	for i in range(len(s)):
		if s[i] == '0':
			totalnum0 += 1
	num0 = 0
	ans = len(s)
	for i in range(len(s) + 1):
		p1 = i - num0 + (totalnum0 - num0)
		p2 = len(s) - p1
		ans = min(ans, p1, p2)
		if i == len(s):
			break
		num0 += s[i] == '0'
	print(ans)


for tc in range(int(input())):
	l = input()
	(s0, s1) = (0, 0)
	for i in l:
		if i == '0':
			s0 += 1
		else:
			s1 += 1
	ans = min(s0, s1)
	(p0, p1) = (0, 0)
	for i in l:
		if i == '0':
			p0 += 1
			s0 -= 1
		else:
			p1 += 1
			s1 -= 1
		ans = min(ans, p1 + s0)
		ans = min(ans, p0 + s1)
	print(ans)


test = int(input())
for i in range(test):
	s = input()
	c0 = 0
	c1 = 0
	for i in s:
		if i == '0':
			c0 += 1
		else:
			c1 += 1
	ans = min(c1, c0)
	n1 = 0
	n0 = 0
	for i in range(len(s)):
		if s[i] == '1':
			n1 += 1
			c1 -= 1
		else:
			n0 += 1
			c0 -= 1
		ans = min(ans, c0 + n1)
		ans = min(ans, c1 + n0)
	print(ans)


T = int(input())
while T:
	S = input()
	n = len(S)
	m = min(S.count('0'), S.count('1'))
	for p in range(n + 1):
		aL = 0
		bL = 0
		aR = 0
		bR = 0
		for i in range(0, p):
			if S[i] == '0':
				aL = aL + 1
			else:
				bL = bL + 1
		for i in range(p, n):
			if S[i] == '0':
				aR = aR + 1
			else:
				bR = bR + 1
		m = min(m, min(aL, bL) + min(aR, bR))
	print(m)
	T = T - 1


for _ in range(int(input())):
	s = input()
	zero = s.count('0')
	ones = len(s) - zero
	ct = 0
	ans = float('inf')
	for i in range(len(s)):
		if s[i] == '0':
			ct += 1
		ans = min(ans, i + 1 + zero - 2 * ct)
		ans = min(ans, 2 * ct + ones - i - 1)
	print(ans)


for _ in ' ' * int(input()):
	a = input()
	z = len(a)
	ans = z
	for i in range(z):
		ans = min(ans, a[:i + 1].count('1') + a[i + 1:].count('0'), a[:i + 1].count('0') + a[i + 1:].count('1'))
	print(ans)


from functools import reduce
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def arr():
	return [int(i) for i in input().split()]

def sarr():
	return [int(i) for i in input()]

def inn():
	return int(input())
mo = 1000000007
for _ in range(inn()):
	s = input()
	n = len(s)
	c0 = 0
	c1 = 0
	for i in range(n):
		if s[i] == '0':
			c0 += 1
		else:
			c1 += 1
	ans = min(c0, c1)
	cnt0 = 0
	cnt1 = 0
	for i in range(n):
		if s[i] == '0':
			cnt0 += 1
			c0 -= 1
		else:
			cnt1 += 1
			c1 -= 1
		ans = min(ans, cnt0 + c1)
		ans = min(ans, cnt1 + c0)
	print(ans)


for _ in range(int(input())):
	s = input()
	n = len(s)
	minn = n
	a = [0 for i in range(n)]
	if s[0] == '1':
		a[0] = 1
	for i in range(1, n):
		if s[i] == '1':
			a[i] = a[i - 1] + 1
		else:
			a[i] = a[i - 1]
	for i in range(n):
		c1 = 2 * a[i] - (i + 1) + n - a[n - 1]
		c2 = a[n - 1] + i + 1 - 2 * a[i]
		minn = min(minn, c1, c2)
	print(minn)


for _ in range(int(input())):
	s = input()
	length = len(s)
	zeros = s.count('0')
	ones = length - zeros
	subrange = [0, 0]
	cost = min(zeros, ones)
	for i in range(length):
		cost_01 = subrange[1] + zeros
		cost_10 = subrange[0] + ones
		cost = min(cost_01, cost_10, cost)
		if s[i] == '0':
			subrange[0] += 1
			zeros -= 1
		else:
			subrange[1] += 1
			ones -= 1
	if len(set(s)) == 1:
		cost = 0
	print(cost)


def solve(s):
	cnt = 0
	n = len(s)
	onecount = 0
	pref = [0] * n
	if n < 3:
		print(0)
		return
	for i in range(n):
		if s[i] == '1':
			onecount += 1
		pref[i] = onecount
	cost = n
	for i in range(n):
		oneleft = pref[i]
		zeroleft = i + 1 - pref[i]
		oneright = onecount - oneleft
		zeroright = n - onecount - zeroleft
		cost = min(cost, oneleft + zeroright)
		cost = min(cost, zeroleft + oneright)
	print(cost)
t = int(input())
for _ in range(t):
	s = input()
	solve(s)


t = int(input())
for _ in [0] * t:
	s = input()
	print(min((s[:i].count('01'[k]) + s[i:].count('10'[k]) for k in [0, 1] for i in range(len(s) + 1))))


import collections

def read():
	return input().strip()

def readInt():
	return int(input().strip())

def readList():
	return list(map(int, input().strip().split()))

def solve(s):
	(sufZero, sufOne) = (0, 0)
	for ch in s:
		sufZero += int(ch == '0')
		sufOne += int(ch == '1')
	ans = min(sufZero, sufOne)
	(prefZero, prefOne) = (0, 0)
	for (i, ch) in enumerate(s):
		prefZero += int(ch == '0')
		prefOne += int(ch == '1')
		sufZero -= int(ch == '0')
		sufOne -= int(ch == '1')
		ans = min(ans, prefOne + sufZero)
		ans = min(ans, prefZero + sufOne)
	return ans
t = readInt()
for i in range(t):
	s = read()
	print(solve(s))


for test_i in range(int(input())):
	s = input()
	arr = []
	num_succ = 1
	prev_el = s[0]
	for i in range(1, len(s)):
		if prev_el == s[i]:
			num_succ += 1
		else:
			prev_el = s[i]
			arr.append(num_succ)
			num_succ = 1
	arr.append(num_succ)
	(l1, l2, r1, r2) = (sum(arr[::2]), sum(arr[1::2]), 0, 0)
	ans = min(l1, l2) + min(r1, r2)
	if len(arr) % 2 == 0:
		for i in range(len(arr) // 2):
			l2 -= arr[-2 * i - 1]
			r1 += arr[-2 * i - 1]
			ans = min(ans, min(l1, l2) + min(r1, r2))
			l1 -= arr[-2 * i - 2]
			r2 += arr[-2 * i - 2]
			ans = min(ans, min(l1, l2) + min(r1, r2))
	else:
		for i in range(len(arr) // 2):
			l1 -= arr[-2 * i - 1]
			r2 += arr[-2 * i - 1]
			ans = min(ans, min(l1, l2) + min(r1, r2))
			l2 -= arr[-2 * i - 2]
			r1 += arr[-2 * i - 2]
			ans = min(ans, min(l1, l2) + min(r1, r2))
		l1 -= arr[0]
		r2 += arr[0]
		ans = min(ans, min(l1, l2) + min(r1, r2))
	print(ans)


import sys, math
from collections import deque

def solve():
	s = list(map(int, list(input())))
	aux0 = [0] * (len(s) + 1)
	aux1 = [0] * (len(s) + 1)
	for i in range(len(s) - 1, -1, -1):
		if s[i] == 0:
			aux0[i] = aux0[i + 1] + 1
			aux1[i] = aux1[i + 1]
		else:
			aux1[i] = aux1[i + 1] + 1
			aux0[i] = aux0[i + 1]
	minV = len(s)
	for i in range(len(s), -1, -1):
		minV = min(aux1[0] - aux1[i] + aux0[i], minV)
		minV = min(aux0[0] - aux0[i] + aux1[i], minV)
	print(minV)
for _ in range(int(input())):
	solve()


from sys import stdin
from typing import List

def solution(line: str):
	(l0, l1) = ([0] * len(line), [0] * len(line))
	(r0, r1) = ([0] * len(line), [0] * len(line))
	(l0[0], l1[0]) = (int(line[0] == '0'), int(line[0] == '1'))
	(zeros, ones) = (0, 0)
	for i in range(1, len(line), 1):
		zeros += line[i] == '0'
		l0[i] = l0[i - 1] + (line[i] == '0')
		l1[i] = l1[i - 1] + (line[i] == '1')
	ones = len(line) - zeros
	for i in range(len(line) - 2, -1, -1):
		r0[i] = r0[i + 1] + (line[i + 1] == '0')
		r1[i] = r1[i + 1] + (line[i + 1] == '1')
	rez: int = float('inf')
	for i in range(len(line)):
		rez = min(rez, min(l1[i] + r0[i], l0[i] + r1[i]))
	return min(rez, zeros, ones)
lines: List[str] = []
tests = int(stdin.readline().rstrip())
for i in range(tests):
	lines.append(stdin.readline().rstrip())
for line in lines:
	print(solution(line))


for _ in range(int(input())):
	s = input()
	t0 = s.count('0')
	t1 = s.count('1')
	res = min(t0, t1)
	seen0 = 0
	seen1 = 0
	for i in range(len(s)):
		if s[i] == '0':
			seen0 += 1
			t0 -= 1
		else:
			seen1 += 1
			t1 -= 1
		res = min(res, seen1 + t0)
		res = min(res, seen0 + t1)
	print(res)


for _ in range(int(input())):
	s = list(input())
	n = len(s)
	ans = n
	o = [0] * n
	z = [0] * n
	count = 1
	i = 1
	while i < n and count < 3:
		if s[i] != s[i - 1]:
			count += 1
		i += 1
	if count < 3:
		print(0)
		continue
	for index in range(n):
		if s[index] == '1':
			o[index] += 1
		else:
			z[index] += 1
		if index > 0:
			o[index] += o[index - 1]
			z[index] += z[index - 1]
	for index in range(n):
		ans = min(ans, min(o[index] + z[n - 1] - z[index], z[index] + o[n - 1] - o[index]))
	print(ans)


for i in range(int(input())):
	s = input()
	minc = 2 * len(s)
	oam = s.count('1')
	zam = s.count('0')
	ops = 0
	zps = 0
	for j in s:
		if j == '1':
			ops += 1
		else:
			zps += 1
		cur = ops + zam - zps
		minc = min(minc, cur)
		cur = zps + oam - ops
		minc = min(minc, cur)
	print(minc)


def danger(s):
	if len(s) < 3:
		return 0
	if len(s) == 3:
		if s == '010' or s == '101':
			return 1
		else:
			return 0
	if sum(list(map(int, s.split()))) == 0 or sum(list(map(int, s.split()))) == len(s):
		return 0
	l = []
	for i in range(len(s)):
		l.append(int(s[i]))
	o_n = sum(l)
	z_n = len(s) - o_n
	i = 0
	(z_c, o_c) = (0, 0)
	res = 1000
	max_res = 10000
	while i < len(s):
		if l[i] == 1:
			o_c += 1
		if l[i] == 0:
			z_c += 1
		res = min(z_c + (o_n - o_c), o_c + (z_n - z_c))
		max_res = min(max_res, res)
		i += 1
	return max_res
for _ in range(int(input())):
	s = input()
	print(danger(s))


def solve():
	s = input()
	res = float('inf')
	for i in range(len(s)):
		(zero, one) = (s[:i].count('0'), s[i:].count('1'))
		res = min(res, zero + one, i - zero + len(s) - i - one)
	print(res)
for _ in range(int(input())):
	solve()


t = int(input())

def solve(s):
	(n0, n1) = (0, 0)
	(m0, m1) = (0, 0)
	for c in s:
		if c == '0':
			m0 += 1
	m1 = len(s) - m0
	best = min(n0, n1) + min(m0, m1)
	for i in range(len(s)):
		c = s[i]
		if c == '0':
			n0 += 1
			m0 -= 1
		else:
			n1 += 1
			m1 -= 1
		best = min(best, min(n0, n1) + min(m0, m1))
	print(best)
for i in range(t):
	s = input()
	solve(s)


for _ in range(int(input())):
	bin = input()
	f1 = [0] * len(bin)
	b1 = [0] * len(bin)
	f0 = [0] * len(bin)
	b0 = [0] * len(bin)
	for i in range(1, len(bin)):
		if bin[i - 1] == '1':
			b1[i] = 1
		else:
			b0[i] = 1
		b1[i] += b1[i - 1]
		b0[i] += b0[i - 1]
	for i in range(len(bin) - 2, -1, -1):
		if bin[i + 1] == '1':
			f1[i] = 1
		else:
			f0[i] = 1
		f1[i] += f1[i + 1]
		f0[i] += f0[i + 1]
	ans = len(bin)
	for i in range(0, len(bin)):
		same_same1 = f0[i] + b0[i]
		same_diff1 = b0[i] + f1[i]
		diff_same1 = b1[i] + f0[i]
		same_same0 = f1[i] + b1[i]
		same_diff0 = b1[i] + f0[i]
		diff_same0 = b0[i] + f1[i]
		if bin[i] == '1':
			ans = min(ans, same_same1, same_diff1, diff_same1, 1 + same_same0, 1 + same_diff0, 1 + diff_same0)
		else:
			ans = min(ans, same_same0, same_diff0, diff_same0, 1 + same_same1, 1 + same_diff1, 1 + diff_same1)
	print(ans)


for i in range(int(input())):
	string = str(input())
	x = 10 ** 18
	for j in range(len(string) + 1):
		x = min(x, string[:j].count('0') + string[j:].count('1'))
		x = min(x, string[j:].count('0') + string[:j].count('1'))
	print(x)


import sys

def main(l):
	lst = []
	a = 1
	for i in range(len(l) - 1):
		if l[i + 1] == l[i]:
			a += 1
		else:
			lst.append(a)
			a = 1
	lst.append(a)
	zero = [lst[i] for i in range(0, len(lst)) if i % 2 == 0]
	one = [lst[i] for i in range(0, len(lst)) if i % 2 == 1]
	if len(lst) == 1 or len(lst) == 2:
		return 0
	elif len(lst) == 3:
		return min(lst)
	elif len(lst) % 2 == 0:
		mini1 = sum(one)
		nouv_mini1 = mini1
		for i in range(len(one) - 1):
			nouv_mini1 = nouv_mini1 + zero[i] - one[i]
			if nouv_mini1 < mini1:
				mini1 = nouv_mini1
		mini2 = sum(zero[1:])
		nouv_mini2 = mini2
		for i in range(len(zero) - 1):
			nouv_mini2 = nouv_mini2 + one[i] - zero[i + 1]
			if nouv_mini2 < mini2:
				mini2 = nouv_mini2
		return min(mini1, mini2)
	elif len(lst) % 2 == 1:
		mini1 = sum(zero[1:])
		nouv_mini1 = mini1
		for i in range(len(zero) - 1):
			nouv_mini1 = nouv_mini1 - zero[i + 1] + one[i]
			if nouv_mini1 < mini1:
				mini1 = nouv_mini1
		mini2 = sum(one)
		nouv_mini2 = mini2
		for i in range(len(one)):
			nouv_mini2 = nouv_mini2 + zero[i] - one[i]
			if nouv_mini2 < mini2:
				mini2 = nouv_mini2
		return min(mini1, mini2)
t = sys.stdin.readline()
for i in range(int(t)):
	l1 = list(sys.stdin.readline())
	l = list(map(int, l1[:-1]))
	print(main(l))


import sys
import math
import collections
from collections import deque
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factors(n):
	return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))
for _ in range(int(stdin.readline())):
	s = list(stdin.readline().strip('\n'))
	n = len(s)
	(num_1, num_0) = (s.count('1'), s.count('0'))
	(done_1, done_0) = (0, 0)
	ans = min(num_0, num_1)
	for i in range(n):
		if s[i] == '1':
			done_1 += 1
			num_1 -= 1
		else:
			done_0 += 1
			num_0 -= 1
		ans = min(ans, done_0 + num_1)
		ans = min(ans, done_1 + num_0)
	print(ans)


t = int(input())
while t > 0:
	s = input()
	ones = 0
	zeros = 0
	for i in range(len(s)):
		if s[i] == '0':
			zeros += 1
		else:
			ones += 1
	x1 = 0
	x2 = 0
	res = len(s)
	for i in range(len(s)):
		if s[i] == '1':
			x1 += 1
		else:
			x2 += 1
		temp1 = x1 + zeros - x2
		temp2 = x2 + ones - x1
		res = min(res, temp1, temp2)
	print(res)
	t -= 1


for _ in range(int(input())):
	s = []
	for x in input():
		s.append(int(x))

	def f(s):
		one = sum(s)
		zero = 0
		MIN = one
		for x in s:
			if x == 0:
				zero += 1
			else:
				one -= 1
			MIN = min(MIN, zero + one)
		return MIN
	print(min(f(s), f(s[::-1])))


def solve():
	s = input()
	c0 = 0
	c1 = 0
	for c in s:
		if c == '0':
			c0 += 1
	c1 = len(s) - c0
	ca = 0
	cb = 0
	minmoves = 100000
	for c in s:
		moves = cb + c0 - ca
		if moves < minmoves:
			minmoves = moves
		moves = ca + c1 - cb
		if moves < minmoves:
			minmoves = moves
		if c == '0':
			ca += 1
		else:
			cb += 1
	print(min(c0, c1, minmoves))

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


import bisect
from itertools import accumulate
import os
import sys
import math
from decimal import *
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def factors(n):
	fac = []
	while n % 2 == 0:
		fac.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 2):
		while n % i == 0:
			fac.append(i)
			n = n // i
	if n > 1:
		fac.append(n)
	return fac
for _ in range(int(input())):
	s = input()
	if len(set(s)) == 1:
		print(0)
	else:
		ones = s.count('1')
		zero = s.count('0')
		maxi = 1e+18
		done1 = 0
		done0 = 0
		for i in range(0, len(s)):
			if s[i] == '0':
				done0 += 1
			else:
				done1 += 1
			maxi = min(maxi, done0 + ones - done1, done1 + zero - done0)
		print(maxi)


for i in range(int(input())):
	s = input()
	g1 = 0
	g0 = 0
	for j in range(len(s)):
		if s[j] == '1':
			g1 += 1
		else:
			g0 += 1
	t1 = 0
	t0 = 0
	ans = 1000000000.0
	for j in range(len(s)):
		ans = min(ans, min(t0 + g1 - t1, t1 + g0 - t0))
		if s[j] == '1':
			t1 += 1
		else:
			t0 += 1
	ans = min(ans, min(t0, t1))
	print(ans)


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator
from collections import deque, defaultdict, OrderedDict
import collections

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(ri()):
		s = rs()
		n = len(s)
		tc1 = 0
		tc0 = 0
		p1 = []
		p0 = []
		for i in s:
			if i == '1':
				tc1 += 1
			else:
				tc0 += 1
			p1.append(tc1)
			p0.append(tc0)
		nf0 = tc0
		nf1 = tc1
		for i in range(n):
			nf0 = min(nf0, p1[i] + tc0 - p0[i])
			nf1 = min(nf1, p0[i] + tc1 - p1[i])
		print(min(nf0, nf1))
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from collections import Counter
from bisect import bisect_left
import sys
from queue import PriorityQueue
import operator as op
from functools import reduce
mod = 1000000007

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())

def printIn(ob):
	return stdout.write(str(ob) + '\n')

def powerLL(n, p):
	result = 1
	while p:
		if p & 1:
			result = result * n % mod
		p = int(p / 2)
		n = n * n % mod
	return result

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def myCode():
	s = input()
	done_zeros = s.count('0')
	done_ones = s.count('1')
	o = 0
	z = 0
	ans = sys.maxsize
	for i in s:
		if i == '1':
			o += 1
		elif i == '0':
			z += 1
		ans = min(ans, o + done_zeros - z, z + done_ones - o)
	print(ans)

def main():
	for t in range(I()):
		myCode()
main()
...


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
testcases = int(input())
for j in range(testcases):
	vals = [int(k) for k in input()]
	pref0 = []
	pref1 = []
	sum0 = 0
	sum1 = 0
	for s in range(len(vals)):
		if vals[s] == 0:
			sum0 += 1
		else:
			sum1 += 1
		pref0.append(sum0)
		pref1.append(sum1)
	move0 = len(vals)
	move1 = len(vals)
	for s in range(len(vals)):
		ma = s + 1 - pref1[s]
		mb = len(vals) - (s + 1) - (pref0[len(vals) - 1] - pref0[min(s, len(vals) - 1)])
		move = ma + mb
		if move < move1:
			move1 = move
	for s in range(len(vals)):
		ma = s + 1 - pref0[s]
		mb = len(vals) - (s + 1) - (pref1[len(vals) - 1] - pref1[min(s, len(vals) - 1)])
		move = ma + mb
		if move < move0:
			move0 = move
	print(min(move1, move0))


t = int(input())
for test in range(t):
	s = input()
	suf0 = 0
	suf1 = 0
	for i in s:
		if i == '0':
			suf0 += 1
		else:
			suf1 += 1
	ans = min(suf0, suf1)
	pref0 = 0
	pref1 = 0
	for i in s:
		if i == '0':
			pref0 += 1
			suf0 -= 1
		else:
			pref1 += 1
			suf1 -= 1
		ans = min(ans, pref1 + suf0)
		ans = min(ans, pref0 + suf1)
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	if len(s) == 1 or len(s) == 2:
		print(0)
	else:
		rightzeros = s.count('0')
		rightones = s.count('1')
		leftzeros = 0
		leftones = 0
		res = min(rightones, rightzeros)
		for x in s:
			if x == '0':
				rightzeros -= 1
				leftzeros += 1
			elif x == '1':
				rightones -= 1
				leftones += 1
			lzro = leftones + rightzeros
			lorz = leftzeros + rightones
			temp = min(lzro, lorz)
			res = min(res, temp)
		print(res)


t = int(input())
for _ in range(t):
	s = list(map(int, input()))
	z = o = 0
	if s[0] == 0:
		z += 1
	else:
		o += 1
	for i in range(1, len(s)):
		s[i] += s[i - 1]
		if s[i] > s[i - 1]:
			o += 1
		else:
			z += 1
	ans = len(s)
	for i in range(len(s)):
		ans = min(2 * s[i] + z - i - 1, i + 1 - 2 * s[i] + o, ans)
	print(min(ans, o, z))


t = int(input())
for _ in range(t):
	s = input()
	zero = s.count('0')
	one = len(s) - zero
	op = 1001
	cf = [0]
	for x in s:
		if x == '1':
			cf.append(cf[-1] + 1)
		else:
			cf.append(cf[-1])
	if zero:
		if one:
			for i in range(1, len(cf)):
				op = min(op, cf[i - 1] + (len(cf) - i) - (cf[-1] - cf[i - 1]))
				op = min(op, i - 1 - cf[i - 1] + cf[-1] - cf[i - 1])
			print(op)
		else:
			print(0)
	else:
		print(0)


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop
import math
from collections import *
from functools import reduce, cmp_to_key, lru_cache
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
M = mod = 10 ** 9 + 7

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip().split()]

def st():
	return str(input().rstrip())[2:-1]

def val():
	return int(input().rstrip())

def li2():
	return [str(i)[2:-1] for i in input().rstrip().split()]

def li3():
	return [int(i) for i in st()]
for _ in range(val()):
	s = li3()
	pref = [0]
	n = len(s)
	l = s[:]
	for i in s:
		pref.append(pref[-1] + i)
	pref = pref[1:]
	pref2 = [0]
	for i in l[::-1]:
		pref2.append(pref2[-1] + i)
	pref2 = pref2[1:]
	pref2.reverse()
	ans = min(l.count(1), l.count(0))
	for i in range(n):
		lc1 = i + 1 - pref[i]
		rc1 = pref2[i]
		lc2 = pref[i]
		rc2 = n - i - pref2[i]
		ans = min(ans, lc1 + rc1 - l[i], lc2 + rc2 - l[i])
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	o = 0
	z = 0
	for i in s:
		if i == '1':
			o += 1
		else:
			z += 1
	ans = min(o, z)
	(n_0, n_1) = (0, 0)
	for i in s:
		if i == '1':
			n_1 += 1
		else:
			n_0 += 1
		ans = min(ans, n_1 + z - n_0)
		ans = min(ans, n_0 + o - n_1)
	print(ans)


t = int(input())
for i in range(t):
	s = str(input())
	mn = 10 ** 10
	for j in range(len(s)):
		osb = s[:j].count('1')
		zsb = s[:j].count('0')
		ose = s[j:].count('1')
		zse = s[j:].count('0')
		mn = min(mn, zsb + ose, zse + osb)
	print(mn)


for _ in range(int(input())):
	s = input()
	n = len(s)
	ones = [0 for i in range(n)]
	zeros = [0 for i in range(n)]
	one = s.count('1')
	zero = s.count('0')
	for i in range(n):
		if s[i] == '0':
			zeros[i] = zeros[i - 1] + 1
			ones[i] = ones[i - 1]
		else:
			zeros[i] = zeros[i - 1]
			ones[i] = ones[i - 1] + 1
	cost = 10 ** 9
	for i in range(n):
		cost = min(cost, zeros[i] + one - ones[i], zero - zeros[i] + ones[i])
	print(cost)


import sys
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		S = input().strip()
		cnt = 0
		prev = S[0]
		for i in range(1, len(S)):
			if prev != S[i]:
				cnt += 1
				prev = S[i]
		if cnt <= 1:
			print(0)
			continue
		pcntz = [0] * len(S)
		scntz = [0] * len(S)
		for i in range(len(S)):
			if S[i] == '0':
				pcntz[i] = pcntz[i - 1] + 1
			else:
				pcntz[i] = pcntz[i - 1]
		for i in range(len(S)):
			if S[-i - 1] == '0':
				scntz[-i - 1] = scntz[-i] + 1
			else:
				scntz[-i - 1] = scntz[-i]
		ans = float('inf')
		for i in range(len(S) - 1):
			ans = min(ans, i + 1 - pcntz[i] + (len(S) - i - 1 - scntz[i + 1]))
			ans = min(ans, pcntz[i] + scntz[i + 1])
			ans = min(ans, pcntz[i] + (len(S) - i - 1 - scntz[i + 1]))
			ans = min(ans, i + 1 - pcntz[i] + scntz[i + 1])
		print(ans)
main()


t = int(input())
for tc in range(t):
	s = input()
	b = s.count('1')
	b1 = b
	s1 = ''
	for i in range(len(s) - 1, -1, -1):
		s1 += s[i]
	mi = 10000000
	le = len(s) - b
	if le < mi:
		mi = le
	j = 1
	l = 0
	for i in s:
		if i == '0':
			r = len(s) - b - j
			va = l + r
			if va < mi:
				mi = va
		else:
			l += 1
			b -= 1
			r = len(s) - b - j
			va = l + r
			if va < mi:
				mi = va
		j += 1
	b = b1
	le = len(s) - b
	if le < mi:
		mi = le
	j = 1
	l = 0
	for i in s1:
		if i == '0':
			r = len(s) - b - j
			va = l + r
			if va < mi:
				mi = va
		else:
			l += 1
			b -= 1
			r = len(s) - b - j
			va = l + r
			if va < mi:
				mi = va
		j += 1
	print(mi)


import os
import sys
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
while t:
	mini = float('inf')
	l = input()
	l = [int(i) for i in l]
	n = len(l)
	nzl = 0
	nol = 0
	nzr = 0
	nor = 0
	for i in range(n):
		if l[i] == 0:
			nzr += 1
		else:
			nor += 1
	mini = min(mini, nor)
	mini = min(mini, nzr)
	for i in range(n):
		nzl = 0
		nol = 0
		nzr = 0
		nor = 0
		for j in range(0, i):
			if l[j] == 0:
				nzl += 1
			else:
				nol += 1
		for j in range(i, n):
			if l[j] == 0:
				nzr += 1
			else:
				nor += 1
		mini = min(mini, nol + nor)
		mini = min(mini, nol + nzr)
		mini = min(mini, nzl + nor)
		mini = min(mini, nzl + nzr)
	print(mini)
	t -= 1


import os
import sys
from io import BytesIO, IOBase

def solution(s):
	n = len(s)
	s = list(s)
	if n < 3:
		write(0)
		return
	s.insert(0, -1)
	zero = sum((s[i] == '0' for i in range(1, n + 1)))
	ones = n - zero
	res = min(zero, ones)
	(n_zeros, n_ones) = (0, 0)
	for i in range(1, n + 1):
		if s[i] == '0':
			n_zeros += 1
		else:
			n_ones += 1
		res = min(res, n_ones + zero - n_zeros)
		res = min(res, n_zeros + ones - n_ones)
	write(res)

def main():
	for _ in range(r_int()):
		s = input()
		solution(s)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def write(*args, end='\n'):
	for x in args[:-1]:
		sys.stdout.write(str(x) + ' ')
	sys.stdout.write(str(args[-1]))
	sys.stdout.write(end)

def r_array():
	return [int(x) for x in input().split()]

def r_int():
	return int(input())
main()


for t in range(int(input())):
	s = input()
	n = len(s)
	(ones, zeros) = ([0] * n, [0] * n)
	ones[0] += int(s[0])
	zeros[0] += 1 - int(s[0])
	for i in range(1, len(s)):
		ones[i] = ones[i - 1] + int(s[i])
		zeros[i] = zeros[i - 1] + 1 - int(s[i])
	best = 100000000
	for i in range(n):
		best = min(best, ones[i] + zeros[-1] - zeros[i])
		best = min(best, zeros[i] + ones[-1] - ones[i])
	print(best)


for _ in range(int(input())):
	s = input().strip()
	total_one = 0
	total_zero = 0
	for i in s:
		if i == '1':
			total_one += 1
		else:
			total_zero += 1
	count_one = 0
	count_zero = 0
	i = 0
	arr_zero = [0] * len(s)
	arr_one = [0] * len(s)
	min_array = [0] * len(s)
	mini = 9999999
	while i < len(s):
		if s[i] == '1':
			count_one += 1
		else:
			count_zero += 1
		arr_zero[i] = count_one + total_zero - count_zero
		arr_one[i] = count_zero + total_one - count_one
		mini = min(arr_zero[i], arr_one[i], mini)
		i += 1
	print(mini)


t = int(input())
for i in range(t):
	s = input()
	left = [0]
	right = [0]
	zero = 0
	for k in s:
		if k == '0':
			zero += 1
		left.append(zero)
	zero = 0
	for k in s[::-1]:
		if k == '0':
			zero += 1
		right.append(zero)
	right = right[::-1]
	right = right[1:]
	left = left[:-1]
	n = len(s)
	mini = n
	for k in range(len(s)):
		minc = min(left[k] + (n - k - 1 - right[k]), right[k] + (k - left[k]))
		mini = min(mini, minc)
	print(mini)


t = int(input())
for _ in range(t):
	s = list(map(int, list(input())))
	c0 = 0
	c1 = 0
	pre = []
	ans = len(s) + 1
	for i in s:
		pre.append([c0, c1])
		if i == 0:
			c0 += 1
		else:
			c1 += 1
	c0 = 0
	c1 = 0
	post = []
	for i in s[::-1]:
		post.append([c0, c1])
		if i == 0:
			c0 += 1
		else:
			c1 += 1
	post = post[::-1]
	for i in range(len(s)):
		ans = min(ans, pre[i][0] + post[i][1])
		ans = min(ans, pre[i][1] + post[i][0])
	print(ans)


import math
t = int(input())
for qq in range(t):
	s = input()
	num = 0
	for i in s:
		if i == '0':
			num += 1
	m = num
	saved = num
	for i in range(len(s) - 1, -1, -1):
		if s[i] == '0':
			num -= 1
		else:
			num += 1
		m = min(num, m)
	num = saved
	for i in range(len(s)):
		if s[i] == '0':
			num -= 1
		else:
			num += 1
		m = min(num, m)
	print(m)


t = int(input())
for i in range(t):
	s = input()
	n = len(s)
	cnt1 = 0
	cnt0 = 0
	for i in range(n):
		if s[i] == '1':
			cnt1 += 1
		else:
			cnt0 += 1
	ans = n
	d0 = 0
	d1 = 0
	for i in range(n):
		if s[i] == '1':
			d1 += 1
		else:
			d0 += 1
		ans = min(d1 + cnt0 - d0, d0 + cnt1 - d1, ans)
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	s = list(input().rstrip())
	zero_left = [0]
	one_left = [0]
	zero = 0
	one = 0
	for ch in s:
		if ch == '1':
			one += 1
		else:
			zero += 1
		one_left.append(one)
		zero_left.append(zero)
	zero_right = []
	one_right = []
	zero = 0
	one = 0
	for ch in s[::-1]:
		if ch == '1':
			one += 1
		else:
			zero += 1
		one_right.append(one)
		zero_right.append(zero)
	one_right.reverse()
	zero_right.reverse()
	one_right.append(0)
	zero_right.append(0)
	ans = len(s)
	for (l, r) in zip(one_left, zero_right[1:]):
		ans = min(ans, l + r)
	for (l, r) in zip(zero_left, one_right[1:]):
		ans = min(ans, l + r)
	print(ans)


for _ in range(int(input())):
	s = input()
	count1 = 0
	count0 = 0
	countlist = []
	for i in range(len(s)):
		if s[i] == '1':
			count1 += 1
		else:
			count0 += 1
	llist = []
	a1 = 0
	b0 = 0
	for i in range(len(s)):
		if s[i] == '0':
			l = min(a1 + count0 - b0, count1 - a1 + b0)
			llist.append(l)
			b0 += 1
		else:
			llist.append(min(b0 + count1 - a1, count0 - b0 + a1))
			a1 += 1
	print(min(llist))


for i in range(int(input())):
	a = input()
	if len(a) == 1:
		print(0)
	else:
		c = 10 ** 10
		for e in range(len(a)):
			z = a[:e].count('0') + a[e:].count('1')
			x = a[:e].count('1') + a[e:].count('0')
			c = min(c, z, x)
		print(c)


for _ in range(int(input())):
	s = input()
	n = len(s)
	a = [0]
	for x in s:
		a += (a[-1] + (x == '0'),)
	b = [0]
	for x in s[::-1]:
		b += (b[-1] + (x == '1'),)
	ans1 = n
	for i in range(n + 1):
		ans1 = min(ans1, a[i] + b[n - i])
	a = [0]
	for x in s:
		a += (a[-1] + (x == '1'),)
	b = [0]
	for x in s[::-1]:
		b += (b[-1] + (x == '0'),)
	ans2 = n
	for i in range(n + 1):
		ans2 = min(ans2, a[i] + b[n - i])
	print(min(ans1, ans2))


import sys
input = sys.stdin.readline

def solve(s):
	l = len(s)
	ans = l
	Acc = [0]
	acc = 0
	for t in s:
		acc += int(t)
		Acc.append(acc)
	total = Acc[-1]
	for i in range(l + 1):
		cnt = Acc[i] + (l - i - (total - Acc[i]))
		ans = min(ans, cnt, l - cnt)
	return ans

def main():
	t = int(input())
	for _ in range(t):
		s = input().strip()
		ans = solve(s)
		sys.stdout.write(str(ans) + '\n')
main()


import sys
input = lambda : sys.stdin.readline().strip()
t = int(input())
while t:
	t -= 1
	s = input()
	zeros_pre = [0] * len(s)
	zeros_suf = [0] * (len(s) + 1)
	one_pre = [0] * len(s)
	one_suf = [0] * (len(s) + 1)
	if s[0] == '0':
		zeros_pre[0] = 1
	else:
		one_pre[0] = 1
	if s[-1] == '0':
		zeros_suf[-2] = 1
	else:
		one_suf[-2] = 1
	for i in range(1, len(s)):
		if s[i] == '0':
			zeros_pre[i] += 1
		zeros_pre[i] += zeros_pre[i - 1]
	for i in range(1, len(s)):
		if s[i] == '1':
			one_pre[i] += 1
		one_pre[i] += one_pre[i - 1]
	for i in range(len(s) - 2, -1, -1):
		if s[i] == '0':
			zeros_suf[i] += 1
		zeros_suf[i] += zeros_suf[i + 1]
	for i in range(len(s) - 2, -1, -1):
		if s[i] == '1':
			one_suf[i] += 1
		one_suf[i] += one_suf[i + 1]
	ans = len(s)
	for i in range(len(s)):
		ans = min(ans, zeros_pre[i] + one_suf[i + 1], one_pre[i] + zeros_suf[i + 1])
	print(ans)


for t in range(int(input())):
	s = input()
	one = s.count('1')
	z = s.count('0')
	if one == 0 or z == 0:
		print(0)
	else:
		cost = one
		mi = 10 ** 9
		for i in s:
			if i == '0':
				cost += 1
			else:
				cost -= 1
			if mi > cost:
				mi = cost
		cost = z
		for i in s:
			if i == '1':
				cost += 1
			else:
				cost -= 1
			if mi > cost:
				mi = cost
		print(mi)


for _ in range(int(input())):
	n = input()
	(one, zero) = (0, 0)
	for i in n:
		if i == '1':
			one += 1
		else:
			zero += 1
	ans = min(one, zero)
	(cur_z, cur_o) = (0, 0)
	for i in n:
		if i == '1':
			cur_o += 1
		else:
			cur_z += 1
		ans = min(ans, cur_z + one - cur_o, cur_o + zero - cur_z)
	print(ans)


from fractions import Fraction
import bisect
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
from queue import Queue

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7

def factors(n):
	return set(reduce(list._add_, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
t = 1
t = iinput()
for _ in range(t):
	s = input()
	s = '*' + s + '*'
	n = len(s)
	zeros = [0] * n
	ones = [0] * n
	for i in range(1, n):
		if s[i] == '0':
			zeros[i] = zeros[i - 1] + 1
		else:
			zeros[i] = zeros[i - 1]
	for i in range(n - 2, -1, -1):
		if s[i] == '1':
			ones[i] = ones[i + 1] + 1
		else:
			ones[i] = ones[i + 1]
	ans = float('inf')
	for i in range(n - 1):
		ans = min(ans, ones[i + 1] + zeros[i])
	zeros = [0] * n
	ones = [0] * n
	for i in range(1, n):
		if s[i] == '1':
			zeros[i] = zeros[i - 1] + 1
		else:
			zeros[i] = zeros[i - 1]
	for i in range(n - 2, -1, -1):
		if s[i] == '0':
			ones[i] = ones[i + 1] + 1
		else:
			ones[i] = ones[i + 1]
	for i in range(n - 1):
		ans = min(ans, ones[i + 1] + zeros[i])
	print(ans)


from sys import stdin, stdout

def subsequence_hate(s):
	n = len(s)
	if n == 1:
		return 0
	la = get_la(s)
	ra = get_ra(s)
	res = 2 ** 31 - 1
	for i in range(1, n):
		res = min(res, la[i - 1][0] + ra[i][1], la[i - 1][1] + ra[i][0], la[i - 1][0] + ra[i][0], la[i - 1][1] + ra[i][1])
	return res

def get_la(s):
	la = [[0, 0] for i in range(len(s))]
	cnt0 = 0
	cnt1 = 0
	for i in range(len(s)):
		if s[i] == '0':
			cnt0 += 1
		else:
			cnt1 += 1
		la[i][0] = cnt0
		la[i][1] = cnt1
	return la

def get_ra(s):
	ra = [[0, 0] for i in range(len(s))]
	cnt0 = 0
	cnt1 = 0
	for i in range(len(s) - 1, -1, -1):
		if s[i] == '0':
			cnt0 += 1
		else:
			cnt1 += 1
		ra[i][0] = cnt0
		ra[i][1] = cnt1
	return ra
t = int(stdin.readline())
for i in range(t):
	s = stdin.readline().strip()
	stdout.write(str(subsequence_hate(s)) + '\n')


for _ in range(int(input())):
	s = input()
	length = len(s)
	zeroCount = 0
	oneCount = 0
	oneDone = 0
	zeroDone = 0
	for i in range(length):
		if s[i] == '0':
			zeroCount += 1
		else:
			oneCount += 1
	minOps = min(zeroCount, oneCount)
	for i in range(length):
		if s[i] == '0':
			zeroDone += 1
		else:
			oneDone += 1
		patternOne = zeroDone + oneCount - oneDone
		patternTwo = oneDone + zeroCount - zeroDone
		mi = min(minOps, patternOne, patternTwo)
		minOps = mi
	print(minOps)


for _ in range(int(input())):
	s = '$' + input()
	n = len(s) - 1
	one = [0] * (n + 1)
	zero = [0] * (n + 1)
	for i in range(1, n + 1):
		if s[i] == '0':
			zero[i] = zero[i - 1] + 1
			one[i] = one[i - 1]
		else:
			zero[i] = zero[i - 1]
			one[i] = one[i - 1] + 1
	ans = 10 ** 12
	for i in range(1, n + 1):
		now1 = i - zero[i]
		now2 = n - i - (one[n] - one[i])
		ans = min(ans, now1 + now2)
		now1 = i - one[i]
		now2 = n - i - (zero[n] - zero[i])
		ans = min(ans, now1 + now2)
	print(ans)


T = int(input())
for t in range(T):
	s = input()
	ot = s.count('1')
	zt = len(s) - ot
	best = len(s)
	(o, z) = (0, 0)
	for c in s:
		if c == '1':
			o += 1
		else:
			z += 1
		best = min(best, o + zt - z, z + ot - o)
	print(best)


import sys
import math
import heapq

def I():
	return int(sys.stdin.readline())

def IL():
	return list(map(int, sys.stdin.readline().strip().split()))

def ILS():
	return list(map(str, sys.stdin.readline().strip().split()))
t = I()
for iii in range(t):
	s = input()
	l = [int(x) for x in s]
	ll = l.copy()
	tot = sum(ll)
	for i in range(1, len(l)):
		l[i] = l[i] + l[i - 1]
	ans = min(tot, len(l) - tot)
	for i in range(len(l)):
		zc = l[i]
		oc = len(l) - i - 1 - (tot - l[i])
		if ans == None:
			ans = zc + oc
		else:
			ans = min(ans, zc + oc)
	for i in range(len(l)):
		oc = i + 1 - l[i]
		zc = tot - l[i]
		if ans == None:
			ans = zc + oc
		else:
			ans = min(ans, zc + oc)
	print(ans)


for _ in range(int(input())):
	s = input()
	ch0 = 0
	ch1 = 0
	ch = [[] for _ in range(1001)]
	for i in range(len(s)):
		ch[i] = [ch0, ch1]
		if int(s[i]) == 0:
			ch0 += 1
		else:
			ch1 += 1
	ch[len(s)] = [ch0, ch1]
	minn = []
	for j in range(len(s) + 1):
		now = ch[j]
		c0 = now[0]
		c1 = now[1]
		minn.append(ch1 - c1 + c0)
		minn.append(ch0 - c0 + c1)
	print(min(minn))


from math import *
t = int(input())

def solve(s):
	a = [0 for _ in s]
	acc = 0
	n = len(s)
	for i in range(len(s)):
		acc += s[i] == '1'
		a[i] = acc
	ans = min(len(s) - a[n - 1], a[n - 1])
	for i in range(len(s)):
		cura = a[i] + n - i - 1 - (a[n - 1] - a[i])
		curb = 1 + i - a[i] + a[n - 1] - a[i]
		ans = min(ans, cura, curb)
	return ans
for _ in range(t):
	print(solve(input()))


import sys
for t in range(int(input())):
	s = input().strip()
	a = []
	for i in s:
		if i == '0':
			a.append(0)
		else:
			a.append(1)
	n = len(s)
	ans = 1000
	pfsums0 = [0 for i in range(len(s))]
	pfsums1 = [0 for i in range(len(s))]
	pfsums0[0] = 1 - a[0]
	pfsums1[0] = a[0]
	for i in range(1, len(a)):
		pfsums0[i] += pfsums0[i - 1] + (1 - a[i])
		pfsums1[i] += pfsums1[i - 1] + a[i]
	for i in range(len(s)):
		ans1 = pfsums0[i] + pfsums1[n - 1] - pfsums1[i]
		ans2 = pfsums1[i] + pfsums0[n - 1] - pfsums0[i]
		ans = min(ans, ans1, ans2)
	ans = min(pfsums1[n - 1], pfsums0[n - 1], ans)
	print(ans)


for x in range(int(input())):
	s = input()
	m = len(s)
	for i in range(len(s)):
		s1 = s[i:]
		n1 = s1.count('1')
		s2 = s[:i]
		n2 = s2.count('1')
		k1 = min(i - n2, n2)
		k2 = min(len(s) - i - n1, n1)
		m = min(k1 + k2, m)
	print(m)


from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def LS():
	return [list(x) for x in sys.stdin.readline().split()]

def S():
	res = list(sys.stdin.readline())
	if res[-1] == '\n':
		return res[:-1]
	return res

def IR(n):
	return [I() for i in range(n)]

def LIR(n):
	return [LI() for i in range(n)]

def SR(n):
	return [S() for i in range(n)]

def LSR(n):
	return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
	t = I()
	for _ in range(t):
		s = list(map(int, input()))
		n = len(s)
		cnt = [0]
		cnt_ = [0]
		for i in range(n):
			cnt.append(cnt[-1] + s[i])
			cnt_.append(cnt_[-1] + 1 - s[i])
		ans = float('inf')
		for i in range(n + 1):
			ans = min(ans, cnt[i] + cnt_[n] - cnt_[i], cnt_[i] + cnt[n] - cnt[i])
		print(ans)
	return
solve()


T = int(input())

def solve():
	s = input()
	changes = float('inf')
	for j in range(len(s)):
		a = s[:j].count('1')
		b = s[:j].count('0')
		c = s[j:].count('1')
		d = s[j:].count('0')
		changes = min(changes, b + c, a + d)
	print(changes)
for _ in range(T):
	solve()


for t in range(int(input())):
	a = input()
	n = len(a)
	cuma_fd = [0] * (n + 1)
	cuma_bd = [0] * (n + 1)
	cu = 0
	cd = 0
	d = 1000000000.0
	for i in range(n):
		cu += [0, 1][int(a[i])]
		cuma_fd[i + 1] = cu
		cd += [1, 0][int(a[n - i - 1])]
		cuma_bd[n - i - 1] = cd
	for i in range(n + 1):
		d = min(abs(cuma_fd[i] + cuma_bd[i]), d)
	cu = 0
	cd = 0
	for i in range(n):
		cu += [1, 0][int(a[i])]
		cuma_fd[i + 1] = cu
		cd += [0, 1][int(a[n - i - 1])]
		cuma_bd[n - i - 1] = cd
	for i in range(n + 1):
		d = min(abs(cuma_fd[i] + cuma_bd[i]), d)
	print(d)


for q in range(int(input())):
	line = input()
	n = len(line)
	if n == 1:
		print(0)
	else:
		cou0 = 0
		pr0 = [0]
		for el in line:
			if el == '0':
				cou0 += 1
			pr0.append(cou0)
		cou1 = n - cou0
		ans = int(1e+20)
		for i in range(n):
			ans = min(ans, pr0[i] + (n - i) - (cou0 - pr0[i]))
			ans = min(ans, i - pr0[i] + (cou0 - pr0[i]))
		print(ans)


from sys import stdin
from math import ceil
input = stdin.readline
for _ in range(int(input())):
	s = input()
	n = len(s) - 1
	ans = 10000000
	(a, b) = (0, s.count('1'))
	for i in range(n):
		if s[i] == '1':
			b -= 1
		else:
			a += 1
		ans = min(ans, a + b, i - a + 1 + n - i - 1 - b)
	print(ans)


t = int(input())
for z in range(t):
	str = input()
	n = len(str)
	(a, b) = ([0 for i in range(n)], [0 for i in range(n)])
	(x, y) = ([0 for i in range(n)], [0 for i in range(n)])
	(ta, tb) = (0, 0)
	for i in range(n):
		if str[i] == '1':
			ta += 1
		else:
			tb += 1
		a[i] = ta
		b[i] = tb
	(ta, tb) = (0, 0)
	for i in range(n - 1, -1, -1):
		if str[i] == '1':
			ta += 1
		else:
			tb += 1
		x[i] = ta
		y[i] = tb
	ans = min(ta, tb)
	for i in range(n - 1):
		temp = min(a[i] + y[i + 1], b[i] + x[i + 1])
		ans = min(ans, temp)
	print(ans)


for _ in range(int(input())):
	s = input()
	ar = [0] * len(s)
	br = [0] * len(s)
	cr = [0] * len(s)
	dr = [0] * len(s)
	for i in range(len(s)):
		if i == 0:
			if s[i] == '0':
				ar[i] += 1
			else:
				br[i] += 1
		else:
			if s[i] == '0':
				ar[i] += 1
			else:
				br[i] += 1
			ar[i] += ar[i - 1]
			br[i] += br[i - 1]
	for i in range(len(s) - 1, -1, -1):
		if i == len(s) - 1:
			if s[i] == '0':
				cr[i] += 1
			else:
				dr[i] += 1
		else:
			if s[i] == '0':
				cr[i] += 1
			else:
				dr[i] += 1
			cr[i] += cr[i + 1]
			dr[i] += dr[i + 1]
	ans = 10000000000
	for i in range(len(s)):
		ans = min(ans, ar[i] + dr[i] - 1)
		ans = min(ans, br[i] + cr[i] - 1)
	print(ans)


t = int(input())
for test in range(0, t):
	s = input()
	n1 = 0
	n0 = 0
	n_min = -1
	n0_total = s.count('0')
	n1_total = s.count('1')
	prev_1 = 0
	prev_0 = 0
	prev_1_inv = n1_total
	prev_0_inv = n0_total
	for i in range(0, len(s)):
		if s[i] == '1':
			if n_min == -1:
				n_min = 1 + prev_1 + prev_0_inv
			else:
				n_min = min(1 + prev_1 + prev_0_inv, n_min)
			prev_1 += 1
		else:
			if n_min == -1:
				n_min = prev_1 + prev_0_inv - 1
			else:
				n_min = min(prev_1 + prev_0_inv - 1, n_min)
			prev_0_inv -= 1
	for i in range(0, len(s)):
		if s[i] == '1':
			if n_min == -1:
				n_min = prev_0 + prev_1_inv - 1
			else:
				n_min = min(prev_0 + prev_1_inv - 1, n_min)
			prev_1_inv -= 1
		else:
			if n_min == -1:
				n_min = prev_0 + prev_1_inv + 1
			else:
				n_min = min(prev_0 + prev_1_inv + 1, n_min)
			prev_0 += 1
	print(n_min)


t = int(input())
while t > 0:
	s = list(input())
	n = len(s)
	so = 0
	s1 = 0
	ans = 10 ** 6
	for i in s:
		if i == '0':
			so += 1
		else:
			s1 += 1
	ans = min(so, s1)
	do = 0
	d1 = 0
	for i in range(n):
		if s[i] == '0':
			do += 1
		else:
			d1 += 1
		ans = min(ans, do + (s1 - d1))
		ans = min(ans, d1 + (so - do))
	print(ans)
	t -= 1


for _ in range(int(input())):
	n = input()
	ans = 100000000.0
	for i in range(len(n)):
		prezero = n[:i].count('0') + n[i:].count('1')
		preone = n[:i].count('1') + n[i:].count('0')
		ans = min(ans, prezero, preone)
	print(ans)


from sys import stdin

def iinput():
	return int(stdin.readline())

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))
t = iinput()
while t:
	t -= 1
	s = input()
	z_tot = s.count('0')
	o_tot = s.count('1')
	ans = min(z_tot, o_tot)
	(z_pref, o_pref) = (0, 0)
	for e in s:
		if e == '0':
			z_pref += 1
			z_tot -= 1
		else:
			o_pref += 1
			o_tot -= 1
		ans = min(ans, o_pref + z_tot)
		ans = min(ans, z_pref + o_tot)
	print(ans)


t = int(input())
while t:
	t -= 1
	str = [int(s) for s in input()]
	if str[0] == 0:
		number = [0]
		count = [1]
	if str[0] == 1:
		number = [1]
		count = [1]
	count0 = 0
	count1 = 0
	for i in str:
		if i == 1:
			count1 += 1
		else:
			count0 += 1
	for i in range(1, len(str)):
		if str[i] == str[i - 1]:
			count[-1] = count[-1] + 1
		else:
			number.append(str[i])
			count.append(1)
	dp = []
	for i in range(len(number)):
		cur_count = 0
		for j in range(i, len(number)):
			if number[j] == 0:
				cur_count = cur_count + count[j]
		for k in range(0, i):
			if number[k] == 1:
				cur_count = cur_count + count[k]
		dp.append(cur_count)
	dp_1 = []
	for i in range(len(number)):
		cur_count = 0
		for j in range(i, len(number)):
			if number[j] == 1:
				cur_count = cur_count + count[j]
		for k in range(0, i):
			if number[k] == 0:
				cur_count = cur_count + count[k]
		dp_1.append(cur_count)
	print(min(min(dp), min(dp_1)))


t = int(input())
for _ in range(t):
	stri = input()
	n = len(stri)
	total_0 = 0
	total_1 = 0
	for i in stri:
		if i == '0':
			total_0 += 1
		else:
			total_1 += 1
	zeros = [0] * n
	ones = [0] * n
	rem_zeros = [0] * n
	rem_ones = [0] * n
	ans = []
	curr_0 = 0
	curr_1 = 0
	for i in range(n):
		if stri[i] == '0':
			zeros[i] = curr_0 + 1
			curr_0 += 1
			ones[i] = curr_1
		elif stri[i] == '1':
			ones[i] = curr_1 + 1
			curr_1 += 1
			zeros[i] = curr_0
	for i in range(n):
		rem_ones[i] = total_1 - ones[i]
		rem_zeros[i] = total_0 - zeros[i]
	for i in range(n):
		ans.append(rem_ones[i] + zeros[i])
		ans.append(rem_zeros[i] + ones[i])
	print(min(ans))