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

import math

s = input()
q = int(input())

l = ['']
k = 0
m = 0
cnt = 0
str = 0
for i in range(q):
    a = input().split()
    if(a[0] == '1'):
        k += 1
        m += 1
    if(a[0] == '2'):
        if(k %2 == 1):
            l = l[::-1]
            k = 0
            cnt = str -cnt
        else:
            k = 0
        if(a[1] == '1'):
            l.insert(0,a[2])
            str += 1
            cnt += 1
        else:
            l.append(a[2])
            str += 1
if(k%2 ==1):
    l = l[::-1]
    cnt = str -cnt
if(m%2 == 1):
    s = s[::-1]
l[cnt] = s
l = ''.join(l)
print(l)



### FIXED CODE ###

from collections import deque

s = list(input())
q = int(input())

mae = deque()
usi = deque()
k = 0
m = 0
for i in range(q):
    a = input().split()
    if(a[0] == '1'):
        k += 1
    if(a[0] == '2'):
        if(k %2 == 0):
            if(a[1] == '1'):
                mae.appendleft(a[2])
            else:
                usi.append(a[2])
        else:
            if(a[1] == '1'):
                usi.append(a[2])
            else:
                mae.appendleft(a[2])
mae = list(mae)
usi = list(usi)

if(k % 2 == 1):
    mae = mae[::-1]
    usi=usi[::-1]
    s = s[::-1]
    s = ''.join(usi+s+mae)
else:
    s = ''.join(mae+s+usi)
print(s)
### BUGGY CODE ###

def input():
    import sys
    return sys.stdin.readline().rstrip()


def main():
    import math
    import collections
    import itertools

    s = input()
    k = int(input())

    chars = sorted(set(s))

    num_substr = 0
    for char in chars:
        substrs = set()
        for i in range(len(s)):
            if s[i] == char:
                for j in range(i, len(s)):
                    substrs.add(s[i:j + 1])
        
        if num_substr + len(substrs) < k:
            num_substr += len(substrs)
            # len_substr -= 1
            continue
        else:
            print(sorted(list(substrs))[k - num_substr - 1])
            break




if __name__ == '__main__':
    main()

### FIXED CODE ###

def input():
    import sys
    return sys.stdin.readline().rstrip()


def main():
    import math
    import collections
    import itertools

    s = input()
    k = int(input())

    substrs = set()
    for i in range(1, 5 + 1):
        for j in range(len(s) - i + 1):
            substrs.add(s[j:j + i])
    # print(substrs)
    print(sorted(list(substrs))[k - 1])


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

s=input()
q=int(input())
muki=True
a=[[_ for _ in input().split()] for i in range(q)]
for i in a:
    if i[0]=="1":
        muki=not muki
        continue
    if (i[1]=="1" and muki) or (i[1]=="2" and not muki):
        s=i[2]+s
    else:
        s+=i[2]
if muki:
    print(s)
else:
    print(s[::-1])

### FIXED CODE ###

s=input()
q=int(input())
muki=True
a=[[_ for _ in input().split()] for i in range(q)]
front=""
back=""
for i in a:
    if i[0]=="1":
        muki=not muki
        continue
    if (i[1]=="1" and muki) or (i[1]=="2" and not muki):
        front=i[2]+front
    else:
        back+=i[2]
if muki:
    print(front+s+back)
else:
    print((front+s+back)[::-1])
### BUGGY CODE ###

A, B, C, D = list(map(int,input().split()))

ans = 0
for i in range(A,B+1):
    if (i /C).is_integer() or (i /D).is_integer():
        pass
    else:
        ans += 1

print(ans)

### FIXED CODE ###

from fractions import gcd
a,b,c,d = map(int, input().split())
 
lcm = (c*d)//gcd(c,d)
 
