#include <bits/stdc++.h>
using namespace std;
template <class T>
void Rd(T &x) {
  x = 0;
  char c;
  while (c = getchar(), c < 48)
    ;
  do x = (x << 1) + (x << 3) + (c ^ 48);
  while (c = getchar(), c > 47);
}
vector<int> G[3005];
int dp[3005][3005], A[3005];
int main() {
  int _;
  for (Rd(_); _; _--) {
    int n;
    Rd(n);
    for (int i = 1; i <= n; ++i) G[i].clear();
    for (int i = 1; i <= n; ++i) {
      Rd(A[i]);
      G[A[i]].push_back(i);
      dp[i + 1][i] = dp[i][i] = 0;
    }
    for (int len = 2; len <= n; ++len) {
      for (int i = 1; i <= n; ++i) {
        int j = i + len - 1;
        if (j > n) break;
        dp[i][j] = dp[i + 1][j];
        int x = A[i];
        for (auto k : G[x]) {
          if (k > i && k <= j)
            dp[i][j] = max(dp[i][j], 1 + dp[i + 1][k - 1] + dp[k][j]);
        }
      }
    }
    printf("%d\n", n - 1 - dp[1][n]);
  }
  return 0;
}
