import Data.Maybe
import Control.Monad
import qualified Data.ByteString.Char8 as C

readInt' = fst.fromJust.C.readInt
readLnInt = map readInt'.C.words<$>C.getLine
readCsInt n = concat<$>replicateM n readLnInt

main = do
 inputs <- readLnInt
 print $ solve inputs

solve [a,b,c,k]
 | k < a = k
 | k < (a + b) = a
 | otherwise = 2*a + b - k
