import Data.Functor ((<$>))
import Data.Array
import Data.Char (isSpace)
import Data.List
import Data.Ord (comparing)
import qualified Data.ByteString.Char8 as B

bsearch :: (Int -> Bool) -> Int -> Int -> Int
bsearch f l r
    | l == r    = r
    | f m       = bsearch f l m
    | otherwise = bsearch f (m + 1) r
    where m = (l + r) `div` 2

check :: Array Int Int -> [Int] -> Int -> Bool
check d a n = and (zipWith (\x y -> exam!x >= y) subj requ)
    where m  = snd (bounds d)
          exam = accumArray (const id) 0 (0, m) (zip a [1..n]) -- deadline
          subj = sortBy (comparing (exam!)) [1..m]  -- subjects sorted by deadline
          requ = scanl1 (+) (map (d!) subj)         -- times required for each subjects w.r.t. subj

solve :: [Int] -> [Int] -> Int
solve dl a
    | check d a (length a) = bsearch (check d a) 0 (length a)
    | otherwise            = -1
    where d = listArray (1, length dl) (map (+1) dl)

main :: IO()
main = do
    _ <- B.getLine
    a <- unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine
    d <- unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine
    print $ solve d a

import Data.Functor ((<$>))
import Data.Array
import Data.List
import Data.Ord (comparing)

bsearch :: (Int -> Bool) -> Int -> Int -> Int
bsearch f l r
    | l == r    = r
    | f m       = bsearch f l m
    | otherwise = bsearch f (m + 1) r
    where m = (l + r) `div` 2

check :: [Int] -> [Int] -> Bool
check d a = all (uncurry (<=)) (zip x (map snd st))
    where m  = length d :: Int
          ed = accumArray (const id) 0 (0, m) (zip a [1::Int ..])
          st = sortBy (comparing snd) (zip d (map (ed!) [1..m]))
          x  = scanl1 (+) (map fst st)

solve :: [Int] -> [Int] -> Int
solve d a
    | check d a = bsearch (check d . (`take` a)) 0 (length a)
    | otherwise = -1

main :: IO()
main = do
    _ <- getLine
    a <- map read . words <$> getLine
    d <- map read . words <$> getLine
    print $ solve (map (+1) d) a

import Data.Functor ((<$>))
import Data.Array
import Data.List
import Data.Ord (comparing)

bsearch :: (Int -> Bool) -> Int -> Int -> Int
bsearch f l r
    | l == r    = r
    | f m       = bsearch f l m
    | otherwise = bsearch f (m + 1) r
    where m = (l + r) `div` 2

check :: Array Int Int -> [Int] -> Bool
check d a = and (zipWith (\x y -> exam!x >= y) subj requ)
    where m  = snd (bounds d)
          exam = accumArray (const id) 0 (0, m) (zip a [1::Int ..]) -- deadline
          subj = sortBy (comparing (exam!)) [1..m]          -- subjects sorted by deadline
          requ = scanl1 (+) (map (d!) subj) -- times required for each subjects w.r.t. subj

solve :: Array Int Int -> [Int] -> Int
solve d a
    | check d a = bsearch (check d . (`take` a)) 0 (length a)
    | otherwise = -1

