Problem: 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.

Submission attempts (in order):
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
3
4
5
6
7
8
9
10
11^CTraceback (most recent call last):
  File "/Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py", line 48, in <module>
    main(ptvsdArgs)
  File "/Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/lib/python/old_ptvsd/ptvsd/__main__.py", line 432, in main
    run()
  File "/Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/lib/python/old_ptvsd/ptvsd/__main__.py", line 316, in run_file
    runpy.run_path(target, run_name='__main__')
  File "/Users/kenmurata/anaconda3/lib/python3.7/runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "/Users/kenmurata/anaconda3/lib/python3.7/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/Users/kenmurata/anaconda3/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/kenmurata/practice/AtCoder/test.py", line 10, in <module>
    print(j)
KeyboardInterrupt
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 63018 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
^CTraceback (most recent call last):
  File "/Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py", line 48, in <module>
    main(ptvsdArgs)
  File "/Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/lib/python/old_ptvsd/ptvsd/__main__.py", line 432, in main
    run()
  File "/Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/lib/python/old_ptvsd/ptvsd/__main__.py", line 316, in run_file
    runpy.run_path(target, run_name='__main__')
  File "/Users/kenmurata/anaconda3/lib/python3.7/runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "/Users/kenmurata/anaconda3/lib/python3.7/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/Users/kenmurata/anaconda3/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/kenmurata/practice/AtCoder/test.py", line 10, in <module>
    if int(s[j:j+nagasa]) == tansaku:
KeyboardInterrupt
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 63781 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
cpa
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 51171 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
cpa
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 51236 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
cbaa
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 51386 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
cbaa
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 51967 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
cbaa
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 52232 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
0
0
0
1
1
0
cbaa
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 52636 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
0
0
0
1
1
0
aabc
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 52680 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
aabc
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 52735 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
xy
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 55393 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
xy
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 55498 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
aacb
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 55580 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
pca
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 56916 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
cap
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 57679 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
cap
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 58073 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
c p
cap
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 58448 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
 cp
acp
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 59236 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
 cp
acp
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 59555 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
 c
ac
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 62327 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
cp 
a
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 62438 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
cpa
cpa
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 62627 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
aabc
(base) bash-3.2$ cd /Users/kenmurata/practice ; env PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /Users/kenmurata/anaconda3/bin/python /Users/kenmurata/.vscode/extensions/ms-python.python-2020.2.64397/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 62684 /Users/kenmurata/practice/AtCoder/test.py < /Users/kenmurata/practice/AtCoder/input.txt 
xy
(base) bash-3.2$ 

def main():
    s = input()
    q = int(input())
    l,r = '',''
    rev = 0
    for i in range(q):
        Q = input().split()
        if Q[0]=="1":
            rev = (rev+1)%2
        else:
            if Q[1]=='1':
                if rev==0:
                    l = Q[2]+l
                else:
                    r = r+Q[2]
            else:
                if rev==0:
                    r = r+Q[2]
                else:
                    l = Q[2]+l
    s = l+s+r
    if rev==0:
        print(s)
    else:
        print(s[::-1])

if __name__ == "__main__":
    main()

Verdicts: Runtime Error

Accepted