Gildong has an interesting machine that has an array $a$ with $n$ integers. The machine supports two kinds of operations:

Increase all elements of a suffix of the array by $1$.

Decrease all elements of a suffix of the array by $1$.

A suffix is a subsegment (contiguous elements) of the array that contains $a_n$. In other words, for all $i$ where $a_i$ is included in the subsegment, all $a_j$'s where $i \lt j \le n$ must also be included in the subsegment.

Gildong wants to make all elements of $a$ equal — he will always do so using the minimum number of operations necessary. To make his life even easier, before Gildong starts using the machine, you have the option of changing one of the integers in the array to any other integer. You are allowed to leave the array unchanged. You want to minimize the number of operations Gildong performs. With your help, what is the minimum number of operations Gildong will perform?

Note that even if you change one of the integers in the array, you should not count that as one of the operations because Gildong did not perform it.


-----Input-----

Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$).

Each test case contains two lines. The first line of each test case consists of an integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of elements of the array $a$.

The second line of each test case contains $n$ integers. The $i$-th integer is $a_i$ ($-5 \cdot 10^8 \le a_i \le 5 \cdot 10^8$).

It is guaranteed that the sum of $n$ in all test cases does not exceed $2 \cdot 10^5$.


-----Output-----

For each test case, print one integer — the minimum number of operations Gildong has to perform in order to make all elements of the array equal.


-----Examples-----

Input
7
2
1 1
3
-1 0 2
4
99 96 97 95
4
-3 -5 -2 1
6
1 4 3 2 4 1
5
5 0 0 0 5
9
-367741579 319422997 -415264583 -125558838 -300860379 420848004 294512916 -383235489 425814447
Output
0
1
3
4
6
5
2847372102


-----Note-----

In the first case, all elements of the array are already equal. Therefore, we do not change any integer and Gildong will perform zero operations.

In the second case, we can set $a_3$ to be $0$, so that the array becomes $[-1,0,0]$. Now Gildong can use the $2$-nd operation once on the suffix starting at $a_2$, which means $a_2$ and $a_3$ are decreased by $1$, making all elements of the array $-1$.

In the third case, we can set $a_1$ to $96$, so that the array becomes $[96,96,97,95]$. Now Gildong needs to:

Use the $2$-nd operation on the suffix starting at $a_3$ once, making the array $[96,96,96,94]$.

Use the $1$-st operation on the suffix starting at $a_4$ $2$ times, making the array $[96,96,96,96]$.

In the fourth case, we can change the array into $[-3,-3,-2,1]$. Now Gildong needs to:

Use the $2$-nd operation on the suffix starting at $a_4$ $3$ times, making the array $[-3,-3,-2,-2]$.

Use the $2$-nd operation on the suffix starting at $a_3$ once, making the array $[-3,-3,-3,-3]$.

Difficulty: MEDIUM
Tags: Implementation

Constructive algorithms

Starter code:

Solutions:
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	tot = 0
	for i in range(1, n):
		tot += abs(arr[i] - arr[i - 1])
	ans = min(tot - abs(arr[0] - arr[1]), tot - abs(arr[n - 1] - arr[n - 2]))
	for i in range(1, n - 1):
		ans = min(ans, tot - abs(arr[i] - arr[i - 1]) - abs(arr[i] - arr[i + 1]) + abs(arr[i - 1] - arr[i + 1]))
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = 0
	best = max(abs(a[-1] - a[-2]), abs(a[1] - a[0]))
	for i in range(n - 2, -1, -1):
		m += abs(a[i + 1] - a[i])
		if i + 2 < n:
			best = max(best, -abs(a[i] - a[i + 2]) + (abs(a[i] - a[i + 1]) + abs(a[i + 1] - a[i + 2])))
	print(m - best)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	for i in range(n - 1):
		ans += abs(a[i] - a[i + 1])
	c = 0
	c = max(abs(a[1] - a[0]), c)
	c = max(c, abs(a[-1] - a[-2]))
	for i in range(1, n - 1):
		c = max(c, abs(a[i] - a[i + 1]) + abs(a[i] - a[i - 1]) - abs(a[i - 1] - a[i + 1]))
	print(ans - c)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
t = int(input())

def findCost(org_cost, arr):
	n = len(arr)
	min_cost = org_cost
	for i in range(n - 1):
		cost = org_cost
		cost -= abs(arr[i + 1] - arr[i])
		if i + 2 < n:
			cost -= abs(arr[i + 2] - arr[i + 1])
			cost += abs(arr[i + 2] - arr[i])
		min_cost = min(min_cost, cost)
	return min_cost
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	diff = [0] * (n - 1)
	for i in reversed(range(n - 1)):
		diff[i] = arr[i + 1] - arr[i]
	org_cost = 0
	for el in diff:
		org_cost += abs(el)
	cost1 = findCost(org_cost, arr)
	cost2 = findCost(org_cost, arr[::-1])
	print(min(cost1, cost2))


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import ceil, floor
from copy import *
from collections import deque, defaultdict
from collections import Counter as counter
from itertools import combinations
from itertools import permutations as permutate
from bisect import bisect_left as bl
from operator import *
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def pow(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
from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def gcd(a, b):
	if a == b:
		return a
	while b > 0:
		(a, b) = (b, a % b)
	return a

def prefix_sum(ar):
	return list(accumulate(ar))

def suffix_sum(ar):
	return list(accumulate(ar[::-1]))[::-1]

def N():
	return int(inp())
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]

def YES():
	print('YES')

def NO():
	print('NO')

def Yes():
	print('Yes')

def No():
	print('No')
from collections import defaultdict

def numberOfSetBits(i):
	i = i - (i >> 1 & 1431655765)
	i = (i & 858993459) + (i >> 2 & 858993459)
	return ((i + (i >> 4) & 252645135) * 16843009 & 4294967295) >> 24

class MergeFind:

	def __init__(self, n):
		self.parent = list(range(n))
		self.size = [1] * n
		self.num_sets = n

	def find(self, a):
		to_update = []
		while a != self.parent[a]:
			to_update.append(a)
			a = self.parent[a]
		for b in to_update:
			self.parent[b] = a
		return self.parent[a]

	def merge(self, a, b):
		a = self.find(a)
		b = self.find(b)
		if a == b:
			return
		if self.size[a] < self.size[b]:
			(a, b) = (b, a)
		self.num_sets -= 1
		self.parent[b] = a
		self.size[a] += self.size[b]

	def set_size(self, a):
		return self.size[self.find(a)]

	def __len__(self):
		return self.num_sets

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

def solve():
	n = N()
	ar = lis()
	temp = []
	for i in range(1, n):
		temp.append(ar[i] - ar[i - 1])
	temp = temp
	m = max(abs(temp[0]), abs(temp[-1]))
	for i in range(1, len(temp)):
		a = temp[i] * temp[i - 1]
		if a >= 0:
			continue
		m = max(m, 0, 2 * min(abs(temp[i]), abs(temp[i - 1])))
	su = 0
	for i in temp:
		su += abs(i)
	print(su - m)
testcase(int(inp()))


from collections import defaultdict, deque
import sys
import bisect
import math
input = sys.stdin.readline
mod = 1000000007
t = int(input())
for ii in range(t):
	n = int(input())
	one = [int(i) for i in input().split() if i != '\n']
	diff = []
	for i in range(1, n):
		diff.append(abs(one[i] - one[i - 1]))
	tmp = []
	for i in range(n):
		if i == 0:
			tmp.append(diff[0])
		elif i == n - 1:
			tmp.append(diff[-1])
		else:
			tmp.append(diff[i - 1] + diff[i] - abs(one[i - 1] - one[i + 1]))
	print(sum(diff) - max(tmp))


import math

def main():
	n = int(input())
	lst = list(map(int, input().split()))
	ind = 0
	dif = 0
	for i in range(n):
		cur_df = 0
		if i == 0:
			cur_df = abs(lst[i + 1] - lst[i])
		elif i == n - 1:
			cur_df = abs(lst[i - 1] - lst[i])
		elif lst[i + 1] > lst[i] and lst[i - 1] > lst[i] or (lst[i + 1] < lst[i] and lst[i - 1] < lst[i]):
			cur_df = 2 * min(abs(lst[i + 1] - lst[i]), abs(lst[i - 1] - lst[i]))
		else:
			cur_df = 0
		if cur_df > dif:
			dif = cur_df
			ind = i
	if ind == 0:
		lst[0] = lst[1]
	elif ind == n - 1:
		lst[-1] = lst[-2]
	elif lst[ind] > lst[ind + 1]:
		if lst[ind + 1] > lst[ind - 1]:
			lst[ind] = lst[ind - 1]
		else:
			lst[ind] = lst[ind + 1]
	elif lst[ind + 1] < lst[ind - 1]:
		lst[ind] = lst[ind - 1]
	else:
		lst[ind] = lst[ind + 1]
	dif = 0
	for i in range(n - 1, 0, -1):
		dif += abs(lst[i] - lst[i - 1])
	print(dif)
t = int(input())
for i in range(t):
	main()


N = int(200000.0)
a = [0 for i in range(N + 5)]

def solve():
	t = int(input())
	for tc in range(t):
		n = int(input())
		b = input().split()
		for i in range(n):
			a[i] = int(b[i])
		(ans, s) = (0, 0)
		for i in range(n - 1):
			s += abs(a[i] - a[i + 1])
		ans = s - max(abs(a[0] - a[1]), abs(a[n - 2] - a[n - 1]))
		for i in range(1, n - 1):
			v = abs(a[i] - a[i - 1]) + abs(a[i] - a[i + 1])
			ans = min(ans, s - v + abs(a[i + 1] - a[i - 1]))
		print(ans)
solve()


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ans = abs(l[0] - l[1])
	mx = 0
	an = 0
	for i in range(1, n - 1):
		an += abs(l[i] - l[i - 1])
		if ans < abs(l[i] - l[i - 1]) + abs(l[i + 1] - l[i]) - abs(l[i - 1] - l[i + 1]):
			ans = max(ans, abs(l[i] - l[i - 1]) + abs(l[i + 1] - l[i]) - abs(l[i - 1] - l[i + 1]))
			mx = i
	an += abs(l[n - 1] - l[n - 2])
	if ans < abs(l[n - 1] - l[n - 2]):
		ans = max(ans, abs(l[n - 1] - l[n - 2]))
		mx = n - 1
	print(an - ans)


import sys
import math
import random
from queue import PriorityQueue as PQ
from bisect import bisect_left as BSL
from bisect import bisect_right as BSR
from collections import OrderedDict as OD
from collections import Counter
from itertools import permutations
from decimal import Decimal as BIGFLOAT
mod = 1000000007
MOD = mod
try:
	sys.stdin = open('actext.txt', 'r')
	OPENFILE = 1
except:
	pass

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

def palindrome(s):
	mid = len(s) // 2
	for i in range(mid):
		if s[i] != s[len(s) - i - 1]:
			return False
	return True

def check(i, n):
	if 0 <= i < n:
		return True
	else:
		return False
t = int(input())
for tt in range(t):
	n = int(input())
	arr = list(get_ints())
	ans1 = max(abs(arr[n - 1] - arr[n - 2]), abs(arr[1] - arr[0]))
	totalcost = abs(arr[1] - arr[0])
	for i in range(n - 2, 0, -1):
		a = abs(arr[i + 1] - arr[i])
		b = abs(arr[i] - arr[i - 1])
		c = abs(arr[i + 1] - arr[i - 1])
		ans1 = max(ans1, abs(c - (a + b)))
		totalcost += a
	print(totalcost - ans1)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
from itertools import permutations, combinations
mod = int(1000000000.0) + 7
mod = 998244353
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
out = lambda x: stdout.write(str(x) + '\n')
t = ip()
for _ in range(t):
	n = ip()
	arr = list(inp())
	ele = arr[0]
	ans1 = 0
	st = 0
	for i in range(1, n):
		val = arr[i] + st
		diff = ele - val
		ans1 += abs(diff)
		st += diff
	ch = []
	for i in range(1, n):
		ch.append(abs(arr[i] - arr[i - 1]))
	ref = sum(ch)
	ans = ref
	for i in range(n):
		if i == 0:
			init = abs(arr[i] - arr[i + 1])
			diff = abs(arr[i] - arr[i + 1])
			ans = min(ans, ref - diff)
		elif i == n - 1:
			diff = abs(arr[i] - arr[i - 1])
			ans = min(ans, ref - diff)
		else:
			diff1 = abs(arr[i] - arr[i + 1])
			diff2 = abs(arr[i] - arr[i - 1])
			store = arr[i]
			init = diff1 + diff2
			arr[i] = arr[i - 1]
			diff1 = abs(arr[i] - arr[i + 1])
			diff2 = abs(arr[i] - arr[i - 1])
			final = diff1 + diff2
			change = init - final
			ans = min(ans, ref - change)
			arr[i] = store
			store = arr[i]
			arr[i] = arr[i + 1]
			diff1 = abs(arr[i] - arr[i + 1])
			diff2 = abs(arr[i] - arr[i - 1])
			final = diff1 + diff2
			change = init - final
			arr[i] = store
			ans = min(ans, ref - change)
	out(ans)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	anss = []
	for i in range(1, n):
		anss.append(abs(a[i] - a[i - 1]))
	diffs = []
	for i in range(1, n - 1):
		diff = abs(a[i] - a[i - 1]) + abs(a[i] - a[i + 1]) - abs(a[i - 1] - a[i + 1])
		diffs.append(diff)
	diffs.append(abs(a[0] - a[1]))
	diffs.append(abs(a[n - 1] - a[n - 2]))
	print(sum(anss) - max(diffs))


