import Control.Monad

main = do
  t <- read <$> getLine :: IO Int
  xs <- forM [1..t] $ \x -> do
    read <$> getLine :: IO Int
  --putStrLn .show $ xs
  case solve xs 0 of
    Nothing -> putStrLn . show $ (-1)
    Just (a,b) -> putStrLn . show $ a

solve :: [Int] -> Int -> Maybe (Int,Int)
solve [] i = Just (0, 0)
solve (x:[]) i = if x <= i then Just(x,x) else Nothing
solve (x:xs) i = 
  case solve xs (i+1) of
    Nothing -> Nothing
    Just (a,b) ->
      if x == b-1 then 
        Just (a,x) 
      else if x > i || b /= 0 && x < b-1 then 
        Nothing 
      else Just (a + x, x)