Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.

Alice just invited Bob to come over for dinner at her place. Bob is not dressed properly and he does not wish to take any chances, so he wants to rush to an apartment of one of his $N$ friends, change there and meet Alice for the dinner.

Alice and Bob's friends live in a skyscraper with many floors. Alice lives on the $a$-th floor, the apartments of Bob's friends are located on the floors $F_{1}, F_{2}, \ldots, F_{N}$ and Bob is initially at the $b$-th floor. It takes exactly $1$ minute to go one floor up or down from each floor.

Bob needs exactly $c$ minutes to change in any of his friends' apartments. Find the minimum time he needs to go to one of his friends' apartments, change and get to Alice's apartment.

------  Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains four space-separated integers $N$, $a$, $b$ and $c$.
The second line contains $N$ space-separated integers $F_{1}, F_{2}, \ldots, F_{N}$.

------  Output ------
Print a single line containing one integer ― the minimum time Bob needs.

------  Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$1 ≤ a, b, c ≤ 10^{9}$
$1 ≤ F_{i} ≤ 10^{9}$ for each valid $i$

------  Subtasks ------
Subtask #1 (100 points): original constraints

----- Sample Input 1 ------ 
2

3 1 5 2

6 7 8

1 1 2 1000000000

1000000000
----- Sample Output 1 ------ 
8

2999999997
----- explanation 1 ------ 
Example case 1: In the optimal solution, Bob goes to his friend at floor $6$, changes there and goes to meet Alice. The total time is $(6-5)$ to reach his friend plus $2$ to change plus $(6-1)$ to reach Alice, which is $1+2+5 = 8$ minutes.

Source: codechef | Difficulty: EASY
Tags: Complete search


Solutions:
t = int(input())
for ni in range(t):
	(n, a, b, c) = map(int, input().split())
	f = [int(i) for i in input().split()]
	l = [abs(b - i) + abs(i - a) for i in f]
	print(min(l) + c)


for _ in range(int(input())):
	(N, a, b, c) = map(int, input().split())
	F = list(map(int, input().split()))
	F.sort()
	if a < b:
		(a, b) = (b, a)
	ans = c
	for f in F:
		if b < f:
			ans += f - b + abs(a - f)
			break
	else:
		ans += abs(f - b) + abs(a - f)
	print(ans)


for _ in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	f = list(map(int, input().split()))
	ft = [abs(i - b) + abs(i - a) for i in f]
	tot = min(ft) + c
	print(tot)


t = int(input())
for t in range(t):
	(n, a, b, c) = [int(i) for i in input().split()]
	floor = [int(i) for i in input().split()]
	arr = [abs(f - b) + abs(f - a) for f in floor]
	s = min(arr)
	print(s + c)


for _ in range(int(input())):
	(N, A, B, C) = map(int, input().split())
	floor_list = list(map(int, input().split()))
	chk_list = [abs(fA - A) + abs(fA - B) for fA in floor_list]
	chk_list.sort()
	print(chk_list[0] + C)


t = input()
for x in range(0, int(t)):
	(n, a, b, c) = map(int, input().split())
	ar = [int(i) for i in input().split()]
	mini = 100000000000
	ans = 0
	for i in range(n):
		ans = abs(ar[i] - b) + abs(ar[i] - a) + c
		mini = min(mini, ans)
	print(mini)


tt = int(input())
while tt != 0:
	(n, a, b, c) = map(int, input().split())
	l = list(map(int, input().split()))
	t = 0
	x = 0
	for f in range(n):
		if l[f] >= a and l[f] <= b or (l[f] >= b and l[f] <= a):
			t = 0
			break
		else:
			s = min(abs(l[f] - a), abs(l[f] - b))
			if x == 0:
				x = 1
				t = s
			else:
				t = min(t, s)
	print(2 * t + c + abs(a - b))
	tt -= 1


for i in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	l = list(map(int, input().split()))
	k = []
	for i in l:
		k.append(c + abs(a - i) + abs(b - i))
	print(min(k))


for _ in range(int(input())):
	(n, a, b, d) = list(map(int, input().split()))
	list_f = list(map(int, input().split()))
	v = 10 ** 10
	for i in list_f:
		x = d + abs(a - i) + abs(b - i)
		if v > x:
			v = x
	print(v)


