You are given a string containing characters $\mbox{A}$ and $\mbox{B}$ only.  Your task is to change it into a string such that there are no matching adjacent characters. To do this, you are allowed to delete zero or more characters in the string.  

Your task is to find the minimum number of required deletions.

Example 

$\textbf{s}=\textit{AABAAB}$  

Remove an $\mbox{A}$ at positions $0$ and $3$ to make $\textbf{s}=\textit{ABAB}$ in $2$ deletions.

Function Description

Complete the alternatingCharacters function in the editor below.  

alternatingCharacters has the following parameter(s):

string s: a string  

Returns  

int: the minimum number of deletions required

Input Format

The first line contains an integer $\textit{q}$, the number of queries. 

The next $\textit{q}$ lines each contain a string $\boldsymbol{\mathrm{~S~}}$ to analyze.

Constraints

$1\leq q\leq10$  
$1\leq\:\text{length of s}\:\leq10^5$
Each string $\boldsymbol{\mathrm{~S~}}$ will consist only of characters $\mbox{A}$ and $\mbox{B}$.

Sample Input
5
AAAA
BBBBB
ABABABAB
BABABA
AAABBB

Sample Output
3
4
0
0
4

Explanation

The characters marked red are the ones that can be deleted so that the string does not have matching adjacent characters.

Source: hackerrank | Difficulty: EASY
Tags: String algorithms


Solutions:
def f(s):
	return sum((1 for (c1, c2) in zip(s, s[1:]) if c1 == c2))
t = int(input())
for _ in range(t):
	print(f(input()))


t = int(input())
for i in range(t):
	x = input()
	count = 0
	for i in range(1, len(x)):
		if x[i - 1] == x[i]:
			count += 1
	print(count)


tcs = int(input())
for tc in range(tcs):
	string = input()
	total = 0
	curr = string[0]
	for i in range(1, len(string)):
		if curr == string[i]:
			total += 1
		else:
			curr = string[i]
	print(total)


T = int(input())
for _ in range(T):
	S = input()
	(p, count) = (None, 0)
	for s in S:
		if s == p:
			count += 1
		p = s
	print(count)


import itertools, sys
T = int(sys.stdin.readline())
for _ in range(T):
	S = sys.stdin.readline().strip()
	print(len(S) - len(list(itertools.groupby(S))))


T = int(input())
for Ti in range(T):
	s = input()
	lch = ''
	count = 0
	for ch in s:
		if ch == lch:
			count += 1
			continue
		lch = ch
	print(count)


tests = int(input())
for _ in range(tests):
	s = input()
	deletions = 0
	prev = 'Q'
	for c in s:
		if c != prev:
			prev = c
		else:
			deletions += 1
	print(deletions)


from itertools import groupby
for _ in range(int(input())):
	print(sum((len(tuple(c)) - 1 for (_, c) in groupby(input().strip()))))


v = int(input())
for l in range(0, v):
	x = input()
	b = 0
	for i in range(0, len(x) - 1):
		if x[i] == x[i + 1]:
			i = i + 1
			b = b + 1
	print(b)


def solve(S):
	charsToDelete = 0
	lastChar = ''
	for x in S:
		if x == lastChar:
			charsToDelete += 1
		lastChar = x
	return charsToDelete
t = int(input())
for i in range(t):
	print(solve(input()))


def get_input():
	string_array = []
	num_tests = int(input())
	for test in range(num_tests):
		string_array.append(input())
	return string_array

def alternate_characters(string_array):
	for string in string_array:
		num_deletions = 0
		if len(string) == 1:
			return num_deletions
		else:
			for index in range(1, len(string)):
				if string[index - 1] == string[index]:
					num_deletions += 1
		print(num_deletions)

def run_test():
	string_array = get_input()
	alternate_characters(string_array)
run_test()


t = int(input())
for j in range(t):
	s = input().strip()
	ans = 0
	n = len(s)
	for i in range(n - 1):
		if s[i] == s[i + 1]:
			ans += 1
	print(ans)


def solve(s):
	prev = s[0]
	d = 0
	for c in s[1:]:
		if c != prev:
			prev = c
			continue
		d += 1
	return d
T = int(input())
for _ in range(T):
	print(solve(input()))


cases = int(input())
for case in range(cases):
	count = 0
	tb = [str(x) for x in input()]
	for k in range(len(tb)):
		if k != 0:
			if ord(tb[k - 1]) == ord(tb[k]):
				count += 1
	print(count)


