/* J. A. Little 18/5/01
   ok, this one is definately going to be the last.. hmm.. maybee not :)
*/

package jlittle_ex10;
import java.io.*;

public class App {

    private static BufferedReader in;
    private static BufferedOutputStream out;

    public static void main(String [] args) throws IOException {
	in = new BufferedReader(new InputStreamReader(System.in));
	out = new BufferedOutputStream(System.out);
	reading();
	in.close();
	out.close();
	
    }

    // encrypt: C = P(M) = M^P mod N
    // decrypt: M = S(C) = C^S mod N
    
    public static void reading(){
	    String method =null;
	    int k=-1;
	    int N=-1;
	    int M=-1;
	    try{
		method = in.readLine();
		k = Integer.parseInt(in.readLine());
		N = Integer.parseInt(in.readLine());
	    } catch (IOException e) {
		System.out.println("This went badly 1 "+e);
	    } catch (NumberFormatException e) {
		System.out.println("This went badly 2 "+e);
	    }
	    //  System.out.println(method);
	    // System.out.println(k);
	    // System.out.println(N);
	
	    try{
		// start reading input
		int c1 =01;
		while((c1=in.read())!=-1) {
		    int c2;
		    int cryptic;

		    c1=c1*256;

		    if((c2=in.read())!=-1){

			cryptic =c1+c2;
		    }
		    else
			cryptic =c1+128;
	    

		    int cypher = 1; 

		    for(int i = 1; i<=k; i++){
			cypher =(int)(((long)cypher * cryptic) % N);
		    }
	       
		    out.write((cypher/256));
		    
		    if(method.equals("encrypt")||(cypher%256 != 128)){
			out.write((cypher%256));
		    }


		}
	    } catch (IOException e) {
		System.out.println("This went badly 1 "+e);
	    }
    }
    

}