x = ((a-1)//c)+((a-1)//d)-((a-1)//lcm)
y = (b//c)+(b//d)-(b//lcm)
 
print((b-(a-1))-(y-x))
### BUGGY CODE ###

import numpy as np
N = int(input())
A = np.array(input().split(),dtype=np.int32)
total_medians = N*(N+1)//2
# n件のデータ
# (上側中央値 >= x) iff (a > x となる a が(n-1)//2以下)

def BIT_update(BIT,x,value):
  # 番号xにvalueを加える
  L = len(BIT)
  while x < L:
    BIT[x] += value
    bottom_bit = x & (-x)
    x += bottom_bit
  return

def BIT_sum(BIT,x):
  # 番号1～xの和を求める
  result = 0
  while x:
    result += BIT[x]
    bottom_bit = x & (-x)
    x -= bottom_bit
  return result

def test(x):
  # 「答はx以下である」
  # 「xより真に大きな区間中央値が(total_medians-1)//2個以下」
  B = np.concatenate([[0],(A > x).cumsum()])
  # count L,R such that -1<=L<R<=N-1 and B(R)-B(L) >= (R-L)/2
  # count L,R such that B(R) - R/2 >= B(L) - L/2
  C = 2*B-np.arange(N+1)
  C -= np.min(C) - 1
  M = C.max()
  BIT = [0]*(M+1)
  
  cnt = 0
  for R in range(1,N+1):
    BIT_update(BIT,C[R-1],1)
    cnt += BIT_sum(BIT,C[R]) # 右端がRとなる(L,R)対
  return cnt <= (total_medians-1)//2

left = 0 # 答はleftより大きいである
right = 10**9 # 答はright以下である
while right - left > 1:
  mid = (left+right)//2
  if test(mid):
    right = mid
  else:
    left = mid
    
answer = right
print(answer)

### FIXED CODE ###

N = int(input())
A = [int(x) for x in input().split()]
total_medians = N*(N+1)//2
# n件のデータ
# (上側中央値 >= x) iff (a > x となる a が(n-1)//2以下)

def BIT_update(BIT,x,value):
  # 番号xにvalueを加える
  L = len(BIT)
  while x < L:
    BIT[x] += value
    bottom_bit = x & (-x)
    x += bottom_bit
  return

def BIT_sum(BIT,x):
  # 番号1～xの和を求める
  result = 0
  while x:
    result += BIT[x]
    bottom_bit = x & (-x)
    x -= bottom_bit
  return result

def test(x):
  # 「答はx以下である」
  # 「xより真に大きな区間中央値が(total_medians-1)//2個以下」
  B = [0]
  for a in A:
    B.append(B[-1] + (1 if a>x else -1))
  m = min(B)-1
  B = [x-m for x in B]

  BIT = [0]*(N+1)
  
  cnt = 0
  for R in range(1,N+1):
    BIT_update(BIT,B[R-1],1)
    cnt += BIT_sum(BIT,B[R]) # 右端がRとなる(L,R)対
  return cnt <= (total_medians-1)//2

AA = sorted(A)
left = -1 # 答はAA[left]より大きい
right = len(AA)-1 # 答はAA[right]以下である
while right - left > 1:
  mid = (left+right)//2
  if test(AA[mid]):
    right = mid
  else:
    left = mid
answer = AA[right]

print(answer)
### BUGGY CODE ###

N = int(input())
R = [tuple(map(int, input().split())) for _ in range(N)]
B = [tuple(map(int, input().split())) for _ in range(N)]

dp = {}


def check(v, p):
    return p & (1 << v)


def visit(v, p):
    return p ^ (1 << v)


def count(p):
    return bin(p).count('1')


def solve(i, p):
    if i == N:
        return count(p)

    k = (i, p)
    if k in dp:
        return dp[k]

    a, b = R[i]
    # ペアにならなかった
    m = solve(i+1, p)
    for j, (c, d) in enumerate(B):
        # すでにペアを持つ要素は無視
        if check(j, p):
            continue
        # ペアになった
        if a < c and b < d:
            m = max(m, solve(i+1, visit(j, p)))
    dp[k] = m
    return m


print(solve(0, 0))


### FIXED CODE ###

N = int(input())
R = [tuple(map(int, input().split())) for _ in range(N)]
B = [tuple(map(int, input().split())) for _ in range(N)]


R.sort(reverse=True, key=lambda k: k[1])
B.sort()

p = []
q = []
for c, d in B:
    for a, b in R:
        if (a, b) in p:
            continue
        if a < c and b < d:
            p.append((a, b))
            q.append((c, d))
            break

print(len(p))
### BUGGY CODE ###

import math

def gcd(a,b,c):
    if a == b==c:
        return a
    elif a ==b and b!=c :
        return math.gcd(a,c)
    elif a ==c and b!=c:
        return math.gcd(a,b)
    elif b ==c and a!=c:
        return math.gcd(a,b)
    else:
        return math.gcd(math.gcd(a,b),c)
        
cnt=0
K = int(input())
for i in range(1,K+1):
    for m in range(1,K+1):
        for u in range(1,K+1):
            cnt += gcd(i,m,u)
print(cnt)

### FIXED CODE ###

import math
K = int(input())

def gcd(a,b):
    return math.gcd(a,b)
cnt=0
for i in range(1,K+1):
    for m in range(1,K+1):
        d = math.gcd(i,m)
        for u in range(1,K+1):
            cnt+=math.gcd(d,u)
            
print(cnt)
### BUGGY CODE ###

def main():
    from sys import stdin
    readline = stdin.readline
    H, N = map(int, readline().split())
    AB = [list(map(int, readline().split())) for _ in range(N)]

    dp = [100000000] * (H + 10 ** 4 + 1)
    dp[0] = 0
    for i in range(H):
        if dp[i] == 100000000:
            continue
        for j in range(N):
            a, b = AB[j]
            if dp[i] + b < dp[i + a]:
                dp[i + a] = dp[i] + b
    print(min(dp[H:]))


main()


### FIXED CODE ###

def main():
    from sys import stdin
    readline = stdin.readline
    H, N = map(int, readline().split())
    AB = [list(map(int, readline().split())) for _ in range(N)]

    dp = [100000000] * (H + 10 ** 4 + 1)
    dp[0] = 0
    for i in range(H):
        if dp[i] == 100000000:
            continue
        for j in range(N):
            a, b = AB[j]
            if dp[i] + b < dp[i + a]:
                dp[i + a] = dp[i] + b
    print(min(dp[H:]))


main()
### BUGGY CODE ###

n=int(input())
l=list(map(int, input().split()))
m=max(l)
for i in range(1, n+1):
  if i<=m:
    print(l.count(i))
  else:
    print(0)

### FIXED CODE ###

n=int(input())
l=list(map(int, input().split()))
import collections

c = collections.Counter(l)

# Counter({'a': 4, 'c': 2, 'b': 1})
for i in range(1, n+1):
  if i in c:
    print(c[i])
  else:
    print(0)
### BUGGY CODE ###

from collections import deque
import sys

N=int(input())
A=deque(map(int,input().split()))
setA=set(A)
number1={}#-1
number2={}
ans={}
if(len(setA)==1):
    answer= len(A) - 1
    q=(answer - 1) * answer //2
    for _ in range(len(A)):
        print(q)
    sys.exit()


if(len(setA)==len(A)):
    for _ in range(len(A)):
        print(0)
    sys.exit()


for i in setA:
    tmp=A.count(i) - 1
    souwa=(tmp - 1)*(tmp)//2
    number1[i]=souwa
    souwa=tmp*(tmp+1)//2
    number2[i]=souwa
    total=number1[i]
    total-=number2[i]
    ans[i]=total
t=sum(number2.values())

for a in A:
    print(ans[a]+t)




### FIXED CODE ###

from collections import Counter
N = int(input())
A = list(map(int,input().split()))
d=Counter(A)
dic={}
total=0
for item in d.items():
    counter=item[1]
    s1=(counter - 1)*(counter - 2)/2 #1個少ない
    s2=(counter)*(counter - 1)/2
    dic[item[0]]=int(s1 - s2)
    total+=int(s2)

for a in A:
    print(total + dic[a])
### BUGGY CODE ###

def main():
    s = " "+input()
    N = len(s)-1

    t = " "+input()
    M = len(t)-1
    if N < M:
        N, M = M, N
        s, t = t, s
    # dp[i][j] := sのi文字目, tのj文字目までを使ったLCSの長さ
    # last_letter[i][j] := s_i, t_jまでを使ったLCSの最後の文字
    dp = [[-1]*(M+1) for i in range(N+1)]
    last_letter = [[-1]*(M+1) for i in range(N+1)]
    dp[0] = [0]*(N+1)
    last_letter[0] = ['' for n in range(N+1)]
    for i in range(N+1):
        dp[i][0] = 0
        last_letter[i][0] = ''
    for i in range(1, N+1):
        for j in range(1, M+1):
            
            if s[i] == t[j]:
                dp[i][j] = dp[i-1][j-1]+1
                last_letter[i][j] = last_letter[i-1][j-1] + s[i]
            elif s[i] != t[j]:
                if dp[i-1][j] > dp[i][j-1]:
                    dp[i][j] = dp[i-1][j]
                    last_letter[i][j] = last_letter[i-1][j]
                else:
                    dp[i][j] = dp[i][j-1]
                    last_letter[i][j] = last_letter[i][j-1]
    print(last_letter[N][M])

if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys
sys.setrecursionlimit(3003)
def main():
    s = " "+input()
    N = len(s)-1

    t = " "+input()
    M = len(t)-1
    if N < M:
        N, M = M, N
        s, t = t, s
    # dp[i][j] := sのi文字目, tのj文字目までを使ったLCSの長さ
    dp = [[-1]*(M+1) for i in range(N+1)]
    dp[0] = [0]*(N+1)
    for i in range(N+1):
        dp[i][0] = 0
    for i in range(1, N+1):
        for j in range(1, M+1):
            
            if s[i] == t[j]:
                dp[i][j] = dp[i-1][j-1]+1
            elif s[i] != t[j]:
                if dp[i-1][j] > dp[i][j-1]:
                    dp[i][j] = dp[i-1][j]
                else:
                    dp[i][j] = dp[i][j-1]
    print(get(s, t, N, M, dp))

def get(s, t, i, j, dp):
    if i == 0 or j == 0:
        return ""

    if s[i]==t[j]:
        return get(s, t, i-1, j-1, dp) + s[i]
    elif s[i] != t[j]:
        if dp[i-1][j] > dp[i][j-1]:
            return get(s, t, i-1, j, dp)
        else:
            return get(s, t, i, j-1, dp)


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

import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline
n,m=map(int,input().split())
z=n*(n-1)//2
l=[-1]*n
a=[1]*n
b=[input().split() for _ in range(m)]
c=[0]*m+[z]
def r(s):
  t=l[s]
  return s if t<0 else r(t)
def f(s):
  return r(int(s)-1)
for i in range(m-1,-1,-1):
  x,y=map(f,b[i])
  if x!=y:
    l[x]=y
    l=list(map(lambda w:y if w==x else w,l))
    z-=a[x]*a[y]
    a[y]+=a[x]
  c[i]=z
for x in c[1:]:
  print(x)

### FIXED CODE ###

import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline
n,m=map(int,input().split())
z=n*(n-1)//2
l=[-1]*n
a=[1]*n
b=[input().split() for _ in range(m)]
c=[0]*m+[z]
def r(s):
  t=l[s]
  if t<0:
    return s
  else:
    l[s]=r(t)
    return l[s]
def f(s):
  return r(int(s)-1)
for i in range(m-1,-1,-1):
  x,y=map(f,b[i])
  if x!=y:
    l[x]=y
    z-=a[x]*a[y]
    a[y]+=a[x]
  c[i]=z
for x in c[1:]:
  print(x)
### BUGGY CODE ###

S = input()
mod = 10 ** 9 + 7
count = 0
leftA = (1 if S[0] == "A" else 0)
leftQ = (1 if S[0] == "?" else 0)
rightC = S[1:].count("C")
rightQ = S[1:].count("?")

def mult(a, b):
    if b == 0:
        return a
    else:
        return ((3*a + b) * pow(3, b-1))

for i in range(1,len(S)-1):
    if S[i] == "B" or S[i] == "?":
        rightQ -= (1 if S[i] == "?" else 0)
        count += mult(leftA, leftQ) * mult(rightC, rightQ)
        count %= mod
        leftQ += (1 if S[i] == "?" else 0)
    else:
        leftA += (1 if S[i] == "A" else 0)
        rightC -= (1 if S[i] == "C" else 0)
print(count)

### FIXED CODE ###

S = input()
mod = 10 ** 9 + 7
count = 0
leftA = (1 if S[0] == "A" else 0)
leftQ = (1 if S[0] == "?" else 0)
rightC = S[1:].count("C")
rightQ = S[1:].count("?")

def mult(a, b, mod):
    if b == 0:
        return a
    else:
        return ((3*a + b) * pow(3, b-1, mod))

for i in range(1,len(S)-1):
    if S[i] == "B" or S[i] == "?":
        rightQ -= (1 if S[i] == "?" else 0)
        count += mult(leftA, leftQ, mod) * mult(rightC, rightQ, mod)
        count %= mod
        leftQ += (1 if S[i] == "?" else 0)
    elif S[i] == "A":
        leftA += 1 
    else:
        rightC -= 1
print(count)
### BUGGY CODE ###

from collections import defaultdict
import sys

if __name__ == "__main__":
  sys.setrecursionlimit(1000000)
  input = sys.stdin.readline
  n, m = map(int, input().split())

  if m%2==1:
    print(-1)
    exit()

  graph = defaultdict(set)
  grapht = defaultdict(set)
  for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].add(b)
    graph[b].add(a)
    grapht[a].add(b)
    grapht[b].add(a)

  graph2 = defaultdict(set)
  visited = [0]*n
  num = [1]*n

  def dfs(x):
    visited[x] = 1

    for nx in graph[x]:
      if not visited[nx]:
        dfs(nx)

    t = set()
    if num[x]==1 and len(grapht[x])%2==0:
      for e in grapht[x]:
        graph2[x].add(e)
        t.add(e)
    elif num[x]==-1 and len(grapht[x])%2==0:
      for j, e in enumerate(grapht[x]):
        if j==len(grapht[x])-1:
          graph2[e].add(x)
          num[e] *= -1
        else:
          graph2[x].add(e)
        t.add(e)
    elif num[x]==1 and len(grapht[x])%2==1:
      for j, e in enumerate(grapht[x]):
        if j==len(grapht[x])-1:
          graph2[e].add(x)
          num[e] *= -1
        else:
          graph2[x].add(e)
        t.add(e)
    else:
      for e in grapht[x]:
        graph2[x].add(e)
        t.add(e)
    for e in t:
      grapht[x].remove(e)
      grapht[e].remove(x)

  dfs(0)

  for i in range(n):
    if len(graph2[i])%2==1:
      print(-1)
      exit()

  for i in range(n):
    for e in graph2[i]:
      print(i+1, e+1)

### FIXED CODE ###

from collections import defaultdict
import sys

def dfs(visited, graph, grapht, graph2, num, x):
  visited[x] = 1

  for nx in graph[x]:
    if not visited[nx]:
      dfs(visited, graph, grapht, graph2, num, nx)

  t = set()
  if num[x]==1 and len(grapht[x])%2==0:
    for e in grapht[x]:
      graph2[x].add(e)
      t.add(e)
  elif num[x]==-1 and len(grapht[x]) and len(grapht[x])%2==0:
    for e in grapht[x]:
      graph2[x].add(e)
      t.add(e)
    tt = grapht[x].pop()
    graph2[tt].add(x)
    graph2[x].remove(tt)
    num[tt] *= -1
  elif num[x]==1 and len(grapht[x])%2==1:
    for e in grapht[x]:
      graph2[x].add(e)
      t.add(e)
    tt = grapht[x].pop()
    graph2[tt].add(x)
    graph2[x].remove(tt)
    num[tt] *= -1
  else:
    for e in grapht[x]:
      graph2[x].add(e)
      t.add(e)
  for e in t:
    if e in grapht[x]:
      grapht[x].remove(e)
    if x in grapht[e]:
      grapht[e].remove(x)

if __name__ == "__main__":
  sys.setrecursionlimit(1000000)
  input = sys.stdin.readline
  n, m = map(int, input().split())

  if m%2==1:
    print(-1)
    exit()

  graph = defaultdict(set)
  grapht = defaultdict(set)
  for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].add(b)
    graph[b].add(a)
    grapht[a].add(b)
    grapht[b].add(a)

  graph2 = defaultdict(set)
  visited = [0]*n
  num = [1]*n

  dfs(visited, graph, grapht, graph2, num, 0)

  for i in range(n):
    if len(graph2[i])%2==1:
      print(-1)
      exit()

  for i in range(n):
    for e in graph2[i]:
      print(i+1, e+1)