import sys
lines = [line.strip() for line in sys.stdin]
for test in lines[1:]:
	data = [x for x in test]
	count = 0
	for i in range(len(data) - 1):
		if data[i] == data[i + 1]:
			count += 1
	print(count)


t = int(input())
l = []
for i in range(0, t):
	x = input()
	l.append(x)
for s in l:
	c = 0
	for j in range(0, len(s) - 1):
		if s[j + 1] == s[j]:
			c = c + 1
		else:
			continue
	print(c)


import re
t = int(input())
for i in range(t):
	c = 0
	s = input()
	m = re.findall('A{2,}|B{2,}', s)
	for j in range(len(m)):
		c += len(m[j]) - 1
	print(c)


import sys
_ = next(sys.stdin)
for seq in sys.stdin:
	seq = seq.strip()
	deletions = 0
	prev = False
	for c in seq:
		if not prev:
			prev = c
			continue
		elif c == prev:
			deletions += 1
		prev = c
	print(deletions)


num = int(input())
for i in range(num):
	mystr = input()
	l = len(mystr)
	deletion = 0
	for j in range(1, l):
		if mystr[j] == mystr[j - 1]:
			deletion = deletion + 1
	print(deletion)


from itertools import groupby
for _ in range(int(input())):
	s = input()
	tot_del = 0
	m = [list(g) for (k, g) in groupby(s)]
	for x in m:
		tot_del += len(x) - 1
	print(tot_del)


lines = int(input())
stru = []
for x in range(lines):
	stru.append(input())
t = 0
while t < lines:
	x = stru[t]
	t = t + 1
	y = 0
	dv = 0
	maxlen = len(x)
	while y < maxlen - 1:
		if x[y] == x[y + 1]:
			dv = dv + 1
		y = y + 1
	print(dv)


N = int(input())
s = []
for i in range(N):
	count = 0
	s.append(input())
	a = list(s[i])
	for j in range(1, len(a)):
		if a[j - 1] == a[j]:
			count += 1
	print(count)


T = int(input())
for k in range(T):
	st = str(input())
	ct = 0
	for j in range(len(st) - 1):
		if st[j] != st[j + 1]:
			ct += 1
	print(len(st) - (ct + 1))


nbTestCase = int(input())
i = 0
while i < nbTestCase:
	line = input()
	a = 0
	j = 0
	while j < len(line) - 1:
		if line[j] == line[j + 1]:
			a += 1
		j += 1
	print(a)
	i += 1


n = int(input())
for i in range(0, n):
	y = list(input())
	count = 0
	for i in range(0, len(y) - 1):
		if y[i] == y[i + 1]:
			count += 1
	print(count)


def main():
	test_cases = input()
	for cases in range(int(test_cases)):
		line = input()
		prev_letter = ''
		del_count = 0
		for letter in line:
			if letter != prev_letter:
				prev_letter = letter
			else:
				del_count += 1
		print(del_count)
main()


T = int(input())
s = [input() for x in range(T)]

def check_min_symb_del(s):
	prev_char = ''
	new_string = ''
	res = 0
	for char in s:
		if char == prev_char:
			res += 1
		prev_char = char
	return res
for string in s:
	print(check_min_symb_del(string))


t = int(input())
for i in range(t):
	text = input()
	final_text = text
	count_i = 0
	for x in range(len(text)):
		if x == len(text) - 1:
			print(count_i)
			break
		elif text[x] == text[x + 1]:
			count_i += 1


test_cases = []
for n in range(int(input())):
	test_cases.append(input())
for test_case in test_cases:
	deletions = 0
	for i in range(1, len(test_case)):
		if test_case[i] == test_case[i - 1]:
			deletions += 1
	print(deletions)


n = int(input())
for _ in range(0, n):
	str = input()
	result = 0
	last = str[0]
	for c in str[1:]:
		if c == last:
			result = result + 1
		else:
			last = c
	print(result)


def main():
	number_of_cases = int(input())
	for test_case in range(number_of_cases):
		test_string = list(str(input()))
		deletion_count = 0
		for i in range(len(test_string) - 1):
			if test_string[i] == test_string[i + 1]:
				deletion_count += 1
		print(deletion_count)
main()


