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

def main():
  n = int(input())
  D = list(map(int,input().split()))
  m = int(input())
  T = list(map(int,input().split()))
  tmp = []
  for t in T:
    if t in D:
      D.remove(t)
      tmp.append(t)
  if T == tmp:
    print("YES")
  else:
    print("NO")

main()

### FIXED CODE ###

def main():
  from collections import Counter
  n = int(input())
  D = list(map(int,input().split()))
  m = int(input())
  T = list(map(int,input().split()))
  
  d = dict(Counter(D))
  ans = "YES"
  for t in T:
    try:
      if d[t] - 1 >= 0:
        d[t] -= 1
      else:
        ans = "NO"
    except:
      ans = "NO"

  print(ans)

main()
### BUGGY CODE ###

import functools
import operator
n=int(input())
a=list(map(int,input().split()))
ans=functools.reduce(operator.mul, a)
if ans > 10**18:
    print(-1)
else:
    print(ans)


### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
limit=10**18
a.sort()
result=a[0]
for k in a[1:]:
    result*=k
    if result > limit:
        print(-1)
        exit()
print(result)
### BUGGY CODE ###

def dec(a):
    res = []
    for i in range(1,int(a**0.5+1)):
        if a%i==0:
            res.append(i)
            res.append(a//i)
    res = list(set(res))
    return res

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

d = {}
for i in range(N):
    if A[i] not in d:
        d[A[i]] = 1
    else:
        d[A[i]] += 1

ans = N
for i in range(N):
    dl = dec(A[i])
    for a in dl:
        if a in d:
            if a==A[i]:
                if d[a]>1:
                    ans -= 1
                    break
            else:
                ans -= 1
                break

print(ans)

### FIXED CODE ###

from collections import defaultdict
N = int(input())
A = list(map(int,input().split()))
A.sort()
count = defaultdict(lambda:0)
for a in A:
    count[a] += 1
dp = [True]*(A[-1]+1)

ans = 0
for a in A:
    if dp[a] and count[a]==1:
        ans += 1
    for i in range(1,len(dp)):
        if a*i>A[-1]:
            break
        dp[a*i] = False

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

from operator import itemgetter


def main():
    n, m = list(map(int, input().split()))
    alist = list(map(int, input().split()))
    bc = []
    for _ in range(m):
        b, c = list(map(int, input().split()))
        bc.append((b, c))

    asorted = sorted(alist)

    bc = sorted(bc, key=itemgetter(1), reverse=True)

    # print(f'initial: {asorted}')
    # print(f'bc: {bc}')

    fixed = []
    for b, c in bc:
        pick = [a for a in asorted if a >= c]

        if len(pick) > 0:
            asorted = asorted[:-len(pick)]
            fixed += pick

        if len(asorted) < 1:
            break

        for _ in range(b):
            if len(asorted) < 1:
                break
            asorted.pop(0)
            fixed.insert(0, c)

        # print(f'--- step n / {len(bc)} ---')
        # print(f'asorted: {asorted}')
        # print(f'pick   : {pick}')
        # print(f'fixed  : {fixed}')

    fixed += asorted

    print(sum(fixed))


main()

### FIXED CODE ###

from operator import itemgetter


def main():
    n, m = list(map(int, input().split()))
    alist = list(map(int, input().split()))
    bc = []
    for _ in range(m):
        b, c = list(map(int, input().split()))
        bc.append((b, c))

    xy = [(1, ai) for ai in alist] + bc
    rest = n
    total = 0
    for number, score in sorted(xy, key=itemgetter(1), reverse=True):
        if rest <= number:
            total += score * rest
            break
        else:
            total += score * number
            rest -= number

    print(total)


main()
### BUGGY CODE ###

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

ans = 0
for i in range(n):
    for j in range(i+a[i]-1, n):
        if a[i] + a[j] == abs(i-j):
          ans += 1

print(ans)

### FIXED CODE ###

import numpy as np
from collections import Counter
n = int(input())
a = np.array(list(map(int, input().split())))
ni = np.array([i for i in range(n)])

ap = a + ni
an = (a - ni) * -1

cp = Counter(ap)
cn = Counter(an)

ans = 0
for i in cp.keys():
  ans += cp[i] * cn[i] 

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

#400
N,K = map(int,input().split())
p = list(map(int,input().split()))
q = [0 for _ in range(N)]

for i in range(N):
    q[i] = (1+p[i])/2

Smax = sum(q[0:K])
for i in range(N-K):
    if sum(q[i+1:K+i+1]) > Smax:
        Smax = sum(q[i+1:K+i+1])

print(Smax)

### FIXED CODE ###

#400
N,K = map(int,input().split())
p = list(map(int,input().split()))
q = [0 for _ in range(N)]

for i in range(N):
    q[i] = (1+p[i])/2

S = sum(q[0:K])
Smax = S
for i in range(N-K):
    S = S - q[i] + q[K+i]
    if S > Smax:
        Smax = S

print(Smax)
### BUGGY CODE ###

# D - String Formation
S = input()
Q = int(input())
now = 1
for _ in range(Q):
    t = list(input().split())
    if t[0]=='1':
        now = 1+(now==1)
    else:
        if int(t[1])==now:
            S = t[2] + S
        else:
            S = S + t[2]
if now==2:
    S = S[::-1]
print(S)

### FIXED CODE ###

# D - String Formation
from collections import deque
S = input()
Q = int(input())
now = 1
tmp = [deque([]),deque([])]
for _ in range(Q):
    t = list(input().split())
    if t[0]=='1':
        now = 1+(now==1)
    else:
        if int(t[1])==now:
            tmp[0].append(t[2])
        else:
            tmp[1].append(t[2])
ans = ''
if now==1:
    i,j = 0,1
else:
    i,j = 1,0
while len(tmp[i])>0:
    ans += tmp[i].pop()
if now==2:
    S = S[::-1]
ans += S
while len(tmp[j])>0:
    ans += tmp[j].popleft()
print(ans)
### BUGGY CODE ###

s = input()
q = int(input())
hanten = True
atm = ""
kts = ""
for i in range(q):
    l = input().split()
    if l[0] == "1":
        hanten = not hanten
    else:
        if l[1] == "1":
            if hanten:
                atm = l[2] + atm
            else:
                kts = kts + l[2]
        else:
            if hanten:
                kts = kts + l[2]
            else:
                atm = l[2] + atm

ans = atm + s + kts
if hanten == False:
    print(ans[::-1])
else:
    print(ans)

### FIXED CODE ###

s = input()
q = int(input())
hanten = True
atm = ""
kts = ""
for i in range(q):
    l = input().split()
    if l[0] == "1":
        hanten = not hanten
    else:
        if l[1] == "1":
            if hanten:
                atm = l[2] + atm
            else:
                kts = kts + l[2]
        else:
            if hanten:
                kts = kts + l[2]
            else:
                atm = l[2] + atm

ans = atm + s + kts
if hanten == False:
    print(ans[::-1])
else:
    print(ans)
### BUGGY CODE ###

R, G, B, N = map(int, input().split())
count = 0

for i in range(N+1):
    if R*i > N:
        break
    for j in range(N+1):
        if R*i + G*j > N:
            break
        for k in range(N+1):
            if R*i + G*j + B*k > N:
                break
            if R*i + G*j + B*k == N:
                count += 1

print(count)

### FIXED CODE ###

R, G, B, N = map(int, input().split())
count = 0

for i in range(N+1):
    r = R*i
    for j in range(N+1):
        g = G*j
        k = int((N-r-g)/B)
        if 0 <= k and r+g+(B*k) == N:
            count += 1

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


N, Q = map(int, input().split())
S = input()
LRs = [list(map(int, input().split())) for i in range(Q)]

counts = []
for i in range(len(S)-1):
    if S[i] == 'A' and S[i+1] == 'C':
        counts.append(1)
    else:
        counts.append(0)
counts.append(0)

for LR in LRs:
    start = LR[0] - 1
    end = LR[1] 
    out = 0
    for i in range(start, end-1):
        out += counts[i]
    print(out)


### FIXED CODE ###


N, Q = map(int, input().split())
S = input()
LRs = [list(map(int, input().split())) for i in range(Q)]

counts = []
for i in range(len(S)-1):
    if S[i] == 'A' and S[i+1] == 'C':
        counts.append(1)
    else:
        counts.append(0)
counts.append(0)

rev_counts = [0]
for i in reversed(range(len(S)-1)):
    rev_counts.append(rev_counts[-1] + counts[i])
rev_counts = list(reversed(rev_counts))
# print(counts)
# print(rev_counts)


for LR in LRs:
    start = LR[0] - 1
    end = LR[1]  -1
    print(rev_counts[start] - rev_counts[end])
### BUGGY CODE ###

from functools import reduce
from fractions import gcd
import numpy as np

def lcm_base(x, y):
    return (x * y) // gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)
mod = 10**9 + 7
n = int(input())
As = list(map(int, input().split()))

lcm = lcm_list(As)

As = np.array(As)
lc = np.array([lcm]*n)

print(np.sum(lc//As) % mod)

### FIXED CODE ###

from functools import reduce
from fractions import gcd

def lcm_base(x, y):
    return (x * y) // gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers)
mod = 10**9 + 7
n = int(input())
As = list(map(int, input().split()))

lcm = lcm_list(As)
b = [lcm//As[i] for i in range(n)]

print(sum(b) % mod)
### BUGGY CODE ###

N, Q = map(int, input().split())
S = input()
lis = []
for i in range(len(S) - 1):
  if S[i] == "A" and S[i + 1] == "C":
    lis.append(i + 1)
for _ in range(Q):
  l, r = map(int, input().split())
  count = 0
  for item in lis:
    if l <= item < r:
      count += 1
  print(count)

### FIXED CODE ###

import bisect
N, Q = map(int, input().split())
S = input()
lis = []
for i in range(len(S) - 1):
  if S[i] == "A" and S[i + 1] == "C":
    lis.append(i + 1)
for _ in range(Q):
  l, r = map(int, input().split())
  ko = bisect.bisect_left(lis, l)
  kk = bisect.bisect_left(lis, r)
  print(kk - ko)
### BUGGY CODE ###

x1 = input()
x2 = [int(i) for i in input().split(" ")]

result =  1
for xv in x2:
  result = xv * result

if 10**18 < result : result = -1
print(result) 


### FIXED CODE ###

x1 = input()
x2 = [int(i) for i in input().split(" ")]

result = 0
if 0 in x2:
  None 
else:
  result = 1
  for xv in x2:
     result = xv * result
  
     if 10**18 < result : 
        result = -1
        break


print(result)
### BUGGY CODE ###

H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
d = dict()
for i in range(H):
    for j in range(W):
        d[A[i][j]] = (i, j)
Q = int(input())
LR = [tuple(map(int, input().split())) for _ in range(Q)]
for L, R in LR:
    pow = 0
    while L != R:
        i, j = d[L]
        L += D
        x, y = d[L]
        pow += abs(x - i) + abs(y - j)
    print(pow)


### FIXED CODE ###

H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]

d = [None for _ in range(H * W + 1)]
for i in range(H):
    for j in range(W):
        d[A[i][j]] = (i, j)

cumsum = [0 for _ in range(H * W + 1)]
for m in range(1, D + 1):
    i = m
    while i <= H * W:
        if i - D <= 0:
            i += D
            continue
        x1, y1 = d[i - D]
        x2, y2 = d[i]
        dist = abs(x2 - x1) + abs(y2 - y1)
        cumsum[i] = cumsum[i - D] + dist
        i += D

Q = int(input())
LR = [tuple(map(int, input().split())) for _ in range(Q)]
for L, R in LR:
    print(cumsum[R] - cumsum[L])
### BUGGY CODE ###

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()

ans = 0
M = ''
for i in range(N):
	if i >= K and M[i-K] == T[i]:
		M += 'x'
		continue
	else:
		if T[i] == 'r':
			ans += P
			M += 'r'
		if T[i] == 's':
			ans += R
			M += 's'
		if T[i] == 'p':
			ans += S
			M += 'p'
print(ans)

### FIXED CODE ###

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()

ans = 0
M = []
for i in range(N):
	if i >= K and M[i-K] == T[i]:
		M.append('x')
		continue
	else:
		if T[i] == 'r':
			ans += P
			M.append('r')
		if T[i] == 's':
			ans += R
			M.append('s')
		if T[i] == 'p':
			ans += S
			M.append('p')
print(ans)
### BUGGY CODE ###

n = int(input())
a = list(map(int,input().split()))
ans = 2*10**9
for i in range(n-1):
    x = sum(a[:i+1])
    y = sum(a[i+1:])
    ans = min(ans,abs(x-y))
print(ans)

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
s = sum(a)
ans = 10**20
x = 0
for i in range(n-1):
    x += a[i]
    y = s - x
    tmp = abs(x-y)
    ans = min(ans,tmp)
print(ans)
### BUGGY CODE ###

n,m = map(int,input().split())
card = [int(0) for i in range(n+1)]
for i in range(m):
    l,r = map(int,input().split())
    for j in range(l,r+1):
        card[j] += 1
#print(card)
cnt = 0
for i in range(n+1):
    if card[i] == m:
        cnt += 1
print(cnt)

### FIXED CODE ###

n,m = map(int,input().split())
min_card,max_card = 0,n
for i in range(m):
    l,r = map(int,input().split())
    min_card = max(min_card,l)
    max_card = min(max_card,r)
#print(min_card,max_card)
if min_card<=max_card:
    print(max_card-min_card+1)
else:
    print(0)
### BUGGY CODE ###

import sys
F = sys.stdin
S = list(map(int, list(F.readline().replace("A", "1").replace("B", "2").strip("\n"))))
T = list(map(int, list(F.readline().replace("A", "1").replace("B", "2").strip("\n"))))

sSum = [0] * (len(S)+1)
tSum = [0] * (len(T)+1)
for i in range(len(S)):
    sSum[i+1] = sSum[i] + S[i]
for i in range(len(T)):
    tSum[i+1] = tSum[i] + T[i]

Ans = ""
Q = int(F.readline().strip("\n"))
for _ in range(Q):
    a, b, c, d = map(int, F.readline().strip("\n").split(" "))
    if (sSum[b] - sSum[a-1]) % 3 == (tSum[d] - tSum[c-1]) % 3: Ans += "YES\n"
    else: Ans += "NO\n"
print(Ans)

### FIXED CODE ###

import sys
F = sys.stdin
S = F.readline().replace("A", "1").replace("B", "2").strip("\n")
T = F.readline().replace("A", "1").replace("B", "2").strip("\n")

sSum = [0] * (len(S)+1)
tSum = [0] * (len(T)+1)
for i in range(len(S)):
    sSum[i+1] = sSum[i] + int(S[i])
for i in range(len(T)):
    tSum[i+1] = tSum[i] + int(T[i])

Ans = ""
Q = int(F.readline().strip("\n"))
for _ in range(Q):
    a, b, c, d = map(int, F.readline().strip("\n").split(" "))
    if (sSum[b] - sSum[a-1]) % 3 == (tSum[d] - tSum[c-1]) % 3: Ans += "YES\n"
    else: Ans += "NO\n"
print(Ans)
### BUGGY CODE ###

def main():
    N = int(input())
    A = [int(n) for n in input().split()]

    M = 10 ** 9 + 7

    sum = 0
    for i in range(N-1):
        for j in range(i+1, N):
            sum += A[i] * A[j] % M
    print((sum + M) % M)


main()


### FIXED CODE ###

def main():
    N = int(input())
    A = [int(n) for n in input().split()]

    M = 10 ** 9 + 7

    ans = 0
    cache = sum(A) % M
    for i in range(N-1):
        cache -= A[i]
        ans += A[i] * cache % M
    print(ans % M)


main()
### BUGGY CODE ###

#!/usr/bin/env python3
import sys

sys.setrecursionlimit(10**9)


def dfs(v, graph, memo):
    if v in memo:
        return memo[v]

    if graph[v]:
        res = max(dfs(child, graph, memo) for child in graph[v])
        memo[v] = res + 1
    else:
        res = 0
        memo[v] = 0

    return memo[v]


def solve(N: int, M: int, x: "List[int]", y: "List[int]"):
    graph = [[] for _ in range(N)]
    for i in range(M):
        graph[x[i]-1].append(y[i]-1)

    memo = {}

    print(max(dfs(i, graph, memo) for i in range(N)))


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    x = [int()] * (M)  # type: "List[int]"
    y = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        x[i] = int(next(tokens))
        y[i] = int(next(tokens))
    solve(N, M, x, y)


if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys

sys.setrecursionlimit(10**9)


def dfs(v, graph, memo):
    if v in memo:
        return memo[v]

    if graph[v]:
        res = float("-inf")
        for child in graph[v]:
            res = max(res, dfs(child, graph, memo) + 1)
        memo[v] = res
    else:
        res = 0
        memo[v] = 0

    return memo[v]


def solve(N: int, M: int, x: "List[int]", y: "List[int]"):
    graph = [[] for _ in range(N)]
    for i in range(M):
        graph[x[i]-1].append(y[i]-1)

    memo = {}

    res = float("-inf")
    for i in range(N):
        res = max(res, dfs(i, graph, memo))
    print(res)


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    x = [int()] * (M)  # type: "List[int]"
    y = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        x[i] = int(next(tokens))
        y[i] = int(next(tokens))
    solve(N, M, x, y)


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

N = int(input())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7

bit2 = [0] * 80
for i in range(N):
  bin_a = bin(A[i])
  #print(bin_a)#2なら0b10と返す
  str_bin_a = str(bin_a)[2:]
  #print(str_bin_a)
  l = len(str_bin_a)
  for j in range(l):
    bit2[j] += int(str_bin_a[-(j + 1)])
    
#print(bit2)
ans = 0
for i in range(80):
  count1 = bit2[i]
  count0 = N - count1
  ans += ((2 ** i) * count0 * count1) % MOD
  
print(ans % MOD)
  
  

### FIXED CODE ###

import numpy as np
n = int(input())
A = np.array(list(map(int, input().split())))
mod = 10 ** 9 + 7

ans = 0
for i in range(64):
    tmp = (A >> i) & 1
    #print(tmp)
    x = np.count_nonzero(tmp)
    x *= n - x
    ans += pow(2, i, mod) * x % mod

print(ans % mod)
### BUGGY CODE ###


def resolve():
    S = input()
    T = input()

    dp = [[0] * (len(T) + 1) for _ in range(len(S) + 1)]

    for i in range(len(S)):
        for j in range(len(T)):
            if S[i] == T[j]:
                dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1)
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j])
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1])

    ans = ""

    i, j = len(S), len(T)
    while i > 0 and j > 0:
        if dp[i][j] == dp[i - 1][j]:
            i -= 1
        elif dp[i][j] == dp[i][j - 1]:
            j -= 1
        else:
            ans = S[i - 1] + ans
            i -= 1
            j -= 1
    print(ans)


