#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
  os << "[ ";
  for (auto v : V) os << v << " ";
  return os << "]";
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U>& l, const std::pair<T, U>& r) {
  return {l.first + r.first, l.second + r.second};
}
const long long int mod = 1e9 + 7;
const int maxn = 3005;
long long int a[maxn];
long long int dp[maxn][maxn];
vector<long long int> f[maxn];
long long int n;
long long int solve(long long int l, long long int r) {
  if (l == r) return 0;
  if (l == r - 1) return (a[l] != a[r]);
  long long int& res = dp[l][r];
  if (res != -1) return res;
  res = r - l;
  res = min(res, 1 + solve(l + 1, r));
  res = min(res, 1 + solve(l, r - 1));
  for (auto v : f[a[l]]) {
    if (v > l and v <= r) {
      res = min(res, solve(l, v - 1) + solve(v, r));
    }
  }
  return res;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (long long int i = 0; i <= n; i++) f[i].clear();
    for (long long int i = 0; i < n; i++) {
      cin >> a[i];
      a[i]--;
      f[a[i]].push_back(i);
      for (long long int j = i; j < n; j++) dp[i][j] = -1;
    }
    cout << solve(0, n - 1) << '\n';
  }
}