main :: IO()
main = do
    _ <- getLine
    a <- map read . words <$> getLine
    dList <- map ((+1) . read) . words <$> getLine
    let d = listArray (1, length dList) dList
    print $ solve d a

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns      #-}
{-# LANGUAGE CPP               #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Safe              #-}
{-# LANGUAGE TupleSections     #-}

import           Control.Applicative
import           Control.Monad
import           Control.Monad.State.Strict
import           Data.Array.Unboxed
import           Data.Bits
import           Data.Bool
import qualified Data.ByteString.Char8      as B
import           Data.Char
import           Data.Function
import           Data.Int
import qualified Data.IntMap.Strict         as IM
import qualified Data.IntSet                as IS
import           Data.List
import           Data.List.Split
import qualified Data.Map.Strict            as M
import           Data.Monoid
import           Data.Ord
import qualified Data.Set                   as S
import           Data.Tuple
import           Data.Word

readInt :: B.ByteString -> Int
readInt bs=case B.readInt bs of{Just(n,_)->n;_->error$"readInt error : bs = "++show bs;}
readInteger :: B.ByteString -> Integer
readInteger bs=case B.readInteger bs of{Just(n,_)->n;_->error$"readInteger error : bs = "++show bs;}

main :: IO ()
main = do
    [n, m] <- map read.words <$> getLine :: IO [Int]
    ds <- map readInt.B.words <$> B.getLine
    xs <- map (fromIntegral.readInt).B.words <$> B.getLine
    print $ solve n m ds xs

solve :: Int -> Int -> [Int] -> [Int64] -> Int
solve n m ds xs
  | isOK n = lowerBound 1 n isOK
  | otherwise = -1
  where
    arr :: UArray Int Int64
    arr = listArray (1, m) xs
    isOK i = go (IS.fromList[1..m]) 0 . reverse $ take i ds
      where
        go !unused !acc (x:xs)
          | IS.member x unused = go (IS.delete x unused) (acc+arr!x) xs
          | otherwise = go unused (max 0 $ acc-1) xs
        go unused acc [] = IS.null unused && acc == 0

lowerBound :: (Integral i) => i -> i -> (i -> Bool) -> i
lowerBound low high p =  go low high
  where
    go !low !high
        | high <= low = high
        | p mid       = go low       mid
        | otherwise   = go (mid + 1) high
      where
        mid = (low + high) `quot` 2
{-# INLINE lowerBound #-}

import Data.Functor ((<$>))
import Data.Array
import Data.Char (isSpace)
import Data.List
import Data.Ord (comparing)
import qualified Data.ByteString.Char8 as B

bsearch :: (Int -> Bool) -> Int -> Int -> Int
bsearch f l r
    | l == r    = r
    | f m       = bsearch f l m
    | otherwise = bsearch f (m + 1) r
    where m = (l + r) `div` 2

check :: Array Int Int -> [Int] -> Int -> Bool
check d a n = and (zipWith (\x y -> exam!x >= y) subj requ)
    where m  = snd (bounds d)
          exam = accumArray (const id) 0 (0, m) (zip a [1..n]) -- deadline
          subj = sortBy (comparing (exam!)) [1..m]  -- subjects sorted by deadline
          requ = scanl1 (+) (map (d!) subj)         -- times required for each subjects w.r.t. subj

solve :: [Int] -> [Int] -> Int
solve dl a
    | check d a (length a) = bsearch (check d a) 0 (length a)
    | otherwise            = -1
    where d = listArray (1, length dl) (map (+1) dl)

main :: IO()
main = do
    _ <- B.getLine
    a <- unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine
    d <- unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine
    print (a `seq` d `seq` solve d a)

import Data.Functor ((<$>))
import Data.Array
import Data.Char (isSpace)
import Data.List
import Data.Ord (comparing)
import qualified Data.ByteString.Char8 as B

bsearch :: (Int -> Bool) -> Int -> Int -> Int
bsearch f l r
    | l == r    = r
    | f m       = bsearch f l m
    | otherwise = bsearch f (m + 1) r
    where m = (l + r) `div` 2

check :: Array Int Int -> [Int] -> Bool
check d a = and (zipWith (\x y -> exam!x >= y) subj requ)
    where m  = snd (bounds d)
          exam = accumArray (const id) 0 (0, m) (zip a [1::Int ..]) -- deadline
          subj = sortBy (comparing (exam!)) [1..m]  -- subjects sorted by deadline
          requ = scanl1 (+) (map (d!) subj)         -- times required for each subjects w.r.t. subj

solve :: [Int] -> [Int] -> Int
solve dl a
    | check d a = bsearch (check d . (`take` a)) 0 (length a)
    | otherwise = -1
    where d = listArray (1, length dl) (map (+1) dl)

main :: IO()
main = do
    _ <- B.getLine
    a <- unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine
    d <- unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine
    print $ solve d a

import Data.Functor ((<$>))
import Data.Array
import Data.List
import Data.Ord (comparing)
import Data.Ix (inRange)

bsearch :: (Int -> Bool) -> Int -> Int -> Int
bsearch f l r
    | l == r    = r
    | f m       = bsearch f l m
    | otherwise = bsearch f (m + 1) r
    where m = (l + r) `div` 2

check :: Array Int Int -> [(Int, Int)] -> Bool
check d a = and (zipWith (\x y -> exam!x >= y) subj requ)
    where exam = accumArray (const id) 0 (bounds d) a   -- deadline
          subj = sortBy (comparing (exam!)) (indices d) -- subjects sorted by deadline
          requ = scanl1 (+) (map (d!) subj)             -- times required for each subjects w.r.t. subj

solve :: [Int] -> [Int] -> Int
solve dl a0
    | check d a = snd $ a !! (bsearch (check d . (`take` a)) 0 (length a) - 1)
    | otherwise = -1
    where d = listArray (1, length dl) (map (+1) dl)
          a = filter (inRange (bounds d) . fst) (zip a0 [1..])

main :: IO()
main = do
    _ <- getLine
    a <- map read . words <$> getLine
    d <- map read . words <$> getLine
    print $ solve d a

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns      #-}
{-# LANGUAGE CPP               #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Safe              #-}
{-# LANGUAGE TupleSections     #-}

import           Control.Applicative
import           Control.Monad
import           Control.Monad.State.Strict
import           Data.Array.Unboxed
import           Data.Bits
import           Data.Bool
import qualified Data.ByteString.Char8      as B
import           Data.Char
import           Data.Function
import           Data.Int
import qualified Data.IntMap.Strict         as IM
import qualified Data.IntSet                as IS
import           Data.List
import           Data.List.Split
import qualified Data.Map.Strict            as M
import           Data.Monoid
import           Data.Ord
import qualified Data.Set                   as S
import           Data.Tuple
import           Data.Word

readInt :: B.ByteString -> Int
readInt bs=case B.readInt bs of{Just(n,_)->n;_->error$"readInt error : bs = "++show bs;}
readInteger :: B.ByteString -> Integer
readInteger bs=case B.readInteger bs of{Just(n,_)->n;_->error$"readInteger error : bs = "++show bs;}

main :: IO ()
main = do
    [n, m] <- map read.words <$> getLine :: IO [Int]
    ds <- map readInt.B.words <$> B.getLine
    xs <- map (fromIntegral.readInt).B.words <$> B.getLine
    print $ solve n m ds xs

solve :: Int -> Int -> [Int] -> [Int64] -> Int
solve n m ds xs
  | isOK n = lowerBound 1 n isOK
  | otherwise = -1
  where
    arr :: UArray Int Int64
    arr = listArray (1, m) xs
    isOK i = go (IS.fromList[1..n]) 0 . reverse $ take i ds
      where
        go !unused !acc (x:xs)
          | IS.member x unused = go (IS.delete x unused) (acc+arr!x) xs
          | otherwise = go unused (max 0 $ acc-1) xs
        go _ acc [] = acc <= 0

lowerBound :: (Integral i) => i -> i -> (i -> Bool) -> i
lowerBound low high p =  go low high
  where
    go !low !high
        | high <= low = high
        | p mid       = go low       mid
        | otherwise   = go (mid + 1) high
      where
        mid = (low + high) `quot` 2
{-# INLINE lowerBound #-}

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns      #-}
{-# LANGUAGE CPP               #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Safe              #-}
{-# LANGUAGE TupleSections     #-}

import           Control.Applicative
import           Control.Monad
import           Control.Monad.State.Strict
import           Data.Array.Unboxed
import           Data.Bits
import           Data.Bool
import qualified Data.ByteString.Char8      as B
import           Data.Char
import           Data.Function
import           Data.Int
import qualified Data.IntMap.Strict         as IM
import qualified Data.IntSet                as IS
import           Data.List
import           Data.List.Split
import qualified Data.Map.Strict            as M
import           Data.Monoid
import           Data.Ord
import qualified Data.Set                   as S
import           Data.Tuple
import           Data.Word

readInt :: B.ByteString -> Int
readInt bs=case B.readInt bs of{Just(n,_)->n;_->error$"readInt error : bs = "++show bs;}
readInteger :: B.ByteString -> Integer
readInteger bs=case B.readInteger bs of{Just(n,_)->n;_->error$"readInteger error : bs = "++show bs;}

main :: IO ()
main = do
    [n, m] <- map read.words <$> getLine :: IO [Int]
    ds <- map readInt.B.words <$> B.getLine
    xs <- map (fromIntegral.readInt).B.words <$> B.getLine
    print $ solve n m ds xs

solve :: Int -> Int -> [Int] -> [Int64] -> Int
solve n m ds xs
  | isOK n = lowerBound 1 n isOK
  | otherwise = -1
  where
    arr :: UArray Int Int64
    arr = listArray (1, m) xs
    isOK i = go (IS.fromList[1..n]) 0 . reverse $ take i ds
      where
        go !unused !acc (x:xs)
          | IS.member x unused = go (IS.delete x unused) (acc+arr!x) xs
          | otherwise = go unused (acc-1) xs
        go _ acc [] = acc <= 0

lowerBound :: (Integral i) => i -> i -> (i -> Bool) -> i
lowerBound low high p =  go low high
  where
    go !low !high
        | high <= low = high
        | p mid       = go low       mid
        | otherwise   = go (mid + 1) high
      where
        mid = (low + high) `quot` 2
{-# INLINE lowerBound #-}


import Control.Monad ( replicateM_ )
import Data.Int ( Int64 )

solve :: IO ()
solve = do
    n <- readLn :: IO Int64
    a <- readMany :: IO [Int64]
    let m = sum a `mod` n
    print (m * (n - m))

readMany :: Read a => IO [a]
readMany = map read . words <$> getLine

main :: IO ()
main = do
    t <- readLn :: IO Int
    replicateM_ t solve

import Control.Monad

solve :: IO ()
solve = do
    n <- readLn :: IO Int
    arr <- map read.words <$> getLine :: IO [Int]
    let s = sum arr
        k = s `mod` n
    print (k * (n - k))

main = do
    test <- readLn :: IO Int
    replicateM_ test solve

import Control.Monad (replicateM_)

readInts :: IO [Integer]
readInts = map read . words <$> getLine

main :: IO ()
main = do
  t <- readLn
  replicateM_ t $ do
    n <- readLn
    a <- readInts
    let
      s = sum a
      miss = s `rem` n
    print $ (n - miss) * miss

{-# OPTIONS_GHC -O2 #-}

import Control.Monad
import Prelude as P
import Data.List as L
import Data.Int (Int64)
--import Data.Char as C
--import Data.IntSet as S

solve :: IO ()
solve = do
  n <- readLn :: IO Int64
  s <- getLine
  let a = map read $ words s :: [Int64]
      su = sum a
      (high, low) = (rem su n, n - (rem su n))
      res = high*low
    in print res

main = do
  t <- readLn :: IO Int
  replicateM_ t solve

import Control.Monad
readInts = fmap (map read.words) getLine :: IO [Integer]

printResult = do
    n <- readLn :: IO Integer
    a <- readInts
    let m = mod (sum a) n
        r = m * (n-m)
    print r

main = do
    t <- readLn :: IO Int
    replicateM_ t printResult

module Main where
import Control.Monad ( replicateM )
import Data.Foldable ( traverse_ )
main :: IO ()
main = do
    t <- read <$> getLine :: IO Int
    inp <- replicateM t $ do
        n <- read <$> getLine :: IO Integer
        arr <- map read . words <$> getLine :: IO [Integer]
        return (n, arr)
    let solved1 = zip [0..] inp
    let solved = map solve solved1
    -- print inp
    traverse_ putStrLn solved

solve :: (Int, (Integer, [Integer])) -> String
solve (tc, (n, arr)) =
    let
        sm = sum arr
        -- avg = div sm n
        k = mod sm n
        -- lft = sm - avg * n
    in
        -- if tc == 6
        -- then unwords $ map show arr
        -- else show $ k * (n - k)
        show $ k * (n - k)

module Main where
import Control.Monad ( replicateM )
import Data.Foldable ( traverse_ )
main :: IO ()
main = do
    t <- read <$> getLine :: IO Int
    inp <- replicateM t $ do
        n <- read <$> getLine :: IO Int
        arr <- map read . words <$> getLine :: IO [Int]
        return (n, arr)
    let solved1 = zip [0..] inp
    let solved = map solve solved1
    -- print inp
    traverse_ putStrLn solved

solve :: (Int, (Int, [Int])) -> String
solve (tc, (n, arr)) =
    let
        sm = sum arr
        -- avg = div sm n
        k = mod sm n
        -- lft = sm - avg * n
    in
        if tc == 6
        then unwords $ map show arr
        else show $ k * (n - k)

module Main where
import Data.List ( intercalate )
main :: IO ()
main = interact solve

solve :: String -> String
solve inp =
    let
        inps = drop 1 . lines $ inp
        ans = [map read . words $ j | (i, j) <- zip [0..] inps, odd i] :: [[Int]]
        sans = map (show . slv) ans

    in
        unlines sans

slv :: [Int] -> Int
slv arr =
    let
        n = length arr
        sm = sum arr
        k = mod sm n
    in
        k * (n - k)

module Main where
import Control.Monad ( replicateM )
import Data.Foldable ( traverse_ )
main :: IO ()
main = do
    t <- read <$> getLine :: IO Int
    inp <- replicateM t $ do
        n <- read <$> getLine :: IO Int
        arr <- map read . words <$> getLine :: IO [Int]
        return (n, arr)
    let solved = map solve inp
    -- print inp
    traverse_ print solved

solve :: (Int, [Int]) -> Int
solve (n, arr) =
    let
        sm = sum arr
        -- avg = div sm n
        k = mod sm n
        -- lft = sm - avg * n
    in
        k * (n - k)

module Main where
import Control.Monad ( replicateM )
import Data.Foldable ( traverse_ )
main :: IO ()
main = do
    t <- read <$> getLine :: IO Int
    inp <- replicateM t $ do
        n <- read <$> getLine :: IO Int
        arr <- map read . words <$> getLine :: IO [Int]
        return (n, arr)
    let solved = map solve inp
    -- print inp
    traverse_ print solved

solve :: (Int, [Int]) -> Int
solve (n, arr) =
    let
        sm = sum arr
        avg = div sm n
        lft = sm - avg * n
    in
        lft * (n - lft)


module Main where

import Data.List
import Data.Set (toList, fromList)

other "First" = "Second"
other "Second" = "First"

countAllElems xs = reverse . sort . map counter . uniques $ xs
    where
        uniques = toList . fromList
        counter c = length $ filter (== c) xs

canPalindrome xs = countOdds < 2
    where countOdds = length . filter odd $ xs

findFirst p xs = findFirst' p xs 0
findFirst' p [] _ = Nothing
findFirst' p (x:xs) ind
    | p x = (Just (x, ind))
    | otherwise = findFirst' p xs (ind+1)

replace i e xs = (take i xs) ++ [e] ++ (drop (i+1) xs)

makeMove xs = filter (> 0) . mkmv . findFirst even $ xs
    where
        mkmv (Just (c, i)) = replace i (c-1) xs
        mkmv Nothing = replace 0 (head xs - 1) xs

canWin plr xs
    | canPalindrome xs = plr
    | otherwise = canWin (other plr) (makeMove xs)

main = do
  starting <- getLine
  putStrLn . canWin "First" . countAllElems $ starting

import Data.List

main :: IO ()
main = do
  s <- getLine
  let t = length $ filter odd $ map length $ group $ sort s
  putStrLn $ if t == 0 || odd t then "First" else "Second"

import Data.Array (accumArray, elems)

solve :: String -> String
solve s
  | odd countOdd || countOdd == 0  =  "First"
  | otherwise  =  "Second"
  where
    countOdd  =  length $ filter odd $ elems array
    array  =  accumArray (+) 0 ('a', 'z') $ zip s $ repeat 1

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

import Data.Array.Base
main=getLine>>=putStrLn.f.unsafeAccumArray (+) 0 (0,128) . (`zip`(repeat 1)) . map fromEnum
f :: UArray Int Int -> String
f arr
    | n > 0 && even n = "Second"
    | otherwise = "First"
   where
     xs = map (unsafeAt arr) [fromEnum 'a'..fromEnum 'z']
     n = length $ filter odd xs

module Main where

import Data.List

odds :: String -> Int
odds = (foldl (+) 0) . map ((`mod` 2) . length) . group . sort

solve :: String -> String
solve str = if odds str == 0 || (odds str) `mod` 2 == 1
               then "First"
               else "Second"

main = getLine >>= (putStrLn . solve)

import Data.List
classify list = classify' (tail list) (head list) 1

classify' [] char pat = [pat]
classify' list char pat
    | (head list) /= char = pat : (classify' (tail list) (head list) 1)
    | otherwise = classify' (tail list) char (pat + 1)

main = do
    input <- getContents
    let sorted = Data.List.sort $ head $ lines input
    let classified = filter (\x -> x `mod` 2 == 1) $ classify sorted
    let total = length classified
    let res = if total == 0 || total `mod` 2 == 1 then "First" else "Second"
    putStrLn res

import Data.List
classify list = classify' (tail list) (head list) 1

classify' [] char pat = [pat]
classify' list char pat
    | (head list) /= char = pat : (classify' (tail list) (head list) 1)
    | otherwise = classify' (tail list) char (pat + 1)

main = do
    input <- getContents
    let sorted = Data.List.sort $ head $ lines input
    let total = sum $ classify sorted
    let res = if total `mod` 2 == 0 then "Second" else "First"
    putStrLn res


{-# LANGUAGE
    BangPatterns
  , BlockArguments
  , DataKinds
  , DeriveGeneric
  , DerivingStrategies
  , DerivingVia
  , FlexibleContexts
  , FlexibleInstances
  , GADTs
  , GeneralizedNewtypeDeriving
  , KindSignatures
  , LambdaCase
  , MultiParamTypeClasses
  , MultiWayIf
  , NPlusKPatterns
  , NegativeLiterals
  , OverloadedLabels
  , OverloadedStrings
  , ParallelListComp
  , PolyKinds
  , RankNTypes
  , ScopedTypeVariables
  , StrictData
  , TupleSections
  , TypeApplications
  , TypeFamilies
  , TypeOperators
  , UndecidableInstances
  , ViewPatterns
  #-}

module Main where

import           Control.Applicative
import           Control.Arrow
import           Control.Monad
import           Control.Monad.ST
import qualified Data.ByteString.Char8         as BS
import           Data.Char
import           Data.Foldable
import           Data.Functor
import           Data.Int
import           Data.List
import           Data.Maybe
import           Data.Monoid
import           Data.Ord
import           Data.Semigroup                 ( Max(..)
                                                , Min(..)
                                                , All(..)
                                                , Arg(..)
                                                )
import           Data.Ratio
-- import           Data.Vector.Unboxing.Mutable   ( Unboxable )
-- import qualified Data.Vector.Unboxing.Mutable  as V
import           System.IO
import           Data.Proxy
import           GHC.TypeLits
import           GHC.Generics                   ( Generic )
import           Data.Function
-- import           Data.List.HT                   ( mapAdjacent
--                                                 , isAscending
--                                                 )
-- import           Control.Monad.Primitive        ( PrimMonad(PrimState) )
import qualified Data.Set                      as S
-- import qualified Data.Vector.Algorithms.Radix  as VA
-- import           Text.Parsec.ByteString

-- #if defined(LOCAL_ZER0STAR)
-- {-# ANN module ("Hlint: ignore Unused LANGUAGE pragma" :: String) #-}
-- {-# ANN module ("Hlint: ignore Reduce duplication" :: String) #-}
-- #endif

twice :: (a -> a -> b) -> a -> b
twice f x = f x x

both :: Arrow a => a b c -> a (b, b) (c, c)
both = twice (***)

both2 f (a1, b1) (a2, b2) = (f a1 a2, f b1 b2)

readsLn :: Read a => IO [a]
readsLn = mapM readIO . words =<< getLine

getInt :: IO Int
getInt = fst . fromJust . BS.readInt <$> BS.getLine

getInts :: IO [Int]
getInts = map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine

getInteger :: IO Integer
getInteger = fst . fromJust . BS.readInteger <$> BS.getLine

getIntegers :: IO [Integer]
getIntegers = map (fst . fromJust . BS.readInteger) . BS.words <$> BS.getLine

main = do
  t <- getInt
  replicateM_
    t
    do
      n <- getInt
      a <- reverse . sort <$> getIntegers
      putStrLn case flip compare 0 $ solve a True 0 of
        LT -> "Bob"
        EQ -> "Tie"
        GT -> "Alice"

solve []       _     ans = ans
solve (a : as) True  ans = solve as False $ ans + if even a then a else 0
solve (a : as) False ans = solve as True $ ans - if odd a then a else 0

alice n = (if even n then n else 0, 0)

bob n = (0, if odd n then n else 0)

gd :: Down a -> a
gd (Down x) = x

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}

import safe Control.Arrow ((>>>))
import qualified Data.Text as T
import qualified Data.Text.IO as TI
import Data.List
import Data.Function
import Data.Maybe

main :: IO ()
main = TI.getContents >>= (T.lines >>> drop 1 >>> tcio >>> (mapM_ putStrLn)) where
  tcio :: [T.Text] -> [String]
  tcio [] = []
  tcio (_ : ns : rest) = ((:[]). solve . linetois) (ns) ++ tcio (rest)
  linetoi = toi; linetois = (map linetoi).(T.words); linestoiss = map linetois
  toi t = toi0 0 t where toi0 n t = if (T.null) t then n else toi0 (10*n + fromIntegral(on (-) fromEnum (T.head t) '0') ) (T.tail t) ;

solve :: [Integer] -> String
solve ns
 |a>b = "Alice"
 |b>a = "Bob"
 |otherwise = "Tie" where
 (a,b) = rec (reverse $ sort $ ns) (0,0)

rec [] (a,b)= (a,b)
rec [k] (a,b) = if even k then (a+k,b) else (a,b)
rec (k1:k2:ks) (a,b) = rec ks (if even k1 then a+k1 else a, if odd k2 then b+k2 else b)

import Control.Monad
import Data.List
import Data.Int -- Int is 32-bit on CF...

main :: IO ()
main = do
  t <- read <$> getLine
  replicateM_ t $ do
    _n <- getLine
    a <- map read . words <$> getLine :: IO [Int64]
    let
      eoT = sum [if  even x  then  x  else  0-x | x <- a]
      -- sort is so slow! hopefully OK with 2e5?
      abT = sum [x * y | (x, y) <- zip (cycle [1, 0-1]) (reverse $ sort a)]
    putStrLn $ case  compare 0 $ eoT + abT  of
      LT -> "Alice"
      EQ -> "Tie"
      GT -> "Bob"

import Prelude
import Data.Int
import Data.List

pairplus :: (Int64,Int64) -> (Int64,Int64) -> (Int64,Int64)
pairplus (a,b) (c,d) = (a+c,b+d)

game :: Int64 -> [Int64] -> (Int64,Int64)
game _ [] = (0,0)
game n (x:xs) = if (mod n 2 == 0) then (if (mod x 2==0)then pairplus(x,0)rec else rec) else (if(mod x 2==1)then pairplus(0,x)rec else rec) where rec = game (n+1) xs

result :: (Int64,Int64) -> String
result (a,b) | a == b = "Tie"
             | a > b = "Alice"
             | otherwise = "Bob"

everyother :: [a] -> [a]
everyother [] = []
everyother (x:y:xs) = y : (everyother xs)
everyother (x:xs) = []

readL :: String -> [Int64]
readL list = map read $ words list

solve :: String -> String
solve string = list where list = concat $ intersperse "\n"  $ map result $ map (game 0) $ map (reverse . sort) $ map readL $ everyother $ tail $ lines string

main :: IO ()
main = interact $ solve

{-# LANGUAGE
    BangPatterns
  , BlockArguments
  , DataKinds
  , DeriveGeneric
  , DerivingStrategies
  , DerivingVia
  , FlexibleContexts
  , FlexibleInstances
  , GADTs
  , GeneralizedNewtypeDeriving
  , KindSignatures
  , LambdaCase
  , MultiParamTypeClasses
  , MultiWayIf
  , NPlusKPatterns
  , NegativeLiterals
  , OverloadedLabels
  , OverloadedStrings
  , ParallelListComp
  , PolyKinds
  , RankNTypes
  , ScopedTypeVariables
  , StrictData
  , TupleSections
  , TypeApplications
  , TypeFamilies
  , TypeOperators
  , UndecidableInstances
  , ViewPatterns
  #-}

module Main where

import           Control.Applicative
import           Control.Arrow
import           Control.Monad
import           Control.Monad.ST
import qualified Data.ByteString.Char8         as BS
import           Data.Char
import           Data.Foldable
import           Data.Functor
import           Data.Int
import           Data.List
import           Data.Maybe
import           Data.Monoid
import           Data.Ord
import           Data.Semigroup                 ( Max(..)
                                                , Min(..)
                                                , All(..)
                                                , Arg(..)
                                                )
import           Data.Ratio
-- import           Data.Vector.Unboxing.Mutable   ( Unboxable )
-- import qualified Data.Vector.Unboxing.Mutable  as V
import           System.IO
import           Data.Proxy
import           GHC.TypeLits
import           GHC.Generics                   ( Generic )
import           Data.Function
-- import           Data.List.HT                   ( mapAdjacent
--                                                 , isAscending
--                                                 )
-- import           Control.Monad.Primitive        ( PrimMonad(PrimState) )
import qualified Data.Set                      as S
-- import qualified Data.Vector.Algorithms.Radix  as VA
-- import           Text.Parsec.ByteString

-- #if defined(LOCAL_ZER0STAR)
-- {-# ANN module ("Hlint: ignore Unused LANGUAGE pragma" :: String) #-}
-- {-# ANN module ("Hlint: ignore Reduce duplication" :: String) #-}
-- #endif

twice :: (a -> a -> b) -> a -> b
twice f x = f x x

both :: Arrow a => a b c -> a (b, b) (c, c)
both = twice (***)

both2 f (a1, b1) (a2, b2) = (f a1 a2, f b1 b2)

readsLn :: Read a => IO [a]
readsLn = mapM readIO . words =<< getLine

getInt :: IO Int
getInt = fst . fromJust . BS.readInt <$> BS.getLine

getInts :: IO [Int]
getInts = map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine

getInteger :: IO Integer
getInteger = fst . fromJust . BS.readInteger <$> BS.getLine

getIntegers :: IO [Integer]
getIntegers = map (fst . fromJust . BS.readInteger) . BS.words <$> BS.getLine

main = do
  t <- getInt
  replicateM_
    t
    do
      n <- getInt
      a <- reverse . sort <$> getInts
      putStrLn case flip compare 0 $ solve a True 0 of
        LT -> "Bob"
        EQ -> "Tie"
        GT -> "Alice"

solve []       _     ans = ans
solve (a : as) True  ans = solve as False $ ans + if even a then a else 0
solve (a : as) False ans = solve as True $ ans - if odd a then a else 0

alice n = (if even n then n else 0, 0)

bob n = (0, if odd n then n else 0)

gd :: Down a -> a
gd (Down x) = x

{-# LANGUAGE
    BangPatterns
  , BlockArguments
  , DataKinds
  , DeriveGeneric
  , DerivingStrategies
  , DerivingVia
  , FlexibleContexts
  , FlexibleInstances
  , GADTs
  , GeneralizedNewtypeDeriving
  , KindSignatures
  , LambdaCase
  , MultiParamTypeClasses
  , MultiWayIf
  , NPlusKPatterns
  , NegativeLiterals
  , OverloadedLabels
  , OverloadedStrings
  , ParallelListComp
  , PolyKinds
  , RankNTypes
  , ScopedTypeVariables
  , StrictData
  , TupleSections
  , TypeApplications
  , TypeFamilies
  , TypeOperators
  , UndecidableInstances
  , ViewPatterns
  #-}

module Main where

import           Control.Applicative
import           Control.Arrow
import           Control.Monad
import           Control.Monad.ST
import qualified Data.ByteString.Char8         as BS
import           Data.Char
import           Data.Foldable
import           Data.Functor
import           Data.Int
import           Data.List
import           Data.Maybe
import           Data.Monoid
import           Data.Ord
import           Data.Semigroup                 ( Max(..)
                                                , Min(..)
                                                , All(..)
                                                , Arg(..)
                                                )
import           Data.Ratio
-- import           Data.Vector.Unboxing.Mutable   ( Unboxable )
-- import qualified Data.Vector.Unboxing.Mutable  as V
import           System.IO
import           Data.Proxy
import           GHC.TypeLits
import           GHC.Generics                   ( Generic )
import           Data.Function
-- import           Data.List.HT                   ( mapAdjacent
--                                                 , isAscending
--                                                 )
-- import           Control.Monad.Primitive        ( PrimMonad(PrimState) )
import qualified Data.Set                      as S
-- import qualified Data.Vector.Algorithms.Radix  as VA
-- import           Text.Parsec.ByteString

-- #if defined(LOCAL_ZER0STAR)
-- {-# ANN module ("Hlint: ignore Unused LANGUAGE pragma" :: String) #-}
-- {-# ANN module ("Hlint: ignore Reduce duplication" :: String) #-}
-- #endif

twice :: (a -> a -> b) -> a -> b
twice f x = f x x

both :: Arrow a => a b c -> a (b, b) (c, c)
both = twice (***)

both2 f (a1, b1) (a2, b2) = (f a1 a2, f b1 b2)

readsLn :: Read a => IO [a]
readsLn = mapM readIO . words =<< getLine

getInt :: IO Int
getInt = fst . fromJust . BS.readInt <$> BS.getLine

getInts :: IO [Int]
getInts = map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine

getInteger :: IO Integer
getInteger = fst . fromJust . BS.readInteger <$> BS.getLine

getIntegers :: IO [Integer]
getIntegers = map (fst . fromJust . BS.readInteger) . BS.words <$> BS.getLine

main = do
  t <- getInt
  replicateM_
    t
    do
      n <- getInt
      a <- map gd . sort . map Down <$> getInts
      putStrLn case flip compare 0 $ solve a True 0 of
        LT -> "Bob"
        EQ -> "Tie"
        GT -> "Alice"

solve []       _     ans = ans
solve (a : as) True  ans = solve as False $ ans + if even a then a else 0
solve (a : as) False ans = solve as True $ ans - if odd a then a else 0

alice n = (if even n then n else 0, 0)

bob n = (0, if odd n then n else 0)

gd :: Down a -> a
gd (Down x) = x

{-# LANGUAGE
    BangPatterns
  , BlockArguments
  , DataKinds
  , DeriveGeneric
  , DerivingStrategies
  , DerivingVia
  , FlexibleContexts
  , FlexibleInstances
  , GADTs
  , GeneralizedNewtypeDeriving
  , KindSignatures
  , LambdaCase
  , MultiParamTypeClasses
  , MultiWayIf
  , NPlusKPatterns
  , NegativeLiterals
  , OverloadedLabels
  , OverloadedStrings
  , ParallelListComp
  , PolyKinds
  , RankNTypes
  , ScopedTypeVariables
  , StrictData
  , TupleSections
  , TypeApplications
  , TypeFamilies
  , TypeOperators
  , UndecidableInstances
  , ViewPatterns
  #-}

module Main where

import           Control.Applicative
import           Control.Arrow
import           Control.Monad
import           Control.Monad.ST
import qualified Data.ByteString.Char8         as BS
import           Data.Char
import           Data.Foldable
import           Data.Functor
import           Data.Int
import           Data.List
import           Data.Maybe
import           Data.Monoid
import           Data.Ord
import           Data.Semigroup                 ( Max(..)
                                                , Min(..)
                                                , All(..)
                                                , Arg(..)
                                                )
import           Data.Ratio
-- import           Data.Vector.Unboxing.Mutable   ( Unboxable )
-- import qualified Data.Vector.Unboxing.Mutable  as V
import           System.IO
import           Data.Proxy
import           GHC.TypeLits
import           GHC.Generics                   ( Generic )
import           Data.Function
-- import           Data.List.HT                   ( mapAdjacent
--                                                 , isAscending
--                                                 )
-- import           Control.Monad.Primitive        ( PrimMonad(PrimState) )
import qualified Data.Set                      as S
-- import qualified Data.Vector.Algorithms.Radix  as VA
-- import           Text.Parsec.ByteString

-- #if defined(LOCAL_ZER0STAR)
-- {-# ANN module ("Hlint: ignore Unused LANGUAGE pragma" :: String) #-}
-- {-# ANN module ("Hlint: ignore Reduce duplication" :: String) #-}
-- #endif

twice :: (a -> a -> b) -> a -> b
twice f x = f x x

both :: Arrow a => a b c -> a (b, b) (c, c)
both = twice (***)

both2 f (a1, b1) (a2, b2) = (f a1 a2, f b1 b2)

readsLn :: Read a => IO [a]
readsLn = mapM readIO . words =<< getLine

getInt :: IO Int
getInt = fst . fromJust . BS.readInt <$> BS.getLine

getInts :: IO [Int]
getInts = map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine

getInteger :: IO Integer
getInteger = fst . fromJust . BS.readInteger <$> BS.getLine

getIntegers :: IO [Integer]
getIntegers = map (fst . fromJust . BS.readInteger) . BS.words <$> BS.getLine

main = do
  t <- getInt
  replicateM_
    t
    do
      n <- getInt
      a <- map gd . sort . map Down <$> getInts
      guard $ length a == n
      putStrLn
        case
          uncurry compare
          . foldl (both2 (+)) (0, 0)
          . zipWith ($) (cycle [alice, bob])
          $ a
        of
          LT -> "Bob"
          EQ -> "Tie"
          GT -> "Alice"

alice n = (if even n then n else 0, 0)

bob n = (0, if odd n then n else 0)

gd :: Down a -> a
gd (Down x) = x

{-# LANGUAGE
    BangPatterns
  , BlockArguments
  , DataKinds
  , DeriveGeneric
  , DerivingStrategies
  , DerivingVia
  , FlexibleContexts
  , FlexibleInstances
  , GADTs
  , GeneralizedNewtypeDeriving
  , KindSignatures
  , LambdaCase
  , MultiParamTypeClasses
  , MultiWayIf
  , NPlusKPatterns
  , NegativeLiterals
  , OverloadedLabels
  , OverloadedStrings
  , ParallelListComp
  , PolyKinds
  , RankNTypes
  , ScopedTypeVariables
  , StrictData
  , TupleSections
  , TypeApplications
  , TypeFamilies
  , TypeOperators
  , UndecidableInstances
  , ViewPatterns
  #-}

module Main where

import           Control.Applicative
import           Control.Arrow
import           Control.Monad
import           Control.Monad.ST
import qualified Data.ByteString.Char8         as BS
import           Data.Char
import           Data.Foldable
import           Data.Functor
import           Data.Int
import           Data.List
import           Data.Maybe
import           Data.Monoid
import           Data.Ord
import           Data.Semigroup                 ( Max(..)
                                                , Min(..)
                                                , All(..)
                                                , Arg(..)
                                                )
import           Data.Ratio
-- import           Data.Vector.Unboxing.Mutable   ( Unboxable )
-- import qualified Data.Vector.Unboxing.Mutable  as V
import           System.IO
import           Data.Proxy
import           GHC.TypeLits
import           GHC.Generics                   ( Generic )
import           Data.Function
-- import           Data.List.HT                   ( mapAdjacent
--                                                 , isAscending
--                                                 )
-- import           Control.Monad.Primitive        ( PrimMonad(PrimState) )
import qualified Data.Set                      as S
-- import qualified Data.Vector.Algorithms.Radix  as VA
-- import           Text.Parsec.ByteString

-- #if defined(LOCAL_ZER0STAR)
-- {-# ANN module ("Hlint: ignore Unused LANGUAGE pragma" :: String) #-}
-- {-# ANN module ("Hlint: ignore Reduce duplication" :: String) #-}
-- #endif

twice :: (a -> a -> b) -> a -> b
twice f x = f x x

both :: Arrow a => a b c -> a (b, b) (c, c)
both = twice (***)

both2 f (a1, b1) (a2, b2) = (f a1 a2, f b1 b2)

readsLn :: Read a => IO [a]
readsLn = mapM readIO . words =<< getLine

getInt :: IO Int
getInt = fst . fromJust . BS.readInt <$> BS.getLine

getInts :: IO [Int]
getInts = map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine

getInteger :: IO Integer
getInteger = fst . fromJust . BS.readInteger <$> BS.getLine

getIntegers :: IO [Integer]
getIntegers = map (fst . fromJust . BS.readInteger) . BS.words <$> BS.getLine

main = do
  t <- getInt
  replicateM_
    t
    do
      n <- getInt
      a <- map gd . sort . map Down <$> getInts
      putStrLn
        case
          uncurry compare
          . foldl (both2 (+)) (0, 0)
          . zipWith ($) (cycle [alice, bob])
          $ a
        of
          LT -> "Bob"
          EQ -> "Tie"
          GT -> "Alice"

alice n = (if even n then n else 0, 0)

bob n = (0, if odd n then n else 0)

gd :: Down a -> a
gd (Down x) = x

{-# LANGUAGE
    BangPatterns
  , BlockArguments
  , DataKinds
  , DeriveGeneric
  , DerivingStrategies
  , DerivingVia
  , FlexibleContexts
  , FlexibleInstances
  , GADTs
  , GeneralizedNewtypeDeriving
  , KindSignatures
  , LambdaCase
  , MultiParamTypeClasses
  , MultiWayIf
  , NPlusKPatterns
  , NegativeLiterals
  , OverloadedLabels
  , OverloadedStrings
  , ParallelListComp
  , PolyKinds
  , RankNTypes
  , ScopedTypeVariables
  , StrictData
  , TupleSections
  , TypeApplications
  , TypeFamilies
  , TypeOperators
  , UndecidableInstances
  , ViewPatterns
  #-}

module Main where

import           Control.Applicative
import           Control.Arrow
import           Control.Monad
import           Control.Monad.ST
import qualified Data.ByteString.Char8         as BS
import           Data.Char
import           Data.Foldable
import           Data.Functor
import           Data.Int
import           Data.List
import           Data.Maybe
import           Data.Monoid
import           Data.Ord
import           Data.Semigroup                 ( Max(..)
                                                , Min(..)
                                                , All(..)
                                                , Arg(..)
                                                )
import           Data.Ratio
-- import           Data.Vector.Unboxing.Mutable   ( Unboxable )
-- import qualified Data.Vector.Unboxing.Mutable  as V
import           System.IO
import           Data.Proxy
import           GHC.TypeLits
import           GHC.Generics                   ( Generic )
import           Data.Function
-- import           Data.List.HT                   ( mapAdjacent
--                                                 , isAscending
--                                                 )
-- import           Control.Monad.Primitive        ( PrimMonad(PrimState) )
import qualified Data.Set                      as S
-- import qualified Data.Vector.Algorithms.Radix  as VA
-- import           Text.Parsec.ByteString

-- #if defined(LOCAL_ZER0STAR)
-- {-# ANN module ("Hlint: ignore Unused LANGUAGE pragma" :: String) #-}
-- {-# ANN module ("Hlint: ignore Reduce duplication" :: String) #-}
-- #endif

twice :: (a -> a -> b) -> a -> b
twice f x = f x x

both :: Arrow a => a b c -> a (b, b) (c, c)
both = twice (***)

both2 f (a1, b1) (a2, b2) = (f a1 a2, f b1 b2)

readsLn :: Read a => IO [a]
readsLn = mapM readIO . words =<< getLine

getInt :: IO Int
getInt = fst . fromJust . BS.readInt <$> BS.getLine

getInts :: IO [Int]
getInts = map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine

getInteger :: IO Integer
getInteger = fst . fromJust . BS.readInteger <$> BS.getLine

getIntegers :: IO [Integer]
getIntegers = map (fst . fromJust . BS.readInteger) . BS.words <$> BS.getLine

main = do
  t <- getInt
  replicateM_
    t
    do
      n <- getInt
      a <- map gd . sort . map Down <$> getInts
      putStrLn
        case flip compare 0 . sum . zipWith ($) (cycle [alice, bob]) $ a of
          LT -> "Bob"
          EQ -> "Tie"
          GT -> "Alice"

alice n = if even n then n else 0

bob n = if odd n then -n else 0

gd :: Down a -> a
gd (Down x) = x

{-# LANGUAGE
    BangPatterns
  , BlockArguments
  , DataKinds
  , DeriveGeneric
  , DerivingStrategies
  , DerivingVia
  , FlexibleContexts
  , FlexibleInstances
  , GADTs
  , GeneralizedNewtypeDeriving
  , KindSignatures
  , LambdaCase
  , MultiParamTypeClasses
  , MultiWayIf
  , NPlusKPatterns
  , NegativeLiterals
  , OverloadedLabels
  , OverloadedStrings
  , ParallelListComp
  , PolyKinds
  , RankNTypes
  , ScopedTypeVariables
  , StrictData
  , TupleSections
  , TypeApplications
  , TypeFamilies
  , TypeOperators
  , UndecidableInstances
  , ViewPatterns
  #-}

module Main where

import           Control.Applicative
import           Control.Arrow
import           Control.Monad
import           Control.Monad.ST
import qualified Data.ByteString.Char8         as BS
import           Data.Char
import           Data.Foldable
import           Data.Functor
import           Data.Int
import           Data.List
import           Data.Maybe
import           Data.Monoid
import           Data.Ord
import           Data.Semigroup                 ( Max(..)
                                                , Min(..)
                                                , All(..)
                                                , Arg(..)
                                                )
import           Data.Ratio
-- import           Data.Vector.Unboxing.Mutable   ( Unboxable )
-- import qualified Data.Vector.Unboxing.Mutable  as V
import           System.IO
import           Data.Proxy
import           GHC.TypeLits
import           GHC.Generics                   ( Generic )
import           Data.Function
-- import           Data.List.HT                   ( mapAdjacent
--                                                 , isAscending
--                                                 )
-- import           Control.Monad.Primitive        ( PrimMonad(PrimState) )
import qualified Data.Set                      as S
-- import qualified Data.Vector.Algorithms.Radix  as VA
-- import           Text.Parsec.ByteString

-- #if defined(LOCAL_ZER0STAR)
-- {-# ANN module ("Hlint: ignore Unused LANGUAGE pragma" :: String) #-}
-- {-# ANN module ("Hlint: ignore Reduce duplication" :: String) #-}
-- #endif

twice :: (a -> a -> b) -> a -> b
twice f x = f x x

both :: Arrow a => a b c -> a (b, b) (c, c)
both = twice (***)

both2 f (a1, b1) (a2, b2) = (f a1 a2, f b1 b2)

readsLn :: Read a => IO [a]
readsLn = mapM readIO . words =<< getLine

getInt :: IO Int
getInt = fst . fromJust . BS.readInt <$> BS.getLine

getInts :: IO [Int]
getInts = map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine

getInteger :: IO Integer
getInteger = fst . fromJust . BS.readInteger <$> BS.getLine

getIntegers :: IO [Integer]
getIntegers = map (fst . fromJust . BS.readInteger) . BS.words <$> BS.getLine

main = do
  t <- getInt
  replicateM_
    t
    do
      n <- getInt
      a <- map gd . sort . map Down <$> getInts
      putStrLn
        case flip compare 0 . sum . zipWith s (cycle [True, False]) $ a of
          LT -> "Bob"
          EQ -> "Tie"
          GT -> "Alice"

s True n | odd n     = 0
         | otherwise = n

s False n | odd n     = -n
          | otherwise = 0

gd (Down x) = x

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}

import safe Control.Arrow ((>>>))
import qualified Data.Text as T
import qualified Data.Text.IO as TI
import Data.List
import Data.Function

main :: IO ()
main = TI.getContents >>= (T.lines >>> drop 1 >>> tcio >>> (mapM_ putStrLn)) where
  tcio :: [T.Text] -> [String]
  tcio [] = []
  tcio (_ : ns : rest) = ((:[]). solve . linetois) (ns) ++ tcio (rest)
  linetoi = toi; linetois = (map linetoi).(T.words); linestoiss = map linetois
  toi t = toi0 0 t where toi0 n t = if (T.null) t then n else toi0 (10*n + fromIntegral(on (-) fromEnum (T.head t) '0') ) (T.tail t) ;

solve :: [Int] -> String
solve ns
 |a>b = "Alice"
 |b>a = "Bob"
 |otherwise = "Tie" where
 (a,b) = rec (reverse $ sort $ ns) (0,0)

rec [] (a,b)= (a,b)
rec [k] (a,b) = if even k then (a+k,b) else (a,b)
rec (k1:k2:ks) (a,b) = rec ks (if even k1 then a+k1 else a, if odd k2 then b+k2 else b)

import Control.Monad
import Data.List

main :: IO ()
main = do
  t <- read <$> getLine
  replicateM_ t $ do
    _n <- getLine
    a <- map read . words <$> getLine :: IO [Int]
    let
      eoT = sum [if  even x  then  x  else  0-x | x <- a]
      -- sort is so slow! hopefully OK with 2e5?
      abT = sum [x * y | (x, y) <- zip (cycle [1, 0-1]) (reverse $ sort a)]
    putStrLn $ case  compare 0 $ eoT + abT  of
      LT -> "Alice"
      EQ -> "Tie"
      GT -> "Bob"


{-# LANGUAGE FlexibleInstances, UndecidableInstances, DuplicateRecordFields #-}

module Main where

import Control.Monad
import System.IO

ca2 0 = 0
ca2 n = 1 + 2*(n-1)

calc x y = 2 * x + ca2 (y - x)

calculate x y = do
    let x1 = max x y
    let y1 = min x y
    calc y1 x1

main :: IO()
main = do
    t <- readLn :: IO Int
    forM_ [1..t] $ \t_itr -> do
        nmsTemp <- getLine
        let nms = words nmsTemp
        let x = read (nms !! 0) :: Int
        let y = read (nms !! 1) :: Int
        let result = calculate x y
        putStrLn(show result)

import Control.Monad

getInt = read `fmap` getLine :: IO Int
getInts = (map read . words) `fmap` getLine :: IO [Int]

main = do
	n <- getInt
	forM_ [1..n] $ \_ -> do
		[x, y] <- getInts
		let common = min x y
		let rest = x + y - common - common
		let answer = (common * 2) + (max 0 (rest * 2 - 1))
		print answer


import qualified Data.ByteString.Char8 as B
import Data.Maybe
import Data.List

main = do
    let int = fst .fromJust . B.readInteger
        ints = map int . B.words
    n <- readLn :: IO Int
    as <- ints <$> B.getLine
    let (b,c) = splitAt (n`div`2) $ sort as
    print $ sum $ map (^2) $ zipWith (+) b $ reverse c

-- {-# LANGUAGE BangPatterns #-}
-- {-# LANGUAGE BinaryLiterals #-}
-- {-# LANGUAGE MultiWayIf #-}
-- {-# LANGUAGE NumDecimals #-}
-- {-# LANGUAGE ScopedTypeVariables #-}
-- {-# LANGUAGE TupleSections #-}

import Control.Applicative
import Control.Arrow
import Control.Monad
-- import Control.Monad.ST

import Data.Char
import Data.List
import Data.Maybe

import qualified Data.ByteString.Char8 as B

-- import Data.Array.ST.Safe
-- import Data.STRef

-- import Debug.Trace
import Text.Printf

readInt = readLn :: IO Int
readInts = map ( fst . fromJust . B.readInteger ) . B.words <$> B.getLine
readNInts = readInt >>= flip replicateM readInt

which a b f = if f then a else b
mp [ a, b ] = ( a, b )

main = do
	n <- readInt
	as <- sort <$> readInts
	let
		rev = reverse as
	print $ sum $ map ( ^ 2 ) $ zipWith (+) as $ take ( n `div` 2 ) rev

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE TupleSections#-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE MultiWayIf #-}

import Data.Bits
import Data.List
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IM
import qualified Data.Array.IArray as A
import qualified Data.Array.MArray.Safe as A
-- import qualified Data.Array.MArray as A
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray)
import Data.Array.MArray.Safe (MArray)
import Data.Array.IO.Safe (IOArray, IOUArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
{-
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
-}
import Control.Monad
import Control.Monad.ST.Safe
-- import Control.Monad.ST
import Control.Applicative
import Data.Maybe
import Data.Tuple
import Data.Ord
import Control.Monad.State.Strict
import Data.Int
import Data.Word
import Data.STRef
import Data.IORef
import Data.Function
import System.IO
import System.Exit

main :: IO ()
main = do
  n <- read <$> getLine :: IO Int
  as_ <- unfoldr (BS.readInt . BS.dropWhile (<'!')) <$> BS.getLine
  let as = runSTUArray $ do
        as <- A.newListArray (0,n-1) (map fromIntegral as_ :: [Int64])
        sortMArray as
        return as
  print $ sum $ [ (as A.! i + as A.! (n-i-1))^2
                | i <- [0..n `shiftR` 1 - 1]]

type Comparison e = e -> e -> Ordering

{-# INLINE sortMArray #-}
sortMArray :: (MArray a e m, Ord e, A.Ix i, Integral i, Bits i)
           => a i e -> m ()
sortMArray = sortMArrayBy compare

{-# INLINE sortMArrayBy #-}
sortMArrayBy :: (MArray a e m, A.Ix i, Integral i, Bits i)
  => Comparison e -> a i e -> m ()
sortMArrayBy cmp = \arr -> A.getBounds arr >>= sortMArrayByBounds cmp arr

-- sort the interval [l,u] of arr.
{-# INLINE sortMArrayByBounds #-}
sortMArrayByBounds :: (MArray a e m, A.Ix i, Integral i, Bits i)
  => Comparison e -> a i e -> (i,i) -> m ()
sortMArrayByBounds cmp = \arr (l,u) -> sortMArrayByBounds_ cmp arr l (u+1)

-- sort the interval [l,u) of arr.
{-# INLINE sortMArrayByBounds_ #-}
sortMArrayByBounds_ :: (MArray a e m, A.Ix i, Integral i, Bits i)
  => Comparison e -> a i e -> i -> i -> m ()
sortMArrayByBounds_ cmp = \arr l u -> do
  let len = u - l
  tmp <- A.newArray_ (0,len `shiftR` 1 - 1)
  mergesort cmp arr tmp l u

mergesort cmp arr tmp l u
  | u - l <= 1 = return ()
  | otherwise  = do mergesort cmp arr tmp l mid
                    mergesort cmp arr tmp mid u
                    merge cmp arr tmp l mid u
    where
      len = u - l
      mid = (u + l) `shiftR` 1

{-# INLINE merge #-}
merge :: (MArray a e m, A.Ix i, Integral i, Bits i)
  => Comparison e -> a i e -> a i e -> i -> i -> i -> m ()
merge cmp arr tmp l mid u = do
  copyMArray arr (l,mid-1) tmp (0,mml-1)
  eLow <- A.readArray tmp 0
  eHigh <- A.readArray arr mid
  loop eLow 0 eHigh mid l
  where
    mml = mid - l
    loop !eLow !iLow !eHigh !iHigh !iIns = case cmp eHigh eLow of
      LT -> do A.writeArray arr iIns eHigh
               wroteHigh eLow iLow (iHigh+1) (iIns+1)
      _  -> do A.writeArray arr iIns eLow
               wroteLow (iLow+1) eHigh iHigh (iIns+1)
    wroteHigh !eLow !iLow !iHigh !iIns
      | iHigh >= u = copyMArray tmp (iLow,mml-1) arr (iIns,u-1)
      | otherwise  = do eHigh <- A.readArray arr iHigh
                        loop eLow iLow eHigh iHigh iIns
    wroteLow !iLow !eHigh !iHigh !iIns
      | iLow >= mml = return ()
      | otherwise   = do eLow <- A.readArray tmp iLow
                         loop eLow iLow eHigh iHigh iIns

{-# INLINE copyMArray #-}
copyMArray :: (MArray a0 e m, A.Ix i0, MArray a1 e m, A.Ix i1)
  => a0 i0 e -> (i0,i0) -> a1 i1 e -> (i1,i1) -> m ()
copyMArray src srcRange dst dstRange =
  forM_ (zip (A.range srcRange) (A.range dstRange))
  $ \(i,j) -> A.writeArray dst j =<< A.readArray src i

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE TupleSections#-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE MultiWayIf #-}

import Data.Bits
import Data.List
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IM
import qualified Data.Array.IArray as A
import qualified Data.Array.MArray.Safe as A
-- import qualified Data.Array.MArray as A
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray)
import Data.Array.MArray.Safe (MArray)
import Data.Array.IO.Safe (IOArray, IOUArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
{-
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
-}
import Control.Monad
import Control.Monad.ST.Safe
-- import Control.Monad.ST
import Control.Applicative
import Data.Maybe
import Data.Tuple
import Data.Ord
import Control.Monad.State.Strict
import Data.Int
import Data.Word
import Data.STRef
import Data.IORef
import Data.Function
import System.IO
import System.Exit

main :: IO ()
main = do
  n <- read <$> getLine :: IO Int
  as <- unfoldr (BS.readInt . BS.dropWhile (<'!')) <$> BS.getLine
  let (pre,suf) = splitAt (shiftR n 1) $ sort $ map fromIntegral as
        :: ([Int64],[Int64])
  print $ sum $ map (^2) $ zipWith (+) pre $ reverse suf

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

readInt = readLn :: IO Int
readInts = map ( fst . fromJust . B.readInt ) . B.words <$> B.getLine

div2 = (flip div) 2
zipEnd xs = zip xs (reverse $ drop (div2 (length xs)) xs)

solve :: [Int] -> Integer
solve = sum
      . map (^2)
      . map toInteger
      . map (uncurry (+))
      . zipEnd
      . sort

main :: IO ()
main = do
    _ <- readInt
    v <- readInts
    print $ solve v

import Data.Int
import Data.List
main = interact $ show . solve . map read . words
solve (n:as) = sum $ genericTake (n `div` 2) $ map (^2) $ zipWith (+) s (reverse s)
  where s = sort as :: [Int64]

import qualified Data.ByteString.Char8 as B
import Data.Maybe
import Data.List

main = do
    let int = fst .fromJust . B.readInt
        ints = map int . B.words
    n <- readLn :: IO Int
    as <- ints <$> B.getLine
    let (b,c) = splitAt (n`div`2) $ sort as
    print $ sum $ map (^2) $ zipWith (+) b $ reverse c

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE TupleSections#-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE MultiWayIf #-}

import Data.Bits
import Data.List
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IM
import qualified Data.Array.IArray as A
import qualified Data.Array.MArray.Safe as A
-- import qualified Data.Array.MArray as A
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray)
import Data.Array.MArray.Safe (MArray)
import Data.Array.IO.Safe (IOArray, IOUArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
{-
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
-}
import Control.Monad
import Control.Monad.ST.Safe
-- import Control.Monad.ST
import Control.Applicative
import Data.Maybe
import Data.Tuple
import Data.Ord
import Control.Monad.State.Strict
import Data.Int
import Data.Word
import Data.STRef
import Data.IORef
import Data.Function
import System.IO
import System.Exit

main :: IO ()
main = do
  n <- read <$> getLine :: IO Int
  as_ <- unfoldr (BS.readInt . BS.dropWhile (<'!')) <$> BS.getLine
  let as = runSTUArray $ do
        as <- A.newListArray (0,n-1) as_
        sortMArray as
        return as
  print $ sum $ [ (as A.! i + as A.! (n-i-1))^2
                | i <- [0..n `shiftR` 1 - 1]]

type Comparison e = e -> e -> Ordering

{-# INLINE sortMArray #-}
sortMArray :: (MArray a e m, Ord e, A.Ix i, Integral i, Bits i)
           => a i e -> m ()
sortMArray = sortMArrayBy compare

{-# INLINE sortMArrayBy #-}
sortMArrayBy :: (MArray a e m, A.Ix i, Integral i, Bits i)
  => Comparison e -> a i e -> m ()
sortMArrayBy cmp = \arr -> A.getBounds arr >>= sortMArrayByBounds cmp arr

-- sort the interval [l,u] of arr.
{-# INLINE sortMArrayByBounds #-}
sortMArrayByBounds :: (MArray a e m, A.Ix i, Integral i, Bits i)
  => Comparison e -> a i e -> (i,i) -> m ()
sortMArrayByBounds cmp = \arr (l,u) -> sortMArrayByBounds_ cmp arr l (u+1)

-- sort the interval [l,u) of arr.
{-# INLINE sortMArrayByBounds_ #-}
sortMArrayByBounds_ :: (MArray a e m, A.Ix i, Integral i, Bits i)
  => Comparison e -> a i e -> i -> i -> m ()
sortMArrayByBounds_ cmp = \arr l u -> do
  let len = u - l
  tmp <- A.newArray_ (0,len `shiftR` 1 - 1)
  mergesort cmp arr tmp l u

mergesort cmp arr tmp l u
  | u - l <= 1 = return ()
  | otherwise  = do mergesort cmp arr tmp l mid
                    mergesort cmp arr tmp mid u
                    merge cmp arr tmp l mid u
    where
      len = u - l
      mid = (u + l) `shiftR` 1

{-# INLINE merge #-}
merge :: (MArray a e m, A.Ix i, Integral i, Bits i)
  => Comparison e -> a i e -> a i e -> i -> i -> i -> m ()
merge cmp arr tmp l mid u = do
  copyMArray arr (l,mid-1) tmp (0,mml-1)
  eLow <- A.readArray tmp 0
  eHigh <- A.readArray arr mid
  loop eLow 0 eHigh mid l
  where
    mml = mid - l
    loop !eLow !iLow !eHigh !iHigh !iIns = case cmp eHigh eLow of
      LT -> do A.writeArray arr iIns eHigh
               wroteHigh eLow iLow (iHigh+1) (iIns+1)
      _  -> do A.writeArray arr iIns eLow
               wroteLow (iLow+1) eHigh iHigh (iIns+1)
    wroteHigh !eLow !iLow !iHigh !iIns
      | iHigh >= u = copyMArray tmp (iLow,mml-1) arr (iIns,u-1)
      | otherwise  = do eHigh <- A.readArray arr iHigh
                        loop eLow iLow eHigh iHigh iIns
    wroteLow !iLow !eHigh !iHigh !iIns
      | iLow >= mml = return ()
      | otherwise   = do eLow <- A.readArray arr iLow
                         loop eLow iLow eHigh iHigh iIns

{-# INLINE copyMArray #-}
copyMArray :: (MArray a0 e m, A.Ix i0, MArray a1 e m, A.Ix i1)
  => a0 i0 e -> (i0,i0) -> a1 i1 e -> (i1,i1) -> m ()
copyMArray src srcRange dst dstRange =
  forM_ (zip (A.range srcRange) (A.range dstRange))
  $ \(i,j) -> A.writeArray dst j =<< A.readArray src i

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE TupleSections#-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE MultiWayIf #-}

import Data.Bits
import Data.List
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IM
import qualified Data.Array.IArray as A
import qualified Data.Array.MArray.Safe as A
-- import qualified Data.Array.MArray as A
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray)
import Data.Array.MArray.Safe (MArray)
import Data.Array.IO.Safe (IOArray, IOUArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
{-
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
-}
import Control.Monad
import Control.Monad.ST.Safe
-- import Control.Monad.ST
import Control.Applicative
import Data.Maybe
import Data.Tuple
import Data.Ord
import Control.Monad.State.Strict
import Data.Int
import Data.Word
import Data.STRef
import Data.IORef
import Data.Function
import System.IO
import System.Exit

main :: IO ()
main = do
  n <- read <$> getLine :: IO Int
  as <- unfoldr (BS.readInt . BS.dropWhile (<'!')) <$> BS.getLine
  let (pre,suf) = splitAt (shiftR n 1) $ sort as
  print $ sum $ map (^2) $ zipWith (+) pre $ reverse suf

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE TupleSections#-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE MultiWayIf #-}

import Data.Bits
import Data.List
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IM
import qualified Data.Array.IArray as A
import qualified Data.Array.MArray.Safe as A
-- import qualified Data.Array.MArray as A
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray)
import Data.Array.MArray.Safe (MArray)
import Data.Array.IO.Safe (IOArray, IOUArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
{-
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
-}
import Control.Monad
import Control.Monad.ST.Safe
-- import Control.Monad.ST
import Control.Applicative
import Data.Maybe
import Data.Tuple
import Data.Ord
import Control.Monad.State.Strict
import Data.Int
import Data.Word
import Data.STRef
import Data.IORef
import Data.Function
import System.IO
import System.Exit

main :: IO ()
main = do
  n <- read <$> getLine :: IO Int
  as_ <- unfoldr (BS.readInt . BS.dropWhile (<'!')) <$> BS.getLine
  let as = runSTUArray $ do
        as <- A.newListArray (0,n-1) as_
        sortMArray as
        return as
  print $ sum $ [ (as A.! i + as A.! (n-i-1))^2
                | i <- [0..n `shiftR` 1 - 1]]

type Comparison e = e -> e -> Ordering

{-# INLINE sortMArray #-}
sortMArray :: (MArray a e m, Ord e, A.Ix i, Integral i, Bits i)
           => a i e -> m ()
sortMArray = sortMArrayBy compare

{-# INLINE sortMArrayBy #-}
sortMArrayBy :: (MArray a e m, A.Ix i, Integral i, Bits i)
  => Comparison e -> a i e -> m ()
sortMArrayBy cmp = \arr -> A.getBounds arr >>= sortMArrayByBounds cmp arr

-- sort the interval [l,u] of arr.
{-# INLINE sortMArrayByBounds #-}
sortMArrayByBounds :: (MArray a e m, A.Ix i, Integral i, Bits i)
  => Comparison e -> a i e -> (i,i) -> m ()
sortMArrayByBounds cmp = \arr (l,u) -> sortMArrayByBounds_ cmp arr l (u+1)

-- sort the interval [l,u) of arr.
{-# INLINE sortMArrayByBounds_ #-}
sortMArrayByBounds_ :: (MArray a e m, A.Ix i, Integral i, Bits i)
  => Comparison e -> a i e -> i -> i -> m ()
sortMArrayByBounds_ cmp = \arr l u -> do
  let len = u - l
  tmp <- A.newArray_ (0,len `shiftR` 1 - 1)
  mergesort cmp arr tmp l u

mergesort cmp arr tmp l u
  | u - l <= 1 = return ()
  | otherwise  = do mergesort cmp arr tmp l mid
                    mergesort cmp arr tmp mid u
                    merge cmp arr tmp l mid u
    where
      len = u - l
      mid = (u + l) `shiftR` 1

{-# INLINE merge #-}
merge :: (MArray a e m, A.Ix i, Integral i, Bits i)
  => Comparison e -> a i e -> a i e -> i -> i -> i -> m ()
merge cmp arr tmp l mid u = do
  copyMArray arr (l,mid-1) tmp (0,mml-1)
  eLow <- A.readArray tmp 0
  eHigh <- A.readArray arr mid
  loop eLow 0 eHigh mid l
  where
    mml = mid - l
    loop !eLow !iLow !eHigh !iHigh !iIns = case cmp eHigh eLow of
      LT -> do A.writeArray arr iIns eHigh
               wroteHigh eLow iLow (iHigh+1) (iIns+1)
      _  -> do A.writeArray arr iIns eLow
               wroteLow (iLow+1) eHigh iHigh (iIns+1)
    wroteHigh !eLow !iLow !iHigh !iIns
      | iHigh >= u = copyMArray tmp (iLow,mml-1) arr (iIns,u-1)
      | otherwise  = do eHigh <- A.readArray arr iHigh
                        loop eLow iLow eHigh iHigh iIns
    wroteLow !iLow !eHigh !iHigh !iIns
      | iLow >= mml = return ()
      | otherwise   = do eLow <- A.readArray tmp iLow
                         loop eLow iLow eHigh iHigh iIns

{-# INLINE copyMArray #-}
copyMArray :: (MArray a0 e m, A.Ix i0, MArray a1 e m, A.Ix i1)
  => a0 i0 e -> (i0,i0) -> a1 i1 e -> (i1,i1) -> m ()
copyMArray src srcRange dst dstRange =
  forM_ (zip (A.range srcRange) (A.range dstRange))
  $ \(i,j) -> A.writeArray dst j =<< A.readArray src i

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

readInt = readLn :: IO Int
readInts = map ( fst . fromJust . B.readInt ) . B.words <$> B.getLine

div2 = (flip div) 2
zipEnd xs = zip xs (reverse $ drop (div2 (length xs)) xs)

solve :: [Int] -> Int
solve = sum
      . map (^2)
      . map (uncurry (+))
      . zipEnd
      . sort

main :: IO ()
main = do
    _ <- readInt
    v <- readInts
    print $ solve v

import Data.List
main = interact $ show . solve . map read . words
solve (n:as) = sum $ take (n `div` 2) $ zipWith (*) s (reverse s)
  where s = sort as

import Data.List
main = interact $ show . solve . map read . words
solve (n:as) = sum $ take (n `div` 2) $ map (^2) $ zipWith (+) s (reverse s)
  where s = sort as


module Main where
import qualified Data.ByteString.Char8 as B
import Control.Monad
import Control.Applicative
import Data.Int

readInt = maybe undefined (fromIntegral . fst) . B.readInt
readInts = (readInt <$>) . B.words

chiến :: [Int64] -> Int64 -> Int64 -> Double
chiến as n k = sum $ map (\p -> (fromIntegral p) / d) $ scanl (\p (hf, hs) -> p - hf + hs) (sum f) $ zip (f ++ s) s
  where
    (f, s) = splitAt (fromIntegral k) as
    d = fromIntegral $ n - k + 1

main = do
  n:k:_ <- readInts <$> B.getLine
  as <- readInts <$> B.getLine
  print $ chiến as n k

-- Codeforces 808B

{-# OPTIONS_GHC -O2 #-}
{-# OPTIONS_GHC -optc-O2 #-}
{-# OPTIONS_GHC -optc-ffast-math #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE MultiWayIf #-}

import Control.Applicative
import Control.Monad
import qualified Data.ByteString.Char8 as BC
import Data.Int
import Data.List
import Data.Maybe

main :: IO ()
main = do
    (n:k:xs) <- (map (fromIntegral . fst . fromJust . BC.readInt) . BC.words) <$> BC.getContents :: IO [Int64]
    let v = min k (n - k + 1)
        s = sum $ zipWith (\x i -> if | i <= v -> i * x
                                      | i > v && i <= n - v -> v * x
                                      | i > n - v -> (n - i + 1) * x) xs [1..]
        s' = fromIntegral s :: Double
        t' = fromIntegral (n - k + 1) :: Double
    print (s' / t')

{-# OPTIONS_GHC -O3 #-}
{-# LANGUAGE ViewPatterns, ScopedTypeVariables #-}

run [] _ _ _ ans = ans
run (x:xs) i n k ans = run xs (i + 1) n k (fromIntegral ((minimum [n - i + 1, i, k, n - k + 1]) * x) + ans)

main = do
    (map read . words -> [n, k]) <- getLine
    (map read . words -> (xs :: [Integer])) <- getLine
    print $ run xs 1 n k 0.0 / fromIntegral (n - k + 1)

module Main where
import qualified Data.ByteString.Char8 as B
import Control.Monad
import Control.Applicative

readInt = maybe undefined fst . B.readInt
readInts = (readInt <$>) . B.words

chiến as n k = (fromIntegral $ sum $ scanl (\p (hf, hs) -> p - hf + hs) (sum f) $ zip (f ++ s) s) / (fromIntegral $ n - k + 1)
  where
    (f, s) = splitAt k as

main = do
  n:k:_ <- readInts <$> B.getLine
  as <- readInts <$> B.getLine
  print $ chiến as n k

module Main where
import qualified Data.ByteString.Char8 as B
import Control.Monad
import Control.Applicative

readInt = maybe undefined fst . B.readInt
readInts = (readInt <$>) . B.words

chiến as n k = sum $ map (\p -> (fromIntegral p) / d) $ scanl (\p (hf, hs) -> p - hf + hs) (sum f) $ zip (f ++ s) s
  where
    (f, s) = splitAt k as
    d = fromIntegral $ n - k + 1

main = do
  n:k:_ <- readInts <$> B.getLine
  as <- readInts <$> B.getLine
  print $ chiến as n k

-- Codeforces 808B

{-# OPTIONS_GHC -O2 #-}
{-# OPTIONS_GHC -optc-O2 #-}
{-# OPTIONS_GHC -optc-ffast-math #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE MultiWayIf #-}

import Control.Applicative
import Control.Monad
import qualified Data.ByteString.Char8 as BC
import Data.Int
import Data.List
import Data.Maybe

main :: IO ()
main = do
    (n:k:xs) <- (map (fromIntegral . fst . fromJust . BC.readInt) . BC.words) <$> BC.getContents :: IO [Int64]
    let s = sum $ zipWith (\x i -> if | i < k -> i * x
                                      | i >= k && i <= n - k -> k * x
                                      | i > n - k -> (n - i + 1) * x) xs [1..]
        s' = fromIntegral s :: Double
        t' = fromIntegral (n - k + 1) :: Double
    print (s' / t')

{-# OPTIONS_GHC -O3 #-}
{-# LANGUAGE ViewPatterns #-}

run [] _ _ _ ans = ans
run (x:xs) i n k ans = run xs (i + 1) n k (fromIntegral ((minimum [n - i + 1, i, k]) * x) + ans)

main = do
    (map read . words -> [n, k]) <- getLine
    (map read . words -> xs) <- getLine
    print $ run xs 1 n k 0.0 / fromIntegral (n - k + 1)


import Data.Char
import Data.List
import Control.Monad

result x = replicate (div (3*x) 4) '9' ++ replicate (x - (div (3*x) 4)) '8'

main = do
    tests <- getLine
    a <- getContents
    putStr $ intercalate "\n" [ result (read x :: Int) | x <- words a ]

module Main where

   solve :: IO ()
   solve = do
       s <- getLine
       let n = read s :: Int
           c = (n + 3) `div` 4
       putStrLn $ replicate (n - c) '9' ++ replicate c '8'

   iter :: Int -> IO ()
   iter 1 = solve
   iter n = do
       solve
       iter $ n - 1

   main :: IO ()
   main = do
       s <- getLine
       iter (read s :: Int)

{- This code was written by
Russell Emerine - linguist,
mathematician, coder,
musician, and metalhead. -}
import Control.Monad

main = getLine >>= flip replicateM_ test . read

test = do
  n <- read <$> getLine
  let c = (n - 1) `div` 4 + 1
  putStrLn $ replicate (n - c) '9' ++ replicate c '8'


base = 1000000007 :: Integer

n_choose_k :: Int -> Int -> Integer
n_choose_k n k = chooseTab !! n !! k

chooseTab :: [[Integer]]
chooseTab = [[ chooseDef n k | k <- [0..n]] | n <- [0..]]

chooseDef :: Int -> Int -> Integer
chooseDef n k
  | k == 0 || k == n = 1
  | k == 1           = toInteger n
  | otherwise        = mod (n_choose_k (n-1) (k-1) + n_choose_k (n-1) k) base

solveTask :: (Int, Int) -> Integer
solveTask (n, m) =
      let k' = 2*m
          n' = n + k' -1
      in n_choose_k n' k'

groupTaskInput :: [String] -> [(Int, Int)]
groupTaskInput [] = []
groupTaskInput (n:(d:xs)) = (read n, read d) : groupTaskInput(xs)

makeTaskInput :: String -> [(Int, Int)]
makeTaskInput s = groupTaskInput . words $ s

solve :: String -> String
solve s = concat $ (map (++ "\n") ) $ (map $ show.solveTask) $ makeTaskInput s

main' :: IO ()
main' = do interact $ solve

main = main'

import Data.Ord
import Data.Int
import Data.Sequence as DSeq (null, Seq( Empty ), (|>), (><), replicate)
import Data.Foldable
import Data.List
import Data.Maybe
import Data.Either
import Data.Array.ST.Safe
import Data.Array.Unboxed
import Control.Monad
import Control.Monad.ST.Safe
import Control.Monad.Trans.Except
import Control.Monad.Trans.Class
import Prelude hiding (showList)

import Text.Read (readEither)

getToken :: IO String
getToken =
    let aux s = do
        x <- getChar
        if x == ' ' || x == '\t' || x == '\n' then
            if DSeq.null s then
                aux s
            else
                return $ toList s
        else
            aux $ s |> x
    in aux DSeq.Empty

type EIO a = ExceptT String IO a

get :: Read a => EIO a
get = ExceptT $ liftM readEither getToken
puts :: String -> EIO ()
puts = lift . putStr
putsln :: String -> EIO ()
putsln = lift . putStrLn
put :: Show a => a -> EIO ()
put = putsln . show
putln :: Show a => a -> EIO ()
putln = putsln . show
showList :: Show a => [a] -> String
showList [] = []
showList (x : l) = shows x $ showChar ' ' $ showList l

main :: IO ()
main = do
    o <- runExceptT main_
    case o of
        Right () -> return ()
        Left s -> putStrLn(s)

main_ :: EIO ()
main_ = do
    (n, m) <- liftM2 (,) get get :: EIO (Int, Int)
    putln $ f1 n m

f1 :: Int -> Int -> Int64
f1 n m =    let a = runSTUArray $ do
                arr <- newArray ((0, 0), (n+1, m)) 0 :: ST s (STUArray s (Int, Int) Int64)
                forM_ [0..n] $ \j -> writeArray arr (j, 0) 1
                forM_ [1..m-1] $ \i ->
                    forM_ [0..n] $ \j ->
                        forM_ [0..j] $ \k -> do
                            a <- readArray arr (j, i)
                            b <- readArray arr (k, i - 1)
                            writeArray arr (j, i) $ (mod (a + b) 1000000007)
                return arr in
            let v = do
                x <- [1..n]
                y <- [1..n]
                [(x, y)] in
            let x = filter (\(x, y) -> x <= y) v in
            foldl (\s (x, y) -> mod (s + (a ! (x - 1, m - 1)) * (a ! (n - y, m - 1))) 1000000007) 0 x

import System.IO
import Data.Array

readInt :: IO Int
readInt = readLn

castToInteger x = read x::Integer

main = do
    line <- getLine
    let test_case = (map castToInteger . take 2 . words) line
    solveTask test_case

solveTask :: [Integer] -> IO ()
solveTask [n, m] = do
    let dpVs = getDp m n
    let res = foldl modSum 0 [
                modProd (dpVs!i) (dpVs!(n-j+1)) |
                i <- [1..n], j <- [i..n]]
    print res
    return ()

-- getDp i n =
--     array (1,n) [(j, foldl modSum 0 [dp!(j') | j' <- [1..j]]) | j <- [1..n]]
--     where dp = getDp (i-1) n
getDp :: Integer -> Integer -> Array Integer Integer
getDp i n = array (1,n) $ enumerate $ getDpList i n

getDpList :: Integer -> Integer -> [Integer]
getDpList 1 n = [1 | i <- [1..n]]
getDpList i n = prefSum dp
    where dp = getDpList (i-1) n

prefSum :: [Integer] -> [Integer]
prefSum xs = prefSum' 0 xs
prefSum' agg [] = []
prefSum' agg (x:xs) = s : prefSum' s xs
    where s = modSum agg x

enumerate :: [Integer] -> [(Integer, Integer)]
enumerate xs = enumerate' 1 xs
enumerate' i [] = []
enumerate' i (x:xs) = (i, x) : enumerate' (i+1) xs

customMod :: Integer
customMod = 10^9 + 7

modSum :: Integer -> Integer -> Integer
modSum a b = if a + b < customMod then a + b else a + b - customMod

modProd :: Integer -> Integer -> Integer
modProd a b = (a * b) `mod` customMod

count :: [Integer] -> Integer
count [] = 0
count (x:xs) = 1 + count xs

import System.IO
import Data.Array

readInt :: IO Int
readInt = readLn

castToInteger x = read x::Integer

main = do
    line <- getLine
    let test_case = (map castToInteger . take 2 . words) line
    solveTask test_case

solveTask :: [Integer] -> IO ()
solveTask [n, m] = do
    let dpVs = getDp m n
    let res = foldl modSum 0 [
                modProd (dpVs!i) (dpVs!(n-j+1)) |
                i <- [1..n], j <- [i..n]]
    print res
    return ()

getDp :: Integer -> Integer -> Array Integer Integer
getDp 1 n = array (1,n) [(i, 1) | i <- [1..n]]
getDp i n =
    array (1,n) [(j, foldl modSum 0 [dp!(j') | j' <- [1..j]]) | j <- [1..n]]
    where dp = getDp (i-1) n

customMod :: Integer
customMod = 10^9 + 7

modSum :: Integer -> Integer -> Integer
modSum a b = if a + b < customMod then a + b else a + b - customMod

modProd :: Integer -> Integer -> Integer
modProd a b = (a * b) `mod` customMod

count :: [Integer] -> Integer
count [] = 0
count (x:xs) = 1 + count xs

import Control.Monad
import Data.Array
import Data.List
import qualified Data.Map.Strict as M

input :: Read a => IO [a]
input = map read <$> words <$> getLine

output :: Show a => [a] -> IO ()
output = putStrLn <$> unwords <$> map show

c n k = product [n-k+1..n] `div` product [1..k]

_mod = 1000000007

main = do
  [n,m] <- input
  print (c (2*m+n-1) (2*m) `mod` _mod)

iter :: [Integer] -> [Integer]
iter xs = tail $ scanl (+) 0 xs

cal :: Int -> Int -> [Integer]
cal n 1 =  take n $ repeat 1
cal n m =
  let ls = cal n 1
      itf = foldl (.) (id) $ take (m - 1) $ repeat (iter)
  in
      itf ls
--   let a = cal n (m - 1)
--       b = cal (n - 1) m
--       v = foldr (+) 0 a
--   in
--       b ++ [v]

calSum :: Int -> Int -> Integer -> Integer
calSum n m d =
  let
    xs = map (`mod` d) $ cal n m
    ss = reverse $ tail $ scanl (+) 0 xs
  in
    (`mod` d) . sum $ zipWith ((*)) xs ss

main :: IO ()
main = do
  line <- getLine
  let (n:m:[]) = map (read :: String -> Int) $ words line
  putStrLn $ show $ calSum n m 1000000007

{- This code was written by
Russell Emerine - linguist,
mathematician, coder,
musician, and metalhead. -}
import Data.Array.IArray

a # b = chooses ! (a, b)

chooses :: Array (Int, Int) Integer
chooses = array ((0, 0), (1100, 1100)) [((a, b), choose a b) | a <- [0..1100], b <- [0..1100]]

choose a b
  | b < 0 || b > a = 0
  | b == 0 || a == b = 1
  | otherwise = mod ((a - 1) # (b - 1) + (a - 1) # b) 1000000007

main = getContents >>= print.(\(n:m:_) -> mod (sum [(i + m - 1) # (m - 1) * (n - j + m - 2) # (m - 1) | i <- [0..n - 1], j <- [i..n - 1]]) 1000000007).map read.words

import Data.Ord
import Data.Int
import Data.Sequence as DSeq (null, Seq( Empty ), (|>), (><), replicate)
import Data.Foldable
import Data.List
import Data.Maybe
import Data.Either
import Data.Array.ST.Safe
import Data.Array.Unboxed
import Control.Monad
import Control.Monad.ST.Safe
import Control.Monad.Trans.Except
import Control.Monad.Trans.Class
import Prelude hiding (showList)

import Text.Read (readEither)

getToken :: IO String
getToken =
    let aux s = do
        x <- getChar
        if x == ' ' || x == '\t' || x == '\n' then
            if DSeq.null s then
                aux s
            else
                return $ toList s
        else
            aux $ s |> x
    in aux DSeq.Empty

type EIO a = ExceptT String IO a

get :: Read a => EIO a
get = ExceptT $ liftM readEither getToken
puts :: String -> EIO ()
puts = lift . putStr
putsln :: String -> EIO ()
putsln = lift . putStrLn
put :: Show a => a -> EIO ()
put = putsln . show
putln :: Show a => a -> EIO ()
putln = putsln . show
showList :: Show a => [a] -> String
showList [] = []
showList (x : l) = shows x $ showChar ' ' $ showList l

main :: IO ()
main = do
    o <- runExceptT main_
    case o of
        Right () -> return ()
        Left s -> putStrLn(s)

main_ :: EIO ()
main_ = do
    (n, m) <- liftM2 (,) get get :: EIO (Int, Int)
    putln $ f1 n m

f1 :: Int -> Int -> Int64
f1 n m =    let ix a b = a * m + b in
            let a = runSTUArray $ do
                arr <- newArray (0, (n + 1) *  m) 0 :: ST s (STUArray s Int Int64)
                forM_ [0..n] $ \j -> writeArray arr (ix j 0) 1
                forM_ [1..m-1] $ \i ->
                    forM_ [0..n] $ \j ->
                        forM_ [0..j] $ \k -> do
                            a <- readArray arr (ix j i)
                            b <- readArray arr (ix k (i - 1))
                            writeArray arr (ix j i) $ (mod (a + b) 1000000007)
                return arr in
            let v = do
                x <- [1..n]
                y <- [1..n]
                [(x, y)] in
            let x = filter (\(x, y) -> x <= y) v in
            foldl (\s (x, y) -> mod (s + (a ! (ix (x - 1) (m - 1))) * (a ! (ix (n - y) (m - 1)))) 1000000007) 0 x

import Data.Ord
import Data.Int
import Data.Sequence as DSeq (null, Seq( Empty ), (|>), (><), replicate)
import Data.Foldable
import Data.List
import Data.Maybe
import Data.Either
import Data.Array.ST.Safe
import Data.Array.Unboxed
import Control.Monad
import Control.Monad.ST.Safe
import Control.Monad.Trans.Except
import Control.Monad.Trans.Class
import Prelude hiding (showList)
import Text.Read (readEither)

getToken :: IO String
getToken =
    let aux s = do
        x <- getChar
        if x == ' ' || x == '\t' || x == '\n' then
            if DSeq.null s then
                aux s
            else
                return $ toList s
        else
            aux $ s |> x
    in aux DSeq.Empty

type EIO a = ExceptT String IO a

get :: Read a => EIO a
get = ExceptT $ liftM readEither getToken
puts :: String -> EIO ()
puts = lift . putStr
putsln :: String -> EIO ()
putsln = lift . putStrLn
put :: Show a => a -> EIO ()
put = putsln . show
putln :: Show a => a -> EIO ()
putln = putsln . show
showList :: Show a => [a] -> String
showList [] = []
showList (x : l) = shows x $ showChar ' ' $ showList l

main :: IO ()
main = do
    o <- runExceptT main_
    case o of
        Right () -> return ()
        Left s -> putStrLn(s)

main_ :: EIO ()
main_ = do
    (n, m) <- liftM2 (,) get get :: EIO (Int, Int)
    putln $ f1 n m

f1 :: Int -> Int -> Int64
f1 n m =    let a = runSTArray $ do
                arr <- newArray ((0, 0), (n+1, m)) 0 :: ST s (STArray s (Int, Int) Int64)
                forM_ [0..n] $ \j -> writeArray arr (j, 0) 1
                forM_ [1..m-1] $ \i ->
                    forM_ [0..n] $ \j ->
                        forM_ [0..j] $ \k -> do
                            a <- readArray arr (j, i)
                            b <- readArray arr (k, i - 1)
                            writeArray arr (j, i) $ (mod (a + b) 1000000007)
                return arr in
            let v = do
                x <- [1..n]
                y <- [1..n]
                [(x, y)] in
            let x = filter (\(x, y) -> x <= y) v in
            foldl (\s (x, y) -> mod (s + (a ! (x - 1, m - 1)) * (a ! (n - y, m - 1))) 1000000007) 0 x


get_my_list :: Int -> [Int]
get_my_list 1 = [1]
get_my_list 2 = [1,2]
get_my_list 3 = [2,1,3]
get_my_list n =
    case (mod n 3) of
        0 -> 1:2:4:3:[5..n]
        1 -> 2:1:[3..n]
        2 -> [1..n]

ints_to_str :: [Int] -> String
ints_to_str = unwords . map show

input_to_output :: String -> IO ()
input_to_output = (putStrLn . ints_to_str . get_my_list . read)

test_case_func :: (Num a,Eq a) => a -> IO ()
test_case_func 0 = return ()
test_case_func t = do
    n <- getLine
    input_to_output n
    test_case_func (t - 1)

main :: IO ()
main = do
    ts <- getLine
    test_case_func $ read ts

import Control.Monad (replicateM_)
main = read <$> getLine >>= flip replicateM_ scase
scase = read <$> getLine >>=
        \n -> mapM_ (\x -> putStr (show x ++ " ")) (getPerm n) >>
              putStrLn ""
getPerm n | mod n 2 == 0 = reverse [1..n-2] ++ [n-1, n]
          | otherwise    = [n-3, n-2] ++ reverse [1..n-4] ++ [n-1, n]

--
-- Michael V. Antosha
-- 2022
-- Michael.Antosha@gmail.com
--

{-# language NoMonomorphismRestriction #-}

{-# options_ghc -O3                              #-}

{-# options_ghc -Weverything                     #-}

{-# options_ghc -Wno-safe                        #-}     -- no warning if inferred as safe
{-# options_ghc -Wno-all-missed-specialisations  #-}     -- "Could not specialise..." when -O1
{-# options_ghc -Wno-missing-import-lists        #-}
{-# options_ghc -Wno-missing-local-signatures    #-}

-- (ghc -O1 -no-keep-o-files -no-keep-hi-files -rtsopts -o a.out *.hs  &&  set -o pipefail  &&  cat -- input.txt | ./a.out  &&  cat -- permutscore.hs | xclip -selection clipboard  &&  echo COPIED  ||  echo ERROR)

import           Prelude               hiding (getLine)
import           Control.Arrow
import           Control.Monad
import qualified Data.ByteString.Char8 as B
import           Data.Char
import           Data.List

ri = readInts <$> B.getLine :: IO [Int]
  where
    readInts  = unfoldr ((second dropSpace <$>) . B.readInt) . dropSpace
    dropSpace = B.dropWhile isSpace

showL = unwords . map show

main :: IO ()
main = do
    [t] <- ri
    tcs <- replicateM t $ do
        [n] <- ri
        return n
    mapM_ (putStrLn.showL.solve) tcs

solve n = answ
  where
    (x:xs,ys) = splitAt (n - 2) [1..n]

    answ | (even n)   =        reverse xs ++ [x] ++ ys
         | otherwise  = [x] ++ reverse xs        ++ ys

{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TupleSections #-}
module Main where

import Control.Monad (liftM2, replicateM_, replicateM, foldM, when, liftM3)
import Data.Char (digitToInt, toLower)
import Data.Maybe (fromJust, fromMaybe)
import qualified Data.ByteString.Char8 as C
import Data.Map.Strict (Map, insert, alter, empty, (!), (!?), fromList, fromListWith, mapWithKey, findWithDefault, unionWithKey, traverseWithKey, mapKeys, filterWithKey, mapKeysWith)
import Data.List (group, sort, intercalate)
import Data.Set (Set, deleteFindMin, union)
import qualified Data.Set as Set
import qualified Data.Map as Map
import Data.Function (on)
import Data.Array.MArray (newArray, readArray, writeArray, newListArray)
import Data.Array.IO (IOUArray, getElems)
import Data.Bool (bool)
import Data.Foldable (foldlM, Foldable (foldl'), find, sequenceA_, foldrM)
import Control.Arrow ((&&&), Arrow ((***)))
import Data.Coerce (coerce)
import Data.Set.Internal(Set(Bin, Tip))
import Debug.Trace (traceShowId, traceShow)
import Data.Foldable (maximumBy)
import Data.Ord (comparing)
import qualified Data.IntMap as IntMap
import Control.Monad.Trans.Class
import Control.Exception (assert)
import Data.String

-- type IMap = Set (Int, Int)
type Domain = Int
type Solver  = Domain -> IO ()

readChar8 :: IO [Int]
readChar8 = fmap (fst . fromJust) . fmap C.readInt . C.words <$> fromString <$> getLine

parse :: IO Domain
parse = do
  [n] <- readChar8
  -- print (n, xs)
  return n

enumerate :: [a] -> [(Int, a)]
enumerate xs = zip [1..length xs] xs

newtype I = I (Int,Int)

-- try for the automatically generated if not fixed
solve :: Solver
solve n = putStrLn $ intercalate " " $ map show $ go xs
  where xs = [1..n]
        go :: [Int] -> [Int]
        go [] = []
        go [x] = [x]
        go [x, y] = [x, y]
        go [x, y, z] = [y, x, z]
        go [a, b, x, y, z] = [a, x, b, y, z]
        go (x:y:xs) = y:x:go xs

main :: IO ()
main = do
  [n] <- readChar8
  sequence_ =<< (replicateM n $ solve <$> parse)

{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TupleSections #-}
module Main where

import Control.Monad (liftM2, replicateM_, replicateM, foldM, when, liftM3)
import Data.Char (digitToInt, toLower)
import Data.Maybe (fromJust, fromMaybe)
import qualified Data.ByteString.Char8 as C
import Data.Map.Strict (Map, insert, alter, empty, (!), (!?), fromList, fromListWith, mapWithKey, findWithDefault, unionWithKey, traverseWithKey, mapKeys, filterWithKey, mapKeysWith)
import Data.List (group, sort, intercalate)
import Data.Set (Set, deleteFindMin, union)
import qualified Data.Set as Set
import qualified Data.Map as Map
import Data.Function (on)
import Data.Array.MArray (newArray, readArray, writeArray, newListArray)
import Data.Array.IO (IOUArray, getElems)
import Data.Bool (bool)
import Data.Foldable (foldlM, Foldable (foldl'), find, sequenceA_, foldrM)
import Control.Arrow ((&&&), Arrow ((***)))
import Data.Coerce (coerce)
import Data.Set.Internal(Set(Bin, Tip))
import Debug.Trace (traceShowId, traceShow)
import Data.Foldable (maximumBy)
import Data.Ord (comparing)
import qualified Data.IntMap as IntMap
import Control.Monad.Trans.Class
import Control.Exception (assert)
import Data.String

-- type IMap = Set (Int, Int)
type Domain = Int
type Solver  = Domain -> IO ()

readChar8 :: IO [Int]
readChar8 = fmap (fst . fromJust) . fmap C.readInt . C.words <$> fromString <$> getLine

parse :: IO Domain
parse = do
  [n] <- readChar8
  -- print (n, xs)
  return n

enumerate :: [a] -> [(Int, a)]
enumerate xs = zip [1..length xs] xs

newtype I = I (Int,Int)

-- try for the automatically generated if not fixed
solve :: Solver
solve n = putStrLn $ intercalate " " $ map show $ go xs
  where xs = [1..n]
        go :: [Int] -> [Int]
        go [] = []
        go [x] = [x]
        go [x, y] = [x, y]
        go [x, y, z] = [y, x, z]
        go [a, b, x, y, z] = [a, x, b, y, z]
        go (x:y:xs) = y:x:xs

main :: IO ()
main = do
  [n] <- readChar8
  sequence_ =<< (replicateM n $ solve <$> parse)

{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TupleSections #-}
module Main where

import Control.Monad (liftM2, replicateM_, replicateM, foldM, when, liftM3)
import Data.Char (digitToInt, toLower)
import Data.Maybe (fromJust, fromMaybe)
import qualified Data.ByteString.Char8 as C
import Data.Map.Strict (Map, insert, alter, empty, (!), (!?), fromList, fromListWith, mapWithKey, findWithDefault, unionWithKey, traverseWithKey, mapKeys, filterWithKey, mapKeysWith)
import Data.List (group, sort, intercalate)
import Data.Set (Set, deleteFindMin, union)
import qualified Data.Set as Set
import qualified Data.Map as Map
import Data.Function (on)
import Data.Array.MArray (newArray, readArray, writeArray, newListArray)
import Data.Array.IO (IOUArray, getElems)
import Data.Bool (bool)
import Data.Foldable (foldlM, Foldable (foldl'), find, sequenceA_, foldrM)
import Control.Arrow ((&&&), Arrow ((***)))
import Data.Coerce (coerce)
import Data.Set.Internal(Set(Bin, Tip))
import Debug.Trace (traceShowId, traceShow)
import Data.Foldable (maximumBy)
import Data.Ord (comparing)
import qualified Data.IntMap as IntMap
import Control.Monad.Trans.Class
import Control.Exception (assert)
import Data.String

-- type IMap = Set (Int, Int)
type Domain = Int
type Solver  = Domain -> IO ()

readChar8 :: IO [Int]
readChar8 = fmap (fst . fromJust) . fmap C.readInt . C.words <$> fromString <$> getLine

parse :: IO Domain
parse = do
  [n] <- readChar8
  -- print (n, xs)
  return n

enumerate :: [a] -> [(Int, a)]
enumerate xs = zip [1..length xs] xs

newtype I = I (Int,Int)

-- try for the automatically generated if not fixed
solve :: Solver
solve n = putStrLn $ intercalate " " $ map show $ go xs
  where xs = [1..n]
        go :: [Int] -> [Int]
        go [] = []
        go [x] = [x]
        go [x, y] = [x, y]
        go [x, y, z] = [y, x, z]
        go [a, b, x, y, z] = [a, b, x, y, z]
        go (x:y:xs) = y:x:xs

main :: IO ()
main = do
  [n] <- readChar8
  sequence_ =<< (replicateM n $ solve <$> parse)

get_my_list :: Int -> [Int]
get_my_list 1 = [1]
get_my_list 2 = [1,2]
get_my_list 3 = [2,1,3]
get_my_list n =
    case (mod n 3) of
        0 -> 1:2:4:3:[5..n]
        1 -> 2:1:[3..n]
        2 -> [1..n]

ints_to_str :: [Int] -> String
ints_to_str = unwords . map show

input_to_output :: String -> IO ()
input_to_output = (print . ints_to_str . get_my_list . read)

test_case_func :: (Num a,Eq a) => a -> IO ()
test_case_func 0 = return ()
test_case_func t = do
    n <- getLine
    input_to_output n
    test_case_func (t - 1)

main :: IO ()
main = do
    ts <- getLine
    test_case_func $ read ts