import Control.Monad

solve :: IO ()
solve = do
    [w, h] <- readMany :: IO [Int]
    [x1, y1, x2, y2] <- readMany :: IO [Int]
    [w', h'] <- readMany :: IO [Int]
    let rs = concat (
            [[w' - x1, x2 - (w - w')] | w' + x2 - x1 <= w] ++
            [[h' - y1, y2 - (h - h')] | h' + y2 - y1 <= h])
    print $ if null rs then -1 else minimum $ map (max 0) rs

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

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

{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE Strict #-}

import Control.Applicative (liftA2)
import Control.Monad (replicateM)
import Control.Monad.State (State, evalState, get, gets, put)
import qualified Data.ByteString.Lazy.Char8 as C
import Data.Maybe (fromJust)

main :: IO ()
main = C.interact $ runScanner (C.unlines <$> numberOf (C.pack <$> testCase))

testCase :: Scanner String
testCase = do
  (wr, hr) <- pair int int
  (x1, y1) <- pair int int
  (x2, y2) <- pair int int
  (w, h) <- pair int int
  let wt = x2 - x1
  let ht = y2 - y1
  let inf = 10^9
  let dw = if wt + w > wr then inf else w - max x1 (wr - x2)
  let dh = if ht + h > hr then inf else h - max y1 (hr - y2)
  let ans = max 0 $ min dw dh
  return . show $ if ans == inf then -1 else ans

-------------------------- Template ------------------------------------------
type Scanner = State [C.ByteString]

runScanner :: Scanner a -> C.ByteString -> a
runScanner = runScannerWith C.words

runScannerWith :: (C.ByteString -> [C.ByteString]) -> Scanner a -> C.ByteString -> a
runScannerWith t s = evalState s . t

peek :: Scanner C.ByteString
peek = gets head

bstr :: Scanner C.ByteString
bstr = get >>= \case s : ss -> put ss >> return s

str :: Scanner String
str = C.unpack <$> bstr

int :: Scanner Int
int = fst . fromJust . C.readInt <$> bstr

integer :: Scanner Integer
integer = read . C.unpack <$> bstr

double :: Scanner Double
double = read . C.unpack <$> bstr

decimal :: Int -> Scanner Int
decimal p = round . ((10 ^ p) *) <$> double

numberOf :: Scanner a -> Scanner [a]
numberOf s = int >>= flip replicateM s

many :: Scanner a -> Scanner [a]
many s = get >>= \case [] -> return []; _ -> (:) <$> s <*> many s

till :: (C.ByteString -> Bool) -> Scanner a -> Scanner [a]
till p s = do
  t <- peek
  if p t
    then return []
    else (:) <$> s <*> till p s

times :: Int -> Scanner a -> Scanner [a]
times = replicateM

(><) = times

two, three, four :: Scanner a -> Scanner [a]
[two, three, four] = map times [2 .. 4]

pair :: Scanner a -> Scanner b -> Scanner (a, b)
pair = liftA2 (,)

import Control.Monad

solve :: IO ()
solve = do
    [w, h] <- readMany :: IO [Int]
    [x1, y1, x2, y2] <- readMany :: IO [Int]
    [w', h'] <- readMany :: IO [Int]
    let isect (l1, h1) (l2, h2) = max 0 $ min h1 h2 - max l1 l2
        fitW = w' + x2 - x1 <= w
        fitH = h' + y2 - y1 <= h
        chk xx yy | ix > 0 && iy > 0 = [ix | fitW] ++ [iy | fitH]
                  | otherwise = [0]
                  where ix = isect (x1, x2) xx
                        iy = isect (y1, y2) yy
        rs = chk (0, w') (0, h')
           ++ chk (w - w', w) (0, h')
           ++ chk (0, w') (h - h', h)
           ++ chk (w - w', w) (h - h', h)
    print $ if null rs then -1 else minimum rs

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

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

import Control.Monad

solve :: IO ()
solve = do
    [w, h] <- readMany :: IO [Int]
    [x1, y1, x2, y2] <- readMany :: IO [Int]
    [w', h'] <- readMany :: IO [Int]
    let isect (l1, h1) (l2, h2) = max 0 $ min h1 h2 - max l1 l2
        fitW = w' + x2 - x1 <= w
        fitH = h' + y2 - y1 <= h
        chk xx yy | ix + iy > 0 = [ix | fitW] ++ [iy | fitH]
                  | otherwise = [0]
                  where ix = isect (x1, x2) xx
                        iy = isect (y1, y2) yy
        rs = chk (0, w') (0, h')
           ++ chk (w - w', w) (0, h')
           ++ chk (0, w') (h - h', h)
           ++ chk (w - w', w) (h - h', h)
    print $ if null rs then -1 else minimum rs

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

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

import Control.Monad

solve :: IO ()
solve = do
    [w, h] <- readMany :: IO [Int]
    [x1, y1, x2, y2] <- readMany :: IO [Int]
    [w', h'] <- readMany :: IO [Int]
    let isect (l1, h1) (l2, h2) = max 0 $ min h1 h2 - max l1 l2
        chkW xx = [isect (x1, x2) xx | w' + x2 - x1 <= w]
        chkH yy = [isect (y1, y2) yy | h' + y2 - y1 <= h]
        rs = chkW (0, w')      ++ chkH (0, h')
           ++ chkW (w - w', w) ++ chkH (0, h')
           ++ chkW (0, w')     ++ chkH (h - h', h)
           ++ chkW (w - w', w) ++ chkH (h - h', h)
        ans | null rs = -1
            | otherwise = minimum rs
    print ans

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

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


{-# LANGUAGE TypeFamilies, UndecidableInstances, FlexibleInstances, MultiParamTypeClasses, ScopedTypeVariables #-}
module Main (main) where
import System.IO
import Data.Ord
import Data.Int
import Data.Bits
import Data.Char
import Data.Foldable
import Data.List
import Data.Maybe
import Data.Array.ST.Safe
import Data.Array.Unboxed
import Control.Monad
import Control.Monad.ST.Safe
import Control.Monad.Trans.Class
import Control.Monad.Trans.State.Strict (StateT)
import qualified Control.Monad.Trans.State.Strict as StateT
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IntMap
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as C

getNewLine :: SIO ByteString
getNewLine = (lift $ C.hGetLine stdin) >>= (\z -> let x = C.dropWhile isSpace z in if C.null x then getNewLine else return x)

getRemain :: ByteString -> SIO ByteString
getRemain b = let bns = C.dropWhile isSpace b in
                if C.null bns then getNewLine >>= liftM2 (>>=) StateT.put (const . return)
                else return bns

data HInt
data HInt64
data HNormal
data HString
data HList

type SIO a = StateT ByteString IO a

class IOInteract a where
    convert :: ByteString -> (a, ByteString)
    toShowS :: a -> String -> String
    toShow :: a -> String
    toShow x = toShowS x []

instance (IOType a ~ flag, IOInteractp flag a) => IOInteract a where
    convert = convertp (undefined :: flag)
    toShowS = toShowSp (undefined :: flag)

type family IOType x where
    IOType Int = HInt
    IOType Int64 = HInt64
    IOType String = HString
    IOType [x] = HList
    IOType x = HNormal

class IOInteractp flag a where
    convertp :: flag -> ByteString -> (a, ByteString)
    toShowSp :: flag -> a -> String -> String

instance (Show a, Read a) => IOInteractp HNormal a where
    convertp _ x = let (a, b) = C.break isSpace x in (read $ C.unpack a, b)
    toShowSp _ x = shows x

instance IOInteractp HInt Int where
    convertp _ x = fromMaybe (error "parse") $ C.readInt x
    toShowSp _ x = shows x

instance IOInteractp HInt64 Int64 where
    convertp _ x = let (a, b) = fromMaybe (error "parse") $ C.readInteger x in (fromIntegral a, b)
    toShowSp _ x = shows x

instance IOInteractp HString String where
    convertp _ x = let (a, b) = C.break isSpace x in (C.unpack a, b)
    toShowSp _ x = showString x

instance (IOInteract a) => IOInteractp HList [a] where
    convertp _ x = let (a, b) = convert x in ([a], b)
    toShowSp _ [] = id
    toShowSp _ (x : []) = toShowS x
    toShowSp dum (x : xs) = toShowS x . showChar ' ' . toShowSp dum xs

get :: IOInteract a => SIO a
get = uncurry (flip (liftM2 seq . StateT.put) . return) =<< fmap convert . getRemain =<< StateT.get
put :: IOInteract a => a -> SIO ()
put = lift . (hPutStr stdout) . toShow
putln :: IOInteract a => a -> SIO ()
putln = lift . (hPutStrLn stdout) . toShow
flush :: SIO ()
flush = lift (hFlush stdout)

main :: IO ()
main = StateT.evalStateT main_ C.empty

main_ :: SIO ()
-------------------------------------------- template ------------------------------------------------------
main_ = do
  t <- get
  replicateM_ t f1

f1 :: SIO ()
f1 = do
  (n, x, y) <- liftM3 (,,) get get get
  putln $ f2 n x y

f2 :: Int64 -> Int64 -> Int64 -> [Int64]
f2 n x y = let d = y - x in
           let dividers = f3 d in
           let candidate = map (\d ->
                                  let cnt = div (y - x) d + 1 in
                                  if cnt > n then Nothing
                                  else
                                  let k = div (x - 1) d in
                                  if cnt + k >= n then Just [y-(n-1)*d,y-(n-2)*d..y]
                                  else let max = y + (n - (cnt + k)) * d in Just [max-(n-1)*d, max-(n-2)*d..max]
                               ) dividers in
           let maxc = foldl (\b a -> case a of
                                      Nothing -> b
                                      Just aa -> if maximum aa > maximum b then b else aa)
                      [1000000000000000] candidate
           in
           maxc

f3 :: Int64 -> [Int64]
f3 d =
  let aux i = if i > d then [] else if mod d i == 0 then i : aux (i + 1) else aux (i + 1) in
  aux 1

import Control.Monad
import Data.List

factors :: Int -> [Int]
factors n = [x | x <- [1..n],  mod n x == 0]

solve :: Int -> Int -> Int -> [Int]
solve n x y = let
  opts = do
    inc <- factors (y - x)
    let
      decrs = take n $ takeWhile (> 0) $ iterate (subtract inc) y
      start = minimum decrs
    [take n (iterate (+ inc) start) | start <= x]
  in  head . sort $ map reverse opts

main :: IO ()
main = do
  t <- read <$> getLine
  replicateM_ t $ do
    [n, x, y] <- map read . words <$> getLine
    putStrLn $ unwords (map show (solve n x y))


--module Test where
import Data.List

solve :: IO ()
solve = do
    nLine <- getLine
    let n = read nLine :: Int
    line <- getLine
    let list = sort $ read <$> words line :: [Int]
    let ans = getAns list
    putStrLn $ show ans

getAns :: [Int] -> Int
getAns [] = 0
getAns (x : xs) = if sz == 0 then 0
                  else 1 + getAns (drop sz list) where
                      sz = tmp x 1 xs
                      list = (x : xs)

tmp :: Int -> Int -> [Int] -> Int
tmp last sz list | sz >= last = sz
                 | otherwise  = if list == [] then 0
                                else tmp (head list) (sz + 1) (tail list)

main :: IO ()
main = do
    line <- getLine
    let t = read line :: Int
    sequence_ (take t $ repeat solve)

import qualified Data.List as L

type Long = Int

solve :: [Int] -> Int
solve es = formGroups (L.sort es) 0

formGroups :: [Int] -> Int -> Int
formGroups [] _ = 0
formGroups (a:rest) curSize
    | a <= curSize + 1  = 1 + formGroups rest 0
    | otherwise         = formGroups rest (curSize+1)

testCases :: Int -> IO ()
testCases 0 = return ()
testCases t = do
    _ <- getLine
    es <- readWords
    print $ solve es
    testCases (t-1)

main :: IO ()
main = do
    (t:_) <- readWords
    testCases t

readWords :: (Read a) => IO [a]
readWords = map read . words <$> getLine

import Control.Monad
import Data.List

main = do
    t <- read <$> getLine
    replicateM_ t test

test = do
    getLine
    es <- map read . words <$> getLine
    print $ solve 1 $ sort es

solve _ [] = 0
solve c (e:es)
    | e <= c = 1 + solve 1 es
    | otherwise = solve (c + 1) es

import Control.Monad
import Data.List

main :: IO ()
main = do
  t <- readI
  replicateM_ t $ do
    n <- readI
    es <- readIs
    print $ solve n es

readI :: IO Int
readI = read <$> getLine

readIs :: IO [Int]
readIs = fmap read . words <$> getLine

solve :: Int -> [Int] -> Int
solve n es = go (sort es) [] n 0 0
  where
    go es0 (c:_) en cn result | c == cn = go es0 [] en 0 (result+1)
    go (e:es) _ en _ result | e > en = result
    go (e:es) cs0 en cn result = go es (e:cs0) en (cn+1) result
    go [] _ _ _ result = result

import Control.Monad
import Data.List

main :: IO ()
main = do
  t <- readI
  replicateM_ t $ do
    n <- readI
    es <- readIs
    print $ solve n es

readI :: IO Int
readI = read <$> getLine

readIs :: IO [Int]
readIs = fmap read . words <$> getLine

solve :: Int -> [Int] -> Int
solve n es = go n (sortBy (flip compare) es) (0 :: Int)
  where
    go n [] result = result
    go n (e:_) result | n < e = result
    go n (e:es) result = go (n-e) (drop (e-1) es) (result+1)

type Long = Int

solve :: Int -> [Int] -> Int
solve n es = formGroups es 0

formGroups :: [Int] -> Int -> Int
formGroups [] _ = 0
formGroups (a:rest) curSize
    | a <= curSize + 1  = 1 + formGroups rest 0
    | otherwise         = formGroups rest (curSize+1)

testCases :: Int -> IO ()
testCases 0 = return ()
testCases t = do
    (n:_) <- readWords
    es <- readWords
    print $ solve n es
    testCases (t-1)

main :: IO ()
main = do
    (t:_) <- readWords
    testCases t

readWords :: (Read a) => IO [a]
readWords = map read . words <$> getLine


{-# LANGUAGE MultiWayIf #-}

import Control.Monad

main :: IO ()
main = do
  n <- read <$> getLine
  replicateM_ n $ do
    [c, m, x] <- map read . words <$> getLine
    print $ solve c m x

solve :: Int -> Int -> Int -> Int
solve c' m' x' = mx + oth
  where oth = if
          | c == 0 || m == 0 -> 0
          | 2 * c < m -> c
          | 2 * m < c -> m
          | otherwise -> (c + m) `div` 3
        mx = minimum [c', m', x']
        (c, m, x) = (c' - mx, m' - mx, x' - mx)

import Data.List
import Data.Int
import qualified Data.Array as A
import Data.Array((!))
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 =
  let n :: Int
      (n:xs) = map read $ words input
      queries = bythree xs
      answers = map solve' queries
  in intercalate "\n" $ map show answers

bythree :: [Int] -> [(Int, Int, Int)]
bythree [] = []
bythree (a:b:c:xs) = (a, b, c):(bythree xs)

solve' :: (Int, Int, Int) -> Int
solve' (a, b, c) =
  let base = minimum (a:b:c:[])
      a' = a - base
      b' = b - base
      c' = c - base
      cleft = c == base
  in if cleft then
       base + min (div (a' + b') 3) (min a' b')
    else
       base

import Data.List
import Data.Int
import qualified Data.Array as A
import Data.Array((!))
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 =
  let n :: Int
      (n:xs) = map read $ words input
      queries = bythree xs
      answers = map solve' queries
  in intercalate "\n" $ map show answers

bythree :: [Int] -> [(Int, Int, Int)]
bythree [] = []
bythree (a:b:c:xs) = (a, b, c):(bythree xs)

solve' :: (Int, Int, Int) -> Int
solve' (a, b, c) =
  let base = minimum (a:b:c:[])
      a' = a - base
      b' = b - base
      c' = c - base
      cleft = c == base
  in if cleft then
       base + (div (a' + b') 3)
    else
       base

import Control.Monad

main :: IO ()
main = do
  n <- read <$> getLine
  replicateM_ n $ do
    [c, m, x] <- map read . words <$> getLine
    print $ solve c m x

solve :: Int -> Int -> Int -> Int
solve c m x = h c m x 0
  where h c m x acc
          | c == 0 || m == 0 || c + m + x < 3 = acc
          | x /= 0 = h (c - 1) (m - 1) (x - 1) (acc + 1)
          | c > m = h (c - 2) (m - 1) 0 (acc + 1)
          | otherwise = h (c - 1) (m - 1) 0 (acc + 1)

import Control.Monad

main :: IO ()
main = do
  n <- read <$> getLine
  replicateM_ n $ do
    [c, m, x] <- map read . words <$> getLine
    print $ solve c m x

solve :: Int -> Int -> Int -> Int
solve c m x = mx + h (c - mx) (m - mx) (x - mx) 0
  where h c m x acc
          | c == 0 || m == 0 || c + m + x < 3 = acc
          | c > 2 * m = 2 * m
          | 2 * c > m = 2 * c
          | c > m = h (c - 2) (m - 1) 0 (acc + 1)
          | otherwise = h (c - 1) (m - 2) 0 (acc + 1)
        mx = maximum [c, m, x]


import Data.IntSet (IntSet, empty, member, fromList, union)

calcH :: Int -> Int -> [Int]
calcH n a = if a > n
  then []
  else (a: calcH n (2 * a))

calcHH :: Int -> IntSet -> Int -> [Int]
calcHH n a x = if x > n
  then []
  else if x `member` a
    then calcHH n a (x + 1)
    else let a1 = calcH n x in a1 ++ calcHH n (a `union` ( fromList a1)) (x + 1)

calc :: Int -> [Int]
calc n = calcHH n empty 1

pr :: [Int] -> IO ()
pr a = putStrLn $ concat $ map (\x -> show x ++ " ") a

for :: Int -> IO ()
for 0 = pure ()
for i = do
  n <- readLn :: IO Int
  print 2
  pr $ calc n
  for $ i - 1

main :: IO ()
main = do
  n <- readLn :: IO Int
  for n

--
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DatatypeContexts #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- {-# LANGUAGE Strict #-}
{-# LANGUAGE TupleSections #-}
{-# OPTIONS_GHC -O3 -funbox-strict-fields -fvia-C -optc-O3 #-}

import Control.Arrow ((>>>))
import Control.Monad (
    MonadPlus (mplus),
    foldM_,
    forM_,
    guard,
    join,
    replicateM,
    replicateM_,
 )
import qualified Control.Monad as M
import qualified Control.Monad.ST.Strict as ST
import qualified Control.Monad.State.Strict as S
import qualified Control.Monad.Trans.Class as T

import qualified Data.Array as A
import qualified Data.Array.MArray.Safe as MA
import qualified Data.Array.IO.Safe as IOA
import qualified Data.Array.ST.Safe as STA
import qualified Data.Array.Unboxed as UA

import Data.Bits
import qualified Data.ByteString.Char8 as B8
import Data.Char
import Data.Int

import Data.Function (on)
import Data.Functor ((<&>))
import Data.Bifunctor (bimap)
import Data.Maybe (fromJust, fromMaybe, isJust)
import qualified Data.Maybe as MB

import qualified Data.IntMap as IM
import qualified Data.IntSet as IS
import qualified Data.List   as L
import Data.List (
    elemIndices,
    find,
    findIndices,
    intersect,
    isPrefixOf,
    nub,
    sort,
    sortOn,
 )
import qualified Data.Map as M
import qualified Data.Set as S
import qualified Data.Ix as Ix
import qualified Data.Ratio as R

import Control.Monad.Cont (MonadIO (liftIO))
import Data.IORef (modifyIORef, newIORef, readIORef)
import GHC.IO.Handle (hDuplicateTo)
import System.IO (
    IOMode (ReadMode, WriteMode),
    openFile,
    stdin,
    stdout,
    hFlush
 )
import System.Posix.Internals (puts)
import Text.Printf (printf)
import qualified Data.Array.IO.Safe as UOA

-- import Debug.Trace (trace, traceM, traceShowM)
-- debug = flip trace
--
-- tracing t x = trace (t ++ " = " ++ show x) x
-- debugging x t = tracing t x
--
-- debug x t = x
-- trace t x = x
-- tracing t x = x
-- debugging x t = x

readIntB8 :: B8.ByteString -> Int
readIntB8 = B8.readInt >>> (fst . fromJust)

readIntB8s :: B8.ByteString -> [Int]
readIntB8s = B8.words >>> map (B8.readInt >>> (fst . fromJust))

readIntegerB8 :: B8.ByteString -> Integer
readIntegerB8 = B8.readInteger >>> (fst . fromJust)

readIntegerB8s :: B8.ByteString -> [Integer]
readIntegerB8s = B8.words >>> map (B8.readInteger >>> (fst . fromJust))

modifyArray :: (MA.MArray a e m, MA.Ix i) => a i e -> i -> (e -> e) -> m ()
modifyArray arr ix f = do
    value <- MA.readArray arr ix
    MA.writeArray arr ix $ f value

putsYesNo :: Bool -> IO ()
putsYesNo value = do
    let result :: String =
            if value
                then "YES"
                else "NO"
    printf "%s\n" result

{- END OF GENERAL -}

solve :: Int -> (Int, [Int])
solve n = (d, ps)
    where
        d = 2
        ps = solve' $ IS.fromList [1 .. n]

        solve' :: IS.IntSet -> [Int]
        solve' is = if (IS.size is == 0)
                then []
                else ts ++ solve' leastIs
            where
                i = IS.findMin is
                ts = L.takeWhile (<= n) $ L.iterate (* d) i
                leastIs = L.foldl' (flip IS.delete) is ts

main :: IO ()
main = do
    t <- B8.getLine <&> readIntB8
    replicateM_ t $ do
        n <- B8.getLine <&> readIntB8
        let (d, ps) = solve n
        printf "%d\n" d
        forM_ ps $ printf "%d "
        putStrLn ""

--
-- 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
import Data.Maybe
import Data.Set              qualified as Set

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

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

showAns xs = unlines ["2", unwords . map show $ xs]

solve n = concat . reverse . snd . fst . fromJust . L.find (Set.null . snd) . iterate (step n) $ ini
  where
    ini = ((Set.empty, []), Set.fromList nums)
    nums = [1..n] :: [Int]

step n ((yset0,ys0), xset0) = ((yset0 `Set.union` eset,
                                es : ys0),
                               xset0 Set.\\ eset)
  where
    h = Set.elemAt 0 xset0
    es = filter (`Set.notMember`yset0) . takeWhile (<=n) . iterate (*2) $ h
    eset = Set.fromList es

--
-- 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
import Data.Maybe
import Data.IntSet           qualified as Set

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

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

showAns xs = unlines ["2", unwords . map show $ xs]

solve n = concat . reverse . snd . fst . fromJust . L.find (Set.null . snd) . iterate (step n) $ ini
  where
    ini = ((Set.empty, []), Set.fromList nums)
    nums = [1..n] :: [Int]

step n ((yset0,ys0), xset0) = ((yset1, diff : ys0),
                               xset1)
  where
    h = Set.findMin xset0
    ((yset1, diff), xset1)
        | (2*h > n)  = ((Set.empty,              Set.toList xset0), Set.empty        )
        | otherwise  = ((yset0 `Set.union` eset, es              ), xset0 Set.\\ eset)
    eset = Set.fromDistinctAscList es
    es = filter (`Set.notMember`yset0) . takeWhile (<=n) . iterate (*2) $ h

import Control.Monad

solve :: Int -> (Int, [Int])
solve n = (2, concat [takeWhile (<= n) $ iterate (* 2) m | m <- [1, 3 .. n]])

testCase :: IO ()
testCase = do
    n <- readLn
    let (d, nums) = solve n
    print d
    putStrLn . unwords $ map show nums

main :: IO ()
main = readLn >>= (`replicateM_` testCase)

--
-- 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         hiding (loop)
import Control.Monad
import Data.ByteString.Char8 qualified as B
import Data.Char
import Data.IntSet           qualified as Set
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

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

showAns xs = unlines ["2", unwords . map show $ xs]

solve n = for2 ++ remdr
  where
    for2 = concat . takeWhile (not.null) . ([1]:) . map (takeWhile (<=n) . iterate (*2)) $ primes
    remdr = Set.toList . (Set.\\ for2set) . Set.fromList $ [1..n]
    for2set = Set.fromList for2

----------------------------
primes :: [Int]
primes = ppp  where  ppp = 2 : 3 : minus [5,7..] (unionAll [[p*p, p*p+2*p..] | p <- tail ppp])
minus :: Ord a => [a] -> [a] -> [a]
minus = minusBy compare
minusBy :: (a -> b -> Ordering) -> [a] -> [b] -> [a]
minusBy cmp = loop
  where
     loop [] _ys = []
     loop xs [] = xs
     loop (x:xs) (y:ys)
       = case cmp x y of
          LT -> x : loop xs (y:ys)
          EQ ->     loop xs ys
          GT ->     loop (x:xs) ys
unionAll :: Ord a => [[a]] -> [a]
unionAll = unionAllBy compare
unionAllBy :: (a -> a -> Ordering) -> [[a]] -> [a]
unionAllBy cmp = serve . foldt union' (Crowd []) . vips
  where
    msg = "Data.List.Ordered.unionAllBy:  the heads of the lists are not sorted"

    union' (VIP x xs) ys
       = VIP x $ case ys of
                  Crowd _ -> union' xs ys
                  VIP y yt -> case cmp x y of
                               LT -> union' xs ys
                               EQ -> union' xs yt
                               GT -> error msg
    union' (Crowd []) ys = ys
    union' (Crowd xs) (Crowd ys) = Crowd (unionBy cmp xs ys)
    union' xs@(Crowd (x:xt)) ys@(VIP y yt)
       = case cmp x y of
           LT -> VIP x (union' (Crowd xt) ys)
           EQ -> VIP x (union' (Crowd xt) yt)
           GT -> VIP y (union' xs yt)
data People a = VIP a (People a) | Crowd [a]
serve (VIP x xs) = x:serve xs
serve (Crowd xs) = xs
foldt :: (a -> a -> a) -> a -> [a] -> a
foldt plus zero = loop
  where
    loop []     = zero
    loop (x:xs) = x `plus` loop (pairs xs)

    pairs (x:y:zs) = plus x y : pairs zs
    pairs zs       = zs
vips xss = [ VIP x (Crowd xs) | (x:xs) <- xss ]
unionBy :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
unionBy cmp = loop
  where
     loop [] ys = ys
     loop xs [] = xs
     loop (x:xs) (y:ys)
       = case cmp x y of
          LT -> x : loop xs (y:ys)
          EQ -> x : loop xs ys
          GT -> y : loop (x:xs) ys
----------------------------

--
-- 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

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

showAns d xs = unlines [show (d::Int), unwords . map show $ xs]

solve d n = ds ++ os
  where
    (ds, os) = L.partition isD inds
    isD x = (x == 1 || x`mod`d == 0)
    inds = [1..n] :: [Int]

calcH :: Int -> Int -> [Int]
calcH n a = if a > n
  then []
  else (a: calcH n (2 * a))

calcHH :: Int -> [Int] -> Int -> [Int]
calcHH n a x = if x > n
  then []
  else if x `elem` a
    then calcHH n a (x + 1)
    else (x: calcHH n a (x + 1))

calc :: Int -> [Int]
calc n = let a = calcH n 1 in a ++ calcHH n a 1

pr :: [Int] -> IO ()
pr a = putStrLn $ concat $ map (\x -> show x ++ " ") a

for :: Int -> IO ()
for 0 = pure ()
for i = do
  n <- readLn :: IO Int
  print 2
  pr $ calc n
  for $ i - 1

main :: IO ()
main = do
  n <- readLn :: IO Int
  for n


import Data.Traversable (mapAccumL)
import Control.Arrow (second)
import Data.Functor ((<$>))
import Data.List (sort)

fun :: Int -> Int -> (Int, Int)
fun a x | 2 * a >= x = (max a x, 0)
        | otherwise  = second (+1) $ fun (2 * a) x

main = do
  [_, k] <- map read <$> words <$> getLine
  xs <- sort <$> map read <$> words <$> getLine
  putStrLn $ show $ sum $ snd $ mapAccumL fun k xs

{-# OPTIONS_GHC -O2 -optc-O3 -optc-ffast-math -funfolding-use-threshold=16 -fexcess-precision -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns #-}
module Main where
import qualified Data.ByteString.Char8 as B
import Data.List

main = do
  [n, k] <- fmap readInts B.getLine
  xs <- fmap readInts B.getLine
  print $ solve k xs

solve k = snd . foldl (\(!t, !c) x -> if x > 2 * t then (x, c + ceiling (logBase 2 $ fromIntegral x / 2 / fromIntegral t)) else (max x t, c)) (k, 0) . sort

readInt = maybe undefined fst . B.readInt
readInts = fmap readInt . B.words

import Data.List
import Control.Applicative

type MaxSolvedDifficulty = Int
type DecoforcesProblems = [Int]
type Accum = Int

f :: MaxSolvedDifficulty -> DecoforcesProblems -> Accum -> Accum
f k (x:xs) acc
  | x <= k*2     = f (max x k) xs acc
  | otherwise    = f (k*2) (x:xs) (acc + 1)

f _ [] acc = acc

main = interact (show . (\[[_, k], a] -> f k (sort a) 0) . (map (map (read :: String -> Int) . words) . lines))

{-# OPTIONS_GHC -O2 -optc-O3 -optc-ffast-math -funfolding-use-threshold=16 -fexcess-precision -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns #-}
module Main where
import qualified Data.ByteString.Char8 as B
import Data.List

main = do
  [n, k] <- fmap readInts B.getLine
  xs <- fmap readInts B.getLine
  print $ solve k xs

solve k = snd . foldl (\(!t, !c) x -> if x > 2 * t then (x, c + 1) else (x, c)) (k, 0) . sort

readInt = maybe undefined fst . B.readInt
readInts = fmap readInt . B.words

import Data.Traversable (mapAccumL)
import Control.Arrow (second)
import Data.Functor ((<$>))

fun :: Int -> Int -> (Int, Int)
fun a x | 2 * a >= x = (a, 0)
        | otherwise  = second (+1) $ fun (2 * a) x

main = do
  [_, k] <- map read <$> words <$> getLine
  xs <- map read <$> words <$> getLine
  putStrLn $ show $ sum $ snd $ mapAccumL fun k xs

import Data.Traversable (mapAccumL)
import Control.Arrow (second)
import Data.Functor ((<$>))
import Data.List (sort)

fun :: Int -> Int -> (Int, Int)
fun a x | 2 * a >= x = (a, 0)
        | otherwise  = second (+1) $ fun (2 * a) x

main = do
  [_, k] <- map read <$> words <$> getLine
  xs <- sort <$> map read <$> words <$> getLine
  putStrLn $ show $ sum $ snd $ mapAccumL fun k xs

import Data.Traversable (mapAccumL)
import Control.Arrow (second)
import Data.Functor ((<$>))
import Data.List (sort)

fun :: Int -> Int -> (Int, Int)
fun a x | 2 * a >= x = (a, 0)
        | otherwise  = second (+1) $ fun (2 * a) x

main = do
  [_, k] <- map read <$> words <$> getLine
  xs <- map read <$> words <$> getLine
  putStrLn $ show $ sum $ snd $ mapAccumL fun k xs

import Data.List
import Control.Applicative

type MaxSolvedDifficulty = Int
type DecoforcesProblems = [Int]
type Accum = Int

f :: MaxSolvedDifficulty -> DecoforcesProblems -> Accum -> Accum
f k (x:xs) acc
  | x <= k*2     = f (max x k) xs acc
  | otherwise    = f (k*2) (x:xs) (acc + 1)

f _ [] acc = acc

main = interact (show . (\[[_, k], a] -> f k a 0) . (map (map (read :: String -> Int) . words) . lines))


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

main = getLine >>= flip replicateM_ test . read

test = do
  s <- getLine
  print $
    length s
      - maximum
        ( [length $ filter (== c) s | c <- ['0' .. '9']]
            ++ [ (* 2)
                   $ flip div 2
                   $ length
                   $ map head
                   $ group
                   $ filter
                     (`elem` [c, d])
                     s
                 | c <- ['0' .. '9'],
                   d <- [succ c .. '9']
               ]
        ) -- ick haskell has ugly formatting

import Data.Bits
import Data.List

parse :: String -> [String]
parse = tail . lines

val1 :: Eq t => [t] -> t -> Int
val1 cs x = length $ filter (== x) cs

val :: Eq t => [t] -> (t, t) -> Int
val cs (x, y)
  | x == y     = val1 cs x
  | otherwise  = (.&. complement 1) . length . group $
      filter (`elem` [x, y]) cs

solve :: String -> Int
solve str = length str - maximum [val str (x, y)
  | x <- ['0' .. '9'], y <- [x .. '9']]

main :: IO ()
main = interact (unlines . map (show . solve) . parse)


module Main where

import Control.Monad (replicateM_)

import qualified Control.Monad.Reader as R
import qualified Control.Monad.State.Strict as ST

main :: IO ()
main = do
    numCases <- (read :: String -> Int) <$> getLine
    replicateM_ numCases solveOne

newtype Size  = Size Int
newtype Index = Index Int

solveOne :: IO ()
solveOne = do
    getLine  -- We get some unnecessary input from the test cases.
    piranhas@(p : ps) <- map (read :: String -> Int) . words <$> getLine

    let maxSize = maximum piranhas
    if p == maxSize && p > head ps
        then print 1
        else print $ R.runReader (go startIndex piranhas) (Size maxSize)
  where
    -- 2 because Codeforce counts from 1, and we skip the first element.
    startIndex :: Index
    startIndex = Index 2

go :: Index -> [Int] -> R.Reader Size Int
go _ []     = return (-1)
go _ [_]    = return (-1)
go (Index i) [x, y] = do
    Size maxSize <- R.ask
    if y == maxSize && y > x
        then return i
        else return (-1)
go (Index i) (x : y : z : xs) = do
    Size maxSize <- R.ask
    if y == maxSize && (y > x || y > z)
        then return i
        else go (Index $ i + 1) $ y : z : xs

import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as C

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

main :: IO ()
main = do
  [t] <- getInts
  replicateM_ t $ do
    _ <- getInts
    as <- getInts
    let xs = zip3 as (tail as) [(1::Int)..]
        m = maximum as
        l1 = filter (\(a, b, _) -> a == m && a > b) xs
        l2 = filter (\(a, b, _) -> b == m && a < b) xs
        notNull = not . null
    putStrLn $ show $ if notNull l1
                        then let (_, _, r) = head l1 in r
                        else if notNull l2
                               then let (_, _, r) = head l2 in r + 1
                               else -1

import Control.Monad

readInt :: IO Int
readInt =  read <$> getLine

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

main :: IO ()
main = do
    t <- readInt
    replicateM_ t $ do
        n <- readInt
        a <- readInts
        print $ solve a

solve :: [Int] -> Int
solve a = if null $ filter (/= m) a then -1 else index
        where m = maximum a
              index
                  | head a == m = length $ takeWhile (== m) a
                  | otherwise = succ $ length $ takeWhile (/= m) a

{-# LANGUAGE Safe #-}

import safe Control.Arrow ((>>>))
import safe Data.Tuple (swap)

main :: IO ()
main = interact $ lines >>> drop 1 >>> map (words >>> map read) >>> process >>> map show >>> unlines

process :: [[Int]] -> [Int]
process [] = []
process (_ : xs : xss) = solve xs : process xss

solve :: [Int] -> Int
solve xs
  | all (== head xs) xs = -1
  | otherwise = snd . maximum . filter (ok . fst) . zipWith (curry swap) [1 ..] $ zip3 xs (tail xs ++ [inf]) (inf : xs)
  where
    ok (x, y, z) = x > min y z
    inf = 10 ^ 9 + 1

module Main where

import Control.Monad (replicateM_)

import qualified Control.Monad.Reader as R
import qualified Control.Monad.State.Strict as ST

main :: IO ()
main = do
    numCases <- (read :: String -> Int) <$> getLine
    replicateM_ numCases solveOne

newtype Size  = Size Int
newtype Index = Index Int

solveOne :: IO ()
solveOne = do
    getLine  -- We get some unnecessary input from the test cases.
    piranhas@(p : ps) <- map (read :: String -> Int) . words <$> getLine

    let maxSize = maximum piranhas
    if p > head ps
        then print 1
        else print $ R.runReader (go startIndex piranhas) (Size maxSize)
  where
    -- 2 because Codeforce counts from 1, and we skip the first element.
    startIndex :: Index
    startIndex = Index 2

go :: Index -> [Int] -> R.Reader Size Int
go _ []     = return (-1)
go _ [_]    = return (-1)
go (Index i) [x, y] = do
    Size maxSize <- R.ask
    if y == maxSize && y > x
        then return i
        else return (-1)
go (Index i) (x : y : z : xs) = do
    Size maxSize <- R.ask
    if y == maxSize && (y > x || y > z)
        then return i
        else go (Index $ i + 1) $ y : z : xs