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

N = int(input())
S = [input() for i in range(N) ]
counter = 0
for i in range(N):
    for j in range(i+1,N):
        if set(S[i]) == set(S[j]):
            if sorted(S[i]) == sorted(S[j]):
                counter += 1
print(counter)

### FIXED CODE ###

import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N = int(input())
dic_s = {}
for i in range(N):
    s = "".join(sorted(input()))
    if s in dic_s:
        dic_s[s] += 1
    else:
        dic_s[s] = 1
counter = 0
for key,value in dic_s.items():
    if value >1:
        counter += combinations_count(value,2)
print(counter)
### BUGGY CODE ###

k, n = map(int, input().split())
a = list(map(int, input().split()))
d = [a[0]]

for i in range(1, n):
    d.append(a[i] - a[i-1])
    
d.append(k - a[-1])
d = tuple(d)

most_min_path = 10000

for i in range(n):
    if i == 0:
        normal_ck = sum(d) - d[i] - d[-1]
        reverse_ck = sum(d) - d[i+1]
    elif i == n - 1:
        normal_ck = sum(d) - d[i]
        reverse_ck = sum(d) - d[i+1] - d[0]
    else:
        normal_ck = sum(d) - d[i]
        reverse_ck = sum(d) - d[i+1]
    if normal_ck > reverse_ck:
        min_ck = reverse_ck
    else:
        min_ck = normal_ck
    
    if most_min_path > min_ck:
        most_min_path = min_ck

print(most_min_path)

### FIXED CODE ###

k, n = map(int, input().split())
a = list(map(int, input().split()))
a.append(a[0] + k)

d = [a[i+1] - a[i] for i in range(n)]
l = 0
for i in range(n):
    l = max(l, a[i+1] - a[i])
print(k - l)
### BUGGY CODE ###

import re

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

ans = []
for li in l:
    ps = S[li[0]-1:li[1]]
    acs = re.findall('AC', ps)
    print(len(acs))

### FIXED CODE ###

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

pos = [0] * N
i = 0
while i < N-1:
    if S[i] == 'A':
        if S[i+1] == 'C':
            pos[i+1] = 1
    i += 1

nums = [0] * (N+1)
for i in range(1, N+1):
    nums[i] += nums[i-1] + pos[i-1]

for _ in range(Q):
    l, r = map(int, input().split())
    print(nums[r]-nums[l])
### BUGGY CODE ###

import itertools,math,bisect,pprint,sys
n, a, b = map(int, raw_input().split())
v = sorted(map(int, raw_input().split()))[::-1]
res1 = 0
for i in range(a,b+1):
    s = sum(v[:i])*1.0/i
    if s > res1:
        res1 = s
print res1
res2 = 0
for i in range(a,b+1):
    for j in itertools.combinations(v,i):
        if sum(j)==res1*i:
            res2 +=1
print res2

### FIXED CODE ###

import itertools,math,bisect,pprint,sys
n, a, b = map(int, raw_input().split())
v = sorted(map(int, raw_input().split()))[::-1]
res1 = sum(v[:a])*1.0/a
print '{0:.6f}'.format(res1)
res2 = 0
def nCr(n,r):
    f = math.factorial
    return f(n) / f(r) / f(n-r)
if a==v[:a].count(v[a-1]):
    res2 = 0
    for i in range(a,b+1):
        if i <= v.count(v[a-1]):
            res2 += nCr(v.count(v[a-1]),i)
    print res2
else:
    print nCr(v.count(v[a-1]),v[:a].count(v[a-1]))
### BUGGY CODE ###

def band(i, j, x, y):
  res = 0
  res = [j-i, abs(x-i) + 1 + abs(j-y), abs(y-i) + 1 + abs(j-x)]
  return min(res)

d = {}
n, x, y = list(map(int,input().split()))
for i in range(1, n+1):
  for j in range(1, n+1):
    if i < j:
      #print(str(i)+','+str(j)+'  '+str(band(i, j, x, y)))
      if band(i, j, x, y) in d:
        d[band(i, j, x, y)] += 1
      else:
        d[band(i, j, x, y)] = 1
for i in range(1,n):
  if i in d:
    print(d[i])
  else:
    print('0')

### FIXED CODE ###

