eps = 1e-9
tMax = 1e13
tau = 2 * pi

hypot a b = sqrt ( a^2 + b^2 )
fix q = q - tau * ( fromIntegral $ floor $ q / tau )

solve [xp0,yp0,vp,x,y,v,rs] = let
	r = hypot x y
	q = atan2 y x
	rp = hypot xp0 yp0
	qp0 = atan2 yp0 xp0
	wp = vp / rp
	arc = pi - asin ( rs / r ) - asin ( rs / rp )
	tans = sqrt ( r^2 - rs^2 ) + sqrt ( rp^2 - rs^2 )
	search a b = if b - a < eps then a else let
		t = ( a + b ) / 2
		qp = fix ( qp0 + wp * t )
		xp = rp * cos qp
		yp = rp * sin qp
		dq = min ( fix $ qp - q ) ( fix $ q - qp )
		dist = if dq <= arc then hypot ( xp - x ) ( yp - y ) else tans + ( dq - arc ) * rs
		in if dist <= t * v then search a t else search t b
	in search 0 tMax

main = interact $ show . solve . map read . words

main = getContents >>= print.solve.map read.words

type Time = Double

eps = 1e-8

type Pos = (Double,Double)
dist :: Pos -> Pos -> Double
dist (x0,y0) (x1,y1) = sqrt $ (x1-x0)*(x1-x0) + (y1-y0)*(y1-y0)

data Line = L Double Double Double
line :: Pos -> Pos -> Line
line (x0,y0) (x1,y1) = L a b c
   where
     a = y1-y0
     b = x0-x1
     c= -a*x0-b*y0
intersection :: Line -> Line -> Maybe Pos
intersection (L a b p) (L c d q)
  | det <= eps = Nothing
  | otherwise  = Just (x,y)
   where
     det = a*d-b*c
     x = (b*q-d*p)/det
     y = (c*p-a*q)/det

solve :: [Double] -> Time
solve [xp0,yp0,vp,x0,y0,v,r] = bsearch 100 0.0 1e6
   where
     bsearch 0 l r = r
     bsearch i l r
       | isReachable m = bsearch (i-1) l m
       | otherwise = bsearch (i-1) m r
      where
        m = (l+r)/2

     rp = dist (xp0,yp0) (0,0)
     phi = atan2 yp0 xp0
     omega = vp/rp
     r0 = dist (x0,y0) (0,0)

     pos :: Time -> Pos
     pos t = (rp*(cos (omega*t+phi)),rp*(sin (omega*t+phi)))
     isBehind :: Pos -> Bool
     isBehind (xp,yp) = d<r && min xp x0 <= x && x<= max xp x0
      where
        L a b c = line (xp,yp) (x0,y0)
        d = abs c/sqrt(a*a+b*b)
        Just (x,y) = intersection (L a b c) (L (-b) a 0)
     extDist :: Pos -> Double
     extDist (xp,yp)
       | d < eps = 0
       | isBehind (xp,yp) = l0+lp+lc
       | otherwise = d
      where
        d = dist (xp,yp) (x0,y0)
        l0 = sqrt (x0*x0+y0*y0-r*r)
        lp = sqrt (xp*xp+yp*yp-r*r)
        lc = r*(acos ((xp*x0+yp*y0)/(rp*r0)) - asin(l0/r0) - asin(lp/rp))
     isReachable :: Time -> Bool
     isReachable t = extDist (pos t) <= v*t

main = getContents >>= print.solve.map read.words

type T = Double

type Pos = (Double,Double)
dist :: Pos -> Pos -> Double
dist (x0,y0) (x1,y1) = sqrt $ (x1-x0)*(x1-x0) + (y1-y0)*(y1-y0)

data Line = L Double Double Double
line :: Pos -> Pos -> Line
line (x0,y0) (x1,y1) = L a b c
   where
     a = y1-y0
     b = x0-x1
     c= -a*x0-b*y0

solve :: [Double] -> T
solve [xp0,yp0,vp,x0,y0,v,r] = bsearch 100 0.0 1e6
   where
     bsearch 0 l r = r
     bsearch i l r
       | isReachable m = bsearch (i-1) l m
       | otherwise = bsearch (i-1) m r
      where
        m = (l+r)/2
     rp = dist (xp0,yp0) (0,0)
     angp = atan2 yp0 xp0
     r0 = dist (x0,y0) (0,0)
     pos :: T -> Pos
     pos t = (rp*(cos (omega*t+angp)),rp*(sin (omega*t+angp)))
      where
        omega = vp/rp
     isBehind :: Pos -> Bool
     isBehind (xp,yp) = d<=r && dist (xp,yp) (x0,y0) > sqrt (r0*r0-d*d)
      where
        L a b c = line (xp,yp) (x0,y0)
        d = abs c/sqrt(a*a+b*b)
     extDist :: Pos -> Double
     extDist (xp,yp)
       | isBehind (xp,yp) = l0+lp+lc
       | otherwise = dist (xp,yp) (x0,y0)
      where
        l0 = sqrt (x0*x0+y0*y0-r*r)
        lp = sqrt (xp*xp+yp*yp-r*r)
        lc = r*(acos ((xp*x0+yp*y0)/(rp*r0)) - asin(l0/r0) - asin(lp/rp))
     isReachable :: T -> Bool
     isReachable t = extDist (pos t) <= v*t

main = getContents >>= print.solve.map read.words

type T = Double

type Pos = (Double,Double)
dist :: Pos -> Pos -> Double
dist (x0,y0) (x1,y1) = sqrt $ (x1-x0)*(x1-x0) + (y1-y0)*(y1-y0)

data Line = L Double Double Double
line :: Pos -> Pos -> Line
line (x0,y0) (x1,y1) = L a b c
   where
     a = y1-y0
     b = x0-x1
     c= -a*x0-b*y0

solve :: [Double] -> T
solve [xp0,yp0,vp,x0,y0,v,r] = bsearch 100 0.0 1e6
   where
     bsearch 0 l r = r
     bsearch i l r
       | isReachable m = bsearch (i-1) l m
       | otherwise = bsearch (i-1) m r
      where
        m = (l+r)/2
     rp = dist (xp0,yp0) (0,0)
     angp = atan2 yp0 xp0
     r0 = dist (x0,y0) (0,0)
     pos :: T -> Pos
     pos t = (rp*(cos (omega*t+angp)),rp*(sin (omega*t+angp)))
      where
        omega = vp/rp
     isBehind :: Pos -> Bool
     isBehind (xp,yp) = abs c/sqrt(a*a+b*b) <= r
      where
        L a b c = line (xp,yp) (x0,y0)
     extDist :: Pos -> Double
     extDist (xp,yp)
       | isBehind (xp,yp) = l0+lp+lc
       | otherwise = dist (xp,yp) (x0,y0)
      where
        l0 = sqrt (x0*x0+y0*y0-r*r)
        lp = sqrt (xp*xp+yp*yp-r*r)
        lc = r*(acos ((xp*x0+yp*y0)/(rp*r0)) - asin(l0/r0) - asin(lp/rp))
     isReachable :: T -> Bool
     isReachable t = extDist (pos t) <= v*t

main = getContents >>= print.solve.map read.words

type Time = Double

type Pos = (Double,Double)
dist :: Pos -> Pos -> Double
dist (x0,y0) (x1,y1) = sqrt $ (x1-x0)*(x1-x0) + (y1-y0)*(y1-y0)

data Line = L Double Double Double
line :: Pos -> Pos -> Line
line (x0,y0) (x1,y1) = L a b c
   where
     a = y1-y0
     b = x0-x1
     c= -a*x0-b*y0

solve :: [Double] -> Time
solve [xp0,yp0,vp,x0,y0,v,r] = bsearch 100 0.0 1e6
   where
     bsearch 0 l r = r
     bsearch i l r
       | isReachable m = bsearch (i-1) l m
       | otherwise = bsearch (i-1) m r
      where
        m = (l+r)/2

     rp = dist (xp0,yp0) (0,0)
     phi = atan2 yp0 xp0
     omega = vp/rp
     r0 = dist (x0,y0) (0,0)

     pos :: Time -> Pos
     pos t = (rp*(cos (omega*t+phi)),rp*(sin (omega*t+phi)))
     isBehind :: Pos -> Bool
     isBehind (xp,yp) = d<r && dist (xp,yp) (x0,y0) >= sqrt (rp*rp-d*d)
      where
        L a b c = line (xp,yp) (x0,y0)
        d = abs c/sqrt(a*a+b*b)
     extDist :: Pos -> Double
     extDist (xp,yp)
       | isBehind (xp,yp) = l0+lp+lc
       | otherwise = dist (xp,yp) (x0,y0)
      where
        l0 = sqrt (x0*x0+y0*y0-r*r)
        lp = sqrt (xp*xp+yp*yp-r*r)
        lc = r*(acos ((xp*x0+yp*y0)/(rp*r0)) - asin(l0/r0) - asin(lp/rp))
     isReachable :: Time -> Bool
     isReachable t = extDist (pos t) <= v*t

main = getContents >>= print.solve.map read.words

type T = Double

type Pos = (Double,Double)
dist :: Pos -> Pos -> Double
dist (x0,y0) (x1,y1) = sqrt $ (x1-x0)*(x1-x0) + (y1-y0)*(y1-y0)

data Line = L Double Double Double
line :: Pos -> Pos -> Line
line (x0,y0) (x1,y1) = L a b c
   where
     a = y1-y0
     b = x0-x1
     c= -a*x0-b*y0

solve :: [Double] -> T
solve [xp0,yp0,vp,x0,y0,v,r] = bsearch 100 0.0 1e6
   where
     bsearch 0 l r = r
     bsearch i l r
       | isReachable m = bsearch (i-1) l m
       | otherwise = bsearch (i-1) m r
      where
        m = (l+r)/2
     rp = dist (xp0,yp0) (0,0)
     angp = atan2 yp0 xp0
     r0 = dist (x0,y0) (0,0)
     pos :: T -> Pos
     pos t = (rp*(cos (omega*t+angp)),rp*(sin (omega*t+angp)))
      where
        omega = vp/rp
     isBehind :: Pos -> Bool
     isBehind (xp,yp) = abs c/sqrt(a*a+b*b)<r && dist (-xp,-yp) (x0,y0) < dist (xp,yp) (x0,y0)
      where
        L a b c = line (xp,yp) (x0,y0)
     extDist :: Pos -> Double
     extDist (xp,yp)
       | isBehind (xp,yp) = l0+lp+lc
       | otherwise = dist (xp,yp) (x0,y0)
      where
        l0 = sqrt (x0*x0+y0*y0-r*r)
        lp = sqrt (xp*xp+yp*yp-r*r)
        lc = r*(acos ((xp*x0+yp*y0)/(rp*r0)) - asin(l0/r0) - asin(lp/rp))
     isReachable :: T -> Bool
     isReachable t = extDist (pos t) <= v*t


