Read problems statements in Mandarin Chinese  and Russian. 

Olya works as a warehouse keeper for a T-Shirt factory. Now the factory is facing hard times, so currently they produce only the T-shirts of three kinds: red, green and blue T-Shirts. All the T-shirts are stored in the containers, each of the containers contain the T-Shirts of a single colour.
Now there are N containers at the warehouse, lined up in a line. Let's enumerate the containers by the positive integers from 1 to N, starting from the leftmost and ending at the rightmost one. Their order is described with a string S. Each symbol of this string is either "r", "g" or "b" and denotes the colour of the respective T-shirts, stored in the container.
Olya likes orderliness. She is not satisfied with the fact that different kinds of containers are messed up. So she wants to rearrange the containers in such a way that the number of pairs of adjacent containers that contain the T-shirts of different colors is as minimal as possible.
For doing that, she has a special crane. The crane is capable of doing the following things:

Take a container with the number X and put it in front of all the containers. This operation takes (X-1) seconds. Note that we are considering the 1-dimensional model of the warehouse, so "in front of all the containers" means to the left of all the containers. The warehouse is so large, so you shouldn't worry about its' size and this operation is always performable.
Take a container with the number X and take some container to the left of it (say, the container number Y). Remove the container number X from its' position and insert it right after the container with the number Y. This operation will take X-Y-1 seconds.
Take a container with the number X and take some container to the right of it (say, the container number Y). Remove the container number X from its' position and insert it right after the container with the number Y. This operation will take Y-X seconds.

Note that after the operation, we will re-enumerate the containers from left to right by the positive integers from 1 to N.
 
Though Olya is keen on orderliness, she doesn't way to stay at the warehouse for long on Sunday. So she asks you to help her and to calculate the minimal possible number of seconds that is necessary to rearrange the containers in the desired way.

------ Input ------ 

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.

The first (and only) line of each test case contains a string S, consisting of N symbols denoting the color string corresponding to the containers.

------ Output ------ 

For each test case, output a single line containing the answer to the problem's question for the corresponding test case.

------ Constraints ------ 

$1 ≤ T ≤ 10$
$The string S consists only of the lower-case Latin letters from the set {r, g, b}.$
$(Subtask 1): 1 ≤ N = |S| ≤ 7 - 33 points.$
$(Subtask 2): 1 ≤ N = |S| ≤ 1000 - 33 points.$
$(Subtask 3): 1 ≤ N = |S| ≤ 10^{5} - 34 points.$

----- Sample Input 1 ------ 
4

rgr

rrr

rgb

rgbr
----- Sample Output 1 ------ 
1

0

0

2
----- explanation 1 ------ 
Example case 1.We can move the second container to the beginning of the line. This will take one second.
Example case 2.Containers are already in desired way.
Example case 3.Here also, containers are already in desired way.
Example case 4.You can put first r to the just right of b. It will take 2 seconds to do so.

Difficulty: HARD
Tags: Sorting

Data structures

Range queries

Segment trees revisited
Skill types: Sorting

Data structures

Range queries

Starter code:

Solutions:
def calcans(s, dest, r, g, b):
	rc = 0
	gc = 0
	bc = 0
	ans = 0
	for i in range(len(s)):
		if s[i] == 'r':
			rc += 1
			if g < r:
				ans += gc
			if b < r:
				ans += bc
		if s[i] == 'b':
			bc += 1
			if g < b:
				ans += gc
			if r < b:
				ans += rc
		if s[i] == 'g':
			gc += 1
			if b < g:
				ans += bc
			if r < g:
				ans += rc
	return ans
for _ in range(int(input())):
	s = input()
	gcount = s.count('g')
	rcount = s.count('r')
	bcount = s.count('b')
	ss = [('r' * rcount + 'g' * gcount + 'b' * bcount, 0, 1, 2), ('r' * rcount + 'b' * bcount + 'g' * gcount, 0, 2, 1), ('g' * gcount + 'b' * bcount + 'r' * rcount, 2, 0, 1), ('g' * gcount + 'r' * rcount + 'b' * bcount, 1, 0, 2), ('b' * bcount + 'r' * rcount + 'g' * gcount, 1, 2, 0), ('b' * bcount + 'g' * gcount + 'r' * rcount, 2, 1, 0)]
	ans = float('inf')
	for (des, r, g, b) in ss:
		ans = min(ans, calcans(s, des, r, g, b))
	print(ans)


def helper(s, r, g, b):
	x = y = z = d = 0
	for i in range(len(s)):
		if s[i] == 'r':
			x += 1
			if g > r:
				d += y
			if b > r:
				d += z
		if s[i] == 'g':
			y += 1
			if r > g:
				d += x
			if b > g:
				d += z
		if s[i] == 'b':
			z += 1
			if r > b:
				d += x
			if g > b:
				d += y
	return d
