Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

nk = [int(x) for x in raw_input().strip().split()]
d = []
for i in range(nk[0]):
    d_tmp = raw_input().strip().split()
    d.append([int(d_tmp[0]), int(d_tmp[1]), d_tmp[2]])

n1 = nk[0]
k1 = nk[1]
k2 = k1 * 2

n = 0
#for i in range(k2):
#    for j in range(k2):
#        n_tmp = 0
#        for k in range(n1):
#            x = (d[k][0] - i) // k1 % 2
#            y = (d[k][1] - j) // k1 % 2
#            if x == y and d[k][2] == 'B':
#                n_tmp += 1
#            elif x != y and d[k][2] == 'W':
#                n_tmp += 1
#        if n_tmp > n:
#            n = n_tmp
for i in range(k1):
    for j in range(k1):
        n_tmp = [0, 0]
        for k in range(n1):
            x = (d[k][0] - i) // k1 % 2
            y = (d[k][1] - j) // k1 % 2
            if x == y and d[k][2] == 'B':
                n_tmp[0] += 1
            elif x != y and d[k][2] == 'W':
                n_tmp[0] += 1
            if x == y and d[k][2] == 'W':
                n_tmp[1] += 1
            elif x != y and d[k][2] == 'B':
                n_tmp[1] += 1
        if n_tmp[0] > n:
            n = n_tmp[0]
        if n_tmp[1] > n:
            n = n_tmp[1]
        n_tmp = 0

print n


### FIXED CODE ###

import numpy as np

def getBoundary(p, k, k2):
    if p - k + 1 >= 0:
        return [(p - k + 1, p + 1)]
    else:
        return [(0, p + 1), ((p - k + 1) % k2, k2)]

n, k = map(int, input().strip().split())
k2 = k * 2
b = [[0 for i in range(k2 + 1)] for j in range(k2 + 1)]

for i in range(n):
    x_str, y_str, c_str = input().strip().split()
    x = int(x_str) % k2
    y = (int(y_str) + (c_str == 'B') * k) % k2
    for xb, xe in getBoundary(x % k2, k, k2):
        for yb, ye in getBoundary(y % k2, k, k2):
            b[xb][yb] += 1
            b[xb][ye] -= 1
            b[xe][yb] -= 1
            b[xe][ye] += 1
    for xb, xe in getBoundary((x + k) % k2, k, k2):
        for yb, ye in getBoundary((y + k) % k2, k, k2):
            b[xb][yb] += 1
            b[xb][ye] -= 1
            b[xe][yb] -= 1
            b[xe][ye] += 1

arr_b = np.array(b)
for i in range(k2):
    arr_b[i + 1] += arr_b[i]
for j in range(k2):
    arr_b[:, j + 1] += arr_b[:, j]
print(arr_b.max())
### BUGGY CODE ###



import sys
sys.setrecursionlimit(50000)

nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    nCr[(n,r)]=nCr[(n,r)]%(10**9+7)
    return nCr[(n,r)]


def f(a,b):
    #print(a,b)
    if b<0 or a<0:    
        return 0
    return cmb(a+b-1,b-1)

def acinput():
    return list(map(int, input().split(" ")))

N,K=acinput()

B=K
R=N-K


for i in range(1,K+1):
    _B=B-i
    _R=R-i+1

    PB=f(_B,i)
    PR=f(_R,i+1)

    #print("b r",_B,_R)
    #print(PB,PR)
    print(int(PB*PR)%(10**9+7))



#2000 1600

### FIXED CODE ###



import sys
sys.setrecursionlimit(40000)

nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    nCr[(n,r)]=nCr[(n,r)]
    return nCr[(n,r)]


def f(a,b):
    #print(a,b)
    if b<0 or a<0:    
        return 0
    return cmb(a+b-1,b-1)

def acinput():
    return list(map(int, input().split(" ")))

N,K=acinput()

B=K
R=N-K


for i in range(1,K+1):
    _B=B-i
    _R=R-i+1

    PR=f(_R,i+1)
    if PR>0:
        PB=f(_B,i)
    else:
        PB=1
    

    #print("b r",_B,_R)
    #print(PB,PR)
    print(int(PB*PR)%(10**9+7))
### BUGGY CODE ###

