#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;
}