#include <bits/stdc++.h>
using namespace std;
const int N = 3e3 + 5;
const int M = 25;
int a[N], pos[N], ne[N];
int dp[N][N];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, m = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
    }
    for (int i = 1; i <= n;) {
      int j = i;
      while (j <= n && a[i] == a[j]) {
        j++;
      }
      a[++m] = a[i];
      i = j;
    }
    for (int i = 1; i <= n; i++) {
      ne[i] = 0;
      pos[i] = m + 1;
    }
    for (int i = m; i >= 1; i--) {
      ne[i] = pos[a[i]];
      pos[a[i]] = i;
    }
    for (int i = 0; i <= m + 1; i++) {
      for (int j = 0; j <= m + 1; j++) {
        dp[i][j] = 0;
      }
    }
    for (int i = m; i >= 1; i--) {
      dp[i][i] = 0;
      for (int j = i + 1; j <= m; j++) {
        dp[i][j] = min(dp[i + 1][j], dp[i][j - 1]) + 1;
        for (int k = ne[i]; k <= j; k = ne[k]) {
          dp[i][j] = min(dp[i][j], dp[i + 1][k - 1] + dp[k][j] + 1);
        }
      }
    }
    printf("%d\n", dp[1][m]);
  }
  return 0;
}
