peterverebics/CodeNet_Python_118 huggingface dataset:
import sys
input = sys.stdin.readline

S = input()
Q = int(input())
W = 0
ans = S.rstrip()

for _ in range(Q):
#    print(W,ans)
    qry = list(input().rstrip().split())
    if qry[0] == "1":
        W = 1-W
        continue
    T,F,C = qry
    T = int(T)
    F = int(F)
    C.rstrip()
    if (W == 0 and F=="2") or (W==1 and F=="1"):
        ans = ans+C
    else:
        ans = C+ans
if W == 1:
    ans = ans[::-1]
print(ans)
S = input()
Q = int(input())
qlst = []
for i in range(Q):
    qlst.append(input().split())
start = S
end = ''
for i in range(Q):
    if qlst[i][0] == '1':
        start, end = end, start
    else:
        if qlst[i][1] == '1':
            start += qlst[i][2]
        else:
            end += qlst[i][2]

print(start[::-1] + end)
from sys import stdin

s = stdin.readline().rstrip().split()
q = int(stdin.readline().rstrip())

left = 1
for i in range(q):
    line = stdin.readline().rstrip().split()
    if len(line) == 1:
        left *= -1
    else:
        if ((line[1] == '1') and (left == 1)) or ((line[1] == '2') and (left == -1)):
            s.append(line[2])
        else:
            s.insert(0, line[2])
from sys import stdin
input = stdin.readline
s=input()
q=int(input())
cnt=0
qq=[list(input().split()) for _ in range(q)]
for j in qq:
    if  len(j)!=1 :
        if (cnt+int(j[1]))%2==1:
            s=j[2]+s
        else:
            s=s+j[2]
    else:
        cnt+=1
if cnt%2==0:
    print(s)
else:
    print(s[::-1])
s = input()
q = int(input())

rev = 0
head = []
tail = []

for i in range(q):
    t = input().split()
    if t[0] == "1":
        rev ^= 1
    else:
        if (int(t[1])-1)^rev == 0:
            head.append(t[2])
        else:
            tail.append(t[2])

h = "".join(head)
t = "".join(tail)

if rev == 0:
    s = s[::-1]
    print(h[::-1] + s + t)
else:
    print(t[::-1] + s + h)
S = list(input())
Q = int(input())

d = 1
for i in range(Q):
    que = list(input().split())
    if que[0] == str(1):
        d *= -1
    else:
        if d * int(que[1]) == 1 or -2:
            S.append(que[2])
        else:
            S.insert(0, que[2])

print(''.join(S) if d == -1 else ''.join(S[::-1]))
from collections import deque
import sys
input = sys.stdin.readline
s = str(input().rstrip())
ans = deque(list(s))

f = True
q = int(input())
for i in range(q):
    q = list(map(str, input().split()))
    if int(q[0]) == 1:
        f = not f
    else:
        f_i = int(q[1])
        if f_i == 1 ^ f:
            ans.appendleft(q[2])
        else:
            ans.append(q[2])
ans = "".join(list(ans))
if not f:
    ans = ans[::-1]
print(ans)
txt = input()
n = (int)(input())
count = 0
sentou = ""
saigo = ""
for i in range(n):
  temp = input()
  if temp[0] == '1':
    count += 1
  else:
    sec, moji = (int)(temp.split(" ")[1]), temp.split(" ")[2]
    if (sec + count) % 2 == 1:
      sentou = moji + sentou
    else:
      saigo += moji
if count % 2 == 1:
  txt = saigo[::-1] + txt + sentou[::-1]
else:
  txt = sentou + txt + saigo
print(txt)
s = input()
q = int(input())
query_list = [input().split() for _ in range(q)]

front = []
back = []
back.append(s)

for query in query_list:
    if query[0] == '1':
        front, back = back, front
    elif query[0] == '2':
        key = int(query[1])
        value = query[2]
        if key == 1:
            front.append(value)
        else:
            back.append(value)
print(''.join(front[::-1]+back))
from itertools import chain

def swap(head, tail):
    return (tail, head)

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

head = []
tail = []