if __name__ == "__main__":
    resolve()


### FIXED CODE ###


def resolve():
    S = input()
    T = input()

    dp = [[0] * (len(T) + 1) for _ in range(len(S) + 1)]

    for i in range(len(S)):
        for j in range(len(T)):
            if S[i] == T[j]:
                dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1)
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j])
            dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1])

    ans = ""

    i, j = len(S), len(T)
    while i > 0 and j > 0:
        if dp[i][j] == dp[i - 1][j]:
            i -= 1
        elif dp[i][j] == dp[i][j - 1]:
            j -= 1
        else:
            ans = S[i - 1] + ans
            i -= 1
            j -= 1
    print(ans)


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

n , m = map(int, input().split())

root = [0]*n
for i in range(m):
    r = list(map(int, input().split()))
    if root[r[0]-1] == 0:
        root[r[0]-1] = [r]
    else:
        root[r[0]-1] = root[r[0]-1]+[r]
    if root[r[1]-1] == 0:
        root[r[1]-1] = [r]
    else:
        root[r[1]-1] = root[r[1]-1]+[r]

kakutei = [1]

answer_li = [0]*n

mitou = [-1]*n

def dest(r,k):
    if r[0] == k:
        return r[1]
    else:
        return r[0] 

while len(kakutei)>=1:
    k = kakutei[0]
    for r in root[k-1]:
        d = dest(r,k)
        if mitou[d-1] == -1:
            mitou[d-1] = 0
            root[d-1].remove(r)
            kakutei.append(d)
            answer_li[d-1] = k

    kakutei = kakutei[1:]

