#include <iostream>
#include <vector>
#include<string>
#include <algorithm>
#include <cmath>
using namespace std;
#define all(v) (v.begin()),(v.end())
void execute_query(string& input, string query){
if (query == "pop_back"){
input.pop_back();
}else if (query == "front"){
cout << input.front() << endl;
}else if (query == "back"){
cout << input.back() << endl;
}else if (query == "sort"){
int x,y;
cin >> x >> y;
x = abs(x);
y = abs(y);
x--;
sort(input.begin() + x, input.begin() + y);
}else if (query == "reverse"){
int x,y;
cin >> x >> y;
x = abs(x);
y = abs(y);
x--;
reverse(input.begin() + x, input.begin() + y);
}else if (query == "print"){
int i;
cin >> i;
cout << input[i] << endl;
}else if (query == "substr"){
int x,y;
cin >> x >> y;
x = abs(x);
y = abs(y);
x--;
cout << input.substr(x, y) << endl;
}else if (query == "push_back"){
char i;
cin >> i;
input.push_back(i);
}
}
int main() {
int n, q;
string input;
cin >> n >> q >> input;
while (q--){
string query;
cin >> query;
execute_query(input, query);
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZTxzdHJpbmc+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxjbWF0aD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgYWxsKHYpICh2LmJlZ2luKCkpLCh2LmVuZCgpKQoKdm9pZCBleGVjdXRlX3F1ZXJ5KHN0cmluZyYgaW5wdXQsIHN0cmluZyBxdWVyeSl7CiAgICBpZiAocXVlcnkgPT0gInBvcF9iYWNrIil7CiAgICAgICAgaW5wdXQucG9wX2JhY2soKTsKICAgIH1lbHNlIGlmIChxdWVyeSA9PSAiZnJvbnQiKXsKICAgICAgICBjb3V0IDw8IGlucHV0LmZyb250KCkgPDwgZW5kbDsKICAgIH1lbHNlIGlmIChxdWVyeSA9PSAiYmFjayIpewogICAgICAgIGNvdXQgPDwgaW5wdXQuYmFjaygpIDw8IGVuZGw7CiAgICB9ZWxzZSBpZiAocXVlcnkgPT0gInNvcnQiKXsKICAgICAgICBpbnQgeCx5OwogICAgICAgIGNpbiA+PiB4ID4+IHk7CiAgICAgICAgeCA9IGFicyh4KTsKICAgICAgICB5ID0gYWJzKHkpOwogICAgICAgIHgtLTsKICAgICAgICBzb3J0KGlucHV0LmJlZ2luKCkgKyB4LCBpbnB1dC5iZWdpbigpICsgeSk7CiAgICB9ZWxzZSBpZiAocXVlcnkgPT0gInJldmVyc2UiKXsKICAgICAgICBpbnQgeCx5OwogICAgICAgIGNpbiA+PiB4ID4+IHk7CiAgICAgICAgeCA9IGFicyh4KTsKICAgICAgICB5ID0gYWJzKHkpOwogICAgICAgIHgtLTsKICAgICAgICByZXZlcnNlKGlucHV0LmJlZ2luKCkgKyB4LCBpbnB1dC5iZWdpbigpICsgeSk7CiAgICB9ZWxzZSBpZiAocXVlcnkgPT0gInByaW50Iil7CiAgICAgICAgaW50IGk7CiAgICAgICAgY2luID4+IGk7CiAgICAgICAgY291dCA8PCBpbnB1dFtpXSA8PCBlbmRsOwogICAgfWVsc2UgaWYgKHF1ZXJ5ID09ICJzdWJzdHIiKXsKICAgICAgICBpbnQgeCx5OwogICAgICAgIGNpbiA+PiB4ID4+IHk7CiAgICAgICAgeCA9IGFicyh4KTsKICAgICAgICB5ID0gYWJzKHkpOwogICAgICAgIHgtLTsKICAgICAgICBjb3V0IDw8IGlucHV0LnN1YnN0cih4LCB5KSA8PCBlbmRsOwogICAgfWVsc2UgaWYgKHF1ZXJ5ID09ICJwdXNoX2JhY2siKXsKICAgICAgICBjaGFyIGk7CiAgICAgICAgY2luID4+IGk7CiAgICAgICAgaW5wdXQucHVzaF9iYWNrKGkpOwogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIGludCBuLCBxOwogICAgc3RyaW5nIGlucHV0OwogICAgY2luID4+IG4gPj4gcSA+PiBpbnB1dDsKICAgIHdoaWxlIChxLS0pewogICAgICAgIHN0cmluZyBxdWVyeTsKICAgICAgICBjaW4gPj4gcXVlcnk7CiAgICAgICAgZXhlY3V0ZV9xdWVyeShpbnB1dCwgcXVlcnkpOwogICAgfQogICAgcmV0dXJuIDA7Cn0=