for _i in range(q):
    query = input()
    if query == "1":
        head, tail = swap(head, tail)
    else:
        _n, f, c = tuple(query.split())
        if f == "1":
            head.append(c)
        else:
            tail.append(c)

print("".join(tuple(chain(reversed(head), s, tail))))
S = str(input())
Q = int(input())
query = [list(map(str,input().split())) for _ in range(Q)]

for i in range(Q):
    if query[i][0]=='1':
        S = S[-1]+S[1:-1]+S[0]
    elif query[i][0]=='2':
        if query[i][1]=='1':
            S = query[i][2] + S
        else:
            S = S + query[i][2]
print(S)
import sys
input=sys.stdin.readline
s=input()
q=int(input())
sign=1
for i in range(q):
  Q=[str(i) for i in input().split()]
  if Q[0]=="1":
    sign*=-1
  else:
    f,c=Q[1:]
    if f=="1" and sign==1:
      s=c+s
    elif f=="2" and sign==-1:
      s=c+s
    else:
      s=s+c
print(s if sign==1 else s[::-1])
S = input()
Q = int(input())
query = [list(input().split()) for i in range(Q)]

for i in range(Q):
    if query[i][0] == '1':
        if len(S) > 1:
            S = S[-1] + S[1:-1] + S[0]
        #print(S)
    else:
        if query[i][1] == '1':
            S = query[i][2] + S
        else:
            S = S + query[i][2]
print(S)
from collections import deque

s = input()
q = int(input())
Q = [list(map(str, input().split())) for _ in range(q)]

S = deque([s])
flip = False

for qi in Q:
    if int(qi[0]) == 1:
      flip = not flip
    else:
      f = int(qi[1]) 
      c = qi[2]
      if flip:
        f = 3-f    
      if f == 1:
        S.appendleft(c)
      else:
        S.append(c)

if flip:
    S.reverse()
print(''.join(S))
from collections import deque
d=deque(map(str,input().split()))
q=int(input())
for _ in range(q):
    ss=input()
    if ss[0]==str(1):
        d.reverse()
    else:
        if ss[2]==str(1):
            d.appendleft(ss[4])
        else:
            d.append(ss[4])
for j in d:
    print(j,end='')
S = input() 
Q = int(input())
rlist, llist = [], []
flag, cnt = 0, 0

def trans(f):
    if f == 1:
        f = 0
    elif f == 0:
        f = 1
    return f

for i in range(Q):
    lists = input().split()
    if lists[0] == "1":
        flag = trans(flag)
    else:
        if lists[1] == "2":
            rlist.append(lists[2])
        else:
            llist.append(lists[2])
print(S)
#D - String Formation
S = list(str(input()))
Q = int(input())
q = [input().split() for i in range(Q)]

#print(S)
#print(q)

for i in range(Q):
    tmp1 = ""
    if q[i][0] == '1':
       tmp1 = S[0]
       S[0] = S[-1]
       S[-1] = tmp1
    else:
        if q[i][1] == "1":
            S.insert(0,q[i][2])
        else:
            S.append(q[i][2])
ans = ''.join(S)
print(ans)
S = input()
Q = int(input())
Q_query = []
for _ in range(Q):
    T = input().split()
    print(T)
    if len(T) == 1:
        T = int(T[0])
        if T == 1:
            S = S[::-1]            
    else: 
        if int(T[0]) == 2:
            if int(T[1]) == 1:
                S = T[2]+ S
            elif int(T[1]) == 2: 
                S = S + T[2]
print(S)
def main():
    c = 0
    s = input()
    for _ in range(int(input())):
        q = input().split()
        if q[0] == '1':
            c += 1
        else:
            if q[1] == '1':
                s = q[2] + s
            else:
                s += q[2]
    if c%2 == 0:
	    print(s)
    else:
        print(s[::-1])

if __name__ == '__main__':
    main()
S = input()
Q = int(input())
reverse_flag = False

for _ in range(Q):
  T = list(input().split())

  if T[0] == '1':
    reverse_flag = not reverse_flag

  else:
    if (T[1] == '1' and reverse_flag) or (T[1] != '2' and (not reverse_flag)):
      S = T[2]+S

    else:
      S = S+T[2]