p = [(0, 1, 2), (2, 0, 1), (1, 2, 0), (0, 2, 1), (2, 1, 0), (1, 0, 2)]
t = int(input())
for _ in range(t):
	s = input()
	res = []
	for (r, g, b) in p:
		res.append(helper(s, r, g, b))
	print(min(res))


def inversions(str, r, g, b):
	r_count = g_count = b_count = inv = 0
	for i in range(len(str)):
		if str[i] == 'r':
			r_count += 1
			if g > r:
				inv += g_count
			if b > r:
				inv += b_count
		if str[i] == 'g':
			g_count += 1
			if r > g:
				inv += r_count
			if b > g:
				inv += b_count
		if str[i] == 'b':
			b_count += 1
			if r > b:
				inv += r_count
			if g > b:
				inv += g_count
	return inv
permutation = [(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)]
for _ in range(int(input())):
	str = input()
	ans = []
	for (r, g, b) in permutation:
		ans.append(inversions(str, r, g, b))
	print(min(ans))


t = int(input())
countans5 = 0

def rgb(s, length):
	no_not_rs = 0
	no_not_bs = 0
	no_not_gs = 0
	countans1 = 0
	for i in range(0, length):
		if s[i] != 'r':
			no_not_rs = no_not_rs + 1
		elif s[i] == 'r':
			countans1 = countans1 + no_not_rs
	for i in range(0, length):
		if s[i] == 'b':
			no_not_gs = no_not_gs + 1
		elif s[i] == 'g':
			countans1 = countans1 + no_not_gs
	return countans1

def rbg(s, length):
	no_not_rs = 0
	no_not_bs = 0
	no_not_gs = 0
	countans2 = 0
	for i in range(0, length):
		if s[i] != 'r':
			no_not_rs = no_not_rs + 1
		elif s[i] == 'r':
			countans2 = countans2 + no_not_rs
	for i in range(0, length):
		if s[i] == 'g':
			no_not_bs = no_not_bs + 1
		elif s[i] == 'b':
			countans2 = countans2 + no_not_bs
	return countans2

def brg(s, length):
	no_not_rs = 0
	no_not_bs = 0
	no_not_gs = 0
	countans3 = 0
	for i in range(0, length):
		if s[i] != 'b':
			no_not_bs = no_not_bs + 1
		elif s[i] == 'b':
			countans3 = countans3 + no_not_bs
	for i in range(0, length):
		if s[i] == 'g':
			no_not_rs = no_not_rs + 1
		elif s[i] == 'r':
			countans3 = countans3 + no_not_rs
	return countans3

def bgr(s, length):
	no_not_rs = 0
	no_not_bs = 0
	no_not_gs = 0
	countans4 = 0
	for i in range(0, length):
		if s[i] != 'b':
			no_not_bs = no_not_bs + 1
		elif s[i] == 'b':
			countans4 = countans4 + no_not_bs
	for i in range(0, length):
		if s[i] == 'r':
			no_not_gs = no_not_gs + 1
		elif s[i] == 'g':
			countans4 = countans4 + no_not_gs
	return countans4

def grb(s, length):
	no_not_rs = 0
	no_not_bs = 0
	no_not_gs = 0
	countans5 = 0
	for i in range(0, length):
		if s[i] != 'g':
			no_not_gs = no_not_gs + 1
		elif s[i] == 'g':
			countans5 = countans5 + no_not_gs
	for i in range(0, length):
		if s[i] == 'b':
			no_not_rs = no_not_rs + 1
		elif s[i] == 'r':
			countans5 = countans5 + no_not_rs
	return countans5

def gbr(s, length):
	no_not_rs = 0
	no_not_bs = 0
	no_not_gs = 0
	countans6 = 0
	for i in range(0, length):
		if s[i] != 'g':
			no_not_gs = no_not_gs + 1
		elif s[i] == 'g':
			countans6 = countans6 + no_not_gs
	for i in range(0, length):
		if s[i] == 'r':
			no_not_bs = no_not_bs + 1
		elif s[i] == 'b':
			countans6 = countans6 + no_not_bs
	return countans6
while t != 0:
	s = str(input())
	length = len(s)
	countans1 = rgb(s, length)
	countans2 = rbg(s, length)
	countans3 = brg(s, length)
	countans4 = bgr(s, length)
	countans5 = gbr(s, length)
	countans6 = grb(s, length)
	print(min(countans1, countans2, countans3, countans4, countans5, countans6))
	t = t - 1