// Hendrik Jan Hoogeboom 27.8'06
// The Bavarian Beer Party, recursief

#include <iostream>
#include <fstream>
#include <string>  

using namespace std;

const int MAXguests = 1000;
// globaal (als 't maar werkt ..)
int  
  brand[MAXguests],
  toast[MAXguests][MAXguests];

int prosit ( int van, int tm )
{ 
  int som, i, best;

//  if ( (tm-van%2) != 1 ) 
//  cout << "(" << van << "," << tm << ")" ;
  
  if ( van >= tm ) return 0;  

  best = prosit( van+1, tm-1  );
  if ( brand[van] == brand[tm] ) best++ ;
  for ( i=van+1; i<tm-1 ; i+=2 )
  { som = prosit( van, i) + prosit( i+1, tm);
    if (som > best) best = som;
  }
  return best;
}


int main ()
{ // standaard vars 
 int Ninst;
  // probleem vars
 int aantal;

//  cin >> Ninst;
  for (cin >> Ninst; Ninst; Ninst--)
  { // cout << "- instantie " << inst << endl;
     cin >> aantal;
     for (int i=0; i<aantal; i++)  cin >> brand[i];

     cout << prosit(0,aantal-1) << endl ;
   } // inst
} // main 
  
