You are the gym teacher in the school.

There are $n$ students in the row. And there are two rivalling students among them. The first one is in position $a$, the second in position $b$. Positions are numbered from $1$ to $n$ from left to right.

Since they are rivals, you want to maximize the distance between them. If students are in positions $p$ and $s$ respectively, then distance between them is $|p - s|$. 

You can do the following operation at most $x$ times: choose two adjacent (neighbouring) students and swap them.

Calculate the maximum distance between two rivalling students after at most $x$ swaps.


-----Input-----

The first line contains one integer $t$ ($1 \le t \le 100$) — the number of test cases.

The only line of each test case contains four integers $n$, $x$, $a$ and $b$ ($2 \le n \le 100$, $0 \le x \le 100$, $1 \le a, b \le n$, $a \neq b$) — the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively.


-----Output-----

For each test case print one integer — the maximum distance between two rivaling students which you can obtain.


-----Example-----
Input
3
5 1 3 2
100 33 100 1
6 0 2 3

Output
2
99
1



-----Note-----

In the first test case you can swap students in positions $3$ and $4$. And then the distance between the rivals is equal to $|4 - 2| = 2$.

In the second test case you don't have to swap students. 

In the third test case you can't swap students.

Difficulty: EASY

Solutions:
import math
t = int(input())

def hello():
	(n, x, a, b) = map(int, input().split())
	beggining = abs(a - b)
	if beggining + x >= n - 1:
		return n - 1
	else:
		return beggining + x
for i in range(t):
	print(hello())


n = int(input())
for i in range(n):
	s = list(map(int, input().split()))
	n = s[0]
	x = s[1]
	a = min(s[2:])
	b = max(s[2:])
	print(min(n - 1, b - a + x))


for _ in range(int(input())):
	(n, x, a, b) = list(map(int, input().split()))
	t = abs(a - b)
	if x == 0:
		print(t)
	elif x < n - t:
		print(t + x)
	elif x >= n - t:
		print(n - 1)


t = int(input())
for el in range(t):
	(n, x, a, b) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	abd = b - a + x
	print(min(abd, n - 1))


T = int(input())
for t in range(T):
	(n, x, a, b) = map(int, input().split())
	max_x = min(a, b) - 1 + n - max(a, b)
	current = abs(a - b)
	min_x = min(max_x, x)
	result = current + min_x
	print(result)


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split(' '))
	if a == n and b == 1 or (b == n and a == 1):
		a1 = a
		b1 = b
	elif a > b:
		if x <= n - a:
			a1 = a + x
			b1 = b
		else:
			a1 = n
			if x - (n - a) <= b - 1:
				b1 = b - (x - (n - a))
			else:
				b1 = 1
	elif x <= n - b:
		b1 = b + x
		a1 = a
	else:
		b1 = n
		if x - (n - b) <= a - 1:
			a1 = a - (x - (n - b))
		else:
			a1 = 1
	print(abs(a1 - b1))


for t in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		dist = abs(b - x - a)
	else:
		dist = abs(b + x - a)
	if dist >= n:
		dist = n - 1
	print(dist)


from sys import stdin
rd = stdin.readline
rrd = lambda : map(int, rd().strip().split())
t = int(input())
while t:
	(n, x, a, b) = rrd()
	print(min(n - 1, abs(a - b) + x))
	t -= 1


t = int(input())
for j in range(t):
	(n, x, a, b) = list(map(int, input().split()))
	print(min(abs(b - a) + x, n - 1))


for i in range(int(input())):
	first = list(map(int, input().split()))
	temp = 0
	if first[2] > first[3]:
		temp = first[2]
		first[2] = first[3]
		first[3] = temp
	while first[1] > 0 and (first[2] > 1 or first[3] < first[0]):
		first[1] -= 1
		if first[2] > 1:
			first[2] -= 1
		elif first[3] < first[0]:
			first[3] += 1
	print(first[3] - first[2])


a = int(input())
b = []
for i in range(0, a):
	b = list(map(int, input().split()))
	if b[0] - 1 >= b[1] + abs(b[2] - b[3]):
		print(abs(b[2] - b[3]) + b[1])
	else:
		print(b[0] - 1)


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	ans = abs(a - b) + x
	print(ans if ans < n else n - 1)


t = int(input())
for i in range(t):
	(n, x, a, b) = (int(j) for j in input().split())
	pos = max(a, b)
	pos += x
	s = pos - min(a, b)
	n -= 1
	if s <= n:
		print(s)
	else:
		print(n)


p = int(input())
for t in range(p):
	entrada = input().split(' ')
	x = int(entrada[1])
	a = min(int(entrada[2]), int(entrada[3]))
	b = max(int(entrada[2]), int(entrada[3]))
	while x != 0:
		if a > 1:
			a -= 1
		elif b < int(entrada[0]):
			b += 1
		x -= 1
	print(abs(a - b))


import math
from sys import stdin
cases = int(stdin.readline().strip())
for _ in range(cases):
	(n, x, a, b) = [int(x) for x in stdin.readline().strip().split(' ')]
	if (max(a, b) + x) / n > 1:
		new_b = n
		extra = max(a, b) + x - n
		new_a = 1 if min(a, b) - extra < 1 else min(a, b) - extra
	else:
		new_b = max(a, b) + x
		new_a = min(a, b)
	print(new_b - new_a)


for _ in range(int(input())):
	(n, x, b, a) = [int(i) for i in input().split()]
	print(abs(b - a) + x if abs(b - a) + x <= n - 1 else n - 1)


qq = int(input())
h = []
for i in range(0, qq):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		t = a
		a = b
		b = t
	while x != 0:
		while a != 1:
			a = a - 1
			x = x - 1
			if x == 0:
				break
		if x == 0 or (a == 1 and b == n):
			break
		while b != n:
			b = b + 1
			x = x - 1
			if x == 0:
				break
	g = b - a
	h.append(g)
for i in range(0, qq):
	print(h[i])


import math
from decimal import Decimal
import heapq
from collections import deque

def na():
	n = int(input())
	b = [int(x) for x in input().split()]
	return (n, b)

def nab():
	n = int(input())
	b = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	return (n, b, c)

def dv():
	(n, m) = map(int, input().split())
	return (n, m)

def dva():
	(n, m) = map(int, input().split())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	return (n, m, b)

def eratosthenes(n):
	sieve = list(range(n + 1))
	for i in sieve:
		if i > 1:
			for j in range(i + i, len(sieve), i):
				sieve[j] = 0
	return sorted(set(sieve))

def lol(lst, k):
	k = k % len(lst)
	ret = [0] * len(lst)
	for i in range(len(lst)):
		if i + k < len(lst) and i + k >= 0:
			ret[i] = lst[i + k]
		if i + k >= len(lst):
			ret[i] = lst[i + k - len(lst)]
		if i + k < 0:
			ret[i] = lst[i + k + len(lst)]
	return ret

def nm():
	n = int(input())
	b = [int(x) for x in input().split()]
	m = int(input())
	c = [int(x) for x in input().split()]
	return (n, b, m, c)

def dvs():
	n = int(input())
	m = int(input())
	return (n, m)

def fact(a, b):
	c = []
	ans = 0
	f = int(math.sqrt(a))
	for i in range(1, f + 1):
		if a % i == 0:
			c.append(i)
	l = len(c)
	for i in range(l):
		c.append(a // c[i])
	for i in range(len(c)):
		if c[i] <= b:
			ans += 1
	if a / f == f and b >= f:
		return ans - 1
	return ans
t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	d = a - 1
	if x >= d:
		d = 1
		x -= a - 1
	else:
		d = a - x
		x = 0
	dd = n - b
	if x >= dd:
		dd = n
	else:
		dd = b + x
	print(dd - d)


from math import ceil
from math import factorial
ii = lambda : int(input())
iia = lambda : list(map(int, input().split()))
isa = lambda : list(input().split())
t = ii()
for i in range(t):
	(n, x, a, b) = iia()
	y = abs(a - b)
	print(min(n - 1, y + x))


from math import *
from cmath import *
from itertools import *
from decimal import *
from fractions import *
from sys import *
for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	print(b - a + min(x, a - 1 + n - b))


for x in range(int(input())):
	(n, x, a, b) = list(map(int, input().split()))
	(a, b) = (max(a, b), min(a, b))
	c = a
	a += min(n - a, x)
	x -= min(x, n - c)
	if x > 0:
		d = b
		b -= min(b - 1, x)
		x -= min(x, d - 1)
	print(a - b)


t = int(input())
i = 0
for i in range(t):
	st = str(input())
	st = st.split(' ')
	n = int(st[0])
	x = int(st[1])
	a = int(st[2])
	b = int(st[3])
	z = abs(b - a)
	z += x
	if z >= n:
		print(n - 1)
	else:
		print(z)


for _ in [0] * int(input()):
	(n, x, *a) = map(int, input().split())
	a = sorted(a)
	b = a[0] - a[1]
	print(min(x, n - 1 + b) - b)


t = int(input())
for i in range(t):
	l = list(map(int, input().split()))
	if l[2] >= l[3]:
		max = l[2]
		min = l[3]
	else:
		max = l[3]
		min = l[2]
	if l[0] - max + min <= l[1]:
		print(l[0] - 1)
	else:
		print(max - min + l[1])


def main():
	ans = ''
	for query in range(int(input())):
		(n, x, a, b) = list(map(int, input().split(' ')))
		max_dist = min(n - 1, abs(a - b) + x)
		ans += str(max_dist) + '\n'
	print(ans)
main()


n = int(input())
for p in range(n):
	c = input().split()
	(n, x, a, b) = (int(c[0]), int(c[1]), int(c[2]), int(c[3]))
	a = abs(a - b)
	if x + a > n - 1:
		print(n - 1)
	else:
		print(x + a)


test_case = int(input())
for _ in range(test_case):
	(n, x, a, b) = map(int, input().split())
	(a, b) = sorted([a, b])
	if b - a + x > n - 1:
		print(n - 1)
	else:
		print(b - a + x)


for z in range(int(input())):
	(n, x, a, b) = list(map(int, input().split()))
	r = abs(a - b)
	if r < n - 1:
		y = n - 1 - r
		if x <= y:
			print(r + x)
		else:
			print(r + y)
	else:
		print(r)


t = int(input())
for i in range(t):
	q = input().split()
	n = int(q[0])
	x = int(q[1])
	a = int(q[2])
	b = int(q[3])
	c = abs(a - b)
	maximum = max(a, b)
	minimum = min(a, b)
	y = n - maximum
	if c < n - 1:
		if maximum + x <= n:
			maximum = maximum + x
			print(maximum - minimum)
		else:
			maximum += y
			if minimum - x + y > 0:
				minimum += y - x
			else:
				minimum = 1
			print(maximum - minimum)
	else:
		print(c)


t = int(input())
for i in range(t):
	[n, x, a, b] = [int(x) for x in input().split()]
	[M, m] = [max(a, b), min(a, b)]
	print(abs(a - b) + x if abs(m - 1) + abs(M - n) > x else n - 1)


from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))

def solve():
	(n, x, a, b) = rl()
	print(min(n - 1, max(a, b) - min(a, b) + x))
mode = 'T'
if mode == 'T':
	t = ri()
	for i in range(t):
		solve()
else:
	solve()


def two_rival_student(n, x, a, b):
	for i in range(x):
		if a > b:
			if a < n:
				a += 1
			elif b > 1:
				b -= 1
		elif b < n:
			b += 1
		elif a > 1:
			a -= 1
	print(abs(a - b))
test_case_number = int(input())
for i in range(test_case_number):
	(n, x, a, b) = map(lambda n: int(n), input().split(' '))
	two_rival_student(n, x, a, b)


T = int(input())
for t in range(T):
	(n, x, a, b) = [int(x) for x in input().split()]
	d = abs(a - b)
	if d + x <= n - 1:
		print(d + x)
	else:
		print(n - 1)


for i in range(int(input())):
	(n, x, a, b) = list(map(int, input().split()))
	c = min(abs(a - b) + x, n - 1)
	print(c)


for i in range(int(input())):
	x = [int(i) for i in input().split()]
	if x[1] == 0:
		print(abs(x[2] - x[3]))
	elif x[1] > 0:
		for j in range(x[1]):
			if x[2] >= x[3]:
				if x[2] != x[0]:
					x[2] += 1
				elif x[3] != 1:
					x[3] -= 1
			elif x[3] >= x[2]:
				if x[3] != x[0]:
					x[3] += 1
				elif x[2] != 1:
					x[2] -= 1
			else:
				break
		print(abs(x[2] - x[3]))


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	b1 = max(a, b)
	a1 = min(a, b)
	if b1 - a1 == n - 1 or x == 0:
		print(b1 - a1)
	else:
		d = b1 - a1 + x
		if d >= n - 1:
			print(n - 1)
		else:
			print(d)


t = int(input())
out = []
for i in range(t):
	(n, x, a, b) = list(map(int, input().split(' ')))
	if a > b:
		temp = a
		a = b
		b = temp
	if n - b > a - 1:
		d = min(x, n - b)
		b += d
		x -= d
		d = min(x, a - 1)
		a -= d
		x -= d
	else:
		d = min(x, a - 1)
		a -= d
		x -= d
		d = min(x, n - b)
		b += d
		x -= d
	out.append(abs(a - b))
for o in out:
	print(o)


for i in ' ' * int(input()):
	(n, x, a, b) = map(int, input().split())
	print(min(abs(a - b) + x, n - 1))


q = int(input())
for w in range(q):
	(n, x, a, b) = (int(i) for i in input().split())
	d = abs(b - a)
	while x > 0 and d < n - 1:
		d += 1
		x -= 1
	print(d)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def main():
	t = I()
	rr = []
	for _ in range(t):
		(n, x, a, b) = LI()
		r = min(abs(a - b) + x, n - 1)
		rr.append(r)
	return JA(rr, '\n')
print(main())


def rivalstudents(n):
	for i in range(n):
		(n, x, a, b) = map(int, input().split())
		print(min(abs(a - b) + x, n - 1))

def main():
	n = int(input())
	rivalstudents(n)
main()


t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	print(min(n - 1, max(a, b) - min(a, b) + x))


n = int(input())
sol = []
for i in range(n):
	test = input().split()
	row = int(test[0])
	swap = int(test[1])
	a = int(test[2])
	b = int(test[3])
	res = abs(a - b) + swap
	if res >= row:
		res = row - 1
	sol.append(res)
for i in sol:
	print(i)


for nt in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	print(min(abs(a - b) + x, n - 1))


for i in range(int(input())):
	(n, x, a, b) = map(int, input().split(' '))
	(a, b) = (min(a, b), max(a, b))
	v = min(n, b + x)
	x -= v - b
	b = v
	v = max(1, a - x)
	a = v
	print(abs(b - a))


for tc in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	aa = min(a, b)
	bb = max(a, b)
	while x != 0 and aa >= 1 and (bb <= n):
		if aa - 1 >= 1:
			aa -= 1
			x -= 1
		elif bb + 1 <= n:
			bb += 1
			x -= 1
		else:
			break
	print(abs(aa - bb))


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	mi = min(a, b)
	ma = max(a, b)
	if x > n - ma:
		x = x - (n - ma)
		ma = n
		if x > mi - 1:
			mi = 1
		else:
			mi = mi - x
	else:
		ma = ma + x
	print(ma - mi)


t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	na = a - min(a, b) + 1
	nb = b - min(a, b) + 1
	print(min(n - 1, abs(na - nb) + x))


t = int(input())
for i in range(t):
	l = list(map(int, input().split()))
	n = l[0]
	x = l[1]
	a = min(l[2], l[3])
	b = max(l[2], l[3])
	if x <= n - b + a - 1:
		print(b - a + x)
	else:
		print(n - 1)


from math import floor, ceil
t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	print(min(abs(a - b) + x, n - 1))


from sys import stdin
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline()
for _ in range(ii()):
	(n, x, a, b) = mi()
	p = abs(a - b) + x
	print(min(n - 1, p))


t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	if b - a + x > n - 1:
		print(n - 1)
	else:
		print(b - a + x)


t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	q = [a, b]
	q.sort()
	a = q[0]
	b = q[1]
	r = min(a - 1, x)
	x -= r
	s = min(n - b, x)
	print(b + s - (a - r))


t = int(input())
l = []
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	j = 0
	lar = abs(1 - n)
	diff = abs(a - b)
	while j < x:
		if diff == lar:
			break
		diff += 1
		j += 1
	l.append(diff)
for i in range(t):
	print(l[i])


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	a -= 1
	b -= 1
	if a > b:
		(a, b) = (b, a)
	ans = b - a
	while x > 0 and a > 0:
		a = a - 1
		x -= 1
		ans += 1
	while x > 0 and b < n - 1:
		b = b + 1
		x -= 1
		ans += 1
	print(ans)


from bisect import bisect
t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	print(min(n - 1, abs(a - b) + x))


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if x == 0:
		print(abs(a - b))
	elif abs(a - b) == n - 1:
		print(n - 1)
	else:
		(a, b) = (min(a, b), max(a, b))
		if a - 1 >= x or n - b >= x or a - 1 + n - b >= x:
			print(b - a + x)
		else:
			print(b - a + (a - 1) + (n - b))


mod = 10 ** 9 + 7
import sys
sys.setrecursionlimit(10 ** 6)
from sys import stdin, stdout
import bisect
from bisect import bisect_left as bl
from bisect import bisect_right as br
import itertools
import collections
import math
import heapq
import random

def modinv(n, p):
	return pow(n, p - 2, p)

def ncr(n, r, p):
	t = fact[n] * (ifact[r] * ifact[n - r] % p) % p
	return t

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def main():
	t = inin()
	for _ in range(t):
		(n, x, i, j) = cin()
		a = max(i, j)
		b = min(i, j)
		if b > x:
			b -= x
			print(a - b)
		else:
			x -= b
			a += x
			a = min(a, n - 1)
			print(a)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def readnumbers(zero=0):
	conv = ord if py2 else lambda x: x
	A = []
	numb = zero
	sign = 1
	i = 0
	s = sys.stdin.buffer.read()
	try:
		while True:
			if s[i] >= b'0'[0]:
				numb = 10 * numb + conv(s[i]) - 48
			elif s[i] == b'-'[0]:
				sign = -1
			elif s[i] != b'\r'[0]:
				A.append(sign * numb)
				numb = zero
				sign = 1
			i += 1
	except:
		pass
	if s and s[-1] >= b'0'[0]:
		A.append(sign * numb)
	return A
main()


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	ans = min(abs(a - b) + x, n - 1)
	print(ans)


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		y = a
		a = b
		b = y
	while a > 1 and x > 0:
		a -= 1
		x -= 1
	while b < n and x > 0:
		b += 1
		x -= 1
	print(abs(a - b))


def diff(a, b):
	if a > b:
		c = a - b
	else:
		c = b - a
	return c
nab = int(input())
my_list = []
for x in range(nab):
	data = input().split()
	for i in range(len(data)):
		data[i] = int(data[i])
	kol = data[0]
	pov = data[1]
	u1 = data[2]
	u2 = data[3]
	if pov != 0:
		if u1 >= u2:
			while pov >= 1:
				if u1 == kol:
					break
				u1 += 1
				pov -= 1
			while pov != 0:
				if u2 == 1:
					break
				u2 -= 1
				pov -= 1
		else:
			while pov >= 1:
				if u2 == kol:
					break
				u2 += 1
				pov -= 1
			while pov != 0:
				if u1 == 1:
					break
				u1 -= 1
				pov -= 1
	my_list.append(diff(u1, u2))
for (a, b) in enumerate(my_list):
	print(b)


for _ in range(int(input())):
	(n, x, a, b) = list(map(int, input().split()))
	c = max(a, b)
	d = a + b - c
	while x:
		x -= 1
		if c != n:
			c += 1
		elif d != 1:
			d -= 1
		else:
			break
	print(c - d)


T = int(input())
for i in range(T):
	(n, x, a, b) = list(map(int, input().split()))
	d = abs(b - a)
	if x == 0:
		print(d)
	elif d == n - 1:
		print(d)
	else:
		for i in range(x):
			if d == n - 1:
				break
			d += 1
		print(d)


import sys
t = int(sys.stdin.readline())
cases = [list(map(int, sys.stdin.readline().split())) for _ in range(t)]
results = []
for (n, x, a, b) in cases:
	results.append(min(n - 1, abs(a - b) + x))
sys.stdout.write('\n'.join((str(x) for x in results)))


def answer():
	n = int(input())
	while n:
		a = [int(x) for x in input().split()]
		them = a[0] - 1
		ans = min(a[1] + abs(a[2] - a[3]), them)
		print(ans)
		n -= 1
answer()


t = int(input())
inp = []
for i in range(t):
	inp.append(input())
for i in range(t):
	inpt = inp[i].split(' ')
	n = int(inpt[0])
	x = int(inpt[1])
	a = int(inpt[2])
	b = int(inpt[3])
	if a > b:
		r1 = 1
		r2 = -1
	else:
		r1 = -1
		r2 = 1
	ctr = 1
	r = 1
	p = abs(a - b)
	while ctr <= x:
		a_old = a
		b_old = b
		if abs(a - b) == n - 1:
			p = abs(a - b)
			ctr = x + 1
		elif a + r1 > 0:
			a = a + r1
		elif b + r2 <= n:
			b += r2
		else:
			None
		if abs(a - b) > abs(a_old - b_old):
			ctr += 1
		else:
			a = a_old
			b = b_old
		p = abs(a - b)
	print(p)


for i in range(int(input())):
	(n, x, a, b) = [int(elem) for elem in input().split()]
	(min_p, max_p) = (min(a, b), max(a, b))
	(a, b) = (min_p, max_p)
	if x > a - 1 + (n - b):
		(b, a) = (n, 1)
		print(b - a)
	elif x > n - b:
		x -= n - b
		b = n
		a -= x
		print(b - a)
	else:
		b += x
		print(b - a)


for T in range(int(input())):
	(n, x, a, b) = map(int, input().split(' '))
	if abs(a - b) + x >= n:
		print(n - 1)
	else:
		print(abs(a - b) + x)


t = input()
t = int(t)
for i in range(t):
	(n, x, a, b) = list(map(int, input().split()))
	ans = a - b
	if ans < 0:
		ans *= -1
	ans += x
	if ans > n - 1:
		ans = n - 1
	print(ans)


def solve():
	(n, x, a, b) = map(int, input().split())
	(a, b) = sorted([a, b])
	ans = min(b - a + x, n - 1)
	print(ans)
t = int(input())
for i in range(t):
	solve()


tests = int(input())
for t in range(tests):
	(n, x, a, b) = [int(x) for x in input().split(' ')]
	d = abs(a - b)
	(left, right) = (min(a, b) - 1, n - max(a, b))
	if left + right <= x:
		print(n - 1)
	else:
		print(d + x)


T = int(input())
for i in range(T):
	(N, X, A, B) = [int(x) for x in input().strip().split()]
	(S, L) = (min(A, B), max(A, B))
	if S - 1 >= X:
		S -= X
	else:
		(S, L) = (1, min(N, L + X - (S - 1)))
	print(abs(S - L))


q = int(input())
for qq in range(q):
	(n, x, a, b) = map(int, input().split())
	dist = b - a
	print(min(max(a, b) - min(a, b) + x, n - 1))


for x in range(int(input())):
	(n, s, a, b) = map(int, input().split())
	if abs(a - b) == n - 1:
		print(abs(a - b))
	elif abs(a - b) + s > n - 1:
		print(n - 1)
	else:
		print(abs(a - b) + s)


from collections import defaultdict
for _ in range(int(input())):
	(n, x, a, b) = list(map(int, input().split()))
	(a, b) = (min(a, b), max(a, b))
	if x >= n - b + a - 1:
		print(n - 1)
	else:
		print(b - a + x)


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	l = abs(a - b)
	mx_l = n - 1
	if mx_l - l - x <= 0:
		print(mx_l)
	else:
		print(l + x)


for _ in range(int(input())):
	(n, swaps, a, b) = map(int, input().split())
	if abs(a - b) + swaps < n:
		d = abs(a - b) + swaps
	else:
		d = n - 1
	print(d)


for u in range(int(input())):
	(n, d, x, y) = map(int, input().split())
	print(min(n - 1, abs(x - y) + d))


def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
import math
t = ii()
for i in range(t):
	(n, x, a, b) = mi()
	if a < b:
		(a, b) = (b, a)
	while x != 0 and a < n:
		a += 1
		x -= 1
	while x != 0 and b > 1:
		b -= 1
		x -= 1
	print(abs(b - a))


t = int(input())
a = []
for i in range(t):
	b = list(map(int, input().split()))
	a.append(b)
for i in a:
	n = i[0]
	x = i[1]
	a = i[2]
	b = i[3]
	k = abs(b - a) + x
	if k >= n - 1:
		print(n - 1)
	else:
		print(k)


T = int(input())
while T != 0:
	T -= 1
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	if a - 1 + n - b <= x:
		print(str(n - 1))
	else:
		print(str(b - a + x))


for _ in range(int(input())):
	(n, x, a, b) = (int(v) for v in input().split())
	d = abs(a - b)
	maxd = n - 1
	ans = min(maxd, d + x)
	print(ans)


t = int(input())
nxab = [list(map(int, input().split())) for _ in range(t)]
for nxabi in nxab:
	d = min(nxabi[1] + abs(nxabi[2] - nxabi[3]), nxabi[0] - 1)
	print(d)


t = input()
ar = []
for i in range(int(t)):
	ar.append(input().split())
for i in range(int(t)):
	now = ar[i]
	n = int(now[0])
	x = int(now[1])
	a = int(now[2])
	b = int(now[3])
	if a > b:
		(a, b) = (b, a)
	while x > 0:
		if a != 1:
			a -= 1
			x -= 1
		elif b != n:
			if x > 0:
				b += 1
				x -= 1
		else:
			break
	print(b - a)


case = int(input())
for caseNum in range(0, case):
	(maxStudents, swap, pos1, pos2) = [int(x) for x in input().split()]
	max_diff = maxStudents - 1
	diff = abs(pos1 - pos2)
	print(min(diff + swap, max_diff))


t = int(input())
for i in range(0, t):
	(n, x, a, b) = map(int, input().split())
	g = abs(a - b)
	while x != 0:
		if g == n - 1:
			break
		else:
			x -= 1
			g += 1
	print(g)


import os
import sys
debug = True
if debug and os.path.exists('input.in'):
	input = open('input.in', 'r').readline
else:
	debug = False
	input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
test_count = int(input())
for t in range(test_count):
	if debug:
		print('Test Case #', t + 1)
	(n, x, a, b) = invr()
	print(min(n - 1, abs(a - b) + x))


for _ in range(int(input())):
	(n, x, i, j) = map(int, input().split())
	a = min(i, j)
	b = max(i, j)
	z = b - a
	k = a - 1 + (n - b)
	if x >= k:
		print(n - 1)
	else:
		print(x + z)


t = int(input())
for i in range(t):
	(n, x, p, s) = map(int, input().split())
	while x > 0:
		if p > s:
			if p < n:
				p += 1
				x -= 1
				continue
			if s > 1:
				s -= 1
				x -= 1
			if s == 1 and p == n:
				break
		elif p < s:
			if s < n:
				s += 1
				x -= 1
				continue
			if p > 1:
				p -= 1
				x -= 1
				continue
			if p == 1 and s == n:
				break
	print(abs(p - s))


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10 ** 9 + 7
INF = 10 ** 20
I = lambda : list(map(int, input().split()))
from collections import defaultdict as dd
from math import gcd
(t,) = I()
while t:
	t -= 1
	(n, x, a, b) = I()
	if b < a:
		(a, b) = (b, a)
	while b != n and x:
		x -= 1
		b += 1
	while a != 1 and x:
		a -= 1
		x -= 1
	print(b - a)


