import Control.Monad import Data.Array import System.IO import Text.Printf f :: Int -> [Int] -> Int f p ps = table ! (1,p) where b = ((1,1),(p,p)) table = array b [((i,j), if i <= j then g i j else 0) | i <- [1..p], j <- [1..p]] g i j | null xs = 0 | otherwise = j - i + minimum [t i (x-1) + t (x+1) j | x<- xs] where xs = filter (\x -> i <= x && x <= j) ps t i j | inRange b (i,j) = table ! (i,j) | otherwise = 0 main :: IO () main = do n <- liftM read getLine forM_ [(1::Int)..n] $ \i -> do [p,q] <- liftM (map read . words) getLine xs <- liftM (map read . words) getLine printf "Case #%d: %d\n" i (f p xs) hFlush stdout