n, x, y = list(map(int,input().split()))
d = [0 for i in range(n-1)] 
for i in range(1,n+1):
  for j in range(i+1,n+1):
    d[min([j-i, abs(x-i) + 1 + abs(j-y), abs(y-i) + 1 + abs(j-x)])-1] += 1

for i in d:
  print(i)
### BUGGY CODE ###

n, q = map(int, input().split())
queue = []
for _ in range(n):
    name, time = input().split()
    queue.append((name, int(time)))

spend_time = 0
while len(queue) > 0:
    name, time = queue[0]
    tmp = min(q, time)
    spend_time += tmp
    time -= tmp
    if time == 0:
        print('{} {}'.format(name, spend_time))
        queue = queue[1:]
    else:
        queue = queue[1:] + [(name, time)]



### FIXED CODE ###

import queue

n, quantum = map(int, input().split())
q = queue.Queue()
for _ in range(n):
    name, time = input().split()
    q.put([name, int(time)])

spend_time = 0
while q.qsize() > 0:
    name, time = q.get()
    tmp = min(quantum, time)
    spend_time += tmp
    time -= tmp
    if time == 0:
        print('{} {}'.format(name, spend_time))
    else:
        q.put([name, time])
### BUGGY CODE ###

def dijkstra(s, n, w, cost):
    d = [float('inf')] * n
    used = [False] * n
    d[s] = 0
    x = [-1] * n
    
    while True:
        v = -1
        for i in range(n):
            if (not used[i]) and (v == -1):
                v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
            break
        used[v] = True
               
        for j in range(n):
            if d[j] > d[v]+cost[v][j]:
                d[j] = d[v]+cost[v][j]
                x[j] = v
    return d, x

n, m, *AB = map(int, open(0).read().split())
cost = [[10**9]*n for _ in range(n)] 
for a, b in zip(AB[::2], AB[1::2]):
    cost[a-1][b-1] = cost[b-1][a-1] = 1
d, p = dijkstra(0, n, m, cost)
if any(x > 10**9-1 for x in d):
    print('No')
else:
    print('Yes')
    print(*map(lambda x: int(x)+1, p[1:]), sep='\n')

### FIXED CODE ###

from collections import deque
n, m, *AB = map(int, open(0).read().split())
graph = [[] for _ in range(n)]
for a, b in zip(AB[::2], AB[1::2]):
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

d = [0] * n
queue = deque([0])
while queue:
    current_node = queue.popleft()
    for next_node in graph[current_node]:
        if d[next_node] == 0:
            d[next_node] = current_node+1
            queue.append(next_node)
print('Yes')
print(*d[1:], sep='\n')
### BUGGY CODE ###

import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N = int(input())
        S = input().rstrip()

        l = N // 2
        for i in range(1, l + 1)[::-1]:
            j = 0
            while j + i < N:
                if S.count(S[j:j + i]) >= 2:
                    print(i)
                    exit()
                else:
                    j += 1
        print(0)


# Run main
if __name__ == '__main__':
    AtCoder().main()

### FIXED CODE ###

import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N = int(input())
        S = input().rstrip()

        dp = [[0 for i in range(N + 1)] for j in range(N + 1)]
        ans = 0
        for i in range(N)[::-1]:
            for j in range(N)[::-1]:
                if S[i] == S[j]:
                    dp[i][j] = min(dp[i + 1][j + 1] + 1, abs(i - j))
                    ans = max(ans, dp[i][j])
        print(ans)


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

N = int(input())
l = sorted([int(item) for item in input().split(' ')])
count = 0

def binary_search(count, a, b, l):
    if len(l) == 0:
        return count
    center = len(l) // 2
    if a + b > l[center]:
        count += center + 1
        return binary_search(count, a, b, l[center+1:])
    else:
        return binary_search(count, a, b, l[:center])


for i in range(N):
    for j in range(i + 1, N):
        l_remained = l[j + 1:]
        count = binary_search(count, l[i], l[j], l_remained)

print(count)


### FIXED CODE ###

import bisect
n = int(input())
l = sorted(map(int, input().split()))

ans = 0
for i in range(n):
    for j in range(i+1, n):
        end = bisect.bisect_left(l, l[i]+l[j])
        ans += end - j - 1
print(ans)
### BUGGY CODE ###