n=int(input())
a_s=list(map(int,input().split()))
while True:
    a_s=set(a_s)
    a_s.discard(0)
    a_s=list(a_s)
    index=a_s.index(min(a_s))
    for i in range(len(a_s)):
        if i!=index and min(a_s)!=0:
            a_s[i]=a_s[i]%min(a_s)
    if len(a_s)==1:
        break
print(sum(a_s))


### FIXED CODE ###

n=int(input())
a_s=list(map(int,input().split()))

def sakujo(a_list):
    for i in range(len(a_list)):
        if a_list[i]!=0:
            end=i
            break
    return a_list[i:]

while True:
    a_s.sort()
    a_s=sakujo(a_s)
    if len(a_s)==1:
        break
    for i in range(1,len(a_s)):
        a_s[i]=a_s[i]%a_s[0]

print(sum(a_s))
### BUGGY CODE ###

n = int(input())
s = list(input())
passwd = set()
for i in range(1,n-1):
    pre = set(s[:i])
    post = (s[i+1:])
    for c0 in pre:
        for c1 in post:
            passwd.add(c0+s[i]+c1)
print(len(passwd))

### FIXED CODE ###

import collections as col
n = int(input())
s = list(input())
passwd = set()
pre = col.Counter({'0':0,'1':0,'2':0,'3':0,'4':0,'5':0,'6':0,
                                '7':0,'8':0,'9':0})
post = col.Counter(s[1:])
for i in range(1,n-1):
    pre[s[i-1]] += 1
    post[s[i]] = max(0, post[s[i]]-1) 
    for c0 in pre:
        for c1 in post:
            if pre[c0] != 0 and post[c1] != 0:
                passwd.add(c0+s[i]+c1)
print(len(passwd))
### BUGGY CODE ###

n = int(input())

a = list(map(int, input().split()))

count = 0

for index, a_item in enumerate(a):
    ab = a[index:].count(a_item)
    if ab >= 2:
        count += 1
        break

if count == 0:
    print('YES')
else:
    print('NO')

### FIXED CODE ###

n = int(input())

a = list(map(int, input().split()))

a.sort()

count = 0

for i in range(n-1):
    if a[i] == a[i+1]:
        count += 1

if count == 0:
    print('YES')
else:
    print('NO')
### BUGGY CODE ###

n,m = map(int,input().split())
keyScope = []
ans = 0
for i in range(m):
    l,r = map(int,input().split())
    keyScope.append([j for j in range(l,r+1)])
# k in keyScope[i] (i= 0~m) を確かめたい
for k in range(1,n+1):
    gate = 0
    while k in keyScope[gate]:
        if gate == m-1:
            ans += 1
            break
        else:
            gate += 1
print(ans)

### FIXED CODE ###

n,m = map(int,input().split())
lScope = []
rScope = []
ans = 0
for i in range(m):
    l,r = map(int,input().split())
    lScope.append(l)
    rScope.append(r)
# k in keyScope[i] (i= 0~m) を確かめたい
if min(rScope) >= max(lScope):
    print(min(rScope) - max(lScope) + 1)
else:
    print(0)
### BUGGY CODE ###

s = list(input())

words = ['dream', 'dreamer', 'erase', 'eraser']
candidate = ['']
ans = 'NO'

while len(candidate)>0:
    p = candidate.pop()
    if list(p) == s:
        ans = 'YES'
        break
    if len(p) >= len(s):
        continue
    for w in words:
        candidate.append(p+w)

print(ans)


### FIXED CODE ###

s = list(input())

words = ['dream', 'dreamer', 'erase', 'eraser']
ans = 'YES'

pointer = len(s)
while pointer > 0:
    if s[pointer-5: pointer] == list(words[0]):
        pointer -= 5
    elif s[pointer-5: pointer] == list(words[2]):
        pointer -= 5
    elif s[pointer-7: pointer] == list(words[1]):
        pointer -= 7
    elif s[pointer-6: pointer] == list(words[3]):
        pointer -= 6
    else:
        ans = 'NO'
        break

print(ans)
### BUGGY CODE ###

l,r = map(int,input().split())
if r-l<1:
    print(0)
else:
    res = 2019
    for i in range(l,r):
        for j in range(i+1,r+1):
            res = min((i*j)%2019,res)
    print(res)

### FIXED CODE ###

l,r = map(int,input().split())
if r-l>=2019:
    print(0)
