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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using static System.Console;
using static Program.Input;
using static Program.MyMath;
using static Program.Const;

namespace Program
{
    static class Const
    {
        public static string alp = "abcdefghijklmnopqrstuvwxyz";
        public static string Alp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        public const long MOD = 1000000007;
        public static int INF = int.MaxValue;
        public static long INFL = long.MaxValue;
    }
    public class Program
    {
        public static void Main()
        {
            var s = ReadLine();
            var q = ScanInt();
            var sgn = 0;
            var add = new List<char>[2]{
                new List<char>(),
                new List<char>()
            };
            for (int i = 0; i < q; i++)
            {
                var input = ScanStrArray();
                if (input[0] == "1") sgn ^= 1;
                else
                {
                    if (input[1] == "1")
                        add[sgn].Add(input[2][0]);
                    else
                        add[sgn ^ 1].Add(input[2][0]);
                }
            }

            if (sgn == 0)
            {
                var head = add[0].ToArray().Reverse().ToArray();
                WriteLine($"{new string(head)}{s}{new string(add[1].ToArray())}");
            }
            else
            {
                var head = (add[1]).ToArray().Reverse().ToArray();
                WriteLine($"{new string(head)}{new string(s.Reverse().ToArray())}{new string(add[0].ToArray())}");
            }
        }
    }
    static class Input
    {
        public static void InputLine(out int a, out int b) { var input = ScanIntArray(); a = input[0]; b = input[1]; }
        public static void InputLine(out int a, out int b, out int c) { var input = ScanIntArray(); a = input[0]; b = input[1]; c = input[2]; }
        public static void InputLine(out int a, out int b, out int c, out int d) { var input = ScanIntArray(); a = input[0]; b = input[1]; c = input[2]; d = input[3]; }

        public static void InputLine(out long a, out long b) { var input = ScanLongArray(); a = input[0]; b = input[1]; }
        public static void InputLine(out long a, out long b, out long c) { var input = ScanLongArray(); a = input[0]; b = input[1]; c = input[2]; }
        public static void InputLine(out long a, out long b, out long c, out long d) { var input = ScanLongArray(); a = input[0]; b = input[1]; c = input[2]; d = input[3]; }

        public static void InputLine(out string a, out string b) { var input = ScanStrArray(); a = input[0]; b = input[1]; }

        public static int ScanInt() { return int.Parse(ReadLine()); }
        static public uint ScanUint() { return uint.Parse(ReadLine()); }
        static public double ScanDouble() { return double.Parse(ReadLine()); }
        static public long ScanLong() { return long.Parse(ReadLine()); }
        static public ulong ScanUlong() { return ulong.Parse(ReadLine()); }
        static public int[] ScanIntArray() { return ReadLine().Split().Select(x => int.Parse(x)).ToArray(); }
        static public uint[] ScanUintArray() { return ReadLine().Split().Select(x => uint.Parse(x)).ToArray(); }
        static public double[] ScanDoubleArray() { return ReadLine().Split().Select(x => double.Parse(x)).ToArray(); }
        static public long[] ScanLongArray() { return ReadLine().Split().Select(x => long.Parse(x)).ToArray(); }
        static public ulong[] ScanUlongArray() { return ReadLine().Split().Select(x => ulong.Parse(x)).ToArray(); }