(L, N) = map(int, input().split())
X = [0 for i in range(N)]
for i in range(N):
    X[i] = int(input())


ans = 0
if N == 1:
    ans = max(X[0], L-X[0])
else:
    X += X
    #print('X: ', X)
    dmax = 0
    for i in range(1, N):
        seg = X[i:i+N]
        #print('seg: ', seg)
        for s in (0, N-1):
            os = (N-1)-s
            cl = N-1    # counterclockwise most
            cr = 0      # clockwise most
            d = 0
            lflg = False
            if s == cl:
                lflg = True
            while True:
                #print('tmpd: ', d)
                # l to l
                if lflg and seg[s] >= seg[os]:
                    d += seg[s]
                    break
                # r to r
                elif not(lflg) and seg[s] <= seg[os]:
                    d += L-seg[s]
                    break
                
                # l to r
                if lflg:
                    d += seg[s]+(L-seg[os])
                    # update segment
                    cl = s-1 if s-1>=0 else N-1 # move to r by 1
                    cr = os
                    # next
                    s = cr
                    os = cl
                # r to l
                else:
                    d += seg[os]+(L-seg[s])
                    # update segment
                    cl = os
                    cr = s+1 if s+1 < N else 0  # move to l by 1
                    # next
                    s = cl
                    os = cr
                lflg = not(lflg)
            #print('d: ', d)
            if d > dmax:
                dmax = d
    ans = dmax

print(ans)


### FIXED CODE ###

(L, N) = map(int, input().split())
X = [0 for i in range(N)]
sumld = [0 for i in range(N)]
sumrd = [0 for i in range(N)]
for i in range(N):
    X[i] = int(input())
    sumld[i] = X[i]*2 if i == 0 else sumld[i-1] + X[i]*2
for i in range(N):
    sumrd[N-1-i] = (L-X[N-1-i])*2 if i == 0 else sumrd[N-1-i+1] + (L-X[N-1-i])*2
#print('X: ', X)
#print('sumld: ', sumld)
#print('sumrd: ', sumrd)

ans = 0
if N == 1:
    ans = max(X[0], L-X[0])
else:
    dmax = 0
    for i in range(1, N):
        l = (N-1+i)%N
        r = i
        lnum = l+1
        rnum = N-lnum
        #print('l: ', l)
        #print('r: ', r)
        #print('lnum: ', lnum)
        #print('rnum: ', rnum)

        # start from l
        d = 0
        # end at l
        if lnum > rnum:
            d = sumld[l] + sumrd[r] - X[l]
            if lnum-rnum > 1:
                d -= sumld[l-rnum-1]
        # end at r
        else:
            d = sumld[l] + sumrd[r] - X[l]
            if rnum-lnum > 0:
                d -= sumrd[r+lnum]
        #print('d from l: ', d)
        if d > dmax:
            dmax = d

        # start from r
        d = 0
        # end at l
        if lnum >= rnum:
            d = sumld[l] + sumrd[r] - (L-X[r])
            if lnum-rnum > 0:
                d -= sumld[l-rnum]
        # end at r
        else:
            d = sumld[l] + sumrd[r] - (L-X[r])
            if rnum-lnum > 1:
                d -= sumrd[r+lnum+1]
        #print('d from r: ', d)
        if d > dmax:
            dmax = d
    ans = dmax

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

n = int(input())
a = list(map(int,input().split()))
b = []
for i in range(n):
    if i % 2 == 0:
        b.append(a[i])
    else:
        b = [a[i]] + b
if n % 2 == 1:
    b.reverse()
print(' '.join(map(str, b)))


### FIXED CODE ###

from collections import deque

n = int(input())
a = list(map(int,input().split()))
b = deque()
for i in range(n):
    if i % 2 == 0:
        b.append(a[i])
    else:
        b.appendleft(a[i])
if n % 2 == 1:
    b.reverse()
print(' '.join(map(str, b)))
### BUGGY CODE ###

import numpy as np
from collections import deque
N = input()
A = []
for i in range(N):
	a = input()
	A.append(a)

