{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns, TupleSections, OverloadedStrings #-}
import Control.Applicative
import Control.Exception
import Control.Monad
import Control.Monad.ST
import Data.Array.Base
import Data.Array.ST (runSTUArray, STArray, runSTArray)
import Data.Bits
import qualified Data.ByteString.Char8 as B
import Data.Char
import Data.Function
import Data.Int
import Data.List
import qualified Data.Map as M
import qualified Data.Set as S
import Data.STRef
import GHC.Arr (Array, Ix, range, unsafeIndex)
bool :: a -> a -> Bool -> a
bool t f b=if b then t else f
readInt :: B.ByteString -> Int
readInt bs=case B.readInt bs of{Just(n,_)->n;_->error$"readInt error : bs = "++show bs;}
rep, rev :: Monad m => Int -> (Int -> m ()) -> m ()
rep !n f=go 0 where go !i=when(i<n)$f i>>go(i+1)
rev !n f=go(n-1)where go !i=when(i>=0)$f i>>go(i-1)
{-# INLINE rep #-}
{-# INLINE rev #-}
for :: Monad m => Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> m ()) -> m ()
for !i0 p next f=go i0 where go !i=when(p i)$f i>>go(next i)
{-# INLINE for #-}
modifyArray :: (MArray a e m, Ix i) => a i e -> i -> (e -> e) -> m ()
modifyArray a i f=readArray a i>>=writeArray a i.f
{-# INLINE modifyArray #-}
unsafeModify :: (MArray a e m, Ix i) => a i e -> Int -> (e -> e) -> m ()
unsafeModify a i f=unsafeRead a i>>=unsafeWrite a i.f
{-# INLINE unsafeModify #-}

data Point = P {-# UNPACK #-} !Int64 {-# UNPACK #-} !Int64 deriving (Eq, Ord)

instance Show Point where
  show (P x y) = show (x,y)

instance Num Point where
  (P x0 y0) + (P x1 y1) = P (x0 + x1) (y0 + y1)
  (P x0 y0) - (P x1 y1) = P (x0 - x1) (y0 - y1)
  (P x0 y0) * (P x1 y1) = P (x0 * x1 - y0 * y1) (x0 * y1 + x1 * y0)
  negate (P x y) = P (negate x) (negate y)
  abs _ = undefined
  signum _ = undefined
  fromInteger n = P (fromIntegral n) 0

infixr 7 *:
(*:) :: Int64 -> Point -> Point
(*:) !k (P x y) = P (k * x) (k * y)
{-# INLINE (*:) #-}

dot :: Point -> Point -> Int64
dot (P x0 y0) (P x1 y1) = x0 * x1 + y0 * y1
{-# INLINE dot #-}

main :: IO ()
main = do
  _n <- getLine
  xs <- map (fromIntegral.readInt).B.words <$> B.getContents
  putStr.unlines.map show. solve $ perse xs

perse :: [Int64] -> [[(Point, Point)]]
perse (x0:y0:a0:b0:x1:y1:a1:b1:x2:y2:a2:b2:x3:y3:a3:b3:rest) = [(P x0 y0,P a0 b0)
                                                               ,(P x1 y1,P a1 b1)
                                                               ,(P x2 y2,P a2 b2)
                                                               ,(P x3 y3,P a3 b3)
                                                               ] : perse rest
perse _ = []

rot0, rot90, rot180, rot270 :: Point -> Point -> Point
rot0 _ xy = xy
rot90 (P a b) (P x y)= P ((b - y) + a) ((x - a) + b)
rot180 p = rot90 p.rot90 p
rot270 p = rot90 p.rot90 p.rot90 p
{-# INLINE rot0 #-}
{-# INLINE rot90 #-}
{-# INLINE rot180 #-}
{-# INLINE rot270 #-}

isSquare :: [Point] -> Bool
isSquare points = and [ v `dot` v == u `dot` u
                      , v `dot` u == 0
                      , p3 == p1 + (p2 - p0)
                      , v /= P 0 0
                      ]
   where
     [p0,p1,p2,p3] = sort points
     !v = p1 - p0
     !u = p2 - p0

solve :: [[(Point,Point)]] -> [Int]
solve moles = map query moles
   where
     query moles = case res of
                     [] -> (-1)
                     res -> minimum res
       where
         res = map (sum.map fst) $ filter p [zipWith (#) fs moles|fs <- replicateM 4 [(0,rot0),(1,rot90),(2,rot180),(3,rot270)]]
     (i,rot)#(xy, ab) = (i, rot ab xy)
     p ixys = isSquare $ map snd ixys

import Data.List
import Data.Function
import Data.Char
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B

getIntArray :: B.ByteString -> [Int]
getIntArray = map fst . mapMaybe B.readInt . B.words

main = interact $ unlines . map show . solve . tail . map (map read . words) . lines

rotate0 (x, y) n
	| n == 0 = (x, y)
	| n == 1 = (-y, x)
	| n == 2 = (-x, -y)
	| otherwise = (y, -x)

rotate (x:y:a:b:_) n = (tx + a, ty + b)
					where (tx, ty) = rotate0 (x - a, y - b) n

minimum' [] = -1
minimum' xs = minimum xs

dis (x, y) (x1, y1) = (x - x1) ^ 2 + (y - y1) ^ 2

isSquare xs = isSqure' ss
		where ss = group $ sort [dis a b | a <- xs, b <- xs];
			  isSqure' (a:b:c:[]) = head a == 0 && head b * 2 == head c && length a == 4 && length b == 8 && length c == 4;
			  isSqure' _ = False

solve :: [[Integer]] -> [Integer]
solve [] = []
solve (m1:m2:m3:m4:xs) = res : solve xs
		where res = minimum' [n1 + n2 + n3 + n4 | n1 <- [0..3], n2 <- [0..3], n3 <- [0..3], n4 <- [0..3],
														isSquare [rotate m1 n1, rotate m2 n2, rotate m3 n3, rotate m4 n4]]

import           Control.Monad
import           Data.List

data Vector = Vector Int Int Int Int deriving (Show, Eq)

rotate :: Vector -> Vector
rotate (Vector a b x y) = Vector a b (a - beta) (b + alpha)
    where
        alpha = x - a; beta = y - b

isSquare :: [Vector] -> Bool
isSquare vecs@(v1:vs@[v2, v3, v4]) =
        dis_x /= -1 &&
        dist vb vd == dis_x &&
        dist vc vb == dis_z && dist vc vd == dis_z &&
        dis_x /=0 && dis_z /= 0
    where
        dist (Vector _ _ x y) (Vector _ _ x' y') = (x'-x)^2+(y'-y)^2
        [d1,d2,d3] = map (dist v1) vs
        (dis_z, dis_x, vc)
            | d1 == d2 = (d1, d3, v4)
            | d2 == d3 = (d2, d1, v2)
            | d1 == d3 = (d1, d2, v3)
            | otherwise = (-1, -1, v1)
        [vb, vd] = delete vc vs

main :: IO ()
main = do
    n <- fmap (head . map read . words) getLine
    forM_ [1..n] (\_ -> do
            moles <- replicateM 4 $ do
                [x, y, a, b] <- fmap (map read . words) getLine
                return $ Vector a b x y
            let squares = filter (isSquare . map snd) $ mapM (zip [0..] . take 4 . iterate rotate) moles
            print $ if null squares
                then -1
                else minimum $ map (sum . map fst) squares
        )

import           Control.Monad
import           Data.List

data Vector = Vector Int Int Int Int deriving (Show, Eq)

rotate :: Vector -> Vector
rotate (Vector a b x y) = Vector a b (a - beta) (b + alpha)
    where
        alpha = x - a; beta = y - b

isSquare :: [Vector] -> Bool
isSquare vecs@(v1:vs@[v2, v3, v4]) =
        dis_x /= -1 &&
        dist vb vd == dis_x &&
        dist vc vb == dis_z && dist vc vd == dis_z &&
        not (and $ zipWith (==) vecs (tail vecs))
    where
        dist (Vector _ _ x y) (Vector _ _ x' y') = (x'-x)^2+(y'-y)^2
        [d1,d2,d3] = map (dist v1) vs
        (dis_z, dis_x, vc)
            | d1 == d2 = (d1, d3, v4)
            | d2 == d3 = (d2, d1, v2)
            | d1 == d3 = (d1, d2, v3)
            | otherwise = (-1, -1, v1)
        [vb, vd] = delete vc vs

main :: IO ()
main = do
    n <- fmap (head . map read . words) getLine
    forM_ [1..n] (\_ -> do
            moles <- replicateM 4 $ do
                [x, y, a, b] <- fmap (map read . words) getLine
                return $ Vector a b x y
            let squares = filter (isSquare . map snd) $ mapM (zip [0..] . take 4 . iterate rotate) moles
            print $ if null squares
                then -1
                else minimum $ map (sum . map fst) squares
        )

import Data.List
import Data.Function
import Data.Char
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B

getIntArray :: B.ByteString -> [Int]
getIntArray = map fst . mapMaybe B.readInt . B.words

main = interact $ unlines . map show . solve . tail . map (map read . words) . lines

rotate0 (x, y) n
	| n == 0 = (x, y)
	| n == 1 = (-y, x)
	| n == 2 = (-x, -y)
	| otherwise = (y, -x)

rotate (x:y:a:b:_) n = (tx + a, ty + b)
					where (tx, ty) = rotate0 (x - a, y - b) n

minimum' [] = -1
minimum' xs = minimum xs

dis (x, y) (x1, y1) = (x - x1) ^ 2 + (y - y1) ^ 2

isSquare xs = isSqure' ss
		where ss = group $ sort [dis a b | a <- xs, b <- xs];
			  isSqure' (a:b:c:[]) = head a == 0 && head b * 2 == head c && length a == 4 && length b == 8 && length c == 4;
			  isSqure' _ = False

solve :: [[Int]] -> [Int]
solve [] = []
solve (m1:m2:m3:m4:xs) = res : solve xs
		where res = minimum' [n1 + n2 + n3 + n4 | n1 <- [0..3], n2 <- [0..3], n3 <- [0..3], n4 <- [0..3],
														isSquare [rotate m1 n1, rotate m2 n2, rotate m3 n3, rotate m4 n4]]

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns, TupleSections, OverloadedStrings #-}
import Control.Applicative
import Control.Exception
import Control.Monad
import Control.Monad.ST
import Data.Array.Base
import Data.Array.ST (runSTUArray, STArray, runSTArray)
import Data.Bits
import qualified Data.ByteString.Char8 as B
import Data.Char
import Data.Function
import Data.Int
import Data.List
import qualified Data.Map as M
import qualified Data.Set as S
import Data.STRef
import GHC.Arr (Array, Ix, range, unsafeIndex)
bool :: a -> a -> Bool -> a
bool t f b=if b then t else f
readInt :: B.ByteString -> Int
readInt bs=case B.readInt bs of{Just(n,_)->n;_->error$"readInt error : bs = "++show bs;}
rep, rev :: Monad m => Int -> (Int -> m ()) -> m ()
rep !n f=go 0 where go !i=when(i<n)$f i>>go(i+1)
rev !n f=go(n-1)where go !i=when(i>=0)$f i>>go(i-1)
{-# INLINE rep #-}
{-# INLINE rev #-}
for :: Monad m => Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> m ()) -> m ()
for !i0 p next f=go i0 where go !i=when(p i)$f i>>go(next i)
{-# INLINE for #-}
modifyArray :: (MArray a e m, Ix i) => a i e -> i -> (e -> e) -> m ()
modifyArray a i f=readArray a i>>=writeArray a i.f
{-# INLINE modifyArray #-}
unsafeModify :: (MArray a e m, Ix i) => a i e -> Int -> (e -> e) -> m ()
unsafeModify a i f=unsafeRead a i>>=unsafeWrite a i.f
{-# INLINE unsafeModify #-}

main :: IO ()
main = do
  _n <- getLine
  xs <- map readInt.B.words <$> B.getContents
  putStr.unlines.map show. solve $ perse xs

perse :: [Int] -> [[(Int,Int,Int,Int)]]
perse (x0:y0:a0:b0:x1:y1:a1:b1:x2:y2:a2:b2:x3:y3:a3:b3:rest) = [(x0,y0,a0,b0),(x1,y1,a1,b1),(x2,y2,a2,b2),(x3,y3,a3,b3)] : perse rest
perse _ = []

rot90 :: (Int,Int,Int,Int) -> (Int,Int,Int,Int)
rot90 (x,y,a,b) = (x',y',a,b)
   where
     !x' = (-y) + a + b
     !y' = x - a + b
{-# INLINE rot90 #-}

rot180 = rot90.rot90
rot270 = rot90.rot90.rot90
{-# INLINE rot180 #-}
{-# INLINE rot270 #-}

dot :: (Int,Int) -> (Int,Int) -> Int
dot (x,y) (a,b) = a*x+b*y
{-# INLINE dot #-}

isRect :: [(Int,Int,Int,Int)] -> Bool
isRect moles = length(nub [(x0,y0),(x1,y1),(x2,y2),(x3,y3)]) == 4
               && (x1-x0,y1-y0)`dot`(x2-x0,y2-y0) == 0
               && (x3,y3) == (x1+x2-x0,y1+y2-y0)
   where
     sorted@[(x0,y0,_,_),(x1,y1,_,_),(x2,y2,_,_),(x3,y3,_,_)] = sort moles

solve :: [[(Int,Int,Int,Int)]] -> [Int]
solve moles = map query moles
   where
     query moles = case res of
                     [] -> (-1)
                     res -> minimum res
       where
         res = map (sum.map fst) $ filter p [zipWith (#) fs moles|fs <-mapM(const[(0,id),(1,rot90),(2,rot180),(3,rot270)]) [1..4]]
     (i,f)#xyab = (i, f xyab)
     p ixys = isRect $ map snd ixys


import Data.List
s[n,m]=take n$cycle$map snd$sort[(abs(i*2-m-1),i)|i<-[1..m]]
main=interact$unlines.map show.s.map read.words

{-# LANGUAGE FlexibleContexts, ScopedTypeVariables, BangPatterns, OverloadedStrings #-}
{-# OPTIONS_GHC -O2 #-}

import Control.Applicative
import Control.Monad hiding ((<$!>))
import Control.Monad.ST.Safe
-- import Control.Monad.State hiding ((<$!>))
-- import Data.Array
import Data.Array.IArray
import Data.Array.IO.Safe
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
import Data.Array.Unboxed
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Builder as BB
import Data.Char
import qualified Data.Foldable as F
import Data.Function
-- import Data.Graph
import Data.Int
import Data.List
-- import Data.List.Split
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.Ratio
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
-- import Data.Tree
import Data.Tuple
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
-- import Data.HashMap.Strict (HashMap)
-- import qualified Data.HashMap.Strict as HashMap
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
-- import Debug.Trace
import System.IO

-- getInts = fmap (map read . words) getLine
getInts = unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine

main = do
  [n, m] <- getInts

  let
    r
      | odd m = [c] ++ concat (zipWith (\a b -> [a, b]) (reverse [1..c-1]) [c+1..m])
      | even m = concat $ zipWith (\a b -> [a, b]) (reverse [1..c]) [c+1..m]
      where
        c = (m+1) `div` 2

  putStr $ unlines $ map show $ take n $ concat $ repeat r

import Data.List (sort)

pt :: (Show a) => [a] -> IO()
pt [] = return ()
pt (x:xs) = do
	putStrLn $ show x
	pt xs

main = do
	str <- getLine
	let [n,m] = [read x::Int | x <- words str]
	let lst = [ (abs $ m+1-2*i,i) | i<-[1..m]]
	let srt = sort lst
	let ret = take n $ cycle $ map snd srt
	pt ret

solve :: Int -> [Int]
solve n
  | odd n  =  cycle $ take n $ solveR (div n 2) (div n 2 + 1)
  | otherwise  =  solveL (div n 2) (div n 2 + 1)
  where
    solveL l r
      | l == 1  =  l : solveR (div n 2) r
      | otherwise  =  l : solveR (l - 1) r
    solveR l r
      | r == n  =   r : solveL l (div n 2 + 1)
      | otherwise  =  r : solveL l (r + 1)

main :: IO ()
main  =  do
    [m, n]  <-  getLine >>= return . map read . words
    mapM_ print $ take m (solve n)

main = interact$unlines.map show.solve.map read.words

solve :: [Int] -> [Int]
solve [n,m]
  | even m    = take n.cycle.take m.map(mid+) $ 0:concatMap(\i->[i,-i])[1..]
  | otherwise = take n.cycle.take m.map(mid+) $ 0:concatMap(\i->[-i,i])[1..]
   where mid = div (m+1) 2

module Main where

import Control.Applicative
import Data.List
import Control.Monad
import Control.Arrow
import Data.Function
import Data.Array

main' a b = f a 0
    where
        f 0 _ = []
        f a c
            | c == b = (g 0) : f (a-1) 1
            | otherwise = (g c) : f (a-1) (c+1)
        r = listArray (0,b-1)$ (sortBy(compare`on`snd)$map(\i->(i,abs$(intToFlt(b+1)/2.0)-intToFlt i))[1..b])
        g c = fst$ r!c

intToFlt = fromInteger.toInteger

main = putStrLn=<<unlines.map show.(\[a,b]->main' a b).map read.words<$>getLine

zipArr (x:xs) (y:ys) = x:y:zipArr xs ys
zipArr x [] = x
zipArr _ y = y

main = do
  d <- getContents
  let (n:m:_) = map read $ words d
  let k = m `div` 2
  let ord = if m `mod` 2 == 0
      then zipArr [k, k-1 .. 1] [k+1 .. m]
	  else zipArr [k+1 .. m] [k, k-1 .. 1]
  let ans = take n $ concat $ repeat ord
  putStr $ unlines $ map show ans

import Data.List
import Data.Ord

main :: IO ()
main = do
  [n, m] <- fmap (map read . words) getLine
  let f i = (abs $ 1 + m - 2 * i, i)
  putStr $ unlines $ map show $ take n $ cycle $ sortBy (comparing f) $ [1 .. m]

import Data.List
import Data.Ord

main :: IO ()
main = do
  [n, m] <- fmap (map read . words) getLine
  let c = (m + 1) `div` 2
      f i = (abs $ i - c , i)
  putStr $ unlines $ map show $ take n $ cycle $ sortBy (comparing f) $ [1 .. m]

{-# LANGUAGE FlexibleContexts, ScopedTypeVariables, BangPatterns, OverloadedStrings #-}
{-# OPTIONS_GHC -O2 #-}

import Control.Applicative
import Control.Monad hiding ((<$!>))
import Control.Monad.ST.Safe
-- import Control.Monad.State hiding ((<$!>))
-- import Data.Array
import Data.Array.IArray
import Data.Array.IO.Safe
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
import Data.Array.Unboxed
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Builder as BB
import Data.Char
import qualified Data.Foldable as F
import Data.Function
-- import Data.Graph
import Data.Int
import Data.List
-- import Data.List.Split
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.Ratio
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
-- import Data.Tree
import Data.Tuple
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
-- import Data.HashMap.Strict (HashMap)
-- import qualified Data.HashMap.Strict as HashMap
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
-- import Debug.Trace
import System.IO

-- getInts = fmap (map read . words) getLine
getInts = unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine

main = do
  [n, m] <- getInts

  let
    c = (m+1) `div` 2
    r = [c] ++ reverse [1..c-1] ++ [c+1..m]

  putStr $ unlines $ map show $ take n $ concat $ repeat r

{-# LANGUAGE FlexibleContexts, ScopedTypeVariables, BangPatterns, OverloadedStrings #-}
{-# OPTIONS_GHC -O2 #-}

import Control.Applicative
import Control.Monad hiding ((<$!>))
import Control.Monad.ST.Safe
-- import Control.Monad.State hiding ((<$!>))
-- import Data.Array
import Data.Array.IArray
import Data.Array.IO.Safe
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
import Data.Array.Unboxed
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Builder as BB
import Data.Char
import qualified Data.Foldable as F
import Data.Function
-- import Data.Graph
import Data.Int
import Data.List
-- import Data.List.Split
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.Ratio
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
-- import Data.Tree
import Data.Tuple
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
-- import Data.HashMap.Strict (HashMap)
-- import qualified Data.HashMap.Strict as HashMap
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
-- import Debug.Trace
import System.IO

-- getInts = fmap (map read . words) getLine
getInts = unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine

main = do
  [n, m] <- getInts

  let
    c = (m+1) `div` 2
    r = [c] ++ merge (reverse [1..c-1]) [c+1..m]
      where
        merge as [] = as
        merge [] bs = bs
        merge (a:as) (b:bs) = a:b:(merge as bs)

  putStr $ unlines $ map show $ take n $ concat $ repeat r

solve :: Int -> [Int]
solve n
  | odd n  =  cycle $ take n $ solveR (div n 2) (div n 2 + 1)
  | otherwise  =  solveL (div n 2) (div n 2 + 1)
  where
    solveL l r
      | l == 1  =  l : solveR (div n 2) r
      | otherwise  =  l : solveR (l - 1) r
    solveR l r
      | r == n  =   r : solveL l (div n 2 + 1)
      | otherwise  =  r : solveL l (r + 1)

main :: IO ()
main  =  do
    [n, m]  <-  getLine >>= return . map read . words
    mapM_ print $ take m (solve n)


checkX :: String -> Int -> [Bool]
checkX [] t = replicate t True
checkX s t = map (\(x, y) -> (x && y)) (zip (map (=='X') cur) (checkX rest t))
    where
        (cur, rest) = splitAt t s

handle :: String -> [String]
handle str = [show $ length res] ++ res
    where
        handle_i :: Int -> [String]
        handle_i 13 = []
        handle_i i = if 12 `mod` a == 0 && (or $ checkX str b) then ((show a) ++ "x" ++ (show b)) : rest else rest
            where
                a = i
                b = 12 `div` i
                rest = handle_i (i + 1)

        res = handle_i 1

handleAllCase :: Int -> IO ()
handleAllCase 0 = return ()
handleAllCase n = do
    str <- getLine
    putStrLn $ unwords $ handle str
    handleAllCase (n - 1)

main :: IO ()
main = do
    sNCases <- getLine
    handleAllCase $ read sNCases

{-# LANGUAGE FlexibleContexts #-}
import Control.Applicative
import Control.Monad
import Control.Monad.ST.Safe
import Control.Monad.State
import Data.Array
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
import Data.Char
import Data.List
import Data.Maybe
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Tuple
import Data.Map (Map)
import qualified Data.Map as Map
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import qualified Data.ByteString.Char8 as B
-- import Debug.Trace

-- getInts = fmap (map read . words) getLine
-- getInts = unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine

main = do
  k <- readLn
  ss <- replicateM k getLine

  let
    solve s = filter check [(a, b) | a <- [1..12], b <- [1..12], a*b == 12]
      where
        check (a, b) = any (all (== 'X')) $ transpose ls
          where
            ls = unfoldr (\s -> if null s then Nothing else Just $ splitAt b s) s

    p as = unwords $ (show $ length as):(map (\(a, b) -> show a ++ "x" ++ show b) as)

  putStrLn $ unlines $ map p $ map solve ss

import Control.Monad (replicateM_)
import Data.List (intercalate)

win :: String -> Int -> Bool
win s a = any allxs [0..b-1]
  where b = length s `div` a
        allxs c = all (\r -> (s !! (r*b+c)) == 'X') [0..a-1]

solve s = map (\a -> let b = length s `div` a in show a ++ "x" ++ show b) ws
  where ws = concatMap (\a -> if win s a then [a] else []) [1,2,3,4,6,12]

main = do
  t <- read `fmap` getLine :: IO Int
  replicateM_ t $ do
    game <- getLine
    let w = solve game
    putStrLn $ show (length w) ++ " " ++ (intercalate " " w)

{-# LANGUAGE MultiParamTypeClasses,FlexibleContexts,FlexibleInstances,TypeSynonymInstances,BangPatterns,RankNTypes,TupleSections #-}
import Control.Monad
import Control.Monad.ST
import Control.Applicative
import Control.Arrow
import Debug.Trace
import Text.Printf

import Data.List
import Data.Int
import Data.Bits
import Data.Maybe
import Data.Array.Unboxed
import Data.Array.ST
import qualified Data.Map as M
import qualified Data.Set as S
import qualified Data.ByteString.Char8 as B

readInt = fromJust . fmap fst . B.readInt
readInts = map readInt . B.words <$> B.getLine
readIntPair = l2p . map readInt . take 2 . B.words <$> B.getLine
readLns :: Read a => IO [a]
readLns = map read . words <$> getLine
cmpFst (a,_) (b,_) = compare a b
cmpSnd (_,a) (_,b) = compare a b
cmpLen a b = length a `compare` length b
swap (a,b) = (b,a)
l2p (a:b:_) = (a,b)
p2l (a,b) = [a,b]
itof :: Int -> Double
itof = fromIntegral
defaultArray :: (IArray a e,Ix i) => e -> (i,i) -> [(i,e)] -> a i e
defaultArray = accumArray $ curry snd
flatten :: [(a,[(b,c)])] -> [((a,b),c)]
flatten = (=<<) $ uncurry $ fmap . first . (,)

main = do
    n <- readLn
    replicateM_ n $ do
        s <- getLine
        let l = [ (a,b) | a <- [1,2,3,4,6,12], let b = 12 `div` a, win a b s]
        putStrLn $ unwords $show (length l):[printf "%dx%d" a b | (a,b) <- l]

win :: Int -> Int -> String -> Bool
win a b = any (==take a (repeat 'X')).transpose . takeBy b

takeBy b [] = []
takeBy b l = let (l1,l2) = splitAt b l in l1:takeBy b l2

import Control.Applicative
import Data.List (sort, groupBy)

solve :: String -> [(Int, Int)]
solve pattern =
  sort [(12 `div` b, b) | b <- bmatches]
  where
    zipped b = zip (cycle [1..b]) pattern
    groupings = groupBy (\x -> \y -> fst x == fst y) . sort
    ismatch = any (all (\x -> snd x == 'X'))
    bmatches = filter (ismatch . groupings . zipped) [b | b <- [1..12], 12 `mod` b == 0]

showSoln :: [(Int, Int)] -> String
showSoln x = unwords $ show (length x):map (\(a,b) -> (show a) ++ "x" ++ (show b)) x

main = do
  (_:patterns) <- lines <$> getContents
  putStrLn $ unlines $ map (showSoln . solve) patterns

main = do
    getLine
    interact $ solve.lines

solve [] = ""
solve (x:xs) = show(length(res x))++" "++unwords(res x) ++"\n"++solve (xs)
res x = [show(i)++"x"++show(12`div`i)|i<-[1,2,3,4,6,12],p (z x (12`div`i)) i]
z x i = [x!!j|r<-[0..i-1],j<-[0..11],j`mod`i==r]
p [] _ = False
p x i
    |take i x==replicate i 'X'  = True
    |otherwise          = p (drop i x) i


split :: String -> [Int]
split = map read.words

helperSecond :: [Int] -> Int -> Int -> Int
helperSecond a pos n
  | pos < (n - 1) && head a >= tail a !! 0  = helperSecond (tail a) (pos + 1) n
  | otherwise = n - pos - 1

helperFirst :: [Int] -> Int -> Int -> Int
helperFirst a pos n
  | pos < (n - 1) && head a <= tail a !! 0 = helperFirst (tail a) (pos + 1) n
  | otherwise = helperSecond a pos n

findShort :: [Int] -> Int -> Int
findShort a n = helperFirst (reverse a) 0 n

solve :: Int -> IO()
solve t
  | t > 0 = do
    n <- getLine
    a <- getLine
    print $ findShort (split a) (split n !! 0)
    solve (t - 1)
  | t == 0 = putStr ""

main :: IO()
main = do
  t <- getLine
  solve $ split t !! 0

import Data.List
import Control.Monad

solve xs = length prefix
  where
    rest = dropWhile2 (<=) (reverse xs) True
    prefix = dropWhile2 (>=) rest True

dropWhile2 f [x1, x2] b
  | f x1 x2 = []
  | b = [x2]
  | otherwise = [x1, x2]
dropWhile2 f xxs @ (x1:x2:xs) b
  | f x1 x2 = dropWhile2 f (x2 : xs) True
  | b = x2 : xs
  | otherwise = xxs
  where
dropWhile2 _ _ _ = []

getTest :: IO [Int]
getTest = do
  _ <- getLine
  r <- getLine
  let
    r' = map read $ words r
  return r'

main :: IO ()
main = do
  n <- getLine
  let n' = (read n :: Int)
  tests <- replicateM n' getTest
  mapM_ (print . solve) tests

module Main where
import Data.List
import Control.Monad
import System.IO

main = do
  n <- getLine
  let nn = read n :: Int
  replicateM_ nn input

input :: IO ()
input = do
  hSetBuffering stdin LineBuffering
  n <- getLine
  s <- getLine
  putStrLn $ solve (read n) (s)

solve :: Integer  ->  String -> String
solve n str =
  let
    nn = n
    ln = map read $ words str :: [Integer]
    (res, ind, nsch) = isGood1 (ln)
  in if res then show $ n - ind else show "-1"
    -- go :: Integer -> [Integer] -> Integer
    -- go _ [] = nn
    -- go c xs  = if (not $ isGood xs (reverse xs) 0 (length xs) False) then (nn - c) else go (c-1) (tail xs)
    -- go c xs  = if isGood1 xs then (nn - c) else go (c-1) (tail xs)
   -- in show $ go nn ln

-- isGood :: [Integer] -> [Integer] -> Integer -> Int -> Bool  -> Bool
-- isGood _ _ _ 0 b = b
-- isGood _ [] _ _ b = b
-- isGood [] _ _ _ b = b
-- isGood [x] _ _ _ b = b
-- isGood l@(x:xs) ll@(y:ys) cl  c False = case compare x y of
--                              GT ->
--                                if y >= cl then isGood l ys y (c - 1) False
--                                                    else True
--                              _ -> if x >= cl then isGood xs (ll) x (c - 1) False
--                                                   else True
data Signs = Inc | Dec | Same deriving (Ord, Eq, Show)

isGood1 :: [Integer] -> (Bool, Integer, Integer)
isGood1 [x] = (True, 1, 0)
isGood1 lz = let  --stop = length lz - 1
                  -- To determine whether inc or dec
                  rl = reverse lz
                  (_,_,nsch,ind,_) = foldr (\cur t@(prev, sgn, nsch, ind, val) ->
                                  if nsch >= 2 then t
                                               -- case compare cur val of
                                               --   EQ -> (cur, sgn, nsch, ind+1, val)
                                               --   _ -> (cur, sgn, nsch, ind, -1)
                                  else
                                      case compare cur prev of
                                        EQ -> (cur, sgn, nsch, ind+1, -1)
                                        LT -> case sgn of
                                                Inc ->  (cur, Dec, nsch+1, ind+1, -1)
                                                Dec ->  (cur, Dec, nsch, ind+1, -1)
                                                Same -> (cur, Dec, nsch, ind+1, -1)
                                        GT -> case sgn of
                                                Inc ->  (cur, Inc, nsch, ind+1, -1)
                                                Dec ->  (cur, Dec, 2, ind, cur)
                                                Same -> (cur, Inc, nsch, ind+1, -1) )
                                    (last lz, Same, 0, 0, -1) lz
                  -- (_, _, res, ind) = foldr (\cur (prev, inc, res, ind) -> if not res then
                  --                       case inc of
                  --                         True -> --if ind == stop then (cur, False, True, if cur <= prev then ind else ind+1)
                  --                           {-else-} if cur >= prev
                  --                                 then (cur, True, False, ind+1)
                  --                                 else (cur, False, False, ind+1)
                  --                         False -> --if ind == stop then (cur, False, True, if cur >= prev then ind else ind+1)
                  --                           {-else-} if cur <= prev
                  --                                  then (cur, False, False, ind+1)
                  --                                  else (cur, False, True , ind)
                  --                         else (prev, inc, res, ind))
                  --                                     (if last lz >= last(init(lz)) then (last lz, False, False, 0) else (0, True, False, 0)) lz
              in (True, toInteger ind, toInteger nsch)

import Control.Monad   ( replicateM_ )

dropWhile' :: Ord a => (a -> a -> Bool) -> [a] -> [a]
dropWhile' f xs@(x1 : x2 : xr)
  | f x1 x2   = dropWhile' f (x2 : xr)
  | otherwise = xs
dropWhile' _ xs = xs

makeItGud :: [Integer] -> Int
makeItGud = length . tail . dropWhile' (>=) . dropWhile' (<=) . reverse

main :: IO ()
main = do
  n <- read <$> getLine
  replicateM_ n $ getLine >> getLine >>= solve
  where
    solve = print . makeItGud . map read . words

module Main where
import Data.List
import Control.Monad
import System.IO

main = do
  n <- getLine
  let nn = read n :: Int
  replicateM_ nn input

input :: IO ()
input = do
  hSetBuffering stdin LineBuffering
  n <- getLine
  s <- getLine
  putStrLn $ solve (read n) (s)

solve :: Integer  ->  String -> String
solve n str =
  let
    nn = n
    ln = map read $ words str :: [Integer]
    (res, ind) = isGood1 (ln)
  in if res then show $ n - ind else show "-1"
    -- go :: Integer -> [Integer] -> Integer
    -- go _ [] = nn
    -- go c xs  = if (not $ isGood xs (reverse xs) 0 (length xs) False) then (nn - c) else go (c-1) (tail xs)
    -- go c xs  = if isGood1 xs then (nn - c) else go (c-1) (tail xs)
   -- in show $ go nn ln

-- isGood :: [Integer] -> [Integer] -> Integer -> Int -> Bool  -> Bool
-- isGood _ _ _ 0 b = b
-- isGood _ [] _ _ b = b
-- isGood [] _ _ _ b = b
-- isGood [x] _ _ _ b = b
-- isGood l@(x:xs) ll@(y:ys) cl  c False = case compare x y of
--                              GT ->
--                                if y >= cl then isGood l ys y (c - 1) False
--                                                    else True
--                              _ -> if x >= cl then isGood xs (ll) x (c - 1) False
--                                                   else True

isGood1 :: [Integer] -> (Bool, Integer)
isGood1 [x] = (True, 1)
isGood1 lz = let  stop = length lz - 1
                  (_, _, res, ind) = foldr (\cur (prev, inc, res, ind) -> if not res then
                                        case inc of
                                          True -> if ind == stop then (cur, False, True, ind)
                                                  else if cur >= prev
                                                  then (cur, True, False, ind+1)
                                                  else (cur, False, False, ind+1)
                                          False -> if ind == stop then (cur, False, True, if cur > prev then ind else ind+1)
                                                   else if cur <= prev
                                                   then (cur, False, False, ind+1)
                                                   else (cur, False, True , ind)
                                          else (prev, inc, res, ind))
                                                      (if last lz >= last(init(lz)) then (last lz, False, False, 0) else (0, True, False, 0)) lz
              in (res, toInteger ind)

module Main where
import Data.List
import Control.Monad
import System.IO

main = do
  n <- getLine
  let nn = read n :: Int
  replicateM_ nn input

input :: IO ()
input = do
  hSetBuffering stdin LineBuffering
  n <- getLine
  s <- getLine
  putStrLn $ solve (read n) (s)

solve :: Integer  ->  String -> String
solve n str =
  let
    nn = n
    ln = map read $ words str :: [Integer]
    (res, ind) = isGood1 (ln)
  in if res then show $ n - ind else show "-1"
    -- go :: Integer -> [Integer] -> Integer
    -- go _ [] = nn
    -- go c xs  = if (not $ isGood xs (reverse xs) 0 (length xs) False) then (nn - c) else go (c-1) (tail xs)
    -- go c xs  = if isGood1 xs then (nn - c) else go (c-1) (tail xs)
   -- in show $ go nn ln

-- isGood :: [Integer] -> [Integer] -> Integer -> Int -> Bool  -> Bool
-- isGood _ _ _ 0 b = b
-- isGood _ [] _ _ b = b
-- isGood [] _ _ _ b = b
-- isGood [x] _ _ _ b = b
-- isGood l@(x:xs) ll@(y:ys) cl  c False = case compare x y of
--                              GT ->
--                                if y >= cl then isGood l ys y (c - 1) False
--                                                    else True
--                              _ -> if x >= cl then isGood xs (ll) x (c - 1) False
--                                                   else True

isGood1 :: [Integer] -> (Bool, Integer)
isGood1 [x] = (True, 0)
isGood1 lz = let  stop = length lz - 1
                  (_, _, res, ind) = foldr (\cur (prev, inc, res, ind) -> if not res then
                                        case inc of
                                          True -> if ind == stop then (cur, False, True, ind+1)
                                                  else if cur >= prev
                                                  then (cur, True, False, ind+1)
                                                  else (cur, False, False, ind+1)
                                          False -> if ind == stop then (cur, False, True, ind+1)
                                                   else if cur <= prev
                                                   then (cur, False, False, ind+1)
                                                   else (cur, False, True , ind)
                                          else (prev, inc, res, ind))
                                                      (0, True, False, 0) lz
              in (res, toInteger ind)

module Main where
import Data.List
import Control.Monad
import System.IO

main = do
  n <- getLine
  let nn = read n :: Int
  replicateM_ nn input

input :: IO ()
input = do
  hSetBuffering stdin LineBuffering
  n <- getLine
  s <- getLine
  putStrLn $ solve (read n) (s)

solve :: Integer  ->  String -> String
solve n str =
  let
    nn = n
    ln = map read $ words str :: [Integer]
    (res, ind, nsch) = isGood1 (ln)
  in if res then show $ n - ind else show "-1"
    -- go :: Integer -> [Integer] -> Integer
    -- go _ [] = nn
    -- go c xs  = if (not $ isGood xs (reverse xs) 0 (length xs) False) then (nn - c) else go (c-1) (tail xs)
    -- go c xs  = if isGood1 xs then (nn - c) else go (c-1) (tail xs)
   -- in show $ go nn ln

-- isGood :: [Integer] -> [Integer] -> Integer -> Int -> Bool  -> Bool
-- isGood _ _ _ 0 b = b
-- isGood _ [] _ _ b = b
-- isGood [] _ _ _ b = b
-- isGood [x] _ _ _ b = b
-- isGood l@(x:xs) ll@(y:ys) cl  c False = case compare x y of
--                              GT ->
--                                if y >= cl then isGood l ys y (c - 1) False
--                                                    else True
--                              _ -> if x >= cl then isGood xs (ll) x (c - 1) False
--                                                   else True
data Signs = Inc | Dec | Same deriving (Ord, Eq, Show)

isGood1 :: [Integer] -> (Bool, Integer, Integer)
isGood1 [x] = (True, 1, 0)
isGood1 lz = let  --stop = length lz - 1
                  -- To determine whether inc or dec
                  rl = reverse lz
                  (_,_,nsch,ind) = foldr (\cur t@(prev, sgn, nsch, ind) ->
                                  if nsch > 1 then
                                               case compare cur prev of
                                                 EQ -> (prev, sgn, nsch, ind+1)
                                                 _ -> t
                                  else
                                      case compare cur prev of
                                        EQ -> (cur, sgn, nsch, ind+1)
                                        LT -> case sgn of
                                                Inc ->  (cur, Dec, nsch+1, ind+1)
                                                Dec ->  (cur, Dec, nsch, ind+1)
                                                Same -> (cur, Dec, nsch, ind+1)
                                        GT -> case sgn of
                                                Inc ->  (cur, Inc, nsch, ind+1)
                                                Dec ->  (cur, Dec, nsch+1, ind)
                                                Same -> (cur, Inc, nsch, ind+1) )
                                    (last lz, Same, 0, 0) lz
                  -- (_, _, res, ind) = foldr (\cur (prev, inc, res, ind) -> if not res then
                  --                       case inc of
                  --                         True -> --if ind == stop then (cur, False, True, if cur <= prev then ind else ind+1)
                  --                           {-else-} if cur >= prev
                  --                                 then (cur, True, False, ind+1)
                  --                                 else (cur, False, False, ind+1)
                  --                         False -> --if ind == stop then (cur, False, True, if cur >= prev then ind else ind+1)
                  --                           {-else-} if cur <= prev
                  --                                  then (cur, False, False, ind+1)
                  --                                  else (cur, False, True , ind)
                  --                         else (prev, inc, res, ind))
                  --                                     (if last lz >= last(init(lz)) then (last lz, False, False, 0) else (0, True, False, 0)) lz
              in (True, toInteger ind, toInteger nsch)

module Main where
import Data.List
import Control.Monad
import System.IO

main = do
  n <- getLine
  let nn = read n :: Int
  replicateM_ nn input

input :: IO ()
input = do
  hSetBuffering stdin LineBuffering
  n <- getLine
  s <- getLine
  putStrLn $ solve (read n) (s)

solve :: Integer  ->  String -> String
solve n str =
  let
    nn = n
    ln = map read $ words str :: [Integer]
    (res, ind, nsch) = isGood1 (ln)
  in if res then show $ n - ind else show "-1"
    -- go :: Integer -> [Integer] -> Integer
    -- go _ [] = nn
    -- go c xs  = if (not $ isGood xs (reverse xs) 0 (length xs) False) then (nn - c) else go (c-1) (tail xs)
    -- go c xs  = if isGood1 xs then (nn - c) else go (c-1) (tail xs)
   -- in show $ go nn ln

-- isGood :: [Integer] -> [Integer] -> Integer -> Int -> Bool  -> Bool
-- isGood _ _ _ 0 b = b
-- isGood _ [] _ _ b = b
-- isGood [] _ _ _ b = b
-- isGood [x] _ _ _ b = b
-- isGood l@(x:xs) ll@(y:ys) cl  c False = case compare x y of
--                              GT ->
--                                if y >= cl then isGood l ys y (c - 1) False
--                                                    else True
--                              _ -> if x >= cl then isGood xs (ll) x (c - 1) False
--                                                   else True
data Signs = Inc | Dec | Same deriving (Ord, Eq, Show)

isGood1 :: [Integer] -> (Bool, Integer, Integer)
isGood1 [x] = (True, 1, 0)
isGood1 lz = let  --stop = length lz - 1
                  -- To determine whether inc or dec
                  rl = reverse lz
                  (_,_,nsch,ind) = foldr (\cur t@(prev, sgn, nsch, ind) ->
                                  if nsch > 1 then
                                               case compare cur prev of
                                                 EQ -> (prev, sgn, nsch, ind+1)
                                                 _ -> t
                                  else
                                      case compare cur prev of
                                        EQ -> (cur, sgn, nsch, ind+1)
                                        LT -> case sgn of
                                                Inc ->  (cur, Dec, nsch+1, ind+1)
                                                Dec ->  (cur, Dec, nsch, ind+1)
                                                Same -> (cur, Dec, nsch, ind+1)
                                        GT -> case sgn of
                                                Inc ->  (cur, Inc, nsch, ind+1)
                                                Dec ->  (cur, Dec, 2, ind)
                                                Same -> (cur, Inc, nsch, ind+1) )
                                    (last lz, Same, 0, 0) lz
                  -- (_, _, res, ind) = foldr (\cur (prev, inc, res, ind) -> if not res then
                  --                       case inc of
                  --                         True -> --if ind == stop then (cur, False, True, if cur <= prev then ind else ind+1)
                  --                           {-else-} if cur >= prev
                  --                                 then (cur, True, False, ind+1)
                  --                                 else (cur, False, False, ind+1)
                  --                         False -> --if ind == stop then (cur, False, True, if cur >= prev then ind else ind+1)
                  --                           {-else-} if cur <= prev
                  --                                  then (cur, False, False, ind+1)
                  --                                  else (cur, False, True , ind)
                  --                         else (prev, inc, res, ind))
                  --                                     (if last lz >= last(init(lz)) then (last lz, False, False, 0) else (0, True, False, 0)) lz
              in (True, toInteger ind, toInteger nsch)

module Main where
import Data.List
import Control.Monad
import System.IO

main = do
  n <- getLine
  let nn = read n :: Int
  replicateM_ nn input

input :: IO ()
input = do
  hSetBuffering stdin LineBuffering
  n <- getLine
  s <- getLine
  putStrLn $ solve (read n) (s)

solve :: Integer  ->  String -> String
solve n str =
  let
    nn = n
    ln = map read $ words str :: [Integer]
    (res, ind) = isGood1 (ln)
  in if res then show $ n - ind else show "-1"
    -- go :: Integer -> [Integer] -> Integer
    -- go _ [] = nn
    -- go c xs  = if (not $ isGood xs (reverse xs) 0 (length xs) False) then (nn - c) else go (c-1) (tail xs)
    -- go c xs  = if isGood1 xs then (nn - c) else go (c-1) (tail xs)
   -- in show $ go nn ln

-- isGood :: [Integer] -> [Integer] -> Integer -> Int -> Bool  -> Bool
-- isGood _ _ _ 0 b = b
-- isGood _ [] _ _ b = b
-- isGood [] _ _ _ b = b
-- isGood [x] _ _ _ b = b
-- isGood l@(x:xs) ll@(y:ys) cl  c False = case compare x y of
--                              GT ->
--                                if y >= cl then isGood l ys y (c - 1) False
--                                                    else True
--                              _ -> if x >= cl then isGood xs (ll) x (c - 1) False
--                                                   else True

isGood1 :: [Integer] -> (Bool, Integer)
isGood1 [x] = (True, 1)
isGood1 lz = let  stop = length lz - 1
                  (_, _, res, ind) = foldr (\cur (prev, inc, res, ind) -> if not res then
                                        case inc of
                                          True -> if ind == stop then (cur, False, True, ind+1)
                                                  else if cur >= prev
                                                  then (cur, True, False, ind+1)
                                                  else (cur, False, False, ind+1)
                                          False -> if ind == stop then (cur, False, True, ind+1)
                                                   else if cur <= prev
                                                   then (cur, False, False, ind+1)
                                                   else (cur, False, True , ind)
                                          else (prev, inc, res, ind))
                                                      (if last lz >= last(init(lz)) then (last lz, False, False, 0) else (0, True, False, 0)) lz
              in (res, toInteger ind)

module Main where
import Data.List
import Control.Monad
import System.IO

main = do
  n <- getLine
  let nn = read n :: Int
  replicateM_ nn input

input :: IO ()
input = do
  hSetBuffering stdin LineBuffering
  n <- getLine
  s <- getLine
  putStrLn $ solve (read n) (s)

solve :: Integer  ->  String -> String
solve n str =
  let
    nn = n
    ln = map read $ words str :: [Integer]
    (res, ind) = isGood1 (ln)
  in if res then show $ n - ind else show "-1"
    -- go :: Integer -> [Integer] -> Integer
    -- go _ [] = nn
    -- go c xs  = if (not $ isGood xs (reverse xs) 0 (length xs) False) then (nn - c) else go (c-1) (tail xs)
    -- go c xs  = if isGood1 xs then (nn - c) else go (c-1) (tail xs)
   -- in show $ go nn ln

-- isGood :: [Integer] -> [Integer] -> Integer -> Int -> Bool  -> Bool
-- isGood _ _ _ 0 b = b
-- isGood _ [] _ _ b = b
-- isGood [] _ _ _ b = b
-- isGood [x] _ _ _ b = b
-- isGood l@(x:xs) ll@(y:ys) cl  c False = case compare x y of
--                              GT ->
--                                if y >= cl then isGood l ys y (c - 1) False
--                                                    else True
--                              _ -> if x >= cl then isGood xs (ll) x (c - 1) False
--                                                   else True

isGood1 :: [Integer] -> (Bool, Integer)
isGood1 [x] = (True, 1)
isGood1 lz = let  stop = length lz - 1
                  (_, _, res, ind) = foldr (\cur (prev, inc, res, ind) -> if not res then
                                        case inc of
                                          True -> if ind == stop then (cur, False, True, ind+1)
                                                  else if cur >= prev
                                                  then (cur, True, False, ind+1)
                                                  else (cur, False, False, ind+1)
                                          False -> if ind == stop then (cur, False, True, ind+1)
                                                   else if cur <= prev
                                                   then (cur, False, False, ind+1)
                                                   else (cur, False, True , ind)
                                          else (prev, inc, res, ind))
                                                      (0, True, False, 0) lz
              in (res, toInteger ind)

module Main where
import Data.List
import Control.Monad
import System.IO

main = do
  n <- getLine
  let nn = read n :: Int
  replicateM_ nn input

input :: IO ()
input = do
  hSetBuffering stdin LineBuffering
  n <- getLine
  s <- getLine
  putStrLn $ solve (read n) (s)

solve :: Integer  ->  String -> String
solve n str =
  let
    nn = n
    ln = map read $ words str :: [Integer]
    (res, ind) = isGood1 (ln)
  in if res then show $ n - ind else show "-1"
    -- go :: Integer -> [Integer] -> Integer
    -- go _ [] = nn
    -- go c xs  = if (not $ isGood xs (reverse xs) 0 (length xs) False) then (nn - c) else go (c-1) (tail xs)
    -- go c xs  = if isGood1 xs then (nn - c) else go (c-1) (tail xs)
   -- in show $ go nn ln

-- isGood :: [Integer] -> [Integer] -> Integer -> Int -> Bool  -> Bool
-- isGood _ _ _ 0 b = b
-- isGood _ [] _ _ b = b
-- isGood [] _ _ _ b = b
-- isGood [x] _ _ _ b = b
-- isGood l@(x:xs) ll@(y:ys) cl  c False = case compare x y of
--                              GT ->
--                                if y >= cl then isGood l ys y (c - 1) False
--                                                    else True
--                              _ -> if x >= cl then isGood xs (ll) x (c - 1) False
--                                                   else True

isGood1 :: [Integer] -> (Bool, Integer)
isGood1 [x] = (True, 1)
isGood1 lz = let  stop = length lz - 1
                  (_, _, res, ind) = foldr (\cur (prev, inc, res, ind) -> if not res then
                                        case inc of
                                          True -> if ind == stop then (cur, False, True, ind)
                                                  else if cur >= prev
                                                  then (cur, True, False, ind+1)
                                                  else (cur, False, False, ind+1)
                                          False -> if ind == stop then (cur, False, True, ind)
                                                   else if cur <= prev
                                                   then (cur, False, False, ind+1)
                                                   else (cur, False, True , ind)
                                          else (prev, inc, res, ind))
                                                      (if last lz >= last(init(lz)) then (last lz, False, False, 0) else (0, True, False, 0)) lz
              in (res, toInteger ind)

module Main where
import Data.List
import Control.Monad
import System.IO

main = do
  n <- getLine
  let nn = read n :: Int
  replicateM_ nn input

input :: IO ()
input = do
  hSetBuffering stdin LineBuffering
  n <- getLine
  s <- getLine
  putStrLn $ solve (read n) (s)

solve :: Integer  ->  String -> String
solve n str =
  let
    nn = n
    ln = map read $ words str :: [Integer]
    (res, ind) = isGood1 (ln)
  in if res then show $ n - ind else show "-1"
    -- go :: Integer -> [Integer] -> Integer
    -- go _ [] = nn
    -- go c xs  = if (not $ isGood xs (reverse xs) 0 (length xs) False) then (nn - c) else go (c-1) (tail xs)
    -- go c xs  = if isGood1 xs then (nn - c) else go (c-1) (tail xs)
   -- in show $ go nn ln

-- isGood :: [Integer] -> [Integer] -> Integer -> Int -> Bool  -> Bool
-- isGood _ _ _ 0 b = b
-- isGood _ [] _ _ b = b
-- isGood [] _ _ _ b = b
-- isGood [x] _ _ _ b = b
-- isGood l@(x:xs) ll@(y:ys) cl  c False = case compare x y of
--                              GT ->
--                                if y >= cl then isGood l ys y (c - 1) False
--                                                    else True
--                              _ -> if x >= cl then isGood xs (ll) x (c - 1) False
--                                                   else True

isGood1 :: [Integer] -> (Bool, Integer)
isGood1 [x] = (True, 1)
isGood1 lz = let  stop = length lz - 1
                  (_, _, res, ind) = foldr (\cur (prev, inc, res, ind) -> if not res then
                                        case inc of
                                          True -> --if ind == stop then (cur, False, True, if cur <= prev then ind else ind+1)
                                            {-else-} if cur >= prev
                                                  then (cur, True, False, ind+1)
                                                  else (cur, False, False, ind+1)
                                          False -> --if ind == stop then (cur, False, True, if cur >= prev then ind else ind+1)
                                            {-else-} if cur <= prev
                                                   then (cur, False, False, ind+1)
                                                   else (cur, False, True , ind)
                                          else (prev, inc, res, ind))
                                                      (if last lz >= last(init(lz)) then (last lz, False, False, 0) else (0, True, False, 0)) lz
              in (True, toInteger ind)


module Main where

import Control.Monad
import Data.List

getStrList :: IO [String]
getStrList = words <$> getLine

getIntList :: IO [Int]
getIntList = map read <$> getStrList

printStrList :: [String] -> IO ()
printStrList = putStrLn . intercalate " "

printList :: (Show a) => [a] -> IO ()
printList = printStrList . map show

main :: IO ()
main = do
    t <- readLn
    replicateM_ t $ do
        n <- readLn
        a <- sort <$> getIntList
        let
            half = n `div` 2
            (lHalf, rHalf) = splitAt half a
            revLHalf = reverse lHalf
        printList $ combine revLHalf rHalf 1

combine :: [Int] -> [Int] -> Int -> [Int]
combine [] [] _ = []
combine [] ys _ = ys
combine xs [] _ = xs
combine (x:xs) (y:ys) z = case z of
    0 -> x : combine xs (y:ys) (1 - z)
    1 -> y : combine (x:xs) ys (1 - z)

import Control.Monad
import Data.List

main :: IO ()
main = do
  t <- readLn :: IO Int
  replicateM_
    t
    (getLine >> getLine >>=
     putStrLn . unwords . map show . solve . map read . words)

f :: [a] -> [a] -> [a]
f [] _ = []
f (a:as) b = a : f b as

solve :: [Int] -> [Int]
solve l = f b (reverse a)
  where
    (a, b) = splitAt (length l `div` 2) (sort l)

module Main where

import Control.Monad
import Data.List

getStrList :: IO [String]
getStrList = words <$> getLine

getIntList :: IO [Int]
getIntList = map read <$> getStrList

printStrList :: [String] -> IO ()
printStrList = putStrLn . intercalate " "

printList :: (Show a) => [a] -> IO ()
printList = printStrList . map show

main :: IO ()
main = do
    t <- readLn
    replicateM_ t $ do
        n <- readLn
        a <- sort <$> getIntList
        let
            half = n `div` 2
            (lHalf, rHalf) = splitAt half a
            revLHalf = reverse rHalf
        printList $ combine revLHalf rHalf 1

combine :: [Int] -> [Int] -> Int -> [Int]
combine [] [] _ = []
combine [] ys _ = ys
combine xs [] _ = xs
combine (x:xs) (y:ys) z = case z of
    0 -> x : combine xs (y:ys) (1 - z)
    1 -> y : combine (x:xs) ys (1 - z)

module Main where

import Control.Monad
import Data.List

getStrList :: IO [String]
getStrList = words <$> getLine

getIntList :: IO [Int]
getIntList = map read <$> getStrList

printStrList :: [String] -> IO ()
printStrList = putStrLn . intercalate " "

printList :: (Show a) => [a] -> IO ()
printList = printStrList . map show

main :: IO ()
main = do
    t <- readLn
    replicateM_ t $ do
        n <- readLn
        a <- sort <$> getIntList
        let
            half = n `div` 2
            (lHalf, rHalf) = splitAt half a
            revLHalf = reverse lHalf
        printList $ combine revLHalf rHalf 0

combine :: [Int] -> [Int] -> Int -> [Int]
combine [] [] _ = []
combine [] ys _ = ys
combine xs [] _ = xs
combine (x:xs) (y:ys) z = case z of
    0 -> x : combine xs (y:ys) (1 - z)
    1 -> y : combine (x:xs) ys (1 - z)

module Main where

import Control.Monad
import Data.List

getStrList :: IO [String]
getStrList = words <$> getLine

getIntList :: IO [Int]
getIntList = map read <$> getStrList

printStrList :: [String] -> IO ()
printStrList = putStrLn . intercalate " "

printList :: (Show a) => [a] -> IO ()
printList = printStrList . map show

main :: IO ()
main = do
    t <- readLn
    replicateM_ t $ do
        n <- readLn
        a <- sort <$> getIntList
        let
            half = n `div` 2
            (lHalf, rHalf) = splitAt half a
            revLHalf = reverse lHalf
        print half
        printList revLHalf
        printList rHalf
        printList $ combine revLHalf rHalf 0

combine :: [Int] -> [Int] -> Int -> [Int]
combine [] [] _ = []
combine [] ys _ = ys
combine xs [] _ = xs
combine (x:xs) (y:ys) z = case z of
    0 -> x : combine xs (y:ys) (1 - z)
    1 -> y : combine (x:xs) ys (1 - z)


calculate :: Integer -> Integer -> Integer -> Integer
calculate n x t
    | (n >= div t x) = (div t x) * n - (div t x * (div t x + 1)) `div` 2
    | otherwise = ((n-1) * n) `div` 2

main = do
    int <- getLine
    let n = read int::Integer
    repeat n
    where
        repeat 0 = return ()
        repeat m = do
            line <- getLine
            let input = map (\x -> read x::Integer) $ words line
            putStrLn $ show $ calculate (input!!0) (input!!1) (input!!2)
            repeat $ m-1

{-# OPTIONS_GHC -O2 #-}

import Control.Monad
import Prelude as P

import Data.Word (Word8)
import Data.Int (Int64)
import qualified Data.Bits as Bits
import qualified Data.Char as Char

import Data.List as L
import qualified Data.Set as Set
import qualified Data.Map as Map

import qualified Data.ByteString as B
import qualified System.IO as Io
--import Data.IntSet

solve :: IO ()
solve = do
  s <- getLine
  let [n, x, t] = map read $ words s :: [Integer]
      mx = min (div t x) (n-1)
      res = div (mx*(mx+1)) 2 + mx * (n - mx - 1)
    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,x,t] <- readInts
    let m = div t x
    if m == 0 then print 0
    else print $ if n >= m + 1 then div ((m+1)*m) 2 + m*(n-m-1) else div (n*(n-1)) 2

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

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

printS = do
    [n,x,t] <- readInts
    let m = div (n*x-t) x + if (div (n*x-t) x > 0) then 1 else 0
        mm = if m < 0 then 0 else m
        s = if mm>1 then (mm-1)*(div t x) else 0
        a = if mm == 0 then 1 else mm
        ret = s + (div ((n-a)*(n-a+1)) 2)
    print ret

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

{-# OPTIONS_GHC -O2 #-}

import Control.Monad
import Prelude as P

import Data.Word (Word8)
import Data.Int (Int64)
import qualified Data.Bits as Bits
import qualified Data.Char as Char

import Data.List as L
import qualified Data.Set as Set
import qualified Data.Map as Map

import qualified Data.ByteString as B
import qualified System.IO as Io
--import Data.IntSet

solve :: IO ()
solve = do
  s <- getLine
  let [n, x, t] = map read $ words s :: [Integer]
      mx = div t x
      res = div (mx*(mx+1)) 2 + mx * (n - mx - 1)
    in print res

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

{-# OPTIONS_GHC -O2 #-}

import Control.Monad
import Prelude as P

import Data.Word (Word8)
import Data.Int (Int64)
import qualified Data.Bits as Bits
import qualified Data.Char as Char

import Data.List as L
import qualified Data.Set as Set
import qualified Data.Map as Map

import qualified Data.ByteString as B
import qualified System.IO as Io
--import Data.IntSet

solve :: IO ()
solve = do
  s <- getLine
  let [n, x, t] = map read $ words s :: [Int64]
      mx = div t x
      res = div (mx*(mx+1)) 2 + mx * (n - mx - 1)
    in print res

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

calculate :: Int -> Int -> Int -> Int
calculate n x t
    | (n >= div t x) = (div t x) * n - (div t x * (div t x + 1)) `div` 2
    | otherwise = ((n-1) * n) `div` 2

main = do
    int <- getLine
    let n = read int::Int
    repeat n
    where
        repeat 0 = return ()
        repeat m = do
            line <- getLine
            let input = map (\x -> read x::Int) $ words line
            putStrLn $ show $ calculate (input!!0) (input!!1) (input!!2)
            repeat $ m-1

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

printResult = do
    [n,x,t] <- readInts
    let m = div t x
    if m == 0 then print 0
    else print $ if n >= m + 1 then div ((m+1)*m) 2 + m*(n-m-1) else div (n*(n-1)) 2

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


import Data.List
import Data.Int
import Data.Array
import System.IO
-- import qualified Data.Set as Set

factor k =
    let arr = [ x | x <- takeWhile ((<=k).(^2)) [2..], k `mod` x == 0]
    in if null arr then k else head arr

main = do
    hSetBuffering stdin (BlockBuffering (Just 4096))
    hSetBuffering stdout (BlockBuffering (Just 4096))
    contents <- drop 1 . lines <$> getContents

    let mapped = map (map (read :: (String -> Int64)) . words) contents
        res = foldl (\y [a, b] -> gcd y (a * b)) 0 mapped
        [a, b] = factor . gcd res <$> head mapped
        ans = max a b
    print $ if ans == 1 then -1 else ans

import Data.List
import Data.Int
import Data.Array
import System.IO
-- import qualified Data.Set as Set

factor k =
    let arr = [ x | x <- takeWhile ((<=k).(^2)) [2..], k `mod` x == 0]
    in if null arr then k else head arr

main = do
    hSetBuffering stdin (BlockBuffering (Just 4096))
    hSetBuffering stdout (BlockBuffering (Just 4096))
    contents <- drop 1 . lines <$> getContents

    let mapped = map (map (read :: (String -> Int64)) . words) contents
        res = foldl' (\y [a, b] -> gcd y (a * b)) 0 mapped
        [a, b] = factor . gcd res <$> head mapped
        ans = max a b
    print $ if ans == 1 then -1 else ans

import Data.List
import Data.Int
import Data.Array
import System.IO
-- import qualified Data.Set as Set

factor k =
    let arr = [ x | x <- takeWhile ((<=k).(^2)) [2..], k `mod` x == 0]
    in if null arr then k else head arr

main = do
    hSetBuffering stdin (BlockBuffering (Just 4096))
    hSetBuffering stdout (BlockBuffering (Just 4096))
    contents <- drop 1 . lines <$> getContents

    let mapped = map (map (read :: (String -> Int64)) . words) contents
        res = foldr (\[a, b] y -> gcd y (a * b)) 0 mapped
        [a, b] = factor . gcd res <$> head mapped
        ans = max a b
    print $ if ans == 1 then -1 else ans

import Data.List
import Data.Array
import System.IO
-- import qualified Data.Set as Set

factor k =
    let arr = [ x | x <- takeWhile ((<=k).(^2)) [2..], k `mod` x == 0]
    in if null arr then k else head arr

main = do
    hSetBuffering stdin (BlockBuffering (Just 4096))
    hSetBuffering stdout (BlockBuffering (Just 4096))
    contents <- drop 1 . lines <$> getContents
    let mapped =  map (map (read :: (String -> Int)) . words) contents
        res = foldl' (\y [a, b] -> gcd y (a * b)) 0 mapped
        [a, b] = factor . gcd (factor res) <$> head mapped
        ans = max a b
    print (if ans == 1 then -1 else ans)

import Data.List
import Data.Array
import System.IO
-- import qualified Data.Set as Set

factor k =
    let arr = [ x | x <- takeWhile ((<=k).(^2)) [2..], k `mod` x == 0]
    in if null arr then k else head arr

main = do
    hSetBuffering stdin (BlockBuffering (Just 4096))
    hSetBuffering stdout (BlockBuffering (Just 4096))
    contents <- drop 1 . lines <$> getContents
    let mapped =  map (map (read :: (String -> Int)) . words) contents
        res = foldr (\[a, b] y -> gcd y (a * b)) 0 mapped
        g = factor res
        ([a, b]) = factor . gcd g <$> head mapped
        ans = max a b
    print (if ans == 1 then -1 else ans)

import Data.List
import Data.Array
import System.IO
-- import qualified Data.Set as Set

factor k =
    let arr = [ x | x <- takeWhile ((<=k).(^2)) [2..], k `mod` x == 0]
    in if null arr then k else head arr

main = do
    hSetBuffering stdin (BlockBuffering (Just 4096))
    hSetBuffering stdout (BlockBuffering (Just 4096))
    contents <- drop 1 . lines <$> getContents
    let mapped =  map (map (read :: (String -> Int)) . words) contents
        res = foldr (\[a, b] y -> gcd y (a * b)) 0 mapped
        [a, b] = factor . gcd (factor res) <$> head mapped
        ans = max a b
    print (if ans == 1 then -1 else ans)

import Data.List
import Data.Int
import Data.Array
import System.IO
-- import qualified Data.Set as Set

factor k =
    let arr = [ x | x <- takeWhile ((<=k).(^2)) [2..], k `mod` x == 0]
    in if null arr then k else head arr

main = do
    hSetBuffering stdin (BlockBuffering (Just 4096))
    -- hSetBuffering stdout (BlockBuffering (Just 4096))
    contents <- drop 1 . lines <$> getContents
    print "Here"
    let mapped = map (map (read :: (String -> Int64)) . words) contents
        res = foldl' (\y [a, b] -> gcd y (a * b)) 0 mapped
        [a, b] = factor . gcd (factor res) <$> head mapped
        ans = max a b
    print (if ans == 1 then -1 else ans)

import Data.List
import Data.Int
import Data.Array
import System.IO
-- import qualified Data.Set as Set

factor k =
    let arr = [ x | x <- takeWhile ((<=k).(^2)) [2..], k `mod` x == 0]
    in if null arr then k else head arr

main = do
    hSetBuffering stdin (BlockBuffering (Just 4096))
    -- hSetBuffering stdout (BlockBuffering (Just 4096))
    contents <- drop 1 . lines <$> getContents
    -- print "Here"
    let mapped = map (map (read :: (String -> Int64)) . words) contents
        res = foldl (\y [a, b] -> gcd y (a * b)) 0 mapped
        [a, b] = factor . gcd res <$> head mapped
        ans = max a b
    print ans

import Data.List
import Data.Array
import System.IO
-- import qualified Data.Set as Set

factor k =
    let arr = [ x | x <- takeWhile ((<=k).(^2)) [2..], k `mod` x == 0]
    in if null arr then k else head arr

main = do
    hSetBuffering stdin (BlockBuffering (Just 4096))
    hSetBuffering stdout (BlockBuffering (Just 4096))
    contents <- drop 1 . lines <$> getContents
    let mapped =  map (map (read :: (String -> Int)) . words) contents
        res = foldl (\y [a, b] -> gcd y (a * b)) 0 mapped
        [a, b] = factor . gcd (factor res) <$> head mapped
        ans = max a b
    print (if ans == 1 then -1 else ans)


import Control.Monad (foldM)

main :: IO ()
main = do
  n <- getLine

  case foldM f ([], []) (zip n [1..]) of
    Just (zs, []) -> do
      print (length zs)
      mapM_ (\xs -> putStrLn (unwords $ map show $ length xs : reverse xs)) zs
    _ -> print (-1)

f :: ([[Int]], [[Int]]) -> (Char, Int) -> Maybe ([[Int]], [[Int]])
f (zs, [])   ('0', i) = Just ([i] : zs, [])
f (zs, o:os) ('0', i) = Just ((i : o) : zs, os)
f (z:zs, os) ('1', i) = Just (zs, (i : z) : os)
f _ _ = Nothing

import Control.Monad
import Data.List

main = do
  inp <- getLine
  let (ans, bns) = genPairs inp
  if not (null bns)
  then print (-1)
  else do
    let l = length ans
    let sans = (map . map) show ans
    print l
    forM_ sans $ \s -> do
         putStr (show (length s) ++ " ")
         putStrLn . unwords . reverse $ s

-- completeted, not
genPairs :: String -> ([[Int]],[[Int]])
genPairs = maybe ([], [[1]]) id . foldl g (Just ([], [])) . zip [1..]
    where
      g :: Maybe ([[Int]], [[Int]]) -> (Int, Char) -> Maybe ([[Int]], [[Int]])
      g m x = m >>= flip f x
      f :: ([[Int]], [[Int]]) -> (Int, Char) -> Maybe ([[Int]], [[Int]])
      f (ha:a, b) (ix, '1') = Just (a, (ix:ha):b)
      f (a, hb:b) (ix, '0') = Just ((ix:hb):a, b)
      f ([], b)   (ix, '1') = Nothing
      f (a, [])   (ix, '0') = Just ([ix]:a, [])

import Control.Monad
import Data.List

main = do
  inp <- getLine
  let (ans, bns) = genPairs inp
  if not (null bns)
  then print (-1)
  else do
    let l = length ans
    let sans = (map . map) show ans
    print l
    forM_ sans $ \s -> do
         putStr (show (length s) ++ " ")
         putStrLn . unwords . reverse $ s

-- completeted, not
genPairs :: String -> ([[Int]],[[Int]])
genPairs = foldl f ([], []) . zip [1..]
    where
      f :: ([[Int]], [[Int]]) -> (Int, Char) -> ([[Int]], [[Int]])
      f (ha:a, b) (ix, '1') = (a, (ix:ha):b)
      f (a, hb:b) (ix, '0') = ((ix:hb):a, b)
      f ([], b)   (ix, '1') = ([], [ix]:b)
      f (a, [])   (ix, '0') = ([ix]:a, [])

import Control.Monad
import Data.List

main = do
  inp <- getLine
  let (ans, bns) = genPairs inp
  print ans
  if not (null bns)
  then print (-1)
  else do
    let l = length ans
    let sans = (map . map) show ans
    print l
    forM_ sans $ \s -> do
         putStrLn . unwords . reverse $ s

-- completeted, not
genPairs :: String -> ([[Int]],[[Int]])
genPairs = foldl f ([], []) . zip [1..]
    where
      f :: ([[Int]], [[Int]]) -> (Int, Char) -> ([[Int]], [[Int]])
      f (ha:a, b) (ix, '1') = (a, (ix:ha):b)
      f (a, hb:b) (ix, '0') = ((ix:hb):a, b)
      f ([], b)   (ix, '1') = ([], [ix]:b)
      f (a, [])   (ix, '0') = ([ix]:a, [])

import Control.Monad
import Data.List

main = do
  inp <- getLine
  let (ans, bns) = genPairs inp
  if not (null bns)
  then print (-1)
  else do
    let l = length ans
    let sans = (map . map) show ans
    print l
    forM_ sans $ \s -> do
         putStrLn . unwords . reverse $ s

-- completeted, not
genPairs :: String -> ([[Int]],[[Int]])
genPairs = foldl f ([], []) . zip [1..]
    where
      f :: ([[Int]], [[Int]]) -> (Int, Char) -> ([[Int]], [[Int]])
      f (ha:a, b) (ix, '1') = (a, (ix:ha):b)
      f (a, hb:b) (ix, '0') = ((ix:hb):a, b)
      f ([], b)   (ix, '1') = ([], [ix]:b)
      f (a, [])   (ix, '0') = ([ix]:a, [])


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

{-# language Safe                      #-}
{-# language ImportQualifiedPost       #-}
{-# language NoMonomorphismRestriction #-}

{-# options_ghc -O3                              #-}

{-# options_ghc -Weverything                     #-}
{-# options_ghc -Werror                          #-}

{-# options_ghc -Wno-missing-import-lists        #-}
{-# options_ghc -Wno-missing-local-signatures    #-}

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

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

showB False = "NO"
showB True  = "YES"

main :: IO ()
main = do
    [t] <- ri
    tcs <- replicateM t $ do
        [[_n], xs] <- replicateM 2 ri
        return xs
    mapM_ (putStrLn.showB.solve) tcs

solve xs = all isConvertible . tail $ xs
  where
    p = head xs
    isConvertible x = (x >= p && x `mod` p == 0)

solve :: IO ()
solve =         readInt <$> getLine >>= \ n     ->
    map readInt . words <$> getLine >>= \(x:xs) ->
        putStrLn $ if all (\i -> i `mod` x == 0) xs then "YES" else "NO"

main :: IO ()
main =
    let f 0 = return ()
        f t = solve >> f (t - 1)
    in readInt <$> getLine >>= f

readInt :: String -> Int
readInt = read

solve :: IO ()
solve =         readInt <$> getLine >>= \ n     ->
    map readInt . words <$> getLine >>= \(x:xs) ->
        putStrLn . (\v -> if v then "YES" else "NO") $
            all (\i -> i `mod` x == 0) xs

main :: IO ()
main =
    let f 0 = return ()
        f t = solve >> f (t - 1)
    in readInt <$> getLine >>= f

readInt :: String -> Int
readInt = read

import Debug.Trace

test (x:xs) = x : map ( `mod` x ) xs

rec a = if (test a)== a then a else test a

is_zero (x:xs) = sum xs == 0

exec 0 = pure ()
exec t = do
	n <- read <$> getLine :: IO Int
	a <- (map read . words) <$> getLine :: IO [Integer]

	putStr $ if is_zero (rec  a) then "YES" else "NO"
	putStr "\n"

	exec (t-1)

main = do
	t <- read <$> getLine :: IO Int
	exec t

import Debug.Trace
diff n e a = [a!!i | i<-[0..e-1] ] ++ [ if a!!e - a!!(e-1) >= 0 then  a!!e - a!!(e-1) else a!!e] ++ [a!!i | i<-[e+1..n-1] ]

-- test _ _ a | trace ("\t" ++ show a ) False = undefined
test _ 0 a = a
test n e a = test  n (e-1) $ diff n e a

rec n a = if (t n a)== a then a else test n (n-1) $ t n a
	where
		t n a = test n (n-1) a

is_zero (x:xs) = sum xs == 0

exec 0 = pure ()
exec t = do
	n <- read <$> getLine :: IO Int
	a <- (map read . words) <$> getLine :: IO [Integer]

	putStr $ if is_zero (rec n  a) then "YES" else "NO"
	putStr "\n"

	exec (t-1)

main = do
	t <- read <$> getLine :: IO Int
	exec t

import Debug.Trace
diff n e a = [a!!i | i<-[0..e-1] ] ++ [ if a!!e - a!!(e-1) >= 0 then  a!!e - a!!(e-1) else a!!e] ++ [a!!i | i<-[e+1..n-1] ]

-- test _ _ a | trace ("\t" ++ show a ) False = undefined
test _ 0 a = a
test n e a = test  n (e-1) $ diff n e a

rec n a = if (t n a)== a then a else test n (n-1) $ t n a
	where
		t n a = test n (n-1) a

is_zero (x:xs) = sum xs == 0

exec 0 = pure ()
exec t = do
	n <- read <$> getLine :: IO Int
	a <- (map read . words) <$> getLine :: IO [Int]

	putStr $ if is_zero (rec n  a) then "YES" else "NO"
	putStr "\n"

	exec (t-1)

main = do
	t <- read <$> getLine :: IO Int
	exec t


-- problem: https://codeforces.com/contest/1644/problem/B
-- origin: https://codeforces.com/contest/1644/submission/147286507

import Control.Monad.State (State, evalState, replicateM, state)
import Data.Bifunctor (Bifunctor (first))
import Data.Char (isDigit, isSpace)
import Data.List (permutations)

isOk :: [Int] -> Bool
isOk li@(a : b : c) = and $ zipWith3 (\x y z -> x + y /= z) li (b : c) c
isOk _ = error "Wrong"

getInt :: State [Char] Int
getInt = state (first (\x -> read x :: Int) . span isDigit . dropWhile isSpace)

putInts :: [Int] -> [Char]
putInts [] = "\n"
putInts (x : xs) = show x ++ concatMap (\y -> ' ' : show y) xs ++ "\n"

mainFunc :: State [Char] [[Char]]
mainFunc = do
  t <- getInt
  replicateM t $ do
    n <- getInt
    let ans = filter isOk $ permutations [1 .. n]
        ans' = take n ans -- accelerate calculating
    pure $ concatMap putInts ans'

main :: IO ()
main = do
  inp <- getContents
  putStr $ concat $ evalState mainFunc inp

chunksOf :: Int -> [a] -> [[a]]
chunksOf _ [] = []
chunksOf i xs = take i xs : chunksOf i (drop i xs)

toArr :: Read a => String -> [a]
toArr = map read . words

showArr :: Show a => [a] -> String
showArr = concatMap (\x -> show x ++ " ")

type InType  = Int
type OutType = [[Int]]

solve :: InType -> OutType
solve n = [go i n | i <- [0 .. n - 1]]
  where go i n = concat [[n - z + 1 | z <- [1 .. i]],
                         [1],
                         [n - z + 1 | z <- [i + 1 .. n - 1]]
                        ]

receive :: [String] -> InType
receive [x] = read x :: Int
receive _   = error "input error"

main :: IO ()
main = interact $ unlines . map (unlines . map showArr . solve . receive) . chunksOf 1 . tail . lines

import Control.Monad
import Data.List

solve :: IO ()
solve = do
    n <- readLn :: IO Int
    mapM_ (putStrLn . unwords . map show)
        [p ++ [1] ++ s | let r = [n,n-1..2], (p, s) <- zip (inits r) (tails r)]

main :: IO ()
main = flip replicateM_ solve =<< readLn

import Control.Monad (forM_)
import Data.List (permutations)

antifib :: [Int] -> Bool
antifib [] = True
antifib [_] = True
antifib [a, b] = True
antifib (a : b : c : cs) = (a + b) /= c && antifib (b : c : cs)

solve :: Int -> [[Int]]
solve n = take n $ filter antifib $ permutations [1 .. n]

main :: IO ()
main = do
    t <- readLn :: IO Int
    forM_ [1 .. t] $ \i -> do
        n <- readLn :: IO Int
        putStr $ unlines $ map (unwords . map show) (solve n)

import qualified Data.ByteString.Lazy.Char8 as P
import Data.ByteString.Builder
import qualified Data.ByteString.Builder.Prim as Prim
import System.IO
import Control.Monad.Trans.State
import Data.Maybe

--import Data.ByteString.Builder.Extra (flush) -- for interactive problems
import Control.Monad
import Data.List
--import Data.Array.Unboxed

mainFun :: SP Builder
mainFun = do
  t <- getInt
  fmap mconcat $ replicateM t $ do
    n <- getInt
    let
      isOK li = and
        $ zipWith3 (\x y z -> x + y /= z) li (tail li) (tail $ tail li)
      ans = take n $ filter isOK $ permutations [1..n]
    pure $ foldMap putInts ans

type SP = State P.ByteString
type S = StateT P.ByteString

dropSpace :: P.ByteString -> P.ByteString
dropSpace = P.dropWhile (<= ' ') -- not exactly right, but close enough

--getNext :: Monad m => S m P.ByteString
--getNext = state $ P.span (> ' ') . dropSpace

getInt :: Monad m => S m Int
getInt = state $ fromJust . P.readInt . dropSpace

putInts :: [Int] -> Builder
putInts vs = let
  sepPrim = (,) ' ' Prim.>$<
    Prim.liftFixedToBounded Prim.char7 Prim.>*< Prim.intDec
  in  case  vs  of
  [] -> char7 '\n'
  x : xs -> intDec x <> Prim.primMapListBounded sepPrim xs <> char7 '\n'

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  inp <- P.getContents
  let  outp = evalState mainFun inp
  P.putStr $ toLazyByteString outp

chunksOf :: Int -> [a] -> [[a]]
chunksOf _ [] = []
chunksOf i xs = take i xs : chunksOf i (drop i xs)

toArr :: Read a => String -> [a]
toArr = map read . words

type InType  = Int
type OutType = [[Int]]

solve :: InType -> OutType
solve n = [n .. 1] : [go i n | i <- [0 .. n - 2]]
  where go i n = concat [[n - z | z <- [0 .. i]],
                         [1],
                         [n - z | z <- [i + 1 .. n - 2]]
                        ]

receive :: [String] -> InType
receive [x] = read x :: Int
receive _   = error "input error"

main :: IO ()
main = interact $ unlines . map (showAnswer . solve . receive) . chunksOf 1 . tail . lines
  where showAnswer :: OutType -> String
        showAnswer [] = ""
        showAnswer (v : vs) = concat [concatMap (\x -> show x ++ " ") v, "\n", showAnswer vs]

import Control.Monad
import Data.List

solve :: IO ()
solve = do
    n <- readLn :: IO Int
    mapM_ (putStrLn . unwords . map show) $ case n of
        1 -> [[1]]
        2 -> [[1, 2], [2, 1]]
        _ -> [[n,n-1..1], [n,n-1..3] ++ [1,2]] ++
             [[1, 3] ++ p ++ [2] ++ s | let r = [4..n], (p, s) <- zip (inits r) (tails r)]

main :: IO ()
main = flip replicateM_ solve =<< readLn


import Data.List
import Data.Int
import qualified Data.Array as A
import Data.Array((!))
import Data.Monoid as M
import Numeric

fastRead :: String -> Int64
fastRead ('-':s) = case readDec s of [(n, "")] -> (-n)
fastRead s = case readDec s of [(n, "")] -> n

main = interact solve

solve input
    | not possible = "No\n"
    | otherwise = "Yes\n" ++ (printTable r c solutionTable) ++ "\n"
    where
        asWords = words input
        rs:cs:tableWords = asWords
        c = read cs :: Int
        r = read rs :: Int
        table = parseTable r c tableWords
        solutionTable = makeSolutionTable table
        possible = M.getAll . mconcat $ map idxCan (A.indices table)
        idxCan :: (Int, Int) -> M.All
        idxCan idx = M.All $ (not isWolf) || (not hasSheepNeighbour)
            where
                curChar = table A.! idx
                isWolf = curChar == 'W'
                nextidx :: [(Int,Int)]
                nextidx = [(1,0),(-1,0),(0,1),(0,-1)]
                (cR,cC) = idx
                hasSheepNeighbour = M.getAny . mconcat $ anyB
                    where
                        okIdx :: [(Int,Int)]
                        okIdx = filter (A.inRange $ A.bounds table) . map (\(r,c) -> (r+cR, c+cC)) $ nextidx
                        anyB :: [M.Any]
                        anyB = map (\cidx -> M.Any $ ((table A.! cidx) == 'S')) okIdx

printTable :: Int -> Int -> A.Array (Int,Int) Char -> String
printTable r c table = intercalate "\n" . map printRow $ [0..(r-1)]
    where
        printRow :: Int -> String
        printRow rowNum = map (\cc -> table A.! (rowNum, cc)) [0..(c-1)]

parseTable :: Int -> Int -> [String] -> A.Array (Int,Int) Char
parseTable r c tableWords = A.array ((0,0), (r-1,c-1)) indexList
    where
        indexList = convertWordsToIndexList 0 tableWords

convertWordsToIndexList :: Int -> [String] -> [((Int, Int), Char)]
convertWordsToIndexList _ [] = []
convertWordsToIndexList curRow wordsList = (drop 1 $ convertRow row) ++ (convertWordsToIndexList (curRow + 1) next)
    where
        row:next = wordsList
        convertRow = scanl scanner ((curRow, -1), ' ') :: String -> [((Int, Int), Char)]
        scanner :: ((Int, Int), Char) -> Char -> ((Int, Int), Char)
        scanner cidx curChar = ((curRow,curCol+1), curChar)
            where
                ((_,curCol),_) = cidx :: ((Int, Int), Char)

makeSolutionTable :: A.Array (Int,Int) Char -> A.Array (Int,Int) Char
makeSolutionTable sourceTable = A.array (A.bounds sourceTable) mappedList
    where
        indices = A.indices sourceTable
        mappedList = map mapNewIdx indices
        mapNewIdx idx
            | isDot && hasNeighbourWolf = (idx, 'D')
            | otherwise = (idx, curChar)
            where
                curChar = sourceTable A.! idx
                isDot = curChar == '.'
                (cR, cC) = idx
                hasNeighbourWolf = M.getAny . mconcat $ anyB
                    where
                        nextidx = [(1,0),(-1,0),(0,1),(0,-1)]
                        okIdx = filter (A.inRange $ A.bounds sourceTable) . map (\(r,c) -> (r+cR, c+cC)) $ nextidx
                        anyB = map (\cidx -> M.Any $ ((sourceTable A.! cidx) == 'W')) okIdx

import Data.List

main = getContents >>= putStr . solve . preProcess

preProcess = tail . lines . map (\x -> if x == '.' then 'D' else x)

solve a
    | exam a && exam (transpose a) = unlines $ "Yes" : a
    | otherwise = "No\n"
    where exam = all isSafe
          isSafe [] = True
          isSafe ('W':'S':xs) = False
          isSafe ('S':'W':xs) = False
          isSafe (x:xs) = isSafe xs

import Data.List

main = getContents >>= putStr . solve . preProcess

preProcess = tail . lines . map (\x -> if x == '.' then 'D' else x)
solve a
    | exam a && exam (transpose a) = unlines $ "Yes" : a
    | otherwise = "No\n"
    where exam = all (\xs -> not (isInfixOf "WS" xs || isInfixOf "SW" xs))

{-# LANGUAGE LambdaCase #-}
import Data.List
main = interact exec
exec = (\(_:_:m) -> if all ok m && all ok (transpose m) then "Yes\n" ++ unlines (map (map (\case { '.' -> 'D'; x -> x })) m) else "No") . words
ok xs = not $ any (\case {('W', 'S') -> True; ('S', 'W') -> True; _ -> False; }) $ zip xs (tail xs)


{-# LANGUAGE BlockArguments, MultiWayIf #-}

import System.IO

solve iask n = iask 1 n >>= go 1 n
  where
    go l r lrmax
      | l == r     = pure l
      | l == r - 1 = pure if lrmax == l then r else l
      | otherwise  = do
          let
            m = (l + r) `div` 2
          lmmax <- iask l m
          mrmax <- iask m r
          if
            | lrmax == lmmax -> go l m lmmax
            | lrmax == mrmax -> go m r mrmax
            | lrmax > m -> go l m lmmax
            | lrmax < m -> go m r mrmax
            | otherwise -> undefined

main = do
  n <- readLn
  let
    iask l r = putStrLn ("? " <> show l <> " " <> show r) >> hFlush stdout >> readLn
  solve iask n >>= putStrLn . ("! " <>) . show

{-# LANGUAGE OverloadedStrings, FunctionalDependencies, FlexibleContexts,
    ConstraintKinds, BangPatterns #-}

import Control.Applicative
import Control.Monad
import Control.Monad.State
import Data.Array.IArray
import qualified Data.ByteString.Char8 as B
import Data.Char
import Data.Int
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IntMap
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import Data.List
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe
import Data.Monoid
import Data.Ratio
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Traversable
import System.IO

------

dropCR :: B.ByteString -> B.ByteString
dropCR s | not (B.null s) && B.last s == '\r' = B.init s
         | otherwise = s

bshow :: Show a => a -> B.ByteString
bshow = B.pack . show

------

getInt = fst . fromJust . B.readInt . dropCR <$> B.getLine

ask l r = do
  B.putStrLn $ B.unwords ["?", bshow (l + 1), bshow r]
  hFlush stdout
  (subtract 1) <$> getInt

answer i = do
  B.putStrLn $ B.unwords ["!", bshow (i + 1)]
  hFlush stdout

main = do
  n <- getInt
  p <- ask 0 n
  let r = sortOn (\(i, j) -> i - j) $
          filter (\(i, j) -> i /= j) [(0, p), (p + 1, n)]
  case r of
    [(i, j)] -> go p i j
    [(i, j), (k, l)]
      | i < p -> do q <- ask i (p + 1)
                    if p == q
                      then go p i j
                      else go p k l
      | otherwise -> do q <- ask p j
                        if p == q
                          then go p i j
                          else go p k l

go p i j | i < p = lgo i j p
         | otherwise = rgo p i j

lgo i j p | j - i == 1 = answer i
          | otherwise = let k = (i + j) `quot` 2 in
                          do q <- ask k (p + 1)
                             if p == q
                               then lgo k j p
                               else lgo i k p

rgo p i j | j - i == 1 = answer i
          | otherwise = let k = (i + j) `quot` 2 in
                          do q <- ask p k
                             if p == q
                               then rgo p i k
                               else rgo p k j

import qualified Data.ByteString.Lazy.Char8 as P
import qualified Data.ByteString.Builder as B
import System.IO

import Control.Monad.Trans.State
import Control.Monad.Trans.Class
import Data.List
import Data.Semigroup

boolInt :: Bool -> Int
boolInt p = if  p  then  1  else  0

query :: Int -> Int -> SIO Int
query li ri = let
  outBuilder = foldr (<>) (B.char7 '\n')
    $ intersperse (B.char7 ' ') (B.char7 '?' : map B.intDec [li, ri])
  in  do
  lift $ B.hPutBuilder stdout outBuilder >> hFlush stdout
  [response] <- readInts
  pure response

go :: Int -> Int -> Int -> SIO Int
go v li ri = let
  mi = div (li + ri) 2
  in  if  li + boolInt (elem v [li, ri]) == ri
  then  pure $ if  v == li
    then  ri
    else  li
  else  if  v < li
  then  do
    resp <- query v mi
    if  resp == v
      then  go v li mi
      else  go v (mi+1) ri
  else  if  ri < v
  then  do
    resp <- query (mi+1) v
    if  resp == v
      then  go v (mi+1) ri
      else  go v li mi
  else  do
  let
    innermi = mi + boolInt (v <= mi) - boolInt (even $ ri - li)
    lower = v <= innermi
  resp <- if  lower  then  query li innermi  else  query (innermi+1) ri
  if  (resp == v) == lower
    then  go v li innermi
    else  go v (innermi+1) ri

main :: IO ()
main = (P.getContents >>=) . evalStateT $ do
  [n] <- readInts
  sndMin <- query 1 n
  ans <- go sndMin 1 n
  lift $ B.hPutBuilder stdout
    $ B.char7 '!' <> B.char7 ' ' <> B.intDec ans <> B.char7 '\n'

type SIO = StateT P.ByteString IO

readLine :: SIO P.ByteString
readLine = do
  (out, next) <- P.break (<= '\r') <$> get
  put $ P.dropWhile (<= '\r') next
  pure out

readInts :: SIO [Int]
readInts = do
  currLine <- readLine
  pure [x | Just (x, _) <- P.readInt <$> P.words currLine]

{-# LANGUAGE BlockArguments, TypeApplications, MultiWayIf #-}

{-# OPTIONS_GHC -O2 #-}

module Main ( main ) where

import Control.Monad
import Data.Foldable
import System.IO

main :: IO ()
main = do
  n <- line1 @Int
  let
    go l r lrmax
      | l == r     = ianswer [l]
      | l == r - 1 = ianswer [if lrmax == l then r else l]
      | otherwise  = do
          let
            m = (l + r) `div` 2
          lmmax <- iask1 [l, m]
          mrmax <- iask1 [m, r]
          if
            | lrmax == lmmax -> go l m lmmax
            | lrmax == mrmax -> go m r mrmax
            | lrmax > m -> go l m lrmax
            | lrmax < m -> go m r mrmax
  i <- iask1 [1, n]
  go 1 n i

-- lib

nCases :: IO a -> IO ()
nCases m = do
  n <- readLn
  replicateM_ n m

line :: Read a => IO [a]
line = fmap read . words <$> getLine

line1 :: Read a => IO a
line1 = readLn

putBool :: Bool -> IO ()
putBool b = putStr if b then "Yes" else "No"

putBoolLn :: Bool -> IO ()
putBoolLn b = putStrLn if b then "Yes" else "No"

iask :: (Read b, Show a) => [a] -> IO [b]
iask l = putStrLn ("? " <> unwords (fmap show l)) >> hFlush stdout >> line

iask1 :: (Read b, Show a) => [a] -> IO b
iask1 l = putStrLn ("? " <> unwords (fmap show l)) >> hFlush stdout >> line1

ianswer :: Show a => [a] -> IO ()
ianswer l = putStrLn ("! " <> unwords (fmap show l)) <> hFlush stdout

{-# LANGUAGE BlockArguments, TypeApplications, MultiWayIf #-}
{-# OPTIONS_GHC -O2 #-}

module Main ( main ) where

import Control.Monad
import Data.Foldable
import System.IO

main :: IO ()
main = do
  n <- line1
  let
    go l r lrmax
      | l == r     = ianswer [l]
      | l == r - 1 = ianswer [if lrmax == l then r else l]
      | otherwise  = do
          let
            m = (l + r) `div` 2
            m' = if l == r - 2 then m else m + 1
          lmmax <- iask1 [l, m]
          mrmax <- iask1 [m', r]
          if
            | lrmax == lmmax -> go l m lmmax
            | lrmax == mrmax -> go m' r mrmax
            | lrmax > m -> go l m lrmax
            | otherwise -> go m' r mrmax
  i <- iask1 [1, n]
  go 1 n i

-- lib

nCases :: IO a -> IO ()
nCases m = do
  n <- readLn
  replicateM_ n m

line :: Read a => IO [a]
line = fmap read . words <$> getLine

line1 :: Read a => IO a
line1 = readLn

putBool :: Bool -> IO ()
putBool b = putStr if b then "Yes" else "No"

putBoolLn :: Bool -> IO ()
putBoolLn b = putStrLn if b then "Yes" else "No"

iask :: (Read b, Show a) => [a] -> IO [b]
iask l = putStrLn ("? " <> unwords (fmap show l)) >> hFlush stdout >> line

iask1 :: (Read b, Show a) => [a] -> IO b
iask1 l = putStrLn ("? " <> unwords (fmap show l)) >> hFlush stdout >> line1

ianswer :: Show a => [a] -> IO ()
ianswer l = putStrLn ("! " <> unwords (fmap show l)) <> hFlush stdout

{-# LANGUAGE BlockArguments, TypeApplications, MultiWayIf #-}
{-# OPTIONS_GHC -O2 #-}

module Main ( main ) where

import Control.Monad
import Data.Foldable
import System.IO

main :: IO ()
main = do
  n <- line1 @Int
  let
    go l r lrmax
      | l == r     = ianswer [l]
      | l == r - 1 = ianswer [if lrmax == l then r else l]
      | otherwise  = do
          let
            m = (l + r) `div` 2
          lmmax <- iask1 [l, m]
          mrmax <- iask1 [m, r]
          if
            | lrmax == lmmax -> go l m lmmax
            | lrmax == mrmax -> go m r mrmax
            | lrmax > m -> go l m lrmax
            | otherwise -> go m r mrmax
  i <- iask1 @Int [1, n]
  go 1 n i

-- lib

nCases :: IO a -> IO ()
nCases m = do
  n <- readLn
  replicateM_ n m

line :: Read a => IO [a]
line = fmap read . words <$> getLine

line1 :: Read a => IO a
line1 = readLn

putBool :: Bool -> IO ()
putBool b = putStr if b then "Yes" else "No"

putBoolLn :: Bool -> IO ()
putBoolLn b = putStrLn if b then "Yes" else "No"

iask :: (Read b, Show a) => [a] -> IO [b]
iask l = putStrLn ("? " <> unwords (fmap show l)) >> hFlush stdout >> line

iask1 :: (Read b, Show a) => [a] -> IO b
iask1 l = putStrLn ("? " <> unwords (fmap show l)) >> hFlush stdout >> line1

ianswer :: Show a => [a] -> IO ()
ianswer l = putStrLn ("! " <> unwords (fmap show l)) <> hFlush stdout

{-# LANGUAGE BlockArguments, TypeApplications, MultiWayIf #-}
{-# OPTIONS_GHC -O2 #-}

module Main ( main ) where

import Control.Monad
import Data.Foldable
import System.IO

main :: IO ()
main = do
  n <- line1 @Int
  let
    go l r lrmax
      | l == r     = ianswer [l]
      | l == r - 1 = ianswer [if lrmax == l then r else l]
      | otherwise  = do
          let
            m = (l + r) `div` 2
          lmmax <- iask1 [l, m]
          mrmax <- iask1 [m, r]
          if
            | lrmax == lmmax -> go l m lmmax
            | lrmax == mrmax -> go m r mrmax
            | lrmax >= m -> go l m lrmax
            | otherwise  -> go m r mrmax
  i <- iask1 @Int [1, n]
  go 1 n i

-- lib

nCases :: IO a -> IO ()
nCases m = do
  n <- readLn
  replicateM_ n m

line :: Read a => IO [a]
line = fmap read . words <$> getLine

line1 :: Read a => IO a
line1 = readLn

putBool :: Bool -> IO ()
putBool b = putStr if b then "Yes" else "No"

putBoolLn :: Bool -> IO ()
putBoolLn b = putStrLn if b then "Yes" else "No"

iask :: (Read b, Show a) => [a] -> IO [b]
iask l = putStrLn ("? " <> unwords (fmap show l)) >> hFlush stdout >> line

iask1 :: (Read b, Show a) => [a] -> IO b
iask1 l = putStrLn ("? " <> unwords (fmap show l)) >> hFlush stdout >> line1

ianswer :: Show a => [a] -> IO ()
ianswer l = putStrLn ("! " <> unwords (fmap show l)) <> hFlush stdout

{-# LANGUAGE OverloadedStrings, FunctionalDependencies, FlexibleContexts,
    ConstraintKinds, BangPatterns #-}

import Control.Applicative
import Control.Monad
import Control.Monad.State
import Data.Array.IArray
import qualified Data.ByteString.Char8 as B
import Data.Char
import Data.Int
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IntMap
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import Data.List
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe
import Data.Monoid
import Data.Ratio
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Traversable
import System.IO

------

dropCR :: B.ByteString -> B.ByteString
dropCR s | not (B.null s) && B.last s == '\r' = B.init s
         | otherwise = s

bshow :: Show a => a -> B.ByteString
bshow = B.pack . show

------

getInt = fst . fromJust . B.readInt . dropCR <$> B.getLine

ask l r = do
  B.putStrLn $ B.unwords ["?", bshow (l + 1), bshow r]
  hFlush stdout
  (subtract 1) <$> getInt

answer i = do
  B.putStrLn $ B.unwords ["!", bshow (i + 1)]
  hFlush stdout

main = do
  n <- getInt
  p <- ask 0 n
  let r = sortOn (\(i, j) -> i - j) $
          filter (\(i, j) -> i /= j) [(0, p), (p + 1, n)]
  case r of
    [(i, j)] -> go p i j
    [(i, j), (k, l)]
      | i < p -> do q <- ask i (p + 1)
                    if p == q
                      then go p i j
                      else go p k l
      | otherwise -> do q <- ask p l
                        if p == q
                          then go p k l
                          else go p i j

go p i j | i < p = lgo i j p
         | otherwise = rgo p i j

lgo i j p | j - i == 1 = answer i
          | otherwise = let k = (i + j) `quot` 2 in
                          do q <- ask k (p + 1)
                             if p == q
                               then lgo k j p
                               else lgo i k p

rgo p i j | j - i == 1 = answer i
          | otherwise = let k = (i + j) `quot` 2 in
                          do q <- ask p k
                             if p == q
                               then rgo p i k
                               else rgo p k j


module Main where
    f :: Int -> Int -> Int
    f 0 res = res
    f n res = f (n `div` 2) (res + 1)

    solve :: IO ()
    solve = do
        n <- getLine
        print $ 2 ^ ((f (read n :: Int) 0) - 1) - 1

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

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

main = interact $ unlines . map (show . (\x -> 2^x - 1) . floor . logBase 2 . fromIntegral . read) . tail . lines

import Data.Bits
main = interact (unlines . map (\x -> show ((\y->2^y-1)(fn x 0))). tail . map g. lines) where g x = read x ::Int
  -- text <- readFile "inputt.txt"

fn :: Int -> Int -> Int
fn n acc = if ((==) n 1) then acc else fn (div n 2) ((+) acc 1)

import Control.Monad

main = do
    t <- read <$> getLine
    replicateM_ t (getLine >>= print . pred . maxBit . read)

maxBit :: Int -> Int
maxBit 1 = 1
maxBit n = 2 * maxBit (quot n 2)

import Control.Arrow ((>>>))

main = interact $ words >>> drop 1 >>> map (read >>> solve >>> show) >>> unlines

solve :: Int -> Int
solve n = subtract 1 $ last $ takeWhile (<= n) $ iterate (* 2) 1

import Control.Monad
import Data.Maybe
import Data.Bits
import qualified Data.ByteString.Char8 as C
import Data.ByteString.Builder
import System.IO

parseInt :: C.ByteString -> Int
parseInt = fst . fromJust . C.readInt

getInt :: IO Int
getInt = fmap parseInt C.getLine

main :: IO ()
main = do
  t <- getInt
  result <- fmap mconcat $ replicateM t $ do
    n <- getInt
    return $ intDec (bit (finiteBitSize n - 1 - countLeadingZeros n) - 1) `mappend` charUtf8 '\n'
  hPutBuilder stdout result


import Data.List

main::IO ()
main =
    do cs <- getContents
       (putStr.showSolve.solve.tail.lines) cs

showSolve ::(Int,Int)->String
showSolve (a,b) = show a ++ " " ++ show b

findAster::String->Bool
findAster s = findAster1 s 0

findAster1 :: String->Int->Bool
findAster1 [] i = (i == 1)
findAster1 (x:xs) i =
  if (x == '*' )
  then findAster1 xs (i+1)
  else findAster1 xs i

getP ::[(Bool,Int)]->(Bool,Int)
getP [] = (False,0)
getP (x@(b,_):xs) = if b
              then x
              else getP xs

solve::[String]->(Int,Int)
solve s = (x,y)
    where
      (_,x) = getP $ zip (map findAster s) [1..]
      (_,y) = getP $ zip (map findAster $ transpose s) [1..]

import Data.List
main=interact$f.drop 2.words
f s=g s++" "++g(transpose s)
g=show.(+1).head.findIndices((=="*").filter(=='*'))

import Data.List

main = putStrLn . solve . drop 2 . words =<< getContents
solve s = f s ++ " " ++ f (transpose s)
f = show . (+1) . head . elemIndices 1 . map (length . filter(=='*'))

import Control.Monad
import Data.Bits

main = do
    [r, c] <- fmap (map read . words) getLine
    a <- replicateM r getLine
    let b = [(i, j) | i <- [1 .. r], j <- [1 .. c], a!!(i-1)!!(j-1) == '*']
        f g = show $ foldl1 xor $ map g b
        in putStrLn $ unwords $ [f fst, f snd]

solve :: Int -> Int -> [String] -> (Int, Int)
solve n m lines  =  (notEq x1 x2 x3, notEq y1 y2 y3)
  where
    [(x1,y1),(x2,y2),(x3,y3)]  =  solve' 1 1 lines
    solve' _ _ []  =  []
    solve' n _ ("" : xs)  =  solve' (n+1) 1 xs
    solve' n m (('*' : s) : xs)  =  (n,m) : solve' n (m+1) (s:xs)
    solve' n m ((_:s) : xs)  =  solve' n (m+1) (s:xs)
    notEq a b c
      | a == b  =  c
      | a == c  =  b
      | b == c  =  a
      | otherwise  =  error ":'("

main :: IO ()
main  =  do
    [n, m]  <-  getLine >>= return . map read . words
    lines  <-  mapM (const getLine) [1..n]
    let (a, b)  =  solve n m lines
    putStrLn $ concat [show a, " ", show b]

import Data.List

f::Int->[String]->[(Int,Int)]
f _ []=[]
f y (str:strs)
    |'*' `elem` str=let xs=elemIndices '*' str
                    in (zip xs (repeat y))++(f (y+1) strs)
    |otherwise=f (y+1) strs

main = do
       e<-getLine
       es<-getContents
       let ds=lines es
           xs=f 1 ds
           ans=nub [(show (y1))++" "++(show (x1+1))|(x1,_)<-xs,(_,y1)<-xs,(x1,y1) `notElem` xs]
       putStrLn $ head ans

getClues rows =
  [(x, y) |
   (x, row) <- zip [1..] rows,
   (y, a)   <- zip [1..] row,
   a == '*']

updateBounds ([minX, maxX], [minY, maxY]) (x, y) =
  ([min minX x, max maxX x],
   [min minY y, max maxY y])

getBounds clues = foldl updateBounds ([101, 0], [101, 0]) clues

solve board =
  let clues = getClues board
      (xs, ys) = getBounds clues
  in head [[x, y] |
           x <- xs,
           y <- ys,
           not $ elem (x, y) clues]

main = interact $ unwords . map show . solve . tail . lines

process :: [[Char]] -> (Int,Int)
process s
    | ax /= bx && ay /= by = (ax+bx-cx,ay+by-cy)
    | bx /= cx && by /= cy = (bx+cx-ax,by+cy-ay)
    | otherwise = (cx+ax-bx,cy+ay-by)
    where
        [(ax,ay),(bx,by),(cx,cy)] = concat $ zipWith f [1..] s
        f i xs = [(i,j) | j <- (g xs)]
        g = map fst . filter ((=='*').snd) . zip [1..]

readInt :: String -> Int
readInt = read

main :: IO ()
main = do
    [n,m] <- fmap (map readInt.words) getLine
    s <- fmap lines getContents
    let (r,c) = process s
    putStrLn $ show r ++ " " ++ show c


import Control.Monad
import Data.Int (Int64)
import Data.Sequence (Seq)
import qualified Data.ByteString.Char8 as C
import qualified Data.Sequence as Seq

f t | even t = 0
    | otherwise = 1

gao :: (Int, Int) -> Int
gao (x1, y1) = f x1 + f y1 * 2 + 1

main = do
  (n : _) <- fmap (map readInt . C.words) C.getLine
  putStrLn "YES"
  r <- replicateM n $ do
    (x1 : y1 : x2 : y2 : _) <- fmap (map readInt . C.words) C.getLine
    return (x1, y1)
  mapM putStrLn $ map show $ map gao r
  where
  readInt s = let Just (i, _) = C.readInt s in i

main = interact $ unlines . ("YES":) . map (show . (\(a:b:_) -> a `mod` 2 + b `mod` 2 * 2 + 1) . map read) . filter ((==4) . length) . map words . lines

main = interact $ unlines . map (show . (\(a:b:_) -> a `mod` 2 + b `mod` 2 * 2 + 1) . map read) . filter ((==4) . length) . map words . lines