answer_li = answer_li[1:]
if 0 in answer_li:
    print('No')
else:
    print('Yes')
    for i in answer_li:
        print(i)


### FIXED CODE ###

n , m = map(int, input().split())

root = [[] for _ in range(n)]
for i in range(m):
    a, b = map(int, input().split())
    root[a-1].append(b)
    root[b-1].append(a)

kakutei = [1]

answer_li = [0]*n

while kakutei:
    k = kakutei.pop(0)
    for r in root[k-1]:
        if answer_li[r-1] == 0:
            kakutei.append(r)
            answer_li[r-1] = k

answer_li = answer_li[1:]
if 0 in answer_li:
    print('No')
else:
    print('Yes')
    for i in answer_li:
        print(i)
### BUGGY CODE ###

n=int(input())
dict={}
a=list(map(int,input().split()))
for i in a:
  if i not in dict:
    dict[i]=1
  else:
    dict[i]+=1
q=int(input())
for i in range(q):
  b,c=map(int,input().split())
  ans=0
  for j in dict:
    if j==b:
      ans+=c*dict[b]
    else:
      ans+=j*dict[j]
  if b in dict and c not in dict:
    dict[c]=dict[b]
    dict[b]=0
  elif b in dict and c in dict:
    dict[c]+=dict[b]
    dict[b]=0
  print(ans)