A = sorted(A)
res = []
begin = 0
end = len(A) - 1
res = deque()
res.append(A[begin])
res.append(A[end])
begin+= 1
end -= 1
for i in range(N - 2):
	cand = np.argmax([abs(res[0] - A[begin]), abs(res[0] - A[end]), abs(res[-1] - A[begin]), abs(res[-1] - A[end])])
	if cand == 0:
		res.appendleft(A[begin])
		begin += 1
	elif cand == 1:
		res.appendleft(A[end])
		end -= 1
	elif cand == 2:
		res.append(A[begin])
		begin += 1
	elif cand == 3:
		res.append(A[end])
		end -= 1
sum_diff = 0
for i in range(N - 1):
	 sum_diff += abs(res[i] - res[i + 1])
print sum_diff


### FIXED CODE ###

from collections import deque
N = input()
A = [int(input()) for i in range(N)]
coef = [2] * N
coef[0] = 1
coef[-1] = 1
coef1 = [coef[i] if i % 2 == 0 else coef[i] * -1 for i in range(N)]
coef2 = [coef[i] if i % 2 != 0 else coef[i] * -1 for i in range(N)]

A = sorted(A)
coef1 = sorted(coef1)
coef2 = sorted(coef2)
res1 = 0
for i in range(N):
	res1 += A[i] * coef1[i]
res2 = 0
for i in range(N):
	res2 += A[i] * coef2[i]
print max(res1, res2)
### BUGGY CODE ###

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

part_of_sum=[]
for i in range(n-1):
  part_of_sum.append(sum(li[i+1:]))

sum=0
for j in range(n-1):
  sum+=li[j]*part_of_sum[j]

print(sum%(10**9+7))

### FIXED CODE ###

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

part_of_sum=[]
s=0
mini_sum=sum(li[1:])
for j in range(n-1):
  s+=li[j]*mini_sum
  mini_sum-=li[j+1]

print(s%(10**9+7))
### BUGGY CODE ###

N, Q = map(int, input().split())
S = input()
lr = [list(map(int, input().split())) for _ in range(Q)]
for i in range(Q):
    print(S[lr[i][0]-1:lr[i][1]].count('AC'))

### FIXED CODE ###

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

cnt = [0] * (N + 1)
for i in range(N):
    if S[i:i+2] == 'AC':
        cnt[i+1] = cnt[i] + 1 
    else:
        cnt[i+1] = cnt[i]

for i in range(Q):
    print(cnt[lr[i][1]-1]-cnt[lr[i][0]-1])
### BUGGY CODE ###

def rabin_karp(T, P, d):
    n = len(T)
    m = len(P)
    if n < m:
        return
    h = 1
    p = 0
    t0 = 0
    for _ in range(m - 1):
        h *= d
    for i in range(m):
        p = d * p + P[i]
        t0 = d * t0 + T[i]
    # print("h = ", h)
    # print("p = ", p)
    for s in range(n - m + 1):
        # print("t0 = ", t0)
        if p == t0:
            print(s)
        if s < n - m:
            # print("     ", t0 - T[s] * h)
            # print("     ",  d)
            # print("     ", T[s + m])
            t0 = d * (t0 - T[s] * h) + T[s + m]


d = 1009
t = list(map(ord, input()))
p = list(map(ord, input()))

rabin_karp(t, p, d)


### FIXED CODE ###

def compute_prefix_function(p):
    m = len(p)
    pi = [0 for _ in range(m + 1)]
    pi[1] = 0
    k = 0
    for q in range(2, m + 1):
        while k > 0 and p[k] != p[q - 1]:
            k = pi[k]
        if p[k] == p[q - 1]:
            k += 1
        pi[q] = k
    return pi


def kmp_matcher(t, p):
    n = len(t)
    m = len(p)
    pi = compute_prefix_function(p)
    q = 0
    for i in range(1, n + 1):
        while q > 0 and p[q] != t[i - 1]:
            q = pi[q]
        if p[q] == t[i - 1]:
            q += 1
        if q == m:
            print(i - m)
            q = pi[q]


t = input()
p = input()
kmp_matcher(t, p)
### BUGGY CODE ###

A, B = map(int, input().split())
i = 1
while True:
    j, k = divmod(i, A)
    l, m = divmod(i, B)
    if not k and not m:
        break
    i += 1
print(i)

### FIXED CODE ###

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

