module Main where
import System.IO
import Data.Maybe
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A
import qualified Data.ByteString.Char8 as T

toHoleArray :: Int -> [Int] -> A.UArray Int Bool
toHoleArray size holeList = A.accumArray (||) False (1,size) $ map (\i -> (i, True)) holeList

solve :: Int -> [Int] -> [Int] -> Int
solve nCups holeList opData = if hasHole 1 then 1 else swap 1 opData where
  holes = S.fromList holeList -- toHoleArray nCups holeList
  hasHole :: Int -> Bool
  hasHole i = i `S.member` holes
  swap boneIndex [] = boneIndex
  swap boneIndex (from:to:rest) =
    if from == boneIndex then
      if hasHole to then to else swap to rest
    else if to == boneIndex then
      if hasHole from then from else swap from rest
    else swap boneIndex rest

tests = and [
   checkEqual 1 $ solve 7 [3,4,6] [1,2, 2,5, 5,7, 7,1],
   checkEqual 2 $ solve 5 [2] [1,2, 2,4],
   checkEqual 300001 $ solve 1000000 [] (concat $ map (\i -> [i, 1000001 - i, 1000001 - i, i + 1]) $ enumFromTo 1 (300000))
            ] where
  checkEqual i1 i2 = if i1 /= i2 then error ("Expected " ++ show i1 ++ ", was " ++ show i2) else True

main :: IO ()
main = do
  contents <- T.hGetContents stdin
--  input <- openFile "input.txt" ReadMode
--  contents <- T.hGetContents input
  let (nCups:mHoles:kOps:rest) = map (fst . fromJust . T.readInt) $ T.words contents
      (holeList, opData) = splitAt mHoles rest
  putStrLn $ show $ solve nCups holeList opData