for _ in range(int(input())):
	(N, a, b, c) = map(int, input().split())
	f = list(map(int, input().split()))
	l = []
	for i in f:
		l.append(abs(i - b) + c + abs(i - a))
	print(min(l))


import math
t = int(input(''))
if 1 <= t <= 10:
	for _ in range(t):
		(N, a, b, c) = map(int, input().split())
		f = input().split()
		if len(f) == N and 1 <= N <= 10 ** 5:
			for i in range(N):
				f[i] = math.fabs(int(f[i]) - b) + math.fabs(int(f[i]) - a)
			print(int(min(f)) + c)


casos = int(input())
for i in range(casos):
	(nro_amigos, piso_Alice, piso_bob, minutos) = map(int, input().split())
	pisos_disponibles = [int(x) for x in input().split()]
	lista = []
	for j in pisos_disponibles:
		lista.append(abs(piso_bob - j) + minutos + abs(j - piso_Alice))
	print(min(lista))


import sys
x = int(input())
for i in range(x):
	k = input().split()
	n = int(k[0])
	p = int(k[1])
	q = int(k[2])
	c = int(k[3])
	ls = list(map(int, input().split()))
	x2 = sys.maxsize
	for j in ls:
		x1 = abs(p - j) + abs(q - j) + c
		x2 = min(x1, x2)
	print(x2)


for _ in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	f = list(map(int, input().split()))
	m = []
	for i in range(n):
		m.append(abs(b - f[i]) + abs(a - f[i]))
	r = m.index(min(m))
	t = abs(b - f[r]) + c + abs(a - f[r])
	print(t)


def bob(n, a, b, c, x):
	min = abs(b - x[0]) + c + abs(x[0] - a)
	for i in range(1, n):
		if min > abs(b - x[i]) + c + abs(x[i] - a):
			min = abs(b - x[i]) + c + abs(x[i] - a)
	return print(min)
k = int(input())
while k:
	arr = list(map(int, input().rstrip().split()))
	x = list(map(int, input().rstrip().split()))
	bob(arr[0], arr[1], arr[2], arr[3], x)
	k -= 1


for _ in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	f = list(map(int, input().split()))
	minimum = 10000000000.0
	ans = 0
	for i in range(n):
		ans = abs(f[i] - b) + abs(f[i] - a) + c
		minimum = min(minimum, ans)
	print(minimum)


def nearest(l, a, b):
	x = l[0]
	diff = abs(a - x) + abs(b - x)
	for i in l:
		if abs(a - i) + abs(b - i) < diff:
			x = i
			diff = abs(a - i) + abs(b - i)
	return diff
for M in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	l = list(map(int, input().split()))
	temp = nearest(l, a, b)
	print(temp + c)


for _ in range(int(input())):
	(N, A, B, C) = map(int, input().split())
	F = list(map(int, input().split()))
	G = list(filter(lambda x: x > B, F))
	S = list(filter(lambda x: x < B, F))
	T = [C, C]
	if B in F:
		print(C + abs(A - B))
	elif G == []:
		print(abs(max(S) - B) + abs(A - max(S)) + C)
	elif S == []:
		print(abs(min(G) - B) + abs(A - min(G)) + C)
	else:
		T[0] += abs(min(G) - B) + abs(A - min(G))
		T[1] += abs(max(S) - B) + abs(A - max(S))
		print(min(T))


T = int(input())
for _ in range(T):
	(n, a, b, c) = map(int, input().split())
	L = list(map(int, input().split()))
	found = False
	for i in range(n):
		if L[i] < b and L[i] > a or (L[i] > b and L[i] < a):
			found = True
			break
	if found:
		print(c + abs(a - b))
	else:
		M = L.copy()
		for i in range(n):
			L[i] = abs(b - L[i])
		k = min(L)
		b = L.index(k)
		print(c + k + abs(M[b] - a))


for _ in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	l = list(map(int, input().split()))
	m = 2 * max(l) + a + b + c
	for i in l:
		if abs(b - i) + c + abs(i - a) < m:
			m = abs(b - i) + c + abs(i - a)
	print(m)