from itertools import *
f = abs
for s in [*open(0)][2::2]:
	(a, b) = zip(*((int(k), len([*g])) for (k, g) in groupby(s.split())))
	a = [x - y << 1 for (x, y) in zip(a, a[1:])] or [0]
	print(sum(map(f, a)) - max((k < 2 and f(x) + f(y) - f(x + y) for (x, y, k) in zip([-a[0] // 2] + a, a + [-a[-1] // 2], b))) >> 1)


import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7

def resolve():
	t = int(input())
	for _ in range(t):
		n = int(input())
		A = list(map(int, input().split()))
		diff = [0] * (n - 1)
		for i in range(n - 1):
			diff[i] = abs(A[i] - A[i + 1])
		total = sum(diff)
		res = total
		for i in range(n):
			if i == 0:
				t = total - diff[i]
			elif i == n - 1:
				t = total - diff[i - 1]
			else:
				t = total - diff[i - 1] - diff[i] + abs(A[i - 1] - A[i + 1])
			res = min(res, t)
		print(res)
resolve()


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	t = 0
	for i in range(1, n):
		t += abs(a[i] - a[i - 1])
	maxDecrease = 0
	for i in range(n):
		if i - 1 >= 0 and i + 1 < n:
			original = abs(a[i + 1] - a[i]) + abs(a[i] - a[i - 1])
			new = abs(a[i + 1] - a[i - 1])
		elif i == 0:
			original = abs(a[i + 1] - a[i])
			new = 0
		else:
			original = abs(a[i] - a[i - 1])
			new = 0
		maxDecrease = max(maxDecrease, original - new)
	print(t - maxDecrease)


test = int(input())
for i in range(test):
	n = int(input())
	s = 0
	l = list(map(int, input().split()))
	for i in range(n - 1):
		s += abs(l[i] - l[i + 1])
	small = min(s, s - abs(l[1] - l[0]), s - abs(l[n - 1] - l[n - 2]))
	for i in range(1, n - 1):
		small = min(small, s - abs(l[i] - l[i - 1]) - abs(l[i + 1] - l[i]) + abs(l[i + 1] - l[i - 1]))
	print(small)


t = int(input())
for i in range(t):
	n = int(input())
	a = [0] + [int(i) for i in input().split()]
	d = 0
	t = 0
	if n <= 2:
		print(0)
		continue
	if a[1] != a[2]:
		d = abs(a[2] - a[1])
	if a[n - 1] != a[n]:
		d = max(d, abs(a[n - 1] - a[n]))
	for i in range(2, n):
		if a[i] <= min(a[i - 1], a[i + 1]):
			d = max(d, abs(a[i] - min(a[i - 1], a[i + 1])) * 2)
		if a[i] >= max(a[i - 1], a[i + 1]):
			d = max(d, abs(a[i] - max(a[i - 1], a[i + 1])) * 2)
	for i in range(1, n):
		t += abs(a[i] - a[i + 1])
	print(t - d)


import math, sys
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, sys.stdin.readline().strip().split()))
	bst_case = [-1, -1, 0]
	for i in range(n):
		if i - 1 >= 0:
			pre = (abs(arr[i + 1] - arr[i]) if i + 1 < n else 0) + abs(arr[i] - arr[i - 1])
			pst = abs(arr[i + 1] - arr[i - 1]) if i + 1 < n else 0
			d = pre - pst
			if d > bst_case[0]:
				bst_case = [d, i, -1]
		if i + 1 < n:
			pre = (abs(arr[i] - arr[i - 1]) if i - 1 >= 0 else 0) + abs(arr[i + 1] - arr[i])
			pst = abs(arr[i - 1] - arr[i + 1]) if i - 1 >= 0 else 0
			d = pre - pst
			if d > bst_case[0]:
				bst_case = [d, i, 1]
	arr[bst_case[1]] = arr[bst_case[1] + bst_case[2]]
	cost = 0
	while len(arr) > 1:
		cost += abs(arr[-2] - arr[-1])
		arr.pop()
	print(cost)


t = int(input())
for _ in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	abs_error = 0
	for i in range(1, n):
		abs_error += abs(nums[i] - nums[i - 1])
	mx = max(abs(nums[1] - nums[0]), abs(nums[n - 1] - nums[n - 2]))
	mx2 = 0
	for i in range(1, n - 1):
		mx = max(abs(nums[i] - nums[i - 1]) + abs(nums[i + 1] - nums[i]) - abs(nums[i - 1] - nums[i + 1]), mx)
	abs_error -= max(mx2, mx)
	print(abs_error)


from collections import Counter
import math
import sys
import heapq
from bisect import bisect, bisect_left, bisect_right

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

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 mod():
	return 10 ** 9 + 7
for i in range(INT()):
	n = INT()
	a = LIST()
	ans = 0
	for i in range(n - 1):
		ans += abs(a[i] - a[i + 1])
	c = ans
	for i in range(1, n - 1):
		c = min(c, ans - (abs(a[i] - a[i - 1]) + abs(a[i] - a[i + 1])) + abs(a[i - 1] - a[i + 1]))
	c = min(c, ans - abs(a[0] - a[1]), ans - abs(a[n - 1] - a[n - 2]))
	print(c)


for _ in range(int(input())):
	n = int(input())
	s = [0] + list(map(int, input().split()))
	ans = 0
	for i in range(2, n + 1):
		ans += abs(s[i] - s[i - 1])
	mx = max(abs(s[2] - s[1]), abs(s[n] - s[n - 1]))
	for i in range(2, n):
		mx = max(mx, abs(s[i] - s[i - 1]) + abs(s[i + 1] - s[i]) - abs(s[i + 1] - s[i - 1]))
	print(ans - mx)


t = int(input())

def cost(vals):
	cur = 0
	cost = 0
	for i in range(1, len(vals)):
		val = vals[i] + cur
		diff = vals[i] - vals[i - 1]
		cost += abs(diff)
		cur += diff
	cut = max(abs(vals[1] - vals[0]), abs(vals[-1] - vals[-2]))
	for i in range(1, len(vals) - 1):
		best = abs(vals[i] - vals[i - 1]) + abs(vals[i + 1] - vals[i]) - abs(vals[i + 1] - vals[i - 1])
		cut = max(cut, best)
	return cost - cut
for z in range(t):
	n = int(input())
	vals = [int(val) for val in input().strip().split()]
	print(cost(vals))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(s) for s in input().split(' ')]
	ans = 0
	for i in range(1, n):
		ans += abs(a[i] - a[i - 1])
	mx = max(abs(a[0] - a[1]), abs(a[n - 1] - a[n - 2]))
	for i in range(1, n - 1):
		mx = max(mx, abs(a[i] - a[i - 1]) + abs(a[i + 1] - a[i]) - abs(a[i + 1] - a[i - 1]))
	print(ans - mx)


t = int(input())
for test in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	change = 0
	for i in range(n):
		if i == 0:
			change = max(change, abs(a[1] - a[0]))
		elif i == n - 1:
			change = max(change, abs(a[n - 1] - a[n - 2]))
		else:
			change = max(change, abs(a[i + 1] - a[i]) + abs(a[i] - a[i - 1]) - abs(a[i + 1] - a[i - 1]))
	s = 0
	for i in range(n - 1):
		s += abs(a[i + 1] - a[i])
	print(s - change)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	c = arr[0]
	ml = []
	ans = 0
	for i in range(1, n):
		ml.append(c - arr[i])
		c -= ml[-1]
		ans += abs(ml[-1])
	mx = max(abs(ml[0]), abs(ml[-1]))
	for i in range(1, n - 1):
		mx = max(abs(abs(ml[i - 1]) + abs(ml[i]) - abs(ml[i] + ml[i - 1])), mx)
	print(ans - mx)


(z, zz) = (input, lambda : list(map(int, z().split())))
fast = lambda : stdin.readline().strip()
zzz = lambda : [int(i) for i in fast().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = 1
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = int(z())
for _ in range(num):
	n = int(z())
	arr = zzz()
	ans = 0
	for i in range(n - 1):
		ans += abs(arr[i] - arr[i + 1])
	sm = min(ans, ans - abs(arr[0] - arr[1]), ans - abs(arr[-1] - arr[-2]))
	for i in range(1, n - 1):
		sm = min(sm, ans - (abs(arr[i] - arr[i - 1]) + abs(arr[i + 1] - arr[i]) - abs(arr[i + 1] - arr[i - 1])))
	print(sm)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def gift():
	for _ in range(t):
		n = int(input())
		nums = list(map(int, input().split()))
		B = []
		for i in range(n - 1):
			B.append(abs(nums[i + 1] - nums[i]))
		S2 = [0]
		for s in B:
			S2.append(S2[-1] + s)
		ans = min(sum(B[1:]), sum(B[:-1]))
		for i in range(n - 2):
			ans = min(ans, S2[i] + (S2[-1] - S2[i + 2]) + abs(nums[i + 2] - nums[i]))
		yield ans
t = int(input())
ans = gift()
print(*ans, sep='\n')


for _ in range(int(input())):
	n = int(input())
	diff = 0
	ans = 0
	a = list(map(int, input().split()))
	b = [a[i + 1] - a[i] for i in range(n - 1)]
	mx = abs(b[0])
	for i in range(n - 2):
		if b[i] * b[i + 1] < 0:
			mx = max(mx, abs(b[i]) + abs(b[i + 1]) - abs(b[i] + b[i + 1]))
		ans += abs(b[i])
	mx = max(mx, abs(b[-1]))
	print(ans + abs(b[-1]) - mx)


def solve():
	n = int(input())
	a = [0] + list(map(int, input().split()))
	ans = 0
	for i in range(2, n + 1):
		ans += abs(a[i] - a[i - 1])
	mx = max(abs(a[2] - a[1]), abs(a[n] - a[n - 1]))
	for i in range(2, n):
		mx = max(mx, abs(a[i] - a[i - 1]) + abs(a[i + 1] - a[i]) - abs(a[i + 1] - a[i - 1]))
	print(ans - mx)
t = int(input())
for _ in range(t):
	solve()


from math import *
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	val1 = abs(a[0] - a[1])
	val2 = abs(a[n - 1] - a[n - 2])
	l = []
	ss = 0
	for i in range(1, n - 1):
		l.append(abs(a[i] - a[i - 1]) + abs(a[i] - a[i + 1]) - abs(a[i - 1] - a[i + 1]))
	for i in range(1, n):
		ss = ss + abs(a[i] - a[i - 1])
	if len(l) != 0:
		print(ss - max(val1, val2, max(l)))
	else:
		print(ss - max(val1, val2))


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	for i in range(1, n):
		ans += abs(arr[i] - arr[i - 1])
	mx = max(abs(arr[1] - arr[0]), abs(arr[n - 1] - arr[n - 2]))
	for i in range(1, n - 1):
		mx = max(mx, abs(arr[i] - arr[i - 1]) + abs(arr[i + 1] - arr[i]) - abs(arr[i + 1] - arr[i - 1]))
	ans -= mx
	print(ans)


t = int(input())
for q in range(t):
	n = int(input())
	mas = list(map(int, input().split()))
	maxi = -10 ** 18
	num = -1
	for i in range(1, n - 1):
		d1 = abs(mas[i] - mas[i - 1]) + abs(mas[i + 1] - mas[i])
		d2 = abs(mas[i - 1] - mas[i + 1])
		d = d1 - d2
		if maxi < d:
			maxi = d
			num = i
	if abs(mas[0] - mas[1]) > maxi:
		maxi = abs(mas[0] - mas[1])
		num = 0
	if abs(mas[-1] - mas[-2]) > maxi:
		maxi = abs(mas[-1] - mas[-2])
		num = n - 1
	if num != -1:
		del mas[num]
	ans = 0
	for i in range(len(mas) - 1):
		ans += abs(mas[i] - mas[i + 1])
	print(ans)


t = int(input())
for q in range(t):
	n = int(input())
	ch = input()
	L = [int(i) for i in ch.split()]
	nb = 0
	for i in range(1, n):
		nb += abs(L[i] - L[i - 1])
	v = []
	v.append(abs(L[0] - L[1]))
	v.append(abs(L[-1] - L[-2]))
	for i in range(1, n - 1):
		if L[i] < L[i + 1] and L[i] < L[i - 1] or (L[i] > L[i + 1] and L[i] > L[i - 1]):
			v.append(abs(L[i] - L[i - 1]) + abs(L[i] - L[i + 1]) - abs(L[i - 1] - L[i + 1]))
	print(nb - max(v))


t = int(input())
for i in range(t):
	n = int(input())
	data = list(map(int, input().split()))
	if n <= 2:
		print(0)
	else:
		m = max(abs(data[1] - data[0]), abs(data[-1] - data[-2]))
		for j in range(1, n - 1):
			m = max(m, abs(data[j] - data[j - 1]) + abs(data[j] - data[j + 1]) - abs(data[j + 1] - data[j - 1]))
		res = 0
		for j in range(1, n):
			res += abs(data[j] - data[j - 1])
		print(res - m)


from sys import stdin, stdout
input = stdin.readline
print = lambda x: stdout.write(str(x) + '\n')
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	opr = 0
	for (idx, ele) in enumerate(a):
		if idx != n - 1:
			opr += abs(ele - a[idx + 1])
	ans = float('inf')
	for (idx, ele) in enumerate(a):
		if idx == 0:
			ans = opr - abs(a[idx + 1] - ele)
		elif idx == n - 1:
			ans = min(ans, opr - abs(a[idx] - a[idx - 1]))
		else:
			ans = min(ans, opr - abs(a[idx] - a[idx - 1]) - abs(a[idx + 1] - a[idx]) + abs(a[idx + 1] - a[idx - 1]))
	print(ans)


from itertools import *
for s in [*open(0)][2::2]:
	(a, b) = zip(*((int(k), len([*g]) < 2) for (k, g) in groupby(s.split())))
	r = sum((abs(x - y) for (x, y) in zip(a, a[1:])))
	print(r and r - max([b[0] and abs(a[1] - a[0]), b[-1] and abs(a[-1] - a[-2])] + [abs(x - y) + abs(y - z) - abs(x - z) for (x, y, z, u) in zip(a, a[1:], a[2:], b[1:]) if u]))


for _ in range(int(input())):
	n = int(input())
	arr = [int(num) for num in input().split(' ')]
	if n == 2:
		ans = 0
	elif len(set(arr)) == 1:
		ans = 0
	else:
		d = []
		for i in range(1, n):
			d.append(abs(arr[i] - arr[i - 1]))
		s = sum(d)
		ans = 10 ** 18
		for i in range(len(d)):
			if d[i] == d[i]:
				a1 = arr[i + 1]
				a2 = arr[i]
				x = s - d[i]
				ans1 = x
				if i + 2 < n and i - 1 >= 0:
					x1 = abs(arr[i + 2] - a2) - abs(arr[i + 1] - arr[i + 2])
					ans3 = min(ans, x1 + ans1)
					x2 = abs(arr[i - 1] - a1) - abs(arr[i] - arr[i - 1])
					ans4 = min(ans, x2 + ans1)
					ans = min(ans3, ans4)
				elif i + 2 < n:
					x1 = abs(arr[i + 2] - a2) - abs(arr[i + 1] - arr[i + 2])
					ans = min(ans, x1 + ans1)
					ans = min(ans, x)
				elif i - 1 >= 0:
					x2 = abs(arr[i - 1] - a1) - abs(arr[i] - arr[i - 1])
					ans = min(ans, x2 + ans1)
					ans = min(ans, x)
	print(ans)


f = abs
for s in [*open(0)][2::2]:
	a = []
	b = []
	p = 0
	for x in s.split():
		if x != p:
			a += (int(x),)
			b += (0,)
			p = x
		b[-1] += 1
	a = [x - y << 1 for (x, y) in zip(a, a[1:])] or [0]
	print(sum(map(f, a)) - max((k < 2 and f(x) + f(y) - f(x + y) for (x, y, k) in zip([-a[0] // 2] + a, a + [-a[-1] // 2], b))) >> 1)


def solve():
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	for i in range(1, n):
		ans += abs(l[i] - l[i - 1])
	mx = max(abs(l[0] - l[1]), abs(l[n - 1] - l[n - 2]))
	for i in range(1, n - 1):
		mx = max(mx, abs(l[i] - l[i - 1]) + abs(l[i + 1] - l[i]) - abs(l[i + 1] - l[i - 1]))
	return ans - mx
for _ in range(int(input())):
	print(solve())


import math

def hi(a):
	l = len(a)
	b = []
	b.append(abs(a[0] - a[1]))
	for i in range(1, l - 1):
		b.append(abs(a[i] - a[i - 1]) + abs(a[i] - a[i + 1]) - abs(a[i - 1] - a[i + 1]))
	b.append(abs(a[-1] - a[-2]))
	m = max(b)
	j = b.index(m)
	c = 0
	if j == 0:
		a[0] = a[1]
		for i in range(l - 1):
			c += abs(a[i] - a[i + 1])
		return c
	if j == l - 1:
		a[-1] = a[-2]
		for i in range(l - 1):
			c += abs(a[i] - a[i + 1])
		return c
	a[j] = a[j - 1]
	for i in range(l - 1):
		c += abs(a[i] - a[i + 1])
	return c
t = int(input())
for i in range(t):
	k = input()
	a = list(map(int, input().split(' ')))
	print(hi(a))


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	for i in range(1, n):
		ans += abs(arr[i] - arr[i - 1])
	mini = min(ans - abs(arr[0] - arr[1]), ans - abs(arr[n - 1] - arr[n - 2]))
	for i in range(1, n - 1):
		mini = min(mini, ans - (abs(arr[i] - arr[i - 1]) + abs(arr[i] - arr[i + 1])) + abs(arr[i - 1] - arr[i + 1]))
	print(mini)


import sys

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

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

def get_list_string():
	return list(map(str, sys.stdin.readline().strip().split()))

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

def get_int():
	return int(sys.stdin.readline().strip())

def get_print_int(x):
	sys.stdout.write(str(x) + '\n')

def get_print(x):
	sys.stdout.write(x + '\n')
from sys import maxsize
from bisect import bisect_left

def solve():
	for _ in range(get_int()):
		n = get_int()
		arr = get_list()
		first = 0
		for i in range(n - 1):
			first += abs(arr[i] - arr[i + 1])
		minn = min(first, first - abs(arr[0] - arr[1]), first - abs(arr[-1] - arr[-2]))
		for i in range(1, n - 1):
			minn = min(minn, first - abs(arr[i - 1] - arr[i]) - abs(arr[i] - arr[i + 1]) + abs(arr[i - 1] - arr[i + 1]))
		get_print_int(minn)
solve()


import os
import sys
from io import BytesIO, IOBase
import heapq as h
import bisect
from types import GeneratorType
BUFSIZE = 8192

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		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 = self.os.read(self._fd, max(self.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 = self.os.read(self._fd, max(self.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:
			self.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')
import sys
from math import ceil, log2
INT_MAX = sys.maxsize
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import collections as col
import math, string

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())

def minVal(x, y):
	return x if x < y else y

def getMid(s, e):
	return s + (e - s) // 2

def RMQUtil(st, ss, se, qs, qe, index):
	if qs <= ss and qe >= se:
		return st[index]
	if se < qs or ss > qe:
		return INT_MAX
	mid = getMid(ss, se)
	return minVal(RMQUtil(st, ss, mid, qs, qe, 2 * index + 1), RMQUtil(st, mid + 1, se, qs, qe, 2 * index + 2))

def RMQ(st, n, qs, qe):
	if qs < 0 or qe > n - 1 or qs > qe:
		print('Invalid Input')
		return -1
	return RMQUtil(st, 0, n - 1, qs, qe, 0)

def constructSTUtil(arr, ss, se, st, si):
	if ss == se:
		st[si] = arr[ss]
		return arr[ss]
	mid = getMid(ss, se)
	st[si] = minVal(constructSTUtil(arr, ss, mid, st, si * 2 + 1), constructSTUtil(arr, mid + 1, se, st, si * 2 + 2))
	return st[si]

def constructST(arr, n):
	x = int(ceil(log2(n)))
	max_size = 2 * int(2 ** x) - 1
	st = [0] * max_size
	constructSTUtil(arr, 0, n - 1, st, 0)
	return st
MOD = 10 ** 9 + 7
mod = 10 ** 9 + 7
t = int(input())
p = 10 ** 9 + 7

def ncr_util():
	inv[0] = inv[1] = 1
	fact[0] = fact[1] = 1
	for i in range(2, 300001):
		inv[i] = inv[i % p] * (p - p // i) % p
	for i in range(1, 300001):
		inv[i] = inv[i - 1] * inv[i] % p
		fact[i] = fact[i - 1] * i % p

def z_array(s1):
	n = len(s1)
	z = [0] * n
	(l, r, k) = (0, 0, 0)
	for i in range(1, n):
		if i > r:
			(l, r) = (i, i)
			while r < n and s1[r - l] == s1[r]:
				r += 1
			z[i] = r - l
			r -= 1
		else:
			k = i - l
			if z[k] < r - i + 1:
				z[i] = z[k]
			else:
				l = i
				while r < n and s1[r - l] == s1[r]:
					r += 1
				z[i] = r - l
				r -= 1
	return z

def primeFactors(n):
	d1 = {}
	while n % 2 == 0:
		d1[2] = d1.get(2, 0) + 1
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			d1[i] = d1.get(i, 0) + 1
			n = n // i
	if n > 2:
		d1[n] = d1.get(n, 0) + 1
	return d1

def solve():
	dp = [0] * (n + 2)
	for i in range(n - 2, -1, -1):
		x = abs(l[i + 1] - l[i])
		dp[i] = x + dp[i + 1]
	ans = 10 ** 18
	ans = min(ans, dp[0] - abs(l[0] - l[1]))
	ans = min(ans, dp[0] - abs(l[-1] - l[-2]))
	for i in range(n - 2, 0, -1):
		ans = min(ans, max(abs(l[i - 1] - l[i + 1]) + dp[0] - (abs(l[i] - l[i - 1]) + abs(l[i] - l[i + 1])), 0))
	return ans
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	print(solve())


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = abs(a[0] - a[1])
	m = 0
	for i in range(1, n - 1):
		if a[i - 1] < a[i] > a[i + 1] or a[i - 1] > a[i] < a[i + 1]:
			k = abs(a[i - 1] - a[i]) + abs(a[i] - a[i + 1]) - abs(a[i - 1] - a[i + 1])
			if m < k:
				m = k
		ans += abs(a[i] - a[i + 1])
	m = max(abs(a[0] - a[1]), abs(a[-1] - a[-2]), m)
	print(ans - m)


import sys
import math
import collections
import bisect
import string

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

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

def get_string():
	return sys.stdin.readline().strip()
for t in range(int(input())):
	n = int(input())
	arr = get_list()
	if len(set(arr)) == 1:
		print(0)
		continue
	ans = 0
	for i in range(1, n):
		diff = arr[i] - arr[i - 1]
		ans += abs(diff)
	val = ans - abs(arr[1] - arr[0])
	for i in range(1, n):
		temp = ans
		temp -= abs(arr[i] - arr[i - 1])
		if i != n - 1:
			temp -= abs(arr[i] - arr[i + 1])
			temp += abs(arr[i - 1] - arr[i + 1])
		val = min(temp, val)
	print(val)


for _ in range(int(input())):
	n = int(input())
	num = list(map(int, input().split()))
	diff = 0
	for i in range(n - 1):
		diff += abs(num[i] - num[i + 1])
	chng = max(abs(num[0] - num[1]), abs(num[n - 1] - num[n - 2]))
	for i in range(1, n - 1):
		chng = max(chng, abs(abs(num[i] - num[i - 1]) + abs(num[i + 1] - num[i]) - abs(num[i - 1] - num[i + 1])))
	print(diff - chng)


import math
T = int(input())
for i in range(T):
	n = input().strip()
	n = int(n)
	l1 = [int(i) for i in input().strip().split()]
	if n == 2:
		print(0)
	else:
		delta = [l1[i + 1] - l1[i] for i in range(n - 1)]
		max_diff = max(abs(delta[0]), abs(delta[-1]))
		for i in range(n - 2):
			max_diff = max(max_diff, abs(delta[i + 1]) + abs(delta[i]) - abs(delta[i + 1] + delta[i]))
		delta = [abs(i) for i in delta]
		print(sum(delta) - max_diff)


import sys
out = []
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, sys.stdin.readline().split()))
	mx = 0
	ans = 0
	for i in range(1, n - 1):
		bfchange = abs(l[i - 1] - l[i]) + abs(l[i + 1] - l[i])
		reduced = bfchange - abs(l[i - 1] - l[i + 1])
		mx = max(mx, reduced)
		ans += abs(l[i - 1] - l[i])
	ans += abs(l[-2] - l[-1])
	mx = max(mx, abs(l[0] - l[1]), abs(l[-1] - l[-2]))
	out.append(ans - mx)
print('\n'.join((str(x) for x in out)))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	dif = []
	tot = 0
	for i in range(n - 1):
		dif.append(a[i + 1] - a[i])
		tot += abs(a[i + 1] - a[i])
	ans = tot
	for i in range(n - 1):
		if i == 0:
			ans = min(ans, tot - abs(dif[i]))
		else:
			ans = min(ans, tot - abs(dif[i]) - abs(dif[i - 1]) + abs(dif[i] + dif[i - 1]))
	ans = min(ans, tot - abs(dif[n - 2]))
	print(ans)


import math
import sys

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 = Read.int()
	a = Read.list_int()
	res = 0
	for i in range(n - 1):
		res += abs(a[i] - a[i + 1])
	mx = 0
	for i in range(n):
		v = a[i]
		lcl = 0
		if i == 0:
			r = a[i + 1]
			lcl = abs(r - v)
		elif i == n - 1:
			l = a[i - 1]
			lcl = abs(l - v)
		else:
			l = a[i - 1]
			r = a[i + 1]
			old = abs(r - v) + abs(l - v)
			lcl = old - abs(r - l)
		if lcl > mx:
			mx = lcl
	print(res - mx)
query_count = Read.int()
while query_count:
	query_count -= 1
	solve()


for _ in range(int(input())):
	n = int(input())
	a = tuple(map(int, input().split()))
	initial = [abs(ai - aj) for (ai, aj) in zip(a, a[1:])]
	save = max(initial[0], initial[-1])
	for i in range(1, n - 1):
		if a[i] != sorted(a[i - 1:i + 2])[1]:
			save = max(save, min(abs(a[i] - a[i - 1]), abs(a[i] - a[i + 1])) * 2)
	print(sum(initial) - save)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	maxdiff = 0
	ans = 0
	if n == 2:
		print(0)
		continue
	for i in range(n - 1):
		ans += abs(a[i] - a[i + 1])
		if i > 0:
			maxdiff = max(maxdiff, abs(a[i] - a[i + 1]) + abs(a[i] - a[i - 1]) - abs(a[i + 1] - a[i - 1]))
	maxdiff = max(maxdiff, abs(a[1] - a[0]))
	maxdiff = max(maxdiff, abs(a[n - 1] - a[n - 2]))
	print(ans - maxdiff)


def solve():
	N = int(input())
	A = list(map(int, input().split()))
	ans = 0
	for i in range(1, N):
		ans += abs(A[i] - A[i - 1])
	mx = max(abs(A[1] - A[0]), abs(A[N - 1] - A[N - 2]))
	for i in range(1, N - 1):
		mx = max(mx, abs(A[i] - A[i - 1]) + abs(A[i + 1] - A[i]) - abs(A[i + 1] - A[i - 1]))
	print(ans - mx)
T = int(input())
for i in range(T):
	solve()


t = int(input())
while t > 0:
	n = int(input())
	arr = [0] + list(map(int, input().split()))
	ans = 0
	for i in range(2, n + 1):
		ans = ans + abs(arr[i] - arr[i - 1])
	mx = max(abs(arr[1] - arr[2]), abs(arr[n] - arr[n - 1]))
	for i in range(2, n):
		mx = max(mx, abs(arr[i] - arr[i - 1]) + abs(arr[i] - arr[i + 1]) - abs(arr[i - 1] - arr[i + 1]))
	f_ans = ans - mx
	print(f_ans)
	t = t - 1


t = int(input())
for i in range(t):
	n = int(input())
	mass = list(map(int, input().split()))
	mas = []
	for i in range(1, n):
		mas.append(mass[-i - 1] - mass[-i])
	l = len(mas)
	s = 0
	for i in mas:
		s += abs(i)
	s_min = s
	if s - abs(mas[0]) < s_min:
		s_min = s - abs(mas[0])
	for i in range(1, l):
		res = s - abs(mas[i]) + abs(mas[i] + mas[i - 1]) - abs(mas[i - 1])
		if res < s_min:
			s_min = res
	if s - abs(mas[l - 1]) < s_min:
		s_min = s - abs(mas[l - 1])
	print(s_min)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	ans = 0
	for j in range(1, n):
		ans += abs(a[j] - a[j - 1])
	mx = max(abs(a[1] - a[0]), abs(a[n - 1] - a[n - 2]))
	for j in range(1, n - 1):
		mx = max(mx, abs(a[j] - a[j - 1]) + abs(a[j] - a[j + 1]) - abs(a[j + 1] - a[j - 1]))
	ans -= mx
	print(ans)


test = int(input())
for _ in range(test):
	n = int(input())
	arr = list(map(int, input().split()))
	cost = 0
	for i in range(n - 1):
		cost += abs(arr[i] - arr[i + 1])
	reduced = max(abs(arr[0] - arr[1]), abs(arr[n - 1] - arr[n - 2]))
	for i in range(1, n - 1):
		reduced = max(reduced, abs(arr[i - 1] - arr[i]) + abs(arr[i] - arr[i + 1]) - abs(arr[i - 1] - arr[i + 1]))
	print(cost - reduced)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n <= 2:
		print(0)
		continue
	p = abs(a[0] - a[1])
	s = p
	for i in range(1, n - 1):
		p = max(p, abs(abs(a[i] - a[i - 1]) + abs(a[i] - a[i + 1]) - abs(a[i - 1] - a[i + 1])))
		s += abs(a[i] - a[i + 1])
	p = max(p, abs(a[n - 1] - a[n - 2]))
	print(s - p)


a = int(input())
for i in range(a):
	b = int(input())
	arr = list(map(int, input().split()))
	arr = arr[::-1]
	ans = []
	for i in range(0, len(arr) - 1):
		ans.append(abs(arr[i + 1] - arr[i]))
	rep = abs(arr[1] - arr[0])
	for i in range(1, len(arr) - 1):
		if abs(arr[i] - arr[i + 1]) + abs(arr[i] - arr[i - 1]) - abs(arr[i + 1] - arr[i - 1]) > rep:
			rep = abs(arr[i] - arr[i + 1]) + abs(arr[i] - arr[i - 1]) - abs(arr[i + 1] - arr[i - 1])
	rep = max(rep, abs(arr[len(arr) - 1] - arr[len(arr) - 2]))
	print(sum(ans) - rep)


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 k 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')
INF = 10 ** 19
MOD = 10 ** 9 + 7
EPS = 10 ** (-10)
for _ in range(INT()):
	N = INT()
	A = LIST()
	B = [0] * (N - 1)
	for i in range(N - 1):
		B[i] = abs(A[i] - A[i + 1])
	mx = max(B[0], B[-1])
	for i in range(1, N - 1):
		if A[i - 1] - A[i] < 0 and A[i] - A[i + 1] > 0 or (A[i - 1] - A[i] > 0 and A[i] - A[i + 1] < 0):
			mx = max(mx, abs(min(abs(A[i - 1] - A[i]), abs(A[i] - A[i + 1]))) * 2)
	ans = sum(B) - mx
	print(ans)


for tt in range(int(input())):
	n = int(input())
	d = [int(i) for i in input().split()]
	diff = [abs(d[i] - d[i - 1]) for i in range(1, n)]
	ans_ = sum(diff)
	ans = ans_ - diff[0]
	for i in range(1, n):
		ans__ = ans_ - diff[i - 1] + (0 if i == n - 1 else abs(d[i + 1] - d[i - 1]) - diff[i])
		ans = min(ans, ans__)
	print(ans)


import sys
import math
from collections import defaultdict, Counter
input = sys.stdin.readline

def print(x):
	sys.stdout.write(str(x) + '\n')
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	ma = abs(a[1] - a[0])
	for j in range(n - 1, 0, -1):
		cur = abs(a[j] - a[j - 1])
		ans += cur
		if j != n - 1:
			cur += abs(a[j + 1] - a[j])
			cur1 = abs(a[j + 1] - a[j - 1])
			ma = max(ma, cur - cur1)
		else:
			ma = max(ma, cur)
	print(ans - ma)


def solve(n, a):
	ans = 0
	for i in range(1, n):
		ans += abs(a[i] - a[i - 1])
	pr = ans
	pr -= abs(a[0] - a[1])
	for i in range(1, n):
		tmp = ans
		tmp -= abs(a[i] - a[i - 1])
		if i != n - 1:
			tmp -= abs(a[i] - a[i + 1])
			tmp += abs(a[i + 1] - a[i - 1])
		pr = min(pr, tmp)
	return pr
t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(n, a))
	t -= 1


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	A = list(map(int, sys.stdin.readline().split()))
	best_save = max(abs(A[1] - A[0]), abs(A[-2] - A[-1]))
	for i in range(1, n - 1):
		(l, m, r) = (A[i - 1], A[i], A[i + 1])
		if l <= m <= r or l >= m >= r:
			continue
		save = abs(2 * m - l - r) - abs(l - r)
		if save > best_save:
			best_save = save
	cost = 0
	for i in range(n - 1):
		cost += abs(A[i] - A[i + 1])
	cost -= best_save
	print(cost)


for i1 in range(int(input())):
	n = int(input())
	a = [0] + list(map(int, input().split()))
	ref = [abs(a[i] - a[i - 1]) for i in range(2, n + 1)]
	ans = sum(ref)
	mx = max(abs(a[2] - a[1]), abs(a[n] - a[n - 1]))
	for i in range(2, n):
		mx = max(mx, abs(a[i] - a[i - 1]) + abs(a[i + 1] - a[i]) - abs(a[i + 1] - a[i - 1]))
	print(ans - mx)


for i in range(int(input())):
	x = int(input())
	a = list(map(int, input().split()))
	summ = 0
	for j in range(1, x):
		summ += abs(a[j] - a[j - 1])
	maxx = abs(a[1] - a[0])
	for j in range(2, x):
		if maxx < abs(a[j] - a[j - 1]) + abs(a[j - 1] - a[j - 2]) - abs(a[j] - a[j - 2]):
			maxx = abs(a[j] - a[j - 1]) + abs(a[j - 1] - a[j - 2]) - abs(a[j] - a[j - 2])
	if maxx < abs(a[x - 1] - a[x - 2]):
		maxx = abs(a[x - 1] - a[x - 2])
	print(str(summ - maxx))


for t in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	p = max(abs(l[0] - l[1]), abs(l[-1] - l[-2]))
	for i in range(1, n - 1):
		p = max(abs(l[i] - l[i - 1]) + abs(l[i] - l[i + 1]) - abs(l[i - 1] - l[i + 1]), p)
	rez = 0
	for i in range(1, n):
		rez += abs(l[i] - l[i - 1])
	print(rez - p)


def max(a, b):
	if a > b:
		return a
	else:
		return b
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	x = 0
	for i in range(1, n):
		x += abs(a[i] - a[i - 1])
	y1 = abs(a[1] - a[0])
	y2 = abs(a[-1] - a[-2])
	y3 = 0
	for i in range(1, n - 1):
		d = abs(a[i] - a[i - 1]) + abs(a[i + 1] - a[i]) - abs(a[i + 1] - a[i - 1])
		if y3 < d:
			y3 = d
	y = max(y1, max(y2, y3))
	print(x - y)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	sum1 = 0
	for i in range(n - 1):
		sum1 += abs(a[i] - a[i + 1])
	max_raznost = max(abs(a[0] - a[1]), abs(a[n - 1] - a[n - 2]))
	for i in range(1, n - 1):
		raznost = abs(a[i] - a[i - 1]) + abs(a[i] - a[i + 1]) - abs(a[i - 1] - a[i + 1])
		if raznost > max_raznost:
			max_raznost = raznost
	print(sum1 - max_raznost)
tests = int(input())
while tests > 0:
	solve()
	tests = tests - 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	min_cost = 10000000000.0
	if n <= 2:
		print(0)
		continue
	for i in range(n):
		if i > 0:
			ans += abs(a[i] - a[i - 1])
		if i == 0:
			min_cost = min(min_cost, -abs(a[0] - a[1]))
		elif i == n - 1:
			min_cost = min(min_cost, -abs(a[n - 2] - a[n - 1]))
		else:
			origin_cost = abs(a[i - 1] - a[i]) + abs(a[i] - a[i + 1])
			cost = abs(a[i - 1] - a[i + 1]) - origin_cost
			min_cost = min(min_cost, cost)
	print(ans + min_cost)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	diff = []
	s = 0
	for i in range(1, n):
		d = l[i] - l[i - 1]
		s += abs(d)
		diff.append(d)
	maxi = max(abs(l[0] - l[1]), abs(l[-1] - l[-2]))
	for i in range(len(diff) - 1):
		absv = abs(diff[i]) + abs(diff[i + 1])
		v = abs(diff[i] + diff[i + 1])
		di = absv - v
		maxi = max(maxi, di)
	print(s - maxi)


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
from itertools import permutations, combinations

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

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write('\n'.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
from decimal import Decimal
sys.setrecursionlimit(100000)
INF = float('inf')
mod = 998244353
for t in range(int(data())):
	n = int(data())
	a = mdata()
	m = abs(a[0] - a[1])
	ans = 0
	for i in range(1, n):
		ans += abs(a[i] - a[i - 1])
		k = abs(a[i] - a[i - 1])
		if i < n - 1:
			k += abs(a[i] - a[i + 1]) - abs(a[i - 1] - a[i + 1])
		m = max(m, k)
	out(ans - m)


t = int(input())
for q in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	s = 0
	if n == 1:
		print(0)
		continue
	for i in range(1, len(a)):
		s += abs(a[i] - a[i - 1])
	v = 0
	n = len(a)
	v = max(abs(a[0] - a[1]), abs(a[n - 1] - a[n - 2]))
	if n > 2:
		v = max(v, max([abs(a[i] - a[i - 1]) + abs(a[i + 1] - a[i]) - abs(a[i + 1] - a[i - 1]) for i in range(1, n - 1)]))
	print(s - v)


for u in range(int(input())):
	n = int(input())
	x = [int(w) for w in input().split()]
	x = x[::-1]
	ans = 0
	for i in range(1, n):
		ans += abs(x[i] - x[i - 1])
	t = ans - abs(x[1] - x[0])
	for i in range(1, n - 1):
		t = min(t, ans + abs(x[i + 1] - x[i - 1]) - abs(x[i] - x[i - 1]) - abs(x[i] - x[i + 1]))
	t = min(t, ans - abs(x[n - 1] - x[n - 2]))
	print(t)


a = int(input())
for x in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	if len(list(set(c))) == 1:
		print(0)
	else:
		d = 0
		for y in range(b - 1):
			d += abs(c[y] - c[y + 1])
		h = min(d, d - abs(c[0] - c[1]), d - abs(c[-2] - c[-1]))
		for y in range(1, b - 1):
			h = min(h, d - (abs(c[y] - c[y - 1]) + abs(c[y + 1] - c[y]) - abs(c[y - 1] - c[y + 1])))
		print(h)


from sys import stdout, stdin, maxsize
from collections import defaultdict, deque
import math
import bisect
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	l = list(map(int, stdin.readline().split()))
	s = 0
	for i in range(n - 1, 0, -1):
		s += abs(l[i] - l[i - 1])
	ans = s - abs(l[0] - l[1])
	for i in range(1, n - 1):
		temp = s - abs(l[i] - l[i - 1]) - abs(l[i] - l[i + 1]) + abs(l[i - 1] - l[i + 1])
		if ans > temp:
			ans = temp
	ans = min(ans, s - abs(l[-1] - l[-2]))
	print(ans)


def solve():
	n = int(input())
	arr = [int(v) for v in input().split()]
	dp = [[0] * (n + 10) for _ in range(2)]
	for i in reversed(range(n - 1)):
		dp[1][i] = dp[1][i + 1] + abs(arr[i + 1] - arr[i])
		if i < n - 2:
			dp[0][i] = dp[0][i + 1] + abs(arr[i + 1] - arr[i])
			dp[0][i] = min(dp[0][i], dp[1][i + 2] + abs(arr[i + 2] - arr[i]))
	print(min(dp[0][0], dp[1][1]))
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	n = int(input())
	l = [0] + [int(i) for i in input().split()]
	sm = 0
	for i in range(2, n + 1):
		sm += abs(l[i] - l[i - 1])
	mx = max(abs(l[2] - l[1]), abs(l[-1] - l[-2]))
	for i in range(2, n):
		mx = max(mx, abs(l[i] - l[i - 1]) + abs(l[i + 1] - l[i]) - abs(l[i - 1] - l[i + 1]))
	print(sm - mx)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	j = 1
	x = 0
	while j <= len(a) - 1:
		x = x + abs(a[j] - a[j - 1])
		j = j + 1
	diffs = list()
	diffs.append(abs(a[1] - a[0]))
	diffs.append(abs(a[len(a) - 1] - a[len(a) - 2]))
	k = 1
	while k <= len(a) - 2:
		diffs.append(abs(a[k] - a[k - 1]) + abs(a[k + 1] - a[k]) - abs(a[k + 1] - a[k - 1]))
		k = k + 1
	y = max(diffs)
	print(x - y)
num_test = int(input())
for i in range(num_test):
	solve()


from sys import stdin
input = stdin.readline

def solve():
	n = int(input())
	l = list(map(int, input().split()))
	ans = 0
	for i in range(1, n):
		ans += abs(l[i] - l[i - 1])
	mx = max(abs(l[0] - l[1]), abs(l[n - 1] - l[n - 2]))
	for i in range(1, n - 1):
		mx = max(mx, abs(l[i] - l[i - 1]) + abs(l[i + 1] - l[i]) - abs(l[i + 1] - l[i - 1]))
	return ans - mx
for _ in range(int(input())):
	print(solve())


import sys, bisect
tc = int(sys.stdin.readline())
for _ in range(tc):
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	if n == 2:
		print(0)
	else:
		temp = []
		for i in range(1, n):
			temp.append(abs(arr[i] - arr[i - 1]))
		val = max(temp)
		now = sum(temp)
		ans = 98765432109876543210
		for i in range(len(temp)):
			if i == 0:
				left = now - temp[i]
				right = now - temp[i] - temp[i + 1] + abs(arr[i] - arr[i + 2])
				ans = min(ans, left, right)
			elif i == len(temp) - 1:
				left = now - temp[i] - temp[i - 1] + abs(arr[i + 1] - arr[i - 1])
				right = now - temp[i]
				ans = min(ans, left, right)
			else:
				left = now - temp[i] - temp[i - 1] + abs(arr[i + 1] - arr[i - 1])
				right = now - temp[i] - temp[i + 1] + abs(arr[i] - arr[i + 2])
				ans = min(ans, left, right)
		print(ans)


import sys, math
from collections import deque, defaultdict
import operator as op
from functools import reduce
I = sys.stdin.readline

def ii():
	return int(I().strip())

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

def mi():
	return map(int, I().strip().split())

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

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

def valid(row, col, rows, cols, rcross, lcross):
	return rows[row] == 0 and cols[col] == 0 and (rcross[col + row] == 0) and (lcross[col - row] == 0)

def div(n):
	tmp = []
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			cnt = 0
			while n % i == 0:
				n = n // i
				cnt += 1
			tmp.append((i, cnt))
	return tmp

def main():
	ans = ''
	for _ in range(ii()):
		n = ii()
		arr = li()
		tmp = 0
		for i in range(1, n):
			tmp += abs(arr[i] - arr[i - 1])
		mini = min(tmp, tmp - abs(arr[1] - arr[0]), tmp - abs(arr[n - 1] - arr[n - 2]))
		for i in range(1, n - 1):
			mini = min(mini, tmp - abs(arr[i] - arr[i - 1]) - abs(arr[i] - arr[i + 1]) + abs(arr[i - 1] - arr[i + 1]))
		ans += str(mini) + '\n'
	print(ans)
main()


t = int(input())
for k in range(t):
	n = int(input())
	lists = input()
	list1 = lists.split()
	for i in range(n):
		list1[i] = int(list1[i])
	ttl = 0
	for i in range(n - 1):
		ttl = ttl + abs(list1[i] - list1[i + 1])
	mn = min(ttl - abs(list1[0] - list1[1]), ttl - abs(list1[n - 2] - list1[n - 1]))
	for i in range(1, n - 1):
		mn = min(mn, ttl - (abs(list1[i] - list1[i - 1]) + abs(list1[i] - list1[i + 1]) - abs(list1[i - 1] - list1[i + 1])))
	print(mn)


def read_generator():
	while True:
		tokens = input().split(' ')
		for t in tokens:
			yield t
reader = read_generator()

def readword():
	return next(reader)

def readint():
	return int(next(reader))

def readfloat():
	return float(next(reader))

def readline():
	return input()

def solve(a, n):
	d = 0
	p = 0
	for i in range(1, n - 1):
		if a[i - 1] > a[i] < a[i + 1]:
			if abs(a[i + 1] - a[i] + a[i - 1] - a[i]) - abs(a[i - 1] - a[i + 1]) > d:
				d = abs(a[i + 1] - a[i] + a[i - 1] - a[i]) - abs(a[i - 1] - a[i + 1])
				p = i
		if a[i - 1] < a[i] > a[i + 1]:
			if abs(a[i] - a[i + 1] + a[i] - a[i - 1]) - abs(a[i - 1] - a[i + 1]) > d:
				d = abs(a[i] - a[i + 1] + a[i] - a[i - 1]) - abs(a[i - 1] - a[i + 1])
				p = i
	if abs(a[0] - a[1]) > d:
		p = 0
		d = abs(a[0] - a[1])
	if abs(a[-1] - a[-2]) > d:
		a[-1] = a[-2]
	else:
		a[p] = a[p + 1]
	res = 0
	for i in range(n - 1, 0, -1):
		res += abs(a[i] - a[i - 1])
	return res
tests = readint()
for test in range(tests):
	n = readint()
	a = [readint() for _ in range(n)]
	print(solve(a, n))


def solve():
	n = int(input())
	a = [0] + list(map(int, input().split()))
	ans = 0
	for i in range(2, n + 1):
		ans += abs(a[i] - a[i - 1])
	mx = max(abs(a[2] - a[1]), abs(a[n] - a[n - 1]))
	for i in range(2, n):
		mx = max(mx, abs(a[i] - a[i - 1]) + abs(a[i + 1] - a[i]) - abs(a[i + 1] - a[i - 1]))
	print(ans - mx)
T = int(input())
for Case in range(T):
	solve()


def solve(arr):
	n = len(arr)
	if n <= 2:
		return 0
	diff = []
	for i in range(n - 1):
		diff.append(abs(arr[i + 1] - arr[i]))
	ans = sum(diff)
	sum_diff = sum(diff)
	for i in range(n):
		if i == 0:
			ans = min(ans, sum_diff - diff[0])
		elif i == n - 1:
			ans = min(ans, sum_diff - diff[-1])
		else:
			ans = min(ans, sum_diff - diff[i - 1] - diff[i] + abs(arr[i + 1] - arr[i - 1]))
	return ans
t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	print(solve(arr))


def func(a, n):
	if n < 3:
		return 0
	diff = sum([abs(a[i] - a[i - 1]) for i in range(1, n)])
	sub = max(abs(a[0] - a[1]), abs(a[-1] - a[-2]))
	for i in range(n - 2):
		if a[i] < a[i + 1] and a[i + 1] > a[i + 2]:
			chng = a[i + 1] - a[i] + a[i + 1] - a[i + 2] - abs(a[i] - a[i + 2])
			sub = max(sub, chng)
		elif a[i] > a[i + 1] and a[i + 1] < a[i + 2]:
			chng = -a[i + 1] + a[i] - a[i + 1] + a[i + 2] - abs(a[i] - a[i + 2])
			sub = max(sub, chng)
	return diff - sub
for _ in range(int(input())):
	n = int(input())
	a = [int(j) for j in input().split()]
	print(func(a, n))


from os import path
import sys, time
from math import ceil, floor, gcd, log, log2, factorial
from collections import *
from bisect import *
maxx = float('inf')
I = lambda : int(sys.stdin.buffer.readline())
tup = lambda : map(int, sys.stdin.buffer.readline().split())
lint = lambda : [int(x) for x in sys.stdin.buffer.readline().split()]
S = lambda : sys.stdin.readline().replace('\n', '').strip()

def grid(r, c):
	return [lint() for i in range(r)]
stpr = lambda x: sys.stdout.write(f'{x}' + '\n')
star = lambda x: print(' '.join(map(str, x)))
localsys = 0
start_time = time.time()
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for _ in range(I()):
	n = I()
	ls = lint()
	tot = sum((abs(a - b) for (a, b) in zip(ls, ls[1:])))
	ans = max(abs(ls[1] - ls[0]), abs(ls[-1] - ls[-2]))
	for (i, j, k) in zip(ls, ls[1:], ls[2:]):
		(p, q) = (abs(i - j) + abs(j - k), abs(i - k))
		ans = max(ans, p - q)
	print(tot - ans)
if localsys:
	print('\n\nTime Elased :', time.time() - start_time, 'seconds')


t = int(input())

def main(arr, n):
	i = -1
	ans = 0
	while i > -n:
		ans += abs(arr[i] - arr[i - 1])
		i -= 1
	return ans
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	i = 0
	a = 0
	while i < n - 1:
		if i != 0:
			a = max(a, abs(arr[i + 1] - arr[i]) - abs(arr[i + 1] - arr[i - 1]) + abs(arr[i] - arr[i - 1]))
		else:
			a = max(a, abs(arr[i + 1] - arr[i]))
		i += 1
	b = 0
	i = -1
	while i > -n:
		if i != -1:
			b = max(b, abs(arr[i] - arr[i - 1]) - abs(arr[i + 1] - arr[i - 1]) + abs(arr[i + 1] - arr[i]))
		else:
			b = max(b, abs(arr[i] - arr[i - 1]))
		i -= 1
	ans = main(arr, n) - max(a, b)
	print(ans)


import math
t = int(input())

def solve():
	n = int(input())
	vals = list(map(int, input().split()))
	diffs = []
	for x in range(1, n):
		diffs.append(abs(vals[x] - vals[x - 1]))
	sdiff = sum(diffs)
	ans = min(sdiff - diffs[0], sdiff - diffs[n - 2])
	for x in range(1, n - 1):
		tans = sdiff - (diffs[x] + diffs[x - 1])
		ans = min(ans, tans + abs(vals[x - 1] - vals[x + 1]))
	return ans
for i in range(t):
	output = solve()
	print(output)


import sys

def solve():
	t = int(input())
	arr = list(map(int, input().split()))
	c = 0
	for i in range(len(arr) - 1, 0, -1):
		c += abs(arr[i] - arr[i - 1])
	dec = []
	for i in range(1, len(arr) - 1):
		dec.append(abs(arr[i] - arr[i - 1]) + abs(arr[i + 1] - arr[i]) - abs(arr[i + 1] - arr[i - 1]))
	dec = max(dec) if len(dec) != 0 else 0
	c -= max(abs(arr[1] - arr[0]), abs(arr[-1] - arr[-2]), dec)
	print(c)
num = int(input())
for n in range(num):
	solve()


from sys import stdin
import math
input = stdin.readline
for _ in range(int(input())):
	(n,) = map(int, input().split())
	l = list(map(int, input().split()))
	s = 0
	for j in range(n - 1):
		s += abs(l[j] - l[j + 1])
	mx = max(abs(l[1] - l[0]), abs(l[n - 1] - l[n - 2]))
	for i in range(1, n - 1):
		mx = max(mx, abs(l[i] - l[i + 1]) + abs(l[i] - l[i - 1]) - abs(l[i + 1] - l[i - 1]))
	print(s - mx)


for _ in range(int(input())):
	n = int(input())
	steps = list(map(int, input().strip().split()))
	res = 0
	maxstepdiff = 0
	for (i, step) in enumerate(steps):
		if i == 0:
			maxstepdiff = max(maxstepdiff, abs(steps[i + 1] - step))
		elif i == n - 1:
			maxstepdiff = max(maxstepdiff, abs(steps[i - 1] - step))
		elif step >= steps[i - 1] and step >= steps[i + 1] or (step <= steps[i - 1] and step <= steps[i + 1]):
			maxstepdiff = max(maxstepdiff, 2 * min(abs(steps[i - 1] - step), abs(steps[i + 1] - step)))
		if i != 0:
			res += abs(steps[i - 1] - step)
	print(res - maxstepdiff)


for y in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	dif = [0] * (n - 1)
	for i in range(n - 2, -1, -1):
		dif[i] = abs(lst[i] - lst[i + 1])
	summ = sum(dif)
	minn = 100000000000000001
	for i in range(n):
		if i == 0:
			minn = min(minn, summ - dif[0])
		elif i == n - 1:
			minn = min(minn, summ - dif[-1])
		else:
			minn = min(minn, summ - dif[i - 1] - dif[i] + abs(lst[i - 1] - lst[i + 1]))
	print(minn)


import sys
import math, bisect, operator
(inf, m) = (float('inf'), 10 ** 9 + 7)
sys.setrecursionlimit(10 ** 5)
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, defaultdict
I = lambda : int(sys.stdin.readline())
neo = lambda : map(int, sys.stdin.readline().split())
Neo = lambda : list(map(int, sys.stdin.readline().split()))
for _ in range(I()):
	n = I()
	A = Neo()[::-1]
	Ans = 0
	for (i, j) in zip(A, A[1:]):
		Ans += abs(i - j)
	tot = Ans
	for (i, j, k) in zip(A, A[1:], A[2:]):
		Ans = min(Ans, tot - abs(i - j) - abs(j - k) + abs(i - k))
	Ans = min(Ans, tot - abs(A[0] - A[1]), tot - abs(A[-1] - A[-2]))
	print(Ans)


import sys
import bisect, string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby
rep = range
R = range

def Golf():
	(n, *t) = map(int, open(0).read().split())

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

def S_():
	return input()

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

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

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 NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def NLI(n):
	return [[int(i) for i in input().split()] for i in range(n)]

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

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def RA():
	return map(int, open(0).read().split())

def RLI(n=8, a=1, b=10):
	return [random.randint(a, b) for i in range(n)]

def RI(a=1, b=10):
	return random.randint(a, b)

def INP():
	N = 9
	n = random.randint(1, N)
	m = random.randint(1, n * n)
	A = [random.randint(1, n) for i in range(m)]
	B = [random.randint(1, n) for i in range(m)]
	G = [[] for i in range(n)]
	RG = [[] for i in range(n)]
	for i in range(m):
		(a, b) = (A[i] - 1, B[i] - 1)
		if a == b:
			continue
		G[a] += ((b, 1),)
		RG[b] += ((a, 1),)
	return (n, m, G, RG)

def Rtest(T):
	(case, err) = (0, 0)
	for i in range(T):
		inp = INP()
		a1 = naive(*inp)
		a2 = solve(*inp)
		if a1 == a2:
			print(inp)
			print('naive', a1)
			print('solve', a2)
			err += 1
		case += 1
	print('Tested', case, 'case with', err, 'errors')

def GI(V, E, ls=None, Directed=False, index=1):
	org_inp = []
	g = [[] for i in range(V)]
	FromStdin = True if ls == None else False
	for i in range(E):
		if FromStdin:
			inp = LI()
			org_inp.append(inp)
		else:
			inp = ls[i]
		if len(inp) == 2:
			(a, b) = inp
			c = 1
		else:
			(a, b, c) = inp
		if index == 1:
			a -= 1
			b -= 1
		aa = (a, c)
		bb = (b, c)
		g[a].append(bb)
		if not Directed:
			g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}, boundary=1):
	mp = [boundary] * (w + 2)
	found = {}
	for i in R(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [boundary] + [mp_def[j] for j in s] + [boundary]
	mp += [boundary] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def accum(ls):
	rt = [0]
	for i in ls:
		rt += [rt[-1] + i]
	return rt

def bit_combination(n, base=2):
	rt = []
	for tb in R(base ** n):
		s = [tb // base ** bt % base for bt in R(n)]
		rt += [s]
	return rt

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

def YN(x):
	print(['NO', 'YES'][x])

def Yn(x):
	print(['No', 'Yes'][x])

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
mo = 10 ** 9 + 7
inf = float('inf')
FourNb = [(-1, 0), (1, 0), (0, 1), (0, -1)]
EightNb = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
compas = dict(zip('WENS', FourNb))
cursol = dict(zip('LRUD', FourNb))
l_alp = string.ascii_lowercase
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
input = lambda : sys.stdin.readline().rstrip()
show_flg = False
show_flg = True
ans = 0
for _ in range(I()):
	ans = 0
	n = I()
	a = LI()
	a = [a[0]] + a + [a[-1]]
	n += 2
	b = []
	c = []
	for i in range(n - 1):
		b += (abs(a[i + 1] - a[i]),)
		c += (a[i + 1] - a[i],)
	tmp = max(b[1], b[-2])
	for i in range(n - 2):
		(x, y) = (c[i], c[i + 1])
		if x * y <= 0:
			tmp = max(tmp, abs(x) + abs(y) - abs(y + x))
	ans = sum(b) - tmp
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input()) - 1
	A = list(map(int, input().split()))
	C = []
	for (i, v) in enumerate(A):
		if i == 0:
			C.append(0)
			continue
		C.append(C[i - 1] + abs(v - A[i - 1]))
	ans = None
	d = 0
	for i in range(n, -1, -1):
		if i == n:
			ans = min(C[i], C[i - 1])
			continue
		if i == 0:
			ans = min(ans, d)
			continue
		ans = min(ans, C[i - 1] + d + abs(A[i + 1] - A[i - 1]))
		d += abs(C[i] - C[i + 1])
	print(ans)


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


for i in range(int(input())):
	input()
	a = list(map(int, input().split()))
	b = list(map(lambda x, y: x - y, a, a[1:]))
	c = [abs(b[0]), abs(b[-1])]
	ans = 0
	for i in b:
		if i > 0 and ans < 0 or (i < 0 and ans > 0):
			c.append(2 * min(abs(i), abs(ans)))
		ans = i
	b = list(map(abs, b))
	print(sum(b) - max(c))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	deltas = []
	for i in range(1, n):
		deltas.append(arr[i] - arr[i - 1])
	minus = 0
	for i in range(len(deltas) - 1):
		(a, b) = (deltas[i], deltas[i + 1])
		if a > 0 and b < 0:
			minus = max(minus, min(a, -b) * 2)
		elif a < 0 and b > 0:
			minus = max(minus, min(-a, b) * 2)
	minus = max(minus, abs(deltas[0]))
	minus = max(minus, abs(deltas[-1]))
	d2 = [abs(i) for i in deltas]
	print(sum(d2) - minus)


class Solution:

	def solution(self, nums):
		if len(nums) <= 2:
			return 0
		diff = []
		for i in range(1, len(nums)):
			diff.append(nums[i] - nums[i - 1])
		max_sub = 0
		for i in range(1, len(diff)):
			if diff[i] * diff[i - 1] >= 0:
				continue
			max_sub = max(max_sub, 2 * min(abs(diff[i]), abs(diff[i - 1])))
		max_sub = max(max_sub, abs(diff[0]), abs(diff[-1]))
		return sum([abs(num) for num in diff]) - max_sub
t = int(input())
for _ in range(t):
	n = int(input())
	nums = [int(num) for num in input().split()]
	sol = Solution()
	print(sol.solution(nums))


import os
from sys import stdin, stdout

def solve(tc):
	n = int(stdin.readline().strip())
	seq = list(map(int, stdin.readline().split()))
	tot = 0
	for i in range(1, n):
		seq[i - 1] -= seq[i]
		tot += abs(seq[i - 1])
	seq[n - 1] = 0
	maxi = max(abs(seq[0]), abs(seq[n - 2]))
	for i in range(n - 1):
		maxi = max(maxi, abs(seq[i]) + abs(seq[i + 1]) - abs(seq[i] + seq[i + 1]))
	print(tot - maxi)
tcs = 1
tcs = int(stdin.readline().strip())
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


t = int(input())
for case in range(t):
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	s = 0
	for i in range(1, n):
		s += abs(a[i] - a[i - 1])
	m = max(abs(a[1] - a[0]), abs(a[-1] - a[-2]))
	for i in range(1, n - 1):
		m = max(m, abs(a[i + 1] - a[i]) + abs(a[i] - a[i - 1]) - abs(a[i + 1] - a[i - 1]))
	print(s - m)


t = int(input())

def main(n, d):
	s = 0
	m = 0
	for i in range(1, n):
		s += abs(d[i] - d[i - 1])
		a = abs(d[i] - d[i - 1]) + (abs(d[i] - d[i + 1]) if i != n - 1 else 0) - (abs(d[i - 1] - d[i + 1]) if i != n - 1 else 0)
		if a > m:
			m = a
	if abs(d[1] - d[0]) > m:
		m = abs(d[1] - d[0])
	return s - m
for _ in range(t):
	n = int(input())
	(*d,) = map(int, input().split())
	print(main(n, d))


import sys
import math
input = sys.stdin.readline
tests = int(input())
for test in range(tests):
	n = int(input())
	array = list(map(int, input().split()))
	diff = 0
	lastInteger = array[0]
	secondLastInteger = array[0]
	largestJump = abs(array[0] - array[1])
	replacementJump = 0
	for integer in array:
		diff += abs(lastInteger - integer)
		currentJump = abs(secondLastInteger - lastInteger) + abs(lastInteger - integer)
		currentReplacementJump = abs(secondLastInteger - integer)
		if currentJump - currentReplacementJump > largestJump - replacementJump:
			largestJump = currentJump
			replacementJump = currentReplacementJump
		secondLastInteger = lastInteger
		lastInteger = integer
	jump = abs(array[-1] - array[-2])
	if jump > largestJump - replacementJump:
		largestJump = jump
		replacementJump = 0
	print(diff - largestJump + replacementJump)


def foo(l):
	n = len(l)
	count = 0
	for i in range(1, n):
		count += abs(l[i] - l[i - 1])
	return count
t = int(input())
for f in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	if n == 2:
		print(0)
	else:
		l1 = []
		out = foo(l)
		l1.append(abs(l[1] - l[0]))
		l1.append(abs(l[-1] - l[-2]))
		for i in range(1, len(l) - 1):
			l1.append(abs(l[i + 1] - l[i]) + abs(l[i] - l[i - 1]) - abs(l[i + 1] - l[i - 1]))
		print(out - max(l1))


t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	lst = arr.copy()
	if n == 2:
		print(0)
	else:
		cnt = reductn = 0
		for i in range(n - 1, 0, -1):
			cnt += abs(lst[i] - lst[i - 1])
			lst[i] = lst[i - 1]
			if i != n - 1:
				k = abs(arr[i] - arr[i - 1]) + abs(arr[i + 1] - arr[i]) - abs(arr[i + 1] - arr[i - 1])
				if reductn < k:
					reductn = k
		if reductn < abs(arr[0] - arr[1]):
			reductn = abs(arr[0] - arr[1])
		if reductn < abs(arr[-1] - arr[-2]):
			reductn = abs(arr[-1] - arr[-2])
		print(cnt - reductn)


import sys
input = sys.stdin.readline
from collections import *
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = 0
	for i in range(n - 1):
		s += abs(a[i + 1] - a[i])
	ans = s
	for i in range(n):
		if i == 0:
			ans = min(ans, s - abs(a[i + 1] - a[i]))
		elif i == n - 1:
			ans = min(ans, s - abs(a[i - 1] - a[i]))
		else:
			ans = min(ans, s - abs(a[i - 1] - a[i]) - abs(a[i + 1] - a[i]) + abs(a[i + 1] - a[i - 1]))
	print(ans)


from sys import stdin, stdout

def main():
	for _ in range(int(stdin.readline())):
		n = int(stdin.readline())
		a = list(map(int, stdin.readline().split()))
		ans = 0
		for i in range(1, n):
			ans += abs(a[i] - a[i - 1])
		temp = max(abs(a[1] - a[0]), abs(a[n - 1] - a[n - 2]))
		for i in range(1, n - 1):
			temp = max(temp, abs(a[i] - a[i - 1]) + abs(a[i + 1] - a[i]) - abs(a[i + 1] - a[i - 1]))
		stdout.write('%d\n' % (ans - temp))
main()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	tot_diff = 0
	max_diff = 0
	pos = 1
	if n > 2:
		for i in range(1, n):
			tot_diff += abs(arr[i] - arr[i - 1])
		tot = tot_diff
		for i in range(1, n - 1):
			temp = tot + abs(arr[i - 1] - arr[i + 1]) - (abs(arr[i] - arr[i - 1]) + abs(arr[i] - arr[i + 1]))
			tot_diff = min(tot_diff, temp)
		temp = max(abs(arr[0] - arr[1]), abs(arr[n - 1] - arr[n - 2]))
		tot_diff = min(tot_diff, tot - temp)
		print(tot_diff)
	else:
		print(0)


import sys

def input():
	return sys.stdin.readline().rstrip()
t = int(input())
for i in range(t):
	n = int(input())
	v = list(map(int, input().split()))
	adv = min(-1 * abs(v[0] - v[1]), -1 * abs(v[n - 2] - v[n - 1]))
	ans = 0
	for j in range(1, n):
		ans += abs(v[j - 1] - v[j])
	for j in range(1, n - 1):
		adv = min(adv, abs(v[j + 1] - v[j - 1]) - abs(v[j - 1] - v[j]) - abs(v[j + 1] - v[j]))
	print(ans + adv)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
from heapq import *
from math import gcd, factorial, floor, ceil, sqrt
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def testcase(t):
	for pp in range(t):
		solve(pp)

def google(p):
	print('Case #' + str(p) + ': ', end='')

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

def power(x, y, p):
	y %= p - 1
	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 ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
inf = pow(10, 20)
mod = 10 ** 9 + 7
dp = [[-1, -1] for i in range(200010)]

def rec(pos, a, passed, diff):
	if pos >= len(a):
		return 0
	if dp[pos][passed] != -1:
		return dp[pos][passed]
	temp = inf
	if not passed:
		temp = min(temp, rec(pos + 1, a, 1 - passed, diff))
	temp = min(temp, abs(a[0] - (a[pos] + diff)) + rec(pos + 1, a, passed, diff + a[0] - (a[pos] + diff)))
	return temp

def solve(case):
	n = int(inp())
	a = lis()
	pre = [0] * n
	for i in range(1, n):
		pre[i] += pre[i - 1]
		pre[i] += abs(a[i] - a[i - 1])
	ans = pre[n - 2]
	suff = 0
	for i in range(n - 2, 0, -1):
		curr = pre[i - 1] + abs(a[i + 1] - a[i - 1]) + suff
		suff += abs(a[i + 1] - a[i])
		ans = min(ans, curr)
	print(min(ans, suff))
testcase(int(inp()))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	for i in range(1, n):
		res += abs(a[i] - a[i - 1])
	sub = max(abs(a[1] - a[0]), abs(a[n - 1] - a[n - 2]))
	for i in range(1, n - 1):
		sub = max(sub, abs(a[i] - a[i - 1]) + abs(a[i + 1] - a[i]) - abs(a[i + 1] - a[i - 1]))
	print(res - sub)


import sys
import bisect
from bisect import bisect_right
import math
from sys import stdin, stdout

def sieve(N):
	primeNumbers = [True] * (N + 1)
	primeNumbers[0] = False
	primeNumbers[1] = False
	i = 2
	while i * i <= N:
		j = i
		if primeNumbers[j]:
			while j * i <= N:
				primeNumbers[j * i] = False
				j += 1
		i += 1
	return primeNumbers

def getPrime(N):
	primes = sieve(N)
	result = []
	for i in range(len(primes)):
		if primes[i]:
			result.append(i)
	return result

def factor(N):
	factors = []
	i = 1
	while i * i <= N:
		if N % i == 0:
			factors.append(i)
			if i != N // i:
				factors.append(N // i)
		i += 1
	return sorted(factors)

def gcd(a, b):
	if a < b:
		return gcd(b, a)
	if b == 0:
		return a
	return gcd(b, a % b)

def extendedGcd(a, b):
	if a < b:
		return extendedGcd(b, a)
	if b == 0:
		return [a, 1, 0]
	res = extendedGcd(b, a % b)
	x = res[2]
	y = res[1] - math.floor(a / b) * res[2]
	res[1] = x
	res[2] = y
	return res

def iterativeModularFunc(a, b, c):
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % c
		a = a * a % c
		b = b // 2
	return res

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

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

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

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

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

def Solution(arr, n):
	ans = 0
	for i in range(1, n):
		ans += abs(arr[i] - arr[i - 1])
	mx = max(abs(arr[1] - arr[0]), abs(arr[n - 1] - arr[n - 2]))
	for i in range(1, n - 1):
		mx = max(mx, abs(arr[i] - arr[i - 1]) + abs(arr[i + 1] - arr[i]) - abs(arr[i + 1] - arr[i - 1]))
	print(ans - mx)

def main():
	for _ in range(get_int()):
		n = get_int()
		arr = get_ints_in_list()
		Solution(arr, n)
main()


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 2:
		print(0)
	else:
		ans = 0
		dec = max(abs(arr[1] - arr[0]), abs(arr[n - 1] - arr[n - 2]))
		for i in range(1, n):
			ans += abs(arr[i - 1] - arr[i])
		for i in range(1, n - 1):
			dec = max(dec, abs(arr[i] - arr[i - 1]) + abs(arr[i + 1] - arr[i]) - abs(arr[i + 1] - arr[i - 1]))
		print(ans - dec)


from sys import stdin
from sys import stdout
from math import *
input = stdin.readline
for __ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	for i in range(1, n):
		ans += abs(a[i] - a[i - 1])
	ans1 = ans - abs(a[0] - a[1])
	ans2 = ans - abs(a[-1] - a[-2])
	temp = 0
	for i in range(1, n - 1):
		temp = max(temp, abs(a[i] - a[i - 1]) + abs(a[i] - a[i + 1]) - abs(a[i + 1] - a[i - 1]))
	print(min(ans1, ans2, ans - temp))


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = []
	for x in range(1, n):
		b.append(abs(a[x] - a[x - 1]))
	sss = sum(b)
	now = min(sss, sss - abs(a[0] - a[1]), sss - abs(a[n - 1] - a[n - 2]))
	for x in range(1, n - 1):
		now = min(now, sss - b[x - 1] - b[x] + abs(a[x + 1] - a[x - 1]))
	print(now)


import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	ans = [abs(A[-1] - A[-2])]
	s = abs(A[-1] - A[-2])
	for i in range(n - 2, 0, -1):
		ans.append(abs(A[i + 1] - A[i]) + abs(A[i] - A[i - 1]) - abs(A[i + 1] - A[i - 1]))
		s = s + abs(A[i] - A[i - 1])
	ans.append(abs(A[1] - A[0]))
	val = s
	for i in range(n):
		val = min(val, s - ans[i])
	print(val)


import sys
import heapq
import copy
import math
import decimal
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 insr2():
	s = input()
	s = list(s[:len(s) - 1])
	s = [ord(s[i]) - ord('a') for i in range(len(s))]
	return s

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

def pr_list(a):
	print(*a, sep=' ')

def swap(a, b):
	temp = a
	a = b
	b = temp
	return [a, b]
mod = 1000000007

def pri(a, b):
	s = '? ' + str(a) + ' ' + str(b)
	print(s)
	sys.stdout.flush()

def solve():
	n = inp()
	arr = inlt()
	max_gain = abs(arr[1] - arr[0])
	index = 0
	for i in range(1, n - 1):
		pre = arr[i - 1]
		after = arr[i + 1]
		gain = abs(arr[i] - after) + abs(arr[i] - pre) - abs(pre - after)
		if gain > max_gain:
			max_gain = gain
			index = i
	gain = abs(arr[n - 1] - arr[n - 2])
	if gain > max_gain:
		max_gain = gain
		index = n - 1
	arr.pop(index)
	ans = 0
	for i in range(1, n - 1):
		ans = ans + abs(arr[i] - arr[i - 1])
	print(ans)

def main():
	test = 1
	test = inp()
	for i in range(test):
		solve()
main()


def suffixOperations(n, array):
	a = []
	a.append(abs(array[1] - array[0]))
	a.append(abs(array[-1] - array[-2]))
	for x in range(1, n - 1):
		p = (array[x - 1] + array[x + 1]) // 2
		a.append(abs(array[x - 1] - array[x]) + abs(array[x + 1] - array[x]) - (abs(array[x - 1] - p) + abs(array[x + 1] - p)))
	ans = 0
	for x in range(n - 1):
		ans += abs(array[x] - array[x + 1])
	ans -= max(a)
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	print(suffixOperations(n, l))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, values) = (int(input()), [int(i) for i in input().split()])
	(total, best) = (sum([abs(values[i] - values[i - 1]) for i in range(1, n)]), max(abs(values[0] - values[1]), abs(values[-1] - values[-2])))
	for i in range(1, n - 1):
		best = max(best, abs(values[i - 1] - values[i]) + abs(values[i + 1] - values[i]) - abs(values[i + 1] - values[i - 1]))
	print(total - best)


from sys import stdin, stdout
input = stdin.readline
print2 = stdout.write

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

def get_list():
	return tuple(map(int, stdin.readline().split()))
for _ in range(int(input())):
	n = int(input())
	a = get_list()
	diff_next = abs(a[1] - a[0])
	profit_max = diff_next
	total_diff = 0
	for i in range(1, n):
		diff = diff_next
		if i == n - 1:
			profit = diff
		else:
			diff_next = abs(a[i + 1] - a[i])
			profit = diff + diff_next - abs(a[i + 1] - a[i - 1])
		if profit > profit_max:
			profit_max = profit
		total_diff += diff
	print(total_diff - profit_max)


number = int(input(''))

def sumAbs(dif):
	sum = 0
	for i in range(len(dif)):
		sum += abs(dif[i])
	return sum
for j in range(number):
	n = int(input(''))
	numbers = []
	num = input('')
	num = num.split(' ')
	difference = []
	for i in range(n):
		numbers.append(int(num[i]))
	for i in range(n - 1):
		difference.append(numbers[i + 1] - numbers[i])
	maxchange = 0
	maxchangeloc = -1
	newchange = 0
	last = False
	first = False
	for i in range(len(difference) - 1):
		adding = abs(difference[i] + difference[i + 1])
		change = abs(difference[i]) + abs(difference[i + 1]) - adding
		if change > maxchange:
			newchange = adding
			maxchange = change
			changeloc = i
	if abs(difference[-1]) > maxchange:
		maxchange = abs(difference[-1])
		last = True
	if abs(difference[0]) > maxchange:
		last = False
		first = True
	if maxchange == 0 and (not last) and (not first):
		print(sumAbs(difference))
	else:
		if last:
			sum1 = 0
			del difference[-1]
		elif first:
			sum1 = 0
			del difference[0]
		else:
			sum1 = abs(newchange)
			del difference[changeloc]
			del difference[changeloc]
		sum1 += sumAbs(difference)
		print(sum1)


t = int(input())
for i in range(t):
	n = int(input())
	data = input().split(' ')
	if n < 3:
		print(0)
	else:
		data = list(map(int, data))
		total = 0
		diff = [0] * (len(data) - 1)
		for j in range(len(data) - 1):
			diff[j] = abs(data[j + 1] - data[j])
			total += diff[j]
		best = max(diff[0], diff[-1])
		for j in range(len(data) - 2):
			x = abs(data[j + 2] - data[j])
			save = diff[j] + diff[j + 1] - x
			if save > best:
				best = save
		print(total - best)


for nt in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	for i in range(1, n):
		ans += abs(a[i] - a[i - 1])
	dec = max(abs(a[1] - a[0]), abs(a[-1] - a[-2]))
	for i in range(1, n - 1):
		dec = max(dec, abs(a[i] - a[i - 1]) + abs(a[i] - a[i + 1]) - abs(a[i - 1] - a[i + 1]))
	print(ans - dec)


import sys, os, io

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

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

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

def ws(s):
	sys.stdout.write(s + '\n')

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

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max, nsmallest, nlargest

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	tc = ri()
	for _ in range(tc):
		n = ri()
		a = ria()
		md = 0
		c = 0
		for i in range(n - 1):
			md += abs(a[i] - a[i + 1])
		ans = md
		a = [a[0]] + a + [a[-1]]
		for i in range(1, n + 1):
			ps = md
			yahakahataoitna = abs(a[i] - a[i - 1]) + abs(a[i] - a[i + 1])
			nayakivalue = a[i - 1]
			ps -= yahakahataoitna
			if i != n:
				laoitna = abs(nayakivalue - a[i + 1])
			else:
				laoitna = 0
			ps += laoitna
			ans = min(ans, ps)
		for i in range(1, n + 1):
			ps = md
			yahakahataoitna = abs(a[i] - a[i - 1]) + abs(a[i] - a[i + 1])
			nayakivalue = a[i + 1]
			ps -= yahakahataoitna
			if i != 1:
				laoitna = abs(nayakivalue - a[i - 1])
			else:
				laoitna = 0
			ps += laoitna
			ans = min(ans, ps)
		print(ans)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			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, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			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()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

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

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


T = int(input())
for _ in range(T):
	N = int(input())
	arr = list(map(int, input().split()))
	count = [abs(arr[0] - arr[1]), abs(arr[0] - arr[1])]
	count2 = [0, 0]
	for i in range(2, len(arr)):
		count.append(abs(arr[i] - arr[i - 1]))
		count2.append(abs(arr[i] - arr[i - 2]))
	if len(arr) > 2:
		res = sum(count) - count[0]
		real_res = res
		if real_res > res - count[1]:
			real_res = res - count[1]
		if real_res > res - count[len(arr) - 1]:
			real_res = res - count[len(arr) - 1]
		for i in range(2, len(arr)):
			if real_res > res - count[i - 1] - count[i] + count2[i]:
				real_res = res - count[i - 1] - count[i] + count2[i]
		print(real_res)
	else:
		print(0)


import math
import bisect
import itertools
import sys
I = lambda : sys.stdin.readline()
one = lambda : int(I())
more = lambda : map(int, I().split())
linput = lambda : list(more())
mod = 10 ** 9 + 7
inf = 10 ** 18 + 1

def merge(a, b):
	i = 0
	j = 0
	c = 0
	ans = []
	while i < len(a) and j < len(b):
		if a[i] < b[j]:
			ans.append(a[i])
			i += 1
		else:
			ans.append(b[j])
			c += len(a) - i
			j += 1
	ans += a[i:]
	ans += b[j:]
	return (ans, c)

def mergesort(a):
	if len(a) == 1:
		return (a, 0)
	mid = len(a) // 2
	(left, left_inversion) = mergesort(a[:mid])
	(right, right_inversion) = mergesort(a[mid:])
	(m, c) = merge(left, right)
	c += left_inversion + right_inversion
	return (m, c)

def is_prime(num):
	if num == 1:
		return False
	if num == 2:
		return True
	if num == 3:
		return True
	if num % 2 == 0:
		return False
	if num % 3 == 0:
		return False
	t = 5
	a = 2
	while t <= int(math.sqrt(num)):
		if num % t == 0:
			return False
		t += a
		a = 6 - a
	return True

def ceil(a, b):
	return (a + b - 1) // b
for _ in range(one()):
	n = one()
	a = linput()
	left = [0] * n
	right = [0] * n
	for i in range(n - 1, 0, -1):
		left[i - 1] = left[i] + abs(a[i] - a[i - 1])
	for i in range(n - 1):
		right[i + 1] = right[i] + abs(a[i + 1] - a[i])
	ans = min(left[1], right[n - 2])
	for i in range(1, n - 1):
		x = left[i + 1] + right[i - 1] + abs(a[i - 1] - a[i + 1])
		ans = min(ans, x)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	diff = 0
	pos = 0
	val = 0
	if n == 1:
		print(0)
		continue
	for i in range(n):
		if i == 0:
			diff = abs(arr[i + 1] - arr[i])
			val = arr[i + 1]
			pos = i
		elif i == n - 1:
			if abs(arr[i] - arr[i - 1]) > diff:
				diff = abs(arr[i] - arr[i - 1])
				val = arr[i - 1]
				pos = i
		else:
			f = abs(arr[i + 1] - arr[i])
			s = abs(arr[i - 1] - arr[i])
			initial = f + s
			fr = abs(arr[i - 1] - arr[i + 1])
			if abs(fr - initial) > diff:
				diff = abs(fr - initial)
				val = arr[i - 1]
				pos = i
	arr[pos] = val
	count = 0
	for i in range(n - 1, 0, -1):
		count = count + abs(arr[i] - arr[i - 1])
	print(count)


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, A):
	freq = Counter(A)
	if len(freq) == 1 or (len(freq) == 2 and any((v == 1 for v in freq.values()))):
		return 0
	delta = 0
	for (x, y) in zip(A, A[1:]):
		delta += abs(y - x)
	best = max(abs(A[1] - A[0]), abs(A[-1] - A[-2]))
	for (x, y, z) in zip(A, A[1:], A[2:]):
		best = max(best, abs(x - y) + abs(y - z) - abs(x - z))
	return delta - best
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N,) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	ans = solve(N, A)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	large = 0
	ans = 0
	for i in range(1, n):
		ans += abs(l[i] - l[i - 1])
	for i in range(1, n - 1):
		large = max(large, abs(l[i] - l[i - 1]) + abs(l[i + 1] - l[i]) - abs(l[i + 1] - l[i - 1]))
	print(ans - max(large, abs(l[1] - l[0]), abs(l[-1] - l[-2])))


t = int(input())
for z in range(t):
	n = int(input())
	q = set()
	arr = list(map(int, input().split()))
	count = 0
	for i in range(n - 1):
		count = count + abs(arr[i + 1] - arr[i])
	for i in range(1, n - 1):
		q.add(count - abs(arr[i + 1] - arr[i]) - abs(arr[i] - arr[i - 1]) + abs(arr[i + 1] - arr[i - 1]))
	q.add(count - abs(arr[1] - arr[0]))
	q.add(count - abs(arr[n - 1] - arr[n - 2]))
	print(min(q))


import sys
from collections import defaultdict as dd
from collections import Counter as cc
from queue import Queue
import math
import itertools
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = lambda : sys.stdin.readline().rstrip()
for _ in range(int(input())):
	q = int(input())
	a = list(map(int, input().split()))
	b = []
	d = 0
	for i in range(1, q):
		b.append(a[i - 1] - a[i])
		d += abs(a[i - 1] - a[i])
	e = 0
	for i in range(1, len(b)):
		if b[i - 1] < 0 and b[i] > 0 or (b[i] < 0 and b[i - 1] > 0):
			e = max(2 * min(abs(b[i - 1]), abs(b[i])), e)
	e = max(e, abs(b[0]), abs(b[-1]))
	print(d - e)


def readline():
	return map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	a = [0] + list(readline())
	ans = 0
	for i in range(2, n + 1):
		ans += abs(a[i] - a[i - 1])
	mx = max(abs(a[2] - a[1]), abs(a[n] - a[n - 1]))
	for i in range(2, n):
		mx = max(mx, abs(a[i] - a[i - 1]) + abs(a[i + 1] - a[i]) - abs(a[i + 1] - a[i - 1]))
	print(ans - mx)


for tt in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	s = 0
	for i in range(1, n):
		s += abs(l[i] - l[i - 1])
	m = max(abs(l[1] - l[0]), abs(l[n - 2] - l[n - 1]))
	for i in range(1, n - 1):
		m = max(abs(l[i] - l[i - 1]) + abs(l[i] - l[i + 1]) - abs(l[i - 1] - l[i + 1]), m)
	print(s - m)


from bisect import *
from collections import *
from math import gcd, ceil, sqrt, floor, inf
from heapq import *
from itertools import *
from operator import add, mul, sub, xor, truediv, floordiv
from functools import *
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')

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

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

def N():
	return int(input())
from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc
farr = [1]
ifa = []

def fact(x, mod=0):
	if mod:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr) % mod)
	else:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr))
	return farr[x]

def ifact(x, mod):
	global ifa
	ifa.append(pow(farr[-1], mod - 2, mod))
	for i in range(x, 0, -1):
		ifa.append(ifa[-1] * i % mod)
	ifa = ifa[::-1]

def per(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return fact(i) // fact(i - j)
	return farr[i] * ifa[i - j] % mod

def com(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return per(i, j) // fact(j)
	return per(i, j, mod) * ifa[j] % mod

def catalan(n):
	return com(2 * n, n) // (n + 1)

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def lowbit(n):
	return n & -n

def inverse(a, m):
	a %= m
	if a <= 1:
		return a
	return (1 - inverse(m, a) * m) // a % m

class BIT:

	def __init__(self, arr):
		self.arr = arr
		self.n = len(arr) - 1

	def update(self, x, v):
		while x <= self.n:
			self.arr[x] += v
			x += x & -x

	def query(self, x):
		ans = 0
		while x:
			ans += self.arr[x]
			x &= x - 1
		return ans

class DSU:

	def __init__(self, n):
		self.c = [-1] * n

	def same(self, x, y):
		return self.find(x) == self.find(y)

	def find(self, x):
		if self.c[x] < 0:
			return x
		self.c[x] = self.find(self.c[x])
		return self.c[x]

	def union(self, u, v):
		(u, v) = (self.find(u), self.find(v))
		if u == v:
			return False
		if self.c[u] < self.c[v]:
			(u, v) = (v, u)
		self.c[u] += self.c[v]
		self.c[v] = u
		return True

	def size(self, x):
		return -self.c[self.find(x)]

class UFS:

	def __init__(self, n):
		self.parent = [i for i in range(n)]
		self.ranks = [0] * n

	def find(self, x):
		if x != self.parent[x]:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, u, v):
		(pu, pv) = (self.find(u), self.find(v))
		if pu == pv:
			return False
		if self.ranks[pu] >= self.ranks[pv]:
			self.parent[pv] = pu
			if self.ranks[pv] == self.ranks[pu]:
				self.ranks[pu] += 1
		else:
			self.parent[pu] = pv

def Prime(n):
	c = 0
	prime = []
	flag = [0] * (n + 1)
	for i in range(2, n + 1):
		if not flag[i]:
			prime.append(i)
			c += 1
		for j in range(c):
			if i * prime[j] > n:
				break
			flag[i * prime[j]] = prime[j]
			if i % prime[j] == 0:
				break
	return flag

def dij(s, graph):
	d = {}
	d[s] = 0
	heap = [(0, s)]
	seen = set()
	while heap:
		(dis, u) = heappop(heap)
		if u in seen:
			continue
		for v in graph[u]:
			if v not in d or d[v] > d[u] + graph[u][v]:
				d[v] = d[u] + graph[u][v]
				heappush(heap, (d[v], v))
	return d

def GP(it):
	return [[ch, len(list(g))] for (ch, g) in groupby(it)]

class DLN:

	def __init__(self, val):
		self.val = val
		self.pre = None
		self.next = None

def nb(i, j):
	for (ni, nj) in [[i + 1, j], [i - 1, j], [i, j - 1], [i, j + 1]]:
		if 0 <= ni < n and 0 <= nj < m:
			yield (ni, nj)

@bootstrap
def gdfs(r, p):
	if len(g[r]) == 1 and p != -1:
		yield None
	for ch in g[r]:
		if ch != p:
			yield gdfs(ch, r)
	yield None
t = N()
for i in range(t):
	n = N()
	a = RLL()
	d = sum((abs(a[i] - a[i + 1]) for i in range(n - 1)))
	ans = d
	for i in range(n):
		if i == 0:
			tmp = d - abs(a[0] - a[1])
		elif i == n - 1:
			tmp = d - abs(a[i] - a[i - 1])
		else:
			tmp = d - abs(a[i] - a[i - 1]) - abs(a[i] - a[i + 1]) + abs(a[i + 1] - a[i - 1])
		ans = min(ans, tmp)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list()
	for i in range(len(a) - 1):
		b.append(abs(a[i] - a[i + 1]))
	sm = sum(b)
	ans = sm - max(b[0], b[-1])
	for i in range(len(b) - 1):
		ans = min(ans, sm - b[i] - b[i + 1] + abs(a[i] - a[i + 2]), sm - b[i + 1] + abs(abs(a[i] - a[i + 2])))
	print(ans)


T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	d = sum((abs(a - b) for (a, b) in zip(A, A[1:])))
	ans = d
	for (a, b, c) in zip(A, A[1:], A[2:]):
		p = abs(a - b) + abs(b - c)
		q = abs(a - c)
		ans = min(ans, d - (p - q))
	ans = min(ans, d - abs(A[0] - A[1]), d - abs(A[-1] - A[-2]))
	print(ans)


from collections import deque
import sys
from array import array
import typing as Tp

def input():
	return sys.stdin.buffer.readline().decode('utf-8')

def output(*args):
	sys.stdout.buffer.write(('\n'.join(map(str, args)) + '\n').encode('utf-8'))

def main():
	t = int(input())
	ans_a = [0] * t
	for ti in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		delta = list((abs(x - y) for (x, y) in zip(a, a[1:])))
		if n > 2:
			base = sum(delta)
			ans = min(base - delta[0], base - delta[-1])
			for i in range(1, n - 1):
				ans = min(ans, base - abs(a[i] - a[i + 1]) - abs(a[i] - a[i - 1]) + abs(a[i + 1] - a[i - 1]))
			ans_a[ti] = ans
	output('\n'.join(map(str, ans_a)))
main()


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	if n <= 2:
		print(0)
		continue
	k = [abs(a[0] - a[1])]
	for i in range(1, n - 1):
		if a[i - 1] > a[i] and a[i + 1] > a[i]:
			k.append(2 * (min(a[i - 1], a[i + 1]) - a[i]))
		elif a[i] > a[i + 1] and a[i] > a[i - 1]:
			k.append(2 * (a[i] - max(a[i - 1], a[i + 1])))
		else:
			k.append(0)
	k.append(abs(a[-1] - a[-2]))
	for i in range(1, n):
		b.append(abs(a[i] - a[i - 1]))
	s = sum(b)
	print(s - max(k))


t = int(input())
for i in range(t):
	n = int(input())
	s = 0
	l = list(map(int, input().split()))
	for i in range(n - 1):
		s += abs(l[i] - l[i + 1])
	sm = min(s, s - abs(l[1] - l[0]), s - abs(l[n - 1] - l[n - 2]))
	for i in range(1, n - 1):
		sm = min(sm, s - abs(l[i] - l[i - 1]) - abs(l[i + 1] - l[i]) + abs(l[i + 1] - l[i - 1]))
	print(sm)


for i in range(int(input())):
	n = int(input())
	d = list(map(int, input().split()))
	l = len(d)
	d_diff = [el for el in d]
	d_diff[0] = abs(d[1] - d[0])
	d_diff[-1] = abs(d[-1] - d[-2])
	for i in range(1, len(d) - 1):
		d_diff[i] = abs(d[i - 1] - d[i]) - abs(d[i + 1] - d[i - 1]) + abs(d[i] - d[i + 1])
	mx = max(d_diff)
	mx_index = d_diff.index(mx)
	if mx_index == 0:
		d[mx_index] = d[1]
	elif mx_index == len(d) - 1:
		d[-1] = d[-2]
	else:
		d[mx_index] = d[mx_index + 1]
	steps = 0
	for i in range(len(d) - 1):
		steps += abs(d[i] - d[i + 1])
	print(steps)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	s = []
	for i in range(1, n):
		s.append(abs(a[i] - a[i - 1]))
	sm = sum(s)
	ans = sm
	if n == 2:
		print(0)
		continue
	for i in range(n - 1):
		if i == 0:
			x = sm - s[i] - s[i + 1]
			ans = min(ans, x + min(abs(a[i] - a[i + 2]), abs(a[i + 1] - a[i + 2])))
		elif i == n - 2:
			x = sm - s[i] - s[i - 1]
			ans = min(ans, x + min(abs(a[i] - a[i - 1]), abs(a[i + 1] - a[i - 1])))
		else:
			x = sm - s[i] - s[i + 1] - s[i - 1]
			y = min(abs(a[i] - a[i - 1]) + abs(a[i] - a[i + 2]), abs(a[i + 1] - a[i - 1]) + abs(a[i + 1] - a[i + 2]))
			ans = min(ans, x + y)
	print(ans)


import sys
import math
from collections import Counter, defaultdict
LI = lambda : list(map(int, input().split()))
MAP = lambda : map(int, input().split())
IN = lambda : int(input())
S = lambda : input()

def case():
	n = IN()
	a = LI()
	ans = 0
	for i in range(1, n):
		ans += abs(a[i] - a[i - 1])
	x = ans
	ans = min(ans, x - abs(a[1] - a[0]))
	for i in range(1, n - 1):
		ans = min(ans, x - abs(a[i] - a[i - 1]) - abs(a[i] - a[i + 1]) + abs(a[i + 1] - a[i - 1]))
	print(min(ans, x - abs(a[-1] - a[-2])))
for _ in range(IN()):
	case()


A = abs
for s in [*open(0)][2::2]:
	l = [*map(int, s.split())]
	a = A(l[1] - l[0])
	m = max(A(l[0] - l[1]), A(l[-1] - l[-2]))
	for i in range(1, len(l) - 1):
		a += abs(l[i] - l[i + 1])
		m = max(m, A(l[i + 1] - l[i]) + A(l[i - 1] - l[i]) - A(l[i + 1] - l[i - 1]))
	print(a - m)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ans = -1
	d = [0] * (n + 1)
	for i in range(n - 2, -1, -1):
		d[i] += d[i + 1] + abs(l[i] - l[i + 1])
	ans = -1
	m = 0
	for i in range(n):
		x = m + d[i + 1]
		if i >= 1:
			if i + 1 < n:
				x += abs(l[i + 1] - l[i - 1])
		if ans == -1:
			ans = x
		else:
			ans = min(ans, x)
		if i >= 1:
			m += abs(l[i] - l[i - 1])
	print(ans)


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


def findMinRemoval(arr, n):
	value = 0
	maximum = 0
	if n == 1:
		return 0
	for i in range(n):
		if i != 0 and i != n - 1:
			value = value + abs(arr[i] - arr[i + 1])
			temp = abs(arr[i] - arr[i + 1]) + abs(arr[i] - arr[i - 1]) - abs(arr[i - 1] - arr[i + 1])
		elif i == 0:
			value = value + abs(arr[i] - arr[i + 1])
			temp = abs(arr[i] - arr[i + 1])
		else:
			temp = abs(arr[i] - arr[i - 1])
		maximum = max(maximum, temp)
	return value - maximum
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	print(findMinRemoval(arr, n))


t = int(input())
output = []
for i in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	tot = 0
	for j in range(1, len(a)):
		tot += abs(a[j] - a[j - 1])
	if n == 2:
		output.append(0)
		continue
	mx2 = 0
	mx2 = max(mx2, abs(a[0] - a[1]))
	mx2 = max(mx2, abs(a[n - 1] - a[n - 2]))
	mx = 0
	for j in range(1, n - 1):
		temp = abs(a[j] - a[j - 1]) + abs(a[j] - a[j + 1])
		temp -= abs(a[j - 1] - a[j + 1])
		mx = max(mx, temp)
	z = tot - mx
	z2 = tot - mx2
	output.append(min(z, z2))
print('\n'.join(list(map(str, output))))