num = int(input())
lis = []
for s in range(num):
	i = input()
	lis.append(i)
for string in lis:
	total = 0
	z = 1
	while z < len(string):
		if string[z] == string[z - 1]:
			total += 1
		z += 1
	print(total)


for _ in range(int(input())):
	count = 0
	toggle_tag = None
	for c in input():
		if c != toggle_tag:
			toggle_tag = c
		else:
			count += 1
	print(count)


def deletion(x):
	n = 0
	m = 0
	temp = ' '
	for i in x:
		m = m + 1
		if i != temp:
			n = n + 1
		temp = i
	return m - n

def main():
	n = int(input(''))
	a = [''] * n
	for i in range(n):
		a[i] = input('')
		a[i] = deletion(a[i])
	for i in range(n):
		print(a[i])
main()


def repeat_chomper(str_in):
	curr_char = str_in[0]
	deletion_count = 0
	for i in range(1, len(str_in)):
		if str_in[i] == curr_char:
			deletion_count += 1
		else:
			curr_char = str_in[i]
	print(deletion_count)
n = int(input())
for i in range(n):
	repeat_chomper(input())


T = int(input())
for i in range(T):
	string = list(input())
	count = 0
	for i in range(len(string) - 1):
		if string[i] == string[i + 1]:
			count += 1
	print(count)


T = int(input())
for i in range(0, T):
	list1 = list(input())
	deletions = 0
	count = 0
	while count < len(list1) - 1:
		if list1[count] == list1[count + 1]:
			deletions += 1
		else:
			pass
		count += 1
	print(deletions)


for case in range(int(input())):
	string = input()
	res = 0
	if len(string) != 0:
		for n in range(1, len(string)):
			if string[n] == string[n - 1]:
				res += 1
	print(str(res))


N = int(input())
for i in range(N):
	s = input()
	lastchar = s[0]
	cnt = 0
	for char in s[1:]:
		if char == lastchar:
			cnt += 1
		else:
			lastchar = char
	print(cnt)


tCases = int(input(''))
for i in range(tCases):
	lastChar = '0'
	tString = input('')
	deletions = 0
	for c in tString:
		if lastChar == c:
			deletions = deletions + 1
		lastChar = c
	print(deletions)


n = int(input())
for i in range(n):
	s = input()
	lc = None
	count = 0
	for c in s:
		if c == lc:
			count += 1
		lc = c
	print(count)


number_of_cases = int(input())
for x in range(number_of_cases):
	case = str(input())
	deletions = 0
	for letter_index in range(len(case) - 1):
		if case[letter_index] == case[letter_index + 1]:
			deletions += 1
	print(deletions)


inputs = int(input())
for i in range(inputs):
	case = str(input())
	deletions = 0
	for y in range(len(case) - 1):
		if case[y] == case[y + 1]:
			deletions += 1
	print(deletions)


for _ in range(int(input())):
	inp = input()
	s = 0
	curr = 0
	for i in range(1, len(inp)):
		if inp[i] == inp[i - 1]:
			curr += 1
		else:
			s += curr
			curr = 0
	s += curr
	print(s)


t = int(input())
for i in range(t):
	counter = 0
	s = list(input())
	for j in range(1, len(s)):
		if s[j] == s[j - 1]:
			counter += 1
	print(counter)


def main():
	nbString = int(input())
	while nbString:
		string = input()
		deletions = 0
		for i in range(len(string) - 1):
			if string[i] == string[i + 1]:
				deletions += 1
		print(deletions)
		nbString -= 1
main()


def solve():
	N = int(input())
	for i in range(N):
		line = list(input())
		deletions = 0
		(i, j) = (0, 1)
		while i < len(line) and j < len(line):
			if line[i] != line[j]:
				i = j
				j = i + 1
			else:
				j += 1
				deletions += 1
		print(deletions)
solve()


T = int(input())
for i in range(T):
	word = input()
	prev = ''
	count = 0
	for letter in word:
		if letter == prev:
			count = count + 1
		prev = letter
	print(count)


def count_stuff(string):
	return sum((1 if a == b else 0 for (a, b) in zip(string, string[1:])))
tests_num = int(input())
tests = [input() for _ in range(tests_num)]
for t in tests:
	print(count_stuff(t))


for t in [1] * int(input()):
	s = input()
	curLetter = s[0]
	count = 0
	for i in range(1, len(s)):
		if s[i] != curLetter:
			curLetter = s[i]
		else:
			count += 1
	print(count)


