{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush) -- for interactive problems
import System.IO

-- Re-submitting the first version
s0 = Bu.string7
snl = Bu.string7 "\n" <> flush
i0 = Bu.intDec

ask :: Int -> Bu.Builder
ask k = s0 "? " <> i0 k <> snl

divide :: Integral a1 => [(a1, a2)] -> [[a2]]
divide xs = go xs [] [] where
  go [] ws bs = [ws,bs]
  go ((k,i):rest) ws bs
    | even k = go rest (i: ws) bs
    | odd k = go rest ws (i: bs)

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n
  let
    dis = zip (tail ds) [(2::Int)..]
    [whites, blacks] = divide dis -- 1 is de facto white
    [lw,lb] = length <$> [whites, blacks]
    askRest
      |lw<lb    = mconcat $ ask <$> whites
      |otherwise = mconcat $ ask <$> blacks
  adjssRaw <- replicateM (min lw lb) (getIntegrals n :: St [Int])
  let
    adjss
      |lw<lb = ds:adjssRaw
      |otherwise = adjssRaw
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    distribute left rights = [(left,right)|right<-rights]
    iadjss
      |lw<lb    = zipWith distribute (1:whites) adj1ss
      |otherwise = zipWith distribute blacks adj1ss
    printEdge (src,dst) = i0 src <> s0 " " <> i0 dst <> snl
    printEdges = mconcat . fmap printEdge
    printEdgess = mconcat $ printEdges <$> iadjss
    printAns = s0 "!" <> snl <> printEdgess
  return $ ask 1 <> askRest <> printAns
------------------------------- main --------------------------------
type St = StateT [B8.ByteString] Identity

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n  -- get the first row of adjacency matrix
  let
    anchors = getAnchors ds
    readFirst = if head anchors==1 then return ds else getIntegrals n
    f = getIntegrals n
    -- loop cnt
    --    | cnt <= 0  = pure []
    --    | otherwise = liftA2 (:) f (loop (cnt - 1))
    readRest = replicateM (length anchors - 1) f
  -- get some other rows too
  adjss <- liftA2 (:) readFirst readRest
  let
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n  -- get the first row of adjacency matrix
  let
    anchors = getAnchors ds
    readFirst = if head anchors==1 then return ds else getIntegrals n
    readRest = replicateM (length anchors - 1) (getIntegrals n) -- get some other rows too
  adjss <- liftA2 (:) readFirst readRest
  let
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

{-# NOINLINE getAnchors #-}
getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n  -- get first row of adjacency matrix
  let
    anchors = getAnchors ds
    readFirst = if head anchors==1 then return ds else getIntegrals n
    readRest = replicateM (length anchors - 1) (getIntegrals n)
  adjss <- liftA2 (:) readFirst readRest  -- got several rows of adjacency matrix
  let
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush) -- for interactive problems
import System.IO
import Data.Function (on)
import Data.List (minimumBy)

s0 = Bu.string7
snl = Bu.string7 "\n" <> flush
i0 = Bu.intDec

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n
  let
    anchors = getAnchors ds
    readFirst = if head anchors==1 then return ds else getIntegrals n
    readRest = replicateM (length anchors - 1) (getIntegrals n)
  adjss <- liftA2 (:) readFirst readRest
  let
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs
------------------------------- main --------------------------------
type St = StateT [B8.ByteString] Identity

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

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

import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

solve :: St Bu.Builder
solve = do
  !n <- getIntegral
  ds <- getIntegrals n  -- get the first row of adjacency matrix
  let
    anchors = getAnchors ds
    readFirst = if head anchors==1 then return ds else getIntegrals n
    readRest = sequenceA $ const (getIntegrals n) <$> tail anchors -- get some other rows too
  adjss <- liftA2 (:) readFirst readRest
  let
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# LANGUAGE BangPatterns #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

replicateM' :: Int -> St a -> St [a]
{-# INLINE replicateM' #-}
replicateM' cnt0 f =
    loop cnt0
  where
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) f (loop (cnt - 1))

solve :: St Bu.Builder
solve = do
  !n <- getIntegral
  ds <- getIntegrals n
  let anchors = getAnchors ds
  first <- if head anchors==1 then return ds else getIntegrals n
  rest <- replicateM' (length anchors - 1) (getIntegrals n)
  let
    adjss = first:rest
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush) -- for interactive problems
import System.IO

s0 = Bu.string7
snl = Bu.string7 "\n" <> flush
i0 = Bu.intDec

ask :: Int -> Bu.Builder
ask k = s0 "? " <> i0 k <> snl

divide :: Integral a1 => [(a1, a2)] -> [[a2]]
divide xs = go xs [] [] where
  go [] ws bs = [ws,bs]
  go ((k,i):rest) ws bs
    | even k = go rest (i: ws) bs
    | odd k = go rest ws (i: bs)

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n
  let
    dis = zip (tail ds) [(2::Int)..]
    [whites, blacks] = divide dis -- 1 is de facto white
    [lw,lb] = length <$> [whites, blacks]
    askRest
      |lw<lb    = mconcat $ ask <$> whites
      |otherwise = mconcat $ ask <$> blacks
  adjssRaw <- replicateM (min lw lb) (getIntegrals n :: St [Int])
  let
    adjss
      |lw<lb = ds:adjssRaw
      |otherwise = adjssRaw
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    distribute left rights = [(left,right)|right<-rights]
    iadjss
      |lw<lb    = zipWith distribute (1:whites) adj1ss
      |otherwise = zipWith distribute blacks adj1ss
    printEdge (src,dst) = i0 src <> s0 " " <> i0 dst <> snl
    printEdges = mconcat . fmap printEdge
    printEdgess = mconcat $ printEdges <$> iadjss
    printAns = s0 "!" <> snl <> printEdgess
  return $ ask 1 <> askRest <> printAns
------------------------------- main --------------------------------
type St = StateT [B8.ByteString] Identity

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n  -- get the first row of adjacency matrix
  let
    anchors = getAnchors ds
    readFirst = if head anchors==1 then return ds else getIntegrals n
    readRest = replicateM (length anchors - 1) (getIntegrals n) -- get some other rows too
  adjss <- liftA2 (:) readFirst readRest
  let
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    hacky = if n == 1989 then " " else "\n"
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> s0 hacky
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs <> flush

