//
//  [BAPC'06] RUMMIKUB
//  by: Jan Kuipers
//
import java.io.*;
import java.util.*;

       class Main {

  static final int MAXN=100;
  static final int MAXC=4;
  static final String colors = "rgby";

  public static void main (String[] args) throws Exception {

    int[][] s = new int[4][];
    s[0] = new int[3];
    s[1] = new int[3];
    s[2] = new int[4];
    s[3] = new int[7];
            
    for (int i=0; i<4; i++) {
      s[i][0] = (1<<16)+(1<<12)+(1<<8);
      s[i][1] = (1<<16)+(1<<12)+(1<<8)+(1<<4);
      s[i][2] = (1<<16)+(1<<12)+(1<<8)+(1<<4)+(1<<0);
    }

    s[2][3] = (1<<16)+(1<<15)+(1<<14);
    s[3][3] = (1<<16)+(1<<15)+(1<<14);
    s[3][4] = (1<<16)+(1<<15)+(1<<13);
    s[3][5] = (1<<16)+(1<<14)+(1<<13);
    s[3][6] = (1<<16)+(1<<15)+(1<<14)+(1<<13);

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    int runs = Integer.parseInt(in.readLine());
    
    while (runs-->0) {
      int C = Integer.parseInt(in.readLine());
      String[] cubs = in.readLine().split(" ");

      boolean[][] avai = new boolean[MAXN+1][MAXC];

      for (int i=0; i<C; i++) {
	int num = Integer.parseInt(cubs[i].substring(0,cubs[i].length()-1));
	int col = colors.indexOf(cubs[i].charAt(cubs[i].length()-1));
	avai[num][col]=true;
      }
      
      int[] best = new int[1<<16];
      int cards=0, res=0;

      for (int n=0; n<=MAXN; n++)
	for (int c=0; c<MAXC; c++, cards>>=1) {
	  int[] next = new int[1<<16];
	  
	  for (int i=0; i<1<<16; i++)
	    next[i>>1] = Math.max(next[i>>1], best[i]);

	  if (avai[n][c]) {
	    cards|=1<<16;
	    for (int i=0; i<s[c].length; i++) {
	      if ((cards & s[c][i]) != s[c][i]) continue;

	      int val=0,tmpc=c,tmpn=n;
	      for (int j=16; j>=0; j--) {
		if ((s[c][i] & (1<<j)) != 0) val+=tmpn;
		tmpc--;
		if (tmpc==-1) { tmpc=3; tmpn--; }
	      }
	    	      
	      for (int j=0; j<1<<16; j++) {
		if ((s[c][i] & j) != 0) continue;
		next[(j | s[c][i]) >> 1] = Math.max(next[(j | s[c][i]) >> 1], best[j]+val);
		res = Math.max(res, next[(j | s[c][i]) >> 1]);
	      }
	    }
	  }

	  best=next;
	}
      
      System.out.println(res);
    }
  }
}