t = int(input())
while t:
	(n, x, a, b) = map(int, input().split())
	if abs(a - b) == n - 1 or x == 0:
		print(abs(a - b))
	else:
		print(min(n - 1, abs(a - b) + x))
	t -= 1


from sys import stdin

def rl():
	return [int(w) for w in stdin.readline().split()]

def nhap():
	(n, x, a, b) = rl()
	if a > b:
		(a, b) = (b, a)
	res = b - a + min(x, n - b + a - 1)
	print(res)
test = int(stdin.readline())
while test:
	nhap()
	test -= 1


from math import *
from collections import *
import sys
sys.setrecursionlimit(10 ** 9)
t = int(input())
for y in range(t):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	while x != 0:
		if b < n:
			b += 1
			x -= 1
		elif a > 1:
			a -= 1
			x -= 1
		else:
			break
	print(abs(b - a))


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		left = b
		right = a
	else:
		left = a
		right = b
	while left > 1 and x > 0:
		left -= 1
		x -= 1
	while x > 0 and right < n:
		right += 1
		x -= 1
	print(abs(left - right))


t = int(input())
for _ in range(t):
	(n, x, a, b) = list(map(int, input().split()))
	if b < a:
		(a, b) = (b, a)
	print(min(n - 1, b - a + x))


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
ans = []
for _ in range(INT()):
	(n, x, a, b) = MAP()
	ans.append(str(min(n - 1, abs(a - b) + x)))
print('\n'.join(ans))


n = int(input())
for i in range(n):
	ls = [*map(int, input().split())]
	print(min(ls[0] - 1, abs(ls[2] - ls[3]) + ls[1]))


cases = int(input())
for c in range(cases):
	(n, x, a, b) = map(int, input().split())
	ans = abs(a - b) + min(x, n - 1 - abs(a - b))
	print(ans)


testCases = int(input())
for t in range(testCases):
	arr = [int(x) for x in input().split()]
	(n, x, a, b) = (arr[0], arr[1], arr[2], arr[3])
	curr_dis = abs(a - b)
	max_dis = curr_dis + x
	ans = min(max_dis, n - 1)
	print(ans)


for i in range(int(input())):
	c = list(map(int, input().split()))
	c1 = c[1] + abs(c[2] - c[3])
	c2 = min(c1, c[0] - 1)
	print(c2)


t = int(input())
for i in range(t):
	(n, x, a, b) = list(map(int, input().split()))
	print(min(n - 1, max(a, b) - min(a, b) + x))


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	mx = max(a, b)
	p = min(n, mx + x)
	q = max(1, min(a, b) - (x - max(0, p - mx)))
	print(abs(p - q))


tc = input()
r = []
for i in range(0, int(tc)):
	(n, x, a, b) = input().split()
	r.append(min(int(n) - 1, abs(int(a) - int(b)) + int(x)))
for x in r:
	print(x)


t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	p = max(a, b)
	q = min(a, b)
	while x > 0:
		if q == 1 and p == n:
			break
		if q > 1:
			q -= 1
			x -= 1
			if x == 0:
				break
		if p < n:
			p += 1
			x -= 1
			if x == 0:
				break
	print(p - q)


q = int(input())
for i in range(q):
	(n, x, a, b) = map(int, input().split())
	print(min(max(a, b) - min(a, b) + x, n - 1))


for _ in range(int(input())):
	(a, k, x, y) = map(int, input().split())
	print(min(a - 1, abs(x - y) + k))


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		c = a
		a = b
		b = c
	y = a - 1 + n - b
	if y >= x:
		print(b - a + x)
	else:
		print(b - a + y)


test_cases_nunmbers = int(input())
i = 0
test_cases = []
for i in range(test_cases_nunmbers):
	test_cases.append(input())
	Spilit_List = test_cases[i].split()
	n = int(Spilit_List[0])
	x = int(Spilit_List[1])
	a = int(Spilit_List[2])
	b = int(Spilit_List[3])
	out = b - a
	if out > 0:
		for i in range(x):
			if out < n - 1:
				out = out + 1
			else:
				break
		print(out)
	else:
		out = out * -1
		for i in range(x):
			if out < n - 1:
				out = out + 1
			else:
				break
		print(out)


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	while b < n and x != 0:
		b += 1
		x -= 1
	while a > 1 and x != 0:
		a -= 1
		x -= 1
	print(b - a)


import sys
data = [[int(x) for x in x.split()] for x in sys.stdin.readlines()[1:]]
for d in data:
	print(min(d[0] - 1, abs(d[2] - d[3]) + d[1]))


q = int(input())
for re in range(q):
	(n, x, a, b) = map(int, input().split())
	if abs(a - b) + x >= n - 1:
		print(n - 1)
	else:
		print(abs(a - b) + x)


t = int(input())
for _ in range(t):
	(n, x, a, b) = [int(x) for x in input().split()]
	dist = min(abs(a - b) + x, n - 1)
	print(dist)


am = int(input())
for _ in range(am):
	strIn = list(map(int, input().split()))
	(n, x, a, b) = (strIn[0], strIn[1], strIn[2], strIn[3])
	print(abs(a - b) + x if abs(a - b) + x < n else n - 1)


for t in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	m = abs(a - b)
	p = min(a, b) - 1
	q = n - max(a, b)
	if p + q <= x:
		print(m + p + q)
	else:
		print(m + x)


n = int(input())
for x in range(n):
	(count, w, s1, s2) = map(int, input().split())
	e = max(s1, s2)
	s = min(s1, s2)
	if e + w > count:
		d = e
		e = count
		w -= e - d
		s -= w
		if s <= 0:
			s = 1
	else:
		e += w
	print(e - s)


a = int(input())
for i in range(a):
	(b, c, d, e) = list(map(int, input().split()))
	if c + max(d, e) <= b:
		print(c + max(d, e) - min(d, e))
	elif c + max(d, e) > b and e >= 1:
		x = c + max(d, e) - b
		y = min(d, e)
		y -= x
		if y < 1:
			y = 1
		print(b - y)


queue = []
t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	m = min(a, b)
	k = max(a, b)
	if m == 1 and k == n:
		queue.append(abs(a - b))
		continue
	for i in range(x):
		if m > 1:
			m -= 1
		elif k < n:
			k += 1
	queue.append(abs(k - m))
for i in range(len(queue)):
	print(queue[i])


t = int(input())
for tt in range(t):
	(n, x, a, b) = map(int, input().split())
	print(min(n - 1, abs(b - a) + x))


exec(int(input()) * 'n,x,a,b=map(int,input().split());print(min(n-1,abs(a-b)+x));')


import sys
input = lambda : sys.stdin.readline().strip()
print = lambda s: sys.stdout.write(s)
t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	print(str(min(abs(a - b) + x, n - 1)) + '\n')


a = int(input())
for i in range(a):
	(n, x, a, b) = (int(t) for t in input().split(' '))
	if a > b:
		(a, b) = (b, a)
	r = b - a
	if a - 1 > x:
		r += x
	else:
		r += a - 1
		x -= a - 1
		if n - b > x:
			r += x
		else:
			r += n - b
	print(r)


t = int(input())
while t > 0:
	t = t - 1
	(n, x, a, b) = map(int, input().split())
	c = abs(a - b)
	if c + x < n:
		print(c + x)
	else:
		print(n - 1)


import os
import sys
from io import BytesIO, IOBase

def readvars():
	k = map(int, input().split())
	return k

def readlist():
	li = list(map(int, input().split()))
	return li

def anslist(li):
	ans = ' '.join([str(v) for v in li])
	return ans

def main():
	t = 1
	t = int(input())
	for xx in range(t):
		(n, x, a, b) = readvars()
		aa = a + b
		b = max(a, b)
		a = aa - b
		dis = b - a
		dis += min(a - 1, x)
		x -= min(a - 1, x)
		dis += min(n - b, x)
		x -= min(n - b, x)
		print(dis)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	(N, X, a, b) = map(int, input().split())
	if abs(a - b) + X > N - 1:
		print(N - 1)
	else:
		print(abs(a - b) + X)


t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	(a, b) = sorted([a, b])
	while x:
		if a > 1:
			a -= 1
			x -= 1
		else:
			break
	while x:
		if b < n:
			b += 1
			x -= 1
		else:
			break
	print(abs(a - b))


a = int(input())
b = []
for i in range(a):
	b = input().split()
	print(min([abs(int(b[2]) - int(b[3])) + int(b[1]), int(b[0]) - 1]))


k = int(input())
for i in range(k):
	(n, x, a, b) = map(int, input().split())
	c = min(a, b)
	d = max(a, b)
	if c - 1 + (n - d) - x > 0:
		print(d - c + x)
	else:
		print(n - 1)


I = input
for _ in [0] * int(I()):
	n = list(map(int, input().split()))
	if n[2] > n[3]:
		print(min(n[0] - 1, n[2] - n[3] + n[1]))
	else:
		print(min(n[0] - 1, n[3] - n[2] + n[1]))


n = int(input())
for i in range(n):
	(h, x, a, b) = map(int, input().split())
	if abs(a - b) + x > h - 1:
		print(h - 1)
	else:
		print(abs(a - b) + x)


t = int(input())
for query in range(t):
	(n, x, a, b) = map(int, input().split())
	y = abs(a - b)
	if y == n - 1:
		print(y)
	elif x + y >= n - 1:
		print(n - 1)
	else:
		print(x + y)


from collections import defaultdict as dc
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	print(abs(a - b) + min(x, a - 1 + n - b))


t = int(input())
for i in range(0, t):
	(n, x, a, b) = [int(x) for x in input().split()]
	maxm = n - 1
	currdiff = abs(b - a)
	maxdiff = currdiff + x
	if maxdiff > maxm:
		maxdiff = maxm
	print(maxdiff)


t = int(input())
for i in range(t):
	(n, x, a, b) = list(map(int, input().split()))
	init_distance = abs(a - b)
	left = min(a, b)
	right = max(a, b)
	num_possible_move = abs(left - 1) + abs(n - right)
	if x < num_possible_move:
		final_dist = init_distance + x
	else:
		final_dist = init_distance + num_possible_move
	print(final_dist)


t = int(input())
while t > 0:
	(n, x, a, b) = [int(i) for i in input().split()]
	if a > b:
		(a, b) = (b, a)
	if x > 0:
		while a - 1 >= 1 and x > 0:
			a = a - 1
			x = x - 1
		while b + 1 <= n and x > 0:
			b = b + 1
			x = x - 1
	print(b - a)
	t = t - 1


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	c = min(a, b) - 1 + (n - max(a, b))
	kq = 0
	if c <= x:
		kq = n - 1
	else:
		kq = max(a, b) - min(a, b) + x
	print(kq)


try:
	for _ in range(int(input())):
		(r, x, m, n) = map(int, input().split())
		t = min(m, n)
		q = max(m, n)
		while x > 0:
			if q != r:
				x -= 1
				q += 1
			else:
				break
		while x > 0:
			if t != 1:
				t -= 1
				x -= 1
			else:
				break
		print(abs(q - t))
except:
	pass


from sys import stdin
n = int(stdin.readline().rstrip())
for _ in range(n):
	(n, x, a, b) = map(int, stdin.readline().rstrip().split(' '))
	cur_dist = abs(a - b)
	if a > b:
		max_movements = min(b - 1 + (n - a), x)
		print(cur_dist + max_movements)
	else:
		max_movements = min(a - 1 + (n - b), x)
		print(cur_dist + max_movements)


t = int(input())
for i in range(t):
	(n, x, y, z) = input().split()
	n = int(n)
	x = int(x)
	y = int(y)
	z = int(z)
	reminder = x + abs(y - z)
	soltn = min(reminder, n - 1)
	print(soltn)
print()


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	M = max(a, b)
	m = min(a, b)
	p = n - M
	q = m - 1
	ans = 0
	if p + q >= x:
		ans = M - m + x
	else:
		ans = M - m + p + q
	print(ans)


t = int(input())
while t:
	(n, x, a, b) = map(int, input().split())
	print(min(n - 1, abs(a - b) + x))
	t -= 1


t = int(input())
for _ in range(t):
	(n, x, a, b) = [int(d) for d in input().split()]
	(left, right) = (0, 0)
	if a > b:
		(left, right) = (b, a)
	else:
		(left, right) = (a, b)
	left -= 1
	right -= 1
	dist = right - left
	if left != 0:
		if x >= left:
			dist += left
		else:
			dist += x
		x -= left
	if x > 0:
		if right != n - 1:
			if x >= n - 1 - right:
				dist += n - 1 - right
			else:
				dist += x
	print(dist)


for i in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if x == 0 or ((a == 1 or b == 1) and (a == n or b == n)):
		print(abs(a - b))
	elif abs(a - b) + x >= n:
		print(n - 1)
	else:
		print(abs(a - b) + x)


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

def solve():
	(n, x, a, b) = mints()
	if a > b:
		(a, b) = (b, a)
	while x > 0:
		if a != 1:
			a -= 1
		elif b != n:
			b += 1
		x -= 1
	print(b - a)
for i in range(mint()):
	solve()


import math
t = int(input())
ans = []
for i in range(t):
	(n, x, a, b) = input().split()
	n = int(n)
	x = int(x)
	a = int(a)
	b = int(b)
	left = min(a, b)
	right = max(a, b)
	ans.append(min(left - 1 + n - right, x) + (right - left))
for i in range(len(ans)):
	print(ans[i])


for x in range(int(input())):
	(a, b, p, s) = list(map(int, input().split()))
	if abs(p - s) < a - 1:
		for y in range(b):
			if p > s > 1:
				s = s - 1
			elif s > p > 1:
				p = p - 1
			elif a > p > s:
				p = p + 1
			elif a > s > p:
				s = s + 1
		print(abs(p - s))
	else:
		print(abs(p - s))


def rivalry(n, x, a, b):
	d = abs(a - b)
	max_boi = n - 1
	smol_boi = min(a, b)
	big_boi = max(a, b)
	if d == max_boi:
		return max_boi
	if n - big_boi <= x:
		x -= n - big_boi
		big_boi = n
	else:
		big_boi += x
		return abs(big_boi - smol_boi)
	if smol_boi - 1 <= x:
		x -= smol_boi - 1
		smol_boi = 1
	else:
		smol_boi -= x
		return abs(big_boi - smol_boi)
	return abs(big_boi - smol_boi)
t = int(input())
for i in range(t):
	arr = list(map(int, input().rstrip().split()))
	n = arr[0]
	x = arr[1]
	a = arr[2]
	b = arr[3]
	print(rivalry(n, x, a, b))


for tc in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	k1 = min(n, x + max(a, b))
	k2 = 0
	if k1 == n:
		k2 = x + max(a, b) - n
	k3 = max(1, min(a, b) - k2)
	print(k1 - k3)


for _ in range(0, int(input())):
	(n, x, a, b) = map(int, input().split())
	max_dist = n - 1
	if abs(a - b) == max_dist:
		print(max_dist)
	elif abs(a - b) < max_dist:
		if x == 0:
			print(abs(a - b))
		else:
			high = max(a, b)
			low = min(a, b)
			z1 = n - high
			z2 = low - 1
			t = max(z1, z2)
			if x >= z1 + z2:
				c = max_dist
				print(c)
			elif x >= max(z1, z2) and x < z1 + z2:
				c = abs(a - b) + x
				print(c)
			elif x < max(z1, z2):
				c = abs(a - b) + x
				print(c)


def mai(a):
	for i in range(a):
		(n, x, a, b) = map(int, input().split())
		h = max(a, b)
		l = min(a, b)
		r = 0
		if l - x >= 1:
			r = h - (l - x)
			print(r)
		else:
			if l != 1:
				x = x - l + 1
			l = 1
			if h + x >= n:
				print(n - l)
			else:
				r = h + x - l
				print(r)
mai(int(input()))


t = int(input())
for _ in range(t):
	(n, x, a, b) = [int(x) for x in input().split()]
	(a, b) = (min(a, b), max(a, b))
	rightDist = n - b
	leftDist = a - 1
	if x <= rightDist:
		b += x
		print(b - a)
	else:
		x -= rightDist
		if x >= leftDist:
			print(n - 1)
		else:
			a -= x
			print(n - a)


t = int(input())
for i in range(0, t):
	(n, x, a, b) = map(int, input().split())
	if x == 0:
		print(abs(a - b))
	elif abs(a - b) + 1 == n:
		print(abs(a - b))
	else:
		y = max(a, b)
		z = n - y
		if x >= z:
			y = y + z
			x = x - z
		else:
			y = y + x
			x = 0
		w = min(a, b)
		v = abs(1 - w)
		if x > v:
			w = w - v
		else:
			w = w - x
		print(abs(y - w))


t = int(input())
for a0 in range(t):
	(n, x, a, b) = map(int, input().split())
	if n > 0:
		print(min(n - 1, abs(a - b) + x))
	else:
		print(abs(a - b))


for i in range(int(input())):
	s = input().split()
	n = int(s[0])
	x = int(s[1])
	a = int(s[2])
	b = int(s[3])
	y = n - 1 - abs(a - b)
	if y <= x:
		print(n - 1)
	else:
		print(abs(b - a) + x)


t = int(input())
for i in range(t):
	in_ = input().split(' ')
	(n, x, a, b) = (int(in_[0]), int(in_[1]), int(in_[2]), int(in_[3]))
	max_dist = n - 1
	dist = abs(a - b)
	if dist == max_dist:
		print(max_dist)
	elif dist < max_dist:
		diff = abs(max_dist - dist)
		if diff <= x:
			print(max_dist)
		else:
			print(x + dist)


import sys
input = sys.stdin.readline

def main():
	N = int(input())
	for _ in range(N):
		(n, x, a, b) = [int(x) for x in input().split()]
		tmp = n - max(a, b) + min(a, b) - 1
		if x >= tmp:
			print(n - 1)
		else:
			print(abs(a - b) + x)
main()


case = int(input())
for i in range(case):
	(n, x, a, b) = (int(v) for v in input().split())
	if abs(a - b) + x >= n - 1:
		print(n - 1)
	else:
		print(abs(a - b) + x)


k = 0
for t in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	dist = abs(a - b)
	for i in range(0, x):
		if b < n:
			dist += 1
			b += 1
		elif a > 1:
			dist += 1
			a -= 1
	print(dist if dist <= n - 1 else n - 1)
	dist = 0


for _ in range(int(input())):
	(n, k, a, b) = map(int, input().split())
	print(min(n - 1, abs(a - b) + k))


t = int(input())
for i in range(t):
	(d, y, z, w) = (0, 0, 0, 0)
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(z, w) = (a, b)
		a = w
		b = z
	if n - b >= x:
		b += x
		d = b - a
	else:
		x -= n - b
		b = n
		d = n - a
		if a > x:
			a -= x
			d += x
		else:
			d = n - 1
	print(d)


n = int(input())
out = ''
for i in range(n):
	(n, x, a, b) = map(int, input().split(' '))
	dist = abs(a - b)
	max_dist = n - 1
	if abs(a - b) + x >= max_dist:
		out += str(max_dist) + '\n'
	else:
		out += str(abs(a - b) + x) + '\n'
print(out[:-1])


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		aux = a
		a = b
		b = aux
	mov = min(x, a - 1)
	a = a - mov
	x = x - mov
	if x > 0:
		mov = min(x, n - b)
		b = b + mov
	res = b - a
	print(res)


t = int(input())
l_sol = []
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	sol = abs(a - b)
	while sol < n - 1 and x > 0:
		sol += 1
		x -= 1
	l_sol.append(sol)
for i in l_sol:
	print(i)


for i in range(int(input())):
	inp = [int(x) for x in input().split()]
	n = inp[0]
	x = inp[1]
	a = inp[2]
	b = inp[3]
	mi = min(a, b)
	ma = max(a, b)
	first_student = None
	if x > 0:
		first = max(mi - 1, n - ma)
		dist = min(first, x)
		x -= dist
		if first == mi - 1:
			first_student = 0
			mi -= dist
		else:
			first_student = 1
			ma += dist
	if x > 0:
		second = None
		if first_student == 1:
			dist = min(mi - 1, x)
			x -= dist
			mi -= dist
		else:
			dist = min(n - ma, x)
			x -= dist
			ma += dist
	print(ma - mi)


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	A = abs(a - b)
	print(A + min(x, n - A - 1))


for e in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if x == 0:
		print(abs(a - b))
	else:
		p = min(a, b)
		q = max(a, b)
		if p - 1 <= x:
			a = 1
			x -= p - 1
		else:
			a = p - x
			x = 0
		b = min(q + x, n)
		print(b - a)


def solve_students(n, x, a, b):
	p = a
	s = b
	if p < s:
		tmp = p
		p = s
		s = tmp
	if x <= n - p:
		p += x
		x = 0
	else:
		x -= n - p
		p = n
	if x <= s - 1:
		s -= x
	else:
		s = 1
	print(p - s)
	return
t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().rstrip().split())
	solve_students(n, x, a, b)


n = int(input())
for i in range(n):
	(t, x, p1, p2) = map(int, input().split())
	z = abs(p2 - p1)
	z = z + x
	if z > t - 1:
		print(t - 1)
	else:
		print(z)


n = int(input())
for i in range(1, n + 1):
	z = list(map(int, input().split()))
	max_dlina = z[0] - 1
	dlina = abs(z[2] - z[3])
	if max_dlina - dlina < z[1]:
		print(max_dlina)
	else:
		print(dlina + z[1])


for _ in range(int(input())):
	(n, x, a, b) = [int(x) for x in input().split()]
	s = abs(a - b)
	if s + 1 == n:
		print(s)
	else:
		s = min(s + x, n - 1)
		print(s)


def solve(n, x, a, b):
	ans = 0
	total = abs(1 - min(a, b)) + abs(n - max(a, b))
	if total >= x:
		ans = abs(a - b) + x
	else:
		ans = abs(a - b) + total
	return ans

def main():
	t = int(input())
	result = []
	for i in range(t):
		(n, x, a, b) = map(int, input().split())
		ans = solve(n, x, a, b)
		result.append(ans)
	for r in result:
		print(r)
main()


no = int(input())
res = []
for i in range(no):
	(n, x, a, b) = map(int, input().split())
	xx = min(n - 1, abs(a - b) + x)
	res.append(xx)
for i in res:
	print(i)


for i in range(int(input())):
	(a, x, c, d) = map(int, input().split())
	if x == 0:
		print(abs(c - d))
	else:
		print(min(a - 1, abs(c - d) + x))


case = int(input())
while case > 0:
	(n, x, a, b) = input().split()
	print(min(int(n) - 1, abs(int(a) - int(b)) + int(x)))
	case = case - 1


from sys import stdout
t = int(input())
for i in range(t):
	(n, x, a, b) = [int(x) for x in input().split()]
	max_a = max(a, b)
	min_b = min(a, b)
	gap_a = n - max_a
	gap_b = min_b - 1
	result = 0
	if gap_a >= x:
		max_a = max_a + x
	else:
		max_a = max_a + gap_a
		x = x - gap_a
		if gap_b <= x:
			min_b = min_b - gap_b
		else:
			min_b = min_b - x
	result = max_a - min_b
	stdout.write('{}\n'.format(result))


from sys import stdin
for _ in range(int(stdin.readline())):
	(n, x, a, b) = map(int, stdin.readline().split())
	if a == 1 and b == n:
		print(n - 1)
	else:
		if a < b:
			while x > 0 and a > 1:
				a -= 1
				x -= 1
			while x > 0 and b < n:
				b += 1
				x -= 1
		else:
			while x > 0 and b > 1:
				b -= 1
				x -= 1
			while x > 0 and a < n:
				a += 1
				x -= 1
		print(abs(a - b))


x = int(input())
for i in range(x):
	p = 0
	(n, x, a, b) = list(map(int, input().split()))
	o = max(a, b)
	k = min(a, b)
	for i in range(x):
		if k == 1:
			break
		k -= 1
		x -= 1
	for i in range(x):
		if o == n:
			break
		o += 1
	print(o - k)


def rint():
	return int(input())

def rints():
	return list(map(int, input().split()))
t = rint()
for _ in range(t):
	(n, x, a, b) = rints()
	a -= 1
	b -= 1
	(a, b) = sorted((a, b))
	l = min(a, x)
	a -= l
	x -= l
	r = min(n - 1 - b, x)
	b += r
	print(b - a)


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().strip().split())
	max = n - 1
	r = abs(a - b) + x
	if r < max:
		print(r)
	else:
		print(max)


t = int(input())
while t:
	(n, x, a, b) = [int(x) for x in input().split()]
	print(min(n - 1, abs(b - a) + x))
	t -= 1


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	if b < a:
		(a, b) = (b, a)
	step = min(a - 1, x)
	a -= step
	x -= step
	b += min(x, n - b)
	print(b - a)


x = int(input())
y = []
for c in range(x):
	y.append(list(map(int, input().split())))
for c in y:
	t = 0
	t = t + (c[2] - c[3])
	if t < 0:
		t = t * -1
	t = t + c[1]
	if t >= c[0]:
		t = 0
		t = t + c[0] - 1
	print(t)


from statistics import median
from bisect import bisect_right as br
import sys
from collections import *
from math import *
import re

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	c = 0
	for i in range(2, n):
		if prime[i]:
			c += 1
	return c

def totient(n):
	(res, p) = (n, 2)
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			res -= int(res / p)
		p += 1
	if n > 1:
		res -= int(res / n)
	return res

def iseven(n):
	return [False, True][0 if n % 2 else 1]

def inp_matrix(n):
	return list(([input().split()] for i in range(n)))

def inp_arr():
	return list(map(int, input().split()))

def inp_integers():
	return map(int, input().split())

def inp_strings():
	return input().split()

def lcm(a, b):
	return a * b / gcd(a, b)
max_int = sys.maxsize
mod = 10 ** 9 + 7
flag1 = False
flag2 = False
mod = 10000007
t = int(input())
while t:
	(n, x, a, b) = inp_integers()
	print(min(n - 1, abs(a - b) + x))
	t -= 1


t = int(input())
for i in range(t):
	s = input()
	(n, x, a, b) = [int(x) for x in s.split(' ')]
	print(min(abs(a - b) + x, n - 1))


t = int(input())
while t > 0:
	t -= 1
	(n, x, a, b) = list(map(int, input().split(' ')))
	smallest = min(a, b)
	biggest = max(a, b)
	diff = biggest - smallest
	room = min(n - 1 - biggest + smallest, x)
	print(diff + room)


t = int(input(''))
for k in range(0, t):
	(p, q, r, s) = input('').split(' ')
	n = int(p)
	x = int(q)
	a = int(r)
	b = int(s)
	if a > b:
		while a < n and x > 0:
			a = a + 1
			x = x - 1
		while b > 1 and x > 0:
			b = b - 1
			x = x - 1
	elif a < b:
		while b < n and x > 0:
			b = b + 1
			x = x - 1
		while a > 1 and x > 0:
			a = a - 1
			x = x - 1
	print(abs(b - a))