print(S) if reverse_flag else print(S[::-1])
s_list = list(input())
q = int(input())
reverse = False

for _ in range(q):
	query= input().split()
	if int(query[0]) == 1:
		reverse = True
	elif int(query[0]) == 2:
		if int(query[1]) == 1:
			if reverse:
				s_list.append(query[2])
			else:
				s_list.insert(0, query[2])
		elif int(query[1]) == 2:
			if reverse:
				s_list.insert(0, query[2])
			else:	
				s_list.append(query[2])


if reverse:
	s_list.reverse()
for s in s_list:
  print(s, end ="")
S = list(input())
Q = int(input())

data = [list(map(str,input().split())) for _ in range(Q)]

A = []
B = []
a = 0

for i in range(Q-1,-1,-1):
  if data[i][0] == '1':
    a += 1
  else:
    if (int(data[i][1]) + a) % 2 == 1:
      A.append(data[i][2])
    else:
      B.insert(0,data[i][2])

A.extend(S)
A.extend(B)

C = ''.join(A)

print(C)
S = input() 
Q = int(input())
for i in range(Q):
    lists = input().split()
    if lists[0] == "1":
        pass
    else:
        pass
print(S)
ans = [str(input())]
n = int(input())
num = [str(input()).split() for _ in range(n)] 
for i in num:
    if len(i) == 1:
        ans.reverse()
    else:
        if i[1] == "1":
            ans.insert(0, i[2])
        else:
            ans.append(i[2])
print("".join(ans))
S = input()
Q = int(input())
Qs = []
for _ in range(Q):
    Qs.append(input().split())  # T, F, C

rev = 0
for i in range(len(Qs)):
    if Qs[i][0]=='1':
        rev += 1
    else:
        if rev%2 == 1:
            S = S[::-1]
            rev = 0
        
        if Qs[i][1] == '1':
            S = Qs[i][2] + S
        else:
            S = S + Qs[i][2]

print(S)
s = input()
q = int(input())

rev = [0] #反転する回数
list = [s] #単語のリスト

for i in range(q):
    t = input().split()

    if int(t[0]) == 1:
        list = list[::-1]
    else:
        if int(t[1]) == 1:
            list.insert(0,t[2])
        else:
            list.append(t[2])

for i in range(len(list)):
    print(list[i],end='')

print()
flag = True
S = input()
Q = int(input())
for i in range(Q):
  T = input().split()
  if T[0] == "1":
    if flag == True:
      flag = False
    else:
      flag = True
  else:
    if flag == True:
      if T[1] == "1":
        S = T[-1] + S
      else:
        S = S + T[-1]
    else:
      if T[1] == "1":
        S = S + T[-1]
      else:
        S = T[-1] + S
print(S)
#ABC158 D - String Formation

S = str(input())
Q = int(input())
print(S)
for i in range(Q):
    q = input().split(' ')
    if(q[0] == '1'):
        S = S[::-1]

    else:
        if(q[1] == '1'):
            temp = [q[2]]
            S = q[2] + S
        else:
            S = S + q[2]

print(S)
s = input()
q = int(input())
for i in range(q):
    t = input().split(' ')
    if t[0] == '1':
        s = s[-1] + s[1:-1] + s[0]
    else:
        if t[1] == '1':
            s = t[2] + s
        else:
            s = s + t[2]
print(s)
S = list(input())
Q = int(input())



for i in range(Q):
    command = list(input().split())
    if command[0] == "1":
        S[0], S[-1] = S[-1], S[0]
    elif command[0] == "2":
        if command[1] == "1":
            S.insert(0, command[2])
        elif command[1] == "2":
            S.append(command[2])
    #print(i+1, S)
print("".join(S))
S = input()
Q = int(input())

Q_list = [list(map(str, input().split())) for i in range(Q)]

for i in range(Q):                
  if len(Q_list[i]) == 1:
    if len(S) >= 2:
      S_list = list(S)
      S_list[0], S_list[-1] = S_list[-1], S_list[0]
      S = "".join(S_list)
  
  else:
    F = Q_list[i][1]
    C = Q_list[i][2]
    
    if int(F) == 1:
      S = C + S
    else:
      S = S + C
      
