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

n = int(input())
x_l = list(map(int,input().split()))
s_x_l = [ (i, j) for i,j in enumerate(x_l) ]
s_x_l = sorted(s_x_l, key=lambda x: x[1])
size = len(s_x_l)
med_idx = int((size-1+1)/2)-1

ans = []
for i in range(n):
    left = s_x_l[:i]
    right = s_x_l[i+1:]
    idx = s_x_l[i][0]
    merge = left+right
    med = merge[med_idx][1]
    ans.append([idx, med])

ans = sorted(ans, key=lambda x:x[0])
for i in ans:
    print(i[1])

### FIXED CODE ###

N = int(input())
X = list(map(int, input().split()))
idx = range(N)
Y = [ [ idx[i], X[i]] for i in range(N) ]
Z = sorted(Y , key=lambda x: x[1])
ans = []
for i,j in enumerate(Z):
    idx_tmp = Z[i][0]
    if i < int(N/2):
        median = Z[int(N/2)][1]
    else:
        median = Z[int(N/2)-1][1]
    ans.append([idx_tmp,median])
ans = sorted(ans, key=lambda x:x[0])
for i in ans:
    print(i[1])
### BUGGY CODE ###

n = int(input())
array = list(map(int, input().strip().split(" ")))
ans = [0]*n

for i in range(1,n+1):
    ans[i-1] = str(array.index(i) + 1)



print(" ".join(ans))


### FIXED CODE ###

n = int(input())
array = list(map(int, input().strip().split(" ")))
ans = [0]*n

for i in range(n):
    ans[array[i] - 1] = str(i + 1 )



print(" ".join(ans))
### BUGGY CODE ###

N = int(input())
an = list(map(int, input().split()))
ans = 1
for a in an:
    if 0 in an:
        ans = 0
        break
    ans *= a
    if ans > 10**18:
        ans = -1
        break
print(ans)


### FIXED CODE ###

N = int(input())
an = list(map(int, input().split()))
ans = 1
if 0 in an:
    ans = 0
else:
    for a in an:
        ans *= a
        if ans > 10**18:
            ans = -1
            break
print(ans)
### BUGGY CODE ###

s = input()
x,y = map(int,input().split())
count = 0
d = []
s = s.split('T')
d = [len(x) for x in s]

if len(d) == 0:
    d.append(0)
if len(d) == 1:
    d.append(0)
dpx = [d[0]]
dpy = [d[1],-d[1]]
for i in range(len(d)-2):
    if i%2==0:
        trans = set()
        for xj in dpx:
            trans.add(xj+d[i+2])
            trans.add(xj-d[i+2])
        dpx = list(trans)
    else:
        trans = set()
        for yj in dpy:
            trans.add(yj+d[i+2])
            trans.add(yj-d[i+2])
        dpy = list(trans)
ans = (x in dpx)&(y in dpy)

if ans:
    print('Yes')
else:
    print('No')

### FIXED CODE ###

s = input()
x,y = map(int,input().split())
count = 0
d = []
s = s.split('T')
d = [len(x) for x in s]

if len(d) == 0:
    d.append(0)
if len(d) == 1:
    d.append(0)
dpx = [d[0]]
dpy = [d[1],-d[1]]
for i in range(2,len(d)):
    di = d[i]
    trans = set()
    if i%2==0:
        for xj in dpx:
            trans.add(xj+di)
            trans.add(xj-di)
        dpx = list(trans)
    else:
        for yj in dpy:
            trans.add(yj+di)
            trans.add(yj-di)
        dpy = list(trans)
ans = (x in dpx)&(y in dpy)

if ans:
    print('Yes')
else:
    print('No')
### BUGGY CODE ###

n, k, l = [int(x) for x in raw_input().split()]
d = [None for i in range(n)]
t = [None for i in range(n)]
for i in range(k):
    p, q = [int(x) - 1 for x in raw_input().split()]
    j = p
    while True:
        if d[j] is None:
            break
        j = d[j]
    j2 = q
    while True:
        if d[j2] is None:
            break
        j2 = d[j2]
    if j == j2:
        continue
    d[j2] = j
for i in range(l):
    r, s = [int(x) - 1 for x in raw_input().split()]
    j = r
    while True:
        if t[j] is None:
            break
        j = t[j]
    j2 = s
    while True:
        if t[j2] is None:
            break
        j2 = t[j2]
    if j == j2:
        continue
    t[j2] = j