        static public string ScanStr() { return ReadLine(); }
        static public string[] ScanStrArray() { return ScanStr().Split(); }
        static public string RegaxReplace(this string arg1, string pattern, string replacement) { return Regex.Replace(arg1, pattern, replacement); }
    }
    static class MyMath
    {
        static public int CountBit(this long a) { if (a == 0) return 0; int c = 1; for (; a > 1; a /= 2) if (a % 2 == 1) c++; return c; }
        static public int Floor2(this int a) { int N = 1; while (N < a) N *= 2; return N / 2; }
        static public long Floor2(this long a) { long N = 1; while (N < a) N *= 2; return N / 2; }
        static public int Ceiling2(this int a) { int N = 1; while (N < a) N *= 2; return N; }
        static public long Ceiling2(this long a) { long N = 1; while (N < a) N *= 2; return N; }
        static public long[] CumSum(this long[] a) { var res = new long[a.Length]; long s = 0; for (int i = 0; i < a.Length; i++) res[i] = (s += a[i]); return res; }
        static public IEnumerable<int> PrimeFactors(this int n)
        {
            int tmp = n;
            for (int i = 2; i * i <= n; i++) while (tmp % i == 0) { tmp /= i; yield return i; }
            if (tmp != 1) yield return tmp;
        }
        static public IEnumerable<long> PrimeFactors(this long n)
        {
            var tmp = n;
            for (long i = 2; i * i <= n; i++) while (tmp % i == 0) { tmp /= i; yield return i; }
            if (tmp != 1) yield return tmp;
        }
        static public int Digit(this int n, int d) { if (d < 0) return 0; return (n % (int)Math.Pow(10, d + 1)) / (int)Math.Pow(10, d); }
        static public int Length(this int n) { if (n < 0) return 0; return (int)Math.Log10(n) + 1; }
        static public int Top(this int n) { return n.Digit(n.Length() - 1); }
        public static bool isPrime(this int p)
        {
            if (p <= 1) return false;
            for (int i = 2; i * i <= p; i++) if (p % i == 0) return false;
            return true;
        }
        public static bool isPrime(this long p)
        {
            if (p <= 1) return false;
            for (long i = 2; i * i <= p; i++) if (p % i == 0) return false;
            return true;
        }
        public static int GCD(int a, int b)
        {
            Func<int, int, int> gcd = null;
            gcd = (x, y) => y == 0 ? x : gcd(y, x % y);
            return a > b ? gcd(a, b) : gcd(b, a);
        }
        public static long GCD(long a, long b)
        {
            Func<long, long, long> gcd = null;
            gcd = (x, y) => y == 0 ? x : gcd(y, x % y);
            return a > b ? gcd(a, b) : gcd(b, a);
        }
        public static ulong extGCD(ulong a, ulong b, ref ulong x, ref ulong y)
        {
            if (b == 0) { x = 1; y = 0; return a; }
            var d = extGCD(b, a % b, ref y, ref x);
            y -= a / b * x;
            return d;
        }
        public static int LCM(int a, int b) { return a * b / GCD(a, b); }
        public static long LCM(long a, long b) { return a * b / GCD(a, b); }
        public static long Inv(this long a, long m = MOD)
        {
            long b = m, u = 1, v = 0;
            while (b != 0)
            {
                long t = a / b;
                a -= t * b;
                swap(ref a, ref b);
                u -= t * v;
                swap(ref u, ref v);
            }
            u %= m;
            if (u < 0) u += m;
            return u;
        }
        private static void swap(ref long a, ref long b) { var t = a; a = b; b = t; }
        public static long ModPow(long a, long n, long m = MOD)
        {
            long res = 1;
            while (n > 0)
            {
                if (n % 2 == 1) res = (res * a) % m;
                a = (a * a) % m;
                n >>= 1;
            }
            return res;
        }
    }

    static class MyLinq
    {
        public static void Sort(this string[] array) { Array.Sort(array, StringComparer.OrdinalIgnoreCase); }
        public static void StringSort(this List<string> list) { list.Sort(StringComparer.OrdinalIgnoreCase); }
        public static string Join<T>(this IEnumerable<T> source, string separator) { return string.Join(separator, source); }
        public static IEnumerable<TItem> Distinct<TItem, TKey>(this IEnumerable<TItem> source, Func<TItem, TKey> keySelector)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));

            if (keySelector == null)
                throw new ArgumentNullException(nameof(keySelector));

            var set = new HashSet<TKey>();
            foreach (var item in source)
                if (set.Add(keySelector(item))) yield return item;
        }
    }
}