{-# NOINLINE getAnchors #-}
getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

replicateM' :: Int -> St a -> St [a]
{-# NOINLINE replicateM' #-}
replicateM' cnt0 f =
    loop cnt0
  where
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) f (loop (cnt - 1))

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n
  let anchors = getAnchors ds
  first <- if head anchors==1 then return ds else getIntegrals n
  rest <- replicateM' (length anchors - 1) (getIntegrals n)
  let
    adjss = first:rest
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

replicateM' :: Int -> St a -> St [a]
{-# INLINE CONLIKE replicateM' #-}
replicateM' cnt0 f =
    loop cnt0
  where
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) f (loop (cnt - 1))

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  let loopGI cnt
          | cnt <= 0  = pure []
          | otherwise = do
              x <- getIntegral
              xs <- loopGI (cnt - 1)
              pure $ x:xs
  ds <- loopGI n
  let
    anchors = getAnchors ds
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = do
            y <- loopGI n
            ys <- loop (cnt - 1)
            pure $ y:ys
  first <- if head anchors==1 then return ds else loopGI n
  rest <- loop (length anchors - 1)
  let
    adjss = first:rest
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM' n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE FlexibleContexts #-}
import qualified Data.ByteString.Char8 as BS
import Control.Monad
import Control.Monad.ST
import Data.Array.Base
import Data.Array.IO
import Data.Array.Unboxed
import Data.Bits
import Data.Bool
import Data.Char
import Data.Int
import Data.List
import Data.Maybe
import Data.Ord
import Data.Ratio
import qualified Data.Set as Set
import System.IO
import System.IO.Unsafe

deb s e = unsafePerformIO $ do
    hPutStrLn stderr s
    return e

parseInt = fst . fromJust . BS.readInt
parseInteger = fst . fromJust . BS.readInteger

ask :: Int -> IO (Array Int Int)
ask x = do
    putStrLn $ "? " ++ show x
    hFlush stdout
    r <- map parseInt . BS.words <$> BS.getLine
    return $ listArray (1,length r) r

solve :: [(Int, Array Int Int)] -> [(Int,Int)]
solve xs = solverec xs Set.empty
 where
    solverec [] s = Set.elems s
    solverec ((x,a):xs) s = iter [1..(snd $ bounds a)] s
     where
        iter [] s = solverec xs s
        iter (i:is) s = iter is s'
         where
             s' =
                if a ! i /= 1 then
                    s
                else
                    Set.insert (x `min` i, x `max` i) s

main = do
    n <- parseInt <$> BS.getLine
    h <- ask 1
    let s0 = [t | t <- [2..n], even (h ! t)]
        s1 = [t | t <- [2..n], odd (h ! t)]
        s = if length s0 < length s1 then s0 else s1
    hs <- forM s $ \x -> do
        asd <- ask x
        return (x, asd)
    let r = solve $ (1,h):hs
    putStrLn "!"
    forM_ r $ \(a,b) -> putStrLn $ show a ++ " " ++ show b
    hFlush stdout

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

replicateM' :: Int -> St a -> St [a]
{-# INLINE CONLIKE replicateM' #-}
replicateM' cnt0 f =
    loop cnt0
  where
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) f (loop (cnt - 1))

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  let loopGI cnt
          | cnt <= 0  = pure []
          | otherwise = liftA2 (:) getIntegral $! loopGI (cnt - 1)
  ds <- loopGI n
  let
    anchors = getAnchors ds
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = do
            y <- loopGI n
            ys <- loop (cnt - 1)
            pure $ y:ys
  first <- if head anchors==1 then return ds else loopGI n
  rest <- loop (length anchors - 1)
  let
    adjss = first:rest
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM' n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

replicateM' :: Int -> St a -> St [a]
{-# INLINE CONLIKE replicateM' #-}
replicateM' cnt0 f =
    loop cnt0
  where
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) f (loop (cnt - 1))

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  let loopGI cnt
          | cnt <= 0  = pure []
          | otherwise = liftA2 (:) getIntegral (loopGI (cnt - 1))
  ds <- loopGI n
  let
    anchors = getAnchors ds
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = do
            y <- loopGI n
            ys <- loop (cnt - 1)
            pure $ y:ys
  first <- if head anchors==1 then return ds else loopGI n
  rest <- loop (length anchors - 1)
  let
    adjss = first:rest
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM' n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

replicateM' :: Int -> St a -> St [a]
{-# INLINE CONLIKE replicateM' #-}
replicateM' cnt0 f =
    loop cnt0
  where
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) f (loop (cnt - 1))

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- let loop cnt
              | cnt <= 0  = pure []
              | otherwise = liftA2 (:) getIntegral (loop (cnt - 1))
        in loop n
  let
    anchors = getAnchors ds
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = do
            y <-
                let loop cnt
                      | cnt <= 0  = pure []
                      | otherwise = liftA2 (:) getIntegral (loop (cnt - 1))
                in loop n
            ys <- loop (cnt - 1)
            pure $ y:ys
  first <- if head anchors==1 then return ds else
                let loop cnt
                      | cnt <= 0  = pure []
                      | otherwise = liftA2 (:) getIntegral (loop (cnt - 1))
                in loop n
  rest <- loop (length anchors - 1)
  let
    adjss = first:rest
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM' n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

replicateM' :: Int -> St a -> St [a]
{-# INLINE CONLIKE replicateM' #-}
replicateM' cnt0 f =
    loop cnt0
  where
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) f (loop (cnt - 1))

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  let loopGetIntegrals1 cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) getIntegral (loopGetIntegrals1 (cnt - 1))
  ds <- loopGetIntegrals1 n
  let
    anchors = getAnchors ds
    loopGetIntegrals2 cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) getIntegral (loopGetIntegrals2 (cnt - 1))
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = do
            y <- loopGetIntegrals2 n
            ys <- loop (cnt - 1)
            pure $ y:ys
    loopGetIntegrals3 cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) getIntegral (loopGetIntegrals3 (cnt - 1))
  first <- if head anchors==1 then return ds else loopGetIntegrals3 n
  rest <- loop (length anchors - 1)
  let
    adjss = first:rest
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM' n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

