#include <bits/stdc++.h>

using namespace std;
mt19937 rnd(time(0));

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#define ios ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define int long long
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
//ll p = 131;
int mod = 1e9 + 7;
int inf = 3000000000000000000;

void solve() {
    int n;
    string s;
    cin >> n >> s;
    int f = 1;
    int q;
    cin >> q;
    while (q-- > 0) {
        int t, a, b;
        cin >> t >> a >> b;
        if (t == 1) {
            --a, --b;
            if (!f)
                a = (a + n) % (2 * n), b = (b + n) % (2 * n);
            swap(s[a], s[b]);
        } else {
            f = 1 - f;
        }
    }
    if (f) {
        cout << s;
    } else {
        cout << s.substr(n) << s.substr(0, n);
    }
}


signed main() {

//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    ios;
//    int t;
//    cin >> t;
//    while (t-- > 0)
    solve();
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define Mp make_pair
#define pb push_back

using ll = long long;
using db = double;
using pii = pair<int, int>;
using vi = vector<int>;
mt19937 mrand(time(0));
ll get(ll r) { return ((ll)mrand() * mrand() % r + r) % r; }
ll get(ll l, ll r) { return get(r - l + 1) + l; }

int n, x[20], y[20], z[20], f[1 << 17][17], ans = 1e9;
int dis(int i, int j) { return abs(x[i] - x[j]) + abs(y[i] - y[j]) + max(0, z[j] - z[i]); }
signed main() {
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
        scanf("%d %d %d", &x[i], &y[i], &z[i]);
    memset(f, 0x3f, sizeof(f)), f[1][0] = 0;
    for(int S = 2; S < 1 << n; S++)
        for(int i = 0; i < n; i++) if(S >> i & 1)
            for(int j = 0; j < n; j++) if((S >> j & 1) && j != i)
                f[S][i] = min(f[S][i], f[S ^ (1 << i)][j] + dis(j, i));
    for(int i = 0; i < n; i++) ans = min(ans, f[(1 << n) - 1][i] + dis(i, 0));
    printf("%d\n", ans);
    fprintf(stderr, "time=%.4f\n", (db)clock()/CLOCKS_PER_SEC);
    return 0;
}