### BUGGY CODE ###

# 約数列挙
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

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

cnt_list = [0] * N
M = 0
ans_list = []
for i in range(N-1, -1, -1):
    if A[i] == 1 and cnt_list[i] % 2 == 0:
        M += 1
        ans_list.append(i+1)
        tmp_list = make_divisors(i+1)
        for t in tmp_list:
            cnt_list[t-1] += 1
    elif A[i] == 1 and cnt_list[i] % 2 == 1:
        pass
    elif A[i] == 0 and cnt_list[i] % 2 == 0:
        pass
    else:
        M += 1
        ans_list.append(i+1)
        tmp_list = make_divisors(i+1)
        for t in tmp_list:
            cnt_list[t-1] += 1
print(M)
if M == 0:
    exit()
print(" ".join(map(str, ans_list)))

### FIXED CODE ###

# 約数列挙
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

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

cnt_list = [0] * N
M = 0
ans_list = []
for i in range(N-1, -1, -1):
    if A[i] == 1 and cnt_list[i] % 2 == 0:
        M += 1
        ans_list.append(i+1)
        tmp_list = make_divisors(i+1)
        for t in tmp_list:
            cnt_list[t-1] += 1
    elif A[i] == 1 and cnt_list[i] % 2 == 1:
        pass
    elif A[i] == 0 and cnt_list[i] % 2 == 0:
        pass
    else:
        M += 1
        ans_list.append(i+1)
        tmp_list = make_divisors(i+1)
        for t in tmp_list:
            cnt_list[t-1] += 1
