description: You are given a 1 by n pixel image. The i-th pixel of the image has color a_i. For each color, the number of pixels of that color is at most 20.
  
  You can perform the following operation, which works like the bucket tool in paint programs, on this image: 
  
    * pick a color — an integer from 1 to n; 
    * choose a pixel in the image; 
    * for all pixels connected to the selected pixel, change their colors to the selected color (two pixels of the same color are considered connected if all the pixels between them have the same color as those two pixels). 
  
  
  
  Compute the minimum number of operations needed to make all the pixels in the image have the same color.
  
  Input
  
  Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^3).
  
  The first line of each test case contains a single integer n (1 ≤ n ≤ 3⋅10^3) — the number of pixels in the image.
  
  The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n) — the colors of the pixels in the image.
  
  Note: for each color, the number of pixels of that color is at most 20.
  
  It is guaranteed that the sum of n over all test cases does not exceed 3⋅10^3.
  
  Output
  
  For each test case, print one integer: the minimum number of operations needed to make all the pixels in the image have the same color.
  
  Example
  
  Input
  
  
  3
  5
  1 2 3 2 1
  4
  1 1 2 2
  5
  1 2 1 4 2
  
  
  Output
  
  
  2
  1
  3
  
  Note
  
  In the first example, the optimal solution is to apply the operation on the third pixel changing its color to 2 and then to apply the operation on any pixel that has color 2 changing its color and the color of all pixels connected to it to 1. The sequence of operations is then: [1, 2, 3, 2, 1] → [1, 2, 2, 2, 1] → [1, 1, 1, 1, 1].
  
  In the second example, we can either change the 1s to 2s in one operation or change the 2s to 1s also in one operation.
  
  In the third example, one possible way to make all the pixels have the same color is to apply the operation on the first, third and the fourth pixel each time changing its color to 2.
