Problem:
Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <string>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <iostream>
#include <cstdio>
#include <deque>
 
using namespace std;
typedef long long ll;
typedef long double ld;
 
const ll MOD=ll(1e9+7);
const ll INF = ll(1e18) + 7;
 
//repetition
#define REP(i,n)  for(ll i=0;i<n;i++)
#define RREP(i,n)  for(ll i=n;i != 0;i--)
 
ll P_POW(ll i, ll n) {
  if(n==0) return 1;
 
  n--;
  ll rep = i;
 
  while(n) {
    rep = rep * i;
    n--;
  }
  return rep;
} 
 
unsigned GetDigit(unsigned num){
    unsigned digit=0;
    while(num!=0){
        num /= 10;
        digit++;
    }
    return digit;
}
 
unsigned binarySearch(ll target, ll max, ll min) {
  while((max - min) > 1) {
      if((max + min)/2 > target) {
        max = (max + min)/2;
        min = min;
      } else {
        max = max;
        min = (max + min)/2;
      }
    }
}
 
void f(int rest, string s) {
  char abc[] = "abc";
  if(rest == 0) {
    cout << s << endl;
  } else {
    for(int i=0; i < 3; i++) {
      f(rest - 1, s+abc[i]);
    }
  }
} 
 
 
 
// origin (ox, oy) destination (dx, dy)
//
/*
ll dfs2(ll ox, ll oy, vector<vector<char>> v, vector<vector<int>> dist) {
 
  int m = 0;
  for (int i = 0; i < 4; i++) {
    int nx = ox+dx[i], ny = oy + dy[i];
    if(nx < 0 || nx >= w || ny < 0 || nx >= h) continue;
    if(v[nx][ny] != '.') continue;
 
    if(dist[nx][ny] == -1) {
      dist[nx][ny] = dist[ox][oy] + 1;
 
      dist[nx][ny] = min(dist[ox][oy], dist[nx][ny]);
 
      cout << "v[nx][ny]: " << dist[nx][ny] << endl;
      cout << "dx: " << dx << endl;
      cout << "dy: " << dy << endl;
      cout << "v[ox][oy]: " << v[ox][oy] << endl;
      cout << "(nx, ny)" << "("<< nx << "," << ny << ")"<< endl;
      dfs2(nx, ny, v, dist);
    }
    m = max(m, dist[nx][ny]);
  }
  return m;
}
 
int main() {
  // vector<vector<ll>> a(n ,vector<ll>{2,0});
  // vector
  // vector<ll> s(n,0);
  // vector<vector<ll>> v;
  // map<ll,ll> m;
  // priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
  // vector<pair<ll, ll>> epv(n, {0, 0});
  // vector<vector<char>> v(h, vector<char>(w));
  
  for(auto&& m: em) {
    epv.push_back({ m.second, m.first });
  }
 
  ll n, k, a;
  cin >> n >> k;
 
  vector<ll> vp;
  vector<ll> vn;
 
  ll c = 0;
 
  REP(i, n) {
    cin >> a;
    if(a >= 0 ) vp.push_back(a);
    else vn.push_back(a);
  }
 
  sort(vp.begin(), vp.end());
  sort(vn.begin(), vn.end());
 
  ll ps = vp.size();
  ll ns = vn.size();
 
  cout << "k: " << k << endl;
  cout << "ps: " << ps << endl;
  cout << "ns: " << ns << endl;
 
  if(k < ps * ns) {
    ll m = k%ns;
    ll d = k - (m+1);
    cout << vn[m] * vp[d] << endl;
 
  }else {
    ll kd = k - ps*ns;
    ll md = kd%ps;
    ll dd = kd - md;
 
 
    ll s = 0;
    REP(i, ps) {
      s = s+(ps-i-1);
      cout << "s: " << s << endl;
      cout << "kd: " << kd << endl;
      cout << "s: " << s << endl;
      cout << "kd: " << kd << endl;
      cout << "i: " << i << endl;
      if(s > kd) {
        cout << "vp[i]: " << vp[i] << endl;
        cout << "vp[ps-i]: " << vp[s-kd+i-1] << endl;
        cout << vp[i] * vp[s-kd+i-1] << endl;
        break;
      }
    }
  }
 
}
 
 
ll h, w;
vector<vector<char>> v;
 
vector<ll> dx = {-1, 0, 1, 0};
vector<ll> dy = {0, -1, 0, 1};
 
ll bfs(ll ox, ll oy) {
  vector<vector<ll>> vd(h, vector<ll>(w, -1));
  queue<pair<ll, ll>> q;
  q.push({ox, oy});
  vd[oy][ox] = 0;
 
  ll res = 0;
  while(!q.empty()) {
    pair<ll, ll> vn = q.front();
    q.pop();
    res = max(res, vd[vn.second][vn.first]);
 
    for(ll i = 0; i < 4; i++){
      ll nx = vn.first +dx[i];
      ll ny = vn.second + dy[i];
 
      if(nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
      if(v[ny][nx] == '#') continue;
      if(vd[ny][nx] == -1) {
        vd[ny][nx] = res + 1;
        q.push({nx, ny});
      }
    }
  }
  return res;
}
*/
 
int main() {
  cin.tie(0);
  std::ios_base::sync_with_stdio(false);
 
  deque<char> ac;
  deque<char> ad;
  deque<char> ae;

  char a;
  ll q;
  cin >> a >> q;
 
  ac.push_back(a);
  ad.push_back(a);
 
  ll t;
  ll f;
 
  char c;
 
  REP(i, q) {
    cin >> t;
 
    if(t == 2) {
      cin >> f >> c;
      if(f == 1) {
        ad.push_back(c);
        ac.push_front(c);
      } else {
        ad.push_front(c);
        ac.push_back(c);
      }
    } else {
      ae = ad; // 転置をcopy
      ad = ac; // 転置後を代入
      ac = ae; // 転置を代入
    }
  }


  for(auto&& d: ac) {
    cout << d;
  }
}