import Control.Arrow
import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as B

main :: IO ()
main = getLine >>= print . solve

solve :: String -> Int
solve = count 0 . reverse . map ((,) <$> head <*> length) . group . edit
    where
        edit ('A' : xs) = 0 : edit xs
        edit ('B' : 'C' : xs) = 1 : edit xs
        edit (_ : xs) = 2 : edit xs
        edit _ = []

        count acc (x@(1, xn) : (0, yn) : zs) = count (acc + yn * xn) $ x : zs
        count acc ((1, xn) : (1, yn) : zs) = count acc $ (1, xn + yn) : zs
        count acc (_ : zs) = count acc zs 
        count acc _ = acc 

readLine :: IO [Int]
readLine = unfoldr (fmap (second (maybe B.empty snd . B.uncons)) . B.readInt) <$> B.getLine

readContents :: Int -> IO [[Int]]
readContents = flip replicateM readLine