def count(stri):
	c = 0
	for i in range(1, len(stri)):
		if stri[i] == stri[i - 1]:
			c += 1
	return c
l = int(input())
lst = list()
for i in range(l):
	print(count(input()))


def string_delete(n):
	length = len(n)
	num_del = 0
	for j in range(1, length):
		if j == n:
			break
		elif n[j] == n[j - 1]:
			num_del += 1
	return num_del
num_of_str = int(input())
for i in range(0, num_of_str):
	string = input()
	del_times = string_delete(string)
	print(del_times)


def minDel(s):
	i = 0
	s1 = s[0]
	for si in s[1:]:
		if si == s1:
			i += 1
		else:
			s1 = si
	return i
t = int(input())
for i in range(t):
	print(minDel(input()))


for i in range(int(input())):
	s = input()
	temp = 0
	for j in range(len(s)):
		try:
			if s[j] == s[j + 1]:
				temp += 1
		except IndexError:
			pass
	print(temp)


a = input()
q = []
for e in a:
	q.append(ord(e) - 48)
r = q[::-1]
i = 1
c = 0
for e in r:
	c += i * e
	i *= 10
while c > 0:
	d = 0
	last = ''
	temp = input()
	for e in temp:
		if e == last:
			d += 1
		else:
			last = e
	print(d)
	c -= 1


for _ in range(int(input())):
	d = 0
	p = ''
	for c in input():
		if p == c:
			d += 1
		else:
			p = c
	print(d)


def shashank(ab_string):
	deletions = 0
	for i in range(0, len(ab_string) - 1):
		if ab_string[i] == ab_string[i + 1]:
			deletions += 1
	return deletions
test_cases = int(input())
while 0 < test_cases:
	string = str(input())
	print(shashank(string))
	test_cases -= 1


T = int(input())
for i in range(T):
	change_needed = 0
	word = str(input())
	for letter in range(len(word) - 1):
		if word[letter] == word[letter + 1]:
			change_needed += 1
	print(change_needed)


number_inputs = int(input())
for i in range(number_inputs):
	line = input()
	repeats = 0
	last_char = 'C'
	for letter in line:
		if letter != last_char:
			last_char = letter
		else:
			repeats += 1
	print(repeats)


from itertools import groupby
T = int(input())
for _ in range(T):
	word = input()
	c = len(list(groupby(word)))
	print(len(word) - c)


def ac(string):
	delCount = 0
	for i in range(len(string) - 1):
		if string[i] == string[i + 1]:
			delCount += 1
	return delCount
t = int(input())
strings = []
for i in range(t):
	strings.append(input())
for s in strings:
	print(ac(s))


def main():
	n = int(input())
	a = 0
	list = [[' ']] * n
	for i in range(n):
		list[i] = input()
	for m in range(n):
		counta = list[m].count('A')
		countb = list[m].count('B')
		for k in range(counta + countb - 1):
			if list[m][k] == list[m][k + 1]:
				a = a + 1
		print(a)
		a = 0
main()


__author__ = 'Hrishikesh'
t = int(input())
for i in range(0, t):
	s = input()
	count = 0
	for c in range(0, len(s) - 1):
		if s[c] == s[c + 1]:
			count += 1
	print(count)


import sys
T = int(sys.stdin.readline())
for i in range(T):
	tmp = str(sys.stdin.readline())
	count = 0
	flag = 2
	for char in tmp:
		if char == 'A' and flag == 0:
			count += 1
		elif char == 'B' and flag == 1:
			count += 1
		elif char == 'A':
			flag = 0
		else:
			flag = 1
	print(count)


test = int(input())
for k in range(0, test):
	str = input()
	prevChar = ' '
	newStr = ''
	de = 0
	for i in str:
		if i != prevChar:
			newStr += i
			prevChar = i
		else:
			de += 1
	print(de)


def req_steps(ab):
	_ab = 0
	for k in range(0, len(ab) - 1):
		if ab[k + 1] == ab[k]:
			_ab += 1
	return _ab
mylist = []
x = int(input())
for i in range(0, x):
	mylist.append(input())
for j in mylist:
	_x = req_steps(j)
	print(_x)


