#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

void func(){

	int N;
	scanf("%d",&N);

	int table[N];
	for(int i = 0; i < N; i++)scanf("%d",&table[i]);

	int dp[N][N];

	for(int i = 0; i < N; i++){
		for(int k = 0; k < N; k++){
			dp[i][k] = 0;
		}
	}

	for(int length = 1; length <= N-1; length++){
		for(int i = 0; i < N; i++){
			for(int k = 0; k < length; k++){
				dp[i][length] = max(dp[i][length],dp[i][k]+dp[(i+k+1)%N][length-k-1]+abs(table[i]-table[(i+k+1)%N]));
			}
		}
	}

	int ans = 0;
	for(int i = 0; i < N; i++){
		ans = max(ans,dp[i][N-1]);
	}

	printf("%d\n",ans);

}

int main(){
	int num_case;
	scanf("%d",&num_case);

	for(int i = 0; i < num_case; i++)func();

	return 0;
}