Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Transformation

Write a program which performs a sequence of commands to a given string $str$. The command is one of:

print a b: print from the a-th character to the b-th character of $str$

reverse a b: reverse from the a-th character to the b-th character of $str$

replace a b p: replace from the a-th character to the b-th character of $str$ with p

Note that the indices of $str$ start with 0.

Input

In the first line, a string $str$ is given. $str$ consists of lowercase letters. In the second line, the number of commands q is given. In the next q lines, each command is given in the above mentioned format.

Output

For each print command, print a string in a line.

Constraints

$1 \leq $ length of $str \leq 1000$

$1 \leq q \leq 100$

$0 \leq a \leq b < $ length of $str$

for replace command, $b - a + 1 = $ length of $p$

Sample Input 1

abcde
3
replace 1 3 xyz
reverse 0 2
print 1 4

Sample Output 1

xaze

Sample Input 2

xyz
3
print 0 2
replace 0 2 abc
print 0 2

Sample Output 2

xyz
abc

Solution in C (judge verdict: Time Limit Exceeded)

#include <stdio.h>
#include <string.h>
#include <ctype.h>

typedef struct {
	char *op_name;
	int (*funcptr)(char *str_p,int a,int b,char *p_p);
}CNTRL_T;


int func_print(char *str_p,int a,int b,char *p_p)
{
	int i;

	for (i=a ; i<=b ; i++) {
		printf("%c",str_p[i]);
	}
	printf("\n");

	return 0;
}
int func_rev(char *str_p,int a,int b,char *p_p)
{
	int i;
	int idx=0;
	char wk[1000];

	for (i=a ; i<=b ; i++) {
		wk[idx] = str_p[i];
		idx++;
	}
	idx--;
	for (i=a ; i<=b ; i++) {
		str_p[i] = wk[idx];
		idx--;
	}

	return 0;
}
int func_rep(char *str_p,int a,int b,char *p_p)
{
	int i;

	i = b-a+1;
	memcpy(&(str_p[a]),&(p_p[0]),i);
	return 0;
}

CNTRL_T	ctrl[] = {
 {"print",func_print},
 {"reverse",func_rev},
 {"replace",func_rep},
 {NULL,NULL}
};

int main()
{
	int  q;
	char str[1000];
	char op[16];
	int	 a;
	int  b;
	int  i,k;
	char p[1000];
	char inp[64];


	gets(inp);
	sscanf(inp,"%s",str);
	gets(inp);
	sscanf(inp,"%d",&q);


	for (i=0 ; i<q ; i++) {
		gets(inp);
		sscanf(inp,"%s %d %d %s",&(op[0]),&a,&b,&(p[0]));


		for(k=0 ; ctrl[k].op_name != NULL ; k++) {
			if (strcmp(&(op[0]), ctrl[k].op_name)==0) {
				ctrl[k].funcptr(&(str[0]),a,b,&(p[0]));
			}
		}

	}
	return 0;
}