// BAPC06 The Bavarian Beer Party, Marijn Kruisselbrink
import java.util.*;
       class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] a) {
		int n = sc.nextInt(); while (n-->0) new Main().solve();
	}
	int p = sc.nextInt();
	int[] brands = new int[p];
	int[][] cache = new int[p][p];
	void solve() {
		for (int i = 0; i < p; i++) {
			brands[i] = sc.nextInt();
			Arrays.fill(cache[i], -1);
		}
		System.out.println(answer(0, p-1));
	}

	int answer(int f, int t) {
		if (f >= t) return 0;
		if (cache[f][t] == -1) for (int i = f+1; i <= t; i+=2) {
			cache[f][t] = Math.max(cache[f][t], (brands[f] == brands[i] ? 1 : 0) + answer(f+1, i-1) + answer(i+1, t));
		}
		return cache[f][t];
	}
}