print(S)
s = input().split()
ans = s
s = input().split()
q = int(s[0])
count=0
for i in range(q):
    s = input().split()
    if s[0]=='1': 
        count+=1
    else:
        if count%2==1:
            ans=ans[::-1]
        count=0
        if s[1]=='1':
            ans=list(s[2])+ans
        else:
            ans.append(s[2])
print(''.join(ans))
s = input()
q = int(input())

flag = 1
for i in range(q):
  st = input().split()
  if int(st[0]) == 1:
    flag *= -1
  else:
    tmp = int(st[1])*flag
    if tmp == 1 or -2:
      s = st[2] + s
    else:
      s = s + st[2]

print(s)
from collections import deque

S = input()
Q = int(input())
W = 0
ans = deque(list(S.rstrip()))

for _ in range(Q):
#    print(W,ans)
    qry = list(input().rstrip().split())
    if qry[0] == "1":
        W = 1-W
        continue
    T,F,C = qry
    T = int(T)
    F = int(F)
    C.rstrip()
    if (W == 0 and F=="2") or (W==1 and F=="1"):
        ans.append(C)
    else:
        ans.appendleft(C)
ans = "".join(map(str,ans))
if W == 1:
    ans = ans[::-1]
print(ans)
from collections import deque
d = deque()
s = input()
d.append(s)
rev = 0

Q = int(input())

for q in range(Q):
    ls = input().split()
    print (ls)
    if ls[0] == '1':
        rev = (rev + 1) % 2
    else:
        if (int(ls[1]) + rev) % 2 == 0:
            d.append(ls[2])
        else:
            d.appendleft(ls[2])

ans = ''.join(list(d))
if rev == 1:
    ans = ans[::-1]
print (ans)
#!/usr/bin/env python3
S = input()
Q = int(input())

count = 0
ans = [S]

for i in range(Q):
    T = input().split()
    if len(T)==1:
        count+=1
        continue

    if count%2==0:
        if T[1] == '1': ans.insert(0, T[2])
        else: ans.append(T[2])
    else:
        if T[1] == '1': ans.append(T[2])
        else: ans.insert(0, T[2])


if count%2==0:
    print(''.join(ans))
else:
    print(''.join(ans[::-1]))
f=input
s=f()
n=int(f())
t=0
l,r='',''
for i in range(n):
  q=f()
  if q[0]=='1': t=1-t
  elif int(q[2])+t==2: r+=q[4]
  else: l+=q[4]
print([l[::-1]+s+r,r[::-1]+s+l][t])
S = input()
Q=int(input())
Query=[S]
flag = True
for i in range(Q):
  S2=input()
  if S2=="1":
    flag = not flag
  else:
    x=S2.split()
    if flag:
      if x[1]=="1":
        Query=[x[2]]+Query
      else:
        Query.append(x[2])
    else:
      if x[1]=="1":
        Query.append(x[2])
      else:
        Query=[x[2]]+Query
if flag==False:
  Query.reverse()

print("".join(Query))
S = input()
Q = int(input())

for i in range(Q):
    query = input()
    if len(query.split()) == 1:
        S = S[-1] + S[1: -1] + S[0]
    else:
        t_i, f_i, c_i = query.split()
        f_i = int(f_i)
        if f_i == 1:
            S = c_i + S
        else:
            S = S + c_i
print(S)
from collections import deque

S = input()
Q = int(input())
q = deque(S)
fg = False
for i in range(Q):
    que = list(map(str , input().split()))
    if que[0] == "1":
        fg = True
    elif (que[1] == "1" and fg == False) or (que[1] == "2" and fg == True) :
        q.appendleft(que[2])
    else:
        q.append(que[2])
if fg:
    q.reverse()
print("".join(q))
s = input()
q = int(input())
count = 0
for i in range (q):
    t = input().split()
    if(t[0]=='2'):
        if((t[1] == 1 and count % 2 == 0 )or(t[1] == 2 and count % 2 == 1)):
            s = t[2] + s
        else:
            s = s + t[2]
    else:
        count+=1
