A,B,C,K=list(map(int,input().split()))



if A>=K:

    print(K)

elif A+B>=K:

    print(A)

else:

    print((A-(K-(A+B))))
a,b,c,k=list(map(int,input().split()))

xa=min(a,k)

k-=xa

xb=min(b,k)

k-=xb

xc=min(c,k)

print((xa-xc))
A, B, C, K = list(map(int, input().split()))
if A >= K:
    print(K)
elif A + B >= K:
    print(A)
else:
    print((A - (K - (A + B))))

a, b, c, k = list(map(int, input().split()))
xa = min(a, k)
k -= xa
xb = min(b, k)
k -= xb
xc = min(c, k)
print((xa - xc))

-A, B, C, K = list(map(int, input().split())) -if A >= K: -    print(K) -elif A + B >= K: -    print(A) -else: -    print((A - (K - (A + B)))) +a, b, c, k = list(map(int, input().split())) +xa = min(a, k) +k -= xa +xb = min(b, k) +k -= xb +xc = min(c, k) +print((xa - xc))