//* Problem  : E: The Bavarian Beer Party
//* Contest  : BAPC - 2006
//* Type     : Solution
//* Date     : 2008.08.05
//* Author   : alt
//* Language : C++
//* Compiler : Microsoft Visual C++ 6.0

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

typedef pair <int,int> PIII;
typedef vector <int> VI;
typedef vector <vector<int> > VVI;
typedef vector <pair<int,int> > VPII;
typedef vector <vector<pair<int,int> > > VVPII;

#define int64 long long
#define PI (2.0 * acos(0.0))
#define MP make_pair
#define PB push_back
#define SZ(a) (int)a.size()
#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define FORSZ(i, a) FOR(i, SZ(a))
#define FORE(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define FORR(i, a, b) for (int i = (int)(a); i >= int(b); i--)
#define INF 1000000000
#define INFL 1000000000000000000LL
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define CLEARA(a) memset(a, 0, sizeof(a))

inline int si(){int tt; scanf("%d", &tt); return tt;}

int n, a[1024], rr[1024][1204], res;

int calc(int l, int r)
{
	if (r <= l) return 0;
	if (rr[l][r]) return rr[l][r] - 1;
	int res = 0;

	for (int ll = l + 1; ll <= r; ll+=2)
		res = max(res, (a[l] == a[ll]) + calc(l + 1, ll - 1) + calc(ll + 1, r));

	rr[l][r] = res + 1; return res;
}

void solve()
{
	memset(rr, 0, sizeof(rr));
	res = calc(0, n - 1);
}

void result()
{
	printf("%d\n", res);
}

int main()
{
#ifdef _DEBUG
	freopen("1064", "r", stdin);
#endif
	int nt = si();
	while (nt--)
	{
		n = si();
		FOR(i,n)
			a[i] = si();
		solve();
		result();
	}
	return 0;
}