### FIXED CODE ###

import collections
n = int(input())
a = list(map(int, input().split()))
count = collections.Counter(a)
q = int(input())
s = sum(a)

for i in range(q):
  b, c = map(int, input().split())
  s = s - (b - c) * count[b]
  
  count[c] += count[b]
  count[b] = 0
  print(s)
### BUGGY CODE ###

#! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
N, K = map(int, input().split())
A = list(map(int, input().split()))
L = [0] * len(set(A))
tmp = 0
for i in set(A):
    L[tmp] = A.count(i)
    tmp += 1
L.sort()
if len(set(A)) <= K:
    print('0')
else:
    print(str(sum(L[0:len(set(A))-K])))


### FIXED CODE ###

#! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = set(A)
    L = [0 for i in range(N+1)]
    for i in A:
        L[i] += 1
    L.sort()
    if len(B) <= K:
        print('0')
    else:
        print(str(sum(L[0:N+1-K])))


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

S = input().strip()
Q = int(input().strip())
flag = 1
for i in range(Q):
  line = input().strip()
  if len(line) == 1:
    flag = 1 - flag
  else:
    _, F, C = line.strip().split()
    if flag:
      if int(F) == 1:
        S = C + S
      else:
        S = S + C
    else:
      if int(F) == 1:
        S = S + C
      else:
        S = C + S

if flag:
  print(S)
else:
  print(''.join([c for c in reversed(list(S))]))  
    

### FIXED CODE ###

from collections import deque
S = deque(list(input().strip()))
Q = int(input().strip())
flag = 1
for i in range(Q):
  line = input().strip()
  if len(line) == 1:
    flag = 1 - flag
  else:
    _, F, C = line.strip().split()
    if flag:
      if int(F) == 1:
        S.appendleft(C)
      else:
        S.append(C)
    else:
      if int(F) == 1:
        S.append(C)
      else:
        S.appendleft(C)

if flag:
  print(''.join(S))
else:
  print(''.join(list(reversed(S))))