t = int(input())
for i in range(t):
	(N, a, b, c) = map(int, input().split())
	f = list(map(int, input().split()))
	m = 2 * max(f) + c + a + b
	check = False
	for j in f:
		if j == a or j == b or (j > a and j < b) or (j < a and j > b):
			print(abs(b - a) + c)
			check = True
			break
		else:
			time = abs(j - a) + abs(j - b) + c
			if time < m:
				m = time
	if not check:
		print(m)


t = int(input())
for tc in range(t):
	(N, a, b, c) = map(int, input().split(' '))
	friend = list(map(int, input().split(' ')))
	mini = 2 * max(friend) + c + a + b
	chk = False
	for fr in friend:
		if fr == a or fr == b or (fr > a and fr < b) or (fr < a and fr > b):
			print(abs(b - a) + c)
			chk = True
			break
		else:
			time = abs(fr - a) + abs(fr - b) + c
			if time < mini:
				mini = time
	if not chk:
		print(mini)


times = []
test_cases = int(input())
for _ in range(test_cases):
	(size, final, start, timeTakenAtRoom) = map(int, input().split())
	nums = set(map(int, input().split()))
	min_time = None
	for num in nums:
		x = abs(final - num) + abs(num - start) + timeTakenAtRoom
		if min_time == None:
			min_time = x
		elif x < min_time:
			min_time = x
	times.append(min_time)
for num in times:
	print(num)


import collections
from math import log2, log10, ceil

def pow2(n):
	t = 0
	while not n & 1:
		n = n // 2
		t += 1
	return t

def solve():
	(n, a, b, c) = map(int, input().split())
	f = list(map(int, input().split()))
	times = []
	for fi in f:
		times.append(abs(fi - b) + abs(fi - a) + c)
	times.sort()
	print(times[0])

def main():
	t = int(input())
	while t:
		t -= 1
		solve()
main()


import sys
t = int(input())
for i in range(t):
	s = input().split()
	n = int(s[0])
	a = int(s[1])
	b = int(s[2])
	c = int(s[3])
	l = list(map(int, input().split()))
	t2 = sys.maxsize
	for j in l:
		t1 = abs(a - j) + abs(b - j) + c
		t2 = min(t1, t2)
	print(t2)


T = int(input())
for i in range(T):
	(N, a, b, c) = map(int, input().split())
	lst = list(map(int, input().split()))
	least = 1000000000000000
	for i in lst:
		if least > abs(b - i) + abs(a - i):
			least = abs(b - i) + abs(a - i)
	time = least + c
	print(time)


from collections import defaultdict, Counter
from sys import stdin, stdout, setrecursionlimit
from math import *
from bisect import bisect_left, bisect_right
from statistics import mode
from functools import reduce
import operator as op
I = lambda : stdin.readline().rstrip('\r\n')
ari = lambda : list(map(int, I().split()))
ars = lambda : list(map(str, I().split()))
arf = lambda : list(map(float, I().split()))
mpi = lambda : map(int, I().split())
mps = lambda : map(str, I().split())
mpf = lambda : map(float, I().split())
mod = 10 ** 9 + 7
setrecursionlimit(10 ** 8)
inf = float('inf')

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

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	for i in range(5, ceil(sqrt(n)) + 1, 6):
		if n % i == 0 or n % (i + 2) == 0:
			return False
	return True

def nearest2(num):
	return int(pow(2, int(log(num, 2))))

def comb(n, r):
	r = min(r, n - r)
	if r < 0:
		return 0
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def func(N, a, b, c, F):
	(lf, rf) = (bisect_left(F, b) - 1, bisect_left(F, b))
	(mn1, mn2) = (inf, inf)
	if lf != -1:
		mn1 = abs(F[lf] - b) + c + abs(F[lf] - a)
	mn2 = abs(F[rf] - b) + c + abs(F[rf] - a)
	return min(mn1, mn2)
for t in range(int(I())):
	(N, a, b, c) = map(int, I().split())
	F = sorted(list(map(int, I().split())))
	print(func(N, a, b, c, F))


import sys
for _ in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	l = list(map(int, input().split()))
	s = l[0]
	m = sys.maxsize
	for i in l:
		ans = abs(i - a) + abs(i - b) + c
		if ans < m:
			m = ans
	print(m)