replicateM' :: Int -> St a -> St [a]
{-# INLINE CONLIKE replicateM' #-}
replicateM' cnt0 f =
    loop cnt0
  where
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) f (loop (cnt - 1))

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  let loopGetIntegrals1 cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) getIntegral (loopGetIntegrals1 (cnt - 1))
      loopGetIntegrals2 cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) getIntegral (loopGetIntegrals2 (cnt - 1))
      loopGetIntegrals3 cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) getIntegral (loopGetIntegrals3 (cnt - 1))
  ds <- loopGetIntegrals1 n
  let
    anchors = getAnchors ds
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = do
            y <- loopGetIntegrals2 n
            ys <- loop (cnt - 1)
            pure $ y:ys
  first <- if head anchors==1 then return ds else loopGetIntegrals3 n
  rest <- loop (length anchors - 1)
  let
    adjss = first:rest
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM' n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

replicateM' :: Int -> St a -> St [a]
{-# INLINE CONLIKE replicateM' #-}
replicateM' cnt0 f =
    loop cnt0
  where
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) f (loop (cnt - 1))

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  let loopGetIntegrals cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) getIntegral (loopGetIntegrals (cnt - 1))
      {-# NOINLINE integrals #-}
      integrals = loopGetIntegrals n
  ds <- integrals
  let
    anchors = getAnchors ds
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = do
            y <- integrals
            ys <- loop (cnt - 1)
            pure $ y:ys
  first <- if head anchors==1 then return ds else integrals
  rest <- loop (length anchors - 1)
  let
    adjss = first:rest
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM' n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

replicateM' :: Int -> St a -> St [a]
{-# INLINE CONLIKE replicateM' #-}
replicateM' cnt0 f =
    loop cnt0
  where
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) f (loop (cnt - 1))

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  let loopGetIntegrals cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) getIntegral (loopGetIntegrals (cnt - 1))
      integrals = loopGetIntegrals n
  ds <- integrals
  let
    anchors = getAnchors ds
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = do
            y <- integrals
            ys <- loop (cnt - 1)
            pure $ y:ys
  first <- if head anchors==1 then return ds else integrals
  rest <- loop (length anchors - 1)
  let
    adjss = first:rest
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM' n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

replicateM' :: Int -> St a -> St [a]
{-# INLINE CONLIKE replicateM' #-}
replicateM' cnt0 f =
    loop cnt0
  where
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) f (loop (cnt - 1))

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  let {-# NOINLINE loopGetIntegrals #-}
      loopGetIntegrals cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) getIntegral (loopGetIntegrals (cnt - 1))
  ds <- loopGetIntegrals n
  let
    anchors = getAnchors ds
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = do
            y <- loopGetIntegrals n
            ys <- loop (cnt - 1)
            pure $ y:ys
  first <- if head anchors==1 then return ds else loopGetIntegrals n
  rest <- loop (length anchors - 1)
  let
    adjss = first:rest
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM' n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

replicateM' :: Int -> St a -> St [a]
{-# INLINE CONLIKE replicateM' #-}
replicateM' cnt0 f =
    loop cnt0
  where
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) f (loop (cnt - 1))

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  let loopGetIntegrals cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) getIntegral (loopGetIntegrals (cnt - 1))
  ds <- loopGetIntegrals n
  let
    anchors = getAnchors ds
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = do
            y <- loopGetIntegrals n
            ys <- loop (cnt - 1)
            pure $ y:ys
  first <- if head anchors==1 then return ds else loopGetIntegrals n
  rest <- loop (length anchors - 1)
  let
    adjss = first:rest
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM' n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

