{-# LANGUAGE TypeFamilies, UndecidableInstances, FlexibleInstances, MultiParamTypeClasses, ScopedTypeVariables, UnboxedTuples #-}
import Data.Ord
import Data.Int
import Data.Foldable
import Data.List
import Data.Maybe
import Data.Either
import Data.Array.ST.Safe
import Data.Array.Unboxed
import Control.Monad
import Control.Monad.ST.Safe
import Control.Monad.Trans.Except
import Control.Monad.Trans.Class
import Text.Read (readEither)

getToken :: IO String
getToken =
    let aux s = do
        x <- getChar
        if x == ' ' || x == '\t' || x == '\n' then
            if s then
                aux s
            else
                return id
        else
            liftM ((.) $ showChar x) $ aux False
    in liftM (flip ($) []) $ aux True

type EIO a = ExceptT String IO a

data HNormal
data HString
data HList

class IOInteract a where
    convertEither :: String -> Either String a
    toShowS :: a -> String -> String
    toShow :: a -> String
    toShow x = toShowS x []

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

type family IOType x where
    IOType String = HString
    IOType [x] = HList
    IOType x = HNormal

class IOInteractp flag a where
    convertEitherp :: flag -> String -> Either String a
    toShowSp :: flag -> a -> String -> String

instance (Show a, Read a) => IOInteractp HNormal a where
    convertEitherp _ x = readEither x
    toShowSp _ x = shows x

instance IOInteractp HString String where
    convertEitherp _ x = Right x
    toShowSp _ x = showString x

instance (IOInteract a) => IOInteractp HList [a] where
    convertEitherp _ x = mapM convertEither $ words x
    toShowSp _ [] = id
    toShowSp _ (x : []) = toShowS x
    toShowSp dum (x : xs) = toShowS x . showChar ' ' . toShowSp dum xs

get :: IOInteract a => EIO a
get = ExceptT $ liftM convertEither getToken
put :: IOInteract a => a -> EIO ()
put = lift . putStr . toShow
putln :: IOInteract a => a -> EIO ()
putln = lift . putStrLn . toShow

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

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

f1 :: EIO ()
f1 = do
    (a, b, c) <- liftM3 (,,) get get get
    let (n, x, y, z) = f2 a b c in
        do
            putln n
            putln [x, y, z]

f2 :: Int -> Int -> Int -> (Int, Int, Int, Int)
f2 a b c = foldl (\(n, x, y, z) i ->
        foldl (\(n, x, y, z) j ->
        foldl (\(n, x, y, z) k ->
            let s = (abs (i - a)) + (abs (j - b)) + (abs (k - c)) in
            if s < n then (s, i, j, k) else (n, x, y, z)
        ) (n, x, y, z) [j,2*j..11000]
        ) (n, x, y, z) [i,2*i..11000]
    ) (1000000, 0, 0, 0) [1..10000]

{-# LANGUAGE TypeFamilies, UndecidableInstances, FlexibleInstances, MultiParamTypeClasses, ScopedTypeVariables, UnboxedTuples #-}
import Data.Ord
import Data.Int
import Data.Foldable
import Data.List
import Data.Maybe
import Data.Either
import Data.Array.ST.Safe
import Data.Array.Unboxed
import Control.Monad
import Control.Monad.ST.Safe
import Control.Monad.Trans.Except
import Control.Monad.Trans.Class
import Text.Read (readEither)

getToken :: IO String
getToken =
    let aux s = do
        x <- getChar
        if x == ' ' || x == '\t' || x == '\n' then
            if s then
                aux s
            else
                return id
        else
            liftM ((.) $ showChar x) $ aux False
    in liftM (flip ($) []) $ aux True

type EIO a = ExceptT String IO a

data HNormal
data HString
data HList

class IOInteract a where
    convertEither :: String -> Either String a
    toShowS :: a -> String -> String
    toShow :: a -> String
    toShow x = toShowS x []

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

type family IOType x where
    IOType String = HString
    IOType [x] = HList
    IOType x = HNormal

class IOInteractp flag a where
    convertEitherp :: flag -> String -> Either String a
    toShowSp :: flag -> a -> String -> String

instance (Show a, Read a) => IOInteractp HNormal a where
    convertEitherp _ x = readEither x
    toShowSp _ x = shows x

instance IOInteractp HString String where
    convertEitherp _ x = Right x
    toShowSp _ x = showString x

instance (IOInteract a) => IOInteractp HList [a] where
    convertEitherp _ x = mapM convertEither $ words x
    toShowSp _ [] = id
    toShowSp _ (x : []) = toShowS x
    toShowSp dum (x : xs) = toShowS x . showChar ' ' . toShowSp dum xs

get :: IOInteract a => EIO a
get = ExceptT $ liftM convertEither getToken
put :: IOInteract a => a -> EIO ()
put = lift . putStr . toShow
putln :: IOInteract a => a -> EIO ()
putln = lift . putStrLn . toShow

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

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

f1 :: EIO ()
f1 = do
    (a, b, c) <- liftM3 (,,) get get get
    let (n, x, y, z) = f2 a b c in
        do
            putln n
            putln [x, y, z]

f2 :: Int -> Int -> Int -> (Int, Int, Int, Int)
f2 a b c = f3 a b c 0 1000000 0 0 0

f3 a b c i n x y z = if i >= a || i >= n
    then (n, x, y, z)
    else
        let (n1, x1, y1, z1) = f4 a b c (a + i) 0 n x y z in
        let (n2, x2, y2, z2) = f4 a b c (a - i) 0 n1 x1 y1 z1 in
        f3 a b c (i + 1) n2 x2 y2 z2

f4 a b c ai j n x y z = foldl (\(n, x, y, z) j ->
            let c1 = (div c j) * j in
            let c2 = (div c j) * j + j in
            let s1 = (abs (ai - a) + abs (j - b) + abs (c1 - c)) in
            let (n1, x1, y1, z1) = if s1 < n then (s1, ai, j, c1) else (n, x, y, z) in
            let s2 = (abs (ai - a) + abs (j - b) + abs (c2 - c)) in
            if s2 < n1 then (s2, ai, j, c2) else (n1, x1, y1, z1)
        ) (n, x, y, z) [ai,2*ai..2*b]

{-# LANGUAGE TypeFamilies, UndecidableInstances, FlexibleInstances, MultiParamTypeClasses, ScopedTypeVariables, UnboxedTuples #-}
import Data.Ord
import Data.Int
import Data.Foldable
import Data.List
import Data.Maybe
import Data.Either
import Data.Array.ST.Safe
import Data.Array.Unboxed
import Control.Monad
import Control.Monad.ST.Safe
import Control.Monad.Trans.Except
import Control.Monad.Trans.Class
import Text.Read (readEither)

getToken :: IO String
getToken =
    let aux s = do
        x <- getChar
        if x == ' ' || x == '\t' || x == '\n' then
            if s then
                aux s
            else
                return id
        else
            liftM ((.) $ showChar x) $ aux False
    in liftM (flip ($) []) $ aux True

type EIO a = ExceptT String IO a

data HNormal
data HString
data HList

class IOInteract a where
    convertEither :: String -> Either String a
    toShowS :: a -> String -> String
    toShow :: a -> String
    toShow x = toShowS x []

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

type family IOType x where
    IOType String = HString
    IOType [x] = HList
    IOType x = HNormal

class IOInteractp flag a where
    convertEitherp :: flag -> String -> Either String a
    toShowSp :: flag -> a -> String -> String

instance (Show a, Read a) => IOInteractp HNormal a where
    convertEitherp _ x = readEither x
    toShowSp _ x = shows x

instance IOInteractp HString String where
    convertEitherp _ x = Right x
    toShowSp _ x = showString x

instance (IOInteract a) => IOInteractp HList [a] where
    convertEitherp _ x = mapM convertEither $ words x
    toShowSp _ [] = id
    toShowSp _ (x : []) = toShowS x
    toShowSp dum (x : xs) = toShowS x . showChar ' ' . toShowSp dum xs

get :: IOInteract a => EIO a
get = ExceptT $ liftM convertEither getToken
put :: IOInteract a => a -> EIO ()
put = lift . putStr . toShow
putln :: IOInteract a => a -> EIO ()
putln = lift . putStrLn . toShow

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

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

f1 :: EIO ()
f1 = do
    (a, b, c) <- liftM3 (,,) get get get
    let (n, x, y, z) = f2 a b c in
        do
            putln n
            putln [x, y, z]

f2 :: Int -> Int -> Int -> (Int, Int, Int, Int)
f2 a b c = foldl (\(n, x, y, z) i ->
        foldl (\(n, x, y, z) j ->
            let c1 = (div c j) * j in
            let c2 = (div c j) * j + j in
            let s1 = (abs (i - a) + abs (j - b) + abs (c1 - c)) in
            let (n1, x1, y1, z1) = if s1 < n then (s1, i, j, c1) else (n, x, y, z) in
            let s2 = (abs (i - a) + abs (j - b) + abs (c2 - c)) in
            if s2 < n1 then (s2, i, j, c2) else (n1, x1, y1, z1)
        ) (n, x, y, z) [i,2*i..2*b]
    ) (1000000, 0, 0, 0) [1..2*a]

{-# LANGUAGE TypeFamilies, UndecidableInstances, FlexibleInstances, MultiParamTypeClasses, ScopedTypeVariables, UnboxedTuples #-}
import Data.Ord
import Data.Int
import Data.Foldable
import Data.List
import Data.Maybe
import Data.Either
import Data.Array.ST.Safe
import Data.Array.Unboxed
import Control.Monad
import Control.Monad.ST.Safe
import Control.Monad.Trans.Except
import Control.Monad.Trans.Class
import Text.Read (readEither)

getToken :: IO String
getToken =
    let aux s = do
        x <- getChar
        if x == ' ' || x == '\t' || x == '\n' then
            if s then
                aux s
            else
                return id
        else
            liftM ((.) $ showChar x) $ aux False
    in liftM (flip ($) []) $ aux True

type EIO a = ExceptT String IO a

data HNormal
data HString
data HList

class IOInteract a where
    convertEither :: String -> Either String a
    toShowS :: a -> String -> String
    toShow :: a -> String
    toShow x = toShowS x []

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

type family IOType x where
    IOType String = HString
    IOType [x] = HList
    IOType x = HNormal

class IOInteractp flag a where
    convertEitherp :: flag -> String -> Either String a
    toShowSp :: flag -> a -> String -> String

instance (Show a, Read a) => IOInteractp HNormal a where
    convertEitherp _ x = readEither x
    toShowSp _ x = shows x

instance IOInteractp HString String where
    convertEitherp _ x = Right x
    toShowSp _ x = showString x

instance (IOInteract a) => IOInteractp HList [a] where
    convertEitherp _ x = mapM convertEither $ words x
    toShowSp _ [] = id
    toShowSp _ (x : []) = toShowS x
    toShowSp dum (x : xs) = toShowS x . showChar ' ' . toShowSp dum xs

get :: IOInteract a => EIO a
get = ExceptT $ liftM convertEither getToken
put :: IOInteract a => a -> EIO ()
put = lift . putStr . toShow
putln :: IOInteract a => a -> EIO ()
putln = lift . putStrLn . toShow

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

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

f1 :: EIO ()
f1 = do
    (a, b, c) <- liftM3 (,,) get get get
    let (n, x, y, z) = f2 a b c in
        do
            putln n
            putln [x, y, z]

f2 :: Int -> Int -> Int -> (Int, Int, Int, Int)
f2 a b c = f3 a b c 0 1000000 0 0 0

f3 a b c i n x y z = if i >= a || i >= n
    then (n, x, y, z)
    else
        let (n1, x1, y1, z1) = f4 a b c (a + i) 0 n x y z in
        let (n2, x2, y2, z2) = f4 a b c (a - i) 0 n1 x1 y1 z1 in
        f3 a b c (i + 1) n2 x2 y2 z2

f4 a b c ai j n x y z = let bk = (div b ai) in
    if ((bk + j) * ai >= 2 * b && bk <= j) || (
        (abs (ai - a) + abs ((bk + j) * ai - b)) > n &&
        (abs (ai - a) + abs ((bk - j) * ai - b)) > n &&
        (abs (ai - a) + abs ((bk + j + 1) * ai - b)) > n &&
        (abs (ai - a) + abs ((bk - j - 1) * ai - b)) > n) then (n, x, y, z)
    else
        let (n1, x1, y1, z1) = f5 a b c ai (max ((bk + j) * ai) 1) n x y z in
        let (n2, x2, y2, z2) = f5 a b c ai (max ((bk - j) * ai) 1) n1 x1 y1 z1 in
        f4 a b c ai (j + 1) n2 x2 y2 z2

f5 a b c ai bi n x y z =
            let c1 = (div c bi) * bi in
            let c2 = (div c bi) * bi + bi in
            let s1 = (abs (ai - a) + abs (bi - b) + abs (c1 - c)) in
            let (n1, x1, y1, z1) = if s1 < n then (s1, ai, bi, c1) else (n, x, y, z) in
            let s2 = (abs (ai - a) + abs (bi - b) + abs (c2 - c)) in
            if s2 < n1 then (s2, ai, bi, c2) else (n1, x1, y1, z1)


import qualified Data.ByteString.Char8 as C

main :: IO ()
main = do
  a <- C.getLine
  b <- C.getLine
  if C.length a == C.length b && C.elem '1' a == C.elem '1' b
    then putStrLn "YES"
    else putStrLn "NO"

import qualified Data.ByteString.Char8 as B

main :: IO ()
main = putStrLn . solve . B.lines =<< B.getContents

solve :: [B.ByteString] -> String
solve (a : b : _)
  | B.length a == B.length b && B.elem '1' a == B.elem '1' b = "YES"
  | otherwise = "NO"

solve :: String -> String -> Bool
solve a b
  | length a /= length b  =  False
  | elem '1' a && elem '1' b  =  True
  | elem '1' a || elem '1' b  =  False
  | otherwise  =  True

main :: IO ()
main  =  do
    a  <-  getLine
    b  <-  getLine
    if solve a b then
        putStrLn "YES"
    else
        putStrLn "NO"

import Data.List

main = do
	start <- getLine
	end <- getLine
	putStrLn.format.not $ unconvertable start end

unconvertable a b =
	length a /= length b
	|| any (=='1') a && all (=='0') b
	|| (length a == 1 || all (=='0') a) && a /= b

format True = "YES"
format False = "NO"

import qualified Data.ByteString.Char8 as B

main=B.getContents>>=putStr.solve.B.words

solve[x,y]
  | B.length x == B.length y && B.elem '1' x == B.elem '1' y = "YES"
  | otherwise = "NO"

import Data.Char

main = interact $ solve . lines
    where
        solve (a:b:[])
            | (length a == length b) && scons (convert a) (convert b) = "YES"
            | otherwise = "NO"
        convert = foldl max 0 . map digitToInt
        scons 0 0 = True
        scons 1 1 = True
        scons _ _ = False

import Data.Char

main = interact $ solve . lines
    where
        solve (a:b:[])
            | (length a == length b) && (elem '1' a == elem '1' b) = "YES"
            | otherwise = "NO"

import Data.List

main = do
	start <- getLine
	end <- getLine
	putStrLn.format.not $ unconvertable start end

unconvertable a b =
	length a /= length b
	|| (any (=='1') a && all (=='0') b)
	|| length a == 1
	||  (all (=='0') a && a /= b)

format True = "YES"
format False = "NO"


{-# LANGUAGE TypeApplications #-}

import Data.List (dropWhile, dropWhileEnd)
import Data.Char (isUpper, isLower)

main :: IO ()
main = do
  str <- dropWhileEnd isLower . dropWhile isUpper <$> getLine
  let uppers = length (filter isUpper str)
  print $ solve uppers uppers str

solve :: Int -> Int -> String -> Int
solve changes acc [] = minimum [acc, changes]
solve changes acc (x:xs) | isUpper x = solve (pred changes) (minimum [acc, pred changes]) xs
                         | otherwise = solve (succ changes) acc xs

import Data.Char

main = do
	s <- getLine

	let cs =  scanl (+) 0 $ map ((\b -> if b then 1 else 0) . isUpper) s
	print $ minimum $ map (\(c, i) -> (i-c) + ((last cs) - c)) $ zip cs [0..length s]

import Data.Char

main = do
	s <- getLine
	let cs = scanl (+) 0 $ map (fromEnum . isAsciiUpper) s
	print $ minimum $ map (\(i, c) -> (i-c) + ((last cs) - c)) $ zip [0..length s] cs

{-# LANGUAGE TypeApplications #-}

import Data.List (dropWhile, dropWhileEnd)
import Data.Char (isUpper, isLower)

main :: IO ()
main = do
  str <- dropWhileEnd isLower . dropWhile isUpper <$> getLine
  let
    lowers = length (filter isLower str)
    uppers = length (filter isUpper str)
  print $ minimum [lowers, uppers]


{-# 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 (second)
import Data.List (sort)

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

type Level = [Int]

testCase :: Scanner [Level]
testCase = numberOf (numberOf int)

solve :: [Level] -> Int
solve = (1 +) . maximum . uncurry (zipWith (-)) . second (scanl (+) 0) . unzip . sort . map info
  where
    info as = (maximum $ zipWith (-) as [0..], length as)

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


{-# LANGUAGE BangPatterns #-}

import Data.Bits
import Data.List
import qualified Data.ByteString.Char8 as BS
{-
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IntMap
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
-}
import Control.Monad
import Control.Applicative
import Data.Maybe
import Data.Tuple
import Data.Ord

main :: IO ()
main = do
  [n,k] <- map read . words <$> getLine :: IO [Int]
  as <- unfoldr (BS.readInt . BS.dropWhile (==' ')) <$> BS.getLine :: IO [Int]
  let as' = sort as
      subs = dropWhile (== 0) $ zipWith (-) as' (0:as')
      rep = map head' $ iterate (\xs -> remv xs (head' xs)) subs
  mapM_ print $ take k rep

remv :: [Int] -> Int -> [Int]
remv [] !y = []
remv xs'@(!x:xs) !y
  | x > y = (x-y):xs
  | otherwise = remv xs (y-x)

head' :: [Int] -> Int
head' [] = 0
head' (x:_) = x

import Control.Monad
import qualified Data.Set as S
main = do
  [n,k] <- getLine >>= return . map (read :: String -> Int) . words
  as' <- getLine >>= return . S.fromList . map (read :: String -> Int) . words
  let as = S.toList as'
  print $ head as
  mapM_ print $ take (pred k) $ (zipWith (-) (tail as) as)
  mapM_ print $ take (k-length as) $ cycle [0]

import Data.List

main = do
    string <- getContents
    let answers = solve string
    sequence (map (putStrLn.show) answers)

solve string = let n = read sn :: Int; k = read sk :: Int in computeAnswer k [read s :: Int | s <- rest]
	       where (sn:sk:rest) = words string

computeAnswer k a = let b = sort a in getK k$head b:zipWith (-) (tail b) b
getK 0 _ = []
getK k [] = replicate k 0
getK k (x:xs) | x == 0 = getK k xs
	      | otherwise =  x : getK (k - 1) xs

import Data.List
import qualified Data.Map.Strict as Map

toint s = (read s) :: Int

doit 0 a s = ""
doit k a [] = "0\n" ++ doit (k-1) a []
doit k a (x:xs) =
  if x > a then
    show(x-a)++"\n"++(doit (k-1) x xs)
  else
    doit k a xs

solve::String -> String
solve ss =
  let n:k:sss = map toint $ words ss in
  let s = sort $ take n sss in
  doit k 0 s

main = do
  interact $ solve


import Data.List
import qualified Data.ByteString.Lazy.Char8 as L
import GHC.Show (intToDigit)
import GHC.Exts
import GHC.Float
import Data.Char (isDigit)

readInt :: L.ByteString -> Int
readInt x =
  case L.readInt x of Just (i,_) -> i
                      Nothing    -> error "Unparsable Int"

readInteger :: Num a => L.ByteString -> a
readInteger x =
  case L.readInteger x of Just (i,_) -> fromIntegral i
                          Nothing    -> error "Unparsable Int"

readDouble :: L.ByteString -> Double
readDouble s = let [integer, fractional] = L.split '.' s
                   i = if L.null integer then 0 else readInteger integer
                   f = if L.null fractional then 0 else readInteger fractional
               in  if not (L.elem '.' s)
                     then readInteger s
                     else i + 0.1 ^ L.length (L.takeWhile isDigit fractional) * f

main = do str <- L.getContents
          let l = L.lines str
              [n,v] = map readInt . L.words . head $ l
              angles = map readDouble . take n . tail $ l
              m = readInt . head . drop (n+1) $ l
              walls = [(x,y) | s <- take m (drop (n+2) l), let w = L.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          putStrLn (output sol)

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
        | otherwise = reverse (f n)
        where
         f n | n < 10    = [intToDigit (fromIntegral n)]
             | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x = f (round (x * fromIntegral m)) m
    where m = toInteger 10 ^ toInteger prec :: Integer
          f n m | n < 0     = '-' : f (-n) m
                | otherwise = ishow first ++ ('.' : replicate leadingZeroes '0') ++ secondStr
                where (first, second) = divMod n m
                      secondStr = ishow second
                      leadingZeroes = prec - length secondStr

output = unlines . map (unwords . map (dshow 6))

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

{-# LANGUAGE ForeignFunctionInterface #-}

import qualified Data.ByteString.Lazy.Char8 as LB
import qualified Data.ByteString as SB
import Data.ByteString.Internal (inlinePerformIO)
import Foreign.C.String (CString)
import Foreign.C (CDouble)

import GHC.Exts
import Data.Maybe (fromJust)
import GHC.Float (int2Double)
import Data.List (sort)
import Text.Printf (printf)

foreign import ccall unsafe "stdlib.h atof" c_atof :: CString -> IO Double
unsafeReadDouble = inlinePerformIO . flip SB.useAsCString c_atof
{-# INLINE unsafeReadDouble #-}
readDouble = unsafeReadDouble . SB.concat . LB.toChunks
readInt = fst . fromJust . LB.readInt

main = do str <- LB.getContents
          let l = LB.lines str
              [n,v] = map readInt . LB.words . head $ l
              angles = map readDouble . take n . tail $ l
              m = readInt $ l !! (n+1)
              walls = [(x,y) | s <- take m (drop (n+2) l), let w = LB.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          mapM_ (uncurry (printf "%.6f %.6f\n") ) sol

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map snd res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

import Data.List
import qualified Data.ByteString.Lazy.Char8 as L
import Text.Printf
import GHC.Exts
import GHC.Float
import Data.Char
import Data.Maybe

main = do str <- L.getContents
          let l = L.lines str
              [n,v] = map readInt . L.words . head $ l
              angles = map readDouble . take n . tail $ l
              m = readInt $ l !! (n+1)
              walls = [(x,y) | s <- take m (drop (n+2) l), let w = L.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          mapM_ (uncurry (printf "%.6f %.6f\n") ) sol

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map snd res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

readInt = fst . fromJust . L.readInt
readInteger = fromIntegral . fst . fromJust . L.readInteger
readDouble s =
  let [si, sf] = L.split '.' s
      i = if L.null si then 0 else readInteger si
      f = if L.null sf then 0 else readInteger sf
  in  if not (L.elem '.' s)
        then readInteger s
        else i + 0.1 ^ L.length (L.takeWhile isDigit sf) * f

import Data.List (sort)
import Data.Maybe (fromJust)
import qualified Data.ByteString.Char8 as C

readInt = fst . fromJust . C.readInt

readDouble bs =
  if C.null bs'
    then fromIntegral a
    else fromIntegral (a * c + b) / fromIntegral c
  where
    Just (a, bs') = C.readInt bs
    t = C.tail bs'
    b = readInt t
    c = 10 ^ C.length t

pairs (x:y:z) = (x, y): pairs z
pairs _ = []

main = do
  (n':v':input) <- fmap C.words C.getContents
  let n = readInt n'
      v = readInt v'
      (a',(m':z')) = splitAt n input
  let a = map readDouble a'
      m = readInt m'
      z = map readDouble z'
  putStrLn $ unwords $ map show $ concatMap snd $ sort $
    gao n (fromIntegral v) (zip a [0 ..]) $ pairs z

gao :: Int -> Double -> [(Double, Int)] -> [(Double, Double)] -> [(Int, [Double])]
gao n v as zs = gao' (sort as) (sort zs) where
  eps = 1e-8
  g = 9.8
  gao' [] _ = []
  gao' ((a,i):a') z = (i, xy) : gao' a' z' where
    vx = v * cos(a)
    vy = v * sin(a)
    yt x = let t = x / vx in vy * t - g * t * t / 2
    z' = dropWhile (\(x, y) -> y < yt x + eps) z
    xy =
      if null z' || y < -eps
        then [v * v * sin(2 * a) / g, 0]
        else let x = fst $ head z' in [x, y]
      where
        x = fst $ head z'
        y = yt x

{- Time limit exceeded on test 7 -}
{- PS: if yt x < 0 .... WA -}

import Data.List (sort)
import Data.Maybe (fromJust)
import qualified Data.ByteString.Char8 as C

readInt = fst . fromJust . C.readInt

readDouble bs =
  if C.null bs'
    then fromIntegral a
    else fromIntegral a + fromIntegral b / fromIntegral c
  where
    Just (a, bs') = C.readInt bs
    t = C.tail bs'
    b = readInt t
    c = 10 ^ C.length t

pairs (x:y:z) = (x, y): pairs z
pairs _ = []

main = do
  (n':v':input) <- fmap C.words C.getContents
  let n = readInt n'
      v = readInt v'
      (a',(m':z')) = splitAt n input
  let a = map readDouble a'
      m = readInt m'
      z = map readDouble z'
  putStrLn $ unwords $ map show $ concatMap snd $ sort $
    gao n (fromIntegral v) (zip a [0 ..]) $ pairs z

gao :: Int -> Double -> [(Double, Int)] -> [(Double, Double)] -> [(Int, [Double])]
gao n v as zs = gao' (sort as) (sort zs) where
  eps = 1e-6
  g = 9.8
  gao' [] _ = []
  gao' ((a,i):a') z = (i, xy) : gao' a' z' where
    vx = v * cos(a)
    vy = v * sin(a)
    yt x = let t = x / vx in vy * t - g * t * t / 2
    z' = dropWhile (\(x, y) -> y < yt x + eps) z --WA
    xy = if null z' then [v * v * sin(2 * a) / g, 0] else let x = fst $ head z' in [x, yt x] --WA

{- Time limit exceeded on test 7 -}
{- PS: if yt x < 0 .... WA -}

import Data.List (sort)
import Data.Maybe (fromJust)
import qualified Data.ByteString.Char8 as C

readInt = fst . fromJust . C.readInt

readDouble bs =
  if C.null bs'
    then fromIntegral a
    else fromIntegral (a * c + b) / fromIntegral c
  where
    Just (a, bs') = C.readInt bs
    t = C.tail bs'
    b = readInt t
    c = 10 ^ C.length t

pairs (x:y:z) = (x, y): pairs z
pairs _ = []

main = do
  (n':v':input) <- fmap C.words C.getContents
  let n = readInt n'
      v = readInt v'
      (a',(m':z')) = splitAt n input
  let a = map readDouble a'
      m = readInt m'
      z = map readDouble z'
  putStrLn $ unwords $ map show $ concatMap snd $ sort $
    gao n (fromIntegral v) (zip a [0 ..]) $ pairs z

gao :: Int -> Double -> [(Double, Int)] -> [(Double, Double)] -> [(Int, [Double])]
gao n v as zs = gao' (sort as) (sort zs) where
  eps = 1e-6
  g = 9.8
  gao' [] _ = []
  gao' ((a,i):a') z = (i, xy) : gao' a' z' where
    vx = v * cos(a)
    vy = v * sin(a)
    yt x = let t = x / vx in vy * t - g * t * t / 2
    z' = dropWhile (\(x, y) -> y < yt x + eps) z --WA
    xy =
      if null z' || y < -eps
        then [v * v * sin(2 * a) / g, 0]
        else let x = fst $ head z' in [x, y] --WA
      where
        x = fst $ head z'
        y = yt x

{- Time limit exceeded on test 7 -}
{- PS: if yt x < 0 .... WA -}

{-# LANGUAGE ForeignFunctionInterface #-}

import Data.List
import Data.Maybe (catMaybes, mapMaybe)
import System.Random
import qualified Data.ByteString.Lazy.Char8 as L
import qualified Data.ByteString.Char8 as B
import Foreign.C.Types
import Foreign.C.String
import System.IO.Unsafe
import Text.Printf
import GHC.Show (intToDigit)
--import Data.ByteString.Lex.Lazy.Double (readDouble)

--import qualified Data.Text.Read as T
import GHC.Exts
import GHC.Float

foreign import ccall unsafe "stdlib.h atof" c_atof :: CString -> IO CDouble
readDouble :: B.ByteString -> Double
readDouble = realToFrac . unsafePerformIO . flip B.useAsCString c_atof

main = interact $ output . solve . parse . lines

--main = do let r = mkStdGen 146
--              lst = iterate (\(x,r) -> random r) (int2Double 1, r)
--          putStrLn . unlines . map(show . fst) $ take 200000 lst

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
           | otherwise = reverse (f n)
           where
            f n | n < 10 = [intToDigit (fromIntegral n)]
                | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x | x < 0  = '-' : dshow prec (-x)
dshow prec x = ishow first ++ ('.' : ishow second)
    where m = toInteger 10^ toInteger prec :: Integer
          n = round (x * fromIntegral m)
          (first, second) = divMod n m

--main = interact $ output' . parse' . lines

--parse' = map fst . mapMaybe ( L.readInt . L.pack )
parse' = map (readDouble . B.pack)
--output' = unlines . map (printf "%.3f")
output' = unlines . map (dshow 4)

solve' = takeWhile (/= 42)

output = unlines . map (unwords . map (dshow 9))

parse (nv:rest) = let [n,v] = map read . words $ nv
                      angles = map (readDouble . B.pack) (take n rest)
                      walls = [(x,y) | s <- drop (n+1) rest, let w = words s,
                                let x = read (head w), let y = read (last w)]
                  in (int2Double v, angles, sort walls)

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

import Data.List
import qualified Data.ByteString.Lazy.Char8 as L
import GHC.Show (intToDigit)
import GHC.Exts
import GHC.Float

readInt :: L.ByteString -> Int
readInt x =
  case L.readInt x of Just (i,_) -> i
                      Nothing    -> error "Unparsable Int"

readInteger :: Num a => L.ByteString -> a
readInteger x =
  case L.readInteger x of Just (i,_) -> fromIntegral i
                          Nothing    -> error "Unparsable Int"

readDouble :: L.ByteString -> Double
readDouble s = let [integer, fractional] = L.split '.' s
                   i = if L.null integer then 0 else readInteger integer
                   f = if L.null fractional then 0 else readInteger fractional
               in  if not (L.elem '.' s)
                     then readInteger s
                     else i + 0.1 ^ L.length fractional * f

main = do str <- L.getContents
          let l = L.lines str
              [n,v] = map readInt . L.words . head $ l
              angles = map readDouble . take n . tail $ l
              m = readInt . head . drop (n+1) $ l
              walls = [(x,y) | s <- take m (drop (n+2) l), let w = L.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          putStrLn (output sol)

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
        | otherwise = reverse (f n)
        where
         f n | n < 10    = [intToDigit (fromIntegral n)]
             | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x = f (round (x * fromIntegral m)) m
    where m = toInteger 10^ toInteger prec :: Integer
          f n m | n < 0     = '-' : f (-n) m
                | otherwise = ishow first ++ ('.' : replicate leadingZeroes '0') ++ secondStr
                where (first, second) = divMod n m
                      secondStr = ishow second
                      leadingZeroes = prec - length secondStr

output = unlines . map (unwords . map (dshow 6))

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

{-# LANGUAGE ForeignFunctionInterface #-}

import Data.List
import Data.Maybe (catMaybes, mapMaybe)
import System.Random
import qualified Data.ByteString.Lazy.Char8 as L
import qualified Data.ByteString.Char8 as B
import Foreign.C.Types
import Foreign.C.String
import System.IO.Unsafe
import Text.Printf
import GHC.Show (intToDigit)
--import Data.ByteString.Lex.Lazy.Double (readDouble)

--import qualified Data.Text.Read as T
import GHC.Exts
import GHC.Float

foreign import ccall unsafe "stdlib.h atof" c_atof :: CString -> IO CDouble
readDouble :: B.ByteString -> Double
readDouble = realToFrac . unsafePerformIO . flip B.useAsCString c_atof

main = interact $ output . solve . parse . lines

--main = do let r = mkStdGen 146
--              lst = iterate (\(x,r) -> random r) (int2Double 1, r)
--          putStrLn . unlines . map(show . fst) $ take 200000 lst

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
           | otherwise = reverse (f n)
           where
            f n | n < 10 = [intToDigit (fromIntegral n)]
                | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x | x < 0  = '-' : dshow prec (-x)
dshow prec x = ishow first ++ ('.' : ishow second)
    where m = toInteger 10^ toInteger prec :: Integer
          n = round (x * fromIntegral m)
          (first, second) = divMod n m

--main = interact $ output' . parse' . lines

--parse' = map fst . mapMaybe ( L.readInt . L.pack )
parse' = map (readDouble . B.pack)
--output' = unlines . map (printf "%.3f")
output' = unlines . map (dshow 4)

solve' = takeWhile (/= 42)

output = unlines . map (unwords . map (dshow 6))

parse (nv:rest) = let [n,v] = map read . words $ nv
                      angles = map (readDouble . B.pack) (take n rest)
                      walls = [(x,y) | s <- drop (n+1) rest, let w = words s,
                                let x = read (head w), let y = read (last w)]
                  in (int2Double v, angles, sort walls)

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

{-# LANGUAGE ForeignFunctionInterface #-}

import qualified Data.ByteString.Lazy.Char8 as LB
import qualified Data.ByteString as SB
import Data.ByteString.Internal (inlinePerformIO)
import Foreign.C.String (CString)
import Foreign.C (CDouble)
import Data.Maybe (fromJust)

foreign import ccall unsafe "stdlib.h atof" c_atof :: CString -> IO Double
unsafeReadDouble = inlinePerformIO . flip SB.useAsCString c_atof
{-# INLINE unsafeReadDouble #-}
readDouble = unsafeReadDouble . SB.concat . LB.toChunks
readInt = fst . fromJust . LB.readInt

main = LB.getContents >>= (print . sum . map readDouble . LB.lines)

import Data.List
import qualified Data.ByteString.Lazy.Char8 as L
import GHC.Show (intToDigit)
import GHC.Exts
import GHC.Float

readInt :: L.ByteString -> Int
readInt x =
  case L.readInt x of Just (i,_) -> i
                      Nothing    -> error "Unparsable Int"

readInteger :: L.ByteString -> Integer
readInteger x =
  case L.readInteger x of Just (i,_) -> i
                          Nothing    -> error "Unparsable Int"

readDouble :: L.ByteString -> Double
readDouble s = let [integer, fractional] = L.split '.' s
               in fromIntegral (readInteger integer) +
                    0.1 ^ L.length fractional * fromIntegral (readInteger fractional)

main = do str <- L.getContents
          let l = L.lines str
              [n,v] = map readInt . L.words . head $ l
              angles = map readDouble (take n (tail l))
              walls = [(x,y) | s <- drop (n+2) l, let w = L.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          putStr (output sol)

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
        | otherwise = reverse (f n)
        where
         f n | n < 10 = [intToDigit (fromIntegral n)]
             | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x = f (round (x * fromIntegral m)) m
    where m = toInteger 10^ toInteger prec :: Integer
          f n m | n < 0 = '-' : f (-n) m
                | otherwise = ishow first ++ ('.' : replicate leadingZeroes '0') ++ secondStr
                where (first, second) = divMod n m
                      secondStr = ishow second
                      leadingZeroes = prec - length secondStr

output = unlines . map (unwords . map (dshow 6))

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

import Data.List
import qualified Data.ByteString.Lazy.Char8 as L
import GHC.Show (intToDigit)
import GHC.Exts
import GHC.Float

readInt :: L.ByteString -> Int
readInt x =
  case L.readInt x of Just (i,_) -> i
                      Nothing    -> error "Unparsable Int"

readInteger :: L.ByteString -> Integer
readInteger x =
  case L.readInteger x of Just (i,_) -> i
                          Nothing    -> error "Unparsable Int"

readDouble :: L.ByteString -> Double
readDouble s = let [integer, fractional] = L.split '.' s
               in fromIntegral (readInteger integer) +
                    0.1 ^ L.length fractional * fromIntegral (readInteger fractional)

main = do str <- L.getContents
          let l = L.lines str
              [n,v] = map readInt . L.words . head $ l
              angles = map readDouble . take n . tail $ l
              walls = [(x,y) | s <- drop (n+2) l, let w = L.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          putStr (output sol)

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
        | otherwise = reverse (f n)
        where
         f n | n < 10 = [intToDigit (fromIntegral n)]
             | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x = f (round (x * fromIntegral m)) m
    where m = toInteger 10^ toInteger prec :: Integer
          f n m | n < 0 = '-' : f (-n) m
                | otherwise = ishow first ++ ('.' : replicate leadingZeroes '0') ++ secondStr
                where (first, second) = divMod n m
                      secondStr = ishow second
                      leadingZeroes = prec - length secondStr

output = unlines . map (unwords . map show)

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

import Data.List
import qualified Data.ByteString.Lazy.Char8 as L
import GHC.Show (intToDigit)
import GHC.Exts
import GHC.Float

readInt :: L.ByteString -> Int
readInt x =
  case L.readInt x of Just (i,_) -> i
                      Nothing    -> error "Unparsable Int"

readInteger :: L.ByteString -> Integer
readInteger x =
  case L.readInteger x of Just (i,_) -> i
                          Nothing    -> error "Unparsable Int"

readDouble :: L.ByteString -> Double
readDouble s = let [integer, fractional] = L.split '.' s
               in fromIntegral (readInteger integer) +
                    0.1 ^ L.length fractional * fromIntegral (readInteger fractional)

main = do str <- L.getContents
          let l = L.lines str
              [n,v] = map readInt . L.words . head $ l
              angles = map readDouble . take n . tail $ l
              walls = [(x,y) | s <- drop (n+2) l, let w = L.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          putStrLn (output sol)

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
        | otherwise = reverse (f n)
        where
         f n | n < 10 = [intToDigit (fromIntegral n)]
             | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x = f (round (x * fromIntegral m)) m
    where m = toInteger 10^ toInteger prec :: Integer
          f n m | n < 0 = '-' : f (-n) m
                | otherwise = ishow first ++ ('.' : replicate leadingZeroes '0') ++ secondStr
                where (first, second) = divMod n m
                      secondStr = ishow second
                      leadingZeroes = prec - length secondStr

output = unlines . map (unwords . map show)

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

import Data.List
import qualified Data.ByteString.Lazy.Char8 as L
import GHC.Show (intToDigit)
import GHC.Exts
import GHC.Float

readInt :: L.ByteString -> Int
readInt x =
  case L.readInt x of Just (i,_) -> i
                      Nothing    -> error "Unparsable Int"

readInteger :: Num a => L.ByteString -> a
readInteger x =
  case L.readInteger x of Just (i,_) -> fromIntegral i
                          Nothing    -> error "Unparsable Int"

readDouble :: L.ByteString -> Double
readDouble s = let [integer, fractional] = L.split '.' s
                   i = if L.null integer then 0 else readInteger integer
                   f = if L.null fractional then 0 else readInteger fractional
               in i + 0.1 ^ L.length fractional * f

main = do str <- L.getContents
          let l = L.lines str
              [n,v] = map readInt . L.words . head $ l
              angles = map readDouble . take n . tail $ l
              m = readInt . head . drop (n+1) $ l
              walls = [(x,y) | s <- take m (drop (n+2) l), let w = L.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          putStrLn (output sol)

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
        | otherwise = reverse (f n)
        where
         f n | n < 10    = [intToDigit (fromIntegral n)]
             | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x = f (round (x * fromIntegral m)) m
    where m = toInteger 10^ toInteger prec :: Integer
          f n m | n < 0     = '-' : f (-n) m
                | otherwise = ishow first ++ ('.' : replicate leadingZeroes '0') ++ secondStr
                where (first, second) = divMod n m
                      secondStr = ishow second
                      leadingZeroes = prec - length secondStr

output = unlines . map (unwords . map (dshow 6))

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

import Data.List
import qualified Data.ByteString.Lazy.Char8 as L
import GHC.Show (intToDigit)
import GHC.Exts
import GHC.Float

readInt :: L.ByteString -> Int
readInt x =
  case L.readInt x of Just (i,_) -> i
                      Nothing    -> error "Unparsable Int"

readInteger :: L.ByteString -> Integer
readInteger x =
  case L.readInteger x of Just (i,_) -> i
                          Nothing    -> error "Unparsable Int"

readDouble :: L.ByteString -> Double
readDouble s = let [integer, fractional] = L.split '.' s
               in fromIntegral (readInteger integer) +
                    0.1 ^ L.length fractional * fromIntegral (readInteger fractional)

main = do str <- L.getContents
          let l = L.lines str
              [n,v] = map readInt . L.words . head $ l
              angles = map readDouble . take n . tail $ l
              m = readInt . head . drop (n+1) $ l
              walls = [(x,y) | s <- take m (drop (n+2) l), let w = L.words s,
                        let x = readDouble (head w), let y = readDouble (last w)]
              sol = solve (int2Double v, angles, sort walls)
          putStrLn (output sol)

ishow :: Integral a => a -> String
ishow n | n < 0  = '-':ishow (-n)
        | otherwise = reverse (f n)
        where
         f n | n < 10 = [intToDigit (fromIntegral n)]
             | otherwise = intToDigit (fromIntegral (mod n 10)) : f (div n 10)

dshow :: Int -> Double -> String
dshow prec x = f (round (x * fromIntegral m)) m
    where m = toInteger 10^ toInteger prec :: Integer
          f n m | n < 0 = '-' : f (-n) m
                | otherwise = ishow first ++ ('.' : replicate leadingZeroes '0') ++ secondStr
                where (first, second) = divMod n m
                      secondStr = ishow second
                      leadingZeroes = prec - length secondStr

output = unlines . map (unwords . map show)

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res     = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0    = freeFall v (n, a) : doSolve v as walls
 | y1 > y    = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as walls
 where y1 = v * t * sin a - g * t * t / 2.0
       t  = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

import Data.List
import GHC.Exts
import GHC.Float

main = interact $ output . solve . parse . lines

output = unlines . map (unwords . map show)

parse (nv:rest) = let [n,v] = map read . words $ nv
                      angles = map read (take n rest)
                      walls = [(x,y) | s <- drop (n+1) rest, let w = words s,
                                let x = read (head w), let y = read (last w)]
                  in (int2Double v, angles, sortWith fst walls)

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0  = freeFall v (n, a) : doSolve v as walls
 | y1 > y  = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as ws
 where y1 = v * t * sin a - g * t * t / 2.0
       t = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g

import Data.List
import GHC.Exts
import GHC.Float

main = interact $ output . solve . parse . lines

output = unlines . map (unwords . map show)

parse (nv:rest) = let [n,v] = map read . words $ nv
                      angles = map read (take n rest)
                      walls = [(x,y) | s <- drop (n+1) rest, let w = words s,
                                let x = read (head w), let y = read (last w)]
                  in (int2Double v, angles, sort walls)

solve (v,angles,walls) = let angles' = sortWith snd $ zip [1..] angles
                             res = sortWith fst $ doSolve v angles' walls
                         in map (\(_, (x,y)) -> [x,y]) res

g = 9.8

doSolve _ [] _ = []
doSolve v angles [] = map (freeFall v) angles
doSolve v angles@((n,a):as) walls@((x,y):ws)
 | y1 < 0  = freeFall v (n, a) : doSolve v as walls
 | y1 > y  = doSolve v angles ws
 | otherwise = (n, (x, y1)) : doSolve v as ws
 where y1 = v * t * sin a - g * t * t / 2.0
       t = x / cos a / v

freeFall v (n, a) = (n, (v * t * cos a, 0.0))
    where t = 2*v*sin a / g


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

import Control.Monad (forM_, forM)
import Data.List (sort)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import Data.Maybe (fromJust, isNothing)
import Data.Set (fromList, difference, size)

readint = fst. fromJust . C.readInteger

run :: Integer -> Integer -> Integer -> [Integer] -> Maybe Int
run 0 q l as | 0 `elem` as = Just 0
             | otherwise = Nothing
run b 0 l as | 0 `elem` as && b `elem` as = Just 0
             | 0 `elem` as && not (b `elem` as) = if abs b <= l then Just 1 else Just 0
             | not (0 `elem` as) && abs b > l = Just 0
             | otherwise = Nothing
run b 1 l as | b `elem` as || abs b > l = Just 0
             | otherwise   = Nothing
run b (-1) l as | not (b `elem` as) && abs b <= l    = Nothing
                | not ((-b) `elem` as) && abs b <= l = Nothing
                | otherwise = Just 0
run b q l as = Just $ size $ difference (fromList lb) (fromList as) where
    lb = filter ((<= l) . abs) (until ((> l) . abs . last) (\x -> x ++ [last x * q]) [b])

main = do
    (map readint . C.words -> [b, q, l, m]) <- B.getLine
    (take (fromIntegral m) . map readint . C.words -> as) <- B.getLine
    let rbq = run b q l as
    putStrLn $ if isNothing rbq then "inf" else show (fromJust rbq)

import qualified Data.ByteString.Char8 as BS
import           Data.List             (unfoldr)

main = do
  [b,q,l,_m] <- getInts
  allow <-fmap ((not.) .flip elem) getInts
  let good num = abs num <= l && allow num
      inf num | good num = "inf"
              | otherwise = "0"
      result | abs b > l = "0"
             | q == 0 && good 0 = "inf"
             | q == 0 && good b = "1"
             | q == 0           = "0"
             | q == 1  = inf b
             | q == -1 = inf b `max` inf (-b)
             | b == 0  = inf 0
             | otherwise = show $ length $ filter allow $ takeWhile ((<= l).abs) $ iterate (* q) b
  putStrLn result
  where
  getInts = fmap (unfoldr readOne) BS.getLine where
    readOne s = do
      (x , s') <- BS.readInteger s
      return (x, BS.drop 1 s')

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

main :: IO()
main = putStrLn. toString . solve' . map read.words =<< getContents

solve' :: [Int64] -> Int64
solve' (b:1:l:m:xs) = if (abs b>l) then 0 else if ((fst $ findPos b (sort xs))==0) then 0 else 2000000000
solve' (0:_:l:m:xs) = if ((fst $ findPos 0 (sort xs))==0) then 0 else 2000000000
solve' (b:(-1):l:m:xs) = if (abs b >l) then 0 else if (((fst $ findPos b (sort xs))==0) && ((fst $ findPos (-b) (sort xs))==0)) then 0 else 2000000000
solve' (b:0:l:m:xs) =  if (abs b>l) then 0 else let xs'= sort xs
                                                    (c1,_) = findPos b xs'
                                                    (c2,_) = findPos 0 xs'
                                                in case (c1,c2) of
                                                (_,1) -> 2000000000
                                                (1,0) -> 1
                                                (0,0) -> 0

solve' (b:q:l:m:xs) = let (xsPos,xsNeg) = separe xs
                      in solve b q l 0 (sort xsPos) (reverse $ sort xsNeg)

solve b q l c xs xs' = if (b>l || (-1)*b>l) then c else if b>=0 then do let (cm,nl) = findPos b xs
                                                                        solve (b*q) q l (c+cm) nl xs'
                                                        else do let (cm2,nls) = findNeg b xs'
                                                                solve (b*q) q l (c+cm2) xs nls

toString n = if(n==2000000000) then "inf" else show n

separe = foldl (\(p,n) e-> if(e<0) then (p,e:n) else (e:p,n)) ([],[])

findPos _ []  = (1,[])
findPos e (x:xs) = if (x==e) then (0,xs) else if(x>e) then (1,x:xs) else findPos e xs

findNeg _ []  = (1,[])
findNeg e (x:xs) = if (x==e) then (0,xs) else if(x<e) then (1,x:xs) else findNeg e xs

import qualified Data.ByteString.Char8 as BS
import           Data.List             (unfoldr)

main = do
  [b,q,l,_m] <- getInts
  allow <-fmap ((not.) .flip elem) getInts
  let good num = abs num < l && allow num
      inf num | good num = "inf"
              | otherwise = "0"
      result | abs b >= l = "0"
             | q == 0 && good 0 = "inf"
             | q == 0 && good b = "1"
             | q == 0           = "0"
             | q == 1  = inf b
             | q == -1 = inf b `max` inf (-b)
             | b == 0  = inf 0
             | otherwise = show $ length $ filter allow $ takeWhile ((<= l).abs) $ iterate (* q) b
  putStrLn result
  where
  getInts = fmap (unfoldr readOne) BS.getLine where
    readOne s = do
      (x , s') <- BS.readInt s
      return (x, BS.drop 1 s')

import qualified Data.ByteString.Char8 as BS
import           Data.List             (unfoldr)

main = do
  [b,q,l,_m] <- getInts
  allow <-fmap ((not.) .flip elem) getInts
  let good num = abs num <= l && allow num
      inf num | good num = "inf"
              | otherwise = "0"
      result | abs b > l = "0"
             | q == 0 && good 0 = "inf"
             | q == 0 && good b = "1"
             | q == 0           = "0"
             | q == 1  = inf b
             | q == -1 = inf b `max` inf (-b)
             | b == 0  = inf 0
             | otherwise = show $ length $ filter allow $ takeWhile ((<= l).abs) $ iterate (* q) b
  putStrLn result
  where
  getInts = fmap (unfoldr readOne) BS.getLine where
    readOne s = do
      (x , s') <- BS.readInt s
      return (x, BS.drop 1 s')

import qualified Data.ByteString.Char8 as BS
import           Data.List             (unfoldr)

main = do
  [b,q,l,_m] <- getInts
  allow <-fmap ((not.) .flip elem) getInts
  let good num = abs num < l && allow num
      inf num | good num = "inf"
              | otherwise = "0"
      result | abs b >= l = "0"
             | q == 0 && good 0 = "inf"
             | q == 0 && good b = "1"
             | q == 0           = "0"
             | q == 1  = inf b
             | q == -1 = inf b `max` inf (-b)
             | b == 0  = inf 0
             | otherwise = show $ length $ filter allow $ takeWhile ((< l).abs) $ iterate (* q) b
  putStrLn result
  where
  getInts = fmap (unfoldr readOne) BS.getLine where
    readOne s = do
      (x , s') <- BS.readInt s
      return (x, BS.drop 1 s')

import qualified Data.ByteString.Char8 as BS
import           Data.List             (unfoldr)

main = do
  [b,q,l,_m] <- getInts
  allow <-fmap ((not.) .flip elem) getInts
  let good num = abs num < l && allow num
      inf num | good num = "inf"
              | otherwise = "0"
      result | q == 0 && good 0 = "inf"
             | q == 0 && good b = "1"
             | q == 0           = "0"
             | q == 1  = inf b
             | q == -1 = inf b `max` inf (-b)
             | b == 0  = inf 0
             | otherwise = show $ length $ filter allow $ takeWhile ((< l).abs) $ iterate (* q) b
  putStrLn result
  where
  getInts = fmap (unfoldr readOne) BS.getLine where
    readOne s = do
      (x , s') <- BS.readInt s
      return (x, BS.drop 1 s')

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

import Control.Monad (forM_, forM)
import Data.List (sort)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import Data.Maybe (fromJust, isNothing)
import Data.Set (fromList, difference, size)

readint = fromIntegral . fst. fromJust . C.readInt

run :: Integer -> Integer -> Integer -> [Integer] -> Maybe Int
run 0 q l as | 0 `elem` as = Just 0
             | otherwise = Nothing
run b 0 l as | 0 `elem` as && b `elem` as = Just 0
             | 0 `elem` as && not (b `elem` as) = if b <= l then Just 1 else Just 0
             | not (0 `elem` as) = Nothing
run b 1 l as | b `elem` as || b > l = Just 0
             | otherwise   = Nothing
run b (-1) l as | not (b `elem` as) && b <= l       = Nothing
                | not ((-b) `elem` as) && (-b) <= l = Nothing
                | otherwise = Just 0
run b q l as | b < 0 || q < 0 = Nothing
run b q l as = Just $ size $ difference (fromList lb) (fromList as) where
    lb = until ((> l) . last) (\x -> x ++ [last x * q]) [b]

main = do
    (map readint . C.words -> [b, q, l, m]) <- B.getLine
    (map readint . C.words -> as) <- B.getLine
    let rbq = run b q l as
    putStrLn $ if isNothing rbq then "inf" else show (fromJust rbq)

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

import Control.Monad (forM_, forM)
import Data.List (sort)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import Data.Maybe (fromJust, isNothing)
import Data.Set (fromList, difference, size)

readint = fst. fromJust . C.readInteger

run :: Integer -> Integer -> Integer -> [Integer] -> Maybe Int
run 0 q l as | 0 `elem` as = Just 0
             | otherwise = Nothing
run b 0 l as | 0 `elem` as && b `elem` as = Just 0
             | 0 `elem` as && not (b `elem` as) = if abs b <= l then Just 1 else Just 0
             | not (0 `elem` as) && b > l = Just 0
             | otherwise = Nothing
run b 1 l as | b `elem` as || abs b > l = Just 0
             | otherwise   = Nothing
run b (-1) l as | not (b `elem` as) && abs b <= l    = Nothing
                | not ((-b) `elem` as) && abs b <= l = Nothing
                | otherwise = Just 0
run b q l as = Just $ size $ difference (fromList lb) (fromList as) where
    lb = filter ((<= l) . abs) (until ((> l) . abs . last) (\x -> x ++ [last x * q]) [b])

main = do
    (map readint . C.words -> [b, q, l, m]) <- B.getLine
    (take (fromIntegral m) . map readint . C.words -> as) <- B.getLine
    let rbq = run b q l as
    putStrLn $ if isNothing rbq then "inf" else show (fromJust rbq)

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

import Control.Monad (forM_, forM)
import Data.List (sort)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import Data.Maybe (fromJust, isNothing)
import Data.Set (fromList, difference, size)

readint = fromIntegral . fst. fromJust . C.readInt

run :: Integer -> Integer -> Integer -> [Integer] -> Maybe Int
run 0 q l as | 0 `elem` as = Just 0
             | otherwise = Nothing
run b 0 l as | 0 `elem` as && b `elem` as = Just 0
             | 0 `elem` as && not (b `elem` as) = if abs b <= l then Just 1 else Just 0
             | not (0 `elem` as) = Nothing
run b 1 l as | b `elem` as || abs b > l = Just 0
             | otherwise   = Nothing
run b (-1) l as | not (b `elem` as) && abs b <= l       = Nothing
                | not ((-b) `elem` as) && abs b <= l = Nothing
                | otherwise = Just 0
run b q l as = Just $ size $ difference (fromList lb) (fromList as) where
    lb = until ((> l) . abs . last) (\x -> x ++ [last x * q]) [b]

main = do
    (map readint . C.words -> [b, q, l, m]) <- B.getLine
    (map readint . C.words -> as) <- B.getLine
    let rbq = run b q l as
    putStrLn $ if isNothing rbq then "inf" else show (fromJust rbq)

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

import Control.Monad (forM_, forM)
import Data.List (sort)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import Data.Maybe (fromJust, isNothing)
import Data.Set (fromList, difference, size)

readint = fromIntegral . fst. fromJust . C.readInt

run :: Integer -> Integer -> Integer -> [Integer] -> Maybe Int
run 0 q l as | 0 `elem` as = Just 0
             | otherwise = Nothing
run b 0 l as | 0 `elem` as && b `elem` as = Just 0
             | 0 `elem` as && not (b `elem` as) = if abs b <= l then Just 1 else Just 0
             | not (0 `elem` as) = Nothing
run b 1 l as | b `elem` as || abs b > l = Just 0
             | otherwise   = Nothing
run b (-1) l as | not (b `elem` as) && abs b <= l       = Nothing
                | not ((-b) `elem` as) && abs b <= l = Nothing
                | otherwise = Just 0
run b q l as = Just $ size $ difference (fromList lb) (fromList as) where
    lb = filter ((<= l) . abs) (until ((> l) . abs . last) (\x -> x ++ [last x * q]) [b])

main = do
    (map readint . C.words -> [b, q, l, m]) <- B.getLine
    (map readint . C.words -> as) <- B.getLine
    let rbq = run b q l as
    putStrLn $ if isNothing rbq then "inf" else show (fromJust rbq)

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

import Control.Monad (forM_, forM)
import Data.List (sort)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import Data.Maybe (fromJust, isNothing)
import Data.Set (fromList, difference, size)

readint = fst. fromJust . C.readInteger

run :: Integer -> Integer -> Integer -> [Integer] -> Maybe Int
run 0 q l as | 0 `elem` as = Just 0
             | otherwise = Nothing
run b 0 l as | 0 `elem` as && b `elem` as = Just 0
             | 0 `elem` as && not (b `elem` as) = if abs b <= l then Just 1 else Just 0
             | not (0 `elem` as) = Nothing
run b 1 l as | b `elem` as || abs b > l = Just 0
             | otherwise   = Nothing
run b (-1) l as | not (b `elem` as) && abs b <= l    = Nothing
                | not ((-b) `elem` as) && abs b <= l = Nothing
                | otherwise = Just 0
run b q l as = Just $ size $ difference (fromList lb) (fromList as) where
    lb = filter ((<= l) . abs) (until ((> l) . abs . last) (\x -> x ++ [last x * q]) [b])

main = do
    (map readint . C.words -> [b, q, l, m]) <- B.getLine
    (take (fromIntegral m) . map readint . C.words -> as) <- B.getLine
    let rbq = run b q l as
    putStrLn $ if isNothing rbq then "inf" else show (fromJust rbq)

import Control.Applicative
import Data.List

main :: IO()
main = putStrLn. toString . solve' . map read.words =<< getContents

solve' :: [Int] -> Int
solve' (b:1:l:m:xs) = if (abs b>l) then 0 else if ((fst $ find' b (sort xs))==0) then 0 else 2000000000
solve' (b:(-1):l:m:xs) = if (abs b >l) then 0 else if (((fst $ find' b (sort xs))==0) && ((fst $ find' (-b) (sort xs))==0)) then 0 else 2000000000
solve' (b:0:l:m:xs) =  if (abs b>l) then 0 else let xs'= sort xs
                                                    (c1,_) = find' b xs'
                                                    (c2,_) = find' 0 xs'
                                                in case (c1,c2) of
                                                (_,1) -> 2000000000
                                                (1,0) -> 1
                                                (0,0) -> 0
solve' (b:q:l:m:xs) = solve b q l 0 (sort xs)

solve b q l c xs = if (abs b>l) then c else let (cm,nl) = find' b xs
                                            in solve (b*q) q l (c+cm) nl

toString n = if(n==2000000000) then "inf" else show n

find' _ []  = (1,[])
find' e (x:xs) = if (x==e) then (0,xs) else if(x>e) then (1,x:xs) else find' e xs

import Control.Applicative
import Data.List

main :: IO()
main = putStrLn. toString . solve' . map read.words =<< getContents

solve' :: [Int] -> Int
solve' (b:1:l:m:xs) = if (abs b>l) then 0 else if ((fst $ find' b (sort xs))==0) then 0 else 2000000000
solve' (b:(-1):l:m:xs) = if (abs b >l) then 0 else if (((fst $ find' b (sort xs))==0) && ((fst $ find' (-b) (sort xs))==0)) then 0 else 2000000000
solve' (b:0:l:m:xs) =  if (abs b>l) then 0 else let xs'= sort xs
                                                    (c1,_) = find' b xs'
                                                    (c2,_) = find' 0 xs'
                                                in case (c1,c2) of
                                                (_,1) -> 2000000000
                                                (1,0) -> 1
                                                (0,0) -> 0
solve' (b:q:l:m:xs) = solve b q l 0 xs

solve b q l c xs = if (abs b>l) then c else let (cm,nl) = find' b xs
                                            in solve (b*q) q l (c+cm) nl

toString n = if(n==2000000000) then "inf" else show n

find' _ []  = (1,[])
find' e (x:xs) = if (x==e) then (0,xs) else if(x>e) then (1,x:xs) else find' e xs

import Control.Applicative
import Data.List

main :: IO()
main = putStrLn. toString . solve' . map read.words =<< getContents

solve' :: [Int] -> Int
solve' (b:1:l:m:xs) = if (b>l) then 0 else if ((fst $ find' b xs)==0) then 0 else 2000000000
solve' (b:0:l:m:xs) =  if (b>l) then 0 else let (c1,_) = find' b xs
                                                (c2,_) = find' 0 xs
                                            in case (c1,c2) of
                                                (_,1) -> 2000000000
                                                (1,0) -> 1
                                                (0,0) -> 0
solve' (b:q:l:m:xs) = solve b q l 0 xs

solve b q l c xs = if (abs b>l) then c else let (cm,nl) = find' b xs
                                            in solve (b*q) q l (c+cm) nl

toString n = if(n==2000000000) then "inf" else show n

find' _ []  = (1,[])
find' e (x:xs) = if (x==e) then (0,xs) else if(x>e) then (1,x:xs) else find' e xs

import Control.Applicative
import Data.List

main :: IO()
main = putStrLn. toString . solve' . map read.words =<< getContents

solve' :: [Int] -> Int
solve' (b:1:l:m:xs) = if (abs b>l) then 0 else if ((fst $ findPos b (sort xs))==0) then 0 else 2000000000
solve' (0:_:l:m:xs) = if ((fst $ findPos 0 (sort xs))==0) then 0 else 2000000000
solve' (b:(-1):l:m:xs) = if (abs b >l) then 0 else if (((fst $ findPos b (sort xs))==0) && ((fst $ findPos (-b) (sort xs))==0)) then 0 else 2000000000
solve' (b:0:l:m:xs) =  if (abs b>l) then 0 else let xs'= sort xs
                                                    (c1,_) = findPos b xs'
                                                    (c2,_) = findPos 0 xs'
                                                in case (c1,c2) of
                                                (_,1) -> 2000000000
                                                (1,0) -> 1
                                                (0,0) -> 0

solve' (b:q:l:m:xs) = let (xsPos,xsNeg) = separe xs
                      in solve b q l 0 (sort xsPos) (reverse $ sort xsNeg)

solve b q l c xs xs' = if (b>l || (-1)*b>l) then c else if b>=0 then let (cm,nl) = findPos b xs
                                                             in solve (b*q) q l (c+cm) nl xs'
                                                else let (cm,nl) = findNeg b xs'
                                                     in solve (b*q) q l (c+cm) xs nl

toString n = if(n==2000000000) then "inf" else show n

separe = foldl (\(p,n) e-> if(e<0) then (p,e:n) else (e:p,n)) ([],[])

findPos _ []  = (1,[])
findPos e (x:xs) = if (x==e) then (0,xs) else if(x>e) then (1,x:xs) else findPos e xs

findNeg _ []  = (1,[])
findNeg e (x:xs) = if (x==e) then (0,xs) else if(x<e) then (1,x:xs) else findNeg e xs

import Control.Applicative
import Data.List

main :: IO()
main = putStrLn. toString . solve' . map read.words =<< getContents

solve' :: [Int] -> Int
solve' (b:1:l:m:xs) = if (abs b>l) then 0 else if ((fst $ findPos b (sort xs))==0) then 0 else 2000000000
solve' (0:_:l:m:xs) = if ((fst $ findPos 0 (sort xs))==0) then 0 else 2000000000
solve' (b:(-1):l:m:xs) = if (abs b >l) then 0 else if (((fst $ findPos b (sort xs))==0) && ((fst $ findPos (-b) (sort xs))==0)) then 0 else 2000000000
solve' (b:0:l:m:xs) =  if (abs b>l) then 0 else let xs'= sort xs
                                                    (c1,_) = findPos b xs'
                                                    (c2,_) = findPos 0 xs'
                                                in case (c1,c2) of
                                                (_,1) -> 2000000000
                                                (1,0) -> 1
                                                (0,0) -> 0

solve' (b:q:l:m:xs) = let (xsPos,xsNeg) = separe xs
                      in solve b q l 0 (sort xsPos) (reverse $ sort xsNeg)

solve b q l c xs xs' = if (abs b>l) then c else if b>=0 then let (cm,nl) = findPos b xs
                                                             in solve (b*q) q l (c+cm) nl xs'
                                                else let (cm,nl) = findNeg b xs'
                                                     in solve (b*q) q l (c+cm) xs nl

toString n = if(n==2000000000) then "inf" else show n

separe = foldl (\(p,n) e-> if(e<0) then (p,e:n) else (e:p,n)) ([],[])

findPos _ []  = (1,[])
findPos e (x:xs) = if (x==e) then (0,xs) else if(x>e) then (1,x:xs) else findPos e xs

findNeg _ []  = (1,[])
findNeg e (x:xs) = if (x==e) then (0,xs) else if(x<e) then (1,x:xs) else findNeg e xs

import Control.Applicative
import Data.List

main :: IO()
main = putStrLn. toString . solve' . map read.words =<< getContents

solve' :: [Int] -> Int
solve' (b:1:l:m:xs) = if ((fst $ find' b xs)==0) then 0 else 2000000000
solve' (b:0:xs) = 2000000000
solve' (b:q:l:m:xs) = solve b q l 0 xs

solve b q l c xs = if (abs b>l) then c else let (cm,nl) = find' b xs
                                            in solve (b*q) q l (c+cm) nl

toString n = if(n==2000000000) then "inf" else show n

find' _ []  = (1,[])
find' e (x:xs) = if (x==e) then (0,xs) else if(x>e) then (1,x:xs) else find' e xs

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

main :: IO()
main = putStrLn. toString . solve' . map read.words =<< getContents

solve' :: [Int64] -> Int64
solve' (b:1:l:m:xs) = if (abs b>l) then 0 else if ((fst $ find' b (sort xs))==0) then 0 else (-1)
solve' (b:(-1):l:m:xs) = if (abs b >l) then 0 else if (((fst $ find' b (sort xs))==0) && ((fst $ find' (-b) (sort xs))==0)) then 0 else 2000000000
solve' (b:0:l:m:xs) =  if (abs b>l) then 0 else let xs'= sort xs
                                                    (c1,_) = find' b xs'
                                                    (c2,_) = find' 0 xs'
                                                in case (c1,c2) of
                                                (_,1) -> (-1)
                                                (1,0) -> 1
                                                (0,0) -> 0
solve' (b:q:l:m:xs) = solve b q l 0 (sort xs)

solve b q l c xs = if (abs b>l) then c else let (cm,nl) = find' b xs
                                            in solve (b*q) q l (c+cm) nl

toString n = if(n==(-1)) then "inf" else show n

find' _ []  = (1,[])
find' e (x:xs) = if (x==e) then (0,xs) else if(x>e) then (1,x:xs) else find' e xs


import Control.Monad

main :: IO ()
main = do
  n <- readLn
  replicateM_ n routine

routine :: IO ()
routine = do
  [a,b] <- (map read.words) <$> getLine
  print $ solve a b

solve :: Int -> Int -> Int
solve a b = minimum [a,b,(a+b) `div` 3]

import Control.Monad
main :: IO ()
main = do
    n <- readLn
    replicateM_ n routine

routine :: IO ()
routine = do
    [a, b] <- (map read.words) <$> getLine
    putStrLn $ show (solve a b)

solve :: Int -> Int -> Int
solve a b = min (min a b) (div (a+b) 3)

module Main where

    solve :: IO ()
    solve = do
        s <- getLine
        let [a, b] = map (\x -> read x :: Integer) $ words s
        print $ minimum [a, b, (a + b) `div` 3]

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

    main :: IO ()
    main = do
        s <- getLine
        let q = read s :: Integer
        iter q

import Control.Monad

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

main :: IO()
main = do
  [t] <- readInts
  replicateM_ t solve

solve :: IO()
solve = do
  [a,b] <- readInts
  print $ minimum [a,b, div (a+b) 3]

module Main where

    solve :: IO ()
    solve = do
        s <- getLine
        let [a, b] = map (\x -> read x :: Integer) $ words s
        print $ maximum [a, b, (a + b) `div` 3]

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

    main :: IO ()
    main = do
        s <- getLine
        let q = read s :: Integer
        iter q