import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;

		auto x = new int [] [] (n + 1, n + 1);
		foreach (i; 0..n)
		{
			foreach (j; i + 1..n + 1)
			{
				bool [int] s;
				foreach (k; i..j)
				{
					s[a[k]] = true;
				}
				x[i][j] = 0;
				while (x[i][j] in s)
				{
					x[i][j] += 1;
				}
				x[i][j] += 1;
			}
		}

		auto f = new int [] [] (n + 1, n + 1);
		foreach (i; 0..n)
		{
			foreach (j; i + 1..n + 1)
			{
				f[i][j] = x[i][j];
				foreach (k; i..j)
				{
					f[i][j] = max (f[i][j],
					    f[i][k] + x[k][j]);
				}
			}
		}

		int res = 0;
		foreach (i; 0..n)
		{
			foreach (j; i + 1..n + 1)
			{
				res += f[i][j];
			}
		}
		writeln (res);
	}
}
