(*
 * Program:  cards_bram
 * Author:   Bram Kuijvenhoven (bram at kuijvenhoven.net)
 * Language: Pascal (FreePascal 2.x or higher recommended)
 * Notes:    Example/test solution to 'Rummikub' problem of BAPC 2006
 *)

program cards_bram;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils, Math;
  
procedure UpdateMax(var max:integer; const by:integer);
begin
  if by>max then max:=by;
end;

var
  NumTestCases, TestCase: integer;
  N:integer;
  i, j, k: integer;
  valueStr: string;
  c: char;
  value, col: integer;
  cards: array[1..4, 1..101] of boolean;
  // max: our DP array
  //   index 1: card value
  //   index 2: magic value indicating kind of vertical series at index 1
  //              0:    all four colors
  //              1..4: three colors; the value indicates which color is left open
  //   index 3: indicates the current horizontal series length, of the color left open (if any); note: 3 means >=3
  max: array[0..101, 0..4, 0..3] of integer;
  // DP iteration
  color, initialLen:integer; // counters
  maxLen:integer;
  len:array[1..4] of integer;
  tuple:integer;
  score:integer;
  scoreUpdate:array[1..4] of integer;

begin

  readln(NumTestCases);
  for TestCase:=1 to NumTestCases do
  begin
    // read input
    for i:=1 to 4 do for j:=1 to 100 do cards[i,j]:=false;
    readln(N);
    try
      for i:=1 to N do
      begin
        valueStr:='';
        repeat
          read(c);
          valueStr+=c;
        until c in ['r','g','b','y'];
        value:=StrToInt(Copy(valueStr,1,Length(valueStr)-1));
        case c of
          'r':col:=1;
          'g':col:=2;
          'b':col:=3;
          'y':col:=4;
        else
          raise Exception.CreateFmt('Card %d: unexpected color %s',[i,c]);
        end;
        if (value<1) or (value>100) then
          raise Exception.CreateFmt('Card %d: value out of range 1..100: %d',[i,value]);
        if cards[col, value] then
          raise Exception.CreateFmt('Card %d: duplicate card %d%s',[i,value,c]);
        cards[col, value]:=true;
        if i<N then read(c); // eat blank/newline
      end;
    except
      on E:Exception do
        writeln(Format('Error reading testcase %d: %s',[TestCase,E.Message]));
    end;
    readln;
    // solve
    for i:=1 to 4 do cards[i,101]:=true; // sentinel :)
    //   init max
    for i:=0 to 101 do
      for color:=0 to 4 do
        for initialLen:=0 to 3 do
          max[i,color,initialLen]:=Low(integer);
    max[0,0,0]:=0;
    //   sweep through values
    for i:=0 to 100 do
      for color:=0 to 4 do
      begin
        if color>0 then maxLen:=3 else maxLen:=0;
        for initialLen:=0 to maxLen do
          if max[i,color,initialLen]>=0 then
          begin
            score:=max[i,color,initialLen];
            //writeln(Format('max[%d,%d,%d] = %d',[i,color,initialLen,score]));
            // init len
            for j:=1 to 4 do
              if j=color then
                len[color]:=initialLen
              else
                len[j]:=0;
            // sweep through values
            for j:=i+1 to 101 do
            begin
              // update len
              for k:=1 to 4 do
                if cards[k,j] then
                  Inc(len[k])
                else
                  len[k]:=0;
              // count number of cards with this color
              tuple:=0;
              for k:=1 to 4 do
                if cards[k,j] then
                  Inc(tuple);
              // try vertical series here
              if tuple=4 then
                UpdateMax(max[j,0,0],score+4*j);
              for k:=1 to 4 do
              begin
                if len[k]>3 then
                  scoreUpdate[k]:=j
                else if len[k]=3 then
                  scoreUpdate[k]:=3*j-3
                else
                  scoreUpdate[k]:=0;
                if (tuple=4) or ((tuple=3) and not cards[k,j]) then
                  UpdateMax(max[j,k,Min(3,len[k])],score+3*j+scoreUpdate[k]);
              end;
              // update score
              for k:=1 to 4 do
                score+=scoreUpdate[k];
            end;
          end;
      end;
    // write solution
    writeln(max[101,0,0]-4*101);
  end;

end.

