#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0; i<(int)(n); ++i)
#define rep_r(i,n) for(int i=(int)(n)-1; i>=0; --i)
#define reps(i,n,m) for(int i=(int)(n); i<(int)(m); ++i)
#define reps_r(i,n,m) for(int i=(int)(m)-1; i>=(int)(n); --i)
#define N 200001
int main(){
    //cout<<fixed<<setprecision(10);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,q,t,a,b,state=1;
    string s,s1,s2;
    char c;
    cin>>n>>s>>q;
    s1 = s.substr(0,n);
    s2 = s.substr(n,2*n);
    rep(i,q){
        cin>>t>>a>>b;
        if(t==1){
            if(b-1<n){
                if(state==1){
                    c=s1[a-1];
                    s1[a-1]=s1[b-1];
                    s1[b-1]=c;
                }else{
                    c=s2[a-1];
                    s2[a-1]=s2[b-1];
                    s2[b-1]=c;
                }
            }else if(n<=a-1){
                if(state==1){
                    c=s2[a-n-1];
                    s2[a-n-1]=s2[b-n-1];
                    s2[b-n-1]=c;
                }else{
                    c=s1[a-n-1];
                    s1[a-n-1]=s1[b-n-1];
                    s1[b-n-1]=c;
                }
            }else{
                if(state==1){
                    c=s1[a-1];
                    s1[a-1]=s2[b-n-1];
                    s2[b-n-1]=c;
                }else{
                    c=s2[a-1];
                    s2[a-1]=s1[b-n-1];
                    s1[b-n-1]=c;
                }
            }
        }else if(t==2){
            state *= -1;
        }
    }
    if(state==1) cout<<s1<<s2<<"\n";
    else cout<<s2<<s1<<"\n";
}
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i=0; i<(int)(n); i++)
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef pair<LL, int> PR;
const LL INF=1e16;
struct Graph{
  int N, M; VI A, B; VL C; vector<VI> S, T;
  Graph(int n) : N(n), M(0), S(n), T(n) {}
  void setEdge(int s, int t, LL u){ 
    A.push_back(s); B.push_back(t); C.push_back(u);
    S[s].push_back(M); T[t].push_back(M); M++;
  }
  VL Dijkstra(int s){
    VL D(N, INF); D[s]=0;
    priority_queue< PR, vector<PR>, greater<PR> > Q;
    REP(i, N){ Q.push(make_pair(D[i], i)); }
    while(Q.size()){
      PR p=Q.top(); int v=p.second; Q.pop();
      if(D[v]<p.first){ continue; }
      for(int e:S[v]){
        int u=B[e]; LL nw=D[v]+C[e];
        if(nw<D[u]){ D[u]=nw; Q.push(make_pair(D[u], u)); }
      }
    }
    return D;
  }
};
int main(){
  int N, M, a, b; LL c;
  cin >> N >> M; Graph G(N); VL J(N, INF);
  REP(i, M){ 
    cin >> a >> b >> c; a--; b--;
    if(a==b){ J[a]=min(J[a], c); }
    G.setEdge(a, b, c);
  }
  vector<VL> d;
  REP(i, N){ d.push_back(G.Dijkstra(i)); }
  REP(i, N){ 
    LL ans=J[i]; 
    REP(j, N){ 
      if(i==j){ continue; }
      ans=min(ans, d[i][j]+d[j][i]);
    }
    if(ans<INF){ cout << ans << "\n"; }
    else{ cout << -1 << "\n"; }
  }
}