for _ in range(int(input())):
	(n, x, A, B) = map(int, input().split())
	a = min(A, B)
	b = max(A, B)
	if a == 1 and b == n:
		print(n - 1)
	elif x == 0:
		print(b - a)
	else:
		if x >= a - 1:
			x -= a - 1
			a = 1
		else:
			a -= x
			x = 0
		if x >= n - b:
			x -= n - b
			b = n
		else:
			b += x
			x = 0
		print(b - a)


def main():
	(n, x, a, b) = map(int, input().split())
	k = max(a, b)
	j = min(a, b)
	while k < n and x > 0:
		k += 1
		x -= 1
	while j > 1 and x > 0:
		j -= 1
		x -= 1
	print(k - j)
t = int(input())
for i in range(t):
	main()


import sys

def main():
	t = int(sys.stdin.readline().strip())
	while t > 0:
		(n, x, a, b) = map(int, sys.stdin.readline().strip().split())
		if a > b:
			(a, b) = (b, a)
		print(min(b - a + x, n - 1))
		t -= 1
	return
main()


k = int(input())
for i in range(k):
	(n, x, a, b) = list(map(int, input().split(' ')))
	diff = abs(a - b)
	ps = n - (diff + 1)
	if x == 0:
		print(diff)
	else:
		if x > ps:
			x = ps
		print(x + diff)