solution: import java.util.*;
  import java.io.*;
  
  public class Div737 {
  
      ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////                                                                                                               /////////
  ////////                                                                                                               /////////
  ////////   HHHH        HHHH  EEEEEEEEEEEEE   MMMM          MMMM         OOOOOO             SSSSSSS      EEEEEEEEEEEEE  /////////
  ////////   HHHH        HHHH  EEEEEEEEEEEEE   MMMMMM      MMMMMM      OOO      OOO        SSSS   SSS     EEEEEEEEEEEEE  /////////
  ////////   HHHH        HHHH  EEEEE           MMMM MMM  MMM MMMM    OOO          OOO    SSSS       SSS   EEEEE          /////////
  ////////   HHHH        HHHH  EEEEE           MMMM  MMMMMM  MMMM   OOO            OOO   SSSS             EEEEE          /////////
  ////////   HHHH        HHHH  EEEEE           MMMM          MMMM  OOO              OOO   SSSSSSS         EEEEE          /////////
  ////////   HHHHHHHHHHHHHHHH  EEEEEEEEEEE     MMMM          MMMM  OOO              OOO      SSSSSS       EEEEEEEEEEE    /////////
  ////////   HHHHHHHHHHHHHHHH  EEEEEEEEEEE     MMMM          MMMM  OOO              OOO         SSSSSSS   EEEEEEEEEEE    /////////
  ////////   HHHH        HHHH  EEEEE           MMMM          MMMM   OOO            OOO              SSSS  EEEEE          /////////
  ////////   HHHH        HHHH  EEEEE           MMMM          MMMM    OOO          OOO     SSS       SSSS  EEEEE          /////////
  ////////   HHHH        HHHH  EEEEEEEEEEEEE   MMMM          MMMM      OOO      OOO        SSS    SSSS    EEEEEEEEEEEEE  /////////
  ////////   HHHH        HHHH  EEEEEEEEEEEEE   MMMM          MMMM         OOOOOO             SSSSSSS      EEEEEEEEEEEEE  /////////
  ////////                                                                                                               /////////
  ////////                                                                                                               /////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      static ArrayList<Integer>[] adj;
  
      public static void main(String[] args) throws IOException {
          Scanner sc = new Scanner(System.in);
          PrintWriter pw = new PrintWriter(System.out);
          int t = sc.nextInt();
          while (t-- > 0) {
              int n = sc.nextInt();
              int[] a = sc.nextIntArr(n);
              int[] nxt = new int[n];
              int[] last = new int[n + 1];
              Arrays.fill(last, n);
  
              for (int i = n - 1; i >= 0; i--) {
                  nxt[i] = last[a[i]];
                  last[a[i]] = i;
              }
  
              int[][] dp = new int[n + 1][n + 1];
              boolean[][] alleq = new boolean[n + 1][n + 1];
              for (int i = 0; i < n; i++) {
                  boolean eq = true;
                  for (int j = i; j < n; j++) {
                      eq &= (a[i] == a[j]);
                      dp[i][j] = eq ? 0 : 2 * n;
                      alleq[i][j] = eq;
                  }
              }
              for (int i = n - 2; i >= 0; i--) {
                  for (int j = i + 1; j < n; j++) {
                      int other = nxt[i];
                      while (other <= j) {
                          dp[i][j] = Math.min(dp[i][j], (alleq[i][other] ? 0 : 1) + (i + 1 <= other - 1 ? dp[i + 1][other - 1] : 0) + dp[other][j]);
                          other = nxt[other];
                      }
                      dp[i][j] = Math.min(dp[i][j], 1 + dp[i + 1][j]);
                      dp[i][j] = Math.min(dp[i][j], 1 + dp[i][j - 1]);
                  }
              }
              pw.println(dp[0][n - 1]);
          }
  
          pw.flush();
      }
  
      static class UnionFind {
          int[] p, rank, setSize;
          int numSets;
  
          public UnionFind(int N) {
              p = new int[numSets = N];
              rank = new int[N];
              setSize = new int[N];
              for (int i = 0; i < N; i++) {
                  p[i] = i;
                  setSize[i] = 1;
              }
          }
  
          public int findSet(int i) {
              return p[i] == i ? i : (p[i] = findSet(p[i]));
          }
  
          public boolean isSameSet(int i, int j) {
              return findSet(i) == findSet(j);
          }
  
          public void unionSet(int i, int j) {
              if (isSameSet(i, j))
                  return;
              numSets--;
              int x = findSet(i), y = findSet(j);
              if (rank[x] > rank[y]) {
                  p[y] = x;
                  setSize[x] += setSize[y];
              } else {
                  p[x] = y;
                  setSize[y] += setSize[x];
                  if (rank[x] == rank[y]) rank[y]++;
              }
          }
  
          public int numDisjointSets() {
              return numSets;
          }
  
          public int sizeOfSet(int i) {
              return setSize[findSet(i)];
          }
      }
  
  
      static int gcd(int a, int b) {
          if (a == 0)
              return b;
          return gcd(b % a, a);
      }
  
  
      static class Scanner {
          StringTokenizer st;
          BufferedReader br;
  
          public Scanner(FileReader r) {
              br = new BufferedReader(r);
          }
  
          public Scanner(InputStream s) {
              br = new BufferedReader(new InputStreamReader(s));
          }
  
          public String next() throws IOException {
              while (st == null || !st.hasMoreTokens())
                  st = new StringTokenizer(br.readLine());
              return st.nextToken();
          }
  
          public int[] nextIntArr(int n) throws IOException {
              int[] a = new int[n];
              for (int i = 0; i < n; i++) {
                  a[i] = nextInt();
              }
              return a;
          }
  
          public int nextInt() throws IOException {
              return Integer.parseInt(next());
          }
  
          public long nextLong() throws IOException {
              return Long.parseLong(next());
          }
  
          public String nextLine() throws IOException {
              return br.readLine();
          }
  
          public double nextDouble() throws IOException {
              String x = next();
              StringBuilder sb = new StringBuilder("0");
              double res = 0, f = 1;
              boolean dec = false, neg = false;
              int start = 0;
              if (x.charAt(0) == '-') {
                  neg = true;
                  start++;
              }
              for (int i = start; i < x.length(); i++)
                  if (x.charAt(i) == '.') {
                      res = Long.parseLong(sb.toString());
                      sb = new StringBuilder("0");
                      dec = true;
                  } else {
                      sb.append(x.charAt(i));
                      if (dec)
                          f *= 10;
                  }
              res += Long.parseLong(sb.toString()) / f;
              return res * (neg ? -1 : 1);
          }
  
          public boolean ready() throws IOException {
              return br.ready();
          }
      }
  
  }