'''
n = int(input())
a,b = map(int, input().split())
arr = list(map(int, input().split()))
'''
cases = int(input())
for case in range(cases):
n, x = map(int, input().split())
nums = list(map(int, input().split()))
diff = 0
for i in range(n-1):
diff += abs(nums[i] - nums[i+1])
min_val = min(nums)
max_val = max(nums)
if x > max_val:
diff += min((x-max_val)*2, x - max(nums[0], nums[-1]))
if min_val > 1:
diff += min((min_val-1)*2, min(nums[0], nums[-1])-1)
print(diff)
/*
key : if elements between max of array and min of array, we dont need to
consider
1) element less than min of array, find their position
2)element greater than max of array, find their position
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define MOD 998244353
#define inf (1LL<<60)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define uint unsigned long long
uint power(int x, int y, int p = MOD)
{
unsigned long long res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
uint modInverse(int n, int p = MOD)// using fermats little thm. [p needs to be prime which is mostly the case as mod value generally is 1e9+7]
{
return power(n, p - 2, p);
}
// =========================================Used to calculate nCr of higher values ===================================
uint nCr(int n, int r, int p = MOD) // faster calculation..
{
if (n < r)
return 0;
if (r == 0)
return 1;
vector<int> fac(n+1,0);
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = (fac[i - 1] * i) % p;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
void init_code()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
/*
think before you start to code, atleast test your approach on all tc
C - bs/dp/nCr/bitmasking/constructive(see ex)/brute/greedy/graphs
*/
const int N = 1e5;
int main()
{
fast_io;
init_code();
int tt;
cin >> tt;
while(tt--)
{
ll n, x;
cin >> n >> x;
vector<ll> v(n);
ll ans = 0;
for(int i = 0; i<n; i++)
{
cin >> v[i];
if(i != 0)
ans += abs(v[i] - v[i-1]);
}
ll least = *min_element(v.begin(), v.end());
ll greatest = *max_element(v.begin(), v.end());
if(1 < least)
least = 1;
if(x > greatest)
greatest = x;
ll cnt1 = min(abs(least - v[0]), abs(least - v[n-1])), cnt2 = min(abs(greatest - v[0]), abs(greatest - v[n-1]));
for(int i = 1; i<n; i++)
{
cnt1 = min(cnt1, abs(least - v[i]) + abs(least - v[i-1]) - abs(v[i] - v[i-1]));
}
for(int i = 1; i<n; i++)
{
cnt2 = min(cnt2, abs(greatest - v[i]) + abs(greatest - v[i-1]) - abs(v[i] - v[i-1]));
}
cout << ans + cnt1 + cnt2 << endl;
}
return 0;
}
550A - Two Substrings | 797B - Odd sum |
1093A - Dice Rolling | 1360B - Honest Coach |
1399C - Boats Competition | 1609C - Complex Market Analysis |
1657E - Star MST | 1143B - Nirvana |
1285A - Mezo Playing Zoma | 919B - Perfect Number |
894A - QAQ | 1551A - Polycarp and Coins |
313A - Ilya and Bank Account | 1469A - Regular Bracket Sequence |
919C - Seat Arrangements | 1634A - Reverse and Concatenate |
1619C - Wrong Addition | 1437A - Marketing Scheme |
1473B - String LCM | 1374A - Required Remainder |
1265E - Beautiful Mirrors | 1296A - Array with Odd Sum |
1385A - Three Pairwise Maximums | 911A - Nearest Minimums |
102B - Sum of Digits | 707A - Brain's Photos |
1331B - Limericks | 305B - Continued Fractions |
1165B - Polycarp Training | 1646C - Factorials and Powers of Two |