/*
  [BAPC'06] THE BAVARIAN BEER PARTY
  by: Jan Kuipers
*/

using namespace std;

#include <iostream>
#include <vector>

int N;
vector<int> x;
vector<vector<int> > cache;

int go (int fr, int to) {

  int &res = cache[fr][to];
  if (res!=-1) return res;
  res=0;

  for (int i=fr+1; i<to; i+=2)
    res >?= go(fr+1,i) + go(i+1,to) + (x[fr]==x[i] ? 1 : 0);

  return res;
}

int main () {

  int runs;
  cin>>runs;
  while (runs--) {
    cin>>N;
    x=vector<int>(N);
    for (int i=0; i<N; i++) cin>>x[i];

    cache=vector<vector<int> >(N+1, vector<int>(N+1, -1));
    cout << go(0,N) << endl;
  }

  return 0;
}