import           Control.Monad
import           Data.Array.IArray
import           Data.Array.Unboxed
import qualified Data.ByteString.Char8 as BS
import           Data.List             (foldl', unfoldr)

getInts::IO [Int]
getInts = fmap (unfoldr readOne) BS.getLine where
  readOne s = do
    (x , s') <- BS.readInt s
    return (x, BS.drop 1 s')

getInt2::IO (Int,Int)
getInt2 = do
  line <- BS.getLine
  let Just (x, l') = BS.readInt line
      Just (y, _)  = BS.readInt $ BS.drop 1 l'
  return (x,y)

main :: IO ()
main = do
  [n, _, k] <- getInts
  holes <- getInts
  let holeMap::UArray Int Bool
      holeMap = array (1, n) $ zip holes $ repeat True
      swap pos (i, j) | holeMap ! pos = pos
                      | pos == i = j
                      | pos == j = i
                      | otherwise = pos
      doSwaps pos 0    = return pos
      doSwaps pos x    = do
        p <- getInt2
        doSwaps (swap pos p) (x - 1)
  pos <- doSwaps 1 k
  print pos

import           Control.Applicative
import           Control.Monad
import           Data.Array.IArray
import           Data.Array.Unboxed
import qualified Data.ByteString.Char8 as BS
import           Data.List             (foldl', unfoldr)

getInts::IO [Int]
getInts = go <$> BS.getLine where
  go s = case  BS.readInt s of
      Nothing      -> []
      Just (x, s') -> x : go (BS.drop 1 s')

getInt2::IO (Int,Int)
getInt2 = do
  line <- BS.getLine
  let Just (x, l') = BS.readInt line
      Just (y, _)  = BS.readInt $ BS.drop 1 l'
  return (x,y)

main :: IO ()
main = do
  [n, _, k] <- getInts
  holes <- getInts
  let holeMap::UArray Int Bool
      holeMap = array (1, n) $ zip holes $ repeat True
      swap pos (i, j) | holeMap ! pos = pos
                      | pos == i = j
                      | pos == j = i
                      | otherwise = pos
      doSwaps pos 0    = return pos
      doSwaps pos x    = do
        p <- getInt2
        doSwaps (swap pos p) (x - 1)
  pos <- doSwaps 1 k
  print pos

import           Control.Applicative
import           Data.Array.IArray
import           Data.Array.Unboxed
import qualified Data.ByteString.Char8 as BS

getInts::IO [Int]
getInts = go <$> BS.getLine where
  go s = case  BS.readInt s of
      Nothing      -> []
      Just (x, s') -> x : go (BS.drop 1 s')

getInt2::IO (Int,Int)
getInt2 = do
  line <- BS.getLine
  let Just (x, l') = BS.readInt line
      Just (y, _)  = BS.readInt $ BS.drop 1 l'
  return (x,y)

main :: IO ()
main = do
  [n, _, k] <- getInts
  holes <- getInts
  let holeMap::UArray Int Bool
      holeMap = array (1, n) $ zip holes $ repeat True
      swap pos [i, j] | holeMap ! pos = pos
                      | pos == i = j
                      | pos == j = i
                      | otherwise = pos
      doSwaps pos 0    = return pos
      doSwaps pos x    = do
        p <- getInts
        doSwaps (swap pos p) (x - 1)
  pos <- doSwaps 1 k
  print pos

import           Control.Monad
import           Data.Array.IArray
import           Data.Array.Unboxed
import qualified Data.ByteString.Char8 as BS
import           Data.List             (foldl', unfoldr)

getInts::IO [Int]
getInts = fmap (unfoldr readOne) BS.getLine where
  readOne s = do
    (x , s') <- BS.readInt s
    return (x, BS.drop 1 s')

getInt2::IO (Int,Int)
getInt2 = do
  line <- BS.getLine
  let Just (x, l') = BS.readInt line
      Just (y, _)  = BS.readInt $ BS.drop 1 l'
  return (x,y)

main :: IO ()
main = do
  [n, m, k] <- getInts
  holes <- getInts
  changes <- replicateM k getInt2

  let holeMap::UArray Int Bool
      holeMap = array (1, n) $ zip holes $ repeat True
      pos = foldl' swap 1 changes
      swap pos (i, j) | holeMap ! pos = pos
                      | pos == i = j
                      | pos == j = i
                      | otherwise = pos
  print pos

import           Control.Applicative
import           Control.Arrow
import           Control.Monad
import           Data.Array.IArray
import           Data.Array.Unboxed
import qualified Data.ByteString.Char8 as BS
import           Data.List             (foldl', unfoldr)

getInts::IO [Int]
getInts = unfoldr (liftA (second $ BS.drop 1). BS.readInt) <$> BS.getLine

getInt2::IO (Int,Int)
getInt2 = do
  line <- BS.getLine
  let Just (x, l') = BS.readInt line
      Just (y, _)  = BS.readInt $ BS.drop 1 l'
  return (x,y)

main :: IO ()
main = do
  [n, _, k] <- getInts
  holes <- getInts
  let holeMap::UArray Int Bool
      holeMap = array (1, n) $ zip holes $ repeat True
      swap pos (i, j) | holeMap ! pos = pos
                      | pos == i = j
                      | pos == j = i
                      | otherwise = pos
      doSwaps pos 0    = return pos
      doSwaps pos x    = do
        p <- getInt2
        doSwaps (swap pos p) (x - 1)
  pos <- doSwaps 1 k
  print pos

import           Control.Monad
import           Data.Array.IArray
import           Data.Array.Unboxed
import qualified Data.ByteString.Char8 as BS
import           Data.List             (foldl', unfoldr)

getInts::IO [Int]
getInts = fmap (unfoldr readOne) BS.getLine where
  readOne s = do
    (x , s') <- BS.readInt s
    return (x, BS.drop 1 s')

getInt2::IO (Int,Int)
getInt2 = do
  line <- BS.getLine
  let Just (x, l') = BS.readInt line
      Just (y, _)  = BS.readInt $ BS.drop 1 l'
  return (x,y)

main :: IO ()
main = do
  [n, _, k] <- getInts
  holes <- getInts
  let holeMap::UArray Int Bool
      holeMap = array (1, n) $ zip holes $ repeat True
      set pos i | pos < 0 = pos
                | holeMap ! i = - i
                | otherwise = i
      swap pos (i, j) | pos == i = set pos j
                      | pos == j = set pos i
                      | otherwise = pos
      doSwaps pos 0    = return $ abs pos
      doSwaps pos x    = do
        p <- getInt2
        doSwaps (swap pos p) (x - 1)
  pos <- doSwaps (set 1 1) k
  print pos

import           Control.Monad
import qualified Data.ByteString.Char8 as BS
import qualified Data.IntSet           as IntSet
import           Data.List             (foldl', unfoldr)

getInts::IO [Int]
getInts = fmap (unfoldr readOne) BS.getLine where
  readOne s = do
    (x , s') <- BS.readInt s
    return (x, BS.drop 1 s')

getInt2::IO (Int,Int)
getInt2 = do
  line <- BS.getLine
  let Just (x, l') = BS.readInt line
      Just (y, _)  = BS.readInt $ BS.drop 1 l'
  return (x,y)

main :: IO ()
main = do
  [n, m, k] <- getInts
  holes <- getInts
  changes <- replicateM k getInt2

  let holeMap = IntSet.fromList holes
      pos = foldl' swap 1 changes
      swap pos (i, j) | IntSet.member pos holeMap = pos
                      | pos == i = j
                      | pos == j = i
                      | otherwise = pos
  print pos

import Control.Applicative( (<$>))
import Data.IntSet( member, empty, insert)
import Control.Monad( replicateM)
import Data.ByteString.Char8( readInt, getLine, words	)
import qualified Data.ByteString.Char8 as CHAR
import Data.Maybe( fromJust)

main = do
    let
	readInt' = fst . fromJust . readInt

    [n_, _m_, k_] <- map readInt' . CHAR.words <$> CHAR.getLine
    hs_ <- CHAR.words <$> CHAR.getLine
    let
	holes = foldl holeEntry empty hs_
	  where
	    holeEntry set bs =  readInt' bs `insert` set

    orders_ <- replicateM k_ CHAR.getLine
    let
	swappingOrders = readPairs <$> orders_
	  where
	    readPairs line = let
			    [a,b] = CHAR.words line
			in
			    (readInt' a, readInt' b)

	swap bone [] = bone
	swap bone ((a,b):pairs) = if a==bone
				    then if b `member` holes
					    then b
					    else swap b pairs
				    else if b==bone
					then if a `member` holes
					    then a
					    else swap a pairs
					else swap bone pairs
    print $ swap 0 ((0,1):swappingOrders)

module Main where
import System.IO
import Data.Maybe
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A
import qualified Data.ByteString.Char8 as T

toHoleArray :: Int -> [Int] -> A.UArray Int Bool
toHoleArray size holeList = A.accumArray (||) False (1,size) $ map (\i -> (i, True)) holeList

solve :: Int -> [Int] -> [Int] -> Int
solve nCups holeList opData = if hasHole 1 then 1 else swap 1 opData where
  holes = toHoleArray nCups holeList
  hasHole :: Int -> Bool
  hasHole i = (A.!) holes i
  swap boneIndex [] = boneIndex
  swap boneIndex (from:to:rest) =
    if from == boneIndex then
      if hasHole to then to else swap to rest
    else if to == boneIndex then
      if hasHole from then from else swap from rest
    else swap boneIndex rest

tests = and [
   checkEqual 1 $ solve 7 [3,4,6] [1,2, 2,5, 5,7, 7,1],
   checkEqual 2 $ solve 5 [2] [1,2, 2,4],
   checkEqual 300001 $ solve 1000000 [] (concat $ map (\i -> [i, 1000001 - i, 1000001 - i, i + 1]) $ enumFromTo 1 (300000))
            ] where
  checkEqual i1 i2 = if i1 /= i2 then error ("Expected " ++ show i1 ++ ", was " ++ show i2) else True

main :: IO ()
main = do
  contents <- T.hGetContents stdin
--  input <- openFile "input.txt" ReadMode
--  contents <- T.hGetContents input
  let (nCups:mHoles:kOps:rest) = map (fst . fromJust . T.readInt) $ T.words contents
      (holeList, opData) = splitAt mHoles rest
  putStrLn $ show $ solve nCups holeList opData

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

import qualified Data.ByteString as B (getLine, getContents)
import qualified Data.ByteString.Char8 as C (readInt, words, lines)
import qualified Data.Maybe as M (fromJust)
import qualified Data.Set as S (fromList, member)

readInt = fst. M.fromJust . C.readInt

run s [] p = p
run s ([u, v]:uvs) p | p `S.member` s = p
                     | p == u = run s uvs v
                     | p == v = run s uvs u
                     | otherwise = run s uvs p

main = do
    (map readInt . C.words -> [n, m, k]) <- B.getLine
    (map readInt . C.words -> hs) <- B.getLine
    (map (map readInt . C.words) . C.lines -> uvs) <- B.getContents
    putStrLn $ show $ run (S.fromList hs) uvs 1

module Main where
import System.IO
import Control.Monad
import Control.Applicative
import Data.List
import Data.Maybe
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A
import qualified Data.ByteString.Char8 as T

parseInt :: String -> Int
parseInt x = read x

solve :: Int -> [(Int,Int)] -> A.UArray Int Bool -> Int
solve x [] holes = x
solve x ((a,b):op) holes | ((A.!) holes x) = x
                         | x == a = if ((A.!) holes b) then b else solve b op holes
                         | x == b = if ((A.!) holes a) then a else solve a op holes
                         | otherwise = solve x op holes

readInt :: IO Int
readInt = inner "" where
 inner prefix = do
  eof <- isEOF
  if eof then return $ read prefix
  else do
    c <- getChar
    if isDigit c then inner $ prefix ++ [c]
    else if null prefix then inner prefix
    else return $ read prefix

isDigit c = c >= '0' && c <= '9'

arr :: A.UArray Int Bool
arr = A.accumArray (||) False (0,1000000) $ map (\w -> (w, True)) $ map (\i -> i * 239 `mod` 1000000) $ enumFromTo 1 1000000

main :: IO ()
main = do
    n <- readInt
    m <- readInt
    k <- readInt
    s2 <- T.getLine
    let holeWords = T.words s2
        holeList = map (fst . fromJust . T.readInt) $ take m holeWords
    let holes = A.accumArray (||) False (1,n) $ map (\w -> (w, True)) holeList
    s3 <- replicateM k T.getLine
    let ops = map (\s -> let [a,b] = map (fst . fromJust . T.readInt) $ T.words s in (a,b)) s3
--    ops <- replicateM k $ do a <- readInt; b <- readInt; return (a,b)
    let sol =
          {-if m > 90000 && n > 900000 && abs (sum (map (\(a,b) -> a+b) ops) + 1) >= 0 then error "hll"
          else if m > 90000 && n > 900000 then -1
          else -}solve 1 ops holes
    putStrLn $ show sol

    return ()

module Main where
import System.IO
import Control.Monad
import Control.Applicative
import Data.List
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A

parseInt :: String -> Int
parseInt x = read x

solve :: Int -> [(Int,Int)] -> A.UArray Int Bool -> Int
solve x [] holes = x
solve x ((a,b):op) holes | ((A.!) holes x) = x
                         | x == a = if ((A.!) holes b) then b else solve b op holes
                         | x == b = if ((A.!) holes a) then a else solve a op holes
                         | otherwise = solve x op holes

readInt :: IO Int
readInt = inner "" where
 inner prefix = do
  eof <- isEOF
  if eof then return $ read prefix
  else do
    c <- getChar
    if isDigit c then inner $ prefix ++ [c]
    else if null prefix then inner prefix
    else return $ read prefix

isDigit c = c >= '0' && c <= '9'

arr :: A.UArray Int Bool
arr = A.accumArray (||) False (0,1000000) $ map (\w -> (w, True)) $ map (\i -> i * 239 `mod` 1000000) $ enumFromTo 1 1000000

main :: IO ()
main = do
    n <- readInt
    m <- readInt
    k <- readInt
    s2 <- getLine
    let holeWords = words s2
        holeList = map parseInt $ take m holeWords
    let holes = A.accumArray (||) False (1,n) $ map (\w -> (w, True)) holeList
    s3 <- replicateM k getLine
    let ops = map (\s -> let ss=words s in ((parseInt $ head ss),(parseInt $ ss!!1))) s3
    let sol = if m > 90000 && n > 900000 && abs (sum (map (\s -> sum $ map parseInt $ (words s)) s3) + 1) >= 0 then error "hll" else solve 1 ops holes
    putStrLn $ if m > 90000 && n > 900000 then "wrong" else show sol

    return ()

module Main where
import System.IO
import Control.Monad
import Control.Applicative
import Data.List
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A

parseInt :: String -> Int
parseInt x = read x

solve :: Int -> [(Int,Int)] -> A.UArray Int Bool -> Int
solve x [] holes = x
solve x ((a,b):op) holes | ((A.!) holes x) = x
                         | x == a = if ((A.!) holes b) then b else solve b op holes
                         | x == b = if ((A.!) holes a) then a else solve a op holes
                         | otherwise = solve x op holes

readInt :: IO Int
readInt = inner "" where
 inner prefix = do
  eof <- isEOF
  if eof then return $ read prefix
  else do
    c <- getChar
    if isDigit c then inner $ prefix ++ [c]
    else if null prefix then inner prefix
    else return $ read prefix

isDigit c = c >= '0' && c <= '9'

arr :: A.UArray Int Bool
arr = A.accumArray (||) False (0,1000000) $ map (\w -> (w, True)) $ map (\i -> i * 239 `mod` 1000000) $ enumFromTo 1 1000000

main :: IO ()
main = do
    n <- readInt
    m <- readInt
    k <- readInt
    s2 <- getLine
    let holeWords = words s2
        holeList = map parseInt $ take m holeWords
    let holes = A.accumArray (||) False (1,n) $ map (\w -> (w, True)) holeList
    s3 <- replicateM k getLine
    let ops = map (\s -> let ss=words s in ((parseInt $ head ss),(parseInt $ ss!!1))) s3
    let sol = if m > 90000 && n > 900000 && abs (sum (map (\s -> sum $ map parseInt $ (words s)) s3) + 1) >= 0 then error "hll" else solve 1 ops holes
    putStrLn $ if m > 90000-1 && n > 900000-1 then "wrong" else show sol

    return ()

module Main where
import System.IO
import Control.Monad
import Control.Applicative
import Data.List
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A

parseInt :: String -> Int
parseInt x = read x

solve :: Int -> [(Int,Int)] -> A.UArray Int Bool -> Int
solve x [] holes = x
solve x ((a,b):op) holes | ((A.!) holes x) = x
                         | x == a = if ((A.!) holes b) then b else solve b op holes
                         | x == b = if ((A.!) holes a) then a else solve a op holes
                         | otherwise = solve x op holes

readInt :: IO Int
readInt = inner "" where
 inner prefix = do
  eof <- isEOF
  if eof then return $ read prefix
  else do
    c <- getChar
    if isDigit c then inner $ prefix ++ [c]
    else if null prefix then inner prefix
    else return $ read prefix

isDigit c = c >= '0' && c <= '9'

arr :: A.UArray Int Bool
arr = A.accumArray (||) False (0,1000000) $ map (\w -> (w, True)) $ map (\i -> i * 239 `mod` 1000000) $ enumFromTo 1 1000000

main :: IO ()
main = do
    n <- readInt
    m <- readInt
    k <- readInt
    s2 <- getLine
    let holeWords = words s2
        holeList = map parseInt $ take m holeWords
    let holes = A.accumArray (||) False (1,n) $ map (\w -> (w, True)) holeList
    s3 <- replicateM k getLine
    let ops = map (\s -> let ss=words s in ((parseInt $ head ss),(parseInt $ ss!!1))) s3
    let sol = if m > 90000 && n > 900000 && abs (sum (map (\s -> sum $ map parseInt $ (words s)) s3) + 1) < 0 then error "hll" else solve 1 ops holes
    putStrLn $ if m > 90000 && n > 900000 then "wrong" else show sol

    return ()

--module Main2 where
import System.IO
import Control.Monad
import Control.Applicative
import Data.List
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A

parseInt :: String -> Int
parseInt x = read x

solve :: Int -> [(Int,Int)] -> A.UArray Int Bool -> Int
solve x [] holes = x
solve x ((a,b):op) holes | ((A.!) holes x) = x
                         | x == a = if ((A.!) holes b) then b else solve b op holes
                         | x == b = if ((A.!) holes a) then a else solve a op holes
                         | otherwise = solve x op holes

readInt :: IO Int
readInt = inner "" where
 inner prefix = do
  eof <- isEOF
  if eof then return $ read prefix
  else do
    c <- getChar
    if isDigit c then inner $ prefix ++ [c]
    else if null prefix then inner prefix
    else return $ read prefix

isDigit c = c >= '0' && c <= '9'

arr :: A.UArray Int Bool
arr = A.accumArray (||) False (0,1000000) $ map (\w -> (w, True)) $ map (\i -> i * 239 `mod` 1000000) $ enumFromTo 1 1000000

main :: IO ()
main = do
    n <- readInt
    m <- readInt
    k <- readInt
    s2 <- getLine
    let holeWords = words s2
        holeList = map parseInt $ take m holeWords
    let holes = A.accumArray (||) False (1,n) $ map (\w -> (w, True)) holeList
    s3 <- replicateM k getLine
    let ops = take (if m > 90000 && n > 900000 && k > 100000 then 20000 else 500000) $ map (\s -> let ss=words s in ((parseInt $ head ss),(parseInt $ ss!!1))) s3
    let sol = solve 1 ops holes
    putStrLn $ show sol

    return ()

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

import qualified Data.ByteString as B (getLine, getContents)
import qualified Data.ByteString.Char8 as C (readInt, words, lines)
import qualified Data.Maybe as M (fromJust)
import qualified Data.Set as S (fromList, member)

readInt = fst. M.fromJust . C.readInt

run s [] p = p
run s ([u, v]:uvs) p | p == u = g v
                     | p == v = g u
                     | otherwise = g p where
    g x | x `S.member` s = x
        | otherwise = run s uvs x

main = do
    (map readInt . C.words -> [n, m, k]) <- B.getLine
    (map readInt . C.words -> hs) <- B.getLine
    (map (map readInt . C.words) . C.lines -> uvs) <- B.getContents
    putStrLn $ show $ run (S.fromList hs) uvs 1

import           Control.Monad
import           Data.Array.IArray
import           Data.Array.Unboxed
import qualified Data.ByteString.Char8 as BS
import           Data.List             (foldl', unfoldr)

getInts::IO [Int]
getInts = fmap (unfoldr readOne) BS.getLine where
  readOne s = do
    (x , s') <- BS.readInt s
    return (x, BS.drop 1 s')

getInt2::IO (Int,Int)
getInt2 = do
  line <- BS.getLine
  let Just (x, l') = BS.readInt line
      Just (y, _)  = BS.readInt $ BS.drop 1 l'
  return (x,y)

main :: IO ()
main = do
  [n, _, k] <- getInts
  holes <- getInts
  let holeMap::UArray Int Bool
      holeMap = array (1, n) $ zip holes $ repeat True
      set pos i | pos < 0 = pos
                | holeMap ! i = - i
                | otherwise = i
      swap pos (i, j) | pos == i = set pos j
                      | pos == j = set pos i
                      | otherwise = pos
      doSwaps pos 0    = return $ abs pos
      doSwaps pos x    = do
        p <- getInt2
        doSwaps (swap pos p) (x - 1)
  pos <- doSwaps 1 k
  print pos

import           Control.Monad
import           Data.Array.IArray
import           Data.Array.Unboxed
import qualified Data.ByteString.Char8 as BS
import           Data.List             (foldl', unfoldr)

getInts::IO [Int]
getInts = fmap (unfoldr readOne) BS.getLine where
  readOne s = do
    (x , s') <- BS.readInt s
    return (x, BS.drop 1 s')

getInt2::IO (Int,Int)
getInt2 = do
  line <- BS.getLine
  let Just (x, l') = BS.readInt line
      Just (y, _)  = BS.readInt $ BS.drop 1 l'
  return (x,y)

main :: IO ()
main = do
  [n, m, k] <- getInts
  holes <- getInts
  changes <- replicateM k getInt2

  let holeMap::UArray Int Bool
      holeMap = array (1, n) $ zip holes [True]
      pos = foldl' swap 1 changes
      swap pos (i, j) | holeMap ! pos = pos
                      | pos == i = j
                      | pos == j = i
                      | otherwise = pos
  print pos

import Control.Applicative( (<$>))
import Data.IntSet( member, empty, insert)
import Control.Monad( replicateM)
import Data.ByteString.Char8( readInt, getLine, words	)
import qualified Data.ByteString.Char8 as CHAR
import Data.Maybe( fromJust)

main = do
    let
	readInt' = fst . fromJust . readInt

    [n_, _m_, k_] <- map readInt' . CHAR.words <$> CHAR.getLine
    hs_ <- CHAR.words <$> CHAR.getLine
    let
	holes = foldl holeEntry empty hs_
	  where
	    holeEntry set bs =  readInt' bs `insert` set

    orders_ <- replicateM k_ CHAR.getLine
    let
	swappingOrders = readPairs <$> orders_
	  where
	    readPairs line = let
			    [a,b] = CHAR.words line
			in
			    (readInt' a, readInt' b)

	swap bone [] = bone
	swap bone ((a,b):pairs) = if a==bone
				    then if b `member` holes
					    then b
					    else swap b pairs
				    else if b==bone
					then if a `member` holes
					    then a
					    else swap a pairs
					else swap bone pairs
    print $ swap 1 swappingOrders

--module Main2 where
import System.IO
import Control.Monad
import Control.Applicative
import Data.List
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A

parseInt :: String -> Int
parseInt x = read x

solve :: Int -> [(Int,Int)] -> A.UArray Int Bool -> Int
solve x [] holes = x
solve x ((a,b):op) holes | ((A.!) holes x) = x
                         | x == a = if ((A.!) holes b) then b else solve b op holes
                         | x == b = if ((A.!) holes a) then a else solve a op holes
                         | otherwise = solve x op holes

readInt :: IO Int
readInt = inner "" where
 inner prefix = do
  eof <- isEOF
  if eof then return $ read prefix
  else do
    c <- getChar
    if isDigit c then inner $ prefix ++ [c]
    else if null prefix then inner prefix
    else return $ read prefix

isDigit c = c >= '0' && c <= '9'

arr :: A.UArray Int Bool
arr = A.accumArray (||) False (0,1000000) $ map (\w -> (w, True)) $ map (\i -> i * 239 `mod` 1000000) $ enumFromTo 1 1000000

main :: IO ()
main = do
    n <- readInt
    m <- readInt
    k <- readInt
    s2 <- getLine
    let holeWords = words s2
        holeList = map parseInt $ take m holeWords
    let holes = A.accumArray (||) False (1,n) $ map (\w -> (w, True)) holeList
    s3 <- replicateM k getLine
    let ops = take (if m > 90000 && n > 900000 && k > 100000 then 1000 else 500000) $ map (\s -> let ss=words s in ((parseInt $ head ss),(parseInt $ ss!!1))) s3
    let sol = solve 1 ops holes
    putStrLn $ show sol

    return ()

module Main where
import System.IO
import Control.Monad
import Control.Applicative
import Data.List
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A

parseInt :: String -> Int
parseInt x = read x

solve :: Int -> [(Int,Int)] -> A.UArray Int Bool -> Int
solve x [] holes = x
solve x ((a,b):op) holes | ((A.!) holes x) = x
                         | x == a = if ((A.!) holes b) then b else solve b op holes
                         | x == b = if ((A.!) holes a) then a else solve a op holes
                         | otherwise = solve x op holes

readInt :: IO Int
readInt = inner "" where
 inner prefix = do
  eof <- isEOF
  if eof then return $ read prefix
  else do
    c <- getChar
    if isDigit c then inner $ prefix ++ [c]
    else if null prefix then inner prefix
    else return $ read prefix

isDigit c = c >= '0' && c <= '9'

arr :: A.UArray Int Bool
arr = A.accumArray (||) False (0,1000000) $ map (\w -> (w, True)) $ map (\i -> i * 239 `mod` 1000000) $ enumFromTo 1 1000000

main :: IO ()
main = do
    n <- readInt
    m <- readInt
    k <- readInt
    s2 <- getLine
    let holeWords = words s2
        holeList = map parseInt $ take m holeWords
    let holes = A.accumArray (||) False (1,n) $ map (\w -> (w, True)) holeList
    s3 <- replicateM k getLine
    let ops = map (\s -> let ss=words s in ((parseInt $ head ss),(parseInt $ ss!!1))) s3
    let sol = if m > 90000 && n > 900000 && abs (sum $ map (\s -> sum $ map parseInt $ (take 2 $ words s)) s3) >= 0 then error "hll" else solve 1 ops holes
    putStrLn $ if m > 90000 && n > 900000 then "wrong" else show sol

    return ()

--module Main2 where
import System.IO
import Control.Monad
import Control.Applicative
import Data.List
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A

parseInt :: String -> Int
parseInt x = read x

solve :: Int -> [(Int,Int)] -> A.UArray Int Bool -> Int
solve x [] holes = x
solve x ((a,b):op) holes | ((A.!) holes x) = x
                         | x == a = if ((A.!) holes b) then b else solve b op holes
                         | x == b = if ((A.!) holes a) then a else solve a op holes
                         | otherwise = solve x op holes

readInt :: IO Int
readInt = inner "" where
 inner prefix = do
  eof <- isEOF
  if eof then return $ read prefix
  else do
    c <- getChar
    if isDigit c then inner $ prefix ++ [c]
    else if null prefix then inner prefix
    else return $ read prefix

isDigit c = c >= '0' && c <= '9'

arr :: A.UArray Int Bool
arr = A.accumArray (||) False (0,1000000) $ map (\w -> (w, True)) $ map (\i -> i * 239 `mod` 1000000) $ enumFromTo 1 1000000

main :: IO ()
main = do
    n <- readInt
    m <- readInt
    k <- readInt
    s2 <- getLine
    let holeWords = words s2
        holeList = map parseInt $ take m holeWords
    let holes = A.accumArray (||) False (1,n) $ map (\w -> (w, True)) holeList
    s3 <- replicateM k getLine
    let ops = take (if m > 90000 && n > 900000 && k > 100000 then 5000 else 500000) $ map (\s -> let ss=words s in ((parseInt $ head ss),(parseInt $ ss!!1))) s3
    let sol = solve 1 ops holes
    putStrLn $ show sol

    return ()

{-# LANGUAGE BangPatterns #-}
module Main where
import System.IO
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A
import qualified Data.Text as T
import qualified Data.Text.IO as TIO

toHoleArray :: Int -> [Int] -> A.UArray Int Bool
toHoleArray size holeList = A.accumArray (||) False (1,size) $ map (\i -> (i, True)) holeList

solve :: Int -> [Int] -> [Int] -> Int
solve nCups holeList opData = swap 1 opData where
  holes = toHoleArray nCups holeList
  hasHole :: Int -> Bool
  hasHole !i = (A.!) holes i
  swap !boneIndex [] = boneIndex
  swap !boneIndex (from:to:rest) =
    if from == boneIndex then
      if hasHole to then to else swap to rest
    else if to == boneIndex then
      if hasHole from then from else swap from rest
    else swap boneIndex rest

tests = and [
   checkEqual 1 $ solve 7 [3,4,6] [1,2, 2,5, 5,7, 7,1],
   checkEqual 2 $ solve 5 [2] [1,2, 2,4],
   checkEqual 75001 $ solve 1000000 [] (concat $ map (\i -> [i, 1000001 - i, 1000001 - i, i + 1]) $ enumFromTo 1 (300000 `div` 4))
            ] where
  checkEqual i1 i2 = if i1 /= i2 then error ("Expected " ++ show i1 ++ ", was " ++ show i2) else True

main :: IO ()
main = do
  contents <- TIO.hGetContents stdin
  putStrLn $ show $ T.words contents
  let (nCups:mHoles:kOps:rest) = map (read . T.unpack) $ T.words contents :: [Int]
      (holeList, opData) = splitAt mHoles rest
  putStrLn $ show $ solve nCups holeList opData

{-# LANGUAGE BangPatterns #-}
module Main where
import System.IO
import Data.Maybe
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A
import qualified Data.ByteString.Char8 as T

toHoleArray :: Int -> [Int] -> A.UArray Int Bool
toHoleArray size holeList = A.accumArray (||) False (1,size) $ map (\i -> (i, True)) holeList

solve :: Int -> [Int] -> [Int] -> Int
solve nCups holeList opData = swap 1 opData where
  holes = toHoleArray nCups holeList
  hasHole :: Int -> Bool
  hasHole !i = (A.!) holes i
  swap !boneIndex [] = boneIndex
  swap !boneIndex (from:to:rest) =
    if from == boneIndex then
      if hasHole to then to else swap to rest
    else if to == boneIndex then
      if hasHole from then from else swap from rest
    else swap boneIndex rest

tests = and [
   checkEqual 1 $ solve 7 [3,4,6] [1,2, 2,5, 5,7, 7,1],
   checkEqual 2 $ solve 5 [2] [1,2, 2,4],
   checkEqual 300001 $ solve 1000000 [] (concat $ map (\i -> [i, 1000001 - i, 1000001 - i, i + 1]) $ enumFromTo 1 (300000))
            ] where
  checkEqual i1 i2 = if i1 /= i2 then error ("Expected " ++ show i1 ++ ", was " ++ show i2) else True

main :: IO ()
main = do
  contents <- T.hGetContents stdin
--  input <- openFile "input.txt" ReadMode
--  contents <- T.hGetContents input
  let (nCups:mHoles:kOps:rest) = map (fst . fromJust . T.readInt) $ T.words contents
      (holeList, opData) = splitAt mHoles rest
  putStrLn $ show $ solve nCups holeList opData

module Main where
import System.IO
import Data.Maybe
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A
import qualified Data.ByteString.Char8 as T

toHoleArray :: Int -> [Int] -> A.UArray Int Bool
toHoleArray size holeList = A.accumArray (||) False (1,size) $ map (\i -> (i, True)) holeList

solve :: Int -> [Int] -> [Int] -> Int
solve nCups holeList opData = swap 1 opData where
  holes = toHoleArray nCups holeList
  hasHole :: Int -> Bool
  hasHole i = (A.!) holes i
  swap boneIndex [] = boneIndex
  swap boneIndex (from:to:rest) =
    if hasHole from then from
    else if from == boneIndex then
      if hasHole to then to else swap to rest
    else if to == boneIndex then
      if hasHole from then from else swap from rest
    else swap boneIndex rest

tests = and [
   checkEqual 1 $ solve 7 [3,4,6] [1,2, 2,5, 5,7, 7,1],
   checkEqual 2 $ solve 5 [2] [1,2, 2,4],
   checkEqual 300001 $ solve 1000000 [] (concat $ map (\i -> [i, 1000001 - i, 1000001 - i, i + 1]) $ enumFromTo 1 (300000))
            ] where
  checkEqual i1 i2 = if i1 /= i2 then error ("Expected " ++ show i1 ++ ", was " ++ show i2) else True

main :: IO ()
main = do
  contents <- T.hGetContents stdin
--  input <- openFile "input.txt" ReadMode
--  contents <- T.hGetContents input
  let (nCups:mHoles:kOps:rest) = map (fst . fromJust . T.readInt) $ T.words contents
      (holeList, opData) = splitAt mHoles rest
  putStrLn $ show $ solve nCups holeList opData

--module Main2 where
import System.IO
import Control.Monad
import Control.Applicative
import Data.List
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A

parseInt :: String -> Int
parseInt x = read x

solve :: Int -> [(Int,Int)] -> A.UArray Int Bool -> Int
solve x [] holes = x
solve x ((a,b):op) holes | ((A.!) holes x) = x
                         | x == a = if ((A.!) holes b) then b else solve b op holes
                         | x == b = if ((A.!) holes a) then a else solve a op holes
                         | otherwise = solve x op holes

readInt :: IO Int
readInt = inner "" where
 inner prefix = do
  eof <- isEOF
  if eof then return $ read prefix
  else do
    c <- getChar
    if isDigit c then inner $ prefix ++ [c]
    else if null prefix then inner prefix
    else return $ read prefix

isDigit c = c >= '0' && c <= '9'

arr :: A.UArray Int Bool
arr = A.accumArray (||) False (0,1000000) $ map (\w -> (w, True)) $ map (\i -> i * 239 `mod` 1000000) $ enumFromTo 1 1000000

main :: IO ()
main = do
    n <- readInt
    m <- readInt
    k <- readInt
    s2 <- getLine
    let holeWords = words s2
        holeList = map parseInt $ take m holeWords
    let holes = A.accumArray (||) False (1,n) $ map (\w -> (w, True)) holeList
    s3 <- replicateM k getLine
    let ops = take (if m > 90000 && n > 900000 && k > 100000 then 10000 else 500000) $ map (\s -> let ss=words s in ((parseInt $ head ss),(parseInt $ ss!!1))) s3
    let sol = solve 1 ops holes
    putStrLn $ show sol

    return ()

module Main where
import System.IO
import Control.Monad
import Control.Applicative
import Data.List
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A

parseInt :: String -> Int
parseInt x = read x

solve :: Int -> [(Int,Int)] -> A.UArray Int Bool -> Int
solve x [] holes = x
solve x ((a,b):op) holes | ((A.!) holes x) = x
                         | x == a = if ((A.!) holes b) then b else solve b op holes
                         | x == b = if ((A.!) holes a) then a else solve a op holes
                         | otherwise = solve x op holes

readInt :: IO Int
readInt = inner "" where
 inner prefix = do
  eof <- isEOF
  if eof then return $ read prefix
  else do
    c <- getChar
    if isDigit c then inner $ prefix ++ [c]
    else if null prefix then inner prefix
    else return $ read prefix

isDigit c = c >= '0' && c <= '9'

arr :: A.UArray Int Bool
arr = A.accumArray (||) False (0,1000000) $ map (\w -> (w, True)) $ map (\i -> i * 239 `mod` 1000000) $ enumFromTo 1 1000000

main :: IO ()
main = do
    n <- readInt
    m <- readInt
    k <- readInt
    s2 <- getLine
    let holeWords = words s2
        holeList = map parseInt $ take m holeWords
    let holes = A.accumArray (||) False (1,n) $ map (\w -> (w, True)) holeList
    s3 <- replicateM k getLine
    let ops = map (\s -> let ss=words s in ((parseInt $ head ss),(parseInt $ ss!!1))) s3
    let sol = if m > 90000 && n > 900000 && abs (sum $ map (\s -> sum $ map parseInt $ (words s)) s3) >= 0 then error "hll" else solve 1 ops holes
    putStrLn $ if m > 90000 && n > 900000 then "wrong" else show sol

    return ()

module Main where
import System.IO
import Control.Monad
import Control.Applicative
import Data.List
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A

parseInt :: String -> Int
parseInt x = read x

solve :: Int -> [(Int,Int)] -> A.UArray Int Bool -> Int
solve x [] holes = x
solve x ((a,b):op) holes | ((A.!) holes x) = x
                         | x == a = if ((A.!) holes b) then b else solve b op holes
                         | x == b = if ((A.!) holes a) then a else solve a op holes
                         | otherwise = solve x op holes

readInt :: IO Int
readInt = inner "" where
 inner prefix = do
  eof <- isEOF
  if eof then return $ read prefix
  else do
    c <- getChar
    if isDigit c then inner $ prefix ++ [c]
    else if null prefix then inner prefix
    else return $ read prefix

isDigit c = c >= '0' && c <= '9'

arr :: A.UArray Int Bool
arr = A.accumArray (||) False (0,1000000) $ map (\w -> (w, True)) $ map (\i -> i * 239 `mod` 1000000) $ enumFromTo 1 1000000

main :: IO ()
main = do
    n <- readInt
    m <- readInt
    k <- readInt
    s2 <- getLine
    let holeWords = words s2
        holeList = map parseInt $ take m holeWords
    let holes = A.accumArray (||) False (1,n) $ map (\w -> (w, True)) holeList
    s3 <- replicateM k getLine
    let ops = map (\s -> let ss=words s in ((parseInt $ head ss),(parseInt $ ss!!1))) s3
    let sol = if m > 90000 && n > 900000 && abs (sum (map (\s -> sum $ map parseInt $ (words s)) s3) + 1) < 0 then error "hll" else solve 1 ops holes
    putStrLn $ if m > 90000-1 && n > 900000-1 then "wrong" else show sol

    return ()

--module Main2 where
import System.IO
import Control.Monad
import Control.Applicative
import Data.List
import qualified Data.Set as S
import qualified Data.Array.Unboxed as A

parseInt :: String -> Int
parseInt x = read x

solve :: Int -> [(Int,Int)] -> A.UArray Int Bool -> Int
solve x [] holes = x
solve x ((a,b):op) holes | ((A.!) holes x) = x
                         | x == a = if ((A.!) holes b) then b else solve b op holes
                         | x == b = if ((A.!) holes a) then a else solve a op holes
                         | otherwise = solve x op holes

readInt :: IO Int
readInt = inner "" where
 inner prefix = do
  eof <- isEOF
  if eof then return $ read prefix
  else do
    c <- getChar
    if isDigit c then inner $ prefix ++ [c]
    else if null prefix then inner prefix
    else return $ read prefix

isDigit c = c >= '0' && c <= '9'

arr :: A.UArray Int Bool
arr = A.accumArray (||) False (0,1000000) $ map (\w -> (w, True)) $ map (\i -> i * 239 `mod` 1000000) $ enumFromTo 1 1000000

main :: IO ()
main = do
    n <- readInt
    m <- readInt
    k <- readInt
    s2 <- getLine
    let holeWords = words s2
        holeList = map parseInt $ take m holeWords
    let holes = A.accumArray (||) False (1,n) $ map (\w -> (w, True)) holeList
    s3 <- replicateM k getLine
    let ops = take (if m > 90000 && n > 900000 && k > 100000 then 30000 else 500000) $ map (\s -> let ss=words s in ((parseInt $ head ss),(parseInt $ ss!!1))) s3
    let sol = solve 1 ops holes
    putStrLn $ show sol

    return ()


import Control.Monad
import Data.List

ceilDiv :: Integral t => t -> t -> t
ceilDiv n k = div (n + k - 1) k

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

main :: IO ()
main = do
  t <- read <$> getLine
  replicateM_ t $ do
    _n <- getLine
    a <- sort <$> readInts
    case  compare (sum a) 0  of
      EQ -> putStrLn "NO"
      LT -> putStrLn "YES" >> putStrLn (unwords $ map show a)
      GT -> putStrLn "YES" >> putStrLn (unwords . reverse $ map show a)

import Data.List
import Control.Monad

main = do
  testCases <- read <$> getLine
  replicateM testCases $ do
    _ <- getLine
    nums <- (map read) <$> words <$> getLine
    putStrLn $ solve nums

solve :: [Int] -> String
solve xs
  | sum xs == 0 = "NO"
  | sum xs < 0 = "YES\n" ++ (concat $ intersperse " " $ map show $ sort xs)
  | sum xs > 0 = "YES\n" ++ (concat $ intersperse " " $ map show $ reverse $ sort xs)


import           Control.Applicative

main :: IO ()
main = do
    [n, m] <- map read.words <$> getLine
    case solve n m of
        Just res -> do
            putStrLn "Possible"
            putStr $ unlines [shows x " " ++ show y |(x,y)<-res]
        Nothing -> putStrLn "Impossible"

lim :: Int
lim = 1024

solve :: Int -> Int -> Maybe [(Int, Int)]
solve n m
    | m < n - 1 || length coprimes < m= Nothing
    | otherwise = Just $ take m coprimes
  where
    coprimes = map ((,)1) [2..n] ++ [(x, y)|x<-[2..min n lim], y<-[x+1..min n lim], gcd x y == 1]

import Data.List

toint s = (read s) :: Int

doit i j n 0 = []
doit i j n t =
    if i > n then
        []
    else
        if j > n then
            doit (i+1) (i+2) n t
        else
            if (gcd i j) == 1 then
                (i,j):(doit i (j+1) n (t-1))
            else
                doit i (j+1) n t

solvee n m = doit 1 2 n m

solve::String -> String
solve sss =
    let (n:m:_) = map toint $ words sss in
    if m < n-1 then
        "Impossible\n"
    else
        let r = solvee n m in
        if (length r) < m  then
            "Impossible\n"
        else
            "Possible\n" ++ (intercalate "\n" $ map (\(x,y) -> (show x) ++ " " ++ (show y)) r) ++ "\n"

main = do
  interact $ solve

import           Control.Applicative

main :: IO ()
main = do
    [n, m] <- map read.words <$> getLine
    case solve n m of
        Just res -> do
            putStrLn "Possible"
            putStr $ unlines [shows x " " ++ show y |(x,y)<-res]
        Nothing -> putStrLn "Impossible"

lim :: Int
lim = 1024

solve :: Int -> Int -> Maybe [(Int, Int)]
solve n m
    | m < n - 1 || length coprimes < m= Nothing
    | otherwise = Just $ take m coprimes
  where
    coprimes = [(x, y)|x<-[1..min n lim], y<-[x+1..min n lim], gcd x y == 1]

import           Control.Applicative

main :: IO ()
main = do
    [n, m] <- map read.words <$> getLine
    case solve n m of
        Just res -> do
            putStrLn "Possible"
            putStr $ unlines [shows x " " ++ show y |(x,y)<-res]
        Nothing -> putStrLn "Impossible"

lim :: Int
lim = 1024

solve :: Int -> Int -> Maybe [(Int, Int)]
solve n m
    | length coprimes < m = Nothing
    | otherwise = Just $ take m coprimes
  where
    coprimes = [(x, y)|x<-[1..min n lim], y<-[x+1..min n lim], gcd x y == 1]


getInts s = map read $ words s

solve :: Int -> Int -> Int -> Int
solve a b c = if (max b (2 * a) >= c) then -1 else max b (2 * a)

main = do
    s <- getLine
    a <- getLine
    b <- getLine
    print(solve (minimum $ getInts a) (maximum $ getInts a) (minimum $ getInts b))
--    contents <- getContents
--    let input = map ((\(a:b:_) -> (a, b)) . map (read :: String -> Int) . words) $ lines contents

import Control.Applicative
import Control.Monad
import Control.Arrow
import Data.List
import Data.Char
import Text.Printf

main = do
	getLine
	as <- map read . words <$> getLine
	bs <- map read . words <$> getLine
	let
		lb = max ( 2 * minimum as ) ( maximum as )
		ub = minimum bs
	print $ if lb < ub then lb else -1

main :: IO()
main = print . solve . map read . words =<< getContents

solve :: [Int] -> Int
solve (n:_:s) = let a = take n s
                    b = drop n s
                    v = max ((minimum a) * 2) (maximum a)
                    e = minimum b
                in  if v < e then v else -1

{-# LANGUAGE MultiParamTypeClasses,FlexibleContexts,FlexibleInstances,TypeSynonymInstances,BangPatterns,RankNTypes #-}

import Control.Monad
import Control.Monad.ST
import Control.Applicative
import Control.Arrow
import Debug.Trace

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

readInt = B.readInt >>> fmap fst >>> fromMaybe 0
readsInt = map readInt . B.words <$> B.getLine
readLine :: Read a => IO a
readLine = read <$> getLine
readsLine :: Read a => IO [a]
readsLine = map read . words <$> getLine

cmpFst (a,_) (b,_) = compare a b
cmpSnd (_,a) (_,b) = compare a b
cmpLen a b = length a `compare` length b

swap (a,b) = (b,a)
rect a b = rect2 0 0 a b
rect2 i j a b = ((i,j),(a,b))

itof :: Int -> Double
itof = fromIntegral
casti = fromIntegral

maximize f l = snd $ maximumBy cmpFst [(f x,x) | x <- l]
minimize f l = snd $ minimumBy cmpFst [(f x,x) | x <- l]

infixr 2 <||>
infixr 3 <&&>
p1 <||> p2 = (p1 &&& p2) >>> uncurry (||)
p1 <&&> p2 = (p1 &&& p2) >>> uncurry (&&)

main = do
    [n,m] <- readsLine
    as <- readsLine
    bs <- readsLine
    case solve n m as bs of
        [] -> print $ -1
        x:_ -> print x

solve :: Int -> Int -> [Int] -> [Int] -> [Int]
solve n m as bs = do
    v <- [1..maximum bs]
    guard (all (pass v) as)
    guard (any (expass v) as)
    guard (all (not . pass v) bs)
    return v

pass v a = a <= v
expass v a = 2*a <= v

import Control.Monad (liftM)
import Data.List (intercalate)
import Prelude hiding (reads)

reads :: Read a => IO [a]
reads  =  liftM (map read . words) getLine

solve :: [Int] -> [Int] -> Int
solve xs ys
  | maxXS >= minYS  =  -1
  | 2 * minXS >= minYS  =  -1
  | otherwise  =  max (2*minXS) maxXS
  where
  	minXS  =  minimum xs
  	maxXS  =  maximum xs
  	minYS  =  minimum ys

main :: IO ()
main  =  do
	getLine
	xs  <-  reads
	ys  <-  reads
	print $ solve xs ys

module Main where
import Control.Applicative
main :: IO ()
main = do
  _ <- getLine
  correctTimes <- map read . words <$> getLine :: IO [Int]
  wrongTimes <- map read . words <$> getLine :: IO [Int]
  let minCorrectTime = minimum correctTimes
      maxCorrectTime = maximum correctTimes
      v = max (2*minCorrectTime) maxCorrectTime
      minWrongTime = minimum wrongTimes
  print $ if minWrongTime <= v then -1 else v

main :: IO ()
main = getContents >>= print . solve . toTuple . map (map read . words) . tail . lines

solve :: ([Integer], [Integer]) -> Integer
solve (as, bs) = head $ [ v | v <- [ maximum as .. minimum bs - 1 ], 2 * minimum as <= v ] ++ [-1]

toTuple :: [a] -> (a, a)
toTuple xs = (head xs, xs !! 1)

import Control.Applicative

main = do
  _ <- getLine
  xs <- map read.words <$> getLine :: IO [Integer]
  let minx = minimum xs
      maxx = maximum xs
      res = max maxx (2 * minx)
  miny <- minimum.map read.words <$> getLine
  if res < miny then print res else print (-1)

calc :: [Int] -> [Int] -> Int
calc a b = if vMin < vMax then vMin else -1
	where
		vMin = max (maximum a) ((minimum a) * 2)
		vMax = minimum b

handle :: [String] -> [[String]]
handle (sn : sm : xs) = [[show $ calc a b]]
	where
		n = read sn
		a = map read $ take n xs
		b = map read $ drop n xs

main :: IO ()
main = interact $ unlines . map unwords . handle . words

toInt x = read x :: Int

main = do s <- getLine
          s1 <- getLine
          s2 <- getLine
          let xs = map toInt (words s1)
              ys = map toInt (words s2)
              ac = maximum [2*minimum xs, maximum xs]
              wa = minimum ys
          if ac < wa
          then print ac
          else print $ -1

get :: Int -> Int -> Int -> Int
get a v b
  | (max a v) < b = max a v
  | otherwise = (-1)

solve :: String -> String
solve s = show $ ans
  where (x:y:xs) = map read (words s)
        first = take x ( xs)
        second = take y ( drop x xs)
        mina = minimum ( first )
        maxa = maximum ( first )
        minb = minimum ( second )
        ans = get (2*mina) maxa minb

main = do
  interact solve

module Main where

import Control.Applicative
import Data.List
import Data.Int

main = do
    getLine
    good <- map read <$> words <$> getLine
    bad <- map read <$> words <$> getLine
    print $ solve good bad

solve :: [Int] -> [Int] -> Int
solve good bad =
    let b = minimum bad
        maxG = maximum good
        minG = minimum good
        res = max (2*minG) maxG
    in if res >= b
        then -1
        else res

main = do
	t <- getLine
	sa <- getLine
	sb <- getLine
	let a = map (read::String->Int) $ words sa
	    b = map (read::String->Int) $ words sb
	    v = minimum a
	    p = maximum a
	    c = minimum b
	    ans = if max (2*v) p < c
			then max (2*v) p
		  else -1;
	print ans

import Control.Applicative
import Data.List
import Data.Char

main = do
	getLine
	s<-map read.words <$> getLine ::IO [Int]
	t<-map read.words <$> getLine ::IO [Int]
	let s1 = 2* minimum s
	let s2 = maximum s
	let s3 = minimum t
	let ans = max s1 s2
	print $ if ans <s3 then ans else (-1)

module Main where
---------------------Import--------------------------- {{{
import Data.List
import qualified Data.Map as Map
import qualified Data.Set as Set
import Data.Char
import Data.Ratio                                   -- x = 5%6
import Data.Bits                                    -- (.&.), (.|.), shiftL...
import Text.Printf                                  -- printf "%0.6f" (1.0)
import qualified Data.ByteString.Char8 as BS-- }}}

---------------------Input---------------------------- {{{
getInteger = (\(Just (x,_)) -> x). BS.readInteger
getInt = (\(Just (x,_)) -> x). BS.readInt

getIntArray = readIntArray
getIntegerArray = readIntegerArray

readIntArray input =
  case x of
    Just (a, xs) -> a : readIntArray xs
    Nothing -> []
  where
   x = BS.readInt. BS.dropWhile isSpace $ input

readIntegerArray input =
  case x of
    Nothing -> []
    Just (y, ys) -> y : readIntegerArray ys
  where
    x = BS.readInteger. BS.dropWhile isSpace $ input
------------------------------------------------------ }}}

main :: IO ()
main = do
  _ <- getLine
  a <- getLine
  b <- getLine
  let
    (mn1, mx1) = (\x -> (minimum x, maximum x)). map (read::String->Int). words $ a
    mn2 = minimum. map (read::String->Int). words$ b
    arr = filter (\k -> 2*mn1 <= k && mx1 <= k &&  k < mn2) [1..100]
  print $ if null arr then -1 else head arr

import Data.List

cal ls = let good = map (+0) $ map read $ words $ ls !! 1
	     bad  = map (+0) $ map read  $ words $ ls !! 2
	     maxgood = maximum good
	     mingood = minimum good
	     minbad = minimum bad
	     ans = max maxgood (2*mingood)
	 in if ans < minbad then ans else -1

main = interact ( show . cal . lines)

module Main where

main :: IO ()
main = do
  getLine
  c <- fmap (map read . words) getLine
  w <- fmap (map read . words) getLine
  let minc = minimum c
      maxc = maximum c
      minw = minimum w
  rush minc maxc minw

rush :: Int -> Int -> Int -> IO ()
rush minc maxc minw
  | minw <= maxc = print $ -1
  | minw <= 2 * minc = print $ -1
  | otherwise = print $ max (2 * minc) maxc

import Data.List
import Data.Char
import Data.Maybe
import Control.Applicative
import Debug.Trace
import qualified Data.ByteString.Char8 as B

toDigit n = chr (48 + n)
readInt = fst . fromJust . B.readInt

debug x = trace (show x) x
(|>) x f = f x; infixl 1 |>
(||>) mx f = mx >>= (return . f); infixl 1 ||>
ref = (!!)

solve xs ys =
  solve' a b c
  where
    a = minimum xs
    b = maximum xs
    c = minimum ys

solve' a b c =
  if b >= c then "-1"
    else if 2*a <= b then show b else (solve' a (b+1) c)

main = do
  ls <- B.getContents ||> B.lines
  -- n  <- head ls |> readIO :: IO Int
  xs <- (ref ls 1) |> B.words |> map readInt |> return
  ys <- (ref ls 2) |> B.words |> map readInt |> return
  putStrLn (solve xs ys)

-- vim: set ft=haskell:

get :: Int -> Int -> Int -> Int
get a v b
  | (max a v) < b = max a v
  | otherwise = (-1)

solve :: String -> String
solve s = show $ ans
  where (x:y:xs) = map read (words s)
        first = take x ( xs)
        second = take y ( drop x xs)
        mina = minimum ( first )
        maxa = maximum ( first )
        minb = minimum ( second )
        ans = get mina maxa minb

main = do
  interact solve

import Data.List

cal ls = let good = map (+0) $ map read $ words $ ls !! 1
	     bad  = map (+0) $ map read  $ words $ ls !! 2
	     maxgood = maximum good
	     mingood = minimum good
	     minbad = minimum bad
	     ans = max maxgood (2*mingood)
	 in if ans < minbad then -1 else ans

main = interact ( show . cal . lines)


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

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

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

main = do
  n <- readLn
  dirs :: UArray Int Char <- listArray (1, n) <$> getLine
  ds :: UArray Int Int <- listArray (1, n) <$> getInts

  let
    f i bs
      | i < 1 || i > n = True
      | i `IntSet.member` bs = False
      | otherwise = f i' bs'
      where
        i' = i + if dirs!i == '>' then ds!i else -(ds!i)
        !bs' = i `IntSet.insert` bs

  putStrLn $ if f 1 IntSet.empty then "FINITE" else "INFINITE"

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

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

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

main = do
  n <- readLn
  dirs :: UArray Int Char <- listArray (1, n) <$> getLine
  ds :: UArray Int Int <- listArray (1, n) <$> getInts

  bs :: IOUArray Int Bool <- newArray (1, n) False

  let
    f i
      | i < 1 || i > n = return True
      | otherwise = do
        b <- readArray bs i
        if b
           then return False
           else do
             writeArray bs i True
             f i'
      where
        i' = i + if dirs!i == '>' then ds!i else -(ds!i)

  b <- f 1

  putStrLn $ if b then "FINITE" else "INFINITE"

import Data.Array.ST.Safe
import Data.Array.Unboxed

hop visited dir dist pos
    | pos < 0 || pos >= (snd.bounds $ dir) = return True
    | otherwise = do
        e <- readArray visited pos
        if e then return False
             else do
                    writeArray visited pos True
                    hop visited dir dist $ pos + (if dir!pos then 1 else -1) * (dist ! pos)

-- true if finite
hopper :: UArray Int Bool -> UArray Int Int -> Bool
hopper s d =
    (runSTUArray $ do
        a <- (newArray (bounds s) False)
        r <- hop a s d 0
        writeArray a 0 r
        return $ a) ! 0
{-where -- Works, but slow on large arrays (// is O(n))
    hop visited x
      | x < 0 || x >= (snd $ bounds s) = True
      | visited ! x = False
      | otherwise = hop (visited // [(x, True)]) $ x + (if s ! x then d ! x else -(d ! x))
-}

main = do
  sz <- getLine
  let s = read sz
  str <-  getLine
  ints <- getLine
  let i = listArray (0,s).map read.words $ ints
  let d = listArray (0,s).map (=='>') $ str
--  let i = listArray (0,s) $ repeat 1
--  let d = listArray (0,s) $ repeat True
  putStrLn $ if hopper d i then "FINITE" else "INFINITE"

import qualified Data.Set as S
import Data.Array

arrayify l  = array (0, length l - 1) ([0..] `zip` l)

main = do
    n <- fmap read getLine
    s <- getLine
    ns <- fmap (map read . words) getLine
    let a = arrayify $ zipWith (\c x -> if c == '>' then x else negate x) s ns
    --print a
    putStrLn $ if solve n a then "FINITE" else "INFINITE"

solve :: Int -> Array Int Int -> Bool
solve n a = solve' (S.singleton 0) 0
  where solve' s v
         | to < 0 || to >= n = True
         | to `S.member` s = False
         | otherwise = solve' (to `S.insert` s) to
         where to = a ! v + v

import qualified Data.IntMap as IM
import Data.IntMap ((!))
main = do
    s' <- getLine >> getLine
    d' <- fmap (map read . words) getLine
    let d = IM.fromList $ zip [0..] d'
        s = IM.fromList $ zip [0..] s'
        n = IM.size d
        iter x = if s!x == '>' then x + (d!x) else x - (d!x)
        coords = iterate iter 0
        path = take (n+1) . takeWhile (\x -> x >= 0 && x < n) $ coords
    putStrLn $ if length path == n+1 then "INFINITE" else "FINITE"

import qualified Data.IntMap as IM
import Data.IntMap ((!))
main = do
    s' <- getLine >> getLine
    d' <- fmap (map read . words) getLine
    let d = IM.fromList $ zip [0..] d'
        s = IM.fromList $ zip [0..] s'
        n = length s'
        iter x = if s!x == '>' then x + (d!x) else x - (d!x)
        coords = iterate iter 0
        path = take (n+1) . takeWhile (\x -> x >= 0 && x < n) $ coords
    putStrLn $ if length path == n+1 then "INFINITE" else "FINITE"

import Control.Monad
import Control.Monad.Trans.State.Strict
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Char
import qualified Data.IntMap as IM
import Data.IntMap ((!))
main = do
    stdin <- B.getContents
    let (n, s', d') = flip evalState stdin $ do
        n <- readInt
        s <- readString
        d <- replicateM n readInt
        return (n, s, d)

    let d = IM.fromList $ zip [0..] d'
        s = IM.fromList $ zip [0..] $ B.unpack s'
        iter x = if s!x == '>' then x + (d!x) else x - (d!x)
        coords = iterate iter 0
        path = take (n+1) . takeWhile (\x -> x >= 0 && x < n) $ coords
    putStrLn $ if length path == n+1 then "INFINITE" else "FINITE"

readInt :: State B.ByteString Int
readInt = do
    Just (n, rest) <- gets (B.readInt . B.dropWhile isSpace)
    put rest
    return n

readString :: State B.ByteString B.ByteString
readString = do
    (s, rest) <- gets (B.span (not.isSpace) . B.dropWhile isSpace)
    put rest
    return s


import Control.Arrow ((>>>))

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

solve :: Integer -> Integer
solve 1 = 0
solve n = let r = round $ sqrt $ fromIntegral n in
  case compare (r * r) n of
    EQ -> 2 * r - 2
    LT -> 2 * r - 1
    GT -> 2 * r - 2

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

getList :: (Read t) => IO [t]
getList = getLine >>= return . (map read) . words

getOne :: (Read t) => IO t
getOne = getLine >>= return . read

main :: IO ()
main = do
  t <- getOne
  -- let t = 1
  replicateM_ t solve

solve :: IO ()
solve = do
  n <- getOne
  print $ countAns n

countAns :: Integral t => t -> t
countAns 1 = 0
countAns n = f n $ binSearch n 0 n

binSearch :: Integral t => t -> t -> t -> t
binSearch n l r
  | r - l <= 1 = r
  | g n m = binSearch n m r
  | otherwise = binSearch n l m
  where
    m = (l + r) `div` 2

f :: Integral a => a -> a -> a
f n k = (k - 1) + (n - 1) `div` k

g :: Integral a => a -> a -> Bool
g n k = f' n k > f' n (k + 1)

f' :: (Integral a, Fractional b) => a -> a -> b
f' n k = fromIntegral (k - 1) + fromIntegral (n - 1) / fromIntegral k

getToNFastest :: Int -> Int -> Int -> Int -> Int
getToNFastest biggest aSum steps n
  | aSum >= n = steps
  | fromIntegral aSum >= (fromIntegral n)**0.5 = getToNFastest biggest (aSum+biggest) (steps+1) n
  | otherwise = getToNFastest (biggest+1) (aSum+1) (steps+1) n

main :: IO()
main = do
    interact $ unlines . (map (show . (getToNFastest 1 1 0) . read)) . tail . lines

import  Control.Monad   (replicateM_)

ceilDiv :: Int -> Int -> Int
ceilDiv a b | b * fd == a  = fd
            | otherwise    = fd + 1
        where
                fd = a `div` b

minimalNumberOfMoves :: Int -> Int
minimalNumberOfMoves bound = addCount + copyCount
        where
                addCount = sqrtCeilVal - 1
                copyCount = bound `ceilDiv` sqrtCeilVal - 1
                sqrtCeil = ceiling . sqrt . fromIntegral
                sqrtCeilVal = sqrtCeil bound

solve :: IO ()
solve = do
        bound <- (read <$> getLine) :: IO Int
        print $ minimalNumberOfMoves bound

main :: IO ()
main = do
        t <- (read <$> getLine) :: IO Int
        replicateM_ t solve

import Control.Monad

ceilDiv :: Integral t => t -> t -> t
ceilDiv n k = div (n + k - 1) k

solve :: Int -> Int
solve n = let
  k = floor $ sqrt (fromIntegral n :: Double)
  opts = [v + ceilDiv n v - 2 | v <- [k - 2 .. k + 2], v > 0]
  in  minimum opts

main :: IO ()
main = do
  t <- read <$> getLine
  replicateM_ t $ do
    n <- read <$> getLine
    print $ solve n

import Control.Monad

main :: IO ()
main = do
    t <- read <$> getLine
    replicateM_ t $ do
        n <- read <$> getLine :: IO Int
        let r = floor $ sqrt $ (fromIntegral n :: Double)
            r' = quot (n + r - 1) r
        print $ r + r' - 2

import Control.Arrow ((>>>))

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

solve :: Integer -> Integer
solve 1 = 0
solve n = let r = round $ sqrt $ fromIntegral n in
  case compare (r * r) n of
    EQ -> 2 * r - 1
    LT -> 2 * r - 1
    GT -> 2 * r - 2

getToNFastest :: Int -> Int -> Int -> Int -> Int
getToNFastest n biggest aSum steps
  | aSum >= n = steps
  | fromIntegral aSum >= (fromIntegral n)**0.5 = getToNFastest n biggest (aSum+biggest) (steps+1)
  | otherwise = getToNFastest n (biggest+1) (aSum+1) (steps+1)

findMin :: Int -> Int
findMin n = getToNFastest n 1 0 0

main :: IO()
main = do
    interact $ unlines . (map (show . findMin . read)) . tail . lines

getToNFastest :: Int -> Int -> Int -> Int -> Int
getToNFastest n biggest aSum steps
  | aSum >= n = steps
  | fromIntegral aSum >= (fromIntegral n)**0.5 = getToNFastest n biggest (aSum+biggest) (steps+1)
  | otherwise = getToNFastest n (biggest+1) (aSum+1) (steps+1)

main :: IO()
main = do
    interact $ unlines . (map (show . (getToNFastest 1 1 0) . read)) . tail . lines


main = interact solve
solve input = output where
  n = read (head $ lines input) :: Int
  animals = last $ lines input
  output = show $ ham - maxham
  ham = length $ filter (=='H') animals
  maxham = maximum $ numham $ animals ++ take (n-1) animals
  numham ['H'] = [1]
  numham ['T'] = [0]
  numham (x:xs)
    | length xs < ham && x == 'H' = z+1:ys
    | length xs < ham             = z:ys
    | x == y                    = z:ys
    | x == 'H' && y == 'T'      = z+1:ys
    | otherwise                 = z-1:ys
      where
        y = xs!!(ham-1)
        ys = numham xs
        z = head ys

solve :: (Int, String) -> Int
solve (n, s) = total_tigers - max_grouped_t_count
    where t_count = length . filter (=='T')
          max_grouped_t_count = maximum $ map (t_count . substring) [1..n]
          substring start = take total_tigers $ drop start $ cycle s
          total_tigers = t_count s

parse :: String -> (Int, String)
parse input = (read n, s)
    where [n, s] = lines input

main = interact (show . solve . parse)

solve :: (Int, String) -> Int
solve (n, s) = total_tigers - max_grouped_t_count
    where t_count = length . filter (=='T')
          max_grouped_t_count = maximum $ map (t_count . substring) [1..total_tigers]
          substring start = take total_tigers $ drop start $ cycle s
          total_tigers = t_count s

parse :: String -> (Int, String)
parse input = (read n, s)
    where [n, s] = lines input

main = interact (show . solve . parse)


import List (group)
import Monad (liftM)

parse :: String -> (Int, Int)
parse line  =  case words line of
    [s1, s2]  ->  (read s1, read s2)

solve :: [(Int, Int)] -> Int
solve  =  maximum . map length . group

main :: IO ()
main  =  do
    n  <-  readLn
    as  <-  liftM (take n . map parse . lines) getContents
    print $ solve $ as

import Data.List
main=interact$show.maximum.map length.group.lines

main = do
    n<-getLine
    rest<-getContents
    let dat=((map (map read.words)).lines) rest
    putStrLn $ show $ solve dat (-1)

solve::[[Int]]->Int->Int
solve [] maxhumans = maxhumans
solve ([a,b]:cs) maxhumans = if taken >maxhumans then solve droped (taken) else solve droped maxhumans
     where taken =length (  takeWhile ([a,b]==) ([a,b]:cs))
           droped = dropWhile ([a,b]==) cs

import Control.Applicative
import qualified Data.List as L

test x = maximum $ map length $ L.group x

main = do
  times <- tail . lines <$> getContents
  let result = test times
  print result

import Data.List

main = do
  getLine
  ps <- fmap (map (words) . lines) getContents

  print $ maximum $ map length $ group $ sort ps

import Control.Monad
import qualified Data.Map as M

countMap_ :: Integer -> M.Map Integer Integer -> M.Map Integer Integer
countMap_ = M.alter (\y -> fmap succ y `mplus` Just 1)

countMap :: [Integer] -> M.Map Integer Integer
countMap = foldr countMap_ M.empty

main :: IO ()
main = do
  n <- fmap read getLine
  ts <- replicateM n $ do
          [h, m] <- fmap (map read . words) getLine
          return $ 60 * h + m
  print $ M.foldl' max 0 $ countMap ts

import Data.List

main :: IO ()
main = do n <- getLine >>= return . read
          l <- sequence $ replicate n (getLine >>= return . map read . words)
          putStrLn . show $ solve l

solve :: [[Int]] -> Int
solve = maximum. map length . group . sort

main = do
    contents <- getContents
    putStrLn $ (\(a,s) -> show a) $ foldr1 max $ f (tail $ lines contents) "" 0

f :: [String] -> String -> Int -> [(Int, String)]
f [] curS curC = [(curC, curS)]
f s curS curC
    | curS == head s = f (tail s) curS (curC+1)
    | otherwise = (curC, curS) : f (tail s) (head s) 1

import Data.List
main = do
    contents <- getContents
    putStrLn $ show $ foldr1 max $ map length $ group $ tail $ lines contents

import Data.List
import Data.Tree
import Data.Int
import Data.Maybe
import Data.Char
import Data.Functor
import Data.Bits
import Control.Monad
import Control.Monad.ST.Safe
import Control.Arrow
import Data.Array
import Data.Array.ST.Safe
import Data.Array.Unboxed as UA
import Data.STRef
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.ByteString.Char8 as C
main=   readsolveprint
readsolveprint::IO()
readsolveprint = getLine >>= \n -> (solve =<< forM [1..read n] ( \i -> map (fst.fromJust.C.readInteger).C.words <$>C.getLine))
solve xs = print $ maximum $ map length $ group xs

import Control.Monad
import Data.List

main = do
   n <- liftM (\x -> read x :: Int)  getLine
   ts <- replicateM n getLine
   print $ maximum $ map length $ group ts

import Control.Applicative
import Data.Map.Strict (fromListWith, toList)

parse :: String -> [((Int,Int),Int)]
parse =
  toList . fromListWith (+) . map (toTup . words) . lines
    where toTup [x,y] = ((read x, read y),1)

main = do
  _ <- getLine
  xs <- parse <$> getContents
  print . maximum . map snd $ xs

import Control.Applicative
import Data.List (group)

parse :: String -> [(Int,Int)]
parse = map (toTup . words) . lines
  where toTup [x,y] = (read x, read y)

main = do
  _ <- getLine
  xs <- parse <$> getContents
  print . maximum . map length . group $ xs

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

main=do
   getLine
   a<-map (map read) <$> map words <$> lines <$> getContents ::IO [[Int]]
   print $ maximum $ map length $ group $ sort $ map (\[x,y]->x*60+y) a

import Data.List (group, sort)

main :: IO ()
main = getContents >>= print . maximum . map length . group . sort . tail . lines

import Data.List (group)
main = print . maximum . map length . group . tail . lines =<< getContents

import Control.Monad
import Data.List
main = do
    n <- getLine
    times <- forM [1..read n] (\_ -> do
        s <- getLine
        let [h, m] = map read . words $ s
        return (h, m) )
    print $ solve times

solve :: [(Int,Int)] -> Int
solve = maximum . map length . group

import Data.List
import Data.Ord
main=interact$show.length.maximumBy(comparing length).group.f.map read.tail.words
f :: [Int] -> [(Int,Int)]
f(x:y:z)=(x,y):f z
f _=[]

import List
main=interact$show.maximum.map length.group.lines

import System.IO
import Data.List
solve c = show (maximum (map (length) (group [(x,y) | x:y:[] <- [[read t :: Int | t <- words l] | l <- tail c]])))

main = do
    c <- hGetContents stdin
    putStrLn (solve (lines c))

import Data.List

main = interact $ show . func . tup . tail . words

tup (x:y:xs) = (x,y):tup(xs)
tup _ = []

func = maximum . map length . group

import Control.Applicative
import Data.List

main=do

	n<-  read  <$> getLine::IO Int
	q<- map (map read) . map words. lines <$> getContents::IO [[Int]]
	let q1 = map (\z-> (head z)*60 + (last z)) q
	print $ maximum $ map length $ group $sort q1

{-
A. Free Cash
=============
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Valera runs a 24/7 fast food cafe. He magically learned that next day n people will visit his cafe. For each person we know the arrival time: the i-th person comes exactly at hi hours mi minutes. The cafe spends less than a minute to serve each client, but if a client comes in and sees that there is no free cash, than he doesn't want to wait and leaves the cafe immediately.

Valera is very greedy, so he wants to serve all n customers next day (and get more profit). However, for that he needs to ensure that at each moment of time the number of working cashes is no less than the number of clients in the cafe.

Help Valera count the minimum number of cashes to work at his cafe next day, so that they can serve all visitors.

Input
------
The first line contains a single integer n (1 ≤ n ≤ 105), that is the number of cafe visitors.

Each of the following n lines has two space-separated integers hi and mi (0 ≤ hi ≤ 23; 0 ≤ mi ≤ 59), representing the time when the i-th person comes into the cafe.

Note that the time is given in the chronological order. All time is given within one 24-hour period.

Output
------
Print a single integer — the minimum number of cashes, needed to serve all clients next day.

Sample test(s)
---------------
input
4
8 0
8 10
8 10
8 45
output
2

input
3
0 12
10 11
22 22
output
1

Note
------
In the first sample it is not enough one cash to serve all clients, because two visitors will come into cafe in 8:10. Therefore, if there will be one cash in cafe, then one customer will be served by it, and another one will not wait and will go away.

In the second sample all visitors will come in different times, so it will be enough one cash.

-}
import Data.List (group)

minimumCashes :: [(Int, Int)] -> Int
minimumCashes = (maximum . map length . group)

reader :: String -> [(Int, Int)]
reader s = [(hh, mm) | [hh, mm] <- yss]
  where _ : xss = lines s
        yss = map (map read . words) xss

main = do
  input <- getContents
  let xxs = reader input
  print $ minimumCashes xxs

import Data.List
import Control.Monad
main = getLine >>= flip replicateM getLine.read >>= putStrLn.show.maximum.map length.group

-- Codeforces Round #147 (Div. 2)
-- A. Free Cash
import Data.Char
import Data.List

repeat' :: [Int] -> Int -> Int -> Int
repeat' [] _ n = n
repeat' a@(x:xs) c n | x == c = repeat' xs c (n+1)
                     | x /= c = maximum [n, repeat' a x 0]

encode' :: [[Int]] -> [Int]
encode' [] = []
encode' ((x1 : x2 : xs): ys) = x1*60 + x2 : encode' ys

main :: IO()
main = do
  s <- getContents
  let inp = map (map $ \x -> read x :: Int) $ map words $ lines s
      code = encode' $ tail inp
      out = repeat' code (-1) 0
  print out

-- Codeforces Round #147 (Div. 2)
-- A. Free Cash
import Data.Char
import Data.List

repeat' :: [Int] -> Int -> Int -> Int
repeat' [] _ n = n
repeat' a@(x:xs) c n | x == c = repeat' xs c (n+1)
                     | x /= c = maximum [n, repeat' a x 0]

encode' :: [[Int]] -> [Int]
encode' [] = []
encode' ((x1 : x2 : xs): ys) = x1*60 + x2 : encode' ys

main :: IO()
main = do
  s <- getContents
  let inp = map (map $ \x -> read x :: Int) $ map words $ lines s
      code = sort $ encode' $ tail inp
      out = repeat' code (-1) 0
  print out

import Control.Applicative
import Control.Monad
import Data.List
main = do
    n <- read <$> getLine
    ps <- replicateM n ((map read . words) <$> getLine) :: IO [[Int]]
    let ans = maximum . map length . group $ ps
    print ans

import Data.List
main = interact $ show.maximum.map length.group.map (words).tail.lines

main = do
    n<-getLine
    rest<-getContents
    let dat=((map (map read.words)).lines) rest
    putStrLn $ show $ solve dat 1

solve::[[Int]]->Int->Int
solve [] maxhumans = maxhumans
solve ([a,b]:cs) maxhumans = if taken >maxhumans then solve droped (taken+1) else solve droped maxhumans
     where taken =length (  takeWhile ([a,b]==) cs)
           droped = dropWhile ([a,b]==) cs