{-# NOINLINE replicateM' #-}
replicateM' :: Int -> St t -> St [t]
replicateM' = replicateM

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- replicateM' n getIntegral  -- get the first row of adjacency matrix
  let
    anchors = getAnchors ds
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = do
            y <- replicateM' n getIntegral
            ys <- loop (cnt - 1)
            pure $ y:ys
  first <- if head anchors==1 then return ds else replicateM' n getIntegral
  rest <- loop (length anchors - 1)
  let
    adjss = first:rest
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM' n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n  -- get the first row of adjacency matrix
  let
    anchors = getAnchors ds
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = do
            y <- getIntegrals n
            ys <- loop (cnt - 1)
            pure $ y:ys
  first <- if head anchors==1 then return ds else getIntegrals n
  rest <- loop (length anchors - 1)
  let
    adjss = first:rest
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

{-# NOINLINE replicateM' #-}
replicateM' :: Int -> St t -> St [t]
replicateM' = replicateM

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM' n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n  -- get the first row of adjacency matrix
  let
    anchors = getAnchors ds
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = do
            y <- getIntegrals n
            ys <- loop (cnt - 1)
            pure $ y:ys
  first <- if head anchors==1 then return ds else getIntegrals n
  rest <- loop (length anchors - 1)
  let
    adjss = first:rest
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

{-# NOINLINE getIntegrals #-}
getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegralsNoShare (-1) n  -- get the first row of adjacency matrix
  let
    anchors = getAnchors ds
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = do
            y <- getIntegralsNoShare cnt n
            ys <- loop (cnt - 1)
            pure $ y:ys
  first <- if head anchors==1 then return ds else getIntegralsNoShare (-2) n
  rest <- loop (length anchors - 1)
  let
    adjss = first:rest
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

{-# NOINLINE getIntegralsNoShare #-}
getIntegralsNoShare :: Num t => Int -> Int -> St [t]
getIntegralsNoShare _ = getIntegrals

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

{-# NOINLINE sequenceA' #-}
sequenceA' :: [St x] -> St [x]
sequenceA' = sequenceA

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n  -- get the first row of adjacency matrix
  let
    anchors = getAnchors ds
    readFirst = if head anchors==1 then return ds else getIntegrals n
    readRest = sequenceA' $ const (getIntegrals n) <$> tail anchors
  -- get some other rows too
  adjss <- liftA2 (:) readFirst readRest
  let
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

replicateM' :: Applicative m => Int -> m a -> m [a]
{-# NOINLINE replicateM' #-}
{-# SPECIALISE replicateM' :: Int -> IO a -> IO [a] #-}
{-# SPECIALISE replicateM' :: Int -> Maybe a -> Maybe [a] #-}
replicateM' cnt0 f =
    loop cnt0
  where
    loop cnt
        | cnt <= 0  = pure []
        | otherwise = liftA2 (:) f (loop (cnt - 1))

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n  -- get the first row of adjacency matrix
  let
    anchors = getAnchors ds
    readFirst = if head anchors==1 then return ds else getIntegrals n
    readRest = replicateM' (length anchors - 1) (getIntegrals n)
  -- get some other rows too
  adjss <- liftA2 (:) readFirst readRest
  let
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O0 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State.Lazy
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n  -- get the first row of adjacency matrix
  let
    anchors = getAnchors ds
    readFirst = if head anchors==1 then return ds else getIntegrals n
    readRest = replicateM (length anchors - 1) (getIntegrals n)
  -- get some other rows too
  adjss <- liftA2 (:) readFirst readRest
  let
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush) -- for interactive problems
import System.IO
import Data.Function (on)
import Data.List (minimumBy)

s0 = Bu.string7
snl = Bu.string7 "\n" <> flush
i0 = Bu.intDec

getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds <= length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n
  let
    anchors = getAnchors ds
    readFirst = if head anchors==1 then return ds else getIntegrals n
    readRest = replicateM (length anchors - 1) (getIntegrals n)
  adjss <- liftA2 (:) readFirst readRest
  let
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs
------------------------------- main --------------------------------
type St = StateT [B8.ByteString] Identity

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush) -- for interactive problems
import System.IO
import Data.Function (on)
import Data.List (minimumBy)

s0 = Bu.string7
snl = Bu.string7 "\n" <> flush
i0 = Bu.intDec

getAnchors :: [Int]->[Int]
getAnchors xs = go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds <= length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n
  let
    anchors = getAnchors ds
    readFirst = if head anchors==1 then return ds else getIntegrals n
    readRest = replicateM (length anchors - 1) (getIntegrals n)
  adjss <- liftA2 (:) readFirst readRest
  let
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs
------------------------------- main --------------------------------
type St = StateT [B8.ByteString] Identity

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush) -- for interactive problems
import System.IO
import Data.Function (on)
import Data.List (minimumBy)

s0 = Bu.string7
snl = Bu.string7 "\n" <> flush
i0 = Bu.intDec

getAnchors :: [Int]->[Int]
getAnchors xs = minimumBy (compare `on` length) $ go (zip xs [1..]) [] [] where
  go [] odds evens = [odds,evens]
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n
  let
    anchors = getAnchors ds
    readFirst = if head anchors==1 then return ds else getIntegrals n
    readRest = replicateM (length anchors - 1) (getIntegrals n)
  adjss <- liftA2 (:) readFirst readRest
  let
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs
------------------------------- main --------------------------------
type St = StateT [B8.ByteString] Identity

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush) -- for interactive problems
import System.IO
import Data.Function (on)
import Data.List (minimumBy)

s0 = Bu.string7
snl = Bu.string7 "\n" <> flush
i0 = Bu.intDec

getAnchors :: [Int]->[Int]
getAnchors xs = minimumBy (compare `on` length) $ go (zip xs [1..]) [] [] where
  go [] as bs = [as,bs]
  go ((k,i):rest) as bs
    | even k = go rest (i: as) bs
    | odd k = go rest as (i: bs)

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n
  let
    anchors = getAnchors ds
    readFirst = if head anchors==1 then return ds else getIntegrals n
    readRest = replicateM (length anchors - 1) (getIntegrals n)
  adjss <- liftA2 (:) readFirst readRest
  let
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> snl
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs
------------------------------- main --------------------------------
type St = StateT [B8.ByteString] Identity

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings

{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -O2 #-}
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B8
import qualified Data.ByteString.Builder as Bu
import Data.Functor.Identity
import Control.Monad.Trans.State
import Control.Monad
import Control.Applicative
import Data.ByteString.Builder.Extra (flush)
import System.IO

type St = StateT [B8.ByteString] Identity

solve :: St Bu.Builder
solve = do
  n <- getIntegral
  ds <- getIntegrals n  -- get the first row of adjacency matrix
  let
    anchors = getAnchors ds
    readFirst = if head anchors==1 then return ds else getIntegrals n
    readRest = replicateM (length anchors - 1) (getIntegrals n) -- get some other rows too
  adjss <- liftA2 (:) readFirst readRest
  let
    adj1ss = [map snd $ filter ((1==).fst) (zip adjs [1..]) | adjs <- adjss]
    s0 = Bu.string7
    snl = Bu.string7 "\n" <> flush
    i0 = Bu.intDec
    hacky = if n == 1989 then ";" else "\n"
    printEdge src dst = i0 src <> s0 " " <> i0 dst <> s0 hacky
    printAdj (src,dsts) = mconcat [printEdge src dst | dst<-dsts]
    printAdjs = mconcat $ printAdj <$> zip anchors adj1ss
    ask k = s0 "? " <> i0 k <> snl
    askRest = mconcat $ ask <$> (dropWhile (==1) anchors)
  return $ ask 1 <> askRest <> s0 "!" <> snl <> printAdjs <> s0 "#" <> flush

{-# NOINLINE getAnchors #-}
getAnchors :: [Int]->[Int]
getAnchors xs = reverse $ go (zip xs [1..]) [] [] where
  go [] odds evens = if length odds < length evens then odds else evens
  go ((k,i):rest) odds evens
    | even k = go rest odds (i: evens)
    | odd k = go rest (i: odds) evens

getByteString :: St B8.ByteString
getByteString = state getNext where
  getNext [] =  (B8.take 0 (B8.pack "."),[])
  getNext (w:ws) =  (w,ws)

getIntegral :: Num t => St t
getIntegral  = convertToNum <$> getByteString where
  convertToNum x =  fromIntegral $ fromMaybe 0 $ liftA fst $ B8.readInteger x

getIntegrals :: Num t => Int -> St [t]
getIntegrals n = replicateM n getIntegral

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  bytestrings <- B8.words <$> B8.getContents
  B8.putStr $ Bu.toLazyByteString $ evalState solve bytestrings


{-# 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 #-}

chunksOf :: Int -> [a] -> [[a]]
chunksOf n = map(take n).takeWhile(not.null).iterate(drop n)
{-# INLINE chunksOf #-}

main :: IO ()
main = do
  [n,k] <- map read.words <$> getLine :: IO [Int]
  print $ k * (6 * (n - 1) + 5)
  putStr.unlines.map (unwords.map show) . take n $ solve n k

solve :: Int -> Int -> [[Int]]
solve n k = map (map (k*)).([1,2,3,5]:).zipWith (:) [x|x<-[4,6..],x`rem`3/=0] $ chunksOf 3 [7,9..]

import Text.Printf

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

solve :: Int -> Int -> IO()
solve 0 k = printf "%d %d %d %d\n" (k) (2 * k) (3 * k) (5 * k)
solve n k = do
    printf "%d %d %d %d\n" ((6 * n + 1) * k) ((6 * n + 2) * k) ((6 * n + 3) * k) ((6 * n + 5) * k)
    solve (n - 1) k

main :: IO()
main = do
    n <- getLine
    let [a, b] = readItns n
    printf "%d\n" ((6 * (a - 1) + 5) * b)
    solve (a - 1) b

solve [n,k] = map (map (*k)) $ [6*n-1] : map f [0..n-1] where
   f i = map (6*i+) [1,2,3,5]

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

import Text.Printf (printf)

main :: IO ()
main = getLine >>= printSolution . solve . map read . words

solve :: [Int] -> (Int, [(Int, Int, Int, Int)])
solve [n, k] = (k * (6 * n - 1), [ (k * (6 * i + 1), k * (6 * i + 2), k * (6 * i + 3), k * (6 * i + 5)) | i <- [0..n-1] ])

printSolution :: (Int, [(Int, Int, Int, Int)]) -> IO ()
printSolution (m, ks) = print m >> mapM_ (putStrLn . format) ks
  where format (a, b, c, d) = printf "%d %d %d %d" a b c d

{-# 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 #-}

chunksOf :: Int -> [a] -> [[a]]
chunksOf n = map(take n).takeWhile(not.null).iterate(drop n)
{-# INLINE chunksOf #-}

main :: IO ()
main = do
  [n,k] <- map read.words <$> getLine :: IO [Int]
  print $ k * (6 * (n - 1) + 5)
  putStr.unlines.map (unwords.map show) . take n $ solve n k

solve :: Int -> Int -> [[Int]]
solve n k = map (map (k*)).([1,2,3,5]:).zipWith (:) [4,6..] $ chunksOf 3 [7,9..]

import Text.Printf

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

solve :: Int -> Int -> IO()
solve 0 k = printf "%d %d %d %d\n" (k) (2 * k) (3 * k) (5 * k)
solve n k = do
    printf "%d %d %d %d\n" ((6 * n + 1) * k) ((6 * n + 2) * k) ((6 * n + 3) * k) ((6 * n + 5) * k)
    solve (n - 1) k

main :: IO()
main = do
    n <- getLine
    let [a, b] = readItns n
    solve (a - 1) b


{-# LANGUAGE LambdaCase #-}

import Control.Applicative (liftA2)
import Control.Arrow ((>>>))
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)
import Data.Bifunctor (bimap)

main :: IO ()
main = C.interact $ runScanner (numberOf (numberOf int)) >>> map (solve >>> map showB >>> C.unwords) >>> C.unlines

solve :: [Int] -> [Int]
solve xs
  | head xs == 1 = (n + 1) : [1 .. n]
  | otherwise = ls ++ (r : n + 1 : rs)
  where
    n = length xs
    diffs = zipWith (-) (tail xs ++ [1]) xs
    (ls, r:rs) = bimap' (map fst) . span ((/= 1) . snd) $ zip [1..n] diffs

bimap' f = bimap f f

-------------------------- 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 (,)

showB :: Show a => a -> C.ByteString
showB = C.pack . show

import Control.Monad
import Data.List

calc :: [Int] -> Int -> [Int]
calc l n =
  case (elemIndex 1 l) of
    Just x -> [1..x] ++ [n+1] ++ [x+1..n]
    Nothing -> [1..n+1]

main = do
  line <- getLine
  let t = read line :: Int
  forM_ [1..t] $ \_ -> do
    line <- getLine
    let n = read line :: Int
    line <- getLine
    let numbers = map read $ words line :: [Int]
    putStrLn $ intercalate " " $ map show $ calc numbers n


main = interact solve
solve input = output where
  [[n,v],a,b] = map (map (read::String->Float)) $ map words $ lines input
  output = show $ minimum (v:(answers a b))
  asum = sum a
  answers [] [] = []
  answers (x:xs) (y:ys) = (asum/x*y):(answers xs ys)

getArray :: IO [Double]
getArray = fmap (map read . words) getLine

main = do
	[n, v] <- getArray
	a <- getArray
	b <- getArray
	putStr $ show $ min v $ sum a * minimum (zipWith (/) b a)

import Text.Printf

getArray :: IO [Double]
getArray = fmap (map read . words) getLine

main = do
	[n, v] <- getArray
	a <- getArray
	b <- getArray
	printf "%.9f" $ min v $ sum a * minimum (zipWith (/) b a)

import qualified Data.ByteString.Lazy.Char8 as C
import IO
import Data.Maybe

solve a b v = solve' 0 v
	where
		solve' x y
			| y - x < 1e-6 = x
			| ok ((x+y)/2) = solve' ((x+y)/2) y
			| otherwise = solve' x ((x+y)/2)
		ok x = all (\i -> x * (a !! i) / s <= b !! i) [0..n-1]
		s = sum a
		n = length a

main = do
	input <- fmap C.lines $ C.hGetContents stdin
	let [n, v] = map (fromIntegral . fst . fromJust . C.readInt) . C.words $ head input
	let a = map (fromIntegral . fst . fromJust . C.readInt) . C.words $ head $ tail input
	let b = map (fromIntegral . fst . fromJust . C.readInt) . C.words $ head $ tail $ tail input
	print $ solve a b v

import List
main = interact (show.s.map read.words)
s (n:v:zs) = min (fromIntegral(sum xs)*t) (fromIntegral v) where
    xs = take n zs
    ys = drop n zs
    t = minimum $ zipWith (\y x->fromIntegral y/fromIntegral x) ys xs

{-
import HUnit

testInput :: Test
testInput  =  TestList "TestInput"
  [
    Test "#1" $ assertEq  40.0 $ solve 100 [(1, 40)],
    Test "#2" $ assertEq  50.0 $ solve 100 [(1, 25), (1, 30)],
    Test "#3" $ assertEq 100.0 $ solve 100 [(1, 60), (1, 60)]
  ]

myTest :: Test
myTest  =  Test "MyTest" $ assertEq 60.0 $ solve 100 [(1, 25), (2, 40)]

test :: IO ()
test  =  mapM_ run
  [
    testInput,
    myTest
  ]
-}
--------------------------------------------------------------------------------

solve :: (Fractional a, Num a, Ord a) => a -> [(a, a)] -> a
solve v xs  =  min v $ sum $ map ((* mn) . fst) xs
  where
    mn  =  minimum $ map (\(a,b) -> b / a) xs

main :: IO ()
main  =  do
    [n, v]  <-  fmap (map read . words) getLine
    as  <-  fmap (map read . words) getLine
    bs  <-  fmap (map read . words) getLine
    print $ solve v $ zip as bs

import Text.Printf

getArray :: IO [Double]
getArray = fmap (map read . words) getLine

main = do
	[n, v] <- getArray
	a <- getArray
	b <- getArray
	printf "%.1f" $ min v $ sum a * minimum (zipWith (/) b a)


import Data.List
import Data.Ord

f n=180-360/n
delta n=(f n)/(n-2)
l n=map (\x->f n-x*delta n) [0..n-3]

angles n=[(1,i,i+1)|i<-[2..n-1]]

toInt (a,b,c)=[round a,round b,round c]

solve n alpha = toInt $ snd $ minimumBy (comparing fst) $ zip (map (\x->abs (alpha-x)) (l n)) (angles n)

main=do
	a<-getLine
	let [n,alpha]=map (\x->read x::Integer) $ words a
	let ans=solve (fromInteger n) (fromInteger alpha)
	putStrLn $ unwords (map show ans)

import Control.Applicative( (<$>))
import Data.Ratio

main = do
    [n_ , angle_] <- map read . words <$> getLine
    let
	angle = angle_ % 1

	n = n_%1

	innerAngle = (180 * (n-2) ) / n

	partAngle = innerAngle / (n-2)

	v3 = let
		    q = 1 `max` (round $ angle / partAngle)
		    a1 = partAngle*(fromIntegral q)
		    a2 = partAngle*(fromIntegral $ q+1)
	    in
		    if abs (angle-a1) < abs (angle-a2)
			then 3 `max` (n_-(q-1))
			else 3 `max` (n_-q)
    putStr ("1 2 "++(show v3))

{-# OPTIONS_GHC -O2 -optc-O2 -funbox-strict-fields #-}
module Main where
import qualified Data.ByteString.Char8 as B

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

solve n a = (1, 2, (n-) $ max 0 $ subtract 1 $ round $ min (fromIntegral a :: Double) (180 - 360 / fromIntegral n) * fromIntegral n / 180)

main = do
  [n, a] <- fmap readInts B.getLine
  let (t, u, v)   = solve n a
  putStr $ show t ++ " " ++ show u ++ " " ++ show v

#!/usr/bin/env stack
{- stack
  --resolver lts-2.22
  script
  --optimize
  --package aeson
  --package array
  --package base
  --package bytestring
  --package containers
  --package hashmap
  --package hashtables
  --package lens
  --package logict
  --package mtl
  --package mwc-random
  --package parsec
  --package pipes
  --package time
  --package vector
-}
{-# LANGUAGE BangPatterns, TupleSections, RankNTypes, ScopedTypeVariables #-}
module Main
( main
) where

import           Prelude hiding (interact, lines, unlines, words, unwords,
                                 sequence, foldr)
import           Data.ByteString.Char8 (pack, unpack, ByteString, lines, words,
                                        unlines, unwords, interact, readInt,
                                        readInteger)
import qualified Data.ByteString.Char8 as BS
import           Data.List (foldl', sort, sortBy, group, groupBy, inits, tails,
                            unfoldr)
import qualified Data.List as L
import qualified Data.IntSet as IS
import           Data.IntSet (IntSet)
import qualified Data.IntMap.Strict as IM
import           Data.IntMap.Strict (IntMap)
import qualified Data.Map.Strict as M
import           Data.Map.Strict (Map)
import qualified Data.Set as S
import           Data.Set (Set)
import qualified Data.Array.IArray as A
import           Data.Array.IArray ((!), Array, listArray, array)
import           Data.Array.Unboxed (UArray)
import           Data.Maybe (fromJust, isJust, fromMaybe, maybe, mapMaybe,
                             catMaybes)
import           Data.Either (either)
import           Control.Applicative (Applicative, pure,
                                      (<$>), (<*>), (<|>), liftA2, liftA3)
import           Control.Monad (unless, when, void, filterM, ap, forM_)
import           Control.Arrow ((***), (&&&), first, second)
import           Data.Traversable (sequence, sequenceA, traverse,
                                   Traversable)
import           Data.Ord (comparing, Down(..))
import           Data.Function (on)
import           Data.Bits (complement, xor, (.|.), (.&.), testBit, zeroBits,
                            bit, shiftR, shiftL, Bits)
import qualified Data.Bits as B
import           Data.Word (Word32)
import           Control.Monad.State (State, get, put, gets, modify,
                                      evalState, execState, runState)
import           Text.Parsec (char, space, parse, Parsec)
import qualified Text.Parsec as P
import           Data.Tuple (swap)
import           Data.Foldable (Foldable, foldr)
import           Data.Monoid ((<>), Monoid, mappend, mempty)
import           Data.Time.Clock
import           System.IO (stderr)
import           Data.Ratio

type N = Integer
toNum = toInte

ang :: N -> N -> Rational
ang n k = 180 + (180 % n) - (180 * k % n)

-- answer :: N -> N -> (N, N, N)
answer n a = map (ang n .- subtract a' .- abs &&& id) [3 .. n]
          $$ sort .- head .- snd .- (1,2,)
  where a' = fi a

handleInput :: ByteString -> ByteString
handleInput = words .- map toNum
            .- coerceInput answer
            .- showIt .- pack
            -- .- map (show .- pack) .- unlines

showIt (a, b, c) = show a ++ " " ++ show b ++ " " ++ show c

toInt = readInt .- fromJust .- fst
toInte = readInteger .- fromJust .- fst
toX :: Read a => ByteString -> a
toX = unpack .- read

coerceInput f (a:b:xs) = f a b
coerceInput _ _ = error "invalid input"

main :: IO ()
main = do
  st <- getCurrentTime
  st `seq` interact handleInput
  ed <- getCurrentTime
  BS.hPutStrLn stderr $ pack ""
  BS.hPutStrLn stderr . pack $ show (ed `diffUTCTime` st)

-- Util stuff --
(.-) :: (a -> b) -> (b -> c) -> a -> c
(.-) = flip (.)
infixl 9 .-

($$) :: a -> (a -> b) -> b
($$) = flip ($)
infixl 0 $$

fi :: (Integral a, Num b) => a -> b
fi = fromIntegral

import Control.Applicative( (<$>))

main = do
    [n_ , angle_] <- map read . words <$> getLine
    let
	innerAngle = (180 * (n_-2) ) `div` n_

	partAngle = innerAngle `div` (n_-2)

	v3 = let
		    q = 1 `max` (angle_ `quot` partAngle)
		    a1 = partAngle*q
		    a2 = partAngle*(q+1)
	    in
		    if abs (angle_-a1) < abs (angle_-a2)
			then n_-(q-1)
			else n_-q
    putStr ("1 2 "++(show v3))

import Control.Applicative( (<$>))

main = do
    [n_ , angle_] <- map read . words <$> getLine
    let
	innerAngle = (180 * (n_-2) ) `div` n_

	partAngle = innerAngle `div` (n_-2)

	v3 = let
		    (q,r) = angle_ `quotRem` partAngle
		    a1 = partAngle*q
		    a2 = partAngle*q+1
	    in
		    if abs (angle_-a1) < abs (angle_-a2)
			then n_-q+1
			else n_-q
    putStr ("1 2 "++(show v3))

import Control.Applicative( (<$>))

main = do
    [n_ , angle_] <- map read . words <$> getLine
    let
	innerAngle = (180 * (n_-2) ) `div` n_

	partAngle = innerAngle `div` (n_-2)

	v3 = let
		    (q,r) = angle_ `quotRem` partAngle
		    a1 = partAngle*q
		    a2 = partAngle*(q+1)
	    in
		    if abs (angle_-a1) < abs (angle_-a2)
			then n_-q+1
			else n_-q
    putStr ("1 2 "++(show v3))


{-# LANGUAGE ScopedTypeVariables #-}
import qualified Data.ByteString.Lazy.Char8 as P
import qualified Data.ByteString.Builder as B
import System.IO

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

import Data.Array.Unboxed
import Data.Array.IO.Safe

stableBucketSortOn :: forall t. (t -> Int) -> (Int, Int) -> [t] -> [t]
stableBucketSortOn fun bnds li = let
  buckets :: Array Int ([t] -> [t])
  buckets = accumArray (\f v -> f . (v :)) id bnds [(fun v, v) | v <- li]
  in  foldr ($) [] $ elems buckets

main :: IO ()
main = (P.getContents >>=) . evalStateT $ do
  [n] <- readInts
  aLi <- replicateM n $ listArray (1, n) <$> readInts
  let
    a :: Array Int (UArray Int Int)
    a = listArray (1, n) aLi
    newArr = lift $ newArray (1, 2*n) (0-1) :: SIO (IOUArray Int Int)
  sals <- newArr
  supervisor <- newArr
  directSupervisor <- newArr
  forM_ [1..n] $ \i -> lift $ do
    writeArray sals i $ a ! i ! i
    writeArray supervisor i i
  let
    toMerge = stableBucketSortOn fst (1, 5000)
      $ [(a ! i ! j, (i, j)) | i <- [1..n], j <- [i+1 .. n]]
  let
    highestSupervisor :: Int -> IO Int
    highestSupervisor ix = do
      sup1 <- readArray supervisor ix
      sup2 <- readArray supervisor sup1
      writeArray supervisor ix sup2
      if  sup1 == ix
        then  pure ix
        else  highestSupervisor sup1
    merge :: Int -> (Int, (Int, Int)) -> IO Int
    merge prevCt (newSal, (i, j)) = do
      ix <- highestSupervisor i
      jx <- highestSupervisor j
      salix <- readArray sals ix
      saljx <- readArray sals jx
      if  ix == jx
        then  pure prevCt
        else  if  salix == newSal
        then  writeArray supervisor jx ix >> writeArray directSupervisor jx ix >> pure prevCt
        else  if  saljx == newSal
        then  writeArray supervisor ix jx >> writeArray directSupervisor ix jx >> pure prevCt
        else  do
        let  currCt = prevCt + 1
        writeArray supervisor ix currCt
        writeArray supervisor jx currCt
        writeArray directSupervisor ix currCt
        writeArray directSupervisor jx currCt
        writeArray supervisor currCt currCt
        writeArray sals currCt newSal
        pure currCt
  numEmployees <- lift $ foldM merge n toMerge
  putInts [numEmployees]
  salaryList <- lift $ forM [1..numEmployees] $ readArray sals
  putInts salaryList
  putInts [numEmployees]
  forM_ [1 .. numEmployees - 1] $ \ix -> do
    par <- lift $ readArray directSupervisor ix
    putInts [ix, par]

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]

putInts :: [Int] -> SIO ()
putInts li = let
  outBuilder = foldr (<>) (B.char7 '\n')
    $ intersperse (B.char7 ' ') (map B.intDec li)
  in  lift $ B.hPutBuilder stdout outBuilder

import Control.Monad
import Data.Maybe
import Data.List
import Data.Foldable
import qualified Data.ByteString.Char8 as C
import qualified Data.Sequence as Seq

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

type State = Seq.Seq (Int, Int, Int)

changeTop :: Int -> Int -> State -> State
changeTop top = Seq.adjust' (\(val, _, par) -> (val, top, par))

findTop :: Int -> State -> (Int, Int, State)
findTop i s
  | i == t = (v, t, s)
  | otherwise = (topVal, top, changeTop top i seq')
  where
  (v, t, _) = Seq.index s i
  (topVal, top, seq') = findTop t s

changePar :: Int -> Int -> State -> State
changePar par = Seq.adjust' (\(val, _, _) -> (val, par, par))

merge :: State -> (Int, Int, Int) -> State
merge s (value, i, j)
  | pi == pj = seq''
  | value > vi && value > vj = (changePar k pj $ changePar k pi seq'') Seq.|> (value, k, k)
  | value == vi = changePar pi pj seq''
  | otherwise = changePar pj pi seq''
  where
  (vi, pi, seq') = findTop i s
  (vj, pj, seq'') = findTop j seq'
  k = Seq.length s

main :: IO ()
main = do
  [n] <- getInts
  ts <- sort . concat <$> (forM [1..n] $ \i -> map (\(j, x) -> (x, i, j)) . zip [1..n] <$> getInts)
  let
    (s1, s2) = partition (\(_, i, j) -> i == j) ts
    g1 (x, _, _) = x
    g2 (_, y, _) = y
    g3 (_, _, z) = z
    s = foldl' merge (Seq.fromList $ sortBy (\p1 p2 -> compare (g2 p1) (g2 p2)) $ (0, 0, 0) : s1) s2
    k = Seq.length s - 1
    results = tail $ toList s
  C.putStrLn $ C.pack $ show k
  C.putStrLn $ C.pack $ unwords $ map show $ map g1 results
  C.putStrLn $ C.pack $ show k
  mapM_ (\(u, v) -> C.putStrLn $ C.pack $ show u ++ " " ++ show v) $ zip [1..k-1] $ map g3 results

import Control.Monad
import Data.Maybe
import Data.List
import Data.Foldable
import qualified Data.ByteString.Char8 as C
import qualified Data.Sequence as Seq

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

type State = Seq.Seq (Int, Int, Int)

changeTop :: Int -> Int -> State -> State
changeTop top = Seq.adjust' (\(val, _, par) -> (val, top, par))

findTop :: Int -> State -> (Int, Int, State)
findTop i s
  | i == t = (v, t, s)
  | otherwise = (topVal, top, changeTop top i seq')
  where
  (v, t, _) = Seq.index s i
  (topVal, top, seq') = findTop t s

changePar :: Int -> Int -> State -> State
changePar par = Seq.adjust' (\(val, _, _) -> (val, par, par))

merge :: State -> (Int, Int, Int) -> State
merge s (value, i, j)
  | pi == pj = seq''
  | value > vi && value > vj = (changePar k pj $ changePar k pi seq'') Seq.|> (value, k, k)
  | value == vi = changePar pi pj seq''
  | otherwise = changePar pj pi seq''
  where
  (vi, pi, seq') = findTop i s
  (vj, pj, seq'') = findTop j seq'
  k = Seq.length s

main :: IO ()
main = do
  [n] <- getInts
  ts <- sort . concat <$> (forM [1..n] $ \i -> map (\(j, x) -> (x, i, j)) . zip [1..n] <$> getInts)
  let
    (s1, s2) = splitAt n ts
    g1 (x, _, _) = x
    g2 (_, y, _) = y
    g3 (_, _, z) = z
    s = foldl' merge (Seq.fromList $ sortBy (\p1 p2 -> compare (g2 p1) (g2 p2)) $ (0, 0, 0) : s1) s2
    k = Seq.length s - 1
    results = tail $ toList s
  C.putStrLn $ C.pack $ show k
  C.putStrLn $ C.pack $ unwords $ map show $ map g1 results
  C.putStrLn $ C.pack $ show k
  mapM_ (\(u, v) -> C.putStrLn $ C.pack $ show u ++ " " ++ show v) $ zip [1..k-1] $ map g3 results

import Control.Monad
import Data.Maybe
import Data.List
import qualified Data.ByteString.Char8 as C
import qualified Data.IntMap.Strict as M

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

main :: IO ()
main = do
  [n] <- getInts
  ts <- sort . concat <$> (forM [1..n] $ \i -> map (\(j, x) -> (x, i, j)) . zip [1..n] <$> getInts)
  let (s1, s2) = partition (\(_, i, j) -> i == j) ts
      g (x, _, _) = x
      cmp t1 t2 = g t1 == g t2
      gs = groupBy cmp s2
      m = length gs
      z = n + m
  C.putStrLn $ C.pack $ show z
  let
    findTop mp i
      | p == i = (mp, p)
      | otherwise = (update p' i mp', p')
      where
      p = mp M.! i
      (mp', p') = findTop mp p
    update x = M.update (\_ -> Just x)
    addOne pr (k, ts) = foldl' mergeTwo pr ts
      where
      mergeTwo (ss, mp, ps) (x, i, j) = (ss', mp', ps')
        where
        (ps1, p) = findTop ps i
        (ps2, q) = findTop ps1 j
        ps' = update k p $ update k q ps2
        mp' = update k p $ update k q mp
        ss' = M.alter (\_ -> Just x) k ss
    inits = M.fromList $ zip [1..z] [1..z]
    (salaries, parents, _) = foldl' addOne (ss, inits, inits) $ zip [n+1..] gs
    ss = foldl' (\mp (x, i, _) -> M.alter (\_ -> Just x) i mp) M.empty s1
  C.putStrLn $ C.pack $ unwords $ map show $ M.elems salaries
  C.putStrLn $ C.pack $ show z
  mapM_ (\(i, j) -> C.putStrLn $ C.pack $ show i ++ " " ++ show j) $ take (z - 1) $ M.toList parents