import fractions
gcd = fractions.gcd(A, B)
print(A * B // gcd)
### BUGGY CODE ###

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

import collections

mod=2019
a=input()

N=len(a)
t=[0]*mod
t[0]=1
ans=0
v=0
w=1

for i in range(N):
  u=int(a[N-1-i]) * w + v
  v=u % mod
  w *=10 % mod
  t[v] += 1
  
#tt = collections.Counter(t)

for i in t:
  ans += (i-1)*i//2

print(ans)

### FIXED CODE ###

mod=2019
a=input()

N=len(a)
t=[0]*mod
t[0]=1

w=1
u=0

for i in range(N):
  u=int(a[N-1-i]) * w + u
  v=u % mod
  t[v] += 1
  w *=10
  w %= mod

ans=0
for i in t:
  ans += (i-1)*i//2

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

ans = 0


def solve():

    N = int(input())
    S = input()

    def dfs(a, b):
        global ans

        if b == -1:
            for i in range(a, N-1):
                if S[a] != S[i]:
                    dfs(a, i)
        else:
            for i in range(b, N):
                if S[b] != S[i] and S[a] != S[i]:
                    if i - b != b - a:
                        ans += 1

    for i in range(0, N-2):
        dfs(i, -1)

    print(ans)


if __name__ == "__main__":
    solve()

    

### FIXED CODE ###



def solve():

    N = int(input())
    S = input()

    r = S.count('R')
    g = S.count('G')
    b = S.count('B')

    ans = r*g*b

    for i in range(N-2):
        for j in range(i+1, N-1):
            # j - i = k - j
            k = j-i+j
            if k >= N:
                continue
            if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                ans -= 1

    print(ans)


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

from collections import deque

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

l = deque([1])

while True:
    check = True
    for nex in e[l[-1]]:
        if nex in l:
            continue
        l.append(nex)
        check = False
        break
    if check:
        break
while True:
    check = True
    for nex in e[l[0]]:
        if nex in l:
            continue
        l.appendleft(nex)
        check = False
        break
    if check:
        print(len(l))
        print(*l)
        exit()

### FIXED CODE ###

from collections import deque

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

l = deque([1])
s = set([1])
while True:
    check = True
    for nex in e[l[-1]]:
        if nex in s:
            continue
        l.append(nex)
        s.add(nex)
        check = False
        break
    if check:
        break
while True:
    check = True
    for nex in e[l[0]]:
        if nex in s:
            continue
        l.appendleft(nex)
        s.add(nex)
        check = False
        break
    if check:
        print(len(l))
        print(*l)
        exit()
### BUGGY CODE ###

H, W = map(int,input().split())

table = []
for _ in range(H):
    table.append(["k"] + list(input()) + ["k"])
    
table = [["k"]*(W+2)] + table + [["k"]*(W+2)]

import queue
q = queue.Queue()

min_memo = [[None]*(W+2) for _ in range(H+2)]

all_cnt = 0
for y, r in enumerate(table):
    for x, c in enumerate(r):
        if c == "#":
            q.put([y,x,0])
            min_memo[y][x] = 0
            all_cnt += 1

if all_cnt == H*W:
    print(0)
else:          
    while True:
        y, x, cnt = q.get()
        for dy,dx in [(-1,0),(1,0),(0,-1),(0,1)]:
            if table[y+dy][x+dx] == ".":
                if min_memo[y+dy][x+dx] == None:
                    min_memo[y+dy][x+dx] = cnt+1
                    q.put([y+dy, x+dx, cnt+1])
                    all_cnt += 1
        if all_cnt >= H*W:
            print(cnt+1)
            break


### FIXED CODE ###

from collections import deque

H, W = map(int,input().split())

table = []
for _ in range(H):
    table.append(["k"] + list(input()) + ["k"])
    
table = [["k"]*(W+2)] + table + [["k"]*(W+2)]

q = deque()

min_memo = [[None]*(W+2) for _ in range(H+2)]

all_cnt = 0
for y, r in enumerate(table):
    for x, c in enumerate(r):
        if c == "#":
            q.append([y,x,0])
            min_memo[y][x] = 0
            all_cnt += 1

if all_cnt == H*W:
    print(0)
else:          
    while True:
        y, x, cnt = q.popleft()
        for dy,dx in [(-1,0),(1,0),(0,-1),(0,1)]:
            if table[y+dy][x+dx] == ".":
                if min_memo[y+dy][x+dx] == None:
                    min_memo[y+dy][x+dx] = cnt+1
                    q.append([y+dy, x+dx, cnt+1])
                    all_cnt += 1
        if all_cnt >= H*W:
            print(cnt+1)
            break
### BUGGY CODE ###

n = input()
count = 0
for i in xrange(n):
    x = input()
    for j in xrange(2,x):
        if x % j == 0:
            break
    else:
        count += 1
print count

### FIXED CODE ###

import math
def prime(x):
    if x == 2: return True
    elif x % 2 == 0: return False
    else:
        for j in xrange(3,int(math.sqrt(x))+1,2):
            if x % j == 0: return False
        else:
            return True 
n = input()
count = 0
for i in xrange(n):
    if prime(input()): count += 1
print count
### BUGGY CODE ###

import sys
input = sys.stdin.readline
import numpy as np

n, k = map(int, input().split())
AA =[]
c =False
for a in input().strip().split():
    a = int(a)
    if not c:
        AA.append(a)
        if len(AA) == k:
            c = True
    else:
        base = AA[0]
        next = a
        if next - base > 0:
            print("Yes")
        else:
            print("No")
        AA = AA[1:]
        AA.append(a)


### FIXED CODE ###

n, k = map(int, input().split())
AA =[int(a) for a in input().split()]


for b, t in zip(AA, AA[k:]):
    if t > b:
        print("Yes")
    else:
        print("No")
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**9)
input=lambda :sys.stdin.readline()