if(count%2 == 1):
    print(s)
else:
    print(s[::-1])
from collections import deque
S = deque(input())

dic = 1
Q = int(input())
for i in range(0,Q):
    Query = input()
    if Query == '1':
        dic = dic*(-1)
    else:
        T, F, C = Query.split()
        if (F*dic == 2) or (F*dic == -1):
            S.append(C)
        else:
            S.appendleft(C)
print(''.join(list(S)[::dic]))
from collections import deque

S  = deque()
S.append(input().strip())

N = int(input().strip())

for _ in range(N):
    ope = input().strip()
    if ope[0] == "1":
        S.reverse()
        continue
    i, s, c = ope.split(" ")
    if s == "1":
        S.appendleft(c)
    else:
        S.append(c)

print(''.join(S))
from collections import deque
S = input()
Q = int(input())

n = S
mode = 0
operations = []
deq = deque()
deq.append(n)
for i in range(Q):
    operations.append(input().split())
for op in operations:
    if op[0] == "1":
        mode += 1
        mode %= 2
    elif int(op[1]) + mode == 2:
        deq.append(op[2])
    else:
        deq.appendleft(op[2])
if mode == 0:
    print("".join(deq))
else:
    print("".join(reversed(deq)))
s = str(input())
q = int(input())
inv = False
for i in range(q):
    a = list(input().split())
    if a[0] == '1':
        inv = ~inv
    if a[0] == '2':
        if (a[1] == '1')^inv:
            s = a[2] + s
        else:
            s += a[2]
if inv:
    print(s[::-1])
else:
    print(s)
s = input()
q = int(input())
Flag = True
right=''
left=''

for i in range(q):
    t = input()
    if t[0] == '1':
        Flag = not Flag
    else:
        _, f, c = t.split()
        if Flag == False and f == '2' or Flag == True and f == '1':
            left += c
        else:
            right += c

ans = right[::-1] + s + left
print(ans if not Flag else ans[::-1])
s = list(input())
q = int(input())
i = 0
while i < q:
    arr = list(input().split(' '))
    if int(arr[0]) == 1:
        tmp = s[0]
        s[0] = s[-1]
        s[-1] = tmp
    else:
        f, c = int(arr[1]), arr[2]
        if f == 1:
            tmp = s
            s = [c]
            s+= tmp
        else:
            s.append(c)
    i += 1
text = "".join(s)
print(text)
s = list(input())
q = int(input())
judge = 1
for i in range(q):
  d = input()
  if d == '1':
    judge *= -1
  else:
    a, b, c = list(d.split())
    b = int(b)
    if (b == 1 and judge ==1) or (b == 2 and judge == -1):
      s.insert(0, c)
    else:
      s.append(c)
print(*s,sep='')
S = input()
Q = int(input())

res = S

is_rev = False

def add_char(s, f, c, is_rev):
  if(f == "1"):
    return c + s if not is_rev else s + c
  else:
    return s + c if not is_rev else c + s

for _ in range(Q):
  q = input().split()
  
  if(q[0] == '1'): # Ti = 1
    is_rev = not is_rev
  
  else: #Ti = 2
    res = add_char(res, q[1], q[2], is_rev)


print(res)
S = input()
Q = int(input())
reverse = 0
left = ""
right = ""
for _ in range(Q):
  q = input().split()
  if q[0]=="1":
    reverse += 1
    left, right = right, left
  if q[0]=="2":
    if q[1]=="1":
      left = left + q[2]
    if q[1]=="2":
      right = q[2] + right
if reverse%2==1:
  left = left[::-1]
  right = right[::-1]
  S = S[::-1]
print(left+S+right)
s = input()
q = int(input())
results_forward = str("")
results_back = str("")
qlist = []
for i in range(q):
  qlist.extend(input().split(" "))

amount = 0
for i in range(len(qlist)):
  if qlist[i] == "1" or qlist[i] == "2":
    amount += int(qlist[i])
  elif amount%2 == 1:
    results_forward = qlist[i] + results_forward
    amount = 0
  elif amount%2 == 0:
    results_back = results_back + qlist[i]
    amount = 0