def alternate(string):
	(i, count) = (0, 0)
	current = string[0]
	while i < len(string):
		if string[i] == current:
			count += 1
		else:
			current = string[i]
		i += 1
	return count - 1
tests = int(input())
for i in range(tests):
	print(alternate(str(input())))


t = int(input())
for _ in range(t):
	word = input()
	dels = 0
	char = word[0]
	i = 1
	while i < len(word):
		if char == word[i]:
			word = word[:i] + word[i + 1:]
			dels += 1
		else:
			char = word[i]
			i += 1
	print(dels)


times = int(input())

def test(stri):
	num = 0
	for (key, val) in enumerate(stri):
		if key + 1 < len(stri) and stri[key] == stri[key + 1]:
			num = int(num) + 1
	return num
for i in range(times):
	print(test(input()))


cnt = int(input())
for i in range(0, cnt):
	s = list(input())
	lc = None
	d = 0
	for c in s:
		if lc == None:
			lc = c
			continue
		if lc == c:
			d += 1
		lc = c
	print(d)


def cdel(L):
	L = list(L)
	i = 1
	count = 0
	while i < len(L):
		if L[i - 1] == L[i]:
			count += 1
			i += 1
		else:
			i += 1
	return count
n = int(input())
for i in range(n):
	print(cdel(input()))


def solve(s):
	res = len(s)
	prev = 'X'
	for ch in s:
		if ch != prev:
			res -= 1
			prev = ch
	return res
import sys
cT = int(sys.stdin.readline())
for t in range(cT):
	s = sys.stdin.readline().strip()
	print(solve(s))


t = int(input())
for i in range(t):
	s = input()
	j = 0
	answer = 0
	while j <= len(s):
		if len(s) == j + 1:
			break
		if s[j] == s[j + 1]:
			answer += 1
		j += 1
	print(answer)


for t in range(int(input())):
	s = input()
	curchar = s[0]
	dels = 0
	for (index, char) in enumerate(s[1:]):
		i = index + 1
		if char == curchar:
			dels += 1
		curchar = char
	print(dels)


def alternate(s):
	prev = s[0]
	count = 0
	for char in s[1:]:
		if char == prev:
			count += 1
		else:
			prev = char
	return count
t = int(input())
for _ in range(t):
	print(alternate(input()))


def Shashank(word):
	a = None
	d = 0
	for i in word:
		if a == i:
			d += 1
		else:
			a = i
	return d
num1 = int(input())
for i in range(num1):
	res = Shashank(str(input()))
	print(res)


T = int(input())

def alternating(s):
	for i in range(len(s) - 1):
		if s[i] == s[i + 1]:
			return False
	return True

def make_alternating(s):
	count = 0
	for i in range(len(s) - 1):
		if s[i] == s[i + 1]:
			count += 1
		else:
			pass
	return count
for i in range(T):
	s = input()
	if len(set(s)) == 1:
		print(len(s) - 1)
	elif alternating(s):
		print(0)
	else:
		print(make_alternating(s))


number_of_tests = int(input())
for x in range(number_of_tests):
	number_of_deletions = 0
	word = input()
	length = len(word)
	for y in range(length - 1):
		if word[y] == word[y + 1]:
			number_of_deletions += 1
	print(number_of_deletions)


T = int(input())
for i in range(T):
	count = 0
	inp = input()
	for j in range(len(inp) - 1):
		if inp[j] == inp[j + 1]:
			count += 1
	print(count)


def numDeletions(l):
	lastChar = None
	output = 0
	for r in l:
		if r != lastChar:
			lastChar = r
		else:
			output += 1
	return output
a = int(input())
for num in range(0, a):
	stringList = input()
	print(numDeletions(stringList))


import re
for case in range(int(input())):
	print(sum((len(s) - 1 for s in re.findall('A+|B+', input()))))


test_count = int(input())
for i in range(0, test_count):
	text = input()
	count = 0
	for j in range(1, len(text)):
		if text[j] == text[j - 1]:
			count = count + 1
	print(count)


x = []
for i in range(0, int(input())):
	x.append(list(input()))
for i in x:
	rep = 0
	prev = ''
	for j in i:
		if prev == j:
			rep += 1
		prev = j
	print(rep)


t = int(input())
for _ in range(t):
	s = str(input())
	minDel = 0
	cur = 0
	for i in range(1, len(s)):
		if s[cur] != s[i]:
			cur = i
		else:
			minDel += 1
	print(minDel)