else:
    res = 2019
    for i in range(l,r):
        for j in range(i+1,r+1):
            res = min((i*j)%2019,res)
    print(res)
### BUGGY CODE ###

N, A, B = map(int,input().split())

list = []
for _ in range(N):
    [list.append('b') for i in range(A)]
    [list.append('c') for j in range(B)]

a = list[:N:]
print(a.count("b"))


### FIXED CODE ###

N, A, B = map(int,input().split())
ans = A*(N//(A+B)) + min(N%(A+B),A)
print(ans)
### BUGGY CODE ###

N,Q = map(int,input().split())
s = input()
t,d = [],[]
for i in range(Q):
    t_next,d_next = input().split()
    t.append(t_next),d.append(d_next)
def golem_out(index):
    golem = [i for i in range(N)]
    for i in range(Q):
        if(s[golem[index]]==t[i]):
            if(d[i]=='R'):
                golem[index] += 1
                if(golem[index]==N):
                    return 'R'
            elif(d[i]=='L'):
                golem[index] -= 1
                if(golem[index]==-1):
                    return 'L'
    return False
#左にそれるゴーレムの数
def count_left():
    left,right = 0,N-1
    if(golem_out(0)!='L'):
        return 0
    if(golem_out(N-1)=='L'):
        return N
    while(right-left>1):
        if(golem_out(int((left+right)/2)) == 'L'):
            left = int((left+right)/2)
        else:
            right = int((left+right)/2)
    return right
def count_right():
    left,right=0,N-1
    if(golem_out(N-1)!='R'):
        return 0
    if(golem_out(0)=='R'):
        return N
    while(right-left>1):
        if(golem_out(int((left+right)/2)) == 'R'):
            right = int((left+right)/2)
        else:
            left = int((left+right)/2)
    return N-1-left
print(N - count_left()-count_right())

### FIXED CODE ###

N,Q = map(int,input().split())
S = '_'+input()+'_'
td = [input().split() for _ in range(Q)]
R = N+1
L = 0
for i in range(Q-1,-1,-1):
    t,d = td[i]
    if t == S[R-1] and d == 'R':
        R -= 1
    elif t == S[R] and d == 'L':
        R += 1
    if t == S[L+1] and d == 'L':
        L += 1
    elif t == S[L] and d == 'R':
        L -= 1
print(max(0,R-L-1))
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
A_plus_i = [i + A[i] for i in range(N)]
A_minus_i = [A[i] - i for i in range(N)]
keys = [-x for x in A_minus_i if x < 0]
ans = 0
for key in keys:
    ans += A_plus_i.count(key)
print(ans)

### FIXED CODE ###

from collections import Counter

N = int(input())
A = list(map(int, input().split()))
i_plus_A = Counter([i + A[i] for i in range(N)])
i_minus_A = [i - A[i] for i in range(N)]
ans = 0
for x in i_minus_A:
    ans += i_plus_A[x]
print(ans)
### BUGGY CODE ###

t = list(input())
count = 0
nums = []
num = []
ans = []

for i in range(len(t)):
  if t[i] == '?':
    count += 1
    num.append(i)

for i in range(2**count):
  tmp = ['D'] * count
  for j in range(count):
    if i >> j & 1:
      tmp[j] = 'P'
    a = num[j]
    t[a] = tmp[j]
  b = ''.join(t)
  nums.append(b)
  c = int(b.count('PD')) + int(b.count('D'))
  ans.append(c)
  
d = ans.index(max(ans))
print(nums[d])

### FIXED CODE ###

t = list(input())
t = ''.join(t)
t = t.replace('?',  'D')

print(t)
### BUGGY CODE ###

n,m = map(int,input().split())
A = map(int,input().split())
A = sorted(A, reverse=True)

#print(A)

for i in range(m):
    if i == 0:
        A[0] = A[0]//2
    else:
        list = A[0:i+1]
#        print(list)
        max_num = max(list)
        max_id = list.index(max_num)
        A[max_id] = max_num//2

#print(A)
print(sum(A))


### FIXED CODE ###

import heapq

n,m = map(int,input().split())
a = list(map(int,input().split()))
list = []
maximum = 0

for i in range(n):
    num = a[i]*(-1)
    heapq.heappush(list,num)

for i in range(m):
    maximum = heapq.heappop(list)
    if maximum%2 == 0:
        maximum = maximum//2
    else:
        maximum = (maximum//2)+1
    heapq.heappush(list,maximum)
#    print(list)

ans = 0
minimum = 0

for i in range(n):
    minimum = heapq.heappop(list)
    minimum = minimum*(-1)
    ans = ans + minimum

print(ans)
### BUGGY CODE ###

K,S=map(int,input().split())
count=0
for i in range(K+1):
    for j in range(K+1):
        for k in range(K+1):
            if (i+j+k == S):count+=1

print(count)
        
        

### FIXED CODE ###

K,S=map(int,input().split())
count=0
for i in range(K+1):
    for j in range(K+1):
        if (S-i-j <= K and S-i-j>=0):count+=1

print(count)
### BUGGY CODE ###

from fractions import gcd  # python<=3.4

MOD=1000000007

n = int(input().rstrip())
a = [int(x) for x in input().rstrip().split()]

el = 1
for ai in a:
  g = gcd(el,ai)
  el = el // g * ai

sum = 0
for ai in a:
  sum += el // ai

sum %= MOD
print(sum)


### FIXED CODE ###

from fractions import gcd  # python<=3.4

MOD=1000000007

def ADD(x, y):
  return (x + y) % MOD

def SUB(x, y):
  return (x - y + MOD) % MOD

def MUL(x, y):
  return x * y % MOD

def POW(x, e):
  v = 1
  while e:
    if e & 1:
      v = MUL(v, x)
    x = MUL(x, x)
    e >>= 1
  return v

def INV(x):
  return POW(x, MOD-2)

def DIV(x, y):
  return MUL(x, INV(y))


n = int(input().rstrip())
a = [int(x) for x in input().rstrip().split()]

el = 1
for ai in a:
  g = gcd(el,ai)
  el = el // g * ai

el %= MOD

sum = 0
for ai in a:
  sum += DIV(el, ai)

sum %= MOD
print(sum)
### BUGGY CODE ###

n=int(input())
s=input()
r=[]
g=[]
b=[]
res=0
for i in range(n):
  if s[i]=="R":
    r.append(i)
  elif s[i]=="G":
    g.append(i)
  elif s[i]=="B":
    b.append(i)

res=len(g)*len(b)*len(r)

for i in r:
  for j in g:
    if (i+j)%2==0 and int((i+j)/2) in b:
      res-=1
    if abs(i-j)+max(i,j) in b:
      res-=1
    if min(i,j)-abs(i-j) in b:
      res-=1
print(res)

### FIXED CODE ###

n=int(input())
s=input()
r=[]
g=[]
b=[]
res=0
for i in range(n):
  if s[i]=="R":
    r.append(i)
  elif s[i]=="G":
    g.append(i)
  elif s[i]=="B":
    b.append(i)

res=len(g)*len(b)*len(r)
r=set(r)
g=set(g)
b=set(b)
for i in r:
  for j in g:
    if (i+j)%2==0 and int((i+j)/2) in b:
      res-=1
    if abs(i-j)+max(i,j) in b:
      res-=1
    if min(i,j)-abs(i-j) in b:
      res-=1
print(res)
### BUGGY CODE ###

H,W,M=map(int,input().split())
L=[]
bh=[0]*(H+1)
bw=[0]*(W+1)
mh,mw=[0,[]],[0,[]]
for _ in range(M):
    h,w=map(int,input().split())
    L.append((h,w))
    bh[h]+=1
    bw[w]+=1
    if bh[h]>mh[0]:
        mh=[bh[h],[h]]
        nh=1
    elif bh[h]==mh[0]:
        mh[1].append(h)
        nh+=1
    if bw[w]>mw[0]:
        mw=[bw[w],[w]]
        nw=1
    elif bw[w]==mw[0]:
        mw[1].append(w)
        nw+=1
ans=mh[0]+mw[0]
n=0
if nh*nw<=M:
    for h,w in L:
        if h in mh[1] and w in mw[1]:
            n+=1
if n==nh*nw:
    ans-=1
print(ans)

### FIXED CODE ###

H,W,M=map(int,input().split())
L=[]
bh=[0]*(H+1)
bw=[0]*(W+1)
for _ in range(M):
    h,w=map(int,input().split())
    L.append((h,w))
    bh[h]+=1
    bw[w]+=1
p,q=max(bh),max(bw)
num=len(list(filter(p.__eq__, bh)))*len(list(filter(q.__eq__,bw)))
num-=len(list(filter(lambda x: bh[x[0]]==p and bw[x[1]]==q, L)))
ans=p+q-1
if num>0:
    ans+=1
print(ans)
### BUGGY CODE ###

n = int(input())
a=[]
b=[]
for i in range(n):
    aa,bb = map(int,input().split())
    a.append(aa)
    b.append(bb)
sum = 0
for i in range(n):
    bb=b.index(min(b))
    bbuf = b.pop(bb)
    sum += a[bb]
    a.pop(bb)
    if sum>bbuf:
        print("No")
        exit()
print("Yes")


### FIXED CODE ###

n = int(input())
ab=[]
for i in range(n):
    ab.append(list(map(int,input().split())))
sum = 0
ab.sort(key=lambda x:x[1])
for i in range(n):
    sum += ab[i][0]
    if sum>ab[i][1]:
        print("No")
        exit()
print("Yes")
### BUGGY CODE ###

import sys
def input():
    return sys.stdin.readline()[:-1]

n=int(input())
def warshall_floyd(d):
    for x in range(n):
        for y in range(n):
            for z in range(n):
                d[y][z] = min(d[y][z],d[y][x]+d[x][z])
    return d 

graph = [[float("inf")]*n for _ in range(n)]

for i in range(n-1):
    a,b,c = map(int,input().split())
    graph[a-1][b-1] = c
    graph[b-1][a-1] = c

for i in range(n):
    graph[i][i] = 0

G = warshall_floyd(graph)

ans = []
q,k = map(int,input().split())
for i in range(q):
    x,y = map(int,input().split())
    ans.append(G[k-1][x-1] + G[k-1][y-1])

print(*ans , sep="\n")

### FIXED CODE ###

from collections import deque

def f(k,edge,n):
    root = [[] for _ in range(n)]
    for i in range(n-1):
        a,b,c = edge[i][0],edge[i][1],edge[i][2]
        root[a-1].append([b-1,c])
        root[b-1].append([a-1,c])
    
    que = deque()
    for l in root[k]:
        que.append(l)
    
    dist = [float("inf")]*n
    dist[k] = 0
    visited = [False]*n
    que = deque([k])

    while que:
        pos = que.popleft()
        if not visited[pos]:
            visited[pos] = True
        for x,y in root[pos]:
            if not visited[x]:
                dist[x] = dist[pos] + y
                visited[x] = True
                que.append(x)
    return dist

n = int(input())
edge = [list(map(int,input().split())) for _ in range(n-1)]

q,k = map(int,input().split())

ans = []
d = f(k-1,edge,n)
for i in range(q):
    x,y = map(int,input().split())
    ans.append(d[x-1] + d[y-1])

print(*ans , sep = "\n")
### BUGGY CODE ###

N, M = map(int, input().split())
ac = []
pena = 0
walen = [0]*N

for i in range(M):
    p, S = map(str, input().split())
    p = int(p) - 1
    if p in ac:
        continue
    elif S == 'AC':
        ac.append(p)
        pena += walen[p]
    else:
        walen[p] += 1

print(len(set(ac)), pena)

### FIXED CODE ###

N, M = map(int, input().split())
ac = set()
pena = 0
walen = [0]*N

for i in range(M):
    p, S = map(str, input().split())
    p = int(p) - 1
    if p in ac:
        continue
    elif S == 'AC':
        ac.add(p)
        pena += walen[p]
    else:
        walen[p] += 1

print(len(ac), pena)
### BUGGY CODE ###

from collections import deque
n,x,y = map(int,input().split())
next = [[] for i in range(n)]
next[0].append(1)
next[n-1].append(n-2)
next[x-1].append(y-1)
next[y-1].append(x-1)
for i in range(1,n-1):
  next[i].append(i-1)
  next[i].append(i+1)

ans = [0] *(n-1)

for j in range(n):
  mark = ['inf']*(n)
  que = deque([j])
  mark[j] = 0
 
  while que:
    q = que.popleft()
    for k in next[q]:
      if mark[k] == 'inf':
        mark[k] = mark[q] + 1
        que.append(k)
  
  for l in range(0,n):
    if mark[l] != 0:
      ans[mark[l]-1] += 1

import numpy as np    
arr = np.array(ans) //2
for x in arr:
  print(x)
      

### FIXED CODE ###

from collections import deque
n,x,y = map(int,input().split())
next = [[] for i in range(n)]
next[0].append(1)
next[n-1].append(n-2)
next[x-1].append(y-1)
next[y-1].append(x-1)
for i in range(1,n-1):
  next[i].append(i-1)
  next[i].append(i+1)

ans = [0] *(n-1)

for j in range(n):
  mark = ['inf']*(n)
  que = deque([j])
  mark[j] = 0
 
  while que:
    q = que.popleft()
    for k in next[q]:
      if mark[k] == 'inf':
        mark[k] = mark[q] + 1
        que.append(k)
  
  for l in range(0,n):
    if mark[l] != 0:
      ans[mark[l]-1] += 1

for x in ans:
  print(x//2)
### BUGGY CODE ###

#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def main(readline=stdin.readline):
    n, q = (int(s) for s in readline().split())
    #assert 1 <= n <= 1000000
    #assert 1 <= q <= 100000

    base = [0] * n

    for _ in range(q):
        a, v = (int(s) for s in readline().split())
        #assert 1 <= a <= n
        #assert -100 <= v <= 100

        base[a-1] += v

        index = 1
        value = base[0]
        for i, v in enumerate(base, 1):
            if value < v:
                value = v
                index = i

        print(index, value)
    exit()


if __name__ == '__main__':
    main()

### FIXED CODE ###

#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def main(readline=stdin.readline):
    n, q = (int(s) for s in readline().split())

    base = [0] * n
    midway = [0] * (n//100 + 1)
    last = [0] * (n//10000 + 1)

    for _ in range(q):
        a, v = (int(s) for s in readline().split())

        a -= 1
        base[a] += v

        b = a - a % 100

        index = b
        value = base[b]
        end = b + 100 if b + 100 < n else n
        for i in range(b, end):
            if value < base[i]:
                value = base[i]
                index = i
        midway[b//100] = index

        c = a - a % 10000
        cc = c // 100

        index = c
        value = base[c]
        for i in midway[cc:cc+100]:
            if value < base[i]:
                value = base[i]
                index = i
        last[c//10000] = index

        index = last[0]
        value = base[index]
        for i in last:
            if value < base[i]:
                value = base[i]
                index = i

        print(index+1, value)
    exit()


if __name__ == '__main__':
    main()
### BUGGY CODE ###

N, M = [int(i) for i in input().split()]
ab = []

possible = False
for _ in range(M):
    ab.append([int(i) for i in input().split()])
    
c = []
for d in ab:
    if d[1] == N:
        c.append(d[0])

for d in ab:
    if d[1] in c and d[0] == 1:
        possible = True

if possible:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

### FIXED CODE ###

N, M = [int(i) for i in input().split()]
ab = []

possible = False
for _ in range(M):
    ab.append([int(i) for i in input().split()])
    

c = []
e = []
for d in ab:
    if d[1] == N:
        c.append(d[0])
    elif d[0] == 1:
        e.append(d[1])

if len(set(c) & set(e)):
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')
### BUGGY CODE ###

#D
#import sys
#input = sys.stdin.readline
from collections import deque
s = deque(input())
q = int(input())
for _ in range(q):
    t = tuple(map(str,input().split()))
    if t[0] == '1':
        s.reverse()
    else:
        if t[1] == "1":
            s.appendleft(t[2])
        else:
            s.append(t[2])
            
print("".join(s))

### FIXED CODE ###

#D
#import sys
#input = sys.stdin.readline
from collections import deque
s = deque(input())
q = int(input())
#flg:反転してるときtrue,してないときFalse
flg = False

for _ in range(q):
    t = tuple(map(str,input().split()))
    if t[0] == '1':
        if flg:
            flg = False
        else:
            flg = True
    else:
        if t[1] == "1" and flg == False:
            s.appendleft(t[2])
        elif t[1] == "1" and flg == True:
            s.append(t[2])
        elif t[1] == "2" and flg == False:
            s.append(t[2])
        else:
            s.appendleft(t[2])
 
    
if flg:
    s.reverse()
print("".join(s))