import Data.List
import qualified Data.Map as Map

f::Int->Int->String->Map.Map Int Int->Map.Map Int Int
f _ _ "" ms=ms
f k c1 (c:[]) ms=(Map.insert k c1 ms)
f k c1 ('A':'C':xs) ms=f (k+2) (c1+1) xs (Map.insert (k+1) (c1+1) (Map.insert k c1 ms))
f k c1 (_:xs) ms=f (k+1) c1 xs (Map.insert k c1 ms)

g::[String]->Map.Map Int Int->[String]
g [] _ =[]
g (x:xs) ms=let (a:b:[])=map read (words x)::[Int]
                Just c2=Map.lookup b ms
                Just c1=Map.lookup a ms
            in (show (c2-c1)):g xs ms

main = do
       l1<-getLine
       l2<-getLine
       ls<-getContents
       let ms=f 1 0 l2 (Map.fromList [])
       mapM_ putStrLn $ g (lines ls) ms