import sys
t = int(input())
for _ in range(t):
	(n, a, b, c) = map(int, input().split())
	ans = sys.maxsize
	f = list(map(int, input().split()))
	for i in f:
		temp = abs(a - i) + abs(b - i) + c
		ans = min(ans, temp)
	print(ans)


PI = 3.141592653589793
INF = float('inf')
MOD = 1000000007

def add(x, y):
	return (x + y) % MOD

def sub(x, y):
	return (x - y + MOD) % MOD

def mul(x, y):
	return x * y % MOD

def gcd(x, y):
	if y == 0:
		return x
	return gcd(y, x % y)

def lcm(x, y):
	return x * y // gcd(x, y)

def power(x, y):
	res = 1
	x %= MOD
	while y != 0:
		if y & 1:
			res = mul(res, x)
		y >>= 1
		x = mul(x, x)
	return res

def mod_inv(n):
	return power(n, MOD - 2)

def prob(p, q):
	return mul(p, power(q, MOD - 2))

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

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

def ls():
	return [i for i in input().split()]
for t in range(ii()):
	t += 1
	(n, a, b, c) = li()
	f = li()
	ans = INF
	for i in f:
		tim = abs(i - a) + abs(i - b) + c
		ans = min(ans, tim)
	print(ans)


for i in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	l1 = list(map(int, input().split()))
	numli = []
	u = 0
	for i in range(n):
		numli.append(abs(a - l1[i]) + abs(b - l1[i]))
	m = min(numli)
	u = m + c
	print(u)


test_case = int(input())
while test_case != 0:
	test_case -= 1
	(n, a, b, c) = map(int, input().split())
	f = list(map(int, input().split()))
	ans = abs(b - f[0]) + c + abs(a - f[0])
	for i in range(0, n):
		temp = abs(b - f[i]) + c + abs(a - f[i])
		if temp < ans:
			ans = temp
	print(ans)


a = int(input())
for i in range(a):
	(n, a, b, c) = map(int, input().split())
	f = list(map(int, input().split()))
	e = []
	for j in f:
		d = abs(j - b) + c + abs(j - a)
		e.append(d)
	print(min(e))


for _ in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	l1 = list(map(int, input().split()))
	l2 = []
	for i in l1:
		p = abs(i - b) + c + abs(i - a)
		l2.append(p)
	print(min(l2))


for i in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	l = list(map(int, input().split()))
	l2 = []
	total = 0
	for j in l:
		d = abs(b - j) + abs(j - a)
		l2.append(d)
	total = min(l2) + c
	print(total)


T = int(input())
for i in range(0, T):
	(n, a, b, c) = map(int, input().split(' '))
	f = list(map(int, input().split(' ')))
	k = []
	l = []
	f.sort()
	for i in range(0, len(f)):
		m = abs(f[i] - a)
		o = abs(f[i] - b)
		k.append(m + o + c)
	print(min(k))


t = int(input())
for i in range(t):
	(n, a, b, c) = map(int, input().split())
	li = list(map(int, input().split()))
	nli = []
	u = 0
	for i in range(n):
		nli.append(abs(a - li[i]) + abs(b - li[i]))
	mi = min(nli)
	u = mi + c
	print(u)


try:
	t = int(input())
	for i in range(0, t):
		(n, a, b, c) = map(int, input().split(' '))
		f = list(map(int, input().split(' ')))
		k = []
		l = []
		f.sort()
		for i in range(0, len(f)):
			m = abs(f[i] - a)
			o = abs(f[i] - b)
			k.append(m + o + c)
		print(min(k))
except:
	pass


for t in range(int(input())):
	(N, a, b, c) = map(int, input().split())
	F = list(map(int, input().split()))
	bb = []
	al = []
	for i in range(len(F)):
		bb.append(abs(b - F[i]))
		al.append(abs(a - F[i]))
	dist = min([bb[i] + al[i] for i in range(len(bb))])
	print(dist + c)


for t in range(int(input())):
	(n, a, b, c) = [int(x) for x in input().rstrip().split()]
	floor = [int(x) for x in input().rstrip().split()]
	floor.sort()
	min = abs(b - floor[0]) + c + abs(floor[0] - a)
	for i in range(0, n):
		s = abs(b - floor[i]) + c + abs(floor[i] - a)
		if s < min:
			min = s
	print(min)


