import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Scanner;
import javax.swing.JFileChooser;


public class codeJam1 {
	
	public static void main(String[] args) throws IOException {

		String[] parameters = new String[1000];
		int i = 0;
        String userInput; 
		
		//Get Input File
		File f = getFile();
		Scanner s = new Scanner(f);
		File file = new File("write.out");
		Writer output = new BufferedWriter(new FileWriter(file));
		String text = "";
		s.nextLine();
		
		//Solve Problems and Output Solutions
		while(s.hasNext()) {
			i++;
			userInput = s.nextLine();
			parameters = userInput.split(" ");
			text = convert(parameters[0], parameters[1], parameters[2]);
			output.write("Case #" + i + ": " + text + "\n");
		}
		
		output.close();
		
	}
	
	
	//Function to convert a number from source language to target language.
	private static String convert(String num,  String source, String target) {
		String result = "";
		char[] sourceNum = new char[source.length()], targetNum = new char[target.length()];
		int[] baseSourceNum = new int[num.length()], baseTargetNum = new int[1000];
		int i,j;
		int base10Num = 0;
		
		//Convert target and source to workable number bases
		for(i = 0; i < source.length(); i++) {
			sourceNum[i] = source.charAt(i);
		}
		for(i = 0; i < target.length(); i++) {
			targetNum[i] = target.charAt(i);
		}
		String str;
		//Convert num to baseSourceNum
		for(i = 0; i < num.length(); i++) {
			for(j = 0; j < sourceNum.length; j++) {
				if(num.charAt(i) == sourceNum[j]) 
					baseSourceNum[i] = j;
			}
			//Convert baseSourceNum to base10Num
			base10Num += (int) (baseSourceNum[i] * Math.pow(source.length(), num.length()-1-i));
			
		}
		
				
		
		
		//Convert base10Num to baseTargetNum
		j = 0;
		while(base10Num > 0) {
			baseTargetNum[j] = base10Num % target.length();
			base10Num /= target.length();			
			j++;
		}
		
		
		
		//Convert baseTargetNum to Result
		for(i = j-1; i >= 0; i--) {
			result += targetNum[baseTargetNum[i]];
		}

		return result;
	}
	
	//File Opening window
	public static File getFile(){
        // create a GUI window to pick the text to evaluate
        JFileChooser chooser = new JFileChooser(".");
        int retval = chooser.showOpenDialog(null);
        File f = null;
        chooser.grabFocus();
        if (retval == JFileChooser.APPROVE_OPTION)
           f = chooser.getSelectedFile();
        return f;
    }
}



