/* Problem 1--Mastermind
   There's no tricky algorithm here...just a generation of all 1296
   possible guesses and checking to see how many match the grading. */

import java.io.*;
import java.util.*;

public class prob1 {

 public static Scanner in=null;
 public static PrintWriter out=null;
 public static int cs=1;
 public static char[] pegs = {'R','O','Y','G','B','V'}; //6 colors

 public static void main (String[] args) throws Exception {

  in = new Scanner (new File ("prob1.in"));
  out = new PrintWriter ("prob1.out");
  int ct = 0;
  while ((ct=in.nextInt())>0) {
   char[][] guesses = new char[ct][4];
   GT[] grades = new GT[ct];
   for (int i=0; i < ct; i++) {
    guesses[i] = in.next().toCharArray(); //read in the guess
    grades[i] = new GT();
    grades[i].b = in.nextInt();//read in the grade
    grades[i].w = in.nextInt();
   }
   char[][] valid = new char[1296][4];
   int vct = 0;
   for (int a=0; a < 6; a++)
    for (int b=0; b < 6; b++)
     for (int c=0; c < 6; c++)
      for (int d=0; d < 6; d++) {
       char[] check = new char[4];
       check[0] = pegs[a]; check[1] = pegs[b]; check[2] = pegs[c];
       check[3] = pegs[d]; //generate all possible guesses
       int i=0;
       for (; i < ct; i++) {
        GT g = Grade (guesses[i].clone(),check.clone()); //grade it
        if (g.b!=grades[i].b || g.w != grades[i].w) { //
         i = -1;
         break;
        }
       }
       if (i != -1) valid[vct++] = check; //add to list if all match
      }
   out.print ("Case "+(cs++)+":  ");
   if (vct==1)
    out.println ("There is 1 solution:  "+new String(valid[0])+".");
   else out.println ("There are "+vct+" solutions.");
  }
  in.close();
  out.close();
 }

 /* Grade returns the black and white peg grading of a guess */
 public static GT Grade (char[] a, char[] b) {

  GT g = new GT ();
  for (int i=0; i < 4; i++) //count exact matches for black
   if (a[i]==b[i]) {        //replace with * so they don't
    g.b++;                  //get recounted
    a[i] = b[i] = '*';
   }
o:
  for (int i=0; i < 4; i++) {  //now check for white matches
   if (a[i]=='*') continue;
   for (int j=0; j < 4; j++) {
    if (b[j]=='*') continue;
    if (a[i]==b[j]) {
     a[i] = b[j] = '*';
     g.w++;
     continue o;
    }
   }
  }
  return g;
 }
}

/* GT keeps track of grade; a number of black and white pegs */
class GT {

 public int b = 0, w = 0;
}