def sol():

  N,Q = map(int,input().split())
  graph = [[] for i in range(N)]
  ans = [0 for i in range(N)]
  for _ in range(N-1):
    a,b = map(int,input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

  for _ in range(Q):
    p, x = map(int,input().split())
    ans[p-1] += x 
 
  def dfs(v,p=-1):
    for u in graph[v]:
      if u == p: continue
      ans[u] += ans[v]
      dfs(u,v)
  dfs(0)
  print(*ans)

sol()

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**9)
input=lambda :sys.stdin.readline()

def sol():

  N,Q = map(int,input().split())
  graph = [[] for i in range(N)]
  ans = [0 for i in range(N)]
  for _ in range(N-1):
    a,b = map(int,input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

  for _ in range(Q):
    p, x = map(int,input().split())
    ans[p-1] += x 
 
  def dfs(v,p=-1):
    for u in graph[v]:
      if u == p: continue
      ans[u] += ans[v]
      dfs(u,v)
  dfs(0)
  print(*ans)

sol()
### BUGGY CODE ###

s=str(input())

q=int(input())
for i in range(q):
    a=input().strip()
    a=list(a)
    #print(a)
    if int(a[0])==1:
        #print("DFGHJ")
        s=s[::-1]
    if int(a[0])==2:
        #print(a[2])
        s=list(s)
        if int(a[2])==1:
            s.insert(0,str(a[4]))
        else:
            s.append(str(a[4]))
        s=''.join(s)
        #print(s)        
print(s)


### FIXED CODE ###

from collections import deque
s=str(input())

front = ""
back = ""#前と後ろに分ける最後につなげてリバース

now_swap = 0
q=int(input())
for i in range(q):
    a = input().strip()
    a = list(a)
    
    if a[0] == "1":
        if now_swap == 0:
           now_swap = 1
        else: #now_step == 1:
            now_swap = 0
    else:
        if now_swap == 1:
            if a[2] == "1":
               # print("back")
                back+=a[4]

            else: #a[1]==2:
               ## print("f 2")
                front = a[4] + front

        else: #now_step == 0:
            if a[2] == "1":
                front = a[4] + front

            else: #a[1]==2:
                #print("hj")
                back+=a[4]
ans= front + s + back
#print(front,a,back)
if now_swap == 1:#合計数が奇数なら
    
    print(ans[::-1])
else:
    print(ans)
### BUGGY CODE ###

a, b = (int(x) for x in input().split())
m = max(a, b)
for k in range(m):
    if abs(a-k) == abs(b-k):
        break
if abs(a-k) == abs(b-k):
    print(k)
else:
    print("IMPOSSIBLE")

### FIXED CODE ###

a, b = (int(x) for x in input().split())
ans = int(a/2+b/2+0.5)
if abs(a-ans) == abs(b-ans):
    print(ans)
else:
    print("IMPOSSIBLE")