{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
            fs = sortBy (comparing (fst . fst)) (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup = process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise = out ++ (process frogs'' rest' mosquitos)
          where
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# OPTIONS_GHC -O3 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

import Control.Monad (when)
import Data.Array (array, elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs -- :: ST s (STArray s Int (Int, Int))
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        s <- readArray a $ (l + r) `div` 2

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
            fs = sort gen (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(c, t) -> show c ++ " " ++ show t) . elems . array (1, n) $ map (\(Frog _ _ i c t) -> (i, (c, t))) result

{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

import Control.Monad (when)
import Data.Array (array, elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs -- :: ST s (STArray s Int (Int, Int))
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        g <- readSTRef gr
        let (x, g') = randomR (l, r-1) g
        writeSTRef gr g'

        s <- readArray a x

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
            fs = sort gen (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(c, t) -> show c ++ " " ++ show t) . elems . array (1, n) $ map (\(Frog _ _ i c t) -> (i, (c, t))) result

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
            fs = sort (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es
              where
                es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
                fs = sort $ zip frogsIn [1..]

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos) = process' lookup
          where
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            process' Nothing =  process frogs (mosquito `Set.insert` rest) mosquitos
            process' (Just frog@(Frog x _ _ _ _)) = out ++ (process frogs'' rest'' mosquitos)
              where
                (frog', rest'') = eat frog rest' mosqs
                  where
                    rest' = mosquito `Set.insert` rest

                    mosqs = mosq:(Set.elems . snd $ Set.split mosq rest')
                      where
                        mosq = fromJust $ Set.lookupGE (Mosq x 0 0) rest'

                    eat frog@(Frog x y i c t) rest mosqs
                      | null mosqs || y < p = (frog, rest)
                      | otherwise = eat frog' rest' $ tail mosqs
                      where
                        mosq@(Mosq p b _) = head mosqs
                        frog' = Frog x (y+b) i (c+1) (t+b)
                        rest' = mosq `Set.delete` rest

                (frogs'', out) = clean frogs' [] frogsList
                  where
                    frogs' = frog' `Set.insert` frogs
                    frogsList = Set.elems . snd $ Set.split frog' frogs'

                    Frog _ y _ _ _ = frog'

                    clean frogs out [] = (frogs, out)
                    clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                      | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                      | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                      | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# OPTIONS_GHC -O3 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

import Control.Monad (when)
import Data.Array (elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs -- :: ST s (STArray s Int (Int, Int))
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        s <- readArray a $ (l + r) `div` 2

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
            fs = sort gen (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)

import Control.Monad (when)
import Data.Array (array, elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random
import System.IO hiding (getContents, getLine)

import Data.ByteString.Lazy.Builder  -- requires bytestring-0.10.x
import Data.ByteString.Lazy.Builder.ASCII -- omit for bytestring-0.10.2.x
import Data.Monoid

data Frog = Frog { frogX :: !Int, frogY :: !Int, frogI :: !Int, frogC :: !Int, frogT :: !Int } deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        g <- readSTRef gr
        let (x, g') = randomR (l, r-1) g
        writeSTRef gr g'

        s <- readArray a x

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            z = zipWith (\frog y -> frog { frogX = max (frogX frog) (y+1) }) fs ys
              where
                fs = sort gen $ zipWith (\(x, t) i -> Frog x (x+t) i 0 t) frogsIn [1..]
                ys = scanl max (-1) $ map frogY fs

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos) = process' lookup
          where
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            process' Nothing = process frogs (mosquito `Set.insert` rest) mosquitos
            process' (Just frog@(Frog x _ _ _ _)) =  out ++ (process frogs'' rest' mosquitos)
              where
                (frog', rest') = eat frog rest (mosquito:mosqs)
                  where
                    frog@(Frog x _ _ _ _) = fromJust lookup

                    mosqs = (case lookup of
                                    Nothing -> []
                                    Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                            )
                      where lookup = Set.lookupGE (Mosq x 0 0) rest

                    eat frog@(Frog x y i c t) rest mosqs
                      | null mosqs || y < p = (frog, rest)
                      | otherwise = eat frog' rest' $ tail mosqs
                      where
                        mosq@(Mosq p b _) = head mosqs
                        frog' = Frog x (y+b) i (c+1) (t+b)
                        rest' = mosq `Set.delete` rest

                (frogs'', out) = clean frogs' [] frogsList
                  where
                    frogs' = frog' `Set.insert` frogs
                    frogsList = Set.elems . snd $ Set.split frog' frogs'

                    Frog _ y _ _ _ = frog'

                    clean frogs out [] = (frogs, out)
                    clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                      | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                      | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                      | otherwise = (frogs, out)

  let a = elems . array (1, n) $ map (\(Frog _ _ i c t) -> (i, (c, t))) result

  let unlines_ = mconcat . map (<> charUtf8 '\n')
  hPutBuilder stdout $ unlines_ $ map (\(c, t) -> intDec c <> charUtf8 ' ' <> intDec t) a

{-# OPTIONS_GHC -O3 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
            fs = sort (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy, sort)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)

import Control.Monad (when)
import Data.Array (array, elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random
import System.IO hiding (getContents, getLine)

import Data.ByteString.Lazy.Builder  -- requires bytestring-0.10.x
import Data.ByteString.Lazy.Builder.ASCII -- omit for bytestring-0.10.2.x
import Data.Monoid

data Frog = Frog { frogX :: !Int, frogY :: !Int, frogI :: !Int, frogC :: !Int, frogT :: !Int } deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort2 gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        g <- readSTRef gr
        let (x, g') = randomR (l, r-1) g
        writeSTRef gr g'

        s <- readArray a x

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            z = zipWith (\frog y -> frog { frogX = max (frogX frog) (y+1) }) fs ys
              where
                fs = sort $ zipWith (\(x, t) i -> Frog x (x+t) i 0 t) frogsIn [1..]
                ys = scanl max (-1) $ map frogY fs

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos) = process' lookup
          where
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            process' Nothing = process frogs (mosquito `Set.insert` rest) mosquitos
            process' (Just frog@(Frog x _ _ _ _)) =  out ++ (process frogs'' rest' mosquitos)
              where
                (frog', rest') = eat frog rest (mosquito:mosqs)
                  where
                    frog@(Frog x _ _ _ _) = fromJust lookup

                    mosqs = (case lookup of
                                    Nothing -> []
                                    Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                            )
                      where lookup = Set.lookupGE (Mosq x 0 0) rest

                    eat frog@(Frog x y i c t) rest mosqs
                      | null mosqs || y < p = (frog, rest)
                      | otherwise = eat frog' rest' $ tail mosqs
                      where
                        mosq@(Mosq p b _) = head mosqs
                        frog' = Frog x (y+b) i (c+1) (t+b)
                        rest' = mosq `Set.delete` rest

                (frogs'', out) = clean frogs' [] frogsList
                  where
                    frogs' = frog' `Set.insert` frogs
                    frogsList = Set.elems . snd $ Set.split frog' frogs'

                    Frog _ y _ _ _ = frog'

                    clean frogs out [] = (frogs, out)
                    clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                      | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                      | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                      | otherwise = (frogs, out)

  let a = elems . array (1, n) $ map (\(Frog _ _ i c t) -> (i, (c, t))) result

  let unlines_ = mconcat . map (<> charUtf8 '\n')
  hPutBuilder stdout $ unlines_ $ map (\(c, t) -> intDec c <> charUtf8 ' ' <> intDec t) a

{-# OPTIONS_GHC -O3 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

import Control.Monad (when)
import Data.Array (elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs -- :: ST s (STArray s Int (Int, Int))
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        s <- readArray a $ (l + r) `div` 2

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
            fs = sort gen (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromDistinctAscList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)
  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)

import Control.Monad (when)
import Data.Array (array, elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random

data Frog = Frog { frogX :: !Int, frogY :: !Int, frogI :: !Int, frogC :: !Int, frogT :: !Int } deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        g <- readSTRef gr
        let (x, g') = randomR (l, r-1) g
        writeSTRef gr g'

        s <- readArray a x

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            z = zipWith (\frog y -> frog { frogX = max (frogX frog) (y+1) }) fs ys
              where
                fs = sort gen $ zipWith (\(x, t) i -> Frog x (x+t) i 0 t) frogsIn [1..]
                ys = scanl max (-1) $ map frogY fs

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos) = process' lookup
          where
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            process' Nothing = process frogs (mosquito `Set.insert` rest) mosquitos
            process' (Just frog@(Frog x _ _ _ _)) =  out ++ (process frogs'' rest' mosquitos)
              where
                (frog', rest') = eat frog rest (mosquito:mosqs)
                  where
                    frog@(Frog x _ _ _ _) = fromJust lookup

                    mosqs = (case lookup of
                                    Nothing -> []
                                    Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                            )
                      where lookup = Set.lookupGE (Mosq x 0 0) rest

                    eat frog@(Frog x y i c t) rest mosqs
                      | null mosqs || y < p = (frog, rest)
                      | otherwise = eat frog' rest' $ tail mosqs
                      where
                        mosq@(Mosq p b _) = head mosqs
                        frog' = Frog x (y+b) i (c+1) (t+b)
                        rest' = mosq `Set.delete` rest

                (frogs'', out) = clean frogs' [] frogsList
                  where
                    frogs' = frog' `Set.insert` frogs
                    frogsList = Set.elems . snd $ Set.split frog' frogs'

                    Frog _ y _ _ _ = frog'

                    clean frogs out [] = (frogs, out)
                    clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                      | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                      | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                      | otherwise = (frogs, out)

  putStr . unlines . map (\(c, t) -> show c ++ " " ++ show t) . elems . array (1, n) $ map (\(Frog _ _ i c t) -> (i, (c, t))) result

{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)

import Control.Monad (when)
import Data.Array (array, elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        g <- readSTRef gr
        let (x, g') = randomR (l, r-1) g
        writeSTRef gr g'

        s <- readArray a x

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
            fs = sort gen (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos) = process' lookup
          where
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            process' Nothing = process frogs (mosquito `Set.insert` rest) mosquitos
            process' (Just frog@(Frog x _ _ _ _)) =  out ++ (process frogs'' rest' mosquitos)
              where
                (frog', rest') = eat frog rest (mosquito:mosqs)
                  where
                    frog@(Frog x _ _ _ _) = fromJust lookup

                    mosqs = (case lookup of
                                    Nothing -> []
                                    Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                            )
                      where lookup = Set.lookupGE (Mosq x 0 0) rest

                    eat frog@(Frog x y i c t) rest mosqs
                      | null mosqs || y < p = (frog, rest)
                      | otherwise = eat frog' rest' $ tail mosqs
                      where
                        mosq@(Mosq p b _) = head mosqs
                        frog' = Frog x (y+b) i (c+1) (t+b)
                        rest' = mosq `Set.delete` rest

                (frogs'', out) = clean frogs' [] frogsList
                  where
                    frogs' = frog' `Set.insert` frogs
                    frogsList = Set.elems . snd $ Set.split frog' frogs'

                    Frog _ y _ _ _ = frog'

                    clean frogs out [] = (frogs, out)
                    clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                      | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                      | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                      | otherwise = (frogs, out)

  putStr . unlines . map (\(c, t) -> show c ++ " " ++ show t) . elems . array (1, n) $ map (\(Frog _ _ i c t) -> (i, (c, t))) result

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max 0 $ map (\((x, t), _) -> x + t) fs
            fs = sort (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x s) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog y y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max 0 $ map (\((x, t), _) -> x + t) fs
            fs = sort (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x s) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y > y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y > x2 = ((Frog y y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max 0 $ map (\((x, t), _) -> x + t) fs
            fs = sort (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y > y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y > x2 = ((Frog y y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max 0 $ map (\((x, t), _) -> x + t) fs
            fs = sort (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)

data Frog = Frog { frogX :: !Int, frogY :: !Int, frogIndex :: !Int, frogCount :: !Int, frogT :: !Int } deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            z = zipWith (\frog y -> frog { frogY = max (frogY frog) (y+1) }) fs ys
              where
                ys = scanl max (-1) $ map frogY fs
                fs = sortBy (comparing frogX) $ zipWith (\(x, t) i -> Frog x (x+t) i 0 t) frogsIn [1..]

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos) = process' lookup
          where
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            process' Nothing =  process frogs (mosquito `Set.insert` rest) mosquitos
            process' (Just frog@(Frog x _ _ _ _)) = out ++ (process frogs'' rest'' mosquitos)
              where
                (frog'@(Frog _ y _ _ _), rest'') = eat frog rest' mosqs
                  where
                    rest' = mosquito `Set.insert` rest

                    mosqs = mosq:(Set.elems . snd $ Set.split mosq rest')
                      where
                        mosq = fromJust $ Set.lookupGE (Mosq x 0 0) rest'

                    eat frog rest [] = (frog, rest)
                    eat frog@(Frog x y i c t) rest (mosq@(Mosq p b _):mosqs)
                      | y < p = (frog, rest)
                      | otherwise = eat frog' rest' mosqs
                      where
                        frog' = Frog x (y+b) i (c+1) (t+b)
                        rest' = mosq `Set.delete` rest

                (frogs'', out) = clean frogs' [] frogsList
                  where
                    frogs' = frog' `Set.insert` frogs
                    frogsList = Set.elems . snd $ Set.split frog' frogs'

                    clean frogs out [] = (frogs, out)
                    clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                      | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                      | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                      | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)

import Control.Monad (when)
import Data.Array (array, elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random

data Frog = Frog { frogX :: !Int, frogY :: !Int, frogI :: !Int, frogC :: !Int, frogT :: !Int } deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        g <- readSTRef gr
        let (x, g') = randomR (l, r-1) g
        writeSTRef gr g'

        s <- readArray a x

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            z = zipWith (\frog y -> frog { frogY = max (frogY frog) (y+1) }) fs ys
              where
                fs = sort gen $ zipWith (\(x, t) i -> Frog x (x+t) i 0 t) frogsIn [1..]
                ys = scanl max (-1) $ map frogY fs

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos) = process' lookup
          where
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            process' Nothing = process frogs (mosquito `Set.insert` rest) mosquitos
            process' (Just frog@(Frog x _ _ _ _)) =  out ++ (process frogs'' rest' mosquitos)
              where
                (frog', rest') = eat frog rest (mosquito:mosqs)
                  where
                    frog@(Frog x _ _ _ _) = fromJust lookup

                    mosqs = (case lookup of
                                    Nothing -> []
                                    Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                            )
                      where lookup = Set.lookupGE (Mosq x 0 0) rest

                    eat frog@(Frog x y i c t) rest mosqs
                      | null mosqs || y < p = (frog, rest)
                      | otherwise = eat frog' rest' $ tail mosqs
                      where
                        mosq@(Mosq p b _) = head mosqs
                        frog' = Frog x (y+b) i (c+1) (t+b)
                        rest' = mosq `Set.delete` rest

                (frogs'', out) = clean frogs' [] frogsList
                  where
                    frogs' = frog' `Set.insert` frogs
                    frogsList = Set.elems . snd $ Set.split frog' frogs'

                    Frog _ y _ _ _ = frog'

                    clean frogs out [] = (frogs, out)
                    clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                      | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                      | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                      | otherwise = (frogs, out)

  putStr . unlines . map (\(c, t) -> show c ++ " " ++ show t) . elems . array (1, n) $ map (\(Frog _ _ i c t) -> (i, (c, t))) result


import Control.Monad
import Data.List

remerge :: [Int] -> [Int] -> [Int] -> [Int]
remerge [] [] [] = []
remerge (a:as) (1:los) unls =     a : remerge as los unls
remerge (_:as) (0:los) (v:unls) = v : remerge as los unls
remerge _ _ _ = error "huh?"

main :: IO ()
main = do
  t <- read <$> getLine
  replicateM_ t $ do
    _nStr <- getLine
    a  <- map read . words <$> getLine
    lo <- map read . words <$> getLine
    let
      unls = reverse $ sort [v | (v, 0) <- zip a lo]
      ans = remerge a lo unls
    putStrLn . unwords $ map show ans

import Control.Monad
import Data.Array
import Data.Foldable
import Data.Graph

-- Split a list in half, with both halves in order
half :: [a] -> ([a], [a])
half = join go
  where
    go t [] = ([], t)
    go t [_] = ([], t)
    go (t : ts) (_ : _ : hs) = (t : front, rear)
      where
        (front, rear) = go ts hs -- Interesting laziness

merge :: Ord a => [a] -> [a] -> [a]
merge [] ys = ys
merge xs [] = xs
merge xxs@(x : xs) yys@(y : ys)
  | x <= y = x : merge xs yys
  | otherwise = y : merge xxs ys

msort :: Ord a => [a] -> [a]
msort [] = []
msort [x] = [x]
msort xs = merge (msort front) (msort rear)
  where
    (front, rear) = half xs

readInput :: IO (Int, [Int], [Int])
readInput = do
  n <- read <$> getLine
  arr <- map read . words <$> getLine
  lock <- map read . words <$> getLine
  return (n, arr, lock)

solve (n, arr, lock) =
  snd $
    foldl
      ( \(j, str) i ->
          if (l ! i) == 0
            then (j + 1, (s' ! j) : str)
            else (j, (a ! i) : str)
      )
      (1, [])
      [1 .. n]
  where
    a = array (1, n) $ zip [1 .. n] arr
    l = array (1, n) $ zip [1 .. n] lock
    s' = array (1, length s) $ zip [1 .. length s] s
    s = reverse $ msort $ map (\(a, b) -> b) $ filter (\(a, b) -> a == 0) (zip lock arr)

main = do
  a <- read <$> getLine
  replicateM_ a $ do
    i <- readInput
    let r = solve i
    putStrLn $ foldl (\s x -> show x ++ " " ++ s) "" r


module Main where

main
  = interact $ solve . map (read::String->Integer) . tail . words

solve list
  = process $ combine list
  where
    combine [] = []
    combine (x:b:rest) = (x,b) : combine rest

process list
  | any (\((x1,b1),(x2,b2))->x1+b1==x2 && x2+b2==x1) product
  = "YES\n"
  | otherwise
  = "NO\n"
  where
    product = [(a,b) | a<-list, b<-list]

import Control.Monad

getA = fmap (map read . words) getLine

main = do
	[n] <- getA
	a <- replicateM n getA
	putStr $ last $ "NO":["YES" | [x,d] <- a, [y,t] <- a, x+d==y, y+t==x]

module Main where
import Control.Monad
import Data.List
import Data.Maybe
import Data.Function
main = do
    n <- readLn :: IO Int
    ds <- replicateM n $ map (read :: String -> Int). words <$> getLine
    let e = zip (map (\[a,b] -> a+b) ds) [1..]
        f = zipWith (\[a,_] i -> (a,i) ) ds [1..]
        g = any ((>1).length) $ groupBy (\(a,b) (c,d) -> a==c && b==d) $  sortOn fst $ catMaybes $  (\(a,b) (c,d) -> if a==c then Just((min b d),(max b d)) else Nothing ) <$> e <*> f
    putStrLn $ if g then "YES" else "NO"

import Array (Array, (!), bounds, listArray)
import Monad (liftM)

solve :: Array Int (Int, Int) -> Bool
solve array  =  not (null solve')
  where
    n  =  snd $ bounds array
    solve'  =  [undefined | i <- [1..n], j <- [1..n], isGood i j]
    isGood i j  =  x1 + y1 == x2 && x2 + y2 == x1
      where
        (x1, y1)  =  array ! i
        (x2, y2)  =  array ! j

readPair :: String -> (Int, Int)
readPair line  =  case words line of
    [a, b]  ->  (read a, read b)
    _  ->  error $ concat ["readPair: ", "string ", show line, " must be two words"]

main :: IO ()
main  =  do
    n  <-  readLn
    pairs  <-  liftM (map readPair . take n . lines) getContents
    let pairs'  =  listArray (1, n) pairs
    putStrLn $ if solve pairs' then "YES" else "NO"

solve :: [(Int,Int)] -> Bool
solve camels = length (spittingPairs camels) > 0

spittingPairs :: [(Int,Int)] -> [(Int,Int)]
spittingPairs camels = filter (\(a,b) -> spitEachOther (camels!!a) (camels!!b)) [(x,y) | x <- [0..len], y <- [0..len], x < y]
    where len = length camels - 1

spitEachOther :: (Int,Int) -> (Int,Int) -> Bool
spitEachOther camel1 camel2 = (fst camel1 + snd camel1 == fst camel2) && (fst camel2 + snd camel2 == fst camel1)

bool2string :: Bool -> [Char]
bool2string True = "YES\n"
bool2string False = "NO\n"

line2tuple :: [Char] -> (Int,Int)
line2tuple line = (head x, last x)
    where x = map read tmp :: [Int]
          tmp = words line

process :: [[Char]] -> [Char]
process input = bool2string (solve (map line2tuple input))

processInput :: [Char] -> [Char]
processInput input = process (tail (lines input))

main = interact processInput

{-# OPTIONS_GHC -funbox-strict-fields #-}

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

main :: IO ()
main = do
  let readInts = map read . words
  n : _ <- readInts <$> getLine
  xs <- replicateM n $ readInts <$> getLine
  let as = do
        x : d : _ <- xs
        return (x, x + d)
      bs = do
        (x, s) <- as
        return (s, x)
      answer = if any (`elem` bs) as
                 then "YES"
                 else "NO"
  putStrLn answer

main = do
  (_:camels) <- fmap (map (map read) . map words . lines) getContents
  let pairs = [(a,b) | a<-camels, b<-camels, f a b]
      f [x,d] [y,e] = x+d == y && y+e == x
  if null pairs
     then putStrLn "NO"
     else putStrLn "YES"

module Main where
import Control.Monad
import Data.List
main = do
    n <- readLn :: IO Int
    ds <- replicateM n $ map (read :: String -> Int). words <$> getLine
    let e = map (\[a,b] -> a+b) ds
        f = e \\ map (\[a,_] -> a) ds
    putStrLn $ if length f > 1 then "YES" else "NO"

module Main where
import Control.Monad
import Data.List
main = do
    n <- readLn :: IO Int
    ds <- replicateM n $ map (read :: String -> Int). words <$> getLine
    let e = map (\[a,b] -> a+b) ds
        f = e `intersect`  map (\[a,_] -> a) ds
    putStrLn $ if length f > 1 then "YES" else "NO"

module Main where
import Control.Monad
import Data.List
import Data.Maybe
import Data.Function
main = do
    n <- readLn :: IO Int
    ds <- replicateM n $ map (read :: String -> Int). words <$> getLine
    let e = zip (map (\[a,b] -> a+b) ds) [1..]
        f = zipWith (\[a,_] i -> (a,i) ) ds [1..]
        g = any ((>1).length) $ groupBy ((==) `on` fst) $ sortOn fst $ catMaybes $  (\(a,b) (c,d) -> if a==c then Just((min b d),(max b d)) else Nothing ) <$> e <*> f
    putStrLn $ if g then "YES" else "NO"

module Main where
import Control.Monad
import Data.List
main = do
    n <- readLn :: IO Int
    ds <- replicateM n $ map (read :: String -> Int). words <$> getLine
    let e = map (\[a,b] -> a+b) ds
        f = e \\ map (\[a,_] -> a) ds
    putStrLn $ if length f > 2 then "YES" else "NO"


{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
import Control.Monad
import qualified Data.ByteString.Char8 as B
import Data.Maybe
import Data.Char
import Data.Array.IArray
--import Debug.Trace

readInt = fst . fromJust . B.readInt <$> B.getLine
readInts = map (fst . fromJust . B.readInt) . B.words <$> B.getLine

charToNum x = ord x - ord 'a'

compress = map (\s -> let c = B.head s in (charToNum c,B.length s)) . B.group

getUpdateList ((l,x):xs) a
    | l == r = (l,if a!l == 0 then max x y else 1+x+y):xs
    | otherwise = (l,a!l+x):(r,a!r+y):xs
        where (r,y) = last xs

f :: Array Int Int -> B.ByteString -> Array Int Int
f a s = let s' = compress s in case s' of
                                    [(i,n)] -> a' // [(i,(a!i+1)*n+a!i)]
                                    xs -> accum max a' $ getUpdateList s' a'
        where a' = amap (\e -> if e == 0 then 0 else 1) a

main = do   n <- readInt
            (x:xs) <- B.words <$> B.getContents
            let a = accumArray max 0 (0,25) . compress $ x
            print $ maximum . foldl f a $ xs

{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
import Control.Monad
import qualified Data.ByteString.Char8 as B
import Data.Maybe
import Data.Char
import Data.Array.IArray
--import Debug.Trace

readInt = fst . fromJust . B.readInt <$> B.getLine
readInts = map (fst . fromJust . B.readInt) . B.words <$> B.getLine

charToNum x = ord x - ord 'a'

compress = map (\s -> let c = B.head s in (charToNum c,B.length s)) . B.group

getUpdateList ((l,x):xs) a
    | l == r = (l,if a!l == 0 then max x y else 1+x+y):xs
    | otherwise = (l,a!l+x):(r,a!r+y):xs
        where (r,y) = last xs

f :: Array Int Int -> B.ByteString -> Array Int Int
f a s = case s' of
            [(i,n)] -> a' // [(i,(a!i+1)*n+a!i)]
            xs -> accum max a' $ getUpdateList s' a'
        where   s' = compress $ B.init s
                a' = amap (\e -> if e == 0 then 0 else 1) a

main = do   n <- readInt
            --(x:xs) <- B.words <$> B.getContents
            --let a = accumArray max 0 (0,25) . compress $ x
            --print $ maximum . foldl f a $ xs
            a <- accumArray max 0 (0,25) . compress . B.init <$> B.getLine
            a' <- foldl f a <$> replicateM (n-1) B.getLine
            print $ maximum a'