if q%2 == (len(results_forward)+len(results_back))%2:
  print(results_forward+s+results_back)
else:
  print((results_forward+s+results_back)[::-1])
string = input()
kai = int(input())
data = [input().split(' ') for i in range(kai)]
key = 0  #0 start

for i in range(kai):
    if(data[i][0] == '1'):
        key = 1
    elif(data[i][1] == '1'):
        string = data[i][2] + string
    else:
        string = string + data[i][2]
if(key == 0):
    print(string)
else:
    print(string[::-1])
from collections import deque
s=input()
que=deque([s])
q=int(input())
rev=False
for i in range(q):
    t=input()
    if t=='1':
        rev=not rev
    else:
        tt,f,c=t.split()
        if (rev==False and f=='1')or(rev and f=='2'):
            que.appendleft(c)
        else:
            que.append(c)
if rev:
    que.reverse()
print(''.join(que))
s=input()
q=int(input())
f=0
l,r='',''
for i in range(q):
    a=list(input().split())
    if a[0]=='1':
        f+=1
    else:
        if int(a[1])^f==0:
            r+=a[2]
        else:
            l+=a[2]
if f%2==1:
    print(r+s+l)
else:
    print(l[::-1]+s+r[::-1])
S = input()
Q = int(input())
ans = [S]
for _ in range(0,Q):
    tmp = input().split(" ")
    if tmp[0] == "1":
        ans.reverse()
    elif tmp[0] == "2":
        if tmp[1] == "1":
            ans.insert(0,tmp[2])
        else:
            ans.append(tmp[2])

print("".join(ans))
n, m = map(int, input().split())
SC = [tuple(map(int, input().split())) for i in range(m)]
if n == 1:
	r = range(0, 10)
elif n == 2:
	r = range(10, 100)
else:
	r = range(100, 1000)
for i in r:
	si = str(i)
	for s, c in SC:
		if si[s-1] != str(c):
			break
	else:
		print(i)
		exit()
else:
	print(-1)
n, m = map(int, input().split())
number = [0] * n
cou = [0] * n
#logging.debug(number)#

for i in range(m):
    s, c = map(int, input().split())
    s -= 1
    if cou[s] > 0 and c != number[s]:
        print(-1)
        exit()
    else:
        cou[s] += 1
        number[s] = c
    #logging.debug("number = {}, cou = {}".format(number, cou))

if n > 1 and number[0] == 0 and cou[0] >= 1:
    print(-1)
    exit()
elif n > 1 and number[0] == 0:
    number[0] = 1

number = map(str, number)
print(''.join(number))
N, M = map(int, input().split())
r = list([None]*N)
for i in range(M):
    s, c = map(int, input().split())
    if r[s-1] is not None and r[s-1] != c:
        print("-1")
        exit()
    r[s-1] = c

if N > 1 and r[0] == 0:
        print("-1")
        exit()

for i in range(len(r)):
    if r[i] is None:
        if N > 1 and i == 0:
            r[i] = 1
        else:
            r[i] = 0

print("".join(map(str, r)))
N,M=map(int,input().split())
A=[0 for i in range(N)]
flg=0

for i in range(M):
    s,c=map(int,input().split())
    if A[s-1]!=0 and A[s-1]!=c:
        flg=1
    if s==1 and c==0 and N!=1:
        flg=1
    if flg==0:
        A[s-1]=c

if flg==1:
    print(-1)
else:
    if A[0]==0:
        if N!=1:
            A[0]=1
    print(''.join(map(str,A)))
n, m = map(int, input().split())
s = []
c = []
for i in range(m):
    line = list(map(int, input().split()))
    s.append(line[0])
    c.append(line[1])
l = 10**(n-1)
if n == 1:
    l = 0
for i in range(l, 10**n):
    flag = 0
    flag2 = 0
    for j in range(m):
        if str(i)[s[j]-1] != str(c[j]):
            flag = 1
    if flag == 0:
        print(i)
        flag2 = 1
        break
if flag2 == 0:
    print(-1)
