#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i,j,n) for(int i=j;i<n;i++)
#define readvec(v,n) for(int i=0;i<n;i++){cin>>v[i];}
#define MOD 1000000007
int main()
{
  int n;
  cin>>n;
  vector<char> v(2*n);
  readvec(v,2*n);
  int q;
  cin>>q;
  int ind=0;
  rep(i,0,q)
  {
    int t,a,b;
    cin>>t>>a>>b;
    if(t==2){ind=(ind==0)?n:0;continue;}
    int ia=(ind+a-1)%(2*n);
    int ib=(ind+b-1)%(2*n);
    char ch = v[ia];
    v[ia] = v[ib];
    v[ib] = ch;
  }
  rep(i,0,2*n)cout<<v[(ind+i)%(2*n)];
  cout<<"\n";
  return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define int long long

int N, ans;
struct Node{
    int v, idx;
    inline bool operator < (const Node &comp) const {return comp.v < v;}
}a[400000 + 5];
set <int> b;
set <int> :: iterator it;

signed main() {
    ios :: sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    
    cin >> N;
    for (int i = 1; i <= N; i ++) {
        cin >> a[i].v; a[i].idx = N + 1 - i; b.insert(i);
    }
    for (int i = 1; i <= N; i ++) {
        cin >> a[N + i].v; a[N + i].idx = i;
    }
    sort(a + 1, a + N + N + 1);
    for (int i = 1, j = 1; i <= N; i ++) {
        while (j <= N + N) {
            it = b.upper_bound(a[j].idx);
            if (it != b.begin()) break;
            j ++;
        }
        ans += a[j ++].v;
        b.erase(-- it);
    }
    cout << ans << '\n';
    
    return 0;
}