def codechef(n, a, b, c, floor):
	diff_lis = []
	time = abs(b - a) + c
	if a >= b:
		x1 = a
		x2 = b
	elif b > a:
		x1 = b
		x2 = a
	for i in floor:
		if x2 < i < x1:
			return time
	for i in range(len(floor)):
		if floor[i] >= x1:
			diff_lis.append(floor[i] - x1)
		elif floor[i] <= x2:
			diff_lis.append(x2 - floor[i])
	time = time + 2 * min(diff_lis)
	return time
t = int(input())
while t > 0:
	(n, a, b, c) = list(map(int, input().split()))
	floor = list(map(int, input().split()))
	print(codechef(n, a, b, c, floor))
	t = t - 1


try:
	for _ in range(int(input())):
		(n, a, b, c) = map(int, input().split(' '))
		fl = [int(x) for x in input().split(' ')[:n]]
		ans = c
		fa = [abs(x - a) + abs(x - b) for x in fl]
		print(c + min(fa))
except EOFError:
	pass


for _ in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	f = list(map(int, input().split()))
	ans = float('inf')
	for i in range(n):
		min_time = abs(f[i] - b) + c + abs(a - f[i])
		if min_time < ans:
			ans = min_time
	print(ans)


T = int(input())
ans = []
for _ in range(T):
	(N, a, b, c) = [int(i) for i in input().split()]
	F = [int(i) for i in input().split()]
	m = float('inf')
	for i in range(N):
		x = abs(F[i] - b) + abs(a - F[i]) + c
		if x < m:
			m = x
	ans.append(m)
for i in ans:
	print(i)


import sys
import math

def main(arr, a, b, c):
	ans = float('inf')
	for i in range(len(arr)):
		e = arr[i]
		if abs(b - e) + c + abs(e - a) < ans:
			ans = abs(b - e) + c + abs(e - a)
	return ans
for _ in range(int(input())):
	(n, a, b, c) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	print(main(arr, a, b, c))


from sys import maxsize, stdout, stdin, stderr
mod = int(1000000000.0 + 7)

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

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

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

def S():
	return input().strip()

def grid(r, c):
	return [lint() for i in range(r)]

def debug(*args, c=6):
	print('\x1b[3{}m'.format(c), *args, '\x1b[0m', file=stderr)
from _collections import defaultdict
for _ in range(I()):
	(n, a, b, c) = tup()
	minn = maxsize
	ls = lint()
	f = -1
	for i in range(n):
		x = abs(ls[i] - b) + c + abs(ls[i] - a)
		if x < minn:
			minn = x
	print(minn)


for o in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	l = list(map(int, input().split()))
	m = 99999999999999
	for i in l:
		x = abs(b - i) + c + abs(i - a)
		if x < m:
			m = x
	print(m)


for h in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	l = list(map(int, input().split()))
	mini = abs(a - l[0]) + abs(b - l[0]) + c
	for i in range(1, n):
		k = abs(a - l[i]) + abs(b - l[i]) + c
		if k < mini:
			mini = k
	print(mini)


def abs(a, b):
	if a > b:
		return a - b
	else:
		return b - a

def minimum(a, b):
	if a < b:
		return a
	else:
		return b
test_cases = int(input())
for _ in range(test_cases):
	(N, a, b, c) = map(int, input().split())
	F = list(map(int, input().split()))
	min = 10 ** 9
	for x in F:
		if (b - x) * (a - x) <= 0:
			print(abs(a, b) + c)
			break
		else:
			p = abs(a, x)
			q = abs(b, x)
			if minimum(p, q) < min:
				min = minimum(p, q)
	else:
		print(2 * min + c + abs(a, b))


import math
n = int(input())
for i in range(n):
	l = []
	(N, a, b, c) = [int(i) for i in input().split()]
	f = [int(i) for i in input().split()]
	for j in range(0, len(f)):
		l.append(abs(b - f[j]) + c + abs(f[j] - a))
	print(min(l))


for _ in range(int(input())):
	(N, a, b, c) = map(int, input().split())
	F = list(map(int, input().split()))
	arr = []
	for i in range(len(F)):
		sum = 0
		sum = abs(a - F[i]) + c + abs(b - F[i])
		arr.append(sum)
	print(min(arr))