main = interact$calc.map trans.tail.lines

trans :: String -> (Int, Int)

trans s = ((read.head.words) s, (read.last.words) s)

calc :: [(Int, Int)] -> String
calc wtf = ((++).show.calc') wtf "\n"
calc' :: [(Int, Int)] -> Int
calc' a | length a == 0 = 0
        | length a == 1 = (if ((snd.head) a) - ((fst.head) a) > 1 then 1 else 0)
        | otherwise = (if ((snd.head) a) - ((fst.head) a) > 1 then 1 + (calc'.tail) a else (calc'.tail) a)

main = print . solve . map parse . tail . lines =<< getContents
parse l = read b - read a where [a,b] = words l
solve = length . filter (>= 2)

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

parse :: String -> (Int, Int)
parse s = (p, q)
  where
    [p, q] = map read . words $ s

solve :: [(Int, Int)] -> Int
solve = length . filter (\(p, q) -> q - p >= 2)

-- Vicfred
-- https://codeforces.com/problemset/problem/467/A

rooms :: [[Int]] -> Int
rooms = length . filter (\(p:q:_) -> q - p >= 2)

solve :: String -> String
solve = show . rooms . parse

parse :: String -> [[Int]]
parse = fmap (fmap read . words) . tail . lines

main :: IO ()
main = interact solve

module Main where
import Control.Applicative

getFreeBeds :: String -> Int
getFreeBeds line = let [taken,maxi] = map (read :: String -> Int) $ words line in maxi - taken

main = do
  roomCount <- (read :: String -> Int) <$> getLine
  print =<< length . filter (>1) <$> mapM (\_ -> getFreeBeds <$> getLine) [1..roomCount]

import Data.List
solve = length . filter (\[o,c] -> c-o > 1)
main = interact $ show . solve . map (map read . words) . tail . lines

import Data.List
solve = length . fst . partition (\[o,c] -> c-o > 1)
main = interact $ show . solve . map (map read . words) . tail . lines

f :: Integer -> IO [(Int, Int)]
f 0 = return []
f n = do
    l <- getLine
    r <- f (n-1)
    let [a,b] = map read $ words l in return ((a,b):r)

main :: IO()
main = do
    n <- getLine
    z <- f $ read n
    putStrLn $ show $ sum $ map (\(x,y) -> if y - x >= 2 then 1 else 0) z

f :: Integer -> IO [(Int, Int)]
f 0 = return []
f n = do
    l <- getLine
    r <- f (n-1)
    let [a,b] = map read $ words l in return ((a,b):r)

main :: IO()
main = do
    n <- getLine
    z <- f $ read n
    putStrLn $ show $ length $ filter (\(x,y) -> y - x >= 2) z

import Data.List
import Data.Char

--In matrix rows values should be space separated
inputNumMatrix :: Int -- ^ nRows
               -> IO [[Int]]
inputNumMatrix nRows =
  sequence $ replicate nRows (map read . words <$> getLine)

rooms :: [[Int]] -> IO ()
rooms rs = putStrLn $ show $ length $ filter (\room -> (room!!1) - (room!!0) >= 2) rs

main :: IO ()
main = read <$> getLine >>= inputNumMatrix >>= rooms

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

main= do
	getLine
 	s<-map (map read) . map words.lines <$> getContents ::IO [[Int]]
	print $ length $  filter (\z-> (last z-head z) >1) s

main = do
  m <- readLn
  ml <- sequence $ replicate m getLine
  let ml2 = map ((\[x, y] -> y - x) . map read. words) ml
  let ml3 = length $ filter (>=2) ml2
  print ml3

import           Control.Monad
main :: IO ()
main = do
    n <- (read :: String -> Int) <$> getLine
    print =<< sum . map (fromEnum . (\[a,b]->a + 2 <= b) . map (read :: String -> Int) . words) <$> replicateM n getLine

import Control.Monad

isAvailable :: [Int] -> Int
isAvailable l = if last l - head l >= 2 then 1 else 0

main :: IO ()
main = do
    input <- getLine
    inputs <- replicateM (read input) getLine
    print $ foldl (+) 0 (fmap (isAvailable . lineToIntL) inputs)
            where lineToIntL = fmap (\i -> read i :: Int) . words

import Control.Monad
import Data.List

main = getLine >>= return . read
               >>= flip replicateM getLine
               >>= return . map (map read . words)
               >>= print . length . filter solve

solve :: [Integer] -> Bool
solve [x , y] = y - x > 1

import Data.List

process :: [(Int,Int)] -> Int
process = length.filter (\(p,q) -> q-p >= 2)

readInt :: String -> Int
readInt = read

readPair :: String -> (Int,Int)
readPair line = (a,b)
    where [a,b] = map readInt $ words line

main = do
    n <- fmap readInt getLine
    xs <- fmap (map readPair . lines) getContents
    print $ process xs

main = interact $ show . sol . map read . drop 1 . words

sol [] = 0
sol (a:b:r)
    | b-a >= 2 = 1 + sol r
    | otherwise = sol r

module Main where

main :: IO ()
main = interact f

f = show . length . filter (\(x:y:_) -> y-x >= 2) . map (map read.words) . tail . lines

main = interact$show.calc.map trans.tail.lines
trans :: String -> (Int, Int)
trans s = ((read.head.words) s, (read.last.words) s)
calc :: [(Int, Int)] -> Int
calc ((x, y):[]) | y - x >= 2 = 1
                 | otherwise = 0
calc ((x, y):s) | y - x >= 2 = 1 + calc s
                | otherwise = calc s

solve :: [Int] -> Int
solve (p:q:rest) = (if q >= p + 2 then 1 else 0) + solve rest
solve [] = 0
main = print . solve . tail . map read . words =<< getContents

main = print . length . filter (>= 2) .map ((\[p, q] -> q - p) . map read . words) . tail . lines =<< getContents

solve [] = 0
solve (p:q:r) = solve r + if q > p + 1 then 1 else 0
main = print . solve . tail . map read . words =<< getContents

f lst = length (filter (\ x -> (x!!1) - (x!!0)  >= 2) lst)
main = do
 getLine
 nums <- getContents
 let linelst = lines nums
     lst     = map (\ x -> map read $ words x) linelst :: [[Int]]
     ans     = f lst
 print ans

parse xs = read xs :: Int
run xs = show $ foldl (+) 0 $ map (\l -> let li = map parse $ words l in if li!!1-li!!0>=2 then 1 else 0) ls where ls = tail (lines xs)
main = interact run

run xs = show $ foldl (+) 0 $ map (\l -> let li = map (read) $ words l in fromEnum (li!!1-li!!0>=2)) ls where ls = tail (lines xs)
main = interact run

main = interact $ show.length.filter (>=2).map (\[x,y]->y-x).map ((map read).words).tail.lines

solve :: [(Int, Int)] -> Int
solve xs = length $ filter (\(x, y) -> y - x > 1) xs

parseInput :: String -> [(Int, Int)]
parseInput input = xs where
    ls = tail $ lines input
    ys = map words ls
    xs = [(read x, read y) | [x, y] <- ys]

main :: IO ()
main = do
    input <- getContents
    let xs = parseInput input
    print $ solve xs

main = do
  d <- getContents
  let (n:ls) = lines d
  let rooms = map (map read . words) ls :: [[Int]]
  let ans = foldr (\[a,b] c -> c + if b - a >= 2 then 1 else 0) 0 rooms
  print ans

findNum :: [(Int, Int)] -> Int
findNum l = length $ filter (\p -> snd p - fst p > 1) l

main = do
    n <- readLn :: IO Int
    content <- getContents
    let
        rooms = map (\[x, y] -> (x, y)) . map (map (read::String->Int)) . map words . lines $ content
    putStrLn $ show $ findNum rooms

module Main (main)
    where

import Control.Monad (replicateM)

main :: IO ()
main = print . count . map readInts =<< flip replicateM getLine =<< readLn
    where count = length . filter (>= 2) . map (\[a,b] -> b - a)
          readInts = map read . words

import qualified Data.ByteString.Char8 as B
import Data.Maybe (fromJust)

readRoom :: B.ByteString -> [Int]
readRoom = map (fst . fromJust . B.readInt) . B.split ' '

isThereSpace :: B.ByteString -> Int
isThereSpace room = if occupied + 2 <= available then 1 else 0
  where space = readRoom room
        occupied = head space
        available = last space

main = B.getContents >>=
    return . show . sum . map (isThereSpace) . tail . B.lines >>=
    putStrLn

import Control.Monad
main = do
	getLine
	c<-getContents
	print $ length $ filter (\[x,y]->y-x>1)$map (map read.words) $ lines c

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

solve [] = 0
solve ([m1,m2]:ms) = if m2-m1>=2 then 1+(solve ms) else solve ms

-- https://stackoverflow.com/questions/10285837/read-n-lines-into-a-string
-- http://hackage.haskell.org/package/base-4.9.1.0/docs/Control-Monad.html#v:replicateM
------------
-- http://learnyouahaskell.com/input-and-output

-- go to the sequence part of the page

-- learn how to use foldl, foldr, map --functions
--------------------------------------------------
-- learn your haskell library:
-- https://downloads.haskell.org/~ghc/latest/docs/html/libraries/
--
-- page 15 of learn you a haskell for great good
-- i can use this

-- next: i discovered the "sequence" function on this webpage
-- http://learnyouahaskell.com/input-and-output
-- it is also the same as page 164 in the book "learn you a
-- haskell for great good"
-- This seems to be a conventional function that is used
-- so i am going o use it.

conv lst nw
  | (null lst) = nw
  | otherwise = conv (tail lst) (nw ++ [(read (head (words (head lst))))::Int]
                                ++ [(read (last (words (head lst))))::Int])

countRooms lst rooms
  | (null lst) = rooms
  | (head (drop 1 lst)) > ((head lst)+1) = countRooms (drop 2 lst) (rooms+1)
  | otherwise = countRooms (drop 2 lst) rooms

main = do
  a <- getLine
  let n = (read a)::Int
  let g = replicate n getLine
  dfd <- sequence g
  let newlist = conv dfd []
  let ans = countRooms newlist 0
  putStrLn (show ans)

import Control.Monad

-- Snippet: readItems
readItems = do
    line <- getLine
    return $ map read $ words line

main = do
    n <- readLn
    a <- replicateM n $ do
        [living, total] <- readItems
        return $ if 2 <= total - living then 1 else 0
    putStrLn $ show $ sum $ a

import Control.Monad

readNLines :: Int -> IO [String]
readNLines n = replicateM n getLine

freeSpace :: (Int, Int) -> Int
freeSpace (a, b) = b - a

main :: IO ()
main = do
    n <- readLn :: IO Int
    lines <- readNLines n
    let args = map (\x -> map (\y -> read y :: Int) $ words x) lines
        pairs = map (\x -> (x!!0, x!!1)) args
    print $ length $ filter (>=2) $ map freeSpace pairs

readPairsList ::Int -> IO [(Int, Int)]
readPairsList n = do
    if n == 0
        then return []
    else do
        line <- getLine
        let pairStr = map read $ words line :: [Int]
        let pair = ((pairStr !! 0), (pairStr !! 1))
        rest <- readPairsList $ n-1
        return (pair:rest)

main = do
    n <- fmap read getLine :: IO Int
    pairs <- readPairsList n
    let answer xs = length [x | x <- xs, fst(x)+2 <= snd(x)]

    putStrLn $ show(answer pairs)

import Data.List
import Data.Function
import Data.Char
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B

getIntArray :: B.ByteString -> [Int]
getIntArray = map fst . mapMaybe B.readInt . B.words

main = interact $ show . solve . tail . map read . words

solve :: [Int] -> Int
solve [] = 0
solve (p:q:xs) = solve xs + if (q >= p + 2) then 1 else 0

main = do
    getLine
    ps <- fmap (map (map read . words). lines) getContents
    print $ length $ filter (\[a, b] -> a+2 <= b) ps

main = interact $ show . length . filter id . map (\[a,b] -> a+2<=b) . map (map read) . map words . tail . lines

main = do
	string <- getContents
	print . length . filter (\(a, b) -> b-a >= 2) . map (\[a,b] -> (a,b)) . map (map read) . map words . tail $ lines string

import Data.List
import Data.Maybe
main=   readsolveprint
readsolveprint::IO()
readsolveprint =  getLine>>= \x->interact $ show .solve. map (map read.words).take (read x) . lines
solve :: [[Int]]->Int
solve xss =foldr (\ a b-> if a>=2 then 1+b else b) 0 [y-x|(x:y:[])<-xss]

import Control.Monad
import Data.List

int x = read x ::Int

rooms = length . filter (>=2)  . (uncurry $ flip $ zipWith (-)) . unzip

main = do
   n <- liftM int getLine
   vs <- liftM (map ((\[x,y] -> (x,y)) . map int. words)) $ replicateM n getLine
   print $ rooms vs

-- Codeforces 467A

import Control.Monad

main :: IO ()
main = do
    n <- fmap read getLine :: IO Int
    replicateM n getLine >>= print . length . filter (\x -> (x!!1)-(x!!0) >= 2) . map (map read . words)

import Control.Applicative

main = do
  n <- read <$> getLine :: IO Int
  xs <- map (map read . words) . lines <$> getContents
  print . sum $ map (\[x,y] -> if y-x >= 2 then 1 else 0) xs

main = interact solve

solve input = show (foldr folder 0 rooms)
    where
        folder (p,q) ccount = if (q-p) >= 2 then
                                ccount+1
                              else
                                ccount
        as_nums::[Integer]
        n::Integer
        (n:as_nums) = (map read.words) input
        rooms = two_by_two as_nums

two_by_two::[a] -> [(a,a)]
two_by_two [] = []
two_by_two (x:y:xs) = (x,y):(two_by_two xs)

import Control.Monad
import Data.List

readInt :: IO Int
readInt = readLn

untilSpace :: [Char] -> [Char] -> [[Char]] -> [[Char]]
split :: [Char] -> [Int]
getInt a = read a::Int

untilSpace arg acc result = if arg == [] then acc : result else
                            if head arg == ' ' then untilSpace (tail arg) ("") (acc : result) else
                            untilSpace (tail arg) (acc ++ [head arg]) (result)
split a = map (getInt) (reverse $ untilSpace (a) ("") ([]))

count :: [[Int]] -> Int -> Int
count [] n = n
count (a:xa) n | a!!1 - a!!0 >= 2 = count xa (n + 1)
               | otherwise = count xa n

solve a = putStrLn $ show (count (map split a)  0)
readLines a = replicateM a getLine >>= solve
main = readInt >>= readLines

import Control.Applicative
import Control.Monad

main = do
  n <- readLn
  xs <- replicateM n $ map read . words <$> getLine
  let
    m = length . filter (\[p,q] -> p+2 <= q) $ xs
   in print m

import Control.Applicative
import Data.List

main=do
    getLine
    x<-( map (map read) <$> map words <$> lines <$> getContents)::IO [[Int]]
    putStrLn $ show $ length $ filter (>=2) $ map (\[a,b]->b-a)   x

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

solve :: [[Int]] -> Int
solve pqs = length [ () | [p, q] <- pqs, p + 2 <= q ]

main=getContents>>=print.solve.map(map read.words).tail.lines
solve w=length[p|[p,q]<-w,p+2<=q]

import Prelude hiding (readList)
import Control.Monad
import Control.Applicative
import Data.List

readList :: Read a => IO [a]
readList = fmap (fmap read . words) getLine

readList' :: Read a => a -> IO [a]
readList' _ = readList

readInt :: IO Int
readInt = head <$> readList' (undefined::Int)

showList :: Show a => [a] -> IO ()
showList = putStrLn . unwords . map show

skipLine :: IO ()
skipLine = void $ getLine

count :: (a -> Bool) -> [a] -> Int
count f = length . filter f

divUp :: Integral a => a -> a -> a
divUp x y
  | x `mod` y /= 0 = x `div` y + 1
  | otherwise      = x `div` y

main :: IO ()
main = do
  n <- readInt
  xs <- replicateM n (readList' (undefined::Int))
  print $ length $ filter (\[a,b] -> a+2 <= b) xs

fit :: String -> Bool
fit s = q >= p + 2
  where p = num !! 0
        q = num !! 1
        num = map read $ words s

rooms :: [String] -> Int
rooms l = length $ filter fit l

main = do
  getLine
  interact $ show . rooms . lines

main = do
    getLine
    ps <- fmap (map (map read . words). lines) getContents
    print $ filter (\[a, b] -> a+2 <= b) ps

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

solve :: [[Int]] -> Int
solve pqs = length [ () | [p, q] <- pqs, p < q ]

f lst = length (filter (\ x -> (abs (x!!0) - (x!!1)) > 1) lst)
main = do
 getLine
 nums <- getContents
 let linelst = lines nums
     lst   = map (\ x -> map read $ words x) linelst :: [[Int]]
     ans = f lst
 print ans

main = interact $ show.length.filter (>2).map (\[x,y]->y-x).map ((map read).words).tail.lines


module Main where

main :: IO ()
main = interact $ show . solve . map read . words

solve :: [Double] -> Double
solve [a, v, l, d, w]
    | w >= v || signAccelPath >= d = time a 0 v l
    | otherwise = signAccelTime + 2 * time a w v ((d - signAccelPath) / 2) + time a w v (l -d)
  where
    signAccelTime = w / a
    signAccelPath = dist a 0 signAccelTime

dist :: Double -> Double -> Double -> Double
dist a v t = v * t + a * t * t / 2

time :: Double -> Double -> Double -> Double -> Double
time a v0 v l
    | fullAccTime > totalTime = totalTime
    | otherwise               = fullAccTime + remainingDist / v
  where
    fullAccTime = (v - v0) / a
    totalTime = (sqrt (v0^2 + 2 * l * a) - v0) / a
    remainingDist = l - dist a v0 fullAccTime

import Monad (liftM)
{-
import HUnit

tests :: Test
tests  =  TestList "Tests"
  [
    Test "#01" $ assertEq 2.5 $ solve [1,1,2,1,3],
    Test "#02" $ assertApproximate 8.965874696353 (solve [5,70,200,170,40]) 6,{-
    Test "#03" $ assertApproximate  $ solve [],
    Test "#04" $ assertApproximate  $ solve [],
    Test "#05" $ assertApproximate  $ solve [],
    Test "#06" $ assertApproximate  $ solve [],
    Test "#07" $ assertApproximate  $ solve [],
    Test "#08" $ assertApproximate  $ solve [],
    Test "#09" $ assertApproximate  $ solve [],
    Test "#10" $ assertApproximate  $ solve [],
    Test "#11" $ assertApproximate  $ solve [],
    Test "#12" $ assertApproximate  $ solve [],
    Test "#13" $ assertApproximate  $ solve [],
    Test "#14" $ assertApproximate  $ solve [],
    Test "#15" $ assertApproximate  $ solve [],
    Test "#16" $ assertApproximate  $ solve [],-}
    Test "#17" $ assertApproximate 57.6 (solve [4,120,5112,3000,130]) 5
  ]

test :: IO ()
test  =  run tests
-}
s' :: Floating a => a -> a -> a -> a
s' v0 a t  =  v0 * t + 0.5 * a * t^2

t' :: Floating a => a -> a -> a -> a
t' v0 a s  =  (-v0 + sqrt (v0^2 + 2 * a * s)) / a

v' :: Floating a => a -> a -> a -> a
v' v0 a t  =  v0 + a * t

tMax' :: (Floating a, Ord a) => a -> a -> a -> a -> a
tMax' v v0 a s
  | t1 < t2  =  t1
  | otherwise  =  t2 + (s - s' v0 a t2) / v
  where
    t1  =  t' v0 a s
    t2  =  (v - v0) / a

tMax'' :: (Floating a, Ord a) => a -> a -> a -> a -> a -> a
tMax'' v w v0 a s
  | w >= v  =  tMax' v v0 a s
  | tS <= tW  =  tMax' v v0 a s
  | sW + 2 * sWV >= s  =  tW + 2 * t' w a (0.5 * (s - sW))
  | otherwise  =  tW + 2 * tWV + (s - sW - 2 * sWV) / v
  where
    tS   =  t' v0 a s
    tW   =  (w - v0) / a
    tWV  =  (v - w) / a
    sW   =  s' v0 a tW
    sWV  =  s' w a tWV

solve :: (Floating a, Ord a) => [a] -> a
solve [a, v, l, d, w]
  | v  <= w    =  tMax' v 0 a l
  | tD <= tW   =  tMax' v 0 a l
  | otherwise  =  tMax'' v w 0 a d + tMax' v w a (l-d)
  where
    tD  =  t' 0 a d
    tW  =  w / a

main :: IO ()
main  =  liftM (map read . take 5 . words) getContents >>= print . solve

import Text.Printf
main = do
    [a, v, l, d, w] <- (map (fromInteger . read) . take 5) `fmap`
                (words `fmap` getContents) :: IO [Double]
    let maxAccelPath = v^2 / 2 / a
        maxAccelPath' = w^2 / 2 / a
	maxDecelPath = maxAccelPath - maxAccelPath'
        (t1, w') | v > w && d < maxAccelPath' = (sqrt (2 * d / a), t1 * a)
                 | v > w && d < maxAccelPath+maxDecelPath = let
                    s' = (d + maxAccelPath') / 2 in (2 * sqrt (2 * s' / a) - w / a, w)
                 | v > w = (v / a + (v - w) /a + (d + maxAccelPath'
                    - 2 * maxAccelPath) / v, w)
                 | w >= v && d < maxAccelPath = (sqrt (2 * d / a), t1*a)
                 | otherwise = (v / a + (d - maxAccelPath) / v, v)
        maxAccelPath2 | w' < v = maxAccelPath - w'^2 / 2 /a
                      | otherwise = 0
        t2 | (l - d) < maxAccelPath2 = sqrt (2*(l - d + w'^2/2/a)/a) - w'/a
           | otherwise = (v - w')/a + (l - d - maxAccelPath2)/v
    printf "%.5f" (t1+t2)

calc a v l d w
     | v <= w = calc2 l 0
     | otherwise = let tw = w/a
                       dw = dist 0 tw
                   in  if dw >= d then
                          calc2 l 0
                       else tw+2*calc2 ((d-dw)/2) w+calc2 (l-d) w
     where calc2 len v0 = let t1 = (v-v0)/a
                              t2 = ((sqrt $ v0*v0+2*a*len) - v0) / a
                          in  if t1 > t2 then t2
                              else t1+(len-dist v0 t1)/v
           dist v0 t = v0*t+a*t*t/2

main = do
     [a, v] <- return . (map read :: [String] -> [Double]) . words =<< getLine
     [l, d, w] <- return . (map read :: [String] -> [Double]) . words =<< getLine
     print $ calc a v l d w

import Monad (liftM)

s :: Floating a => a -> a -> a -> a
s v0 a t  =  v0 * t + 0.5 * a * t^2

t :: Floating a => a -> a -> a -> a
t v0 a s  =  (-v0 + sqrt (v0^2 + 2 * a * s)) / a

v :: Floating a => a -> a -> a -> a
v v0 a t  =  v0 + a * t

solve :: (Floating a, Ord a) => [a] -> a
solve [a, vMax, l, d, w]
  | w >= vMax  =  solve' 0 vMax a l
  | v 0 a tr <= w  =  solve' 0 vMax a l
  | otherwise  =  solve'' w vMax a d + t2
  where
    tr  =  t 0 a d
    t2  =  solve' w vMax a (l - d)
    solve' v0 vMax a l
      | v v0 a tr <= vMax  =  t v0 a l
      | otherwise  =  vMax / a + (l - s v0 a (vMax / a)) / vMax
      where
        tr  =  t v0 a l
    solve'' w vMax a d
      | tr2 <= tr1  =  w/a + 2 * tr2
      | otherwise  =  w/a + 2 * tr1 + (d - d' - 2 * s w a tr1) / vMax
      where
        tr1  =  (vMax - w) / a
        tr2  =  t w a (0.5 * (d - d'))
        d'  =  s 0 a (w/a)

main :: IO ()
main  =  liftM (map read . take 5 . words) getContents >>= print . solve

import Monad (liftM)

s' :: Floating a => a -> a -> a -> a
s' v0 a t  =  v0 * t + 0.5 * a * t^2

t' :: Floating a => a -> a -> a -> a
t' v0 a s  =  (-v0 + sqrt (v0^2 + 2 * a * s)) / a

v' :: Floating a => a -> a -> a -> a
v' v0 a t  =  v0 + a * t

tMax' :: (Floating a, Ord a) => a -> a -> a -> a -> a
tMax' v v0 a s
  | t1 < t2  =  t1
  | otherwise  =  t2 + (s - s' v0 a t2) / v
  where
    t1  =  t' v0 a s
    t2  =  (v - v0) / a

tMax'' :: (Floating a, Ord a) => a -> a -> a -> a -> a -> a
tMax'' v w v0 a s
  | w >= v  =  tMax' v v0 a s
  | tS <= tW  =  tMax' v v0 a s
  | sW + 2 * sWV >= s  =  tW + 2 * t' w a (0.5 * (s - sW))
  | otherwise  =  tW + 2 * tWV + (s - sW - 2 * sWV) / v
  where
    tS   =  t' v0 a s
    tW   =  (w - v0) / a
    tWV  =  (v - w) / a
    sW   =  s' v0 a tW
    sWV  =  s' w a tWV

solve :: (Floating a, Ord a) => [a] -> a
solve [a, v, l, d, w]
  | tD <= tW   =  tMax' v 0 a l
  | otherwise  =  tMax'' v w 0 a d + tMax' v w a (l-d)
  where
    tD  =  t' 0 a d
    tW  =  w / a

main :: IO ()
main  =  liftM (map read . take 5 . words) getContents >>= print . solve

import Text.Printf
main = do
    [a, v, l, d, w] <- (map (fromInteger . read) . take 5) `fmap`
                (words `fmap` getContents) :: IO [Double]
    let maxAccelPath = v^2 / 2 / a
        maxAccelPath' = w^2 / 2 / a
	maxDecelPath = maxAccelPath - maxAccelPath'
        (t1, w') | v > w && d < maxAccelPath' = (sqrt (2 * d / a), t1 * a)
                 | v > w && d < maxAccelPath+maxDecelPath = let
                    s' = (d + maxAccelPath') / 2 in (2 * sqrt (2 * s' / a) - w / a, w)
                 | v > w = (v / a + (v - w) /a + (d + maxAccelPath'
                    - 2 * maxAccelPath) / a, w)
                 | w >= v && d < maxAccelPath = (sqrt (2 * d / a), t1*a)
                 | otherwise = (v / a + (d - maxAccelPath) / v, v)
        maxAccelPath2 | w' < v = maxAccelPath - w'^2 / 2 /a
                      | otherwise = 0
        t2 | (l - d) < maxAccelPath2 = sqrt (2*(l - d + w'^2/2/a)/a) - w'/a
           | otherwise = (v - w')/a + (l - d - maxAccelPath2)/v
    printf "%.5f" (t1+t2)

calc a v l d w
     | v <= w = calc2 l 0
     | otherwise = let tw = w/a
                       dw = dist 0 tw
                   in  if dw >= l then
                          calc2 l 0
                       else tw+2*calc2 ((d-dw)/2) w+calc2 (l-d) w
     where calc2 len v0 = let t1 = (v-v0)/a
                              t2 = ((sqrt $ v0*v0+2*a*len) - v0) / a
                          in  if t1 > t2 then t2
                              else t1+(len-dist v0 t1)/v
           dist v0 t = v0*t+a*t*t/2

main = do
     [a, v] <- return . (map read :: [String] -> [Double]) . words =<< getLine
     [l, d, w] <- return . (map read :: [String] -> [Double]) . words =<< getLine
     print $ calc a v l d w

module Main where

main :: IO ()
main = interact $ show . solve . map read . words

solve :: [Double] -> Double
solve [a, v, l, d, w]
    | w >= v || signAccelPath >= d = time2 a 0 v l
    | otherwise = signAccelTime + 2 * time1 a w ((d - signAccelPath) / 2) + time2 a w v (l -d)
  where
    signAccelTime = w / a
    signAccelPath = dist a 0 signAccelTime

dist :: Double -> Double -> Double -> Double
dist a v t = v * t + a * t * t / 2

time1 :: Double -> Double -> Double -> Double
time1 a v l = (sqrt (v^2 + 2 * l * a) - v) / a

time2 :: Double -> Double -> Double -> Double -> Double
time2 a v0 v l
    | fullAccTime > totalTime = totalTime
    | otherwise               = fullAccTime + remainingDist / v
  where
    fullAccTime = (v - v0) / a
    totalTime = time1 a v0 l
    remainingDist = l - dist a v0 fullAccTime


import Control.Applicative

solve :: [Int] -> Int
solve cookies =
  if even (sum cookies) then numEvens else numOdds
  where
    numEvens = length $ filter even cookies
    numOdds = length cookies - numEvens
main :: IO ()
main = do
  getLine
  cookies <- map read . words <$> getLine
  putStrLn $ show $ solve cookies

module Main where

import Control.Monad

main = do
    getLine
    a <- liftM ( map read . words ) getLine :: IO [Int]

    putStrLn $ show $ length $ filter ( if even ( sum a )
        then even
        else odd
        ) a

main = do
   _ <- getLine
   ls <- fmap (map read . words) getLine
   let m2 = sum ls `mod` 2
   print . length $ filter ((==m2) . (`mod`2)) ls

-- vim: set expandtab:

-- cookies
parse [] word = [read $ reverse word :: Int]
parse (' ':xs) word = (read $ reverse word :: Int):(parse xs [])
parse (x:xs) word = parse xs (x:word)

find [] _ a = a
find (x:xs) s akk = if (mod (s-x) 2 == 0) then find xs s (akk + 1) else find xs s akk

main = do
			readLn :: IO Int
			l <- getLine
			print $ find (parse l []) (sum (parse l []) 0) 0
	where sum [] akk = akk
	      sum (x:xs) akk = sum xs $ akk+x

import List
getAns a | (sum a) `mod` 2 == 0=length (filter even a)
         | otherwise =length (filter odd a)
main=interact$show.getAns.map read.tail.words

readWords :: IO [String]
readWords = getLine >>= return . words

reads :: Read a => IO [a]
reads = readWords >>= return . (map read)

solve :: [Int] -> Int
solve xs
  | odd s      =  length (filter odd xs)
  | otherwise  =  length (filter (not . odd) xs)
  where
    s = sum xs

main :: IO ()
main = do
    getLine >> Main.reads >>= print . solve

solve xs = let s = sum xs
           in sum $ map (\x -> 1 - mod (s - x) 2) xs

main = interact $ show.solve.map read.tail.words

main = do s <- getLine
          s <- fmap ((map ((flip mod 2) . read::String->Int)) . words) getLine
          putStrLn $ (show . length . (filter (\x->x==mod (sum s) 2))) s

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

solve :: [Int] -> Int
solve as = length $ filter (if even (sum as) then even else odd) as

main=interact$show.f.map read.tail.words
f x=length[a|a<-x,odd a==odd(sum x)]

main=interact$show.f.map read.tail.words
f x=length[a|a<-x,even a==even(sum x)]

import Control.Arrow
import Data.List
solve ns = if odd o then o else e
    where (e,o) = (length *** length) (partition even ns)
main = print . solve . map read . tail . words =<< getContents

main = do
    getLine
    a <- fmap (map read . words) getLine
    print $ solve a
solve a = if even $ sum a then length $ filter even a else length $ filter odd a

{-# LANGUAGE BangPatterns, ViewPatterns, PatternGuards, ScopedTypeVariables #-}
{-# OPTIONS_GHC -Wall -fno-warn-unused-do-bind -fno-warn-type-defaults -fno-warn-orphans #-}

import Control.Applicative
import Control.Monad

main :: IO ()
main = do
  n   :: Int    <- readLn
  xs  :: [Int]  <- take n . map read . words <$> getLine
  let acc     = sum xs
      answer  = sum $ do
        x <- xs
        guard $ even (acc - x)
        return 1
  print answer

main = do
	getLine
	l <- (map read . words) `fmap` getLine
	let oddEven = if odd $ sum l then odd else even
	print $ length $ filter oddEven l

readInts :: [String] -> [Int]
readInts = map read

main = interact solve

solve :: String -> String
solve input = result
    where result = show $ numberOfWays cookies
          cookies = readInts $ words $ lines input !! 1

numberOfWays :: [Int] -> Int
numberOfWays cookies
    | even total = length . filter even $ cookies
    | otherwise  = length . filter odd $ cookies
    where total = sum cookies

readInts :: [String] -> [Int]
readInts = map read

main = interact solve

solve :: String -> String
solve input = unlines $ result : []
    where result = show $ numberOfWays cookies
          cookies = readInts $ words $ lines input !! 1

numberOfWays :: [Int] -> Int
numberOfWays cookies
    | even total = length . filter even $ cookies
    | otherwise  = length . filter odd $ cookies
    where total = sum cookies

import Data.List
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as BS

reaD :: BS.ByteString -> Int
reaD = fst.fromJust . BS.readInt

solve :: [ Int ] -> BS.ByteString
solve xs = BS.pack.show $ a where
	s = sum xs
	a = helpSolve xs where
	  helpSolve :: [ Int ] -> Int
	  helpSolve [] = 0
	  helpSolve ( y : ys )
            | even $  s - y = 1 + helpSolve ys
	    | otherwise = helpSolve ys

main = BS.interact $ solve . map reaD . BS.words . last . BS.lines

import Control.Applicative
import Data.Char
main= do
	getLine
	s<- map read.words <$> getLine ::IO [Int]
	print $ length $ filter (if even (sum s) then even else odd) s

import Data.List
import qualified Data.Map as Map
import qualified Data.Set as Set
import Data.Char

main :: IO ()
main = do
	_ <- getLine
	line <- getLine
	let	arr = map (\x -> read x:: Int) . words $ line
		total = foldl (+) 0 arr
		ans = foldl (\acc x -> acc + (if (total - x) `mod` 2 == 0 then 1 else 0) ) 0 arr
	print ans

-- Codeforces Beta Round #94
-- Problem A -- Cookies
main = interact work

work :: String -> String
work input = show $ solve . parse $ input

parse :: String -> [Int]
parse = map (\i -> read i :: Int) . words . last . lines

solve :: [Int] -> Int
solve a = length . filter (\x -> x `mod` 2 == (sum a) `mod` 2) $ a

import Data.List
import Debug.Trace (trace)

main2 = print $ solv [1, 2, 2, 3, 4, 4, 4, 2, 2, 2]
main =
	do
		[n] <- getNums
		lines <- getLine
		putStrLn $ show $ solv $ getNumbers lines

solv l = length $ filter (if isE then isEven else not . isEven ) l
	where
		isE = isEven $ sum l

isEven x = x `mod` 2 == 0

------------------utils-----------------------
tr x y = trace ((show x) ++ (show y)) y
tr1 x y = trace ((show x)) y
tr2 y = trace ((show y)) y

(|>) x f = f x

parseInt x = (read x) :: Int

getIndex = zip [0..]

getLines n = repeat getLine |> take n |> sequence

getNums ::  IO [Int]
getNums =
	do
		line <- getLine
		return $ getNumbers line

getNumbers line =
	case index of
		Nothing -> [parseInt line]
		Just i ->
			let (l,r) = splitAt (i + 1) line in
				(parseInt l) : (getNumbers r)
	where
		index = elemIndex ' ' line

splitBy k =
	case index of
		Nothing -> [k]
		Just i ->
			let (l,r) = splitAt (i) k in
				l : splitBy (drop 1 r)
	where
		index = elemIndex ' ' k


import Data.List (sortBy)
import Data.Ord (comparing)

main = do
    n <- fmap read getLine
    xs <- fmap (sortBy (comparing snd) . map (\[k, c] -> (read k, read c) :: (Integer, Integer)) . map words) $ sequence $ replicate n getLine
    getLine
    ps <- fmap ((++ [10 ^ 15]) . map read . words) getLine
    print $ solve xs ps 1 0 0
    where
        solve [] _ _ _ ans = ans
        solve ((k, c):xs) (p:ps) f used ans | used + k < p = solve xs (p:ps) f (used + k) (ans + k * f * c)
                                            | otherwise    = solve ((k - (p - used), c):xs) ps (f + 1) p (ans + (p - used) * f * c)

{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, OverloadedStrings #-}
{-# OPTIONS_GHC -O2 #-}

import Data.List
import Data.Maybe
import Data.Char
import Data.Array.IArray
import Data.Array.Unboxed (UArray)
import Data.Int
import Data.Ratio
import Data.Bits
import Data.Function
import Data.Ord
--import Control.Monad.State
import Control.Monad
import Control.Applicative
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as BS
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Map (Map)
import qualified Data.Map as Map
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.Sequence (Seq, (<|), (|>), (><), ViewL(..), ViewR(..))
import qualified Data.Sequence as Seq
import qualified Data.Foldable as F
import Data.Graph

parseInput = do
    n <- readInt
    kc <- replicateM n ((,) <$> readInt64 <*> readInt64)
    t <- readInt
    p <- replicateM t readInt64
    return (kc, p)
  where
    readInt64 = fromIntegral <$> readInteger :: State ByteString Int64
    readInt = state $ fromJust . BS.readInt . BS.dropWhile isSpace
    readInteger = state $ fromJust . BS.readInteger . BS.dropWhile isSpace
    readString = state $ BS.span (not . isSpace) . BS.dropWhile isSpace
    readLine = state $ BS.span (not . isEoln) . BS.dropWhile isEoln
    isEoln ch = ch == '\r' || ch == '\n'

main = print =<< solve . evalState parseInput <$> BS.getContents

data Chunk a = Chunk Int64 a deriving (Eq, Show)

zipWithChunk :: (a -> b -> c) -> [Chunk a] -> [Chunk b] -> [Chunk c]
zipWithChunk _ [] _ = []
zipWithChunk _ _ [] = []
zipWithChunk f (Chunk 0 _:xs) ys = zipWithChunk f xs ys
zipWithChunk f xs (Chunk 0 _:ys) = zipWithChunk f xs ys
zipWithChunk f (Chunk a x:xs) (Chunk b y:ys) = Chunk c (f x y) :
    zipWithChunk f (Chunk (a - c) x:xs) (Chunk (b - c) y:ys)
  where
    c = min a b

solve (kc', p) = sum [a * b | Chunk a b <- lst]
  where
    kc = sortBy (comparing snd) kc'

    kcs = [Chunk k c | (k, c) <- kc]
    ps = zipWith Chunk (zipWith (-) (p++[10^12]) (0:p)) [1..]

    lst = zipWithChunk (*) kcs ps :: [Chunk Int64]

-- {{{ A minimal State Monad
class (Monad m) => MonadState s m | m -> s where
	get :: m s
	put :: s -> m ()

modify :: (MonadState s m) => (s -> s) -> m ()
modify f = do
	s <- get
	put (f s)

gets :: (MonadState s m) => (s -> a) -> m a
gets f = do
	s <- get
	return (f s)

newtype State s a = State { runState :: s -> (a, s) }

instance Functor (State s) where
	fmap f m = State $ \s -> let
		(a, s') = runState m s
		in (f a, s')

instance Applicative (State s) where
    pure = return
    (<*>) = ap

instance Monad (State s) where
	return a = State $ \s -> (a, s)
	m >>= k  = State $ \s -> let
		(a, s') = runState m s
		in runState (k a) s'

instance MonadState s (State s) where
	get   = State $ \s -> (s, s)
	put s = State $ \_ -> ((), s)

evalState :: State s a -> s -> a
evalState m s = fst (runState m s)

execState :: State s a -> s -> s
execState m s = snd (runState m s)

mapState :: ((a, s) -> (b, s)) -> State s a -> State s b
mapState f m = State $ f . runState m

withState :: (s -> s) -> State s a -> State s a
withState f m = State $ runState m . f

state = State
-- }}}

{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, OverloadedStrings #-}
{-# OPTIONS_GHC -O2 #-}

import Data.List
import Data.Maybe
import Data.Char
import Data.Array.IArray
import Data.Array.Unboxed (UArray)
import Data.Int
import Data.Ratio
import Data.Bits
import Data.Function
import Data.Ord
--import Control.Monad.State
import Control.Monad
import Control.Applicative
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as BS
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Map (Map)
import qualified Data.Map as Map
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.Sequence (Seq, (<|), (|>), (><), ViewL(..), ViewR(..))
import qualified Data.Sequence as Seq
import qualified Data.Foldable as F
import Data.Graph

parseInput = do
    n <- readInt
    kc <- replicateM n ((,) <$> readInt64 <*> readInt64)
    t <- readInt
    p <- replicateM n readInt64
    return (kc, p)
  where
    readInt64 = fromIntegral <$> readInt :: State ByteString Int64
    readInt = state $ fromJust . BS.readInt . BS.dropWhile isSpace
    readInteger = state $ fromJust . BS.readInteger . BS.dropWhile isSpace
    readString = state $ BS.span (not . isSpace) . BS.dropWhile isSpace
    readLine = state $ BS.span (not . isEoln) . BS.dropWhile isEoln
    isEoln ch = ch == '\r' || ch == '\n'

main = print =<< solve . evalState parseInput <$> BS.getContents

data Chunk a = Chunk Int64 a deriving (Eq, Show)

zipWithChunk :: (a -> b -> c) -> [Chunk a] -> [Chunk b] -> [Chunk c]
zipWithChunk _ [] _ = []
zipWithChunk _ _ [] = []
zipWithChunk f (Chunk 0 _:xs) ys = zipWithChunk f xs ys
zipWithChunk f xs (Chunk 0 _:ys) = zipWithChunk f xs ys
zipWithChunk f (Chunk a x:xs) (Chunk b y:ys) = Chunk c (f x y) :
    zipWithChunk f (Chunk (a - c) x:xs) (Chunk (b - c) y:ys)
  where
    c = min a b

solve (kc', p) = sum [a * b | Chunk a b <- lst]
  where
    kc = sortBy (flip $ comparing snd) kc'

    kcs = [Chunk k c | (k, c) <- kc]
    ps = zipWith Chunk (zipWith (-) (p++[10^12]) (0:p)) [1..]

    lst = zipWithChunk (*) kcs ps :: [Chunk Int64]

-- {{{ A minimal State Monad
class (Monad m) => MonadState s m | m -> s where
	get :: m s
	put :: s -> m ()

modify :: (MonadState s m) => (s -> s) -> m ()
modify f = do
	s <- get
	put (f s)

gets :: (MonadState s m) => (s -> a) -> m a
gets f = do
	s <- get
	return (f s)

newtype State s a = State { runState :: s -> (a, s) }

instance Functor (State s) where
	fmap f m = State $ \s -> let
		(a, s') = runState m s
		in (f a, s')

instance Applicative (State s) where
    pure = return
    (<*>) = ap

instance Monad (State s) where
	return a = State $ \s -> (a, s)
	m >>= k  = State $ \s -> let
		(a, s') = runState m s
		in runState (k a) s'

instance MonadState s (State s) where
	get   = State $ \s -> (s, s)
	put s = State $ \_ -> ((), s)

evalState :: State s a -> s -> a
evalState m s = fst (runState m s)

execState :: State s a -> s -> s
execState m s = snd (runState m s)

mapState :: ((a, s) -> (b, s)) -> State s a -> State s b
mapState f m = State $ f . runState m

withState :: (s -> s) -> State s a -> State s a
withState f m = State $ runState m . f

state = State
-- }}}


main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility arg1 arg2 arg3

divisibility k a b
  | k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
  | k > abs (a) && k > abs (b) && a <= 0 && b >= 0 = 1
  | combo1 == 0 && combo2 == 0 = abs (result)
  | k > abs (a) && combo1 /= 0 = divisibility k k b
  | k < abs (a) && combo1 /= 0 = divisibility k (a - combo1 + k) b
  | k > abs (b) && combo2 /= 0 = divisibility k a 0
  | k < abs (b) && combo2 /= 0 = divisibility k a (b - combo2)
  | otherwise = 0
  where combo1 = mod a k
        combo2 = mod b k
        result = 1 + div (b - a) k
        l = 10^18

import Data.Int

main = getLine >>= print . solve . map (read :: String -> Int64) . words

solve [k, a, b]
  | a == 0 = b `div` k + 1
  | 0 < a =  solve [k, 0, b] - solve [k, 0, a - 1]
  | a < 0 && 0 < b = solve [k, 0, -a] + solve [k, 0, b] - 1
  | otherwise = solve [k, -b, -a]

main = getLine >>= print.check.map read.words

check [k, a, b] = b `quot` k - a `quot` k +
    if max a (-b) `mod` k == 0 || b - a > abs (a + b)
            then 1 else 0

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility2 arg1 arg2 arg3

divisibility2 k a b
   | k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
   | mod (abs a) k /= 0 && mod (abs b) k /= 0 = result
   | otherwise = 1 + result
   where l = 10^18
         result = round $ fromInteger (b - a) / fromInteger k

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility2 arg1 arg2 arg3

divisibility2 k a b
   | k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
   | mod (abs a) k /= 0 && mod (abs b) k /= 0 = result
   | otherwise = 1 + result
   where l = 10^18
         result = div (b - a) k

main :: IO ()
main = do
       args <- getLine
       result <- return $ show $ divisibility (read (head $ split args) :: Int)
                                           (read (secd $ split args) :: Int)
                                           (read (last $ split args) :: Int)
       putStrLn result

divisibility k a b = sum [1 | k > 0, a <= l, a >= s, b <= l, b >= s, a <= b,
                              xs <- [a..b], mod xs k == 0]
                     where l = 10^18
                           s = -1*l

split' :: Eq a => a -> [a] -> [[a]]
split' d [] = []
split' d s = x : split' d (drop 1 y) where (x,y) = span (/= d) s

split = split' ' '
secd = head . tail

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility arg1 arg2 arg3

divisibility k a b
  | k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
  | k > abs (a) && k > abs (b) = 1
  | combo1 == 0 && combo2 == 0 = result
  | k > abs (a) && combo1 /= 0 = divisibility k k b
  | k < abs (a) && combo1 /= 0 = divisibility k (a - combo1 + k) b
  | k > abs (b) && combo2 /= 0 = divisibility k a 0
  | k < abs (b) && combo2 /= 0 = divisibility k a (b - combo2)
  | otherwise = 0
  where combo1 = mod a k
        combo2 = mod b k
        result = 1 + div (b - a) k
        l = 10^18

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility arg1 arg2 arg3

divisibility k a b
  | k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
  | k > abs (a) && k > abs (b) && a < 0 && b > 0 = 1
  | combo1 == 0 && combo2 == 0 = abs (result)
  | k > abs (a) && combo1 /= 0 = divisibility k k b
  | k < abs (a) && combo1 /= 0 = divisibility k (a - combo1 + k) b
  | k > abs (b) && combo2 /= 0 = divisibility k a 0
  | k < abs (b) && combo2 /= 0 = divisibility k a (b - combo2)
  | otherwise = 0
  where combo1 = mod a k
        combo2 = mod b k
        result = 1 + div (b - a) k
        l = 10^18

main :: IO ()
main = do
       args <- getLine
       result <- return $ show $ divisibility (read (head $ split args) :: Int)
                                              (read (secd $ split args) :: Int)
                                              (read (last $ split args) :: Int)
       putStr result

divisibility k a b = sum [1 | k > 0, a <= l, a >= s, b <= l, b >= s, a <= b,
                              xs <- [a..b], mod xs k == 0]
                     where l = 10^18
                           s = -1*l

split' :: Eq a => a -> [a] -> [[a]]
split' d [] = []
split' d s = x : split' d (drop 1 y) where (x,y) = span (/= d) s

split = split' ' '
secd = head . tail

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ floor $ divisibility arg1 arg2 arg3

divisibility k a b
  | combo1 == 0 && combo2 == 0 = fromIntegral (b - a + k) / fromIntegral k
  | combo1 /= 0 = divisibility k (a + 1) b
  | otherwise = divisibility k a (b - 1)
  where combo1 = mod a k
        combo2 = mod b k

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility arg1 arg2 arg3

divisibility k a b
  | k > a || k > b || k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
  | combo1 == 0 && combo2 == 0 = result
  | combo1 /= 0 = divisibility k (a + 1) b
  | combo1 == 0 = divisibility k a (b - 1)
  where combo1 = mod a k
        combo2 = mod b k
        result = 1 + div (b - a) k
        l = 10^18

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility2 arg1 arg2 arg3

divisibility2 k a b
   | k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
   | mod (abs a) k /= 0 && mod (abs a) k /= 0 = result
   | otherwise = 1 + result
   where l = 10^18
         result = div (b - a) k

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility2 arg1 arg2 arg3

divisibility2 k a b
   | k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
   | (abs a < k || abs b < k) && a /= 0 && b /= 0  = result
   | otherwise = 1 + result
   where l = 10^18
         result = div (b - a) k

main :: IO Int
main = do
     args <- getLine
     return $ divisibility (read (head (split ' ' args)) :: Int)
                           (read (head $ tail (split ' ' args)) :: Int)
                           (read (last (split ' ' args)) :: Int)

divisibility k a b = sum [1 | k > 0, a <= l, a >= s, b <= l, b >= s, a <= b,
                              xs <- [a..b], mod xs k == 0]
                     where l = 10^18
                           s = -1*l

split :: Eq a => a -> [a] -> [[a]]
split d [] = []
split d s = x : split d (drop 1 y) where (x,y) = span (/= d) s

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility arg1 arg2 arg3

divisibility k a b = (b - a + 1) / k

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility arg1 arg2 arg3

divisibility k a b
  | k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
  | k > abs (a) && k > abs (b) = 1
  | combo1 == 0 && combo2 == 0 = abs (result)
  | k > abs (a) = divisibility k k b
  | k < abs (a) = divisibility k (a - combo1 + k) b
  | k > abs (b) = divisibility k a 0
  | k < abs (b) = divisibility k a (b - combo2)
  | otherwise = 0
  where combo1 = mod a k
        combo2 = mod b k
        result = 1 + div (b - a) k
        l = 10^18

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility arg1 arg2 arg3

divisibility k a b
  | k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
  | k > abs (a) && k > abs (b) = 1
  | combo1 == 0 && combo2 == 0 = result
  | k > abs (a) = divisibility k k b
  | k < abs (a) = divisibility k (a - combo1 + k) b
  | k > abs (b) = divisibility k a 0
  | k < abs (b) = divisibility k a (b - combo2)
  | otherwise = 0
  where combo1 = mod a k
        combo2 = mod b k
        result = 1 + div (b - a) k
        l = 10^18

main :: IO ()
main = do
     args <- getLine
     print $ divisibility (read (head (split ' ' args)) :: Int)
                           (read (head $ tail (split ' ' args)) :: Int)
                           (read (last (split ' ' args)) :: Int)

divisibility k a b = sum [1 | k > 0, a <= l, a >= s, b <= l, b >= s, a <= b,
                              xs <- [a..b], mod xs k == 0]
                     where l = 10^18
                           s = -1*l

split :: Eq a => a -> [a] -> [[a]]
split d [] = []
split d s = x : split d (drop 1 y) where (x,y) = span (/= d) s

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility2 arg1 arg2 arg3

divisibility2 k a b
   | k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
   | abs a < k && abs b < k && a /= 0 && b /= 0  = result
   | otherwise = 1 + result
   where l = 10^18
         result = div (b - a) k

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility arg1 arg2 arg3

divisibility k a b
  | (k > abs (a) && k > abs (b)) || k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
  | a == 0 && b == 0 = 1
  | combo1 == 0 && combo2 == 0 = result
  | k > abs (a) = divisibility k k b
  | k < abs (a) = divisibility k (a - combo1 + k) b
  | k > abs (b) = divisibility k a 0
  | k < abs (b) = divisibility k a (b - combo2)
  | otherwise = 0
  where combo1 = mod a k
        combo2 = mod b k
        result = 1 + div (b - a) k
        l = 10^18

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility arg1 arg2 arg3

divisibility :: Int -> Int -> Int -> Int
divisibility 1 a b = b - a + 1
divisibility k a b = length [x | x <- [a..b], mod x k == 0]

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility arg1 arg2 arg3

divisibility 1 a b = b - a + 1
divisibility k a b = length [x | x <- [a..b], mod x k == 0]

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility arg1 arg2 arg3

divisibility k a b
  | (k > abs (a) && k > abs (b)) || k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
  | a == 0 && b == 0 = 0
  | combo1 == 0 && combo2 == 0 = result
  | k > abs (a) = divisibility k k b
  | k < abs (a) = divisibility k (a - combo1 + k) b
  | k > abs (b) = divisibility k a 0
  | k < abs (b) = divisibility k a (b - combo2)
  | otherwise = 0
  where combo1 = mod a k
        combo2 = mod b k
        result = 1 + div (b - a) k
        l = 10^18

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility2 arg1 arg2 arg3

divisibility2 k a b
   | k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
   | abs a < k || abs b < k = result
   | otherwise = 1 + result
   where l = 10^18
         result = div (b - a) k

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility arg1 arg2 arg3

divisibility k a b
  | k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
  | k > abs (a) && k > abs (b) && a < 0 = 1
  | combo1 == 0 && combo2 == 0 = abs (result)
  | k > abs (a) && combo1 /= 0 = divisibility k k b
  | k < abs (a) && combo1 /= 0 = divisibility k (a - combo1 + k) b
  | k > abs (b) && combo2 /= 0 = divisibility k a 0
  | k < abs (b) && combo2 /= 0 = divisibility k a (b - combo2)
  | otherwise = 0
  where combo1 = mod a k
        combo2 = mod b k
        result = 1 + div (b - a) k
        l = 10^18

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility arg1 arg2 arg3

divisibility k 0 0 = 0
divisibility k a b
  | (k > abs (a) && k > abs (b)) || k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
  | combo1 == 0 && combo2 == 0 = result
  | k > abs (a) = divisibility k k b
  | k < abs (a) = divisibility k (a - combo1 + k) b
  | k > abs (b) = divisibility k a 0
  | k < abs (b) = divisibility k a (b - combo2)
  | otherwise = 0
  where combo1 = mod a k
        combo2 = mod b k
        result = 1 + div (b - a) k
        l = 10^18

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility2 arg1 arg2 arg3

divisibility2 k a b
   | k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
   | mod (abs b) k /= 0 = result
   | otherwise = 1 + result
   where l = 10^18
         result = div (b - a) k

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility2 arg1 arg2 arg3

divisibility2 k a b
   | k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
   | (mod (abs a) k == 0) || mod (abs b) k == 0 = 1 + result
   | otherwise = result
   where l = 10^18
         result = div (b - a) k

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility2 arg1 arg2 arg3

divisibility2 k a b
   | k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
   | otherwise = 1 + div (b - a) k
   where l = 10^18

main = getLine >>= print.check.map read.words

check [k, a, b] = b `div` k - a `div` k +
    if min a b `mod` k == 0 || b - a > abs (a + b)
            then 1 else 0

main = getLine >>= print.check.map read.words

check [k, a, b] = segment `div` k +
    if min a b `mod` k == 0 || segment > abs (a + b)
            then 1
            else 0
    where segment = b - a

main = getLine >>= print.check.map read.words

check [k, a, b] = b `quot` k - a `quot` k +
    if min a b `mod` k == 0 || b - a > abs (a + b)
            then 1 else 0

main :: IO ()
main = do
       argsStr <- getLine
       let [arg1, arg2, arg3] = map read $ words argsStr
       print $ divisibility arg1 arg2 arg3

divisibility k a b
  | (k > abs (a) && k > abs (b)) || k < 0 || k > l || a < (-1)*l || a > l || b < (-1)*l || b > l = 0
  | combo1 == 0 && combo2 == 0 = result
  | k > abs (a) = divisibility k k b
  | k < abs (a) = divisibility k (a - combo1 + k) b
  | k > abs (b) = divisibility k a 0
  | k < abs (b) = divisibility k a (b - combo2)
  | otherwise = 0
  where combo1 = mod a k
        combo2 = mod b k
        result = 1 + div (b - a) k
        l = 10^18


process :: Int -> Int
process n
    | n < 10 = n
    | otherwise = n-m + process (d+m)
    where (d,m) = n `divMod` 10

readInt :: String -> Int
readInt = read

run :: Int -> IO ()
run 0 = return ()
run t = do
    n <- fmap readInt getLine
    print $ process n
    run (t-1)

main :: IO ()
main = do
    t <- fmap readInt getLine
    run t

import Control.Applicative
import Control.Monad
import Data.List
import Data.Maybe
import Data.Array

process n 0 = n
process n a | a<10 = n+a
            |otherwise = process (n+ 10*(div a 10)) ((mod a 10) + (div a 10))

onecase = do
		a<-read <$> getLine ::IO Integer
		print $ process 0 a

main = do
		n<-read <$> getLine ::IO Int
		replicateM_ n onecase

import Control.Monad
import Data.Ratio
import Data.Char

solve :: Int -> Int
solve n
  | n < 10 = n
  | otherwise = a + solve ((n - a) + (floor $ a % 10))
  where
    rest = n - a
    ds = map digitToInt $ show n
    l = length ds
    a :: Int
    a = (head ds) * 10 ^ (l - 1)

main = do
  t <- readLn :: IO Int
  replicateM_ t $ do
    n <- readLn :: IO Int
    print $ solve n

import Control.Monad

getInt = fmap read getLine :: IO Int

main = do
    t <- getInt
    replicateM_ t $ do
        n <- getInt
        print $ n `div` 9 * 10 + n `mod` 9 - if n `mod` 9 == 0 then 1 else 0

import Control.Monad

getInt = fmap read getLine :: IO Int

main = do
    t <- getInt
    replicateM_ t $ do
        n <- getInt
        if n > 9 then print $ n `div` 9 * 10 + n `mod` 9
                 else print n


import Data.List (findIndex)
import Control.Monad (replicateM_)

main = read <$> getLine >>= flip replicateM_ singleCase
singleCase = getLine >>= print . minToEven 0

searchPair :: (Ord a) => Int -> [a] -> Int -> [(Int, [a])]
searchPair m [] _ = []
searchPair m (x:xs) c
  | c >= m = []
  |otherwise =
   case findWithin x xs m c of
     Just (numBw, rest) -> (numBw, rest) : searchPair numBw xs (c+1)
     Nothing -> searchPair m xs (c+1)

findWithin :: (Ord a) => a -> [a] -> Int -> Int -> Maybe (Int, [a])
findWithin _ [] _ _ = Nothing
findWithin e (x:xs) m c | c >= m = Nothing
                        | e == x = Just (c, xs)
                        | otherwise = findWithin e xs m (c+1)

minToEven :: Int -> String -> Int
minToEven a [] = a
minToEven a [x] = if x == '\r' then a else a+1
minToEven a (x:y:xs)
  | x == y = minToEven a xs
  | otherwise = case findIndex (x==) (y:xs) of
                  Nothing -> minToEven (a+1) (y:xs)
                  Just i -> case searchPair i (y:xs) 1 of
                              [] -> minToEven (a+i) (drop i xs)
                              pairs -> let (cost, rest) = minimum pairs in
                                         minToEven (a+cost) rest

import Control.Monad
import Control.Monad.State
import qualified Data.Set as Set

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

solvet :: IO ()
solvet = do
    str <- getLine
    let (s, r) = foldl (flip makeEven) (Set.empty, 0) str
    print (Set.size s + r)

makeEven :: Char -> (Set.Set Char, Int) -> (Set.Set Char, Int)
makeEven c (s, res) =
    if Set.member c s
        then (Set.empty, res + Set.size s - 1)
        else (Set.insert c s, res)

import           Control.Monad
import           Control.Monad.State
import Data.Set ( empty, insert, member, size, Set )

-- main = undefined

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

solvet :: IO ()
solvet = do
  str <- getLine
  let (MyState s r) = foldl (\s c -> execState (makeEven c) s) (MyState empty 0) str
  print (size s + r)

data MyState =
  MyState
    { msSet    :: Set Char
    , msResult :: Int
    }

makeEven :: Char -> State MyState ()
makeEven c = do
  (MyState s res) <- get
  if c `member` s
    then do
      put $ MyState empty (res + size s - 1)
    else do
      let newS = c `insert` s
      put $ MyState newS res

import qualified Data.ByteString.Char8 as B
import Data.List (findIndex)
import Control.Monad (replicateM_)

main = read <$> getLine >>= flip replicateM_ singleCase
singleCase = B.getLine >>= print . minToEven 0 . B.unpack

searchPair :: (Ord a) => Int -> [a] -> Int -> [(Int, [a])]
searchPair m [] _ = []
searchPair m (x:xs) c =
  case findWithin x xs m c of
    Just (numBw, rest) -> (numBw, rest) : searchPair m xs (c+1)
    Nothing -> searchPair m xs (c+1)

findWithin :: (Ord a) => a -> [a] -> Int -> Int -> Maybe (Int, [a])
findWithin _ [] _ _ = Nothing
findWithin e (x:xs) m c | c >= m = Nothing
                        | e == x = Just (c, xs)
                        | otherwise = findWithin e xs m (c+1)

minToEven :: Int -> String -> Int
minToEven a [] = a
minToEven a [_] = a+1
minToEven a (x:y:xs)
  | x == y = minToEven a xs
  | otherwise = case findIndex (x==) (y:xs) of
                  Nothing -> minToEven (a+1) (y:xs)
                  Just i -> case searchPair i (y:xs) 1 of
                              [] -> minToEven (a+i) (drop i xs)
                              pairs -> let (cost, rest) = minimum pairs in
                                         minToEven (a+cost) rest

import Data.List (findIndex)
import Control.Monad (replicateM_)

main = read <$> getLine >>= flip replicateM_ singleCase
singleCase = getLine >>= print . minToEven 0

searchPair :: (Ord a) => Int -> [a] -> Int -> [(Int, [a])]
searchPair m [] _ = []
searchPair m (x:xs) c =
  case findWithin x xs m c of
    Just (numBw, rest) -> (numBw, rest) : searchPair m xs (c+1)
    Nothing -> searchPair m xs (c+1)

findWithin :: (Ord a) => a -> [a] -> Int -> Int -> Maybe (Int, [a])
findWithin _ [] _ _ = Nothing
findWithin e (x:xs) m c | c >= m = Nothing
                        | e == x = Just (c, xs)
                        | otherwise = findWithin e xs m (c+1)

minToEven :: Int -> String -> Int
minToEven a [] = a
minToEven a [_] = a+1
minToEven a (x:y:xs)
  | x == y = minToEven a xs
  | otherwise = case findIndex (x==) (y:xs) of
                  Nothing -> minToEven (a+1) (y:xs)
                  Just i -> case searchPair i (y:xs) 0 of
                              [] -> minToEven (a+i) (drop i xs)
                              pairs -> let (cost, rest) = minimum pairs in
                                         minToEven (a+cost) rest


-- @betaveros :: vim:set fdm=marker:
{-# LANGUAGE LambdaCase, NPlusKPatterns, TupleSections #-}
{-# OPTIONS_GHC -fno-warn-unused-imports -fno-warn-missing-signatures #-}
-- import ALL the things! {{{
import Control.Applicative
import Control.Arrow
import Control.Exception
import Control.Monad
import Control.Monad.ST

import Data.Bits
import Data.Char
import Data.Either
import Data.Function
import Data.IORef
import Data.List
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.STRef
import Data.String
import Data.Tuple

import qualified Data.Map as Map
import Data.Map (Map)
import qualified Data.Set as Set
import Data.Set (Set)
import qualified Data.Sequence as Seq
import Data.Sequence (Seq, (<|), (|>), (><))

import Debug.Trace
import Text.Printf
-- }}}
-- silly utilities {{{
(#) = flip ($)
infixl 0 #

glength :: (Num b) => [a] -> b
glength = genericLength

readInt     = read :: String -> Int
readInteger = read :: String -> Integer
-- (!?) :: (Ord k) => Map k v -> k -> Maybe v
-- (!?) = flip Map.lookup
histogram :: (Ord a, Num b) => [a] -> Map a b
histogram = Map.fromListWith (+) . map (,1)

(!>) :: Seq a -> Int -> a
(!>) = Seq.index
-- }}}
-- input and output {{{
inputInt     = (read <$> getLine) :: IO Int
inputInteger = (read <$> getLine) :: IO Integer
inputDouble  = (read <$> getLine) :: IO Double

inputRow :: (Read a) => IO [a]
inputRow = map read . words <$> getLine
inputInts     = inputRow :: IO [Int]
inputIntegers = inputRow :: IO [Integer]
inputDoubles  = inputRow :: IO [Double]
-- }}}

pushStart s = let (ls, rs) = span (== '.') s in case rs of
	"" -> length ls
	('L':xs) -> push xs
	('R':xs) -> length ls + pushR xs
	_ -> error "what?"

pushR s = let (ls, rs) = span (== '.') s in case rs of
	"" -> 0
	('L':xs) -> (length ls `rem` 2) + push xs
	_ -> error "what?"

push s = let (ls, rs) = span (== '.') s in case rs of
	"" -> length ls
	('R':xs) -> length ls + pushR xs
	_ -> error "what?"

main :: IO ()
main = do
	void getLine
	print . pushStart =<< getLine

data LR = L | R | Vertical deriving Eq

main = do
    n <- getLine
    dominos <- getLine
    putStrLn.show $ countUp Vertical (0,0) dominos

countUp :: LR -> (Int,Int) -> String -> Int
countUp Vertical (amout,tmp) [] = amout+tmp
countUp L (amout,tmp) [] = amout+tmp
countUp R (amout,tmp) [] = amout
countUp Vertical (amout,tmp) ('.':xs) = countUp Vertical (amout,tmp+1) xs
countUp Vertical (amout,tmp) ('L':xs) = countUp L (amout,0) xs
countUp Vertical (amout,tmp) ('R':xs) = countUp R (amout+tmp,0) xs

countUp L (amout,tmp) ('.':xs) = countUp Vertical (amout+1,0) xs
countUp L (amout,tmp) ('R':xs) = countUp R (amout,0) xs

countUp R (amout,tmp) ('.':xs) = countUp R (amout,tmp+1) xs
countUp R (amout,tmp) ('L':xs) = countUp L (amout + if odd tmp then 1 else 0,0) xs

main=interact$show.solve.parse.(!!1).lines
data A = L | R | N !Int
parse :: String -> [A]
parse ('R':cs) = R : parse cs
parse ('L':cs) = L : parse cs
parse cs@(_:_) | (xs, ys) <- span ('.'==) cs = N(length xs) : parse ys
parse [] = []

solve :: [A] -> Int
solve xs = go 0 xs
   where
     go acc (R:N n:L:rest)
       | even n = go acc rest
       | otherwise = go (acc+1) rest
     go acc (R:N n:R:rest) = go acc (R:rest)
     go acc (R:L:rest) = go acc rest
     go acc (R:R:rest) = go acc (R:rest)
     go acc (L:rest) = go acc rest
     go acc (N n:R:rest) = go (acc+n) (R:rest)
     go acc (N n:L:rest) = go acc rest
     go acc [N n] = acc + n
     go acc [R, N n] = acc
     go acc [R] = acc
     go acc [] = acc

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

main = getLine >> getLine >>= print . solve

solve :: String -> Int
solve dominoes
    | all (== '.') dominoes = length dominoes
    | otherwise             = solve $ func dominoes

func :: String -> String
func ('R':'.':'L':xs) = '.' : func xs
func ('R':'.':xs)     = 'R' : func xs
func ('.':'L':xs)     = 'L' : func xs
func ('.':xs)         = '.' : func xs
func ( _ :xs)         = func xs
func xs               = xs

main = interact $ show . f . span (=='.') . head . tail . lines
f (a, []) = length a
f (a, b:bs)
 | b == 'L' = g bs
 | b == 'R' = length a + g (b:bs)
g [] = 0
g (x:xs)
 | x == 'R' && null (dropWhile (=='.') xs) = 0
 | x == 'R' = let (a, b) = span (=='.') xs in mod (length a) 2 + g (tail b)
 | x == '.' = let (a, b) = span (=='.') (x:xs) in length a + g b

solve :: [ (Char,Int) ] -> Int
solve []                = 0
solve (('L',b):rest)    = solve' b rest
solve xs@(('R',a):rest) = solve' 0 xs

solve' :: Int -> [ (Char,Int) ] -> Int
solve' b' (('R',a):('L',b):rest) = a-b'-1 + (if even (b-a) then 1 else 0) + solve' b rest
solve' b' (('R',a):_)            = a-b'-1
solve' b' []                     = 0 -- never happens

(-->) = flip fmap

main = do
  n <- getLine --> read :: IO Int
  doms <- getLine
  let pairs = filter (\(d,a) -> d == 'L' || d == 'R') (zip doms [1..])
  print $ solve (pairs ++ [('R',n+1)])

data LR = L | R | Vertical deriving Eq

main = do
    n <- getLine
    dominos <- getLine
    putStrLn.show $ countUp Vertical (0,0) dominos

countUp :: LR -> (Int,Int) -> String -> Int
countUp _ (amout,tmp) [] = amout+tmp
countUp Vertical (amout,tmp) ('.':xs) = countUp Vertical (amout,tmp+1) xs
countUp Vertical (amout,tmp) ('L':xs) = countUp L (amout,0) xs
countUp Vertical (amout,tmp) ('R':xs) = countUp R (amout+tmp,0) xs

countUp L (amout,tmp) ('.':xs) = countUp Vertical (amout+1,0) xs
countUp L (amout,tmp) ('R':xs) = countUp R (amout,0) xs

countUp R (amout,tmp) ('.':xs) = countUp R (amout,tmp+1) xs
countUp R (amout,tmp) ('L':xs) = countUp L (amout + if odd tmp then 1 else 0,0) xs