a,b,c,k=list(map(int,input().split(" ")))

if a >= k:

  print(k)

elif b >= k-a:

  print(a)

else:

  print((2*a+b-k))
a,b,c,k=list(map(int,input().split()))



if a >= k:

    print(k)

    

elif a+b < k:

    print((a-(k-a-b)))

    

else:

    print(a)
a, b, c, k = list(map(int, input().split(" ")))
if a >= k:
    print(k)
elif b >= k - a:
    print(a)
else:
    print((2 * a + b - k))

a, b, c, k = list(map(int, input().split()))
if a >= k:
    print(k)
elif a + b < k:
    print((a - (k - a - b)))
else:
    print(a)

-a, b, c, k = list(map(int, input().split(" "))) +a, b, c, k = list(map(int, input().split())) -elif b >= k - a: +elif a + b < k: +    print((a - (k - a - b))) +else: -else: -    print((2 * a + b - k))