#include <bits/stdc++.h>
// #include <atcoder/all>

using std::cin;
using std::cout;
using std::endl;
using std::sort;
using std::string;
using std::vector;
// using namespace atcoder;
typedef long long ll;

#ifdef _DEBUG
#define DEBUG(x) cout << #x << ": " << x << endl;
#else
#define DEBUG(x)
#endif

#define ALL(obj) (obj).begin(), (obj).end()
#define BIT(n) (1LL << (n))

#define REP(i, n) for (int i = 0; i < (n); ++i)

const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    // write code below
    int n, q;
    string s;
    cin >> n >> s >> q;

    bool flip = false;

    REP(i, q)
    {
        int t, a, b;
        cin >> t >> a >> b;
        a--;
        b--;
        if (t == 1)
        {
            if (flip)
            {
                if (a >= n)
                {
                    a %= n;
                }
                else
                {
                    a += n;
                }
                if (b >= n)
                {
                    b %= n;
                }
                else
                {
                    b += n;
                }
            }

            std::swap(s[a], s[b]);
        }
        else
        {
            flip = !flip;
        }
    }

    string l = s.substr(0, n), r = s.substr(n);

    if (flip)
    {
        cout << r + l << endl;
    }
    else
    {
        cout << l + r << endl;
    }

    // write code above
    return 0;
}

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i = a; i < b; i++)
#define rrep(i,a,b) for (int i = a - 1; i >= b; i--)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ll long long
#define dbwt(x) std::cout << fixed << setprecision(15) << x << '\n'
#define Wt(x) cout << x << '\n'
#define re0 return 0
#define Int(...) int __VA_ARGS__; in(__VA_ARGS__)
#define Ll(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define Str(...) string __VA_ARGS__; in(__VA_ARGS__)
#define Vec(t,a,s) vector<t>a(s); in(a)
#define Vec2(t,a,b,s) vector<t>a(s),b(s);rep(i,0,s)in(a[i],b[i])
#define Vec3(t,a,b,c,s) vector<t>a(s),b(s),c(s);rep(i,0,s)in(a[i],b[i],c[i])
#define Vec4(t,a,b,c,d,s) vector<t>a(s),b(s),c(s),d(s);rep(i,0,s) in(a[i],b[i],c[i],d[i])
#define mvec(t,a,i) vector<t>(a,i)
#define mvec2(t,a,b,i) vector<vector<t>>(a,vector<t>(b,i))
void scan(int& x) {std::cin >> x;}
void scan(ll& x) {std::cin >> x;}
void scan(double& x) {std::cin >> x;}
void scan(std::string& x) {std::cin >> x;}
void scan(std::vector<int>& x) {rep (i, 0, x.size()) std::cin >> x[i];}
void scan(std::vector<ll>& x) {rep (i, 0, x.size()) std::cin >> x[i];}
void in(){}
template<class Head, class... Tail>
void in(Head& head, Tail&... tail) {scan(head); in(tail...);}
template<class T, class U>
bool chmax(T& a, U b) {if(a < b){a=b; return true;} return false;}
template<class T, class U>
bool chmin(T& a, U b) {if(a > b){a=b; return true;} return false;}
using namespace std;
template<class T>
using vvec = vector<vector<T>>;

const int INF = 1001001001;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};

int main() {
    Int(H, W);
    vector<string> a(H);
    rep (i, 0, H) cin >> a[i];

    int sx, sy, gx, gy;
    set<int> st[26];
    rep (i, 0, H) rep (j, 0, W) {
        if (a[i][j] == 'S') sy = i, sx = j;
        if (a[i][j] == 'G') gy = i, gx = j;
        if ('a' <= a[i][j] && a[i][j] <= 'z') {
            st[a[i][j] - 'a'].insert(i * W + j);
        }
    }

    vector<bool> visited(26, false);
    vvec<int> dist(H, vector<int>(W, INF));
    dist[sy][sx] = 0;
    queue<int> Q;
    Q.push(sy * W + sx);
    while (!Q.empty()) {
        int v = Q.front(); Q.pop();
        int vx = v % W;
        int vy = v / W;
        if ('a' <= a[vy][vx] && a[vy][vx] <= 'z' && !visited[a[vy][vx] - 'a']) {
            visited[a[vy][vx] - 'a'] = true;
            st[a[vy][vx] - 'a'].erase(v);

            rep (i, 0, 4) {
                int x = vx + dx[i];
                int y = vy + dy[i];
                if (y < 0 || y >= H || x < 0 || x >= W || a[y][x] == '#') {
                    continue;
                }
                if (chmin(dist[y][x], dist[vy][vx] + 1)) {
                    Q.push(y * W + x);
                }
            }
            for (auto idx = st[a[vy][vx] - 'a'].begin(); idx != st[a[vy][vx] - 'a'].end(); ++idx) {
                int x = *idx % W;
                int y = *idx / W;
                if (chmin(dist[y][x], dist[vy][vx] + 1)) {
                    Q.push(y * W + x);
                }
            }
        }
        else {
            rep (i, 0, 4) {
                int x = vx + dx[i];
                int y = vy + dy[i];
                if (y < 0 || y >= H || x < 0 || x >= W || a[y][x] == '#') {
                    continue;
                }
                if (chmin(dist[y][x], dist[vy][vx] + 1)) {
                    Q.push(y * W + x);
                }
            }
        }
    }

    if (dist[gy][gx] == INF) Wt(-1);
    else Wt(dist[gy][gx]);
}