// ColorfulTowers.java import java.util.Vector; import java.util.StringTokenizer; import java.io.*; public class ColorfulTowers { static BufferedReader in; public final Character red = new Character('R'), green = new Character('G'), blue = new Character('B'); public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); int numCases = Integer.parseInt(in.readLine()); for (int i = 0; i < numCases; i++){ Vector input = getInput(); ColorfulTowers towers = new ColorfulTowers(); System.out.println( towers.nbrMoves(input, new Character('R')) ); } } // returns the number of moves for distributing all disks by color where s // is the sequence of disks on peg ordered from bottom to top public long nbrMoves(Vector s, Character peg) { skim( s, peg ); if (s.isEmpty()) return 0; else { // a is the cost of moving the disks off peg, which is equal to the // cost of moving the top s.size()-1 disks to the remaining // peg (whose color is different from that of the bottom disk) plus // a cost of 1 for moving the bottom disk to the peg of its color. // b is the cost of distributing the disks from the remaining peg. Character bottomDisk = (Character)s.get(0); s.remove(0); long a = power(2, s.size()); long b = nbrMoves(s, remainingPeg(peg, bottomDisk)); return a + b; } } // drops the longest initial sequence of disks from s whose color matches peg // since such disks need not be moved and so can be ignored. public Vector skim(Vector s, Character peg) { if (s.isEmpty()) return s; else if (peg.equals((Character)s.get(0))) { s.remove(0); return skim(s, peg); } else return s; } public Character remainingPeg(Character p1, Character p2) { if ((!p1.equals(red)) && (!p2.equals(red))) return red; else if ((!p1.equals(green)) && (!p2.equals(green))) return green; else return blue; } public long power(int a, int b) { if (b == 0) return 1; else if ((b % 2) == 1) return a * power(a, b-1); else { long c = power(a, b/2); return c * c; } } static Vector getInput() throws IOException { Vector res = new Vector(); StringTokenizer toks = new StringTokenizer(in.readLine()); int numDisks = Integer.parseInt(toks.nextToken()); for (int i = 0; i < numDisks; i++){ res.add( new Character(toks.nextToken().charAt(0)) ); } return res; } }