import sys
import collections
from collections import Counter
import itertools
import math
import timeit

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	divisors = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				divisors.append(i)
			else:
				divisors.extend([i, n // i])
	return divisors

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def flin(d, x, default=-1):
	left = right = -1
	for i in range(len(d)):
		if d[i] == x:
			if left == -1:
				left = i
			right = i
	if left == -1:
		return (default, default)
	else:
		return (left, right)

def ceil(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return collections.defaultdict(int)
t = ii()
for _ in range(t):
	(n, x, a, b) = mi()
	if a > b:
		(a, b) = (b, a)
	t = a - 1
	t = min(x, t)
	a -= t
	x = x - t
	t = n - b
	t = min(x, t)
	b += t
	print(b - a)


tt = int(input())
while tt:
	tt -= 1
	import math
	ll = list(map(int, input().split()))
	(n, x, a, b) = (ll[0], ll[1], ll[2], ll[3])
	l = []
	for i in range(1, n + 1):
		l.append(i)
	if a == l[0] and b == l[-1] or (a == l[-1] and b == l[0]):
		print(abs(a - b))
	elif x == 0:
		print(abs(a - b))
	else:
		print(min(n - 1, abs(a - b) + x))


for _ in range(int(input())):
	(n, x, y, z) = map(int, input().split())
	if x == 0:
		print(abs(y - z))
	else:
		m1 = min(y, z)
		m2 = max(y, z)
		a1 = m1 - 1
		a2 = n - m2
		while m1 > 1 and x > 0:
			m1 -= 1
			x -= 1
		while m2 < n and x > 0:
			m2 += 1
			x -= 1
		print(abs(m1 - m2))


q = int(input())
for _ in range(q):
	(n, x, a, b) = map(int, input().split())
	g = abs(a - b)
	g += x
	if g > n - 1:
		g = n - 1
	print(g)


tests = int(input())
while tests:
	tests -= 1
	(n, x, a, b) = map(int, input().split())
	l = [x for x in range(1, n + 1)]
	if x == 0:
		print(abs(a - b))
		continue
	(a, b) = (min(a, b), max(a, b))
	n1 = a - x
	if n1 > 0:
		x = 0
	else:
		x = 1 - n1
		n1 = 1
	n2 = b + x if b + x <= n else n
	print(abs(n1 - n2))


for _ in range(int(input())):
	(n, x, l, r) = map(int, input().split())
	print(min(abs(l - r) + x, n - 1))


from math import *
from copy import *
from string import *
from random import *
from sys import stdin
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
import math

def bin1(l, r, k, t, b, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		v = k ** mid
		if v == val:
			return v
		elif v > val:
			ans = mid
			return bin1(mid + 1, r, k, t, b, val, ans)
		else:
			return bin1(l, mid - 1, k, t, b, val, ans)

def bin2(l, r, k, t, b, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		v = t * k ** mid + b * mid
		if v == val:
			return v
		elif v > val:
			ans = mid
			return bin2(l, mid - 1, k, t, b, val, ans)
		else:
			return bin2(mid + 1, r, k, t, b, val, ans)

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	l = []
	for i in range(2, n + 1):
		if prime[i]:
			l.append(i)
	return l

def bin(l, r, ll, val):
	if l > r:
		return -1
	else:
		mid = (l + r) // 2
		if val >= ll[mid][0] and val <= ll[mid][1]:
			return mid
		elif val < ll[mid][0]:
			return bin(l, mid - 1, ll, val)
		else:
			return bin(mid + 1, r, ll, val)

def deci(n):
	s = ''
	while n != 0:
		if n % 2 == 0:
			n = n // 2
			s = '0' + s
		else:
			n = n // 2
			s = '1' + s
	return s

def diff(s1, s2):
	if len(s1) < len(s2):
		v = len(s1)
		while v != len(s2):
			s1 = '0' + s1
			v = v + 1
	else:
		v = len(s2)
		while v != len(s1):
			s2 = '0' + s2
			v = v + 1
	c = 0
	for i in range(len(s1)):
		if s1[i:i + 1] != s2[i:i + 1]:
			c = c + 1
	return c
from sys import stdin, stdout

def fac(a, b):
	v = a
	while a != b:
		v *= a - 1
		a = a - 1
	return v

def bino(l, r, n):
	if l > r:
		return -1
	else:
		mid = (l + r) // 2
		val1 = math.log(n / mid + 1, 2)
		val2 = int(val1)
		if val1 == val2:
			return val1
		elif val1 < 1.0:
			return bino(l, mid - 1, n)
		else:
			return bino(mid + 1, r, n)

def binary(l, r, ll, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		if ll[mid] == val:
			return ll[mid]
		elif ll[mid] < val:
			ans = ll[mid]
			return binary(mid + 1, r, ll, val, ans)
		else:
			return binary(l, mid - 1, ll, val, ans)

def check(n):
	v = 1
	while n != 0:
		if n % 10 == 0 or n % 10 == 1:
			n = n // 10
		else:
			v = 0
			break
	return v
t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split(' '))
	if x >= min(a, b) - 1 + n - max(b, a):
		print(n - 1)
	else:
		print(max(b, a) - min(a, b) + x)


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	value1 = abs(a - b)
	value1 += x
	value2 = int(n)
	value3 = int(min(value1, value2 - 1))
	print(value3)


results = []
for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split(' '))
	(a, b) = (min(a, b), max(a, b))
	dis = b - a
	if x > a - 1 + (n - b):
		results.append(n - 1)
	else:
		results.append(dis + x)
for i in results:
	print(i)


for _ in range(int(input())):
	(n, x, y, z) = map(int, input().split())
	a = min(y, z)
	b = max(y, z)
	y = b + min(x, n - b)
	x -= min(x, n - b)
	z = max(a - x, 1)
	print(y - z)


casos = int(input())
for i in range(casos):
	(n, x, a, b) = [int(x) for x in input().split()]
	diff = abs(b - a)
	resultado = min(n - 1, diff + x)
	print(resultado)


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if abs(a - b) == n - 1:
		print(abs(a - b))
	elif x == 0:
		print(abs(a - b))
	else:
		t = max(a, b)
		p = min(a, b)
		if t + x <= n:
			print(t + x - p)
		else:
			r = t + x - n
			if p > 0:
				s = p - r
				if s > 0:
					print(n - s)
				else:
					print(n - 1)


t = int(input())
for i in range(t):
	(n, m, a, b) = list(map(int, input().split()))
	print(min(abs(a - b) + m, n - 1))


import random

def get_many_ints():
	return list(map(int, input().split()))

def get_int():
	return int(input())

def get_int_list():
	return list(get_many_ints())

def prog():
	(n, x, a, b) = get_many_ints()
	if a > b:
		(a, b) = (b, a)
	da = min(a - 1, x)
	a -= da
	x -= da
	db = min(n - b, x)
	x -= db
	b += db
	print(b - a)
q = get_int()
for _ in range(q):
	prog()


t = int(input())
for _ in range(t):
	arr = [int(num) for num in input().split()]
	if arr[2] < arr[3]:
		if arr[2] - 1 + arr[0] - arr[3] <= arr[1]:
			print(arr[0] - 1)
		else:
			print(arr[3] - arr[2] + arr[1])
	elif arr[3] - 1 + arr[0] - arr[2] <= arr[1]:
		print(arr[0] - 1)
	else:
		print(arr[2] - arr[3] + arr[1])


x = int(input())
for i in range(0, x):
	m = list(map(int, input().split()))[:4]
	k = abs(m[2] - m[3]) + m[1]
	d = m[0] - 1
	if k <= d:
		print(k)
	else:
		print(d)


n = int(input())
res = []
for i in range(n):
	(n, x, a, b) = [int(x) for x in input().split(' ')]
	if a > b:
		m = b + n - a - 1
	else:
		m = a + n - b - 1
	if x > m:
		res.append(m + abs(a - b))
	else:
		res.append(x + abs(a - b))
for y in res:
	print(y)


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		t = b
		b = a
		a = t
	while x > 0 and (a > 1 or b < n):
		x -= 1
		if a > 1:
			a -= 1
		elif b < n:
			b += 1
	print(b - a)


import sys
import math
for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	(a, b) = sorted([a, b])
	if x >= a + n - b:
		print(n - 1)
		continue
	print(b - a + x)


n = int(input())
for i in range(n):
	p = list(map(int, input().split()))
	_min = min(p[0] - 1, abs(p[2] - p[3]) + p[1])
	print(_min)


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	diff = abs(a - b)
	space = n - diff - 1
	allowed = min(space, x)
	print(diff + allowed)


t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	A = min(a, b)
	B = max(a, b)
	if A - x > 1:
		A = A - x
	else:
		x = x - (A - 1)
		A = 1
		if B + x < n:
			B = B + x
		else:
			B = n
	print(abs(B - A))


for _ in range(int(input())):
	(n, x, a, b) = list(map(int, input().split()))
	d = abs(a - b)
	ans = x + d
	print(min(n - 1, ans))


a = int(input())
for i in range(a):
	(n, k, x, y) = map(int, input().split())
	print(min(n - 1, abs(x - y) + k))


t = int(input())
for _ in range(t):
	(n, sw, a, b) = map(int, input().split())
	diff = abs(a - b)
	maxi = n - 1
	if diff == maxi:
		print(maxi)
	elif maxi > sw + diff:
		print(diff + sw)
	else:
		print(maxi)


n = int(input())
c = []
for i in range(n):
	s = list(map(int, input().split()))
	c.append(s)

def disdance(n1, x, a, b):
	res = abs(a - b)
	res2 = n1 - res - 1
	if res2 >= x:
		return x + res
	else:
		return n1 - 1
for x in range(n):
	print(disdance(c[x][0], c[x][1], c[x][2], c[x][3]))


def max(p, q):
	if p > q:
		return (q, p)
	else:
		return (p, q)
for t in range(int(input())):
	(n, x, p, q) = map(int, input().split())
	(a, b) = max(p, q)
	x1 = a - 1
	x2 = n - b
	if x1 + x2 < x:
		print(n - 1)
	else:
		print(b - a + x)


import math
for i in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if math.fabs(a - b) == n - 1 or x == 0:
		print(int(math.fabs(a - b)))
	else:
		w = math.fabs(a - b) + x
		if w >= n:
			print(n - 1)
		else:
			print(int(w))


t = int(input())
while t:
	t += -1
	(n, x, a, b) = [int(x) for x in input().split()]
	if a > b:
		(a, b) = (b, a)
	ans = 0
	if x >= a - 1:
		x -= a - 1
		a = 1
	else:
		a -= x
		x = 0
	if x >= n - b:
		b = n
	else:
		b += x
	print(b - a)


import sys

def main():
	inp = sys.stdin.read().strip().split('\n')[1:]
	out = []
	for i in inp:
		(n, x, a, b) = map(int, i.split())
		out.append(min(abs(a - b) + x, n - 1))
	return out
print(*main(), sep='\n')


n = int(input())
for _ in range(n):
	(n, x, a, b) = map(int, input().split())
	blank = min(a, b) - 1 + (n - max(a, b))
	if blank <= x:
		print(n - 1)
	else:
		print(abs(a - b) + x)


t = int(input())
for tt in range(t):
	(n, x, a, b) = map(int, input().split())
	mn = min(a, b)
	mx = max(a, b)
	if x == 0:
		print(abs(b - a))
	else:
		while mn > 1 and x > 0:
			mn -= 1
			x -= 1
		while mx < n and x > 0:
			mx += 1
			x -= 1
		print(abs(mx - mn))


T = int(input())
for t in range(T):
	(n, x, a, b) = map(int, input().split())
	res = min(n - 1, abs(a - b) + x)
	print(res)


import math
from sys import stdin, stdout
in1 = int(stdin.readline())
lines = stdin.readlines()
for line in lines:
	in2 = line.split()
	in2 = list(map(int, in2))
	og = int(math.fabs(in2[2] - in2[3]))
	if og == in2[0] - 1:
		print(og)
	else:
		dis = int(max(in2[2], in2[3]) - min(in2[2], in2[3]) + in2[1])
		if dis <= in2[0] - 1:
			print(dis)
		else:
			print(in2[0] - 1)


for _ in range(int(input())):
	(n, x, a, b) = list(map(int, input().split()))
	k = abs(a - b)
	if k < n - 1:
		y = n - 1 - k
		if x <= y:
			print(k + x)
		else:
			print(k + y)
	else:
		print(k)


no_of_test_cases = int(input())
my_answers = []
for _ in range(no_of_test_cases):
	(n, x, a, b) = map(int, input().split())
	if x == 0:
		my_answers.append(abs(a - b))
	elif n == a and b == 1 or (n == b and a == 1):
		my_answers.append(abs(a - b))
	else:
		if a > b:
			temp1 = n - a
			temp2 = b
			if temp1 > x:
				a += x
			elif b > x:
				b -= x
			else:
				a += temp1
				x = x - temp1
				if x >= temp2:
					b = 1
				else:
					b -= x
			my_answers.append(a - b)
		if a < b:
			temp1 = n - b
			temp2 = a
			if temp1 > x:
				b += x
			elif a > x:
				a -= x
			else:
				b += temp1
				x = x - temp1
				if x >= temp2:
					a = 1
				else:
					a -= x
			my_answers.append(b - a)
for answer in my_answers:
	print(answer)


T = int(input())
for i in range(T):
	A = input()
	A = A.split()
	(n, x, a, b) = (int(A[0]), int(A[1]), int(A[2]), int(A[3]))
	if abs(b - a) + x < n:
		print(abs(b - a) + x)
	else:
		print(n - 1)


import sys

def main():
	for _ in range(int(input())):
		(n, x, a, b) = [int(x) for x in sys.stdin.readline().split()]
		if a > b:
			ans = n - a
			if x <= ans:
				a = a + x
				x = 0
			else:
				a = n
				x = x - ans
			an = b - 1
			if x <= an:
				b = b - x
			else:
				b = 1
				x = x - an
			print(a - b)
		else:
			(a, b) = (b, a)
			ans = n - a
			if x <= ans:
				a = a + x
				x = 0
			else:
				a = n
				x = x - ans
			an = b - 1
			if x <= an:
				b = b - x
			else:
				b = 1
				x = x - an
			print(a - b)
main()


q = int(input())
in_data = []
for _ in range(q):
	in_data.append([int(i) for i in input().split()])
for (n, x, a, b) in in_data:
	start_distance = abs(a - b)
	max_distance = n - 1
	if x == 0 or start_distance == max_distance:
		print(start_distance)
	elif start_distance + x <= max_distance:
		print(start_distance + x)
	elif start_distance + x > max_distance:
		print(max_distance)


T = int(input())
for test in range(T):
	(N, X, A, B) = list(map(int, input().split()))
	print(min(N - 1, abs(B - A) + X))


t = int(input())

def foo(n, x, a, b):
	while b > 1 and x > 0:
		b = b - 1
		x = x - 1
	while a < n and x > 0:
		a = a + 1
		x = x - 1
	return abs(a - b)
for i in range(t):
	(n, x, a, b) = [int(k) for k in input().split()]
	print(foo(n, x, max(a, b), min(a, b)))


t = int(input())
for i in range(0, t, 1):
	(n, x, a, b) = map(int, input().split())
	r = x + abs(a - b)
	if n < r + 1:
		r = n - 1
	print(r)


from sys import stdin, stdout, exit as sys_ret
(f_input, f_print, f_flush) = (stdin.readline, stdout.write, stdout.flush)
for _ in range(int(f_input())):
	(amount, moves, a, b) = map(int, f_input().split())
	if a > b:
		(a, b) = (b, a)
	if b + moves >= amount:
		moves -= amount - b
		b = amount
		if a - moves <= 1:
			moves -= a - 1
			a = 1
		else:
			a -= moves
			moves = 0
	else:
		b += moves
		moves = 0
	f_print(str(b - a) + '\n')


n = int(input())
for i in range(n):
	angka = [int(x) for x in input().split()]
	if angka[2] > angka[3]:
		while angka[1] != 0:
			if angka[2] != angka[0]:
				angka[2] += 1
			elif angka[3] != 1:
				angka[3] -= 1
			angka[1] -= 1
	elif angka[3] > angka[2]:
		while angka[1] != 0:
			if angka[3] != angka[0]:
				angka[3] += 1
			elif angka[2] != 1:
				angka[2] -= 1
			angka[1] -= 1
	print(abs(angka[2] - angka[3]))


from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations
import sys
import bisect
import string

def I():
	return int(input())

def MI():
	return map(int, input().split())

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
YN = ['Yes', 'No']
mo = 10 ** 9 + 7
inf = float('inf')
input = sys.stdin.readline
show_flg = False
show_flg = True
t = I()
for _ in range(t):
	(n, x, a, b) = LI()
	ans = min(n - 1, abs(a - b) + x)
	print(ans)


t = int(input())
for i in range(t):
	arr = input().split()
	(n, x, a, b) = (int(arr[0]), int(arr[1]), int(arr[2]), int(arr[3]))
	currDis = abs(a - b)
	while True:
		if x == 0:
			break
		if currDis == n - 1:
			break
		elif currDis < n - 1:
			currDis += 1
			x -= 1
	print(currDis)


n = int(input())
array = []
for i in range(n):
	(n, x, a, b) = input().split()
	n = int(n)
	x = int(x)
	a = int(a)
	b = int(b)
	dist = abs(b - a)
	dist += x
	if dist > n - 1:
		dist = n - 1
	array.append(dist)
for num in array:
	print(num)


t = int(input())
while t:
	t -= 1
	(n, x, a, b) = map(int, input().split())
	if a > b:
		c = a - 1
		d = b - 1
	else:
		c = b - 1
		d = a - 1
	temp = x
	while temp > 0:
		temp -= 1
		if c + 1 < n:
			c = c + 1
		elif d > 0:
			d = d - 1
		else:
			break
	print(c - d)


import sys
import math
import itertools
import collections

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = prime[1] = False
	r = [p for p in range(n + 1) if prime[p]]
	return r

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def wr(arr):
	return ''.join(map(str, arr))

def revn(n):
	return str(n)[::-1]

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base=3):
	newnumber = ''
	while number > 0:
		newnumber = str(number % base) + newnumber
		number //= base
	return newnumber
t = ii()
for _ in range(t):
	(n, x, a, b) = mi()
	print(min(abs(a - b) + x, n - 1))


kol = int(input())
vse = [input().split() for i in range(kol)]
for i in range(kol):
	moj = int(vse[i][1])
	per = int(vse[i][2])
	vtor = int(vse[i][3])
	uch = int(vse[i][0])
	res1 = abs(per - vtor)
	while res1 < uch - 1 and moj != 0:
		moj -= 1
		res1 += 1
	print(res1)


t = int(input())
output = []
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	d = abs(a - b)
	(a_index, b_index) = (a, b)
	while x > 0 and d < n - 1:
		if a_index > 1:
			x -= 1
			a_index -= 1
			d += 1
		elif b_index < n:
			x -= 1
			b_index += 1
			d += 1
	output.append(str(d))
print('\n'.join(output))


for i in range(int(input())):
	(n, x, a, b) = list(map(int, input().split()))
	t = [a, b]
	t.sort()
	if t[1] + x <= n:
		print(t[1] + x - t[0])
	elif t[1] + x > n and t[0] - (x - (n - t[1])) >= 1:
		y = t[0] - (x - (n - t[1]))
		print(t[1] + (n - t[1]) - y)
	elif t[1] + x > n and t[0] - (x - (n - t[1])) < 1:
		print(n - 1)
	elif t[0] == 1 and t[1] == n:
		print(n - 1)


test = int(input())
for q in range(test):
	(n, x, a, b) = map(int, input().split())
	w = abs(a - b) + x
	print(min(n - 1, w))


t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	l1 = []
	l1.append(a)
	l1.append(b)
	l1.sort()
	c = abs(1 - l1[0])
	k = abs(n - l1[1])
	print(abs(a - b) + min(c + k, x))


T = int(input())
for _ in range(T):
	(n, x, xx, yy) = map(int, input().split())
	ans = 0
	a = min(xx, yy)
	b = max(xx, yy)
	p = a
	a = max(a - x, 1)
	x -= p - a
	b = min(x + b, n)
	print(b - a)


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	t = abs(a - b)
	for i in range(x):
		if a - x <= n and b - x <= n:
			if t + 1 < n:
				t = t + 1
	print(t)


fg = int(input())
for i in range(fg):
	aa = input()
	arr = aa.split()
	n = int(arr[0])
	x = int(arr[1])
	a = int(arr[2])
	b = int(arr[3])
	high = int(n)
	low = int(1)
	mins = min(a, b)
	maxs = max(a, b)
	if mins == low and maxs == high:
		print(high - low)
	elif mins == low:
		cur = maxs - mins
		tmp = min(high - maxs, x)
		cur += tmp
		print(cur)
	else:
		cur = maxs - mins
		lowdiff = mins - low
		highdiff = high - maxs
		total = lowdiff + highdiff
		gyu = min(total, x)
		cur += gyu
		print(cur)


t = int(input())
while t > 0:
	t -= 1
	(n, k, a, b) = map(int, input().split())
	maxi = max(a, b)
	mini = min(a, b)
	if maxi + k > n:
		k = k - (n - maxi)
		maxi = n
		mini = max(1, mini - k)
		print(maxi - mini)
	else:
		print(maxi + k - mini)


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	if a - x > 0:
		(a, x) = (a - x, 0)
	else:
		(a, x) = (1, x - a + 1)
	b = min(n, b + x)
	print(b - a)


test_cases = int(input())
input_ = []
for i in range(test_cases):
	input_.append(input())
for i in range(len(input_)):
	inputs = input_[i].split()
	n = int(inputs[0])
	x = int(inputs[1])
	a = int(inputs[2])
	b = int(inputs[3])
	curr_dist = abs(a - b)
	max_dist = n - 1
	if x == 0:
		print(curr_dist)
		continue
	if curr_dist == max_dist:
		print(curr_dist)
		continue
	xx = 0
	if a > b:
		while curr_dist < max_dist and xx < x:
			if a != n:
				a += 1
				curr_dist += 1
				xx += 1
			elif b != 1:
				b -= 1
				curr_dist += 1
				xx += 1
	else:
		while curr_dist < max_dist and xx < x:
			if b != n:
				b += 1
				curr_dist += 1
				xx += 1
			elif a != 1:
				a -= 1
				curr_dist += 1
				xx += 1
	print(curr_dist)


from __future__ import division, print_function

def main():
	import sys
	data = iter(map(int, sys.stdin.buffer.read().decode('ascii').split()))
	n = next(data)
	res = [None] * n
	for i in range(n):
		(n, x, a, b) = tuple((next(data) for _ in range(4)))
		(a, b) = (min(a, b), max(a, b))
		b += x
		res[i] = min(b - a, n - 1)
	sys.stdout.buffer.write('\n'.join((str(x) for x in res)).encode('ascii'))
	return 0
main()


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	for j in range(x):
		if n != a and a > b:
			a += 1
		elif n != b and b > a:
			b += 1
		elif a != 1 and a < b:
			a -= 1
		elif b != 1 and b < a:
			b -= 1
	s = abs(a - b)
	print(s)


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	ad = max(1, a - x)
	x -= a - ad
	bd = min(n, b + x)
	print(bd - ad)


t = int(input())
for i in range(t):
	(n, x, a, b) = [int(x) for x in input().split()]
	if a > b:
		(a, b) = (b, a)
	mv_a = a - 1
	mv_b = n - b
	can_mv = min(mv_a + mv_b, x)
	print(can_mv + b - a)


t = int(input())
while t > 0:
	t -= 1
	inpt = list(map(int, input().split()))
	n = inpt[0]
	x = inpt[1]
	a = inpt[2]
	b = inpt[3]
	if a > b:
		tmp = a
		a = b
		b = tmp
	tmp1 = a - 1
	tmp2 = n - b
	tmp3 = tmp1 + tmp2 - x
	if tmp3 > 0:
		print(n - 1 - tmp3)
	else:
		print(n - 1)


t = int(input())
while t:
	(n, x, a, b) = input().split()
	n = int(n)
	x = int(x)
	a = int(a)
	b = int(b)
	if a > b:
		c = a
		d = b
	else:
		c = b
		d = a
	while x:
		if c < n:
			c = c + 1
			x = x - 1
		elif d > 1:
			d = d - 1
			x = x - 1
		else:
			break
	print(abs(c - d))
	t = t - 1


t = int(input())
while t > 0:
	t -= 1
	(n, x, a, b) = map(int, input().split(' '))
	k = min(a, b)
	r = max(a, b)
	a = k
	b = r
	s = 0
	p = max(1, a - x)
	x = x - (a - p)
	a = p
	b = min(n, b + x)
	s = b - a
	print(s)


amount = int(input())
for i in range(amount):
	(n, x, a, b) = [int(s) for s in input().split()]
	if x < n - max(a, b) + (min(a, b) - 1):
		print(max(a, b) - min(a, b) + x)
	else:
		print(n - 1)


import math
import collections
a = collections.OrderedDict()

def main():
	t = int(input())
	for i in range(t):
		(n, x, a, b) = tuple([int(x) for x in input().split()])
		result = abs(a - b) + x
		if result > n - 1:
			print(n - 1)
		else:
			print(result)
main()


from sys import stdin, stdout
from collections import Counter
import math

def rsingle_int():
	return int(stdin.readline().rstrip())

def rmult_int():
	return [int(x) for x in stdin.readline().rstrip().split()]

def rmult_str():
	return stdin.readline().rstrip().split()

def r_str():
	return stdin.readline().rstrip()

def rsingle_char():
	return stdin.read(1)

def main():
	t = rsingle_int()
	for i in range(t):
		(n, x, a, b) = rmult_int()
		max_d = abs(a - b) + x
		print(max_d if max_d < n else n - 1)
main()


n = int(input())
for ni in range(n):
	(sctr, sp, a, b) = (int(i) for i in input().split(' '))
	d = abs(a - b)
	print(min(d + sp, sctr - 1))


n = int(input())
l = []
aux = []
j = 0
res = 0
a = 0
l2 = []
for i in range(n):
	li = list(map(int, input().split()))
	l.append(li[0])
	l.append(li[1])
	l.append(li[2])
	l.append(li[3])
while j + 4 <= len(l):
	aux = l[j:j + 4]
	if abs(aux[2] - aux[3]) == aux[0] - 1 or abs(aux[2] - aux[3]) + aux[1] >= aux[0]:
		res = abs(aux[0] - 1)
		l2.append(res)
		j = j + 4
	else:
		a = abs(aux[2] - aux[3])
		res = a + aux[1]
		j = j + 4
		l2.append(res)
for k in range(len(l2)):
	print(l2[k])


t = int(input())
for _ in range(t):
	(n, x, a, b) = list(map(int, input().split()))
	mi = min(a, b)
	ma = max(a, b)
	c = abs(a - b)
	ans = min(x, mi - 1)
	x -= ans
	c += ans
	ans = min(x, n - ma)
	c += ans
	print(c)


t = int(input())
for i in range(0, t):
	(n, x, a, b) = [int(x) for x in input().split()]
	k = a - b
	if k < 0:
		k = k * -1
	if x + k > n - 1:
		print(n - 1)
	else:
		print(x + k)


t = int(input())
for i in range(0, t):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		temp = a
		a = b
		b = temp
	d = n - 1
	c = abs(b - a)
	if c == d:
		s = d
	elif x >= n - b + (a - 1):
		s = d
	else:
		while x > 0:
			if b < n:
				b = b + 1
				x = x - 1
			if x > 0:
				if a > 1:
					a = a - 1
					x = x - 1
		s = abs(b - a)
	print(s)


rr = lambda : input().strip()
rri = lambda : int(rr())
rrm = lambda : map(int, rr().split())
T = rri()
for tc in range(1, T + 1):
	(n, x, a, b) = rrm()
	ans = abs(a - b) + x
	ans = ans if ans < n else n - 1
	print(ans)


tc = int(input())
for ii in range(tc):
	(n, x, a, b) = map(int, input().split())
	dif = abs(a - b)
	maxdif = n - 1
	if x < maxdif - dif:
		ans = dif + x
	else:
		ans = maxdif
	print(ans)


for case in range(int(input())):
	(m, n, a, b) = map(int, input().split())
	c = min(a, b)
	d = max(a, b)
	while c != 1:
		if n != 0:
			c = c - 1
			n = n - 1
		else:
			break
	while d != m:
		if n != 0:
			d = d + 1
			n = n - 1
		else:
			break
	print(d - c)


for i in range(int(input())):
	(n, x, a, b) = (int(j) for j in input().split())
	print(min(n - 1, abs(a - b) + x))


t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	max_distance = n - 1
	current_distance = abs(a - b)
	new_distance = current_distance + x
	print(max_distance if new_distance >= max_distance else new_distance)


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	maxi = abs(b - a)
	if b > a:
		temp = b
		b = a
		a = temp
	for _ in range(x):
		if abs(b - a) == n - 1:
			maxi = abs(b - a)
			break
		elif a != n:
			a += 1
			m = abs(b - a)
			if m > maxi:
				maxi = m
		else:
			b -= 1
			m = abs(b - a)
			if m > maxi:
				maxi = m
	print(maxi)


r = []
t = int(input())
for s in range(t):
	z = input().split(' ')
	for i in range(len(z)):
		z[i] = int(z[i])
	if z[2] > z[3]:
		while z[0] > z[2] and z[1] > 0:
			z[2] = z[2] + 1
			z[1] = z[1] - 1
		if z[1] > 0:
			while z[3] > 1 and z[1] > 0:
				z[3] = z[3] - 1
				z[1] = z[1] - 1
		r.append(z[2] - z[3])
	else:
		while z[0] > z[3] and z[1] > 0:
			z[3] = z[3] + 1
			z[1] = z[1] - 1
		if z[1] > 0:
			while z[2] > 1 and z[1] > 0:
				z[2] = z[2] - 1
				z[1] = z[1] - 1
		r.append(z[3] - z[2])
for f in range(len(r)):
	print(r[f])


t = int(input())
while t > 0:
	(n, x, a, b) = input().split()
	n = int(n)
	x = int(x)
	a = int(a)
	b = int(b)
	firstindex = 0
	lastindex = 0
	possible = 0
	diff = 0
	if a > b:
		firstindex = b
		lastindex = a
	else:
		firstindex = a
		lastindex = b
	diff = lastindex - firstindex
	possible = firstindex - 1 + (n - lastindex)
	while possible > 0 and x > 0:
		diff = diff + 1
		possible -= 1
		x -= 1
	print(diff)
	t = t - 1


import sys

def main():
	for tc in range(int(input())):
		(n, x, a, b) = get_ints()
		a -= 1
		b -= 1
		if a > b:
			(a, b) = (b, a)
		while x > 0 and a > 0:
			x -= 1
			a -= 1
		while x > 0 and b < n - 1:
			x -= 1
			b += 1
		print(b - a)
get_array = lambda : list(map(int, sys.stdin.readline().split()))
get_ints = lambda : map(int, sys.stdin.readline().split())
input = lambda : sys.stdin.readline().strip()
main()


from sys import stdin
t = int(stdin.readline())
while t:
	(n, x, a, b) = map(int, stdin.readline().split())
	d = abs(a - b)
	if d != n - 1 and x != 0:
		(a, b) = (min(a, b), max(a, b))
		l = a - 1
		r = n - b
		d = d + min(x, l + r)
	print(d)
	t -= 1


for i in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	hod = min(x, n - b)
	b += hod
	x -= hod
	if x != 0:
		hod = min(x, a - 1)
		a -= hod
	print(b - a)


n = int(input())
for i in range(n):
	(a, b, c, d) = input().split()
	a = int(a)
	b = int(b)
	c = int(c)
	d = int(d)
	if abs(d - c) == a - 1:
		print(a - 1)
	elif b == 0:
		print(abs(d - c))
	elif abs(d - c) + b > a - 1:
		print(a - 1)
	else:
		print(abs(d - c) + b)


for _ in range(int(input())):
	(n, x, a, b) = [int(i) for i in input().split(' ')]
	(a, b) = (min(a, b), max(a, b))
	while x:
		if a > 1:
			a -= 1
		elif b < n:
			b += 1
		x -= 1
	print(b - a)


t = int(input())
for __ in range(t):
	(n, x, a, b) = map(int, input().split())
	cur_dis = abs(a - b)
	print(min(n - 1, cur_dis + x))


n = int(input())
for i in range(n):
	(a, b, c, d) = map(int, input().split())
	z = abs(c - d)
	if z + b >= a - 1:
		print(a - 1)
	else:
		print(z + b)


def solve(n, x, a, b):
	return min(n - 1, abs(a - b) + x)
t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	print(solve(n, x, a, b))


n = int(input())
ss = []
for i in range(n):
	ss += [list(map(int, input().split(' ')))]
for i in range(n):
	n = ss[i][0]
	x = ss[i][1]
	a = ss[i][2]
	b = ss[i][3]
	print(min(abs(a - b) + x, n - 1))


num = int(input())
while num > 0:
	arr = list(map(int, input().split()))
	if arr[1] == 0:
		print(abs(arr[2] - arr[3]))
		num -= 1
		continue
	elif arr[2] == 1 and arr[3] == arr[0]:
		print(abs(arr[2] - arr[3]))
		num -= 1
		continue
	else:
		x = arr[1]
		ele = [arr[2], arr[3]]
		ele.sort()
		if x > abs(ele[1] - arr[0]):
			x = x - abs(ele[1] - arr[0])
			ele[1] = arr[0]
			if x > abs(ele[0] - 1):
				ele[0] = 1
			else:
				ele[0] = ele[0] - x
		else:
			ele[1] = ele[1] + x
		print(abs(ele[0] - ele[1]))
	num -= 1


for i in range(int(input())):
	(n, x, a, b) = list(map(int, input().split(' ')))
	max_r = n - 1
	r_b = abs(a - b)
	act_r = r_b + x
	if act_r >= max_r:
		print(max_r)
	else:
		print(act_r)


t = int(input())
while t:
	(n, x, a, b) = map(int, input().split())
	max_dist = n - 1
	dist = abs(a - b)
	if dist == max_dist:
		print(max_dist)
	elif x == 0:
		print(dist)
	elif dist + x >= max_dist:
		print(max_dist)
	else:
		print(dist + x)
	t = t - 1


import sys
input = sys.stdin.readline
t = int(input())
for testcases in range(t):
	(n, x, a, b) = map(int, input().split())
	ANS = abs(b - a)
	print(min(n - 1, ANS + x))


for _ in range(int(input())):
	(n, x, a, b) = list(map(int, input().split()))
	lista = [a, b]
	lista.sort()
	soma = lista[1] - lista[0] + x
	if soma > n - 1:
		print(n - 1)
	else:
		print(soma)


t = int(input())
ans = []
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	md = a - 1 + n - b
	ans.append(min(md, x) + b - a)
[print(i) for i in ans]


x = int(input())
i = 0
l1 = []
while i < x:
	l = [int(e) for e in input().split(' ')]
	l1.append(min(l[0] - 1, abs(l[2] - l[3]) + l[1]))
	i += 1
for i in l1:
	print(i)


t = int(input())
for i in range(t):
	(n, x, a, b) = [int(s) for s in input().split()]
	(a, b) = (min(a, b), max(a, b))
	first = a - 1
	if first >= x:
		a -= x
		x = 0
	else:
		x -= first
		a = 1
	second = n - b
	if second >= x:
		b += x
		x = 0
	else:
		x -= second
		b = n
	print(b - a)


def maxRange(num_students, num_swaps, first_pos, second_pos):
	(first_pos, second_pos) = sorted([first_pos, second_pos])
	while num_swaps > 0 and first_pos > 1:
		first_pos -= 1
		num_swaps -= 1
	while num_swaps > 0 and second_pos < num_students:
		second_pos += 1
		num_swaps -= 1
	return second_pos - first_pos
num_tests = int(input())
for i in range(num_tests):
	(num_students, num_swaps, first_pos, second_pos) = [int(el) for el in input().split()]
	print(maxRange(num_students, num_swaps, first_pos, second_pos))


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split(' '))
	max = n - 1
	now = abs(b - a) + x
	if max >= now:
		print(now)
	else:
		print(max)


a = int(input())
ou = []
for i in range(a):
	(n, x, a, b) = list(map(int, input().split()))
	tw = min(a, b) + (n - max(a, b))
	te = abs(b - a)
	res = te + x
	if res < n:
		ou.append(res)
	else:
		ou.append(n - 1)
for j in ou:
	print(j)


for i in range(int(input())):
	(n, x, a, b) = [int(x) for x in input().split()]
	if a > b:
		(a, b) = (b, a)
	left_shift = min(x, a - 1)
	a -= left_shift
	x -= left_shift
	right_shift = min(x, n - b)
	b += right_shift
	x -= right_shift
	print(b - a)


t = int(input())
for i in range(t):
	(n, x, c, d) = map(int, input().split())
	(a, b) = (min(c, d), max(c, d))
	ma = abs(b - a)
	if ma + 1 == n:
		print(ma)
	else:
		ans = abs(b - a)
		l = a
		r = n - b
		if x > l - 1:
			ans += l - 1
			x -= l - 1
			if x > r:
				ans += r
			else:
				ans += x
		else:
			ans += x
		print(ans)


import math
for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	(mini, maxi) = (min(a, b), max(a, b))
	while x > 0 and mini > 1:
		mini -= 1
		x -= 1
	while x > 0 and maxi < n:
		maxi += 1
		x -= 1
	print(maxi - mini)


n = int(input())
for i in range(n):
	k = list(map(int, input().split()))
	if abs(k[3] - k[2]) + k[1] < k[0] - 1:
		print(abs(k[3] - k[2]) + k[1])
	else:
		print(k[0] - 1)


import math
t = int(input())
array = []
for i in range(t):
	x = list(map(int, input().split()))
	array.append(x)
for j in array:
	if math.fabs(j[2] - j[3]) + j[1] >= j[0]:
		print(int(j[0] - 1))
	else:
		print(int(math.fabs(j[2] - j[3]) + j[1]))


for i in range(int(input())):
	(n, b, c, d) = map(int, input().split())
	if abs(c - d) == n - 1:
		print(n - 1)
	else:
		if c < d:
			(c, d) = (d, c)
		j = 0
		w = c + b
		if w > n:
			j = w - n
			w = n
		k = d - j
		if k < 1:
			k = 1
		print(abs(w - k))


N = int(input())
for _ in range(N):
	(n, x, a, b) = map(int, input().split())
	k = min(a, b)
	k1 = max(a, b)
	if k - x <= 0:
		if k1 + (x - k + 1) >= n:
			k1 = n
			ans = abs(k1 - 1)
		else:
			k1 = k1 + (x - k + 1)
			ans = abs(k1 - 1)
	else:
		k = k - x
		ans = abs(k - k1)
	print(ans)


t = int(input())
for you in range(t):
	l = input().split()
	n = int(l[0])
	x = int(l[1])
	a = int(l[2])
	b = int(l[3])
	z = abs(a - b)
	print(min(z + x, n - 1))


t = int(input())
for i in range(t):
	nums = list(map(int, input().split()))
	n = nums[0]
	x = nums[1]
	a = min(nums[2], nums[3])
	b = max(nums[3], nums[2])
	if x == 0:
		print(abs(a - b))
		continue
	else:
		total = n - b + (a - 1)
		if total < x:
			print(b - a + total)
		else:
			print(b - a + x)
			continue


def solve(n, x, a, b):
	(a, b) = sorted([a, b])
	while a > 1 and x > 0:
		a -= 1
		x -= 1
	while b < n and x > 0:
		b += 1
		x -= 1
	print(b - a)
t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	solve(n, x, a, b)


from collections import defaultdict, deque, Counter
from sys import stdin, stdout
from heapq import heappush, heappop
import math
import io
import os
import math
import bisect

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def digits(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def ceil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def mapin():
	return map(int, input().split())
t = int(input())
for _ in range(t):
	(n, x, a, b) = mapin()
	if a - b > 0:
		(a, b) = (b, a)
	temp = min(a - 1, x)
	a = a - temp
	x = x - temp
	temp = min(n - b, x)
	b = b + temp
	print(abs(a - b))


t = int(input())

def f(n, x, a, b):
	if a > b:
		(a, b) = (b, a)
	while x > 0:
		if a == 1 and b == n:
			break
		elif a > 1:
			a = a - 1
			x = x - 1
		elif b < n:
			b = b + 1
			x = x - 1
	return b - a
for i in range(0, t):
	(n, x, a, b) = map(int, input().split())
	print(f(n, x, a, b))


magi = int(input())
for i in range(1, magi + 1, 1):
	(sex, bra, penty, fuck) = map(int, input().split())
	chuda = sex - 1
	khanki = abs(penty - fuck)
	print(min(chuda, khanki + bra))


import sys

def readline_single(dtype):
	return dtype(sys.stdin.readline().rstrip())

def readline_multiple(dtype):
	return map(dtype, sys.stdin.readline().split())

def readline_list(dtype):
	return list(map(dtype, sys.stdin.readline().split()))

def readline_string():
	return sys.stdin.readline()[:-1]

def print_list(lst):
	print(' '.join((str(item) for item in lst)))

def print_list_of_list(lists):
	for lst in lists:
		print_list(lst)

def main():
	T = readline_single(int)
	for _ in range(T):
		(n, x, a, b) = readline_multiple(int)
		ans = min(n - 1, abs(a - b) + x)
		print(ans)
main()


n = int(input())
e = []
for i in range(n):
	l = list(map(int, input().split()))
	r1 = l[1] + abs(l[2] - l[3])
	r2 = min(r1, l[0] - 1)
	e.append(r2)
for j in range(len(e)):
	print(e[j])


t = int(input())
while t > 0:
	t -= 1
	(n, x, a, b) = (int(x) for x in input().split())
	if x == 0:
		print(abs(a - b))
	elif abs(a - b) == n - 1:
		print(n - 1)
	else:
		print(min(abs(a - b) + x, n - 1))


test = int(input())
result = []
for x in range(test):
	(n, ran, a, b) = list(map(int, input().split()))
	S = abs(a - b) + ran
	if S >= n:
		result.append(n - 1)
	else:
		result.append(S)
for x in result:
	print(x)


t = int(input())
for i in range(t):
	(n, x, a, b) = input().strip().split(' ')
	(n, x, a, b) = [int(n), int(x), int(a), int(b)]
	f = max(a, b)
	ff = min(a, b)
	f1 = min(ff - 1, x)
	x = x - ff + 1
	f2 = 0
	if x > 0:
		f2 = min(n - f, x)
	f = f + f2
	ff = ff - f1
	ans = f - ff
	print(ans)


for t in range(int(input())):
	(n, x, a, b) = [int(y) for y in input().split()]
	m = n - 1
	current = abs(a - b)
	if m == current:
		print(m)
	elif current + x > m:
		print(m)
	else:
		print(current + x)


t = int(input())
while t > 0:
	(n, x, a, b) = map(int, input().split())
	if a > b:
		tmp = a
		a = b
		b = tmp
	tmp = min(x, a - 1)
	a -= tmp
	x -= tmp
	tmp = min(x, n - b)
	b += tmp
	print(b - a)
	t -= 1


def testCase():
	(n, x, a, b) = tuple(map(int, input().split()))
	return min(abs(b - a) + x, n - 1)
t = int(input())
ans = [str(testCase()) for x in range(t)]
print('\n'.join(ans))


import math
k = int(input())
ans = []
for i in range(k):
	l = input().split()
	n = int(l[0])
	x = int(l[1])
	a = int(l[2])
	b = int(l[3])
	if a == 1 and b == n:
		ans.append(b - a)
	elif x >= n:
		ans.append(n - 1)
	elif x + math.fabs(a - b) >= n:
		ans.append(n - 1)
	elif a > b:
		if b - x >= 0:
			ans.append(a - b + x)
		elif a + (x - b) >= n:
			ans.append(n - 1)
		elif a + math.fabs(x - b) >= n:
			ans.append(n - 1)
		else:
			ans.append(a + (x - b))
	elif a - x >= 0:
		ans.append(b - a + x)
	elif b + (x - a) >= n:
		ans.append(n - 1)
	elif b + math.fabs(x - a) >= n:
		ans.append(n - 1)
	else:
		ans.append(b + (x - a))
for i in ans:
	print(i)


t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	a1 = max(0, a - 1) + (n - b)
	d = abs(a - b) + min(x, a1)
	print(d)


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	member = [a, b]
	member.sort()
	need = member[0] - 1
	need += n - member[1]
	ret = min(x, need) + member[1] - member[0]
	print(ret)


t = int(input())
for _ in range(t):
	(n, x, a, b) = list(map(int, input().split(' ')))
	distance = abs(a - b)
	while distance < n - 1 and x > 0:
		distance += 1
		x -= 1
	print(distance)


queries = int(input())
for _ in range(queries):
	(length, swaps, f_p, s_p) = map(int, input().split())
	if length - 1 > abs(f_p - s_p) + swaps:
		print(abs(f_p - s_p) + swaps)
	else:
		print(length - 1)


import sys
input = sys.stdin.readline
q = int(input())
for _ in range(q):
	(n, x, a, b) = map(int, input().split())
	mins = min(a, b)
	maxs = max(a, b)
	print(min(maxs - mins + x, n - 1))


import math
for i in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	d = abs(a - b)
	temp = min(a, b)
	b = max(a, b)
	a = temp
	count = 0
	while x > 0 and d < n:
		if x >= n - b:
			d = d + n - b
			x = x - (n - b)
		else:
			d = d + x
			break
		if x >= a - 1:
			d = d + a - 1
			x = x - (a - 1)
			break
		else:
			d = d + x
			break
	print(d)


t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	ans = abs(a - b)
	while x and ans + 1 < n:
		ans += 1
		x -= 1
	print(ans)


from math import fabs
t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	print(int(min(n - 1, fabs(a - b) + x)))


t = int(input())
result = []
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	result.append(min(n - 1, abs(a - b) + x))
for x in result:
	print(x)


b = int(input())
for i in range(b):
	ar = list(map(int, input().split()))
	n = ar[0]
	l = ar[1]
	a = ar[2]
	b = ar[3]
	k = abs(a - b) + l
	if k >= n:
		print(n - 1)
	else:
		print(k)


for x in range(int(input())):
	(n, x, *a) = map(int, input().split())
	a.sort()
	a1 = a[0]
	b = a[1]
	a = a1
	d = a - 1
	t = n - b
	if x == 0:
		print(b - a)
	elif d != 0:
		if d >= x:
			print(b - (a - x))
		elif t > x - d:
			print(b + x - d - (a - d))
		else:
			print(b + t - (a - d))
	elif t > x:
		print(b + x - 1)
	else:
		print(b + t - 1)


t = int(input())
for i in range(t):
	inp = list(map(int, input().split(' ')))
	(n, x, a, b) = (inp[0], inp[1], inp[2], inp[3])
	dist = abs(a - b) + x
	if dist >= n - 1:
		print(n - 1)
	else:
		print(dist)


t = int(input())
while t:
	t -= 1
	s = input().split(' ')
	ans = min(int(s[0]) - 1, abs(int(s[3]) - int(s[2])) + int(s[1]))
	print(ans)


import math
from collections import *
import sys
from functools import reduce

def get_ints():
	return map(int, input().strip().split())

def get_list():
	return list(get_ints())

def get_string():
	return list(input().strip().split())

def printxsp(*args):
	return print(*args, end='')

def printsp(*args):
	return print(*args, end=' ')
UGLYMOD = int(1000000000.0) + 7
SEXYMOD = 998244353
MAXN = int(100000.0)
for _testcases_ in range(int(input())):
	(n, x, a, b) = get_ints()
	print(min(n - 1, abs(a - b) + x))


q = int(input())
for i in range(q):
	(row_size, max_swaps, a_position, b_position) = map(int, input().split())
	push_to_right = max(a_position, b_position)
	push_to_left = min(a_position, b_position)
	if row_size - push_to_right > max_swaps:
		a_position = push_to_right + max_swaps
		max_swaps = 0
	else:
		a_position = row_size
		max_swaps -= row_size - push_to_right
	if push_to_left > max_swaps:
		b_position = push_to_left - max_swaps
	else:
		b_position = 1
	print(a_position - b_position)


t = int(input())
while t != 0:
	(a, b, c, d) = map(int, input().split())
	print(min(a - 1, abs(c - d) + b))
	t -= 1


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	if x == 0:
		print(abs(a - b))
		continue
	n1 = max(a, b)
	n2 = min(a, b)
	dis = 0
	if n1 + x <= n:
		dis = abs(n1 + x - n2)
		print(dis)
	else:
		temp = n - n1
		n1 = n
		x = x - temp
		if n2 - x >= 1:
			dis = abs(n2 - x - n1)
			print(dis)
		else:
			print(abs(n1 - 1))


import math, itertools, fractions, heapq, collections, bisect, sys, queue, copy
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def LS():
	return sys.stdin.readline().split()

def S():
	return input()

def main():
	n = I()
	ans = []
	for _ in range(n):
		(k, x, a, b) = LI()
		_ans = abs(a - b)
		_ans = min(_ans + x, k - 1)
		ans.append(_ans)
	for x in ans:
		print(x)
main()


for j in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	(a, b) = (max(a, b), min(a, b))
	for i in range(x):
		if a - b < n - 1:
			if a < n:
				a += 1
			elif b > 1:
				b -= 1
	print(a - b)


t = int(input().strip())
for i in range(t):
	(n, x, a, b) = map(int, input().strip().split())
	a1 = min(a, b)
	b1 = max(a, b)
	d1 = a1 - 1
	d2 = n - b1
	d0 = b1 - a1
	if d1 + d2 <= x:
		print(n - 1)
	else:
		print(d0 + x)


def run_testcase():
	[n, x, a, b] = [int(x) for x in input().split()]
	return min(n - 1, abs(a - b) + x)
testcase_count = int(input())
for i in range(testcase_count):
	print(str(run_testcase()))


def main():
	for _ in range(int(input())):
		(n, x, a, b) = map(int, input().split())
		print(min(x + max(a, b) - min(a, b), n - 1))
main()


t = int(input())
for i in range(t):
	(n, x, a, b) = [int(j) for j in input().split()]
	p = max(a, b)
	q = min(a, b)
	print(min(n - 1, p - q + x))


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	mini = min(a, b)
	maxi = max(a, b)
	if mini > x:
		mini = mini - x
	else:
		x = x - (mini - 1)
		mini = 1
		if n - maxi > x:
			maxi = maxi + x
		else:
			maxi = n
	print(maxi - mini)


def rywale(lista, x, a, b):
	a2 = min(a, b) - 1
	b2 = max(a, b) - 1
	while a2 > lista[0] and x > 0:
		a2 -= 1
		x -= 1
	while b2 < lista[-1] and x > 0:
		b2 += 1
		x -= 1
	return b2 - a2

def k_rywale(i):
	wynik = []
	for x in range(i):
		lista_i = [int(x) for x in input().split()]
		lista = []
		for z in range(lista_i[0]):
			lista.append(z)
		wynik.append(rywale(lista, lista_i[1], lista_i[2], lista_i[3]))
	for i in wynik:
		print(i)
i = int(input())
k_rywale(i)


q = int(input())
for i in range(q):
	n = list(map(int, input().split(' ')))
	if n[2] > n[3]:
		ans = n[2] - n[3]
	else:
		ans = n[3] - n[2]
	ans = ans + n[1]
	if ans >= n[0]:
		ans = n[0] - 1
	print(ans)


t = int(input())
while t:
	(n, x, a, b) = map(int, input().split())
	maxx = max(a, b)
	minn = min(a, b)
	print(min(n - 1, maxx - minn + x))
	t = t - 1


def f(n, x, a, b):
	t = abs(a - b) + x
	if t < n - 1:
		return t
	else:
		return n - 1
t = int(input())
for s in range(t):
	(n, x, a, b) = map(int, input().split())
	print(f(n, x, a, b))


for t in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	d = abs(b - a)
	if n == 3:
		if d == 2:
			if x == 1:
				d = 1
			else:
				d = 2
		elif x == 0:
			d = 1
		else:
			d = 2
	else:
		max = n - 1
		d = min(max, d + x)
	print(d)


t = int(input())
mass = []
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	c = min(a, b)
	b = max(a, b)
	a = c
	while x != 0:
		if b < n:
			b += 1
			x -= 1
		elif a > 1:
			a -= 1
			x -= 1
		if b == n and a == 1:
			break
	mass.append(b - a)
for i in mass:
	print(i)


def program():
	(n, x, a, b) = input().split()
	n = int(n)
	x = int(x)
	a = int(a)
	b = int(b)
	if abs(a - b) + x < n - 1:
		print(abs(a - b) + x)
	else:
		print(n - 1)
t = int(input())
for x in range(t):
	program()


t = int(input())
for i in range(t):
	(n, x, a, b) = input().split()
	n = int(n)
	x = int(x)
	a = int(a)
	b = int(b)
	if a < b:
		p = a
		q = b
	else:
		p = b
		q = a
	end = 0
	distance = 0
	front = p - 1
	end = n - q
	if x < end + front:
		distance = x + q - p
	else:
		distance = front + end + q - p
	print(distance)


for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	print(min(a - 1, abs(c - d) + b))


import sys

def I():
	return sys.stdin.readline().rstrip()
for _ in range(int(I())):
	(n, x, a, b) = list(map(int, I().split()))
	a = abs(a - b)
	print(min(n - 1, a + x))


t = int(input())
for i in range(t):
	(students, swaps, a, b) = map(int, input().split())
	if b > a:
		(a, b) = (b, a)
	if a + swaps >= students:
		maxa = students
	else:
		maxa = a + swaps
	swaps -= maxa - a
	if b - swaps <= 1:
		minb = 1
	else:
		minb = b - swaps
	print(abs(maxa - minb))


t = int(input())
while t > 0:
	w = []
	w = input().split()
	c = abs(int(w[2]) - int(w[3])) + int(w[1])
	w[0] = int(w[0]) - 1
	if w[0] >= c:
		print(c)
	else:
		print(w[0])
	t -= 1


for _ in range(int(input())):
	(n, x, a1, b1) = map(int, input().split())
	a = max(a1, b1)
	b = min(a1, b1)
	temp = n - a
	if x > temp:
		a = n
		x = x - temp
		if b - x > 0:
			b = b - x
		else:
			b = 1
	else:
		a = a + x
	print(abs(a - b))


def max_distance(n, x, a, b):
	current_dist = abs(a - b)
	if current_dist + 1 >= n - x:
		return n - 1
	else:
		return current_dist + x
q = int(input())
for i in range(q):
	(n, x, a, b) = map(int, input().split())
	print(max_distance(n, x, a, b))


ts = int(input())
for i in range(ts):
	[n, x, a, b] = list(map(int, input().split(' ')))
	pot_dist_1 = abs(a - 1) if a < b else abs(b - 1)
	pot_dist_2 = abs(a - ts) if a > b else abs(b - ts)
	max_dist = n - 1
	print(abs(a - b) + x if abs(a - b) + x < max_dist else max_dist)


T = int(input())
for t in range(T):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	if a == 1 and b == n:
		print(b - a)
		continue
	if x > 0:
		if a > 1:
			if a > x:
				a -= x
				x = 0
			else:
				x -= a - 1
				a = 1
		if x == 0:
			print(b - a)
			continue
		if b <= n:
			if n - b > x:
				b += x
				x = 0
			else:
				b = n
	print(b - a)


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	k = abs(a - b)
	for j in range(x):
		if k == n - 1:
			break
		else:
			k += 1
	print(k)


query = int(input())
for _ in range(query):
	(n, m, p, s) = map(int, input().split())
	cur = abs(p - s)
	if cur + m > n - 1:
		print(n - 1)
	else:
		print(cur + m)


import sys
input = sys.stdin.readline

def main():
	N = int(input())
	for _ in range(N):
		(n, x, a, b) = map(int, input().split())
		(a, b) = (min(a, b), max(a, b))
		while x > 0:
			if a > 1:
				a -= 1
				x -= 1
			elif b < n:
				b += 1
				x -= 1
			else:
				break
		print(b - a)
main()


arr = [list(map(int, input().split(' '))) for i in range(int(input()))]
result = []
for para in arr:
	if para[2] > para[3]:
		max_item = 2
		min_item = 3
	else:
		max_item = 3
		min_item = 2
	while para[1] > 0:
		if para[max_item] < para[0]:
			para[max_item] += 1
			para[1] -= 1
		elif para[min_item] > 1:
			para[min_item] -= 1
			para[1] -= 1
		else:
			para[1] -= 1
	result.append(str(para[max_item] - para[min_item]))
print('\n'.join(result))


for _ in range(int(input())):
	l = list(map(int, input().split()))
	n = l[0]
	x = l[1]
	a = l[2]
	b = l[3]
	l1 = []
	for i in range(1, n + 1):
		l1.append(i)
	for i in range(x):
		x1 = l1.index(b)
		y = l1.index(a)
		if x1 < y:
			if x1 > 0:
				temp = l1[x1]
				l1[x1] = l1[x1 - 1]
				l1[x1 - 1] = temp
			elif y < n - 1:
				temp = l1[y]
				l1[y] = l1[y + 1]
				l1[y + 1] = temp
		elif x1 < n - 1:
			temp = l1[x1]
			l1[x1] = l1[x1 + 1]
			l1[x1 + 1] = temp
		elif y > 0:
			temp = l1[y]
			l1[y] = l1[y - 1]
			l1[y - 1] = temp
	print(abs(l1.index(b) + 1 - (l1.index(a) + 1)))


t = int(input())
for tt in range(t):
	(n, x, a, b) = map(int, input().split())
	aa = min(a, b)
	bb = max(a, b)
	temp = n - bb
	ans = bb - aa
	lol = min(temp, x)
	x -= lol
	ans += lol
	lol = min(aa - 1, x)
	x -= lol
	ans += lol
	print(ans)


def distance(n, x, a, b):
	op = abs(a - b) + x
	if op < n:
		return op
	return n - 1

def main():
	t = int(input())
	for _ in range(t):
		(n, x, a, b) = map(int, input().split())
		print(distance(n, x, a, b))
main()


a = int(input())
ans = []
for i in range(0, a):
	b = input()
	c = [int(x) for x in b.split()]
	if c[2] > c[3]:
		jarak = c[2] - c[3]
	else:
		jarak = c[3] - c[2]
	jarak = jarak + c[1]
	if jarak > c[0] - 1:
		jarak = c[0] - 1
	ans.append(jarak)
for j in range(0, a):
	print(ans[j])


t = int(input())
i = 0
while i < t:
	(n, x, a, b) = map(int, input().split())
	z = abs(a - b)
	ans = z + x
	if ans > n - 1:
		print(n - 1)
	else:
		print(ans)
	i = i + 1


import math
import bisect
from collections import defaultdict as df
from collections import deque
from sys import stdin, stdout
t = int(input())
for i in range(t):
	(n, x, a, b) = list(map(int, input().split()))
	p = [a, b]
	p.sort()
	(a, b) = p
	room = a - 1 + n - b
	mini = min(room, x)
	ans = b - a + mini
	print(ans)


from sys import stdin

def iin():
	return int(stdin.readline())

def lin():
	return list(map(int, stdin.readline().split()))

def main():
	t = iin()
	while t:
		t -= 1
		(n, x, a, b) = lin()
		ans = abs(a - b)
		if a > b:
			(a, b) = (b, a)
		ans += min(n - b + a - 1, x)
		print(ans)
main()


for _ in range(int(input())):
	(n, x, a, b) = [int(i) for i in input().split()]
	if a > b:
		(a, b) = (b, a)
	for i in range(x):
		if a != 1:
			a = a - 1
			continue
		if b != n:
			b = b + 1
		else:
			break
	print(b - a)


n = int(input())
for _ in range(n):
	(a, b, c, d) = list(map(int, input().split()))
	(c, d) = (min(c, d), max(c, d))
	for i in range(b):
		if c > 1:
			c -= 1
		elif d < a:
			d += 1
	print(d - c)


from sys import stdin
test = int(stdin.readline())
for i in range(test):
	(n, x, a, b) = map(int, stdin.readline().split(' '))
	mi = min(a, b) - 1
	ma = n - max(a, b)
	move = mi + ma
	move = min(move, x)
	res = abs(a - b) + move
	print(res)


t = int(input())
for ts in range(t):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	m = n - b + a - 1
	if x < m:
		print(b - a + x)
	else:
		print(b - a + m)


from math import *
from sys import stdin, stdout
import itertools, os, sys, threading
from collections import deque, Counter, OrderedDict, defaultdict
from heapq import *
from fractions import Fraction
mod = 100000009

def ii():
	return int(input())

def si():
	return str(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def fii():
	return int(stdin.readline())

def fsi():
	return str(stdin.readline())

def fmi():
	return map(int, stdin.readline().split())

def fli():
	return list(fmi())
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000

def bfs(d, v):
	q = []
	q.append(v)
	visited[v] = 1
	while len(q) != 0:
		x = q[0]
		q.pop(0)
		for i in d[x]:
			if visited[i] != 1:
				visited[i] = 1
				q.append(i)
		print(x)

def make_graph(e):
	d = {}
	for i in range(e):
		(x, y) = mi()
		if x not in d:
			d[x] = [y]
		else:
			d[x].append(y)
		if y not in d:
			d[y] = [x]
		else:
			d[y].append(x)
	return d

def gr2(n):
	d = defaultdict(list)
	for i in range(n):
		(x, y) = mi()
		d[x].append(y)
	return d

def connected_components(graph):
	seen = set()

	def dfs(v):
		vs = set([v])
		component = []
		while vs:
			v = vs.pop()
			seen.add(v)
			vs |= set(graph[v]) - seen
			component.append(v)
		return component
	ans = []
	for v in graph:
		if v not in seen:
			d = dfs(v)
			ans.append(d)
	return ans

def primeFactors(n):
	s = set()
	while n % 2 == 0:
		s.add(2)
		n = n // 2
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0:
			s.add(i)
			n = n // i
	if n > 2:
		s.add(n)
	return s

def find_all(a_str, sub):
	start = 0
	while True:
		start = a_str.find(sub, start)
		if start == -1:
			return
		yield start
		start += len(sub)

def SieveOfEratosthenes(n, isPrime):
	isPrime[0] = isPrime[1] = False
	for i in range(2, n):
		isPrime[i] = True
	p = 2
	while p * p <= n:
		if isPrime[p] == True:
			i = p * p
			while i <= n:
				isPrime[i] = False
				i += p
		p += 1
	return isPrime

def dijkstra(edges, f, t):
	g = defaultdict(list)
	for (l, r, c) in edges:
		g[l].append((c, r))
	(q, seen, mins) = ([(0, f, ())], set(), {f: 0})
	while q:
		(cost, v1, path) = heappop(q)
		if v1 not in seen:
			seen.add(v1)
			path = (v1, path)
			if v1 == t:
				return (cost, path)
			for (c, v2) in g.get(v1, ()):
				if v2 in seen:
					continue
				prev = mins.get(v2, None)
				next = cost + c
				if prev is None or next < prev:
					mins[v2] = next
					heappush(q, (next, v2, path))
	return float('inf')
t = ii()
for a0 in range(t):
	(n, x, a, b) = mi()
	ma = n - 1
	if n > 0:
		print(min(ma, abs(a - b) + x))
	else:
		print(abs(a - b))


t = int(input())
for i in range(t):
	(n, x, a, b) = input().split()
	n = int(n)
	x = int(x)
	a = int(a)
	b = int(b)
	for j in range(x):
		if abs(a - b) == n - 1:
			a = a
			b = b
		elif a > b:
			if n - b > n - a:
				p = a + 1
				a = p
		elif n - a > n - b:
			p = b + 1
			b = p
	print(abs(a - b))


result = []
number = int(input())
for a in range(number):
	(b, c, d, e) = map(int, input().split())
	if d > e:
		f = e
		e = d
		d = f
	while e - d < b and c > 0:
		if 1 < d:
			d -= 1
			c -= 1
		elif e < b:
			e += 1
			c -= 1
		else:
			break
	result.append(e - d)
for g in result:
	print(g)


c = []
for i in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	l = abs(a - b)
	for k in range(n + 1):
		if k == x or l == n - 1:
			break
		l += 1
	c.append(l)
for i in range(len(c)):
	print(c[i])


def examA():
	t = I()
	ans = []
	for _ in range(t):
		(n, x, a, b) = LI()
		cur = min(n - 1, abs(a - b) + x)
		ans.append(cur)
	for v in ans:
		print(v)
import sys
import copy
import bisect
import heapq
from collections import Counter, defaultdict, deque

def I():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LS():
	return sys.stdin.readline().split()

def S():
	return sys.stdin.readline().strip()
mod = 10 ** 9 + 7
inf = float('inf')
examA()


t = int(input())
while t:
	t -= 1
	(n, x, a, b) = list(map(int, input().split()))
	(a, b) = (min(a, b), max(a, b))
	temp = min(a - 1, x)
	a -= temp
	x -= temp
	b += min(n - b, x)
	print(b - a)


t = int(input())

def outer(n, x, a, b):
	if x < n - abs(b - a) - 1:
		return x + abs(b - a)
	return n - 1
for i in range(t):
	(n, x, a, b) = [int(x) for x in input().split()]
	print(outer(n, x, a, b))


import sys
import math
from statistics import mean
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
t = inp()
for i in range(t):
	(n, x, a, b) = invr()
	d = max(a, b) - min(a, b)
	if x == 0:
		print(d)
	elif d + 1 == n:
		print(d)
	else:
		d = d + x
		if d >= n:
			d = n - 1
		print(d)


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if x == 0:
		print(abs(a - b))
	else:
		(c, d) = (min(a, b), max(a, b))
		f = min(n, d + x)
		x -= f - d
		print(abs(max(1, c - x) - f))


times = int(input())
for i in range(times):
	number = input().split(' ')
	distancia = abs(int(number[2]) - int(number[3])) + int(number[1])
	if distancia >= int(number[0]):
		print(int(number[0]) - 1)
	else:
		print(distancia)


t = int(input())
while t:
	l = list(map(int, input().split()))
	(n, x) = (l[0], l[1])
	d = abs(l[2] - l[3])
	while x > 0 and d < n - 1:
		d = d + 1
		x = x - 1
	print(d)
	t = t - 1


t = int(input())
z = [1] * t
for q in range(0, t):
	(n, x, a, b) = list(map(int, input().split()))
	p = max(a, b)
	s = min(a, b)
	while p - s != n - 1 and x != 0:
		if p != n:
			p = p + 1
			x = x - 1
		else:
			s = s - 1
			x = x - 1
	z[q] = p - s
for q in range(0, t):
	print(z[q])


from sys import stdin
read = stdin.readline

def main():
	cases = int(read())
	for case in range(cases):
		(n, x, a, b) = map(int, read().strip().split())
		ans = abs(a - b)
		dist = min(abs(a - 1) + abs(n - b), abs(a - n) + abs(b - 1))
		if x >= dist:
			ans += dist
		else:
			ans += x
		print(ans)
main()


import sys
sys.setrecursionlimit(2000)
from collections import Counter
from functools import reduce
total_test_cases = int(sys.stdin.readline().rstrip())
for _ in range(total_test_cases):
	test_case = [int(val) for val in sys.stdin.readline().rstrip().split()]
	(n, x, a, b) = test_case
	max_distance = abs(a - b)
	high_student = max(a, b)
	low_student = min(a, b)
	ceiling = n
	floor = 1
	while x > 0:
		if high_student < ceiling:
			max_distance += 1
			ceiling -= 1
		elif low_student > floor:
			max_distance += 1
			floor += 1
		x -= 1
	print(max_distance)


number = int(input())
m = []
for i in range(number):
	s = list(map(int, input().split()))
	m.append(s)
for i in range(number):
	n = m[i][0]
	x = m[i][1]
	a = m[i][2]
	b = m[i][3]
	if a > b:
		(a, b) = (b, a)
	anwser = b - a + x
	if anwser <= n - 1:
		print(anwser)
	else:
		print(n - 1)


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	if abs(a - b) == n - 1:
		print(abs(a - b))
	else:
		mn = min(a, b)
		mx = max(a, b)
		if mx + x > n:
			x = x + mx - n
			mx = n
		else:
			mx += x
			x = 0
		if mn - x < 1:
			mn = 1
		else:
			mn -= x
		print(abs(mn - mx))


t = int(input())
while t:
	(n, x, a, b) = map(int, input().split())
	if x == 0:
		print(abs(a - b))
	elif a == 1 and b == n or (a == n and b == 1):
		print(abs(a - b))
	else:
		m1 = max(a, b)
		m2 = min(a, b)
		if m1 + x <= n:
			print(m1 + x - m2)
		elif m2 - x >= 1:
			print(m1 - (m2 - x))
		elif m1 + x > n or m2 - x < 1:
			z = n - m1
			z1 = x - z
			if z1 >= m2:
				print(m1 + z - 1)
			else:
				print(m1 + z - (m2 - z1))
	t = t - 1


tc = int(input())
for _ in range(tc):
	(n, x, a, b) = map(int, input().split())
	maxdis = min(n - 1, abs(b - a) + x)
	print(maxdis)


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	r = abs(a - b)
	posM = max(a, b) + x
	posm = min(a, b)
	if posM > n:
		x = posM - n
		posM = posM - x
		posm = posm - x
	if posm < 1:
		print(n - 1)
	else:
		print(posM - posm)


T = int(input())

def mod(x):
	if x > 0:
		return x
	else:
		return -x
for i in range(T):
	(n, x, a, b) = map(int, input().split())
	if n - mod(a - b) > x:
		print(mod(a - b) + x)
	else:
		print(n - 1)


def min(a, b):
	if a < b:
		return a
	else:
		return b

def module(a):
	if a < 0:
		return -a
	else:
		return a
t = int(input())
for i in range(t):
	(n, x, a, b) = input().split(' ')
	n = int(n)
	x = int(x)
	a = int(a)
	b = int(b)
	print(min(n - 1, module(a - b) + x))


t = int(input())
s = 0
for i in range(t):
	(n, x, a, b) = [int(i) for i in input().split()]
	if abs(a - b) + x > n - 1:
		d = n - 1
	else:
		d = abs(a - b) + x
	print(d)


for i in range(int(input())):
	d = [int(i) for i in input().split()]
	print(min(d[0] - 1, abs(d[2] - d[3]) + d[1]))


t = int(input())
lst = []
for i in range(t):
	lst.append(list(map(int, input().split())))
for i in lst:
	(n, x, p, s) = (i[0], i[1], i[2], i[3])
	dist = abs(p - s)
	yo = n - dist
	if x > yo:
		print(n - 1)
	elif x == yo:
		print(n - 1)
	else:
		print(dist + x)


test_cases = int(input())
for i in range(test_cases):
	(n, x, a, b) = map(int, input().split(' '))
	if max(a, b) - min(a, b) + x >= n - 1:
		print(n - 1)
	else:
		print(max(a, b) - min(a, b) + x)


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	q = max(a, b)
	w = min(a, b)
	if q + x <= n:
		q += x
		x = 0
	else:
		x = x - (n - q)
		q = n
	if w - x >= 1:
		w -= x
	else:
		x = x - (1 + w)
		w = 1
	print(q - w)


for i in range(int(input())):
	(n, s, a, b) = map(int, input().split())
	print(min(n - 1, abs(a - b) + s))


for _ in range(int(input())):
	(n, x, a, b) = (int(x) for x in input().split())
	aa = max(a, b)
	bb = min(a, b)
	d = aa - bb
	d1 = n - aa
	if d1 > x:
		print(d + x)
	else:
		x -= d1
		print(min(n - 1, x + d1 + d))


import sys
raw_data = sys.stdin.readlines()
raw_data.pop(0)

def swap(data):
	test_case = data.split()
	changes_available = int(test_case[1])
	seats = int(test_case[0])
	a = int(test_case[2])
	b = int(test_case[3])
	while changes_available > 0 and seats - 1 != abs(b - a):
		if b > a:
			if b < seats:
				b += 1
				changes_available -= 1
			elif a > 1:
				a -= 1
				changes_available -= 1
		elif a > b:
			if a < seats:
				a += 1
				changes_available -= 1
			elif b > 1:
				b -= 1
				changes_available -= 1
	raw_data
	print(abs(b - a))
for i in raw_data:
	swap(i)


t = int(input())
rA = []
for z in range(t):
	(n, x, a, b) = [int(i) for i in input().split()]
	rA.append(min(n - 1, abs(a - b) + x))
for i in rA:
	print(i)


t = int(input())
while t:
	t = t - 1
	(n, x, a1, b1) = [int(x) for x in input().split(' ')]
	a = min(a1, b1)
	b = max(a1, b1)
	ind_a = a
	ind_b = b
	while x:
		if a < 2:
			break
		a = a - 1
		x = x - 1
		ind_a = a
	while x:
		if b >= n:
			break
		x = x - 1
		b = b + 1
		ind_b = b
	a = min(ind_a, ind_b)
	b = max(ind_a, ind_b)
	print(b - a)


nbt = int(input())
for _ in range(nbt):
	(n, x, a, b) = [int(x) for x in input().split()]
	print(min(n - 1, abs(a - b) + x))


for m in range(int(input())):
	nxab = list(map(int, input().split()))
	while nxab[1] != 0 and (min(nxab[2], nxab[3]) != 1 or max(nxab[2], nxab[3]) != nxab[0]):
		if nxab[2] < nxab[3] and nxab[2] > 1:
			nxab[2] -= 1
		elif nxab[3] < nxab[2] and nxab[3] > 1:
			nxab[3] -= 1
		elif nxab[2] < nxab[3] and nxab[2] == 1 and (nxab[3] != nxab[0]):
			nxab[3] += 1
		elif nxab[3] < nxab[2] and nxab[3] == 1 and (nxab[2] != nxab[0]):
			nxab[2] += 1
		nxab[1] -= 1
	distance = abs(nxab[2] - nxab[3])
	print(distance)


for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	r = a - 1
	if c > d:
		p = c - d
	else:
		p = d - c
	print(min(r, p + b))


from sys import stdin, stdout
t = int(stdin.readline())
for a in range(t):
	(n, x, a, b) = map(int, stdin.readline().split())
	if a > b:
		(a, b) = (b, a)
	rem = a - 1 + n - b
	if rem >= x:
		print(b - a + x)
	else:
		print(b - a + rem)


q = int(input())
for i in range(q):
	(w, e, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	b += e
	if b > w:
		mn = b - w
		b -= mn
		e = mn
	else:
		e = 0
	a -= e
	if a < 1:
		a = 1
	print(abs(b - a))


T = int(input())
while T:
	T -= 1
	ls = list(map(int, input().split()))
	(n, x, a, b) = (ls[0], ls[1], ls[2], ls[3])
	if a > b:
		temp = b
		b = a
		a = temp
	m = n - b + (a - 1)
	dist = b - a
	if m > x:
		dist += x
	else:
		dist = n - 1
	print(dist)


n = int(input())
for i in range(n):
	(n, x, a, b) = map(int, input().split())
	y = [a, b]
	y = sorted(y)
	p = abs(y[1] - y[0])
	for i in range(y[0], 1, -1):
		if x >= 1:
			x -= 1
			p += 1
	if x >= 1:
		for i in range(y[1], n):
			if x >= 1:
				x -= 1
				p += 1
	print(p)


n = int(input())
i = 0
while i < n:
	li = list(map(int, input().split(' ')))
	print(min(li[0] - 1, abs(li[2] - li[3]) + li[1]))
	i = i + 1


from collections import defaultdict, deque
from itertools import permutations
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
from copy import deepcopy
int_input = lambda : int(stdin.readline())
string_input = lambda : stdin.readline()
multi_int_input = lambda : map(int, stdin.readline().split())
multi_input = lambda : stdin.readline().split()
list_input = lambda : list(map(int, stdin.readline().split()))
string_list_input = lambda : list(string_input())
MOD = pow(10, 9) + 7
test = int_input()
for _ in range(test):
	(n, x, a, b) = multi_int_input()
	if b < a:
		(a, b) = (b, a)
	if x == 0:
		print(b - a)
	else:
		while x > 0 and b < n:
			b = b + 1
			x = x - 1
		while x > 0 and a > 1:
			a = a - 1
			x = x - 1
		print(b - a)


I = lambda : int(input())
ID = lambda : map(int, input().split())
IL = lambda : list(ID())
for _ in range(I()):
	(n, x, a, b) = ID()
	k = abs(a - b) + x
	min1 = min(n - 1, k)
	print(min1)


t = int(input())
ans = []
for number in range(t):
	(n, x, a, b) = map(int, input().split())
	if n - 1 == abs(a - b):
		ans.append(n - 1)
		continue
	if x == 0:
		ans.append(abs(a - b))
		continue
	max1 = max(a, b)
	min1 = min(a, b)
	if min1 - x <= 0:
		x = x - (min1 - 1)
		min1 = 1
		if max1 + x >= n:
			max1 = n
		else:
			max1 = max1 + x
		ans.append(abs(max1 - min1))
	else:
		min1 = min1 - x
		x = 0
		ans.append(abs(max1 - min1))
for item in ans:
	print(item)


for k in range(int(input())):
	(n, k, a, b) = map(int, input().split())
	l = k + abs(a - b)
	if l > n - 1:
		print(n - 1)
	else:
		print(l)


import sys
t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, sys.stdin.readline().split())
	if a > b:
		move_a = min(n, a + x)
		remainder = max(0, a + x - n)
		move_b = max(1, b - remainder)
	else:
		move_b = min(n, b + x)
		remainder = max(0, b + x - n)
		move_a = max(1, a - remainder)
	print(abs(move_a - move_b))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	print(min(n - 1, abs(a - b) + x))


t = int(input())
for i in range(t):
	b = input().split()
	n = int(b[0])
	x = int(b[1])
	a = int(b[2])
	b = int(b[3])
	print(min(n - 1, max(a, b) - min(a, b) + x))


times = int(input())
for r in range(0, times):
	text = input()
	t = text.split(' ')
	n = int(t[0])
	x1 = int(t[1])
	a = int(t[2])
	b = int(t[3])
	x = x1
	for q in range(0, x1):
		if a > 1 and x > 0 and (a < n):
			x -= 1
			if a > b:
				a += 1
			else:
				a -= 1
	for w in range(0, x1):
		if b < n and x > 0 and (b > 1):
			x -= 1
			if a > b:
				b -= 1
			else:
				b += 1
	print(abs(b - a))


import math
t = int(input())
for si in range(t):
	(n, x, a, b) = map(int, input().split())
	a = a - 1
	b = b - 1
	n = n - 1
	if a > b:
		buf = a
		a = b
		b = buf
	delta = b - a
	d = n - b
	ans = min(d + a, x) + delta
	print(ans)


def call():
	(n, x, a, b) = map(int, input().split())
	t = abs(a - b) + x
	print(min(n - 1, t))
for _ in range(int(input())):
	call()


n = int(input())
for i in range(n):
	var = [int(x) for x in input().split(' ')]
	number = var[0]
	swap = var[1]
	pos1 = var[2]
	pos2 = var[3]
	current_dist = abs(var[3] - var[2])
	swaps_available = number - current_dist - 1
	if swaps_available >= swap:
		print(str(current_dist + swap))
	else:
		print(str(current_dist + swaps_available))


T = int(input())
for _ in range(T):
	(N, X, A, B) = map(int, input().split())
	print(min(abs(B - A) + X, N - 1))


t = int(input())
for _ in range(t):
	(n, p, a, b) = map(int, input().strip().split())
	x = min(a, b)
	y = max(a, b)
	ans = y - x
	if x != 1:
		if p > x - 1:
			ans += x - 1
			p -= x - 1
		else:
			ans += p
			p = 0
	if y != n:
		if p > n - y:
			ans += n - y
			p -= n - y
		else:
			ans += p
			p = 0
	print(ans)


def solve(n, x, a, b):
	t = sorted([a, b])
	return t[1] - t[0] + min(x, n - t[1] + t[0] - 1)
r = []
for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	r.append(solve(n, x, a, b))
print(*r, sep='\n')


def main():
	import sys
	input = sys.stdin.readline
	t = int(input())
	for _ in range(t):
		(n, x, a, b) = map(int, input().split())
		if a > b:
			(a, b) = (b, a)
		print(min(n - 1, b - a + x))
main()


q = int(input())
for i in range(q):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	y = a - 1 + (n - b)
	if x < y:
		s = b - a + x
	else:
		s = b - a + y
	print(s)


t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	for _ in range(x):
		if a >= 2:
			a -= 1
		elif b <= n - 1:
			b += 1
	print(b - a)


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	mn = min(a, b)
	mx = max(a, b)
	ans = mx - mn + x
	if mn == 1 and mx == n:
		print(n - 1)
	elif ans < n:
		print(ans)
	elif ans >= n:
		print(n - 1)


def vrazd(n, x, a, b):
	while x > 0:
		if a < b:
			if a > 1:
				a -= 1
			elif b < n:
				b += 1
		elif b > 1:
			b -= 1
		elif a < n:
			a += 1
		x -= 1
	return abs(b - a)
R = []
t = int(input())
for i in range(t):
	A = list(input().split())
	A = [int(item) for item in A]
	n = A[0]
	x = A[1]
	a = A[2]
	b = A[3]
	R.append(vrazd(n, x, a, b))
for k in R:
	print(k)


def solve():
	t = int(input())
	for i in range(t):
		(n, x, a, b) = [int(s) for s in input().split()]
		(a, b) = sorted([a, b])
		while b < n and x > 0:
			b += 1
			x -= 1
		while a > 1 and x > 0:
			a -= 1
			x -= 1
		print(abs(a - b))
solve()


for i in range(int(input())):
	(n, x, a, b) = list(map(int, input().split()))
	ans = abs(a - b)
	if x <= n - 1 + min(a, b) - max(a, b):
		ans += x
	else:
		ans += n - 1 + min(a, b) - max(a, b)
	print(ans)


t = int(input())
while t > 0:
	l = list(map(int, input().split()))
	c = abs(l[2] - l[3])
	print(min(l[0] - 1, max(c, c + l[1])))
	t -= 1


for i in range(int(input())):
	first = list(map(int, input().split()))
	print(min(first[0] - 1, abs(first[2] - first[3]) + first[1]))


cnt = int(input())
for i in range(cnt):
	(n, x, a, b) = tuple(map(int, input().split()))
	print(min(n - 1, abs(a - b) + x))


test_cases_input = input()
test_cases = int(test_cases_input)
test_data_list = [input() for x in range(test_cases)]
for test_data in test_data_list:
	(number_of_students, number_of_swaps, student_a, student_b) = [int(x) for x in test_data.split()]
	current_distance = abs(student_a - student_b)
	max_distance = number_of_students - 1
	print(current_distance + number_of_swaps if current_distance + number_of_swaps <= max_distance else max_distance)


k = int(input())
result = []
for o in range(k):
	(n, x, a, b) = [int(a) for a in input().split()]
	(a, b) = (min(a, b), max(a, b))
	r = b - a + min(a - 1 + n - b, x)
	result.append(r)
print('\n'.join([str(a) for a in result]))


import math
in1 = int(input())
for i in range(in1):
	in2 = input().split()
	in2 = list(map(int, in2))
	og = int(math.fabs(in2[2] - in2[3]))
	if og == in2[0] - 1:
		print(og)
	else:
		dis = int(max(in2[2], in2[3]) - min(in2[2], in2[3]) + in2[1])
		if dis <= in2[0] - 1:
			print(dis)
		else:
			print(in2[0] - 1)


num = int(input())
res = []
for i in range(num):
	vals = input().split(' ')
	n = int(vals[0])
	x = int(vals[1])
	if int(vals[2]) < int(vals[3]):
		a = int(vals[2])
		b = int(vals[3])
	else:
		b = int(vals[2])
		a = int(vals[3])
	for i in range(x):
		if b == n:
			if a == 1:
				break
			else:
				a -= 1
		elif a == 1:
			b += 1
			if b == n:
				break
		else:
			b += 1
	res.append(b - a)
for i in range(len(res)):
	print(res[i])


Q = int(input())
for i in range(Q):
	x = [int(a) for a in input().split()]
	mini = 1
	maxi = x[0]
	maxdis = maxi - mini
	if abs(x[2] - x[3]) == maxdis:
		print(maxdis)
	elif abs(x[2] - x[3]) + x[1] <= maxdis:
		print(abs(x[2] - x[3]) + x[1])
	else:
		print(maxdis)


for i in range(int(input())):
	(n, s, f, l) = list(map(int, input().strip().split()))
	t = n - 1
	if s == 0:
		print(abs(l - f))
	elif abs(l - f) == t:
		print(abs(l - f))
	else:
		for j in range(1, s + 1):
			if abs(l - f) + j == t:
				print(abs(l - f) + j)
				break
		else:
			print(abs(l - f) + s)


for i in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	d = abs(a - b)
	x1 = min(a, b) - 1
	y1 = n - max(a, b)
	d1 = min(x, x1 + y1)
	print(d + d1)


t = int(input())
while t > 0:
	t = t - 1
	(n, x, a, b) = map(int, input().split())
	small = min(a, b)
	big = max(a, b)
	if big - small < n:
		if n - big <= x:
			x = x - (n - big)
			big = n
			if small <= x:
				small = 1
			else:
				small = small - x
		else:
			big = big + x
	print(big - small)


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	if n == 2:
		print(1)
	elif b == a + 1 and x == 0:
		print(1)
	else:
		if a - 1 < x and n - b < x:
			if a - 1 + n - b < x:
				ans = abs(n - 1)
			else:
				ans = b + x - a
		elif a - 1 < x and n - b >= x:
			ans = b - a + x
		else:
			ans = b - a + x
		print(ans)


from sys import stdin, stdout
for i in stdin:
	t = int(i)
	break
for i in range(t):
	for i in stdin:
		(n, x, a, b) = map(int, i.strip().split())
		break
	if a > b:
		(a, b) = (b, a)
	while x:
		if a > 1:
			a -= 1
			x -= 1
		elif b < n:
			b += 1
			x -= 1
		else:
			x = 0
	stdout.write(str(b - a) + '\n')


from sys import stdin, stdout
test_cases = int(stdin.readline())
for i in range(test_cases):
	line = list(map(int, stdin.readline().split(' ')))
	total = line[0]
	swaps = line[1]
	pos1 = line[2]
	pos2 = line[3]
	res = min(abs(pos1 - pos2) + swaps, total - 1)
	stdout.write(str(res) + '\n')


def solve():
	(n, x, a, b) = [int(c) for c in input().split()]
	print(min(n - 1, abs(a - b) + x))
t = int(input())
for _ in range(t):
	solve()


n = int(input())
for i in range(n):
	(a, b, c, d) = list(map(int, input().split()))
	if c > d:
		larger = c
		smaller = d
		x = c - d
	else:
		larger = d
		smaller = c
		x = d - c
	print(min(a - 1, x + b))


from sys import stdin

def rl():
	return [int(w) for w in stdin.readline().split()]
(t,) = rl()
for _ in range(t):
	(n, x, a, b) = rl()
	print(min(n - 1, abs(a - b) + x))


def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
mod = 1000000007
t = ii()
while t:
	t -= 1
	(n, x, a, b) = mi()
	k = 0
	c = abs(b - a)
	if a > b:
		k += b - 1
		k += n - a
	else:
		k += a - 1
		k += n - b
	if k > x:
		c += x
	else:
		c += k
	print(c)


q = int(input())
for w in range(q):
	(n, x, a, b) = (int(i) for i in input().split())
	d = abs(b - a)
	print(min(d + x, n - 1))


import math
t = int(input())
l = []
for i in range(t):
	(n, x, a, b) = [int(x) for x in input().split()]
	idf = int(math.fabs(a - b))
	if idf == n - 1:
		l.append(int(math.fabs(a - b)))
	elif x >= n - idf:
		l.append(n - 1)
	else:
		l.append(idf + x)
for i in l:
	print(i)


def count_distance(inputs):
	(n, x, a, b) = inputs
	if not x:
		return abs(a - b)
	x = min([n - 1 - abs(a - b), x])
	return x + abs(a - b)
N = int(input())
for _ in range(N):
	print(count_distance(list(map(lambda x: int(x), input().split()))))


t = int(input())
for fwe in range(t):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	ans = a - 1 + (n - b)
	print(b - a + min(ans, x))


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	temp = 0
	if a > b:
		temp = a
		a = b
		b = temp
	k = b - a
	if k == n - 1:
		print(k)
	else:
		while x != 0 and k != n - 1:
			k += 1
			x -= 1
		print(k)


for TT in range(1, int(input()) + 1):
	(n, x, a, b) = map(int, input().split())
	(a, b) = sorted([a, b])
	while x and b < n:
		b += 1
		x -= 1
	while x and a > 1:
		a -= 1
		x -= 1
	print(b - a)


import math
t = int(input())
for a0 in range(t):
	l = [int(x) for x in input().split()]
	(n, x, a, b) = l
	if x + abs(a - b) < n:
		print(x + abs(a - b))
	elif x + abs(a - b) >= n:
		print(n - 1)


for i in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	s = 0
	s = s + min(a, b) - 1
	s = s + n - max(a, b)
	ans = abs(b - a)
	ans = ans + min(s, x)
	print(ans)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def main():
	t = inp()
	for i in range(t):
		(n, x, a, b) = inlt()
		print(min(n - 1, abs(a - b) + x))
main()


k = int(input())
for _ in range(k):
	(n, x, a, b) = map(int, input().split())
	i = min(a, b)
	j = max(a, b)
	while x > 0:
		c = x
		if i > 1:
			i -= 1
			x -= 1
		if j < n and x > 0:
			j += 1
			x -= 1
		if c == x:
			x = 0
	print(abs(i - j))


n = int(input())
for _ in range(n):
	a = list(map(int, input().split(' ')))
	print(min(a[0] - 1, abs(a[2] - a[3]) + a[1]))


for i in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if abs(a - b) == n - 1:
		print(abs(a - b))
	else:
		aa = min(a, b)
		bb = max(a, b)
		if bb + x > n:
			x = x - (n - bb)
			bb = n
		else:
			bb += x
			x = 0
		if aa - x < 1:
			aa = 1
		else:
			aa -= x
		print(abs(aa - bb))


def solve(n, x, a, b):
	p = min(a, b) - 1 + (n - max(a, b))
	if p >= x:
		return abs(a - b) + x
	else:
		return n - 1

def main():
	t = int(input())
	for i in range(t):
		(n, x, a, b) = map(int, input().split())
		print(solve(n, x, a, b))
main()


for _ in range(int(input())):
	(n, x, b, a) = map(int, input().split())
	(b, a) = (max(a, b), min(a, b))
	while (a != 1 or b != n) and x != 0:
		if a != 1:
			a -= 1
			x -= 1
		if b != n and x != 0:
			b += 1
			x -= 1
	print(abs(a - b))


t = int(input())
for _ in range(t):
	(n, x, a1, b1) = map(int, input().split())
	b = max(a1, b1)
	a = min(a1, b1)
	cd = b - a
	lsp = a - 1
	rsp = n - b
	if lsp + rsp <= x:
		print(n - 1)
	elif lsp + rsp > x:
		print(cd + x)


t = int(input())
for _ in range(t):
	nsab = list(map(int, input().rstrip().split()))
	n = nsab[0]
	s = nsab[1]
	a = nsab[2]
	b = nsab[3]
	diff = abs(a - b)
	if diff == n - 1:
		print(diff)
	else:
		while s:
			diff += 1
			s -= 1
			if diff == n - 1:
				break
		print(diff)


n = int(input())
for i in range(n):
	(l, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	ans = min(l - 1, b - a + x)
	print(ans)


query = int(input())
for _ in range(query):
	(n, x, a, b) = map(int, input().split())
	m0 = min(a, b)
	m1 = max(a, b)
	d = m1 - m0
	s = n - m1 + (m0 - 1)
	if x - s <= 0:
		print(x + d)
	else:
		print(s + d)


for t in range(int(input())):
	(n, x, a, b) = [int(j) for j in input().split()]
	s = min(a, b)
	t = max(a, b)
	for i in range(x):
		if s == 1 and t == n:
			break
		elif s == 1 and t != n:
			t += 1
		elif s != 1 and t == n:
			s -= 1
		else:
			s -= 1
	print(abs(s - t))


for _ in range(int(input())):
	arr = [int(j) for j in input().split()]
	swaps = arr[1]
	position = abs(arr[2] - arr[3])
	while arr[0] - 1 > position and swaps != 0:
		position += 1
		swaps -= 1
	print(position)


test = int(input())
for case in range(test):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	if a - 1 <= x:
		x -= a - 1
		a = 1
	else:
		a -= x
		x = 0
	if n - b <= x:
		b = n
	else:
		b += x
	print(b - a)


t = int(input())
for i in range(t):
	temp = front = rear = restx = p_shift = result = pos_shift = 0
	(n, x, a, b) = map(int, input().split())
	if a > b:
		temp = a
		a = b
		b = temp
	front = a
	rear = b
	pos_shift = n - b
	if pos_shift > x:
		x = x
	elif pos_shift == x:
		x = x
	elif pos_shift < x:
		restx = x - pos_shift
		x = pos_shift
	rear = b + x
	if restx > 0:
		p_shift = a - 1
		if p_shift > restx:
			restx = restx
		elif p_shift == restx:
			restx == restx
		elif p_shift < restx:
			restx = p_shift
		front = a - restx
	result = rear - front
	print(result)


from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
n = int(input())
for i in range(n):
	(n, x, a, b) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	if n - b + a - 1 <= x:
		print(n - 1)
	else:
		print(b + x - a)


nr = int(input())
for x in range(nr):
	(n, x, a, b) = input().split(' ')
	n = int(n)
	x = int(x)
	a = int(a)
	b = int(b)
	if a > b:
		aux = a
		a = b
		b = aux
	dif = b - a
	spatius = a - 1
	spatiud = n - b
	marire = spatiud + spatius
	if marire - x >= 0:
		print(dif + x, end='\n')
	else:
		print(dif + marire, end='\n')


for _ in range(int(input())):
	(n, x, p1, p2) = map(int, input().split())
	(a, b) = (min(p1, p2), max(p1, p2))
	if a == 1 and b == n:
		print(n - 1)
	else:
		while x:
			if a > 1:
				a -= 1
			elif b < n:
				b += 1
			x -= 1
		print(b - a)


for i in range(int(input())):
	lst = list(map(int, input().split()))
	if lst[2] >= lst[3]:
		lst[2] += lst[1]
		if lst[2] > lst[0]:
			(lst[2], lst[0]) = (lst[0], lst[2] - lst[0])
			lst[3] -= lst[0]
			if lst[3] < 1:
				lst[3] = 1
		print(lst[2] - lst[3])
	else:
		lst[3] += lst[1]
		if lst[3] > lst[0]:
			(lst[3], lst[0]) = (lst[0], lst[3] - lst[0])
			lst[2] -= lst[0]
			if lst[2] < 1:
				lst[2] = 1
		print(lst[3] - lst[2])


n = int(input())
for nn in range(n):
	(num_of_pupils, num_of_allowed_changes, pos1, pos2) = map(int, input().split())
	current_distance = abs(pos1 - pos2)
	if current_distance < num_of_pupils - 1:
		if current_distance + num_of_allowed_changes > num_of_pupils - 1:
			answer = num_of_pupils - 1
		else:
			answer = current_distance + num_of_allowed_changes
	else:
		answer = current_distance
	print(answer)


import sys
ip = lambda : sys.stdin.readline().rstrip()
from collections import *
for _ in range(int(ip())):
	(n, x, a, b) = map(int, ip().split())
	(a, b) = sorted([a, b])
	if x >= a - 1:
		x -= a - 1
		a = 1
	else:
		a -= x
		x = 0
	if x >= n - b:
		x -= n - b
		b = n
	else:
		b += x
		x = 0
	print(b - a)


q = int(input())
inp = []
for i in range(q):
	inp.append(input().split(' '))
for inpp in inp:
	n = int(inpp[0])
	x = int(inpp[1])
	a = int(inpp[2])
	b = int(inpp[3])
	cvp = abs(a - b)
	cvp += x
	if cvp >= n - 1:
		cvp = n - 1
	print(cvp)


def rival(n, x, a, b):
	d0 = abs(a - b)
	return min(d0 + x, n - 1)
t = int(input())
for i in range(t):
	(n, x, a, b) = list(map(int, input().split()))
	print(rival(n, x, a, b))


t = int(input())
for i in range(t):
	(n, x, a, b) = list(map(int, input().split()))
	if a > b:
		(a, b) = (b, a)
	if a == 1 and b == n:
		print(b - a)
	else:
		if x != 0:
			for k in range(x):
				if b != n:
					b += 1
				elif a != 1:
					a -= 1
		print(b - a)


import itertools

def main():
	n = int(input())
	for i in range(n):
		(n, x, a, b) = [int(v) for v in input().split()]
		print(min(abs(a - b) + x, n - 1))
main()


for j in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	b = b + x
	diff = 0
	if b > n:
		diff = b - n
		b = n
	a = a - diff
	if a < 1:
		a = 1
	print(b - a)


for i in range(int(input())):
	(s, o, n, a) = list(map(int, input().split()))
	l = abs(n - a)
	if l + o >= s:
		print(s - 1)
	else:
		print(l + o)


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	diff = abs(a - b) + x
	if diff > n:
		print(n - 1)
	elif diff == n:
		print(diff - 1)
	else:
		print(diff)


from collections import defaultdict as dfd
import math
import sys
import heapq

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def main():
	for _ in range(int(input())):
		(n, x, a, b) = MI()
		print(min(n - 1, abs(a - b) + x))
main()


t = int(input())
for i in range(t):
	(n, x, a, b) = [int(i) for i in input().split()]
	distance = abs(a - b)
	if x + distance < n:
		distance = x + distance
	else:
		distance = n - 1
	print(distance)


z = []
for i in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if max(a, b) + x - min(a, b) < n:
		z.append(abs(max(a, b) + x - min(a, b)))
	else:
		z.append(n - 1)
for i in z:
	print(i)


inp = input
li = list
iinp = lambda : int(inp())
ent = lambda : map(int, inp().split())
lient = lambda : [int(i) for i in inp().split()]
li0 = lambda x: [0 for i in range(x)]
stent = lambda : [i for i in inp()]
ab = abs
mi = min
ma = max
t = iinp()
for _ in range(t):
	(n, x, a, b) = ent()
	print(mi(n - 1, ab(a - b) + x))


def main():
	print = out.append
	(n, x, a, b) = get_list()
	print(min(n - 1, abs(a - b) + x))
import sys
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
out = []
get_int = lambda : int(input())
get_list = lambda : list(map(int, input().split()))
[main() for _ in range(int(input()))]
print(*out, sep='\n')


a = input()
content = []
for i in range(int(a)):
	b = input()
	content.append(b.split())
for i in range(int(a)):
	for j in range(4):
		content[i][j] = int(content[i][j])
for i in range(int(a)):
	total = abs(content[i][2] - content[i][3])
	swap = content[i][1]
	print(min(total + swap, content[i][0] - 1))


from datetime import datetime
import math

class Read:

	@staticmethod
	def string():
		return input()

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def list(sep=' '):
		return input().split(sep)

	@staticmethod
	def list_int(sep=' '):
		return list(map(int, input().split(sep)))

def solve():
	(n, x, a, b) = Read.list_int()
	print(int(min(n - 1, math.fabs(a - b) + x)))
query_count = 1
query_count = Read.int()
while query_count:
	query_count -= 1
	solve()


n = int(input())
for i in range(n):
	(n, x, a, b) = map(int, input().split())
	for l in range(x):
		if a > b:
			if a in range(2, n):
				a += 1
			elif b in range(2, n):
				b -= 1
		elif a < b:
			if b in range(2, n):
				b += 1
			elif a in range(2, n):
				a -= 1
	print(abs(a - b))


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for i in range(l()[0]):
	(n, x, a, b) = l()
	if x == 0:
		print(abs(b - a))
	else:
		print(min(n - 1, abs(b - a) + x))


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	start = abs(a - b)
	opt_moves = n - max(a, b) + (min(a, b) - 1)
	change = min(x, opt_moves)
	print(start + change)


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	if n == 2:
		print(1)
		continue
	if x == 0:
		print(abs(a - b))
		continue
	if x >= n - 2:
		print(n - 1)
		continue
	if abs(a - b) == n - 1:
		print(abs(a - b))
		continue
	if x >= n - abs(a - b):
		x = n - abs(a - b) - 1
	print(abs(a - b) + x)


n = int(input())
a = [[int(i) for i in input().split()] for m in range(n)]
for i in a:
	if i[1] == 0:
		print(abs(i[2] - i[3]))
	else:
		x = max(i[2], i[3])
		y = min(i[2], i[3])
		for j in range(i[1]):
			if x < i[0] and i[1] > 0:
				x += 1
				i[1] -= 1
			if y > 1 and i[1] > 0:
				y -= 1
				i[1] -= 1
		print(x - y)


t = int(input())

def solution(n, x, a, b):
	if x + abs(a - b) > n - 1:
		return n - 1
	else:
		return x + abs(a - b)
for _ in range(t):
	(n, x, a, b) = [int(f) for f in input().split()]
	res = solution(n, x, a, b)
	print(res)


import sys
t = int(sys.stdin.readline())
for i in range(t):
	(n, x, a, b) = list(map(int, sys.stdin.readline().strip().split()))
	(a, b) = sorted((a, b))
	op = b - a
	op += min(n - b + a - 1, x)
	print(op)


t = int(input())
for i in range(t):
	(n, x, a, b) = [int(x) for x in input().split()]
	(a, b) = (min(a, b), max(a, b))
	dist = b - a
	res = dist
	if dist == n - 1 or n == 0:
		res = dist
	else:
		res = dist + x
		if res > n - 1:
			res = n - 1
	print(res)


n = int(input())
ar = []
for i in range(n):
	s = input()
	a = s.split(' ')
	ar.append(a)
for i in range(n):
	n1 = int(ar[i][0])
	swap = int(ar[i][1])
	x = int(ar[i][2])
	y = int(ar[i][3])
	t = abs(x - y) + swap
	if t >= n1 - 1:
		print(n1 - 1)
	else:
		print(t)


import sys
import string
import math
import bisect as bi
input = sys.stdin.readline

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
for i in range(inin()):
	(n, x, a, b) = cin()
	if a > b:
		(a, b) = (b, a)
	if x >= n - 2 or (a == 1 and b == n):
		print(n - 1)
	elif b + x <= n:
		b = b + x
		print(b - a)
	elif a - x > 0:
		a = a - x
		print(b - a)
	else:
		x = x - (a - 1)
		a = 1
		if b + x >= n:
			b = n
		else:
			b = b + x
		print(b - a)


def maxDistance(totalStudents, swaps, aIndex, bIndex):
	result = abs(aIndex - bIndex) + swaps
	if result < totalStudents:
		return result
	else:
		return totalStudents - 1
for i in range(int(input())):
	(students, swapCount, a, b) = map(int, input().split())
	print(maxDistance(students, swapCount, a, b))


def main():
	T = int(input())
	for kase in range(T):
		(n, x, a, b) = map(int, input().split())
		print(min(n - 1, abs(a - b) + x))
	return 0
main()


2
n = int(input())
for i in range(n):
	(a, b, c, d) = tuple(map(int, input().split()))
	(l, r) = (min(c, d), max(c, d))
	if r - l + b >= a - 1:
		print(a - 1)
	else:
		print(r - l + b)


t = int(input())
for case in range(t):
	(n, x, a, b) = list(map(int, input().split()))
	dist = abs(b - a)
	ans = min(x + dist, n - 1)
	print(ans)


import math
m = int(input())
for i in range(m):
	(n, x, a, b) = [int(i) for i in input().split()]
	if a > b:
		c = n - a
		d = b - 1
	else:
		c = n - b
		d = a - 1
	if x == 0:
		print(abs(a - b))
	elif c == 0 and d == 0 or x >= c + d:
		print(n - 1)
	else:
		d = max(a, b)
		e = n - d
		f = min(a, b)
		if e <= x:
			d = n
			x = x - e
			f = f - x
			if f < 0:
				f = 0
			print(abs(d - f))
		else:
			d = d + x
			f = min(a, b)
			print(abs(d - f))


cases = int(input())
for cas in range(cases):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	while x > 0:
		if a > 1:
			a -= 1
			x -= 1
		elif b < n:
			b += 1
			x -= 1
		else:
			break
	print(b - a)


t = int(input())
for _ in range(t):
	(n, x, a, b) = [int(x) for x in input().strip().split()]
	s = abs(a - b)
	(a, b) = (min(a, b), max(a, b))
	while x and a > 1:
		s += 1
		a -= 1
		x -= 1
	while x and b < n:
		s += 1
		b += 1
		x -= 1
	print(s)


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	l = a - 1
	r = n - b
	s = l + r
	ans = b - a + min(x, s)
	print(ans)


test = int(input())
for temp in range(test):
	(n, x, a, b) = map(int, input().split())
	print(min(n - 1, abs(a - b) + x))


q = int(input())
for _ in range(q):
	(n, x, a, b) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	opt = a - 1 + n - b
	print(min(x, opt) + b - a)


for test in range(int(input())):
	(students, swaps, rival1, rival2) = list(map(int, input().split()))
	distance = abs(rival1 - rival2)
	potential = students - distance - 1
	if potential > swaps:
		print(distance + swaps)
	else:
		print(distance + potential)


from sys import stdin, stdout

def main():
	t = int(input())
	for _ in range(t):
		(n, x, a, b) = [int(x) for x in input().split(' ')]
		if a > b:
			(a, b) = (b, a)
		to_take_up = n - min(n, b)
		to_take_down = max(1, a) - 1
		swap = min(to_take_down + to_take_up, x)
		dist = b - a + swap
		print(dist)
main()


t = int(input())
o = []
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	ps = abs(a - b)
	n -= 1
	to = ps + x
	if to > n:
		to = n
	o.append(to)
for x in o:
	print(x)


q = int(input())
while q:
	(n, x, a, b) = [int(x) for x in input().split()]
	if a > b:
		(a, b) = (b, a)
	print(min(a - 1, x) + (b - a) + min(x - min(a - 1, x), n - b))
	q -= 1


def main():
	n = int(input())
	for i in range(n):
		(nst, x, a, b) = [int(i) for i in input().split()]
		if a > b:
			(a, b) = (b, a)
		dist = b - a
		if a - 1 + (nst - b) <= x:
			print(nst - 1)
		else:
			print(dist + x)
main()


q = int(input())
for z in range(q):
	(n, x, a, b) = list(map(int, input().split()))
	l = min(a, b)
	r = max(a, b)
	while x > 0 and l > 1:
		l -= 1
		x -= 1
	while x > 0 and r < n:
		r += 1
		x -= 1
	print(abs(l - r))


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	(a, b) = sorted((a, b))
	dist_now = b - a
	available = a - 1 + (n - b)
	if available < x:
		print(n - 1)
	else:
		print(dist_now + x)


t = int(input())
for i in range(t):
	(n, x, a, b) = [int(x) for x in input().split()]
	(a, b) = (max(a, b), min(a, b))
	newA = min(a + x, n)
	newB = max(1, b - max(0, x - newA + a))
	print(newA - newB)


from sys import stdin

def input():
	return int(stdin.readline())

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))
for _ in range(input()):
	(n, x, a, b) = minput()
	if a > b:
		(a, b) = (b, a)
	p = a - 1
	if x >= p:
		x -= p
		a = 1
	else:
		a = a - x
		x = 0
	if x >= n - b:
		x -= n - b
		b = n
	else:
		b = b + x
		x = 0
	print(b - a)


for q in range(int(input())):
	(n, x, a, b) = [int(i) for i in input().split()]
	if b < a:
		(b, a) = (a, b)
	if a - x >= 1:
		a -= x
		print(b - a)
	else:
		x -= a - 1
		a = 1
		if b + x <= n:
			b += x
			print(b - a)
		else:
			print(n - 1)


test = int(input())
list = []
for i in range(test):
	t = [int(x) for x in input().split()]
	l = [abs(1 - min(t[2:])), abs(max(t[2:]) - t[0])]
	dist = abs(t[2] - t[3])
	while (l[0] != 0 or l[1] != 0) and t[1] != 0:
		flag = 1
		if l[0] != 0:
			dist += 1
			flag = 0
			l[0] -= 1
		if l[1] != 0 and flag:
			dist += 1
			l[1] -= 1
		t[1] -= 1
	list.append(dist)
for i in list:
	print(i)


t = int(input())
while t > 0:
	(n, x, a, b) = map(int, input().split())
	m1 = max(a, b)
	m2 = min(a, b)
	if x == 0:
		n1 = a
		n2 = b
	elif abs(a - b) == n - 1:
		n1 = n
		n2 = 1
	elif m1 + x <= n:
		n1 = m1 + x
		n2 = m2
	elif m1 + x > n and m1 != n:
		x -= n - m1
		n1 = n
		if m2 - x >= 1:
			n2 = m2 - x
		else:
			n2 = 1
	elif m1 == n:
		n1 = n
		if m2 - x > 0:
			n2 = m2 - x
		else:
			n2 = 1
	print(abs(n1 - n2))
	t -= 1


for i in range(int(input())):
	a = input().split()
	n = int(a[0])
	kolvo = int(a[1])
	if int(a[2]) > int(a[3]):
		x1 = int(a[2])
		x2 = int(a[3])
	else:
		x1 = int(a[3])
		x2 = int(a[2])
	for i in range(kolvo):
		if x1 != n:
			x1 = x1 + 1
			continue
		if x2 != 1:
			x2 = x2 - 1
			continue
		if x1 == n and x2 == 1:
			break
	print(x1 - x2)


import math
q = int(input())
for i in range(q):
	t = list(map(int, input().split()))
	n = t[0]
	x = t[1]
	a = min(t[2], t[3])
	b = max(t[2], t[3])
	print(min(a - 1 + n - b, x) + b - a)


t = int(input())
for _ in '0' * t:
	(n, x, a, b) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	m = min(n - b, x)
	b += m
	x -= m
	m = min(a - 1, x)
	a -= m
	print(b - a)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
mod = pow(10, 9) + 7

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	(n, x, a, b) = sp()
	(a, b) = (min(a, b), max(a, b))
	answer = b - a
	if x >= a - 1:
		x -= a - 1
		a = 1
	elif a != 1:
		a -= x
		x = 0
	if x and x >= n - b:
		b = n
	elif b != n:
		b += x
		x = 0
	out(max(answer, b - a))


t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	if x == 0 or (b == n and a == 1) or (a == n and b == 1):
		print(abs(a - b))
	elif a > b:
		i = b
		j = a
		while i > 1 and x > 0 and (b != 1):
			i -= 1
			x -= 1
		while j < n and x > 0 and (a != n):
			j += 1
			x -= 1
		print(j - i)
	elif b == a:
		print(0)
	else:
		i = a
		j = b
		while i > 1 and x != 0 and (a != 1):
			i -= 1
			x -= 1
		while j < n and x != 0 and (b != n):
			j += 1
			x -= 1
		print(j - i)


t = int(input())
for i in range(0, t):
	m = input()
	(n, x, a, b) = [int(xx) for xx in m.split()]
	if a > b:
		c = b
		b = a
		a = c
	nn = b - a + x
	if nn > n - 1:
		nn = n - 1
	print(nn)


a = int(input())
for i in range(a):
	d = []
	(n, x, a, b) = map(int, input().split())
	if a < b:
		c = b - a
		d.append(c)
		for i in range(x):
			if a != 1:
				a = a - 1
			elif b < n:
				b = b + 1
			else:
				break
			c = b - a
			d.append(c)
		print(max(d))
	else:
		c = a - b
		d.append(c)
		for i in range(x):
			if b != 1:
				b = b - 1
			elif a < n:
				a = a + 1
			else:
				break
			c = a - b
			d.append(c)
		print(max(d))


t = int(input())
while t > 0:
	t -= 1
	(n, x, a, b) = [int(q) for q in input().split()]
	right_num = max(a, b)
	left_num = min(a, b)
	for i in range(x):
		if right_num < n:
			right_num += 1
		elif left_num > 1:
			left_num -= 1
	length = right_num - left_num
	print(length)


q = int(input())
for i in range(q):
	(n, x, a, b) = map(int, input().split(' '))
	current = abs(b - a)
	if x == 0:
		print(abs(b - a))
	elif b > a:
		print(min(n - 1, current + x))
	else:
		print(min(n - 1, current + x))


import os
import sys

def main():
	T = read_int()
	for _ in range(T):
		(N, X, A, B) = read_ints()
		print(solve(N, X, A, B))

def solve(N, X, A, B):
	max_dist = N - 1
	cur_dist = abs(B - A)
	return min(cur_dist + X, max_dist)
DEBUG = 'DEBUG' in os.environ

def inp():
	return sys.stdin.readline().rstrip()

def read_int():
	return int(inp())

def read_ints():
	return [int(e) for e in inp().split()]

def dprint(*value, sep=' ', end='\n'):
	if DEBUG:
		print(*value, sep=sep, end=end)
main()


from sys import stdin, stdout

def main():
	number_of_cases = stdin.readline()
	for line in stdin:
		(n, x, a, b) = (int(x) for x in line.strip().split())
		stdout.write(str(a_two_rival_students(n, x, a, b)))
		stdout.write('\n')

def a_two_rival_students(n, x, a, b):
	if abs(a - b) == n - 1:
		return n - 1
	if a < b:
		left = a
		right = b
	else:
		left = b
		right = a
	left_distance_to_1 = left - 1
	right_distance_to_n = n - right
	max_left_movement = left_distance_to_1 - x
	max_right_movement = right_distance_to_n - x
	if max_left_movement >= 0:
		return right - (left - x)
	elif max_right_movement >= 0:
		return right + x - left
	else:
		new_left = left - left_distance_to_1
		if right_distance_to_n - abs(max_left_movement) > 0:
			new_right = right + abs(max_left_movement)
		else:
			new_right = right + right_distance_to_n
		return new_right - new_left
main()


import sys
from time import *
from math import *
from bisect import bisect_left as bl, bisect_right as br, bisect, insort, insort_left, insort_right
INF = float('inf')
MOD = 998244353
mod = 10 ** 9 + 7
from collections import Counter as cc, defaultdict as df, OrderedDict as od
from functools import reduce

def counter(l):
	return dict(cc(l))

def lcm(a, b):
	return int(a * b / gcd(a, b))

def so(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}

def rev(l):
	return list(reversed(l))

def ini():
	return int(sys.stdin.readline())

def inp():
	return map(int, sys.stdin.readline().strip().split())

def li():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()

def inputm(n, m):
	return [input() for i in range(n)]

def yes():
	print('YES')

def no():
	print('NO')
b = 'NO'

def main():
	(n, x, a, b) = inp()
	(a, b) = (min(a, b), max(a, b))
	while a > 1 and x > 0:
		a -= 1
		x -= 1
	while b < n and x > 0:
		b += 1
		x -= 1
	print(b - a)
for i in range(ini()):
	main()


T = int(input())
for i in range(T):
	l = list(map(int, input().split(' ')))
	n = l[0]
	x = l[1]
	a = l[2]
	b = l[3]
	greaterPos = max(a, b)
	minPos = min(a, b)
	maxD = n - 1
	if x == 0:
		print(abs(a - b))
	elif abs(a - b) == n - 1:
		print(abs(a - b))
	else:
		if n - greaterPos >= minPos - 1:
			if x <= n - greaterPos:
				greaterPos += x
				x = 0
			else:
				x -= n - greaterPos
				greaterPos = n
				if minPos > 1:
					if minPos - 1 > x:
						minPos -= x
						x = 0
					else:
						x -= minPos - 1
						minPos = 1
		elif x <= minPos - 1:
			minPos -= x
			x = 0
		else:
			x -= minPos - 1
			minPos = 1
			if greaterPos < n:
				if n - greaterPos > x:
					greaterPos += x
					x = 0
				else:
					x -= n - greaterPos
					greaterPos = n
		print(abs(greaterPos - minPos))


t = int(input())
for j in range(t):
	(n, x, a, b) = [int(i) for i in input().split()]
	max1 = n - 1
	ans = abs(a - b) + x
	if ans < max1:
		print(ans)
	else:
		print(max1)


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	mini = min(a, b)
	maxi = max(a, b)
	mini -= x
	if mini < 1:
		mini = 1
		x = x - min(a, b) + 1
	else:
		x = 0
	maxi += x
	if maxi > n:
		maxi = n
	print(abs(maxi - mini))


import math
t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	maxa = n - 1
	dist = abs(a - b) + x
	if dist >= maxa:
		print(maxa)
	else:
		print(dist)


t = int(input())
ans = []
for i in range(t):
	(n, x, a, b) = map(int, input().split(' '))
	(r, l) = (max(a, b), min(a, b))
	(lhg, rhg) = (l - 1, n - r)
	gap = r - l
	while x != 0:
		if lhg >= 1:
			lhg -= 1
			x -= 1
			gap += 1
		elif rhg >= 1:
			rhg -= 1
			x -= 1
			gap += 1
		else:
			break
	ans.append(gap)
for i in range(t):
	print(ans[i])


import math

def rating_changes(ratings):
	extra = 0
	for r in ratings:
		extra += r if not r % 2 else 0
	changed_ratings = []
	for rating in ratings:
		if rating % 2 == 0:
			changed_ratings.append(rating / 2)
		elif extra < 0:
			pass

def rivals(n, x, a, b):
	max_value = n - 1
	if abs(a - b) + x > max_value:
		return max_value
	return abs(a - b) + x

class ReadInputs:

	def read_line(self) -> str:
		_s = self.__doc__
		return input()

	def read_number(self) -> int:
		return int(self.read_line())

	def read_string(self) -> str:
		return self.read_line()

	def read_list_str(self) -> list:
		return self.read_line().split(' ')

	def read_list_int(self) -> list:
		return [int(item) for item in self.read_line().split(' ')]

	def read_list_list_int(self, cols: int) -> list:
		return [self.read_list_int() for _ in range(cols)]

	def read_list_strings(self, count: int) -> list:
		return [self.read_string() for _ in range(count)]
read = ReadInputs()
test_cases = read.read_number()
for _ in range(test_cases):
	(_n, _x, _a, _b) = read.read_list_int()
	print(rivals(_n, _x, _a, _b))


import sys
input = sys.stdin.readline
Q = int(input())
Query = [list(map(int, input().split())) for _ in range(Q)]
for (N, X, a, b) in Query:
	print(min(N - 1, abs(a - b) + X))


import sys
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	if a - x > 0:
		s_max = b - a + x
	else:
		s_max = b - 1
		x -= a - 1
		if x > 0:
			if x > 0 and b + x <= n:
				s_max += x
			else:
				s_max += n - b
	if b + x <= n:
		b_max = b + x - a
	else:
		b_max = n - a
		x -= n - b
		if x > 0:
			if a - x > 0:
				b_max += x
			else:
				b_max += a - 1
	print(max(s_max, b_max))


import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *

class Scanner:

	@staticmethod
	def int():
		return int(sys.stdin.readline().rstrip())

	@staticmethod
	def string():
		return sys.stdin.readline().rstrip()

	@staticmethod
	def map_int():
		return [int(x) for x in Scanner.string().split()]

	@staticmethod
	def string_list(n):
		return [input() for i in range(n)]

	@staticmethod
	def int_list_list(n):
		return [Scanner.map_int() for i in range(n)]

	@staticmethod
	def int_cols_list(n):
		return [int(input()) for i in range(n)]

class Math:

	@staticmethod
	def gcd(a, b):
		if b == 0:
			return a
		return Math.gcd(b, a % b)

	@staticmethod
	def lcm(a, b):
		return a * b // Math.gcd(a, b)

	@staticmethod
	def divisor(n):
		res = []
		i = 1
		for i in range(1, int(n ** 0.5) + 1):
			if n % i == 0:
				res.append(i)
				if i != n // i:
					res.append(n // i)
		return res

	@staticmethod
	def round_up(a, b):
		return -(-a // b)

	@staticmethod
	def is_prime(n):
		if n < 2:
			return False
		if n == 2:
			return True
		if n % 2 == 0:
			return False
		d = int(n ** 0.5) + 1
		for i in range(3, d + 1, 2):
			if n % i == 0:
				return False
		return True

class PriorityQueue:

	def __init__(self, l=[]):
		self.q = l
		heapq.heapify(self.q)
		return

	def push(self, n):
		heapq.heappush(self.q, n)
		return

	def pop(self):
		return heapq.heappop(self.q)

def pop_count(x):
	x = x - (x >> 1 & 6148914691236517205)
	x = (x & 3689348814741910323) + (x >> 2 & 3689348814741910323)
	x = x + (x >> 4) & 1085102592571150095
	x = x + (x >> 8)
	x = x + (x >> 16)
	x = x + (x >> 32)
	return x & 127
MOD = int(1000000000.0) + 7
INF = int(1000000000000000.0)

def main():
	T = Scanner.int()
	for _ in range(T):
		(n, x, a, b) = Scanner.map_int()
		print(min(n - 1, abs(b - a) + x))
main()


t = int(input())
l = []
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	e = min(abs(a - b) + x, n - 1)
	l.append(e)
for i in l:
	print(i)


for i in range(int(input())):
	(n, x, a, b) = input().split()
	d = abs(int(a) - int(b))
	d = d + int(x)
	ans = min(int(n) - 1, d)
	print(ans)


t = int(input())
for _ in range(t):
	(n, x, a, b) = [int(i) for i in input().split()]
	if abs(a - b) == n - 1:
		print(abs(a - b))
	elif x == 0:
		print(abs(a - b))
	else:
		ans = abs(a - b) + x
		if ans > n - 1:
			print(n - 1)
		else:
			print(ans)


for i in range(int(input())):
	(n, k, a, b) = map(int, input().split())
	print(min(n - 1, max(k + a - b, k + b - a)))


def findMaxDistanceAfterXSwaps(n, x, a, b):
	if x == 0:
		return abs(a - b)
	if a > b:
		return findMaxDistanceAfterXSwaps(n, x, b, a)
	if x > a - 1:
		x = x - (a - 1)
		a = 1
	else:
		a = a - x
		x = 0
	if x > 0:
		if x > n - b:
			b = n
		else:
			b = b + x
	return b - a
t = int(input())
for i in range(t):
	(n, x, a, b) = input().split()
	print(findMaxDistanceAfterXSwaps(int(n), int(x), int(a), int(b)))


q = int(input())
for _ in range(q):
	(n, x, a, b) = map(int, input().split())
	left = min(a, b)
	right = max(a, b)
	right_shift = min(n - right, x)
	right += right_shift
	x -= right_shift
	left_shift = min(left - 1, x)
	left -= left_shift
	print(right - left)


t = int(input())
for test in range(t):
	[n, x, a, b] = list(map(int, input().split()))
	aa = min(a, b)
	bb = max(a, b)
	print(bb - aa + min(x, aa - 1 + n - bb))


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().rstrip().split())
	(a, b) = (b, a) if a > b else (a, b)
	dis = n - b + a - 1
	if x > dis:
		print(n - 1)
	else:
		print(b - a + x)


q = int(input())
for i in range(q):
	(w, e, r, t) = map(int, input().split())
	(r, t) = sorted((r, t))
	for i in range(e):
		if r > 1:
			r -= 1
		elif t < w:
			t += 1
	print(t - r)


def main():
	for _ in range(int(input())):
		(n, x, a, b) = map(int, input().split())
		left = min(a, b)
		right = max(a, b)
		ans = 0
		while left > 1 and x > 0:
			left -= 1
			x -= 1
		while right < n and x > 0:
			right += 1
			x -= 1
		print(abs(right - left))
main()


l = list()
for i in range(int(input())):
	arr = list(map(int, input().split()))
	dis = abs(arr[2] - arr[3]) + arr[1]
	l.append(min(arr[0] - 1, dis))
for i in range(len(l)):
	print(l[i])


for _ in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	(b, c) = sorted([b, c])
	print(min(a, b + n - c - 1) + c - b)


test_count = int(input())

def check():
	my_list = list(map(int, input().split()))
	n = my_list[0]
	x = my_list[1]
	a = my_list[2]
	b = my_list[3]
	sc = abs(a - b)
	while x != 0:
		if sc == n - 1:
			break
		else:
			sc += 1
			x -= 1
	print(sc)
for i in range(test_count):
	check()


def maximize_distance(test_case):
	n = test_case[0]
	x = test_case[1]
	a = test_case[2]
	b = test_case[3]
	maximo = max(a, b)
	minimo = min(a, b)
	for swap in enumerate(range(x)):
		if maximo < n:
			maximo += 1
		elif minimo > 1:
			minimo -= 1
	return maximo - minimo
for _ in range(int(input())):
	test_case = [int(i) for i in input().split(' ')]
	print(maximize_distance(test_case))


import sys
import math
Input = sys.stdin.readline
get_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_int = lambda : int(sys.stdin.readline())
for _ in range(0, get_int()):
	a = get_list()
	space = abs(a[3] - a[2])
	r = a[0] - space - 1
	n = min(r, a[1])
	print(space + n)


for _ in range(int(input())):
	(n, x, a, b) = [*map(int, input().split())]
	if a > b:
		(a, b) = (b, a)
	pb = b
	b = min(x + b, n)
	x -= b - pb
	a = max(a - x, 1)
	print(b - a)


def main(n, x, a, b):
	if a >= b:
		tmp = a
		a = b
		b = tmp
	if x >= n - 1:
		return n - 1
	else:
		return min([x + b - a, n - 1])
ret = []
t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	ret.append(main(n, x, a, b))
for elem in ret:
	print(elem)


for _ in range(int(input())):
	(n, x, a, b) = list(map(int, input().split()))
	d = abs(a - b)
	maxi = n - 1
	s = d + x
	print(s if s <= maxi else maxi)


t = int(input())
while t > 0:
	(n, x, a, b) = [int(i) for i in input().split()]
	if b < a:
		(a, b) = (b, a)
	b1 = n - b
	a1 = a - 1
	print(b - a + min(x, b1 + a1))
	t -= 1


import sys, math

def input():
	return sys.stdin.readline()[:-1]

def main():
	q = int(input())
	for _ in range(q):
		(n, x, a, b) = map(int, input().split())
		print(min(n - 1, abs(b - a) + x))
main()


t = int(input())
for _ in range(t):
	args = [int(x) for x in input().split(' ')]
	n = args[0]
	x = args[1]
	a = args[2]
	b = args[3]
	if abs(a - b) + x >= n:
		print(str(n - 1))
	else:
		print(str(abs(a - b) + x))


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	mi = min(a, b)
	mx = max(a, b)
	while x >= 1:
		if mi > 1 and x >= 1:
			mi -= 1
			x -= 1
		if mx < n and x >= 1:
			mx += 1
			x -= 1
		if mi == 1 and mx == n:
			break
	print(mx - mi)


f = int(input())
for q in range(f):
	(n, x, a, b) = map(int, input().split())
	print(min(n - 1, abs(a - b) + x))


def mod(s):
	if s < 0:
		return -s
	return s
t = int(input())
for i in range(t):
	(n, x, a, b) = [int(x) for x in input().split()]
	(a, b) = (max(a, b), min(a, b))
	print(mod(a - b) + min(x, b - 1 + n - a))


t = int(input())
for __ in range(t):
	(n, x, a, b) = [int(k) for k in input().split()]
	temp1 = min(a, b)
	temp2 = max(a, b)
	a = temp1
	b = temp2
	dista = a - 1
	distb = n - b
	distatual = b - a
	distatual += min(x, dista + distb)
	print(distatual)


def main():
	numbers = []
	number_of_test = int(input(''))
	for i in range(number_of_test):
		a = list(map(int, input('').strip().split()))[:4]
		numbers.append(a)
	print('')
	for i in range(number_of_test):
		distanc = numbers[i][2] - numbers[i][3]
		distanc = abs(distanc)
		if distanc + numbers[i][1] >= numbers[i][0]:
			print(numbers[i][0] - 1)
		else:
			distanc = distanc + numbers[i][1]
			print(distanc)
main()


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	l = min(a, b)
	r = max(a, b)
	if x == 0:
		print(abs(l - r))
		continue
	elif n - r <= x:
		x = x - (n - r)
		r = r + (n - r)
	else:
		r = r + x
		x = 0
	if x == 0:
		print(abs(l - r))
		continue
	elif l - 1 < x:
		x = x - (l - 1)
		l = l - (l - 1)
	else:
		l = l - x
		x = 0
	print(abs(l - r))


t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	a1 = max(a, b)
	a2 = min(a, b)
	dis = a1 - a2
	rem1 = n - a1
	rem2 = a2 - 1
	rem = rem1 + rem2
	res = 0
	if x >= rem:
		res = n - 1
	else:
		res = dis + x
	print(res)


def ab():
	(n, x, a, b) = map(int, input().split(' '))
	t = abs(a - b)
	while x != 0 and t != n - 1:
		t = t + 1
		x = x - 1
	print(t)
a = int(input())
for i in range(0, a):
	ab()


t = int(input())
while t:
	t -= 1
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	(da, db) = (a - 1, n - b)
	if da + db <= x:
		print(n - 1)
	else:
		a -= min(da, x)
		x -= min(da, x)
		b += min(db, x)
		print(b - a)


q = int(input())
for _ in q * ' ':
	(n, x, a, b) = map(int, input().split())
	if b < a:
		(a, b) = (b, a)
	print(abs(a - b) + min(x, a - 1 + n - b))


import math
for _ in range(int(input())):
	(n, x, a, b) = [int(i) for i in input().split()]
	print(min(n - 1, int(math.fabs(a - b) + x)))


def main():
	t = int(input())
	for i in range(t):
		(n, x, a1, b) = list(map(int, input().split(' ')))
		a = max(a1, b)
		b = min(a1, b)
		if n - 1 == a - b:
			print(a - b)
			continue
		else:
			x1 = a - b
			y1 = x1 + x
			if y1 > n - 1:
				print(n - 1)
				continue
			else:
				print(y1)
main()


t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().strip().split())
	avail_steps = min(a, b) - 1 + (n - max(a, b))
	if x <= avail_steps:
		print(x + abs(a - b))
	else:
		print(abs(min(a, b) - 1) + abs(a - b) + abs(n - max(a, b)))


for _ in range(int(input())):
	(students, swaps, pos1, pos2) = map(int, input().split())
	if abs(pos1 - pos2) == students - 1 or swaps == 0:
		print(abs(pos1 - pos2))
	else:
		print(min(abs(pos1 - pos2) + swaps, students - 1))


numTest = int(input())
for test in range(numTest):
	(numRow, numSwaps, pos1, pos2) = map(int, input().split())
	print(min(abs(pos1 - pos2) + numSwaps, numRow - 1))


for i in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	p = abs(a - b)
	if p == n - 1:
		pass
	else:
		p += x
	print(min(n - 1, p))


t = int(input())
for i in range(0, t):
	line = input().split()
	n = int(line[0])
	x = int(line[1])
	a = int(line[2])
	b = int(line[3])
	d = abs(a - b)
	d = d + x
	if d > n - 1:
		d = n - 1
	print(d)


t = int(input())
for i in range(t):
	n = list(map(int, input().split()))
	maxi = max(n[2], n[3])
	mini = min(n[2], n[3])
	agr = min(n[0] - maxi, n[1])
	n[1] = n[1] - agr
	decr = min(mini - 1, n[1])
	maxi += agr
	mini -= decr
	print(maxi - mini)


i = int(input(''))
a = [0] * i
for j in range(i):
	(w, x, y, z) = [int(x) for x in input('').split()]
	if w - 1 > x + abs(y - z):
		o = x + abs(y - z)
		a[j] = o
	else:
		p = w - 1
		a[j] = p
for k in range(i):
	print(a[k])


n = int(input())
count = 0
dist = []
while count < n:
	q = input()
	q = [int(x) for x in q.split()]
	t = q[0]
	x = q[1]
	a = q[2]
	b = q[3]
	if abs(a - b) == t - 1:
		dist.append(t - 1)
	elif x != 0:
		q.pop(0)
		q.pop(0)
		q = sorted(q)
		flag = 0
		if q[0] - 1 < x:
			flag = 1
			x = x - (q[0] - 1)
			new_a = 1
		else:
			new_a = q[0] - x
			x = 0
			dist.append(abs(new_a - q[1]))
		if flag == 1:
			new_b = q[1] + x
			if new_b > t:
				new_b = t
			dist.append(abs(new_a - new_b))
	else:
		dist.append(abs(a - b))
	count += 1
for y in dist:
	print(y)


t = int(input())
for ij in range(0, t):
	(n, x, a, b) = map(int, input().split())
	t = abs(a - b)
	print(min(t + x, n - 1))


t = int(input())
while t:
	(n, x, a, b) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	f = 0
	for _ in range(0, x):
		if f == 0:
			if a == 1:
				if b != n:
					b += 1
			else:
				a -= 1
			f = 1
		else:
			if b == n:
				if a != 1:
					a -= 1
			else:
				b += 1
			f = 0
	print(abs(b - a))
	t -= 1


import math
t = int(input())
for k in range(t):
	(n, x, a, b) = input().split(' ')
	n = int(n)
	x = int(x)
	a = int(a)
	b = int(b)
	if a > b:
		flag = 1
	elif a == b:
		flag = 0
	else:
		flag = 2
	cnt = 0
	if flag == 1 or flag == 0:
		while b > 1 and cnt != x:
			b -= 1
			cnt += 1
		while a < n and cnt != x:
			a += 1
			cnt += 1
		print(a - b)
	elif flag == 2:
		while a > 1 and cnt != x:
			a -= 1
			cnt += 1
		while b < n and cnt != x:
			b += 1
			cnt += 1
		print(b - a)


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	(kanan, kiri) = (max(a, b), min(a, b))
	while x > 0 and kanan <= n and (kiri >= 1) and (n - kanan > 0 or kiri - 1 > 0):
		if n - kanan >= kiri - 1:
			kanan += x
			lebih = 0
			if kanan > n:
				lebih = kanan - n
			kanan -= lebih
			x = lebih
		else:
			kiri -= x
			lebih = 0
			if kiri < 0:
				lebih = 1 - kiri
			kiri += lebih
			x = lebih
	if kanan > n:
		kanan -= 1
	elif kiri == 0:
		kiri += 1
	print(kanan - kiri)


import sys
from array import array
import typing as Tp

def input():
	return sys.stdin.buffer.readline().decode('utf-8')

def main():
	t = int(input())
	for _ in range(t):
		(n, x, a, b) = map(int, input().split())
		if a > b:
			(a, b) = (b, a)
		left_mv = min(x, a - 1)
		x -= left_mv
		a -= left_mv
		right_mv = min(x, n - b)
		b += right_mv
		print(b - a)
main()


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return map(int, input().split())

def array():
	return [int(i) for i in input().split()]
for i in range(int(input())):
	(n, x, a, b) = value()
	ans = abs(b - a)
	dif1 = min(a, b) - 1
	dif2 = n - max(a, b)
	if x >= dif1 + dif2:
		print(n - 1)
	else:
		print(ans + x)


import time

def calcMaxDistance(stu, swaps, pA, pB):
	maxDist = 0
	origDist = abs(pA - pB)
	maxMaxDist = stu - 1
	maxSwaps = stu - 2
	if maxMaxDist == origDist or swaps == 0:
		maxDist = origDist
		return maxDist
	if maxMaxDist > origDist + swaps:
		maxDist = origDist + swaps
	else:
		maxDist = maxMaxDist
	return maxDist

def solve():
	tcases = int(input())
	for x in range(tcases):
		(stu, swaps, pA, pB) = [int(y) for y in input().split()]
		print(calcMaxDistance(stu, swaps, pA, pB))
solve()


t = int(input())
for _ in range(t):
	(n, x, at, bt) = list(map(int, input().split()))
	a = min(at, bt)
	b = max(at, bt)
	while x > 0 and (a > 1 or b < n):
		x -= 1
		if a > 1:
			a -= 1
		elif b < n:
			b += 1
	print(b - a)


def solve():
	data = list(map(int, input().split()))
	num_ppl = data[0]
	switches = data[1]
	posA = data[2]
	posB = data[3]
	distance = abs(posB - posA)
	distance = distance + switches
	if distance < num_ppl:
		print(distance)
	else:
		print(num_ppl - 1)
test_cases = int(input())
for x in range(test_cases):
	solve()


t = int(input())
for i in range(t):
	(n, x, a, b) = list(map(int, input().split()))
	y = min(a, b) - 1
	z = n - max(a, b)
	if x >= y + z:
		print(n - 1)
	else:
		print(abs(a - b) + x)


for x in range(int(input())):
	(a, b, c, d) = map(int, input().strip().split()[:4])
	if b == 0:
		print(abs(c - d))
	elif c > d:
		c += b
		if c > a:
			k = c - a
			c -= k
			b = k
			d -= k
			if d <= 1:
				d += abs(1 - d)
				print(abs(c - d))
			else:
				print(abs(c - d))
		else:
			print(abs(c - d))
	else:
		d += b
		if d > a:
			k = d - a
			d = a
			b = k
			c -= k
			if c <= 0:
				c += abs(1 - c)
				print(abs(c - d))
			else:
				print(abs(c - d))
		else:
			print(abs(c - d))


def calculate_distance(start, end):
	return abs(start - end)

def process_input_line():
	(n, x, a, b) = map(lambda input_value: int(input_value), input().split(' '))
	calculated_distance = calculate_distance(a, b)
	while calculated_distance != n - 1 and x > 0:
		calculated_distance += 1
		x -= 1
	return calculated_distance
T = int(input())
for i in range(T):
	print(process_input_line())


t = int(input())
s = []
for i in range(0, t):
	x = input()
	xx = x.split()
	n = int(xx[0])
	k = int(xx[1])
	gh = [int(xx[2]), int(xx[3])]
	a = min(gh)
	b = max(gh)
	if a - 1 + n - b >= k:
		s.append(b - a + k)
	else:
		s.append(n - 1)
for i in s:
	print(i)


n = int(input())
res = []
for i in range(n):
	res += [input().split()]
for i in range(n):
	length = int(res[i][0])
	move = int(res[i][1])
	distance = abs(int(res[i][2]) - int(res[i][3]))
	if move >= length - distance - 1:
		print(length - 1)
	else:
		print(distance + move)


for i in [0] * int(input()):
	(n, x, a, b) = map(int, input().split())
	(b, a) = sorted([a, b])
	c = n - a + (b - 1)
	print(a - b + c if c <= x else a - b + x)


import math
import sys
sys.setrecursionlimit(10 ** 8)
input = sys.stdin.readline
t = int(input())
for i in range(t):
	(n, x, a, b) = [int(item) for item in input().split()]
	ans = abs(a - b) + x
	ans = min(n - 1, ans)
	print(ans)


def main():

	def solve():
		(n, x, a, b) = map(int, input().split())
		print(min(n - 1, abs(a - b) + x))
	q = int(input())
	for _ in range(q):
		solve()
main()


def swap(info):
	dist = abs(info[2] - info[3])
	if dist + info[1] < info[0]:
		return dist + info[1]
	else:
		return info[0] - 1
num = int(input())
for i in range(num):
	temp = input().split(' ')
	info = [int(x) for x in temp]
	print(swap(info))


def solve(arr):
	(n, x, a, b) = arr
	if a > b:
		(a, b) = (b, a)
	if abs(a - b) == n - 1:
		return abs(a - b)
	g1 = abs(1 - a)
	g2 = abs(n - b)
	if x > g1 + g2:
		return n - 1
	else:
		return abs(a - b) + x
for _ in range(int(input())):
	arr = map(int, input().split())
	print(solve(arr))


count = int(input())
result = []
for _ in range(count):
	(n, x, a, b) = list(map(lambda num: int(num), input().split()))
	result.append(str(min(abs(a - b) + x, n - 1)))
print('\n'.join(result))


def calculate_max_distance(n, x, a, b):
	max_distance = abs(a - b) + x
	if max_distance > n - 1:
		return n - 1
	else:
		return max_distance
for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	print(calculate_max_distance(n, x, a, b))


for t in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	diff = abs(a - b)
	ans = diff + x
	if ans >= n:
		ans = n - 1
	print(ans)


query = int(input())
for x in range(query):
	nums = input().split()
	nums = (int(x) for x in nums)
	(n, x, a, b) = nums
	maxd = n - 1
	distance = abs(a - b)
	while x > 0:
		if distance < maxd:
			x -= 1
			distance += 1
		else:
			break
	print(distance)


def gns():
	return list(map(int, input().split()))
t = int(input())
for i in range(t):
	(n, x, a, b) = gns()
	ans = abs(a - b) + x
	ans = min(ans, n - 1)
	print(ans)


for i in range(int(input())):
	X = list(map(int, input().split()))
	Temp = sorted([X[-1], X[-2]])
	Left = min(X[1], Temp[0] - 1)
	X[1] -= Left
	Right = min(X[1], X[0] - Temp[1])
	print(Temp[1] - Temp[0] + Right + Left)


for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	aa = max(a, b)
	bb = min(a, b)
	an = aa - bb
	while x > 0 and (n - aa > 0 or bb - 1 > 0):
		an = an + 1
		x = x - 1
		if n - aa > 0:
			aa = aa + 1
		else:
			bb = bb - 1
	print(an)


def dist(n, x, a, b):
	d = abs(a - b)
	return min(n - 1, d + x)
N = int(input())
for _ in range(N):
	print(dist(*map(int, input().split())))


import sys, math, queue, collections
MOD = 10 ** 9 + 7
for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	if a - 1 >= x:
		a = a - x
	else:
		x -= a - 1
		a = 1
		b += x
		b = min(b, n)
	print(b - a)


from sys import stdin
t = int(stdin.readline().rstrip())
while t > 0:
	(n, x, a, b) = map(int, stdin.readline().split())
	mi = min(a, b)
	ma = max(a, b)
	q = ma - mi
	if mi != 1:
		q += min(x, mi - 1)
		x -= min(x, mi - 1)
	if ma != n:
		q += min(x, n - ma)
	print(q)
	t -= 1


def solve():
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	while x > 0 and a > 1:
		x -= 1
		a -= 1
	while x > 0 and b < n:
		x -= 1
		b += 1
	return b - a

def main():
	q = int(input())
	for _ in range(q):
		print(solve())
main()


t = int(input())
out = []
for i in range(t):
	inarr = [int(c) for c in input().split()]
	check = inarr[1]
	ma = max(inarr[2], inarr[3])
	mi = min(inarr[2], inarr[3])
	while check > 0:
		if ma - mi == inarr[0] - 1:
			check = 0
		else:
			if ma < inarr[0] and check > 0:
				ma += 1
				check -= 1
			if mi > 1 and check > 0:
				mi -= 1
				check -= 1
	out.append(ma - mi)
for i in range(t):
	print(out[i])


T = int(input())
for i in range(T):
	(n, x, a, b) = map(int, input().strip().split())
	print(min([abs(b - a) + x, n - 1]))


N = int(input())
while N != 0:
	N -= 1
	(n, x, a, b) = map(int, input().split())
	print(min(abs(b - a) + x, n - 1))


import sys, math
from heapq import heappush, heappop
readline = sys.stdin.readline
mr = lambda : map(int, readline().split())
t = int(readline())
for _ in range(t):
	(n, x, a, b) = mr()
	print(min(n - 1, abs(a - b) + x))


t = int(input())
for i in range(t):
	(n, x, a, b) = map(int, input().split())
	l = []
	l.append(max(a, b))
	l.append(min(a, b))
	z = abs(a - b)
	for i in range(x):
		if l[0] < n:
			l[0] = l[0] + 1
		elif l[1] > 1:
			l[1] = l[1] - 1
	if z >= abs(l[0] - l[1]):
		print(z)
	else:
		print(abs(l[0] - l[1]))


a = int(input())
r = 0
d = list()
while r < a:
	b = [int(b) for b in input().split()]
	if b[3] > b[2]:
		c = 0
		while c < b[1]:
			if b[3] != b[0]:
				b[3] = b[3] + 1
			elif b[2] != 1:
				b[2] = b[2] - 1
			c = c + 1
	else:
		c = 0
		while c < b[1]:
			if b[2] != b[0]:
				b[2] = b[2] + 1
			elif b[3] != 1:
				b[3] = b[3] - 1
			c = c + 1
	r = r + 1
	d.append(abs(b[2] - b[3]))
r = 0
while r < len(d):
	print(d[r])
	r = r + 1


for i in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	rs = abs(a - b)
	ans = rs + x
	if ans < n:
		print(ans)
	if ans >= n:
		print(n - 1)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
t = inp()
for testcase in range(t):
	(n, x, a, b) = invr()
	(a, b) = (min(a, b), max(a, b))
	cnt = 0
	for i in range(x):
		if a > 1:
			a -= 1
			x -= 1
		else:
			break
	for i in range(x):
		if b < n:
			b += 1
			x -= 1
	print(b - a)


n = int(input())
for i in range(n):
	a = [int(a) for a in input().split()]
	b = a[0] - 1
	if abs(a[2] - a[3]) == b or abs(a[2] - a[3]) + a[1] >= b:
		print(b)
	else:
		print(abs(a[2] - a[3]) + a[1])


import math
t = int(input())
while t > 0:
	v = input().split(' ')
	n = int(v[0])
	x = int(v[1])
	a = int(v[2])
	b = int(v[3])
	print(min(abs(a - b) + x, n - 1))
	t -= 1


for i in [0] * int(input()):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	a -= 1
	b -= 1
	da = min(a, x)
	x -= da
	db = min(x, n - 1 - b)
	print(b - a + da + db)


import math
t = int(input())
while t:
	(n, x, a, b) = map(int, input().split())
	if abs(a - b) == n - 1:
		print(n - 1)
	else:
		a1 = min(a, b)
		b1 = max(a, b)
		a = a1 - 1
		b = n - b1
		if a <= b:
			if x > a:
				x -= a
				if x > b:
					print(n - 1)
				else:
					print(b1 + x - 1)
			elif x == a:
				print(b1 - 1)
			else:
				print(b1 - (a1 - x))
		elif x > b:
			x -= b
			if x > a:
				print(n - 1)
			else:
				print(n - (a1 - x))
		elif x == b:
			print(n - a1)
		else:
			print(b1 + x - a1)
	t -= 1


def stud(n, x, a, b):
	max = n - 1
	pos = abs(a - b) + x
	if max > pos:
		return pos
	else:
		return max
set = int(input())
while set > 0:
	l = input().split(' ')
	(n, x, a, b) = (int(l[0]), int(l[1]), int(l[2]), int(l[3]))
	set -= 1
	print(stud(n, x, a, b))


(t, maxDisList) = (int(input()), [])
for z in range(t):
	(n, x, a, b) = [int(i) for i in input().split(' ')]
	maxDis = abs(a - b)
	if a < b:
		(preDis, postDis) = (a, n - b)
	else:
		(preDis, postDis) = (b, n - a)
	if preDis - x > 0:
		maxDis += x
	else:
		maxDis += preDis - 1
		remainder = x - preDis + 1
		if postDis - remainder > 0:
			maxDis += remainder
		else:
			maxDis += postDis
	maxDisList.append(maxDis)
print('\n'.join([str(i) for i in maxDisList]))


import sys
t = int(sys.stdin.readline())
for i in range(t):
	inp = sys.stdin.readline().split(' ')
	n = int(inp[0])
	swaps = int(inp[1])
	first = int(inp[2])
	second = int(inp[3])
	print(min(n - 1, abs(first - second) + swaps))


t = int(input())
for i in range(t):
	n = tuple(input().split(' '))
	test = abs(int(n[3]) - int(n[2]))
	aux = int(n[0]) - 1 - test
	if aux < int(n[1]):
		print(test + aux)
	else:
		print(test + int(n[1]))


t = int(input())
for i in range(t):
	(n, x, a, b) = [int(i) for i in input().split()]
	c = 0
	y = abs(b - a)
	if a < b:
		max = b
		min = a
	else:
		max = a
		min = b
	while max < n and c < x:
		y += 1
		max += 1
		c += 1
	while min > 1 and c < x:
		y += 1
		min -= 1
		c += 1
	print(y)


t = int(input())
for i in range(t):
	c = input().split()
	(n, x, a, b) = [int(c[k]) for k in range(4)]
	c = min(a, b)
	a = max(a, b)
	b = c
	p = abs(b - a)
	for j in range(x):
		if a == 1 and b == n or (b == 1 and a == n):
			p = n - 1
		elif a < n:
			p += 1
			a += 1
		elif b > 1:
			b -= 1
			p += 1
	print(p)


for _ in range(int(input())):
	(n, x, c, d) = map(int, input().split())
	a = min(c, d)
	b = max(c, d)
	while x and b < n:
		b += 1
		x -= 1
	while x and a > 1:
		a -= 1
		x -= 1
	print(b - a)


from sys import stdin

def input():
	return stdin.readline()[:-1]

def intput():
	return int(input())

def sinput():
	return input().split()

def intsput():
	return map(int, sinput())
t = intput()
for _ in range(t):
	(n, x, a, b) = intsput()
	if a > b:
		(a, b) = (b, a)
	print(min(b - a + x, n - 1))


from collections import Counter
from collections import defaultdict
import math
t = int(input())
for i in range(0, t):
	(n, s, a, b) = list(map(int, input().split()))
	mn = max(a, b) - min(a, b)
	mn = mn + s
	if mn > n - 1:
		print(n - 1)
	else:
		print(mn)


t = int(input())
while t:
	(n, x, a, b) = map(int, input().split())
	d = abs(a - b)
	p = d + x
	if p < n:
		print(p)
	else:
		print(n - 1)
	t -= 1


def solve():
	(n, x, a, b) = [int(x) for x in input().split(' ')]
	a_larger = False
	b_larger = False
	if a > b:
		a_larger = True
	else:
		b_larger = True
	if a_larger:
		if n > a + x:
			a += x
		else:
			x -= n - a
			a = n
			if b - x < 1:
				b = 1
			else:
				b -= x
	elif n > b + x:
		b += x
	else:
		x -= n - b
		b = n
		if a - x < 1:
			a = 1
		else:
			a -= x
	return abs(b - a)
t = int(input())
for i in range(t):
	ans = solve()
	print(ans)


t = int(input())
for I in range(t):
	line = [int(i) for i in input().split(' ')]
	n = line[0]
	x = line[1]
	a = line[2]
	b = line[3]
	ans = min(abs(a - b) + x, n - 1)
	print(ans)


t = int(input())
for __ in range(t):
	(a, b, c, d) = map(int, input().split())
	e = abs(d - c)
	e += b
	a -= 1
	if e > a:
		e = a
	print(e)


t = int(input())
for tt in range(0, t):
	(n, x, a, b) = [int(x) for x in input().split()]
	p = abs(a - b)
	p = min(n - 1, p + x)
	print(p)


from collections import defaultdict
from math import inf as INFINITY

def solve(n, x, a, b):
	return min(n - 1, abs(a - b) + x)
for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	print(solve(n, x, a, b))


t = int(input(''))
for i in range(t):
	c = input('')
	d = c.split(' ')
	n = int(d[0])
	x = int(d[1])
	a = int(d[2])
	b = int(d[3])
	e = abs(a - b)
	if e + x <= n - 1:
		print(e + x)
	else:
		print(n - 1)


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if c > d:
		(c, d) = (d, c)
	count = d - c
	while b > 0:
		if d + 1 <= a:
			d += 1
		elif c - 1 >= 1:
			c -= 1
		b -= 1
	print(max(count, d - c))


n = int(input())
Result = []
for i in range(0, n):
	List = list(map(int, input().strip().split()))[0:4]
	n = List[0]
	x = List[1]
	if List[2] < List[3]:
		left_pos = List[2]
		right_pos = List[3]
	else:
		left_pos = List[3]
		right_pos = List[2]
	pos_distance = right_pos - left_pos
	low = left_pos - 1
	high = n - right_pos
	Sum = low + high
	if x <= Sum:
		Result.append(x + pos_distance)
	else:
		Result.append(n - 1)
for i in Result:
	print(i)


import math
t = int(input())
output = []
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	for _ in range(x):
		if abs(a - b) == n - 1:
			break
		if a > b and a < n:
			a = a + 1
		elif a > b and a == n:
			b = b - 1
		elif b > a and b < n:
			b = b + 1
		elif b > a and b == n:
			a = a - 1
	output.append(abs(a - b))
for x in output:
	print(x)


def Neighbours():
	(n, x, a, b) = list(map(int, input().split()))
	print(min(n - 1, abs(a - b) + x))
t = int(input())
for i in range(t):
	Neighbours()


cases = []
num_of_cases = int(input())
for _ in range(num_of_cases):
	cases.append(list(map(int, input().strip().split())))
for case in cases:
	print(min(abs(case[2] - case[3]) + case[1], case[0] - 1))


y = int(input())
lst = []
for i in range(0, y):
	(n, x, a1, b1) = map(int, input().split())
	a = min(a1, b1)
	b = max(a1, b1)
	c = n - b
	d = min(c, x)
	x = x - d
	f = max(1, a - x)
	lst.append(b + d - f)
print(*lst, sep='\n')


T = int(input())
while T:
	T -= 1
	(n, x, a, b) = map(int, input().split())
	ans = min(n - 1, abs(b - a) + x)
	print(ans)


t = int(input())
L = []
for _ in range(t):
	temp = list(map(int, input().split()))
	L.append(temp)
for i in range(t):
	(n, x, a, b) = L[i]
	l = abs(b - a)
	a_ = max(a, b)
	b_ = min(a, b)
	z = n - 1 - a_ + b_
	print(l + min(x, z))


t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	m1 = min(a, b)
	m2 = max(a, b)
	for i in range(x):
		if m1 > 1:
			m1 = m1 - 1
		elif m2 < n:
			m2 = m2 + 1
	print(m2 - m1)


def solve(test):
	ans = 0
	(n, x, a, b) = map(int, input().split())
	ans = min(n - 1, abs(a - b) + x)
	print(ans)
t = int(input())
for _ in range(t):
	solve(_ + 1)


import sys, atexit
from io import BytesIO
inp = BytesIO(sys.stdin.buffer.read())
input = lambda : inp.readline().decode('ascii')
buf = BytesIO()
atexit.register(lambda : sys.__stdout__.buffer.write(buf.getvalue()))
for i in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	da = a - 1
	a -= min(da, x)
	x -= min(da, x)
	db = n - b
	b += min(db, x)
	x -= min(db, x)
	print(b - a)


t = int(input())
for case in range(t):
	(n, x, a, b) = [int(x) for x in input().split()]
	print(min(abs(a - b) + x, n - 1))


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if c > d:
		ans = min(b, a - c)
		b = b - ans
		if b == 0:
			print(ans + (c - d))
		else:
			e = min(d - 1, b)
			print(e + ans + (c - d))
	else:
		ans = min(b, a - d)
		b = b - ans
		if b == 0:
			print(ans + (d - c))
		else:
			e = min(c - 1, b)
			print(e + ans + (d - c))


t = int(input())
for _ in range(t):
	(n, x, a, b) = map(int, input().split())
	mi = min(a, b)
	mx = max(a, b)
	d1 = abs(1 - mi)
	d2 = abs(n - mx)
	to = d1 + d2
	ans = abs(a - b)
	if d1 == x:
		print(max(ans, abs(1 - mx)))
	elif d2 == x:
		print(max(ans, abs(n - mi)))
	elif d1 > x:
		dr = mi - x
		print(max(ans, abs(dr - mx)))
	elif d2 > x:
		di = mx + x
		print(max(ans, abs(mi - di)))
	elif to <= x:
		print(max(ans, abs(1 - n)))
	else:
		temp = x
		x = x - d1
		y = abs(temp - d2)
		print(max(ans, abs(1 - mx + x), abs(mi - y - n)))


t = int(input())
result = str()
for i in range(t):
	(n, x, a, b) = list(map(int, input().split()))
	distance = abs(a - b) + x
	if distance >= n:
		distance = n - 1
	result += f'\n{distance}'
print(result.strip())


PosicionRival1 = int(input())
Datos = []
for i in range(PosicionRival1):
	Datos.append(list(map(int, input().split())))
for i in Datos:
	t = 0
	t = t + (i[2] - i[3])
	if t < 0:
		t = t * -1
	t = t + i[1]
	if t >= i[0]:
		t = 0
		t = t + i[0] - 1
	print(t)


a = int(input())

def ju(z, z1, z2, z3):
	x = abs(z2 - z3)
	if x + 1 <= z:
		if z1 == 0:
			return x
		v = x + z1
		if v + 1 <= z:
			return v
		else:
			return z - 1
	else:
		return x
for i in range(a):
	m_i = input()
	c_i = m_i.split()
	b_i = list(map(int, c_i))
	print(ju(b_i[0], b_i[1], b_i[2], b_i[3]))


import sys
for _ in range(int(input())):
	(n, x, a, b) = map(int, input().split())
	print(min(n - 1, abs(a - b) + x))


testCases = int(input())
for t in range(testCases):
	(numStudents, numSwaps, rivalA, rivalB) = tuple(map(int, input().split(' ')))
	initDistance = abs(rivalA - rivalB)
	maxMovement = numStudents - initDistance - 1
	maxFinalDistance = initDistance + min(maxMovement, numSwaps)
	print(maxFinalDistance)


t = int(input())
for _ in range(t):
	(n, x, a1, b1) = map(int, input().split())
	a = min(a1, b1)
	b = max(a1, b1)
	da = a
	db = n - b
	if db == 0 and da == 1:
		print(n - 1)
		continue
	if x >= da + db:
		print(n - 1)
		continue
	else:
		print(b - a + x)


ct = int(input())
for i in range(ct):
	(n, x, a, b) = map(int, input().split())
	if a > b:
		(a, b) = (b, a)
	na = max(1, a - x)
	x -= a - na
	nb = min(n, b + x)
	print(nb - na)


import sys
readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda : readline().rstrip()
ni = lambda : int(readline().rstrip())
nm = lambda : map(int, readline().split())
nl = lambda : list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
	(n, x, a, b) = nm()
	print(min(n - 1, abs(b - a) + x))
	return
T = ni()
for _ in range(T):
	solve()


def getMaxDistance(cases):
	results = []
	for (n, x, a, b) in cases:
		if b < a:
			(a, b) = (b, a)
		while x > 0 and a > 1:
			x -= 1
			a -= 1
		while x > 0 and b < n:
			x -= 1
			b += 1
		max_dst = abs(a - b)
		results.append(max_dst)
	return results
import sys
t = int(sys.stdin.readline())
cases = [list(map(int, sys.stdin.readline().split())) for _ in range(t)]
results = getMaxDistance(cases)
sys.stdout.write('\n'.join((str(x) for x in results)))


def main():
	for _ in range(int(input())):
		list = [int(x) for x in input().split()]
		print(min(list[0] - 1, abs(list[2] - list[3]) + list[1]))
main()


t = int(input())
for i in range(0, t):
	(n, x, a, b) = map(int, input().split())
	m = min(a, b)
	y = min(m - 1, x)
	m = m - y
	l = m
	x = x - y
	m = max(a, b)
	y = min(n - m, x)
	m = y + m
	u = m
	print(u - l)


n = int(input())
for i in range(n):
	(n, x, a, b) = list(map(int, input().split()))
	if b < a:
		(a, b) = (b, a)
	dist = b - a
	if a - 1 > 0 and a - 1 <= x:
		dist += a - 1
		x -= a - 1
	elif a - 1 > 0 and a - 1 > x:
		dist += x
		x = 0
	if n - b > 0 and n - b <= x:
		dist += n - b
		x -= n - b
	elif n - b > 0 and n - b > x:
		dist += x
		x = 0
	print(dist)


import math, sys
from collections import Counter, defaultdict, deque
from sys import stdin, stdout
input = stdin.readline
lili = lambda : list(map(int, sys.stdin.readlines()))
li = lambda : list(map(int, input().split()))
I = lambda : int(input())
S = lambda : input()
t = I()
for i in range(0, t):
	(n, x, a, b) = li()
	(a, b) = (min(a, b), max(a, b))
	c = a - x
	if a - x > 0:
		print(b - c)
	else:
		c = 1
		x = x - (a - 1)
		if b + x > n:
			print(n - c)
		else:
			b = b + x
			print(b - c)


t = int(input())
while t != 0:
	(n, x, a, b) = map(int, input().split())
	l = min(a, b)
	r = max(a, b)
	l -= min(l - 1, x)
	x -= min(min(a, b) - 1, x)
	r += min(n - r, x)
	print(abs(l - r))
	t -= 1


q = int(input())
for _ in range(q):
	(n, x, a, b) = list(map(int, input().split()))
	dist = abs(a - b) + x
	result = min(dist, n - 1)
	print(result)


t = int(input())
while t:
	(n, x, a, b) = map(int, input().split())
	if n == 2:
		print(1)
	elif x == 0:
		print(abs(a - b))
	elif a < b:
		while 1:
			if a != 1:
				a -= 1
				x -= 1
			if x == 0:
				break
			if b != n:
				b += 1
				x -= 1
			if x == 0:
				break
			if a == 1 and b == n:
				break
		print(abs(a - b))
	else:
		while 1:
			if a != n:
				a += 1
				x -= 1
			if x == 0:
				break
			if b != 1:
				b -= 1
				x -= 1
			if x == 0:
				break
			if a == n and b == 1:
				break
		print(abs(a - b))
	t -= 1


t = int(input())
for tt in range(t):
	(n, x, a, b) = map(int, input().split())
	if a < b:
		(a, b) = (b, a)
	p = b - x
	b = 1 if p < 2 else p
	if p < 1:
		p = abs(p)
		p = p + 1
		a = a + p if a + p < n else n
	print(abs(a - b))


t = int(input())
for x in range(t):
	(n, x, a, b) = map(int, input().split())
	(mmax, mmin) = (0, 0)
	if a > b:
		mmax = a
		mmin = b
	else:
		mmax = b
		mmin = a
	if n - mmax + (mmin - 1) <= x:
		print(n - 1)
	else:
		print(mmax - mmin + x)