for _ in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	arr = list(map(int, input().split()))
	count = 20 ** 9
	for i in range(n):
		if arr[i] > a and arr[i] < b or (arr[i] < a and arr[i] > b):
			if abs(a - b) < count:
				count = abs(a - b)
		elif abs(arr[i] - a) + abs(arr[i] - b) < count:
			count = abs(arr[i] - a) + abs(arr[i] - b)
	print(count + c)


for T in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	f = list(map(int, input().split()))
	f.sort()
	for (i, floor) in enumerate(f):
		if floor >= b:
			prev_floor_no = f[i - 1]
			floor_no = floor
			break
	time1 = abs(floor_no - b) + abs(floor_no - a) + c
	time2 = abs(prev_floor_no - b) + abs(prev_floor_no - a) + c
	print(min(time1, time2))


def mindist(a, b, F):
	min = 2000000000
	for val in F:
		curr = abs(a - val) + abs(b - val)
		if curr < min:
			min = curr
	return min
T = int(input())
for i in range(T):
	(N, a, b, c) = map(int, input().split(' '))
	F = list(map(int, input().split(' ')))
	m = mindist(a, b, F)
	print(m + c)


t = int(input())
for i in range(t):
	(n, a, b, c) = map(int, input().split())
	f = list(map(int, input().split()))
	list1 = []
	for i in range(n):
		min1 = 0
		min1 = abs(b - f[i]) + abs(a - f[i])
		list1.append(min1)
	print(min(list1) + c)


def solve():
	(n, a, b, c) = list(map(int, input().split()))
	friend = list(map(int, input().split()))
	ans = float('inf')
	for fr in friend:
		temp = abs(fr - a) + abs(fr - b) + c
		ans = min(ans, temp)
	print(ans)
t = int(input())
while t != 0:
	solve()
	t -= 1


t = int(input())
for _ in range(t):
	(n, a, b, c) = map(int, input().split())
	l = list(map(int, input().split()))
	p = float('inf')
	for i in l:
		x = abs(a - i) + abs(b - i) + c
		p = min(p, x)
	print(p)


for i in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	f = list(map(int, input().split()))
	f.append(a)
	f.sort()
	x = f.index(a)
	if x == 0:
		ans = abs(b - f[1]) + (f[1] - a) + c
	elif x == n:
		ans = abs(b - f[n - 1]) + (a - f[n - 1]) + c
	else:
		i = abs(b - f[x - 1]) + (a - f[x - 1]) + c
		j = abs(b - f[x + 1]) + (f[x + 1] - a) + c
		ans = min(i, j)
	print(ans)


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

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

def si():
	return input()

def mi():
	return map(int, input().split())
for _ in range(ii()):
	(n, a, b, c) = mi()
	f = li()
	time = 0
	mn = 10 ** 9 + 1
	for i in range(n):
		if f[i] in range(a, b) or f[i] in range(b, a):
			print(abs(a - b) + c)
			break
		else:
			if abs(a - f[i]) < mn:
				mn = abs(a - f[i])
			if abs(b - f[i]) < mn:
				mn = abs(b - f[i])
	else:
		print(2 * mn + c + abs(a - b))


from sys import stdin, maxsize
try:
	t = eval(stdin.readline())
	for _ in range(t):
		(n, a, b, c) = [int(x) for x in stdin.readline().split()]
		arr = [int(x) for x in stdin.readline().split()]
		minDist = maxsize
		chosenAp = -1
		for i in range(n):
			cur = abs(arr[i] - b) + abs(arr[i] - a)
			if cur < minDist:
				minDist = cur
		answer = c + minDist
		print(answer)
except:
	pass


def binserach(a, aa, b, i, j, valid, ans):
	while i <= j:
		mid = (i + j) // 2
		if a[mid] > aa and a[mid] < b or (a[mid] < aa and a[mid] > b):
			return 0
		g = abs(a[mid] - b)
		if a[mid] > b:
			if g < ans:
				valid = []
				valid.append(mid)
				ans = g
			elif g == ans:
				valid.append(mid)
			j = mid - 1
		else:
			if g < ans:
				valid = []
				valid.append(mid)
				ans = g
			elif g == ans:
				valid.append(mid)
			i = mid + 1
	return valid
