#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x1fffffffffffffff;
const double INFD = 1e20;
const double EPS = 1e-9;
const double PI = atan(1) * 4;
const long long M = 1000000007;
long long scan() {
  long long r;
  scanf("%lld", &r);
  return r;
}
void scanstr(char *buf) { scanf("%s", buf); }
long long solve(vector<long long> &A) {
  long long n = A.size();
  vector<vector<long long> > f(n, vector<long long>(n));
  vector<long long> prev(n, -1);
  map<long long, long long> v2lastocc;
  for (long long i = 0; i < n; ++i) {
    prev[i] = v2lastocc[A[i]] - 1;
    v2lastocc[A[i]] = i + 1;
  }
  for (long long w = 0; w < n; ++w) {
    for (long long i = 0; i < n - w; ++i) {
      long long j = i + w;
      f[i][j] = (i == j) ? 0 : f[i][j - 1];
      for (long long k = prev[j]; k >= i; k = prev[k]) {
        f[i][j] = max(f[i][j], f[i][k] + f[k + 1][j - 1] + 1);
      }
    }
  }
  return n - 1 - f[0][n - 1];
}
int main() {
  long long tn = scan();
  for (long long ti = 0; ti < tn; ++ti) {
    long long n = scan();
    vector<long long> A(n);
    for (long long i = 0; i < n; ++i) A[i] = scan();
    long long ans = solve(A);
    cout << ans << endl;
  }
  return 0;
}