d2 = dict((i, []) for i, v in enumerate(d) if v is None)
t2 = dict((i, []) for i, v in enumerate(t) if v is None)
for i, v in enumerate(d):
    if v is None:
        v2 = i
    else:
        v2 = v
        while True:
            if d[v2] is None:
                break
            v2 = d[v2]
        d[i] = v2
    d2[v2].append(i)
for i, v in enumerate(t):
    if v is None:
        v2 = i
    else:
        v2 = v
        while True:
            if t[v2] is None:
                break
            v2 = t[v2]
        t[i] = v2
    t2[v2].append(i)
for i in range(n):
    jd = d[i]
    if jd is None:
        jd = i
    jt = t[i]
    if jt is None:
        jt = i
    d2[jd]
    t2[jt]
    print len(set(d2[jd]).intersection(t2[jt])),


### FIXED CODE ###

#http://www.geocities.jp/m_hiroi/light/pyalgo61.html
class UF(object):
    def __init__(self, n):
        self.table = [None for i in range(n)]
    def find(self, x):
        if self.table[x] is None:
            return x
        else:
            self.table[x] = self.find(self.table[x])
            return self.table[x]
    def union(self, x, y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 != s2:
            self.table[s2] = s1

n, k, l = [int(x) for x in raw_input().split()]
d = UF(n)
t = UF(n)
for i in range(k):
    p, q = [int(x) - 1 for x in raw_input().split()]
    d.union(p, q)
for i in range(l):
    r, s = [int(x) - 1 for x in raw_input().split()]
    t.union(r, s)
res = {}
for i in range(n):
    key = (d.find(i), t.find(i))
    res[key] = res.get(key, 0) + 1
for i in range(n):
    print res[(d.find(i), t.find(i))],
### BUGGY CODE ###

n=int(input())
A=list(map(int,input().split( )))
B=list(map(int,input().split( )))
count=0
prv_count=-1#人の見て追加

#flag=True
while count!=prv_count:
    prv_count=count
    for i in range(-1,n-1):
        tonari=B[i-1]+B[i+1]
        tmp=(B[i]-A[i])//tonari
        if tmp>0:
            B[i]-=tonari*tmp
            count+=tmp
            #flag=True
if A==B:
    print(count)
else:
    print(-1)

### FIXED CODE ###

n=int(input())
A=list(map(int,input().split( )))
B=list(map(int,input().split( )))
count=0
prv_count=-1#人の見て追加

#flag=True
while count!=prv_count:
    prv_count=count
    for i in range(-1,n-1):
        tonari=B[i-1]+B[i+1]
        tmp=(B[i]-A[i])//tonari
        if tmp>0:
            B[i]-=tonari*tmp
            count+=tmp
            #flag=True
if A==B:
    print(count)
else:
    print(-1)
### BUGGY CODE ###

n = int(input())


def devide2(x):
    ar = []
    for i in range(1, int(x)):
        if x % i == 0:
            ar.append([i, x // i])
    return ar


kumi = devide2(n)

ans = 10 ** 12 + 1000
for i in range(len(kumi)):
    ans = min(ans, sum(kumi[i]))

print(ans - 2)


### FIXED CODE ###

n = int(input())


def devide2(x):
    ar = []
    ar.append([1, x])
    for i in range(2, int(-(-x ** 0.5 // 1)) + 1):
        if x % i == 0:
            ar.append([i, x // i])
    return ar


kumi = devide2(n)
# print(kumi)

ans = 10 ** 12 + 1000
for i in range(len(kumi)):
    ans = min(ans, sum(kumi[i]))

print(ans - 2)
### BUGGY CODE ###

import math
n=int(input())
z=0
ans=0
for N in range(1,n+1):
    ans=0
    z=0
    for x in range(1,100):
        for y in range(1,100):
            if x**2+y**2<=N:
                if (x+y)**2+4*N-4*(x**2+y**2)-4*x*y>=0:
                    z=((-x-y)+math.sqrt((x+y)**2+4*N-4*(x**2+y**2)-4*x*y))/2
                    #print(x,y,z)
                    if z>=1 and z%1==0:
                        ans+=1
                        #print(x,y,z)
    print(ans)

### FIXED CODE ###

n=int(input())
z=0
N=[0]*10000
ans=0
ko=0
for x in range(1,101):
    for y in range(1,101):
        for z in range(1,101):
            ko=x**2+y**2+z**2+x*y+y*z+z*x
            if ko<=n:
                N[ko-1]+=1
            else:
                break
for i in range(n):
    print(N[i])
### BUGGY CODE ###

def ints():
  return [int(x) for x in input().split()]
def readi():
  return int(input())

N = readi()
A = ints()

s = 0
for i in range(N-1):
  for j in range(i+1, N):
    s += A[i] ^ A[j]
print(s%(10**9+7))


### FIXED CODE ###

def ints():
  return [int(x) for x in input().split()]
def readi():
  return int(input())

N = readi()
A = ints()

w = 1000000007

s = 0
for k in range(60):
  n = [0, 0]
  for i in range(N):
    n[A[i]%2] += 1
    A[i] >>= 1

  s += (n[0]*n[1] << k) % w

print(s%w)
### BUGGY CODE ###

n,m=map(int, input().split())
l={}
for i in range(n):
    a,b=map(int, input().split())
    if not a in l:
        l[a]=[b]
    else:
        l[a].append(b)

res=0
target=[0]
for i in range(1,m+1):
    if i in l:
        target.extend(l[i])
    val=max(target)
    if val != 0:
        target.remove(val)
    res+=val

print(res)

### FIXED CODE ###

import bisect
import array

n,m=map(int, input().split())
l={}
for i in range(n):
    a,b=map(int, input().split())
    if not a in l:
        l[a]=[b]
    else:
        l[a].append(b)

res=0
target=array.array('I', [])
for i in range(1,m+1):
    if i in l:
        for j in l[i]:
            bisect.insort_left(target, j)

    if not target:
        val=0
    else:
        val=target.pop()
    res+=val
    #print(target, max(target))

print(res)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7


from functools import lru_cache


def main(): 
    S = SI()

    # 桁DP
    dp_table = [[0 for _ in range(13)] for _ in range(len(S))]

    # 最初の桁を見る。
    if S[0] == '?':
        for i in range(10):
            dp_table[0][i] = 1
    else:
        dp_table[0][int(S[0])] = 1

    # 以降の桁を見ていく。
    for digit in range(1, len(S)):
        if S[digit] == '?':
            for prev_res, comb in enumerate(dp_table[digit-1]):
                for x in range(10):
                    res = (prev_res*10+x)%13
                    dp_table[digit][res] = (dp_table[digit][res] + comb)%MOD

        else:
            for prev_res, comb in enumerate(dp_table[digit-1]):
                res = (prev_res*10+int(S[digit]))%13
                dp_table[digit][res] = (dp_table[digit][res] + comb)%MOD

    print(dp_table[len(S)-1][5])





main()

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

debug = True
debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)

def solve():
    S = SI()
    N = len(S)
    S = reversed(S)
    dp = [[0 for j in range(13)] for i in range(N)]
 
    for i, es in enumerate(S):
        c = es
        if i == 0:
            if c == '?':
                for j in range(10):
                    dp[i][j] = 1
            else:
                dp[i][int(c)] = 1
        else:
            if c == '?':
                for j in range(10):
                    amari = (j * pow(10, i, 13)) % 13
                    for k in range(13):
                        dp[i][k] = (dp[i][k] + dp[i-1][(k-amari)%13]) % MOD
            else:
                amari = (int(c) * pow(10, i, 13)) % 13
                for k in range(13):
                    dp[i][k] += (dp[i][k] + dp[i - 1][(k-amari) % 13]) % MOD
 
    dprint(dp)
    print(dp[N-1][5])


solve()
### BUGGY CODE ###

import sys
from collections import deque
from operator import itemgetter

def read():
    return sys.stdin.readline().rstrip()

def main():
    n = int(read())
    al = [[] for _ in range(n)]
    e = dict()
    for i in range(n - 1):
        a, b = [int(j) - 1 for j in read().split()]
        al[a].append(b)
        al[b].append(a)
        e[(a, b)] = i
    v, k = 0, 0
    for i in range(n):
        if len(al[i]) > k:
            v = i
            k = len(al[i])
    print(k)
    sn = deque([v])
    sd = set()
    cv = [set() for _ in range(n)]
    ce = dict()
    while sn:
        v = sn.popleft()
        sd.add(v)
        for vn in al[v]:
            if vn in sd:
                continue
            for ci in range(1, k + 1):
                if ci not in cv[v] and ci not in cv[vn]:
                    cv[v].add(ci)
                    cv[vn].add(ci)
                    if (v, vn) in e:
                        ce[(v, vn)] = ci
                    else:
                        ce[(vn, v)] = ci
                    break
            sn.append(vn)
    for k, _ in sorted(e.items(), key=itemgetter(1)):
        print(ce[k])

if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
from collections import deque
from operator import itemgetter

def read():
    return sys.stdin.readline().rstrip()

def main():
    n = int(read())
    al = [[] for _ in range(n)]
    e = dict()
    for i in range(n - 1):
        a, b = [int(j) - 1 for j in read().split()]
        al[a].append(b)
        al[b].append(a)
        e[(a, b)] = i
    v, k = 0, 0
    for i in range(n):
        if len(al[i]) > k:
            v = i
            k = len(al[i])
    print(k)
    sn = deque([(v, 0)])
    sd = set()
    ce = dict()
    while sn:
        v, cv = sn.popleft()
        sd.add(v)
        ci = 1
        for vn in al[v]:
            if vn in sd:
                continue
            if ci == cv:
                ci += 1
            if (v, vn) in e:
                ce[(v, vn)] = ci
            else:
                ce[(vn, v)] = ci
            sn.append((vn, ci))
            ci += 1
    for k, _ in sorted(e.items(), key=itemgetter(1)):
        print(ce[k])

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

A,B,K=map(int,input().split())
for i in range(1,K+1):
  if A>=1:
    A-=1
  elif A<1 and B>=1:
    B-=1
  else:
    pass
print(A,B,end=' ')


### FIXED CODE ###

A,B,K=map(int,input().split())
print(max(A-K,0),min(max(B-K+A,0),B),end=' ')
### BUGGY CODE ###

n = int(input())
x = [int(i) for i in input().split()]
for i in range(n):
    tmp = list(x)
    tmp[i] *= -1
    tmp.sort()
    median = int(n/2)
    print(tmp[median])
    


### FIXED CODE ###

import numpy as np
n = int(input())
x = [int(i) for i in input().split()]

index_hold = np.argsort(x)
index_hold_rev = [0 for i in range(n)]
for i in range(n):
    index_hold_rev[index_hold[i]] = i
for i in range(n):
    if index_hold_rev[i] < n/2:
        print(x[index_hold[int(n/2)]])
    else:
        print(x[index_hold[int(n/2-1)]])
### BUGGY CODE ###

N = int(input())
items = []
cnt = 0
for i in range(N):
    item = input()
    if item not in items:
        items.append(item)
        cnt += 1
print(cnt)

### FIXED CODE ###

N = int(input())
items = []

for i in range(N):
    item = input()
    items.append(item)    
items.sort()
cnt = 0
before = ""
for index,item in enumerate(items):
    if index == 0:
        cnt += 1    
    elif before != item:
        cnt += 1
    before = item
print(cnt)
### BUGGY CODE ###

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

ans = 0
for i in range(N+1):
    tmp = ((A*i)//B) - (A*(i//B))
    if tmp >= ans:
        ans = tmp
    else:
        break
        
print(ans)


### FIXED CODE ###

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

ans = 0
i = 1
while True:
    i += max(1, (B-1)//A+1)
    if i > N:
        break
    tmp = ((A*i)//B) - (A*(i//B))
    if tmp > ans:
        ans = tmp
    else:
        break
        
print(ans)
### BUGGY CODE ###

N,M=map(int,input().split())
List=[list(input().split()) for i in range(M)]
AC = set([])
WA = []
accnt = 0
wacnt = 0

for i in range(M):
  num = List[i][0]
  res = List[i][1]
  
  if num in AC:
    aa = 9
  else:
    if res == "AC":
      accnt = accnt + 1
      AC.add(num)
      wacnt = wacnt + WA.count(num)
    if res == "WA":
      WA.append(num)
  
ans = [int(accnt),int(wacnt)]
print(*ans)


### FIXED CODE ###

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

List = []
ans = [0  for i in range(N+1)]
WA = [0  for i in range(N+1)]

for i in range(M):
  p,S = input().split()
  p = int(p)
  
  if ans[p] == 0:
    if S == "AC":
      ans[p] = 1
    else:
      WA[p] += 1
      
A = sum(ans)

W= 0

for i in range(N+1):
  W += ans[i] * WA[i]

print(A,W)
### BUGGY CODE ###

# -*- coding: utf-8 -*-

X, Y, Z, K = map(int, input().split())
A = sorted(map(int, input().split()), reverse=True)
B = sorted(map(int, input().split()), reverse=True)
C = sorted(map(int, input().split()), reverse=True)

AB = []

for a in A:
    for b in B:
        AB.append(a + b)

AB = sorted(AB, reverse=True)

if K < len(AB):
    AB = AB[:K]

ABC = []
for ab in AB:
    for c in C:
        ABC.append(ab + c)

ABC = sorted(ABC, reverse=True)

ABC = ABC[:K]
ABC = [str(abc) for abc in ABC]

print("\n".join(ABC))


### FIXED CODE ###

# -*- coding: utf-8 -*-

X, Y, Z, K = map(int, input().split())
A = sorted(map(int, input().split()), reverse=True)
B = sorted(map(int, input().split()), reverse=True)
C = sorted(map(int, input().split()), reverse=True)

ABC = []

for x in range(X):
    for y in range(Y):
        if (x + 1) * (y + 1) > K:
            break
        for z in range(Z):
            if (x + 1) * (y + 1) * (z + 1) > K:
                break
            ABC.append(A[x] + B[y] + C[z])

ABC = sorted(ABC, reverse=True)

ABC = ABC[:K]
ABC = [str(abc) for abc in ABC]

print("\n".join(ABC))
### BUGGY CODE ###

from collections import deque
h,w = list(map(int,input().split()))
s = [[x for x in input()] for _ in range(h)]

def find_w():
  for i in range(h):
    for j in range(w):
      if s[i][j] == '.':
        return True

def find_b():
  b = deque([])
  for i in range(h):
    for j in range(w):
      if s[i][j] == '#':
        b.append([i,j])
  return b

d = [[float('inf') for _ in range(w)] for _ in range(h)]
def bfs():
  que = deque([])
  que.extend(find_b())
  dh = [1,0,-1,0]
  dw = [0,1,0,-1]
  while que:
    p = que.popleft()
    for i in range(4):
      nh = p[0] + dh[i]
      nw = p[1] + dw[i]
      
      if nh >= 0 and nh < h and nw >= 0 and nw < w and \
          s[nh][nw] == '.' and d[nh][nw] == float('inf'):
        s[nh][nw] = '#'

cnt = 0
while find_w():
  bfs()
  cnt += 1

print(cnt)


### FIXED CODE ###

from collections import deque

h,w = list(map(int,input().split()))
m = [[x for x in input()] for _ in range(h)]
next_que = deque([])
for i in range(h):
  for j in range(w):
    if m[i][j] == '#':
      next_que.append([i,j])


dh = [1,0,-1,0]
dw = [0,1,0,-1]

def bfs(next_que):
  cnt = -1
  while next_que:
    cnt += 1
    que = list(next_que)
    next_que = deque([])
    for cur in que:
      #cur = que.popleft()
      for i in range(4):
        nh = cur[0] + dh[i]
        nw = cur[1] + dw[i]
        
        if nh >= 0 and nh < h and nw >= 0 and nw < w and m[nh][nw] == '.':
          m[nh][nw] = '#'
          next_que.append([nh,nw])
  
  return cnt



cnt = bfs(next_que) 

print(cnt)
### BUGGY CODE ###

import math
import collections
N,K,Q=map(int,input().split())
A=[int(input()) for _ in range(Q)]
for i in range(N):
 if K+A.count(i+1)-Q>0:
  print("Yes")
 else:
  print("No")

### FIXED CODE ###

import math
import collections
N,K,Q=map(int,input().split())
A=[int(input()) for _ in range(Q)]
c=collections.Counter(A)
for i in range(N):
 if K+c[i+1]-Q>0:
  print("Yes")
 else:
  print("No")
### BUGGY CODE ###

if __name__ == '__main__':
    k, s = map(int, input().split())
    ans = 0
    for i in range(k+1):
        if i > s:
            break
        for j in range(k+1):
            if i + j > s:
                break
            for l in range(k+1):
                if i+j+l == s:
                    ans += 1
                elif i+j+l > s:
                    break
            else:
                continue
        else:
            continue
    print(ans)


### FIXED CODE ###

if __name__ == '__main__':
    k, s = map(int, input().split())
    ans = 0
    for i in range(k+1):
        if i > s:
            break
        for j in range(k+1):
            z = s - i - j
            if i + j > s:
                break
            elif 0 <= z and z <= k:
                ans += 1
    print(ans)
### BUGGY CODE ###

n = int(input())
s = input().strip()
assert n == len(s)

maxlen = 0
for offset in range(1, n):
  t = s[offset:]
  d = 0
  for (a, b) in zip(s, t):
    if a == b:
      d += 1
    else:
      d = 0
    if maxlen < d and d <= offset:  # (d > offset) のとき共通部分文字列が重なってる
      maxlen = d

print(maxlen)

### FIXED CODE ###

n = int(input())
s = input().strip()
assert n == len(s)

maxlen = 0
for offset in range(1, n):
  t = s[offset:]
  d = 0
  for (a, b) in zip(s, t):
    if a == b:
      d += 1
    else:
      d = 0
    if maxlen < d and d <= offset:  # (d > offset) のとき共通部分文字列が重なってる
      maxlen = d

print(maxlen)
### BUGGY CODE ###

s = input()
pos = 0
cnt = 0
if len(s) < 3:
    print(0)
else:
    # print(type(s))
    # print(type(s[0:3]))
    # print(type("ABC"))
    while(True):
        if s[pos:pos+3] == "ABC":
            s = s[:pos] + "BCA" + s[pos+3:]
            pos = 0
            cnt += 1
            # print(s)
        else:
            pos += 1
        if pos >= len(s)-2:
            break
    print(cnt)


### FIXED CODE ###

s = input()
pos = 0
cnt = 0
acnt = 0
if len(s) < 3:
    print(0)
else:
    while(True):
        if s[pos:pos+2] == "BC" and acnt > 0:
            pos += 2
            cnt += acnt
            # print(s, s[pos:pos+2], cnt, pos)
        else:
            if s[pos] == "A":
                acnt += 1
            else:
                acnt = 0
            pos += 1
        if pos >= len(s)-1:
            break
    print(cnt)
### BUGGY CODE ###

N, K = map(int,input().split())
x = list(map(int,input().split()))

ans = -1

for i in range(N - K + 1):
    px = x[i:i+K]
    xl = px[0]
    xr = px[-1]
    tmp = min(abs(xl),abs(xr)) + xr - xl
    if ans == -1 or tmp < ans:
        ans = tmp
        
print(ans)

### FIXED CODE ###

N, K = map(int,input().split())
x = list(map(int,input().split()))

ans = -1

for i in range(N - K + 1):
    l, r = i, i + K - 1
    xl = x[l]
    xr = x[r]
    tmp = min(abs(xl),abs(xr)) + xr - xl
    if ans == -1 or tmp < ans:
        ans = tmp
        
print(ans)
### BUGGY CODE ###

s = list(input())
q = int(input())
sento = 0
for i in range(q):
    line = list(input().split(" "))
    if line[0] == "1":
        sento += 1
    elif line[1] == "1":
        if sento % 2 == 0:
            s.insert(0,line[2])
        else:
            s.append(line[2])
    else:
        if sento % 2 == 0:
            s.append(line[2])
        else:
            s.insert(0,line[2])
if sento % 2 ==1:
    s.reverse()
for i in range(len(s)):
    print(s[i],end ="")
print()

### FIXED CODE ###

from collections import deque
s = deque(list(input()))
q = int(input())
sento = 0
for i in range(q):
    line = list(input().split(" "))
    if line[0] == "1":
        sento += 1
    elif line[1] == "1":
        if sento % 2 == 0:
            s.appendleft(line[2])
        else:
            s.append(line[2])
    else:
        if sento % 2 == 0:
            s.append(line[2])
        else:
            s.appendleft(line[2])
if sento % 2 ==1:
    s.reverse()
for i in range(len(s)):
    print(s[i],end ="")
print()