print(M)
if M == 0:
    exit()
print(" ".join(map(str, ans_list)))
### BUGGY CODE ###

#!/usr/bin/env python3
from itertools import product
n = int(input())
xy = [list(map(int, input().split())) for _ in range(n)]

xy.sort(key=lambda x: (x[0], x[1]))
ps = set()
for i, j in product(range(n), range(n)):
    if i >= j:
        continue
    ps.add(xy[j][0] - xy[i][0])
qs = set()
for i, j in product(range(n), range(n)):
    if i >= j:
        continue
    qs.add(xy[j][1] - xy[i][1])

m = 0
for p, q in product(ps, qs):
    c = 0
    for i in range(n - 1):
        for j in range(i + 1, n):
            if xy[j][0] - xy[i][0] != p:
                continue
            if xy[j][1] - xy[i][1] != q:
                continue
            c += 1
            break
    m = max(m, c)

xy.sort(key=lambda x: (x[1], x[0]))
ps = set()
for i, j in product(range(n), range(n)):
    if i >= j:
        continue
    ps.add(xy[j][0] - xy[i][0])
qs = set()
for i, j in product(range(n), range(n)):
    if i >= j:
        continue
    qs.add(xy[j][1] - xy[i][1])

for p, q in product(ps, qs):
    c = 0
    for i in range(n - 1):
        for j in range(i + 1, n):
            if xy[j][0] - xy[i][0] != p:
                continue
            if xy[j][1] - xy[i][1] != q:
                continue
            c += 1
            break
    m = max(m, c)

