#include #include #include #include using namespace std; const int N = 1e5 + 5; int n, a[N]; int main() { int t; scanf("%d", &t); while (t--) { long long S = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", a + i); int ma1 = 0, ma2 = 0, ma3 = 0; for (int i = 1; i < n; i++) { if (abs(a[i] - a[i+1]) > ma1) { ma3 = ma2; ma2 = ma1; ma1 = abs(a[i] - a[i+1]); } else if (abs(a[i] - a[i+1]) > ma2) { ma3 = ma2; ma2 = abs(a[i] - a[i+1]); } else if (abs(a[i] - a[i+1]) > ma3) ma3 = abs(a[i] - a[i+1]); } for (int i = 1; i <= n; i++) { if (i == 1) { int tmp = abs(a[1] - a[2]); if (tmp == ma1) S += ma2; else S += ma1; } else if (i == n) { int tmp = abs(a[n] - a[n-1]); if (tmp == ma1) S += ma2; else S += ma1; } else { int t1 = abs(a[i] - a[i-1]); int t2 = abs(a[i] - a[i+1]); int t3 = abs(a[i+1] - a[i-1]); if (t3 >= ma1) S += t3; else { if (t1 < t2) swap(t1, t2); if (t1 != ma1) S += ma1; else if (t2 != ma2) S += ma2; else S += ma3; } } } printf("%I64d\n", S); } }