import qualified Data.ByteString.Char8 as B import Data.Maybe(fromJust) main = B.getContents >>= main' . map readInteger' . B.lines where main' [a, x] = print (pow' a x `mod` m) {- main' [a, x] | a == 0 && x == 0 = return () | otherwise = print (pow' a x `mod` m) >> main -} m :: Integer m = 10 readInteger' :: B.ByteString -> Integer {-# INLINE readInteger' #-} readInteger' = fst . fromJust . B.readInteger pow' :: Integer -> Integer -> Integer pow' a x | x == 0 = 1 | x > m = pow' a (x `mod` m) | a > m = pow' (a `mod` m) x | even x = pow' (a * a) (x `div` 2) | otherwise = a * pow' a (x - 1)