print(n - m)


### FIXED CODE ###

#!/usr/bin/env python3
from itertools import product
n = int(input())
xy = [list(map(int, input().split())) for _ in range(n)]

m = 0
xy.sort(key=lambda x: (x[0], x[1]))
for l in range(n - 1):
    for r in range(l + 1, n):
        p = xy[r][0] - xy[l][0]
        q = xy[r][1] - xy[l][1]
        c = 0
        for i in range(n - 1):
            for j in range(i + 1, n):
                if xy[j][0] - xy[i][0] != p: continue
                if xy[j][1] - xy[i][1] != q: continue
                c += 1
                break
        m = max(m, c)

xy.sort(key=lambda x: (x[1], x[0]))
for l in range(n - 1):
    for r in range(l + 1, n):
        p = xy[r][0] - xy[l][0]
        q = xy[r][1] - xy[l][1]
        c = 0
        for i in range(n - 1):
            for j in range(i + 1, n):
                if xy[j][0] - xy[i][0] != p: continue
                if xy[j][1] - xy[i][1] != q: continue
                c += 1
                break
        m = max(m, c)

print(n - m)
### BUGGY CODE ###

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

mmax = 0
pre = False
num = 0
ans = 0
ans2 = 0
for i in range(N+1):
  if i == 0:
    ans = 0
  else:
    ans += A[i-1]
  if ans > K:
    break
  for j in range(M+1):
    if j == 0:
      ans2 = ans
    else:
      ans2 += B[j-1]
    if ans2 > K:
      break
    else:
      mmax = max(mmax, i+j)

