import qualified Data.ByteString.Lazy.Char8 as B
import Data.Maybe
import Debug.Trace

traceQ :: (Show a) => String -> a -> a
-- traceQ label a = trace (label ++ ": " ++ show a) a
traceQ _ a = a

traceZ :: String -> a -> a
-- traceZ = trace
traceZ _ a = a

bsReadInts :: B.ByteString -> [Int]
bsReadInts = map (fst . fromJust . B.readInt) . B.words

type Mms = (Int, Int, Int, Int)
gMin   (x, _, _, _) = x
gMax   (_, x, _, _) = x
gShift (_, _, x, _) = x
gDir   (_, _, _, x) = x

solve :: Int -> Int -> [Int] -> Int -> [[Int]] -> [Int]
solve valX valK rs valQ tas = vals

  where

    states = scanl op (0, (0, valX, 0, -1)) rs
      where
        op (t, mms) r = (r, mmsUpd valX (r - t) mms)

    valueA :: Int -> Mms -> Int -> Int
    valueA t (minA, maxA, shift, dir) a = min valX (max 0 v)
      where v = min maxA (max minA a) + shift + t * dir

    findInterval :: Int -> [(Int, Mms)] -> [(Int, Mms)]
    findInterval t [s] = [s]
    findInterval t ss1@(_ : ss2@((r2, _) : _))
      | t >= r2  = findInterval t ss2
      | t <  r2 = ss1

    procTa :: Int -> Int -> [(Int, Mms)] -> (Int, [(Int, Mms)])
    procTa t a states =
      let newStates@((r, mms) : _) = findInterval t states
          v = valueA (t - r) mms a
          res = (v, newStates)
      in (traceZ ("procTa: " ++ show (t, res)) res)

    vals = map fst $ tail $ scanl op (0, (traceQ "states" states)) tas
      where
        op (_, ss) [t, a] = procTa t a ss


mmsUpd :: Int -> Int -> Mms -> Mms
mmsUpd _    r (minA, maxA, shift, -1) = (newMinA, maxA, newShift,  1)
  where (newMinA, newShift) | r - shift < minA = (minA, shift - r)
                            | maxA < r - shift = (maxA, -maxA)
                            | otherwise        = (r - shift, shift - r)
mmsUpd valX r (minA, maxA, shift,  1) =
  let (minB, maxB, shiftB) = (valX - maxA, valX - minA, -shift)
      (minB', maxB', shiftB', _) = mmsUpd valX r (minB, maxB, shiftB, -1)
  in (valX - maxB', valX - minB', -shiftB', -1)

main :: IO ()
main = do
  conts <- B.getContents
  let [valX] : [valK] : rs : [valQ] : tas = map bsReadInts (B.lines conts)
      answer = solve valX valK rs valQ tas
  putStr $ unlines $ map show answer