for _ in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	floor = list(map(int, input().split()))
	floor.sort()
	ind = binserach(floor, a, b, 0, len(floor) - 1, [], 1000000000000)
	f = abs(a - b)
	f = f + c
	if ind == 0:
		print(f)
	else:
		print(f + 2 * abs(floor[ind[0]] - b))


inp = lambda : map(int, input().split())
for _ in range(int(input())):
	(N, a, b, c) = inp()
	seq = list(inp())
	minn = float('inf')
	if a > b:
		(b, a) = (a, b)
	for i in range(N):
		if seq[i] < a:
			v = a - seq[i]
			if v < minn:
				minn = v
		elif seq[i] > b:
			v = seq[i] - b
			if v < minn:
				minn = v
		else:
			print(b - a + c)
			break
	else:
		print(b - a + minn * 2 + c)


t = int(input())
for i in range(0, t):
	(n, a, b, c) = map(int, input().split(' '))
	if n == 1:
		l = []
		l.append(int(input()))
	else:
		l = list(map(int, input().split(' ')))
	mini = 100000000000
	for j in range(0, n):
		mini = min(abs(l[j] - b) + abs(l[j] - a) + c, mini)
	print(mini)


t = int(input())
while t > 0:
	(n, a, b, c) = map(int, input().split())
	x = list(map(int, input().split()))
	y = []
	for i in range(n):
		val = abs(x[i] - b) + abs(x[i] - a)
		y.append(val)
	print(min(y) + c)
	t = t - 1


for _ in range(int(input())):
	(N, A, B, C) = map(int, input().rstrip().split())
	FriendFLoor = list(map(int, input().split()))
	time = []
	for elem in FriendFLoor:
		time.append(abs(B - elem) + abs(A - elem) + C)
	print(min(time))


t = int(input())
if t >= 1 and t <= 10:
	for i in range(t):
		s = list(map(int, input().strip().split(' ')))
		(n, a, b, c) = (s[0], s[1], s[2], s[3])
		f = list(map(int, input().strip().split(' ')))
		p = []
		for i in range(n):
			p.append(abs(b - f[i]) + abs(a - f[i]) + c)
		print(min(p))
else:
	print('Invalid input')


t = int(input())
for i in range(t):
	(n, a, b, c) = map(int, input().split())
	lst = [int(x) for x in input().split()]
	b1 = []
	for i in range(n):
		c2 = abs(lst[i] - a) + abs(lst[i] - b) + c
		b1.append(c2)
	print(min(b1))


import math
for _ in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	l = list(map(int, input().split()))
	t = math.inf
	for i in range(len(l)):
		t_1 = abs(l[i] - a) + abs(l[i] - b)
		if t > t_1:
			t = t_1
	t += c
	print(t)


try:
	t = int(input())
	while t:
		t = t - 1
		(n, a, b, c) = map(int, input().split())
		f = list(map(int, input().split()))
		s = 9999999999
		for i in range(n):
			x = abs(a - f[i]) + abs(b - f[i])
			if x < s:
				s = x
		print(s + c)
except:
	pass


for _ in range(int(input())):
	(n, a, b, c) = map(int, input().split())
	ar = list(map(int, input().split()))
	ans1 = None
	for i in ar:
		ans = abs(b - i) + abs(a - i) + c
		if ans1 is None:
			ans1 = ans
		elif ans < ans1:
			ans1 = ans
	print(ans1)


t = int(input())
for _ in range(t):
	(n, a, b, c) = map(int, input().split())
	N = list(map(int, input().split()))
	N.append(b)
	N.sort()
	inu = N.index(b)
	if a > b:
		if inu + 1 < n + 1:
			print(abs(N[inu + 1] - b) + c + abs(a - N[inu + 1]))
		else:
			print(abs(N[inu - 1] - b) + c + abs(a - N[inu - 1]))
	elif inu - 1 >= 0:
		print(abs(N[inu - 1] - b) + c + abs(a - N[inu - 1]))
	else:
		print(abs(N[inu + 1] - b) + c + abs(a - N[inu + 1]))