wprob_28517
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}.
(Example
Input:
4
rgr
rrr
rgb
rgbr
Output:
1
0
0
2

Explanation
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.
for _ in xrange(input()):
    s = raw_input()
    n = len(s)
    rs = [0 for i in xrange(n)]
    gs = [0 for i in xrange(n)]
    bs = [0 for i in xrange(n)]
    r, g, b = 0, 0, 0
    for i in xrange(n):
        rs[i] = r
        gs[i] = g
        bs[i] = b
        if s[i] == 'r':
            r += 1
        if s[i] == 'g':
            g += 1
        if s[i] == 'b':
            b += 1
            
    rg, rb, gr, gb, br, bg = 0, 0, 0, 0, 0, 0
    for i in xrange(n):
        if s[i] == 'r':
            rg += gs[i]
            rb += bs[i]
        if s[i] == 'g':
            gr += rs[i]
            gb += bs[i]
        if s[i] == 'b':
            br += rs[i]
            bg += gs[i]
    print min(rg + rb + gb, rg + rb + bg, gr + gb + rb, gr + gb + br, br + bg + rg, br + bg + gr)
4
rgr
rrr
rgb
rgbr 4
rfr
rrr
rgb
rgbr 4
rrf
rrr
arg
rfbr 4
rrf
rrr
arg
rfbq 4
rfr
rrr
grb
rgbr 4
frr
rrr
grb
rgbr 4
frr
rrr
brg
rgbr 4
frr
rrr
arg
rgbr 4
rrf
rrr
arg
rgbr 4
rrf
rrr
arg
rfcq 4
rrf
rrr
rag
rfcq 4
rrf
rrr
sag
rfcq 4
rrf
rrr
sag
qcfr 4
rrf
qrr
sag
qcfr 4
rrf
qrr
sag
qcer 4
frr
qrr
sag
qcer 4
frr
qrr
tag
qcer 4
fqr
qrr
tag
qcer 4
fqr
qrr
tag
rceq 4
qfr
qrr
tag
rceq 4
qfr
qrr
gat
rceq 4
qfq
qrr
gat
rceq 4
qfq
qrr
gbt
rceq 4
qfq
rrr
gbt
rceq 4
qfr
rrr
gbt
rceq 4
qfr
rrr
gbt
ecrq 4
qfr
rrr
gbt
ecqr 4
pfr
rrr
gbt
ecqr 4
ofr
rrr
gbt
ecqr 4
ofr
rrr
tbg
ecqr 4
ofr
rrr
tbg
ecqs 4
ofr
rrr
gbt
ecqs 4
pfr
rrr
gbt
ecqs 4
pfr
rrr
gbt
edqs 4
pfr
rrr
fbt
edqs 4
qfr
rrr
fbt
edqs 4
qfr
rrr
fbt
fdqs 4
qfr
rsr
fbt
fdqs 4
qfr
rsr
fbt
fcqs 4
qfr
rsr
ebt
fcqs 4
qfr
rsr
ebt
fdqs 4
rfr
rsr
ebt
fdqs 4
rfr
rsr
ect
fdqs 4
rfr
rsr
etc
fdqs 4
rfr
rsr
cte
fdqs 4
rfr
rsr
cte
fdqt 4
rfr
rsr
cte
edqt 4
rfr
rsr
cse
edqt 4
rfq
rsr
cse
edqt 4
rfq
rsr
esc
edqt 4
rgq
rsr
esc
edqt 4
rgq
rsr
dsc
edqt 4
rgq
rsr
dcs
edqt 4
qgq
rsr
dsc
edqt 4
qgr
rsr
dsc
edqt 4
qgr
rss
dsc
edqt 4
qgq
rss
dsc
edqt 4
qgq
rss
esc
edqt 4
qgq
rss
esb
edqt 4
qgq
rss
esa
edqt 4
gqq
rss
esa
edqt 4
gqq
rss
fsa
edqt 4
gqq
rss
fs`
edqt 4
gqq
rss
fs`
edqs 4
gqq
sss
fs`
edqs 4
gqq
sss
ft`
edqs 4
gqq
sss
ft`
sqde 4
gqq
rss
ft`
sqde 4
gqq
rss
`tf
sqde 4
gqq
ssr
`tf
sqde 4
gqq
srs
`tf
sqde 4
qqg
srs
`tf
sqde 4
qpg
srs
`tf
sqde 4
qpg
rss
`tf
sqde 4
ppg
rss
`tf
sqde 4
ppg
rss
`te
sqde 4
ppg
rss
et`
sqde 4
ppg
rss
`te
edqs 4
ppg
rss
`te
eqds 4
gpp
rss
`te
eqds 4
gpp
rss
et`
eqds 4
gqp
rss
et`
eqds 4
gqp
rss
et`
edqs 4
gqp
rss
`te
edqs 4
gqp
rrs
`te
edqs 4
gqp
rrs
`se
edqs 4
gqp
rsr
`se
edqs 4
gqp
rsr
`se
sqde 4
gpq
rsr
`se
sqde 4
gpq
ssr
`se
sqde 4
gpq
ssr
`se
sqce 4
gpq
ssr
`se
sqcf 4
gpq
ssr
`sd
sqcf 4
gpp
ssr
`sd
sqcf 4
gpp
str
`sd
sqcf 4
gpp
str
`sd
sqcg 4
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