print(mmax)

### FIXED CODE ###

N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
 
a = [0]
b = [0]
for i in range(N):
  a.append(a[i]+A[i])
for i in range(M):
  b.append(b[i]+B[i])

ans, j = 0, M
for i in range(N+1):
  if a[i] > K:
    break
  while b[j] > K - a[i]:
    j -= 1
  ans = max(ans, i+j)
print(ans)
### BUGGY CODE ###

N, M, Q = map(int, input().split())

matrix = [[0 for _ in range(N+1)] for _ in range(N+1)]

for L, R in (map(int, input().split()) for _ in range(M)):
    for i in range(L, -1, -1):
        for j in range(R, N+1):
            matrix[i][j] += 1

for p, q in (map(int, input().split()) for _ in range(Q)):
    print(matrix[p][q])


### FIXED CODE ###

N, M, Q = map(int, input().split())

class Cell:
    def __init__(self):
        self.value = 0
        self.L = 0
    def __repr__(self):
        return str((self.value, self.L))

pairs = [[Cell() for _ in range(N+1)] for _ in range(N+1)]
for L, R in (map(int, input().split()) for _ in range(M)):
    cell = pairs[L][R]
    cell.value += 1
    cell.L = cell.value

for i in range(N-1, 0, -1):
    for j in range(i+1, N+1):
        cell = pairs[i][j]
        cell.L += pairs[i][j-1].L
        cell.value = cell.L + pairs[i+1][j].value
for p, q in (map(int, input().split()) for _ in range(Q)):
    print(pairs[p][q].value)
### BUGGY CODE ###

from collections import deque

s = deque(input())
q = int(input())
query = [input() for i in range(q)]

for i in range(q):
  temp = query[i].split()
  if int(temp[0]) == 1:
    s.reverse()
  else:
    if int(temp[1]) == 1:
      s.appendleft(temp[2])
    else:
      s.append(temp[2])

print(''.join(s))


### FIXED CODE ###

from collections import deque

def main():
  rev = False
  s = deque(input())
  q = int(input())
  query = tuple(input() for i in range(q))

  for i in range(q):
    temp = query[i].split()
    if int(temp[0]) == 1:
      rev = not rev
    else:
      if (int(temp[1]) == 1 and (not rev)) or (int(temp[1]) == 2 and rev):
        s.appendleft(temp[2])
      else:
        s.append(temp[2])

  if rev:
    s.reverse()

  print(''.join(s))

if __name__ == '__main__':
  main()