import numpy as np
N , M = map(int, input().split())
ans = np.zeros(N)
S_C = []
for _ in range(M):
  s, c = map(int, input().split())
  for i, j in S_C:
    if s == i and c != j:
      print(-1)
      quit()
  ans[s-1] = c
  S_C.append([s, c])

if ans[0] == 0 and [1, 0] in S_C and N > 1:
  print(-1)
  quit()
if ans[0] == 0 and [1, 0] not in S_C and N > 1:
  ans[0] = 1
print(int("".join([str(int(x)) for x in ans])))
import sys
input = sys.stdin.readline

N, M = [int(x) for x in input().split()]
ans = ["0"] * N
sc = []
for _ in range(M):
    tmp = [int(x) for x in input().split()]
    sc.append(tmp)
for i in range(1000):
    flag = 1
    i = str(i)
    if len(i) != N:
        continue
    for j in sc:
        if i[j[0] - 1] != str(j[1]):
            flag = 0
            break
    if flag:
        print(i)
        sys.exit()
print(-1)
import sys
n,m=map(int,input().split())
tmp=[-1]*n
for _ in range(m):
    s,c=map(int,input().split())
    s-=1
    if tmp[s]<0:
        tmp[s]=c
    if tmp[s]!=c:
        print(-1)
        sys.exit()
if tmp[0]==0 and n>1:
    print(-1)
    sys.exit()
if tmp[0]<0 and n>1:
    tmp[0]=1
if n==1 and tmp[0]==-1:
    tmp[0]=0
for i in range(1,n):
    if tmp[i]<0:
        tmp[i]=0
for i in tmp:
    print(i,end='')
N, M = map(int, input().split())
SC = [list(map(int, input().split())) for _ in range(M)]
 
for X in range(10**N):
  X = str(X)
  
  #all　すべてがtrueならという意味
  if len(X) == N and all(X[s-1] == str(c) for s, c in SC):
    print(X)
    break
else:
  print(-1)
n, m = map(int, input().split())
sc = [list(map(int, input().split())) for _ in range(m)]

if m == 0:
    if n == 1:
        print(0)
    else:
        print(10 ** (n - 1))
    exit()

if n == 1:
    left = 0
else:
    left = 10 ** (n - 1)
right = 10 ** n

ans = -1
for i in range(left, right):
    i = str(i)
    for s, c in sc:
        if int(i[s - 1]) != c:
            break
    else:
        ans = i
        break

print(ans)
n,m=map(int,input().split())
ans=[-1]*n
if n==1 and m==0:
    print(0)
    exit()
for i in range(m):
    s,c=map(int,input().split())
    if ans[s-1]!=-1 and ans[s-1]!=c:
        print(-1)
        exit()
    ans[s-1]=c
if ans[0]==-1:
    ans[0]=1
elif ans[0]==0 and n>1:
    print(-1)
    exit()
x=0
for i in range(n):
    x*=10
    if ans[i]!=-1:
        x+=ans[i]

print(x if len(str(x))==n else -1)
N,M=map(int,input().split())
SC=[input().split() for _ in range(M)]
ans=["0"]*N
for s,c in SC:
  if ans[int(s)-1]!="0" and ans[int(s)-1]!=c:
    print(-1)
    exit()
  if N>1 and s=="1" and c=="0":
    print(-1)
    exit()
  ans[int(s)-1]=c
if ans[0]=="0" and N>1:
  ans[0]="1"
print("".join(ans))
import sys

n, m = map(int, sys.stdin.readline().rstrip().split())
sc = [sys.stdin.readline().rstrip().split() for i in range(m)]
if n > 1:
    ans = ['1'] + ['0'] * (n - 1)
else:
    ans = ['0']
tmp = [0] * n
for sci in sc:
    s, c = sci
    if n > 1 and s == '1' and c == '0':
        print(-1)
        sys.exit()
    if tmp[int(s) - 1] > 0 and ans[int(s)-1] != c:
        print(-1)
        sys.exit()
    ans[int(s)-1] = c
    tmp[int(s) - 1] += 1
# print(ans)
print(''.join(ans))