def find_min_deletion():
	T = int(input())
	for i in range(T):
		string = input()
		length = len(string)
		cost = 0
		for i in range(1, length):
			currentchar = string[i - 1]
			nextchar = string[i]
			if currentchar == nextchar:
				cost = cost + 1
		print(cost)
find_min_deletion()


def minDeletion(str):
	numDeletes = 0
	currChar = str[0]
	for i in range(1, len(str)):
		if currChar == str[i]:
			numDeletes = numDeletes + 1
		else:
			currChar = str[i]
	return numDeletes
T = int(input())
for i in range(T):
	testStr = input()
	print(minDeletion(testStr))


t = int(input())
while t > 0:
	t = t - 1
	s = input()
	l = len(s)
	c = 0
	for i in range(l - 1):
		if s[i] == s[i + 1]:
			c = c + 1
	print(c)


T = int(input())
for case in range(T):
	msg = list(input())
	count = 0
	for i in range(1, len(msg)):
		if msg[i] == msg[i - 1]:
			count += 1
	print(count)


t = int(input())
while t > 0:
	count = 0
	t = t - 1
	s = input()
	for i in range(len(s) - 1):
		if s[i] == s[i + 1]:
			count = count + 1
	print(count)


N = int(input())
for n in range(N):
	inp = input()
	delete = 0
	while len(inp) > 0:
		if inp[0] == 'A':
			delete += len(inp) - len(inp.lstrip('A')) - 1
			inp = inp.lstrip('A')
		else:
			delete += len(inp) - len(inp.lstrip('B')) - 1
			inp = inp.lstrip('B')
	print(delete)


for _ in range(int(input())):
	word = str(input())
	total = 0
	for i in range(len(word) - 1):
		if word[i + 1] == word[i]:
			total += 1
	print(total)


def parse_input():
	num_tests = int(input())
	tests = []
	for test in range(num_tests):
		tests.append(str(input()))
	return tests

def del_chars(a_str):
	temp_char = ''
	num_del = 0
	for char in a_str:
		if char == temp_char:
			num_del += 1
		temp_char = char
	return num_del
tests = parse_input()
for test in tests:
	print(del_chars(test))


T = int(input())
for i in range(T):
	x = input()
	count = 0
	for j in range(1, len(x)):
		if x[j] == x[j - 1]:
			count += 1
	print(count)


def sol_old(s):
	s = list(s)
	i0 = 0
	i1 = 1
	count = 0
	while i1 < len(s):
		if s[i1] == s[i0]:
			del s[i1]
			count = count + 1
		else:
			i1 = i1 + 1
			i0 = i0 + 1
	return count

def sol(s):
	s = list(s)
	count = 0
	while len(s) > 1:
		e = s.pop()
		if e == s[-1]:
			count += 1
	return count
n_test = int(input())
for n in range(n_test):
	print(sol(input()))


def getMinElims(string):
	currentLetter = string[0]
	elims = 0
	for letter in string[1:]:
		if currentLetter == letter:
			elims = elims + 1
		elif currentLetter != letter:
			currentLetter = letter
	return elims
cases = int(input())
for i in range(cases):
	currentString = input()
	print(str(getMinElims(currentString)))


T = int(input())
for t in range(T):
	i = input()
	x = i[0]
	res = 0
	for n in i[1:]:
		if n == x:
			res += 1
		else:
			x = n
	print(res)


from itertools import groupby

def main():
	n = int(input())
	for _ in range(n):
		line = input()
		answer = sum((len(list(g)) - 1 for (k, g) in groupby(line)))
		print(answer)
main()


def consec(str):
	count = 0
	for i in range(1, len(str)):
		if str[i] == str[i - 1]:
			count += 1
	return count
howmany = int(input())
i = 0
while i < howmany:
	stri = str(input())
	print(consec(stri))
	i += 1


def min_deletions(s):
	return sum([s[x] == s[x + 1] for x in range(len(s) - 1)])
test_cases = int(input())
for _ in range(test_cases):
	print(min_deletions(input()))


def countDeletions(s):
	deletions = 0
	previousChar = ''
	for c in s:
		if c == previousChar:
			deletions += 1
		else:
			previousChar = c
	return deletions

def solve():
	testCases = int(input())
	for i in range(testCases):
		s = input()
		print(countDeletions(s))
solve()