#include <algorithm>
#include <iostream>
#include <cmath>// TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
#include <vector>
using  namespace std ;
int main() {

int n,t;
    cin>>n>>t;
    string s;
    cin>>s;
    int x,y,pos;
    while(t--) {
        string op;
        cin>>op;
        if (op=="pop_back") {
            s.pop_back();
        }
else if (op=="front"      )

cout<< s.front();
        else if (op=="back"      )
            cout<< s.back()<<"\n";
else if (op=="sort") {
    cin>>x>>y;
    if (x>y) swap(x,y);
    sort(s.begin()+x-1,s.begin()+y);
}
        else if (op=="reverse") {
            cin>>x>>y;
            if (x>y)
                swap(x,y);
            reverse(s.begin()+x-1,s.begin()+y);
        }

        else if (op=="print") {
            cin>>pos;
            cout<<s[pos-1] <<"\n";}
        else if (op=="substr")
            {cin>>x>>y;
            if (x>y) swap(x,y);
string s1=s.substr(x-1,(y-x)+1);
            cout<<s1<<"\n";
        }
    else if (op=="push_back") {
        char c;
        cin>>c;
        s.push_back(c);
    }







cout<< '\n' ;

    }










    return 0;
}

// TIP See CLion help at <a
// href="https://w...content-available-to-author-only...s.com/help/clion/">jetbrains.com/help/clion/</a>.
//  Also, you can try interactive lessons for CLion by selecting
//  'Help | Learn IDE Features' from the main menu.