You are given a long decimal number $a$ consisting of $n$ digits from $1$ to $9$. You also have a function $f$ that maps every digit from $1$ to $9$ to some (possibly the same) digit from $1$ to $9$.

You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $a$, and replace each digit $x$ from this segment with $f(x)$. For example, if $a = 1337$, $f(1) = 1$, $f(3) = 5$, $f(7) = 3$, and you choose the segment consisting of three rightmost digits, you get $1553$ as the result.

What is the maximum possible number you can obtain applying this operation no more than once?


-----Input-----

The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of digits in $a$.

The second line contains a string of $n$ characters, denoting the number $a$. Each character is a decimal digit from $1$ to $9$.

The third line contains exactly $9$ integers $f(1)$, $f(2)$, ..., $f(9)$ ($1 \le f(i) \le 9$).


-----Output-----

Print the maximum number you can get after applying the operation described in the statement no more than once.


-----Examples-----
Input
4
1337
1 2 5 4 6 6 3 1 9

Output
1557

Input
5
11111
9 8 7 6 5 4 3 2 1

Output
99999

Input
2
33
1 1 1 1 1 1 1 1 1

Output
33

Difficulty: EASY

Solutions:
alen = int(input())
a = input()
funcs = input().split()
start_index = None
end_index = None
ans = ''
for (i, c) in enumerate(a):
	ic = int(c)
	maps_to = int(funcs[ic - 1])
	if not end_index:
		if ic < maps_to:
			start_index = True
			ans += str(maps_to)
			continue
		elif ic > maps_to:
			if start_index is not None:
				end_index = True
	ans += c
print(ans)


n = int(input())
a = input()
tab = [int(x) for x in input().split()]
res = []
applied = False

def f(x):
	return int(tab[x - 1])
i = 0
while i < n:
	if f(int(a[i])) > int(a[i]) and (not applied):
		applied = True
		while i < n and f(int(a[i])) >= int(a[i]):
			res.append(f(int(a[i])))
			i += 1
	else:
		res.append(int(a[i]))
		i += 1
print(*res, sep='')


n = int(input())
a = list(input())
f = ['0']
f.extend(list(input().split()))
started = False
for (i, item) in enumerate(a):
	if not started:
		if int(f[int(item)]) > int(item):
			a[i] = f[int(item)]
			started = True
	elif int(f[int(item)]) >= int(item):
		a[i] = f[int(item)]
	else:
		break
print(''.join(a))


(n, s, arr) = (int(input()), input(), [int(s) for s in input().split()])
(start, end) = (n, n)
for i in range(n):
	temp = int(s[i])
	if temp < arr[temp - 1]:
		start = min(start, i)
	if start == n or end != n:
		continue
	if temp > arr[temp - 1]:
		end = i
for i in range(n):
	if i < start or i >= end:
		print(s[i], end='')
	else:
		print(arr[int(s[i]) - 1], end='')


t = int(input())
n = list(map(int, list(input())))
f = list(map(int, input().split()))
s = dict()
for i in range(1, 10):
	s[i] = f[i - 1]
change = False
for i in range(0, t):
	if n[i] < s[n[i]]:
		n[i] = s[n[i]]
		change = True
	elif n[i] == s[n[i]]:
		pass
	elif change:
		break
for i in range(t):
	print(n[i], end='')


n = int(input())
s = str(input())
f = list(map(int, input().split()))
a = []
for i in s:
	a.append(int(i))
c = 0
for (i, vi) in enumerate(a):
	if vi < f[vi - 1]:
		if c == 0 or c == 1:
			a[i] = f[vi - 1]
			c = 1
	elif vi > f[vi - 1]:
		if c == 1:
			c = 2
s2 = ''
for i in a:
	s2 += str(i)
print(s2)


n = int(input())
a = list(input())
b = list(map(int, input().split()))
i = 0
while i < n:
	k = int(a[i])
	if b[k - 1] > k:
		while i < n and b[int(a[i]) - 1] >= int(a[i]):
			a[i] = str(b[int(a[i]) - 1])
			i += 1
		break
	i += 1
print(*a, sep='')


n = int(input())
s = list(input())
l = list(map(int, input().rstrip().split(' ')))
change = False
for i in range(n):
	x = int(s[i])
	if x < l[x - 1]:
		change = True
		s[i] = str(l[x - 1])
	elif x > l[x - 1] and change:
		break
for i in range(n):
	print(s[i], end='')


n = int(input())
s = list(map(int, list(input())))
mapping = list(map(int, input().split(' ')))
for x in range(n):
	if s[x] < mapping[s[x] - 1]:
		y = x
		while y < n and s[y] <= mapping[s[y] - 1]:
			s[y] = mapping[s[y] - 1]
			y += 1
		break
print(*s, sep='')


n = int(input())
m = input()
a = list(map(int, input().split()))
d = {i + 1: item for (i, item) in enumerate(a)}
ans = []
sss = True
f = True
for i in m:
	if int(i) <= d[int(i)] and f:
		ans.append(str(d[int(i)]))
		if not int(i) == d[int(i)]:
			sss = False
	else:
		ans.append(i)
		if not sss:
			f = False
print(''.join(ans))


n = int(input())
s = input()
a = list(map(int, input().split()))
s = list(map(int, s))
c = 1
for i in range(0, n):
	if s[i] < a[s[i] - 1]:
		c = 0
		s[i] = a[s[i] - 1]
	elif s[i] > a[s[i] - 1] and c == 0:
		break
print(''.join(map(str, s)))


import math

def main():
	n = int(input())
	s = input()
	f = list(map(int, input().split()))
	s1 = [str(f[int(s[i]) - 1]) for i in range(n)]
	pos = 0
	while pos < n and s1[pos] <= s[pos]:
		pos += 1
	if pos == n:
		print(s)
	else:
		end = pos
		while end + 1 < n and s1[end + 1] >= s[end + 1]:
			end += 1
		res = list(s)
		for i in range(pos, end + 1):
			res[i] = s1[i]
		print(''.join(res))
main()


import math
from collections import defaultdict
ml = lambda : map(int, input().split())
ll = lambda : list(map(int, input().split()))
ii = lambda : int(input())
ip = lambda : input()
t = ii()
a = list(ip())
lol = ll()
f = 0
for i in range(t):
	while i < t:
		if f == 0:
			if lol[int(a[i]) - 1] > int(a[i]):
				f = 1
				a[i] = str(lol[int(a[i]) - 1])
				i += 1
			else:
				break
		elif lol[int(a[i]) - 1] >= int(a[i]):
			f = 1
			a[i] = str(lol[int(a[i]) - 1])
			i += 1
		else:
			break
	if f:
		break
print(''.join(a))


from itertools import chain

def main():
	_ = input()
	number = input()
	replace_map = ['0'] + list(input().split())
	i = next((i for (i, x) in enumerate(number) if replace_map[int(x)] > x), len(number))
	j = next((j for (j, x) in enumerate(number) if j > i and replace_map[int(x)] < x), len(number))
	for x in chain(number[0:i], (replace_map[int(x)] for x in number[i:j]), number[j:]):
		print(x, end='')
main()


a = int(input())
b = input()
c = list(map(int, input().split()))
p = 0
ans = []
for i in range(a):
	if int(b[i]) > c[int(b[i]) - 1] and p == 1:
		p = 2
	if int(b[i]) < c[int(b[i]) - 1] and p != 2:
		p = 1
		ans.append(chr(c[int(b[i]) - 1] + 48))
	else:
		ans.append(b[i])
print(''.join(ans))


import math
n = int(input())
t = input()
a = []
for i in t:
	a.append(int(i))
d = {}
x = [int(i) for i in input().split()]
for i in range(len(x)):
	d[i + 1] = x[i]
s = ''
cont = 0
for i in range(len(a)):
	if a[i] < d[a[i]]:
		j = i
		while j < n and a[j] <= d[a[j]]:
			a[j] = d[a[j]]
			j += 1
		break
for i in a:
	s = s + str(i)
print(s)


n = int(input())
d = list(map(int, list(input())))
f = list(map(int, input().split()))
flag = False
for i in range(n):
	if f[d[i] - 1] > d[i]:
		d[i] = f[d[i] - 1]
		flag = True
	elif flag and d[i] > f[d[i] - 1]:
		break
print(*d, sep='')


from itertools import accumulate
import os
import sys
from io import BytesIO, IOBase
import math
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
n = int(input())
s = list(input())
a = [0] + list(map(int, input().split()))
f = 0
for i in range(0, len(s)):
	if int(s[i]) < a[int(s[i])]:
		s[i] = a[int(s[i])]
		f = 1
	elif int(s[i]) == a[int(s[i])]:
		if f == 0:
			pass
	elif f == 1:
		break
for i in range(0, len(s)):
	print(s[i], end='')


n = int(input())
a = input()
a = [int(i) for i in a]
z = list(map(int, input().split()))
i = 0
while i < n and a[i] >= z[a[i] - 1]:
	i += 1
while i < n and a[i] <= z[a[i] - 1]:
	a[i] = z[a[i] - 1]
	i += 1
print(''.join(map(str, a)))


n = int(input())
s = input()
a = list(map(int, input().split()))
ans = 0
ind = -1
izmen = []
for i in range(n):
	if izmen != []:
		if izmen[-1] - i != -1:
			print(s[i:], end='')
			exit()
	if int(s[i]) > a[int(s[i]) - 1]:
		print(s[i], end='')
	elif int(s[i]) == a[int(s[i]) - 1]:
		if izmen == []:
			print(s[i], end='')
		elif abs(izmen[-1] - i) == 1:
			print(a[int(s[i]) - 1], end='')
			izmen.append(i)
	elif izmen == []:
		print(a[int(s[i]) - 1], end='')
		izmen.append(i)
	elif abs(izmen[-1] - i) == 1:
		print(a[int(s[i]) - 1], end='')
		izmen.append(i)
	else:
		print(s[i], end='')


def main():
	n = int(input())
	a = input()
	d = [i for i in input().split()]
	f = {}
	feels_good = set()
	feels_great = set()
	for i in range(9):
		f[str(i + 1)] = d[i]
		if i + 1 <= int(d[i]):
			feels_good.add(str(i + 1))
		if i + 1 < int(d[i]):
			feels_great.add(str(i + 1))
	ans = ''
	flag = True
	for i in range(n):
		if flag and a[i] in feels_great:
			flag = False
			ans += f[a[i]]
		elif a[i] in feels_good:
			ans += f[a[i]]
		elif not flag:
			ans += a[i:]
			break
		else:
			ans += a[i]
	print(ans)
main()


R = input
n = int(R())
a = list(map(int, R()))
f = [0] + list(map(int, R().split()))
for i in range(n):
	if f[a[i]] > a[i]:
		j = i
		while j < n and f[a[j]] >= a[j]:
			a[j] = f[a[j]]
			j += 1
		break
print(''.join(map(str, a)))


n = int(input())
nums = list(map(int, list(input())))
arr = list(map(int, input().split()))
fl = False
for i in range(n):
	if nums[i] < arr[nums[i] - 1]:
		fl = True
		nums[i] = arr[nums[i] - 1]
	elif nums[i] == arr[nums[i] - 1] and fl:
		nums[i] = arr[nums[i] - 1]
	elif fl:
		break
print(*nums, sep='')


from sys import stdin, stdout
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

def it():
	return int(stdin.readline().strip())
from math import ceil
from collections import defaultdict as dd
n = it()
s = input()
l = mp()
v = []
flag = 0
for i in range(n):
	if l[int(s[i]) - 1] > int(s[i]):
		v.append(str(l[int(s[i]) - 1]))
		flag = 1
	elif l[int(s[i]) - 1] == int(s[i]):
		v.append(s[i])
	else:
		v.append(s[i])
		if flag == 1:
			k = i + 1
			break
x = ''.join(v) + s[i + 1:]
print(x)


import sys
input = sys.stdin.readline
from math import log2, log, ceil

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return res

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	while x != link[x]:
		x = link[x]
	return x

def union(x, y, size, link):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
n = int(input())
num = list(input().strip())
fxn = int_array()
fxn.insert(0, -1)
for i in range(n):
	x = int(num[i])
	if fxn[x] > x:
		num[i] = fxn[x]
		for j in range(i + 1, n):
			y = int(num[j])
			if fxn[y] >= y:
				num[j] = fxn[y]
			else:
				break
		break
print(''.join(list(map(str, num))))


n = int(input())
b = str(input())
m = list(map(int, input().split()))
mp = {i: m[i - 1] for i in range(1, 10)}
res = ''
first = True
flag = False
for i in range(0, len(b)):
	if mp[int(b[i])] < int(b[i]):
		if not first:
			flag = True
	elif mp[int(b[i])] != int(b[i]):
		first = False
	if flag or first:
		res += b[i]
	else:
		res += str(mp[int(b[i])])
print(res)


n = int(input())
d = list(input())
d = [int(x) for x in d]
c = [int(x) for x in input().split(' ')]
f = 0
for i in range(n):
	if d[i] < c[d[i] - 1]:
		d[i] = c[d[i] - 1]
		f = 1
	elif f == 1 and d[i] > c[d[i] - 1]:
		break
d = [str(x) for x in d]
print(''.join(d))


n = int(input())
s = str(input())
dict1 = {}
arr = list(map(int, input().split()))
for i in range(1, 10):
	dict1[i] = arr[i - 1]
ans = ''
i = 0
while i < n:
	if dict1[int(s[i])] > int(s[i]):
		break
	else:
		ans += s[i]
		i += 1
while i < n:
	if dict1[int(s[i])] < int(s[i]):
		break
	else:
		ans += str(dict1[int(s[i])])
		i += 1
ans += s[i:]
print(ans)


from collections import Counter, defaultdict
BS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def to_base(s, b):
	res = ''
	while s:
		res += BS[s % b]
		s //= b
	return res[::-1] or '0'
alpha = 'abcdefghijklmnopqrstuvwxyz'
from math import floor, ceil, pi
import sys
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919]
import timeit

def primef(n, plst=[]):
	if n == 1:
		return plst
	else:
		for m in primes:
			if n % m == 0:
				return primef(n // m, plst + [m])
		return primef(1, plst + [n])

def lmii():
	return list(map(int, input().split()))

def ii():
	return int(input())

def countOverlapping(string, sub):
	count = start = 0
	while True:
		start = string.find(sub, start) + 1
		if start > 0:
			count += 1
		else:
			return count
n = ii()
sc = list(map(int, list(input())))
nums = lmii()
ans = set()
flag = False
pos = 0
found = False
while pos < len(sc):
	if sc[pos] < nums[sc[pos] - 1] and (not found) or (sc[pos] <= nums[sc[pos] - 1] and found):
		found = True
		sc[pos] = nums[sc[pos] - 1]
		pos += 1
	elif found:
		break
	else:
		pos += 1
if not flag:
	print(''.join(list(map(str, sc))))


from sys import stdin
input = stdin.readline
n = int(input())
a = list(map(int, list(input().strip())))
f = list(map(int, input().split()))
out = ''
for i in range(len(a)):
	if a[i] < f[a[i] - 1]:
		n = i
		break
for i in range(n, len(a)):
	if a[i] <= f[a[i] - 1]:
		a[i] = f[a[i] - 1]
	else:
		break
for i in a:
	print(i, end='')


n = int(input())
num = list(input())
code = [*map(int, input().split())]
f = 0
ans = ''
for i in range(n):
	if f == 0 and int(num[i]) < code[int(num[i]) - 1]:
		f = 1
	if f == 1 and int(num[i]) > code[int(num[i]) - 1]:
		f = 2
	if f == 1:
		ans += str(code[int(num[i]) - 1])
	else:
		ans += num[i]
print(ans)


n = int(input())
s = list(input())
l = list(map(int, input().split()))
c = 0
while c < n:
	if l[int(s[c]) - 1] <= int(s[c]):
		c += 1
	else:
		break
while c < n:
	if l[int(s[c]) - 1] >= int(s[c]):
		s[c] = str(l[int(s[c]) - 1])
		c += 1
	else:
		break
print(''.join(s))


start = int(input())
a = list(map(int, input()))
f = [0] + list(map(int, input().split()))
for i in range(len(a)):
	if a[i] < f[a[i]]:
		start = i
		a[i] = f[a[i]]
		break
for i in range(start + 1, len(a)):
	if a[i] <= f[a[i]]:
		a[i] = f[a[i]]
	else:
		break
print(*a, sep='')


n = int(input())
a = [*map(int, input())]
f = (0, *map(int, input().split()))
i = 0
while i < n and f[a[i]] <= a[i]:
	i += 1
j = i
while j < n and f[a[j]] >= a[j]:
	a[j] = f[a[j]]
	j += 1
print(*a, sep='')


n = int(input())
s = input()
s = [int(x) for x in s]
func = list(map(int, input().split()))
flag = 0
for i in range(n):
	if s[i] < func[s[i] - 1]:
		s[i] = func[s[i] - 1]
		flag = 1
	elif s[i] > func[s[i] - 1] and flag:
		break
for i in range(n):
	print(s[i], end='')


def main():
	replaced = False
	input()
	number = [int(x) for x in input()]
	replace_map = ['0'] + list(input().split())
	a = 0
	for digit in number:
		candidate = int(replace_map[digit])
		if candidate > digit or (candidate == digit and replaced):
			replaced = True
			number[a] = candidate
		elif replaced:
			break
		a += 1
	for digit in number:
		print(digit, end='')
main()


n = int(input())
s = list(input())
l = [0] + [i for i in input().split()]
st = -1
for i in range(n):
	if s[i] < l[int(s[i])]:
		st = i
		break
if st == -1:
	print(*s, sep='')
	exit()
for i in range(st, n):
	if s[i] > l[int(s[i])]:
		break
	s[i] = l[int(s[i])]
print(*s, sep='')


n = int(input())
s = input()
k = int(s)
a = []
for el in s:
	a.append(el)
arr = list(map(int, input().split()))
max_ = a
for i in range(n):
	if arr[int(s[i]) - 1] > int(s[i]):
		test = a.copy()
		test[i] = arr[int(s[i]) - 1]
		go = 1
		for j in range(i + 1, n):
			if arr[int(s[j]) - 1] < int(s[j]):
				go = 0
			if go == 1:
				test[j] = arr[int(s[j]) - 1]
		max_ = test.copy()
		break
for el in max_:
	print(el, end='')


import sys
input = sys.stdin.readline
n = int(input())
s = list(input().strip())
arr = [0] + list(map(int, input().strip().split()))
start = -1
for i in range(n):
	if int(s[i]) < arr[int(s[i])]:
		start = i
		break
if start == -1:
	exit(print(''.join(s)))
for i in range(start, n):
	if int(s[i]) <= arr[int(s[i])]:
		s[i] = str(arr[int(s[i])])
	else:
		break
print(''.join(s))


N = int(input())
num = input()
F = list(map(int, input().split()))
StF = ''
flag = 0
for i in range(N):
	if flag == 1 and F[int(num[i]) - 1] >= int(num[i]):
		StF += str(F[int(num[i]) - 1])
	elif flag == 2:
		StF += num[i]
	elif flag == 0:
		if F[int(num[i]) - 1] <= int(num[i]):
			StF += num[i]
		else:
			StF += str(F[int(num[i]) - 1])
			flag = 1
	else:
		StF += num[i]
		flag = 2
print(StF)


n = int(input())
s = input()
a = [int(i) for i in input().split()]
k1 = -1
k2 = -1
for i in range(n):
	if int(s[i]) < a[int(s[i]) - 1]:
		k1 = i
		break
for i in range(k1, n):
	if int(s[i]) > a[int(s[i]) - 1]:
		k2 = i
		break
if k1 == -1:
	print(s)
elif k2 == -1:
	print(s[:k1], end='')
	for i in range(k1, n):
		print(a[int(s[i]) - 1], end='')
else:
	print(s[:k1], end='')
	for i in range(k1, k2):
		print(a[int(s[i]) - 1], end='')
	print(s[k2:], end='')


n = int(input())
s = input()
d = input().split()
x = 0
for i in s:
	if d[int(i) - 1] > i:
		break
	x += 1
r = s[:x]
p = x
for i in s[x:]:
	if d[int(i) - 1] >= i:
		r += d[int(i) - 1]
	else:
		r += s[p:]
		break
	p += 1
print(r)


import sys
input = sys.stdin.readline
n = int(input())
s = [int(x) for x in input().strip()]
f = list(map(int, input().split()))
started = False
for i in range(len(s)):
	if f[s[i] - 1] > s[i]:
		started = True
		s[i] = f[s[i] - 1]
	elif started and f[s[i] - 1] < s[i]:
		break
print(''.join([str(x) for x in s]))


n = int(input())
s = input()
cipher = [0] + [int(x) for x in input().split()]
status = 'later'
output = ''
for char in s:
	if status == 'later':
		if cipher[int(char)] > int(char):
			output += str(cipher[int(char)])
			status = 'now'
		else:
			output += char
	elif status == 'now':
		if cipher[int(char)] < int(char):
			output += char
			status = 'done'
		else:
			output += str(cipher[int(char)])
	else:
		output += char
print(output)


n = int(input())
a = list(input())
for i in range(n):
	a[i] = int(a[i])
l = list(map(int, input().split()))
flag = 0
for i in range(n):
	if l[a[i] - 1] > a[i]:
		a[i] = l[a[i] - 1]
		flag = 1
	elif flag == 1 and l[a[i] - 1] == a[i]:
		continue
	elif flag == 0:
		continue
	else:
		break
for i in range(n):
	print(a[i], end='')


import sys
import os
n = int(input())
a = input()
f = list(map(int, input().split()))
ans = ''
pos = 0
while pos < n:
	now = ord(a[pos]) - 48
	if a[pos] >= chr(f[now - 1] + 48):
		ans = ans + a[pos]
		pos += 1
	else:
		while pos < n and a[pos] <= chr(f[now - 1] + 48):
			ans = ans + chr(f[now - 1] + 48)
			pos += 1
			if pos < n:
				now = ord(a[pos]) - 48
		for i in range(pos, n):
			ans = ans + a[i]
		pos = n
		break
print(ans)


n = int(input())
a = list(input())
f = input().split()
i = 0
for i in range(n):
	if a[i] < f[int(a[i]) - 1]:
		a[i] = f[int(a[i]) - 1]
		break
for j in range(i + 1, n):
	if a[j] > f[int(a[j]) - 1]:
		break
	a[j] = f[int(a[j]) - 1]
print(''.join(a))


n = int(input())
a = input()
f = list(map(int, input().split()))
ans = ''
r = 0
fa = 0
for i in a:
	r += 1
	chert = max(int(i), f[int(i) - 1])
	if chert != int(i):
		fa += 1
	ans += str(chert)
	if chert != f[int(i) - 1] and chert == int(i) and (r != len(a)) and (fa > 0):
		ans = ans + a[len(ans):]
		break
print(ans)


x = int(input())
n = [int(c) for c in input()]
a = [int(x) for x in input().split(' ')]
s = False
for i in range(len(n)):
	if a[n[i] - 1] < n[i] and s:
		break
	elif a[n[i] - 1] >= n[i] and s:
		n[i] = a[n[i] - 1]
	elif a[n[i] - 1] > n[i] and (not s):
		n[i] = a[n[i] - 1]
		s = True
	elif a[n[i] - 1] <= n[i] and (not s):
		pass
print(''.join([str(d) for d in n]))


a = int(input())
y = input()
z = list(map(int, input().split()))
k = 0
t = []
flag = 0
for i in y:
	r = int(i)
	t.append(r)
for i in range(len(t)):
	if z[t[i] - 1] > t[i]:
		t[i] = z[t[i] - 1]
		flag = 1
	else:
		if z[t[i] - 1] == t[i]:
			continue
		if flag == 0:
			continue
		else:
			break
print(''.join(map(str, t)))


n = int(input())
y = list(input())
function = '0' + ''.join(input().split())
join = []
replacing = False
stopped = False
for index in range(n):
	rep = function[int(y[index])]
	if replacing:
		if rep >= y[index]:
			join.append(rep)
		else:
			replacing = False
			join.append(y[index])
			stopped = True
	elif rep > y[index] and (not stopped):
		join.append(rep)
		replacing = True
	else:
		join.append(y[index])
print(''.join(join))


t = int(input())
s = list(input())
l = list(map(int, input().split()))
flag = 0
for i in range(len(s)):
	if l[int(s[i]) - 1] > int(s[i]) or (flag == 1 and l[int(s[i]) - 1] >= int(s[i])):
		print(l[int(s[i]) - 1], end='')
		flag = 1
	elif flag == 1:
		print(*s[i:], sep='')
		break
	else:
		print(s[i], end='')


q = int(input())
w = list(map(int, list(input())))
e = list(map(int, input().split()))
y = True
for i in range(q):
	if w[i] == e[w[i] - 1]:
		w[i] = e[w[i] - 1]
	else:
		while w[i] <= e[w[i] - 1] and i < q - 1:
			w[i] = e[w[i] - 1]
			y = False
			i += 1
		if w[i] <= e[w[i] - 1]:
			w[i] = e[w[i] - 1]
			y = False
			i += 1
	if y == True:
		continue
	else:
		break
t = ''
t = t.join(list(map(str, w)))
print(t)


n = int(input())
inputString = input()
arr = [int(x) for x in input().split()]
flag = 0
ans = ''
for c in inputString:
	yo = int(c)
	if flag == 0:
		if yo < arr[yo - 1]:
			ans = ans + str(arr[yo - 1])
			flag = 1
		else:
			ans = ans + str(yo)
	elif flag == 1:
		if yo > arr[yo - 1]:
			ans = ans + str(yo)
			flag = 2
		else:
			ans = ans + str(arr[yo - 1])
	else:
		ans = ans + str(yo)
print(ans)


import sys
if len(sys.argv) >= 2:
	if sys.argv[1] == 'LOCAL':
		sys.stdin = open('input.in', 'r')
n = int(input())
s = input()
l = [int(i) for i in input().split()]
i = 0
while i < n:
	index = int(s[i])
	if l[index - 1] > index:
		break
	print(s[i], end='')
	i += 1
while i < n:
	index = int(s[i])
	if l[index - 1] >= index:
		print(l[index - 1], end='')
	else:
		break
	i += 1
while i < n:
	print(s[i], end='')
	i += 1


n = int(input())
nu = list(map(int, list(input())))
a = [0] + list(map(int, input().split()))
for i in range(n):
	t = nu[i]
	if t < a[t]:
		while i < n and t <= a[t]:
			nu[i] = a[t]
			i += 1
			if i < n:
				t = nu[i]
		break
print(''.join((str(x) for x in nu)))


__author__ = 'Esfandiar'
import sys
input = sys.stdin.readline
n = int(input())
s = input()
f = input().split()
res = ''
start = -1
for i in range(n):
	y = int(s[i]) - 1
	if s[i] < f[y] and (start == -1 or start + 1 == i):
		res += f[y]
		start = i
	else:
		if s[i] == f[y]:
			if start != -1:
				start += 1
		res += s[i]
print(res)


r = lambda : int(input())
ra = lambda : [*map(int, input().split())]
p = lambda a: print('{:.6f}'.format(a))
(a, b) = (r(), input())
c = ra()
b = list(b)
b = [int(i) for i in b]
k = 0
for i in range(a):
	if k > -1:
		if c[b[i] - 1] > b[i] or (c[b[i] - 1] >= b[i] and k == 1):
			b[i] = c[b[i] - 1]
			k = 1
		elif k == 1:
			k = -1
an = ''
while b != []:
	an += str(b.pop())
print(an[::-1])


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

def g(i):
	return f[i - 1]
i = 0
while i < n and a[i] >= g(a[i]):
	i += 1
begin = i
while i < n and a[i] <= g(a[i]):
	i += 1
end = i
for i in range(begin, end):
	a[i] = g(a[i])
print(''.join(map(str, a)))


input()
t = input()
f = '*' + input().replace(' ', '')
s = ''
c = 1
for a in t:
	b = int(a)
	if int(f[b]) > b and c or (int(f[b]) == b and c > 1):
		c += 1
		s += f[b]
	else:
		if c > 1:
			c = 0
		s += a
print(s)


n = int(input())
a = [int(c) for c in input()]
s = [int(c) for c in input().split()]
replacing = False
for i in range(n):
	y = s[a[i] - 1]
	if a[i] < y:
		a[i] = y
		replacing = True
	elif replacing:
		if a[i] != y:
			break
print(''.join((str(x) for x in a)))


import sys
input = sys.stdin.readline

def solve(n, nums, trans):
	flag = False
	for i in range(n):
		digit = nums[i]
		if trans[digit - 1] > digit:
			flag = True
			nums[i] = trans[digit - 1]
		elif trans[digit - 1] < digit and flag:
			break
	return ''.join(map(str, nums))
n = int(input())
nums = [int(x) for x in input().strip()]
trans = list(map(int, input().split()))
print(solve(n, nums, trans))


n = int(input())
s = [int(x) for x in input()]
a = [int(x) for x in input().split()]
a.insert(0, 0)
st = n + 1
for i in range(n):
	if a[s[i]] > s[i]:
		st = i
		break
while st < n and a[s[st]] >= s[st]:
	s[st] = a[s[st]]
	st += 1
print(*s, sep='')


n = int(input())
num = input()
mx = int(num)
val = dict()
vals = input().split()
for i in range(1, 10):
	val[i] = int(vals[i - 1])
flag = False
temp = num
c = 0
ans = ''
fill = False
for i in range(n):
	f = False
	if int(temp[i]) > val[int(temp[i])]:
		if flag:
			fill = True
		else:
			ans = ans + temp[i]
		f = True
	elif int(temp[i]) < val[int(temp[i])] and (not fill):
		flag = True
		c = 1
		f = True
		ans = ans + str(val[int(temp[i])])
	if fill == True or f == False:
		ans = ans + temp[i]
if c == 0:
	print(num)
else:
	print(ans)


n = int(input())
s = list(input())
f = [0] + list(map(int, input().split()))
cok = 1
for i in range(n):
	q = int(s[i])
	if cok:
		if f[q] > q:
			cok = 0
			s[i] = str(f[q])
	elif f[q] >= q:
		s[i] = str(f[q])
	else:
		print(''.join(s))
		exit(0)
print(''.join(s))


n = int(input())
a = list(map(int, list(input())))
f = list(map(int, input().split()))
(l, r) = (-1, 0)
gt = False
for i in range(n):
	d = a[i]
	if f[d - 1] >= d:
		gt |= f[d - 1] > d
		if l < 0:
			l = i
		r += 1
	elif r > 0 and gt:
		break
	else:
		l = -1
		r = 0
for i in range(l, l + r):
	a[i] = f[a[i] - 1]
print(''.join(map(str, a)))


input()
n = [int(i) for i in input()]
li = [int(i) for i in input().split()]
flag = False
for i in range(len(n)):
	if n[i] == li[n[i] - 1] and (not flag):
		pass
	elif n[i] <= li[n[i] - 1]:
		n[i] = li[n[i] - 1]
		flag = True
	elif flag:
		break
print(''.join([str(i) for i in n]))


import sys
import math

def main():
	n = int(sys.stdin.readline().strip())
	s = list(map(int, sys.stdin.readline().strip()))
	f = [0] + list(map(int, sys.stdin.readline().strip().split()))
	i = 0
	while i < n and f[s[i]] <= s[i]:
		i += 1
	while i < n and f[s[i]] >= s[i]:
		s[i] = f[s[i]]
		i += 1
	print(''.join(map(str, s)))
main()


from sys import stdin, stdout
import math
import sys

def target_X():
	n = int(stdin.readline())
	nest = stdin.readline()
	num = [int(x) for x in nest[0:-1]]
	arr = list(map(int, stdin.readline().split()))
	for i in range(0, n):
		store = num[i] - 1
		if num[i] < arr[store]:
			num[i] = arr[store]
			for j in range(i + 1, n):
				store = num[j] - 1
				if num[j] < arr[store]:
					num[j] = arr[store]
				else:
					for x in num:
						print(x, end='')
					return
from sys import stdin, stdout
import math
import sys

def target_X():
	n = int(stdin.readline())
	nest = stdin.readline()
	num = [int(x) for x in nest[0:-1]]
	arr = list(map(int, stdin.readline().split()))
	flag = 0
	for i in range(0, n):
		store = num[i] - 1
		if num[i] < arr[store]:
			num[i] = arr[store]
			for j in range(i + 1, n):
				store = num[j] - 1
				if num[j] <= arr[store]:
					num[j] = arr[store]
				else:
					for x in num:
						print(x, end='')
					flag = 1
					return
			else:
				for x in num:
					print(x, end='')
				flag = 1
				return
		if flag == 1:
			break
	for x in num:
		print(x, end='')
target_X()


n = int(input())
s = [int(i) for i in input()]
f = [0] + list(map(int, input().split()))
l = n
for i in range(n):
	if f[s[i]] > s[i]:
		l = i
		break
for i in range(l, n):
	if f[s[i]] >= s[i]:
		s[i] = f[s[i]]
	else:
		break
print(''.join([str(t) for t in s]))


a = int(input())
s = input()
z = list(map(int, input().split()))
fin = []
for i in range(len(s)):
	if z[int(s[i]) - 1] > int(s[i]):
		fin.append(z[int(s[i]) - 1])
		flag = 0
		j = i + 1
		for j in range(i + 1, len(s)):
			if z[int(s[j]) - 1] >= int(s[j]):
				fin.append(z[int(s[j]) - 1])
			else:
				flag = 1
				break
		if flag == 0:
			j += 1
		for m in range(j, len(s)):
			fin.append(int(s[m]))
		break
	else:
		fin.append(int(s[i]))
print(''.join(map(str, fin)))


n = int(input())
x = list(input())
f = dict(zip(range(1, 10), map(int, input().strip().split())))
for i in range(n):
	p = int(x[i])
	if p < f[p]:
		while i < n and int(x[i]) <= f[int(x[i])]:
			x[i] = str(f[int(x[i])])
			i += 1
		break
print(''.join(x))


import sys

def main():
	n = int(input())
	s = input()
	assert len(s) == n
	digits = list(s)
	m = [0]
	m.extend(list(map(int, input().split())))
	change = False
	for (i, ch) in enumerate(digits):
		d = int(ch)
		if m[d] > d:
			digits[i] = str(m[d])
			change = True
		elif m[d] < d and change:
			break
	print(''.join(digits))
main()


n = int(input())
numstr = input()
funstr = input().split()
N = [int(numstr[i]) for i in range(n)]
f = [0] + [int(funstr[i]) for i in range(9)]
i = 0
while i < n and N[i] >= f[N[i]]:
	i += 1
if i < n:
	j = i + 1
	N[i] = f[N[i]]
	while j < n and N[j] <= f[N[j]]:
		N[j] = f[N[j]]
		j += 1
print(''.join((str(x) for x in N)))


import sys
sys.setrecursionlimit(10 ** 5 + 1)
inf = int(10 ** 20)
max_val = inf
min_val = -inf
RW = lambda : sys.stdin.readline().strip()
RI = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]
lens = RI()
src = [int(x) for x in input()]
maps = [0] + RMI()
i = 0
while i < lens and maps[src[i]] <= src[i]:
	i += 1
j = i
while j < lens and maps[src[j]] >= src[j]:
	src[j] = maps[src[j]]
	j += 1
print(''.join(map(str, src)))


n = int(input())
s = input()
l1 = list(map(int, input().split()))
l2 = list(s)
for i in range(0, n):
	x = int(s[i])
	j = i
	if x < l1[x - 1]:
		while x <= l1[x - 1]:
			l2[j] = l1[x - 1]
			j += 1
			if j == n:
				break
			x = int(s[j])
		break
print(''.join((str(x) for x in l2)))


n = int(input())
a = list(input())
s = list(map(int, input().split()))
s = [0] + s
for i in range(n):
	if s[int(a[i])] > int(a[i]):
		break
for j in range(i, n):
	if s[int(a[j])] < int(a[j]):
		break
	a[j] = str(s[int(a[j])])
print(''.join(a))


n = int(input())
a = [int(s) for s in input()]
t = [int(s) for s in input().split()]
for i in range(n):
	if t[a[i] - 1] > a[i]:
		for j in range(i, n):
			if t[a[j] - 1] >= a[j]:
				a[j] = t[a[j] - 1]
			else:
				break
		break
print(*a, sep='')


n = int(input())
s = input()
s = list(s)
F = list(map(int, input().split()))
d = 0
for i in range(n):
	if F[int(s[i]) - 1] > int(s[i]):
		s[i] = str(F[int(s[i]) - 1])
		d = 1
	elif d == 1 and F[int(s[i]) - 1] < int(s[i]):
		break
for i in s:
	print(i, end='')
print()


def transformNumber(n, s, a):
	i = 0
	flag = 2
	result = []
	while True:
		digit = int(s[i])
		if flag == 2:
			if digit < a[digit - 1]:
				flag = 1
				result.append(str(a[digit - 1]))
			else:
				result.append(s[i])
		elif flag == 1:
			if digit <= a[digit - 1]:
				result.append(str(a[digit - 1]))
			else:
				flag = 0
				result.append(s[i])
		elif flag == 0:
			result.append(s[i])
		i += 1
		if i >= n:
			break
	return ''.join(result)
n = int(input())
s = input()
a = [int(item) for item in input().split(' ')]
print(transformNumber(n, s, a))


input()
num = list(map(int, input()))
sub = list(map(int, input().split()))
swap = [1 if x > i + 1 else -1 if x <= i else 0 for (i, x) in enumerate(sub)]
do_swap = [swap[x - 1] for x in num]
if 1 in do_swap:
	left = do_swap.index(1)
	right = len(num) if -1 not in do_swap[left:] else do_swap[left:].index(-1) + left
	for i in range(left, right):
		num[i] = sub[num[i] - 1]
print(*num, sep='')


k = int(input())
n = list(input())
l = list(map(int, input().split()))
ind = 0
for i in range(k):
	if l[int(n[i]) - 1] > int(n[i]):
		ind = i
		break
while ind < k:
	if l[int(n[ind]) - 1] >= int(n[ind]):
		n[ind] = l[int(n[ind]) - 1]
	else:
		break
	ind = ind + 1
print(*n, sep='')


n = int(input())
s = input()
ls = input().split()
d = {}
for i in range(1, 10):
	d[str(i)] = ls[i - 1]
res = ''
for i in range(n):
	if int(d[s[i]]) > int(s[i]):
		break
	else:
		res += s[i]
else:
	print(res)
	exit()
index = i
for i in range(index, n):
	if int(d[s[i]]) < int(s[i]):
		break
	else:
		res += d[s[i]]
else:
	print(res)
	exit()
index = i
for i in range(index, n):
	res += s[i]
print(res)


n = int(input())
m = input()
b = list(map(int, input().split()))
p = -1
for i in range(0, n):
	k = int(m[i])
	if k < b[k - 1]:
		p = i
		break
if p == -1:
	print(m)
	exit(0)
m = list(m)
for i in range(p, n):
	k = int(m[i])
	if k <= b[k - 1]:
		m[i] = str(b[k - 1])
	else:
		break
ans = ''.join((str(e) for e in m))
print(ans)


n = int(input())
a = list(input())
f = list(map(int, input().split()))
e = 0
for (i, x) in enumerate(a):
	(x, y) = (int(x), f[int(x) - 1])
	if x <= y:
		if x < y:
			e = 1
		a[i] = str(f[x - 1])
	elif e == 1:
		break
print(''.join(a))


num = int(input())
input_a = input()
f_str = input().split()

def max_num(a, coded):
	result = [el for el in a]
	for (index, el) in enumerate(a):
		if coded[int(el) - 1] > el:
			result[index] = coded[int(el) - 1]
			start_index = index + 1
			while start_index < len(a):
				if a[start_index] > coded[int(a[start_index]) - 1]:
					break
				result[start_index] = coded[int(a[start_index]) - 1]
				start_index += 1
			break
	return ''.join(result)
print(max_num(input_a, f_str))


n = int(input())
a = list(input())
f = input().split()
start = False
for (i, c) in enumerate(a):
	d = int(c)
	fd = int(f[d - 1])
	if fd > d:
		if not start:
			start = True
		a[i] = str(fd)
	elif fd < d:
		if start:
			break
print(''.join(a))


n = int(input())
b = list(map(str, input().split()))
x = list(map(int, input().split()))
d = {}
flag = 0
for i in range(9):
	d[i + 1] = x[i]
for k in range(len(b[0])):
	if (flag == 0 or flag == 1) and d[int(b[0][k])] > int(b[0][k]) or (flag == 1 and d[int(b[0][k])] >= int(b[0][k])):
		flag = 1
		print(d[int(b[0][k])], end='')
	else:
		if flag == 1:
			flag = -1
		print(int(b[0][k]), end='')


t = int(input())
s = input()
z = [0] * t
for i in range(t):
	z[i] = int(s[i])
lst = [int(x) for x in input().split(' ')]
started = False
i = 0
while i < t:
	if int(z[i]) < lst[int(z[i]) - 1]:
		z[i] = lst[int(z[i]) - 1]
		started = True
	elif int(z[i]) > lst[int(z[i]) - 1] and started:
		break
	i += 1
d = [str(x) for x in z]
m = ''
for i in d:
	m += i
print(m)


n = int(input())
s = list(map(int, input()))
f = [0] + list(map(int, input().split()))
i = 0
while i < n and f[s[i]] <= s[i]:
	i += 1
while i < n and f[s[i]] >= s[i]:
	s[i] = f[s[i]]
	i += 1
print(''.join(map(str, s)))


n = int(input())
a = [int(i) for i in str(input())]
b = [int(i) for i in input().split()]
idx = 0
while idx < n and a[idx] >= b[a[idx] - 1]:
	idx += 1
while idx < n and a[idx] <= b[a[idx] - 1]:
	a[idx] = b[a[idx] - 1]
	idx += 1
print(''.join(map(str, a)))


from bisect import bisect_right as br
from bisect import bisect_left as bl
from collections import *
from itertools import *
import functools
import sys
from math import *
from decimal import *
getcontext().prec = 30
MAX = sys.maxsize
MAXN = 10 ** 5 + 10
MOD = 10 ** 9 + 7

def isprime(n):
	n = abs(int(n))
	if n < 2:
		return False
	if n == 2:
		return True
	if not n & 1:
		return False
	for x in range(3, int(n ** 0.5) + 1, 2):
		if n % x == 0:
			return False
	return True

def mhd(a, b, x, y):
	return abs(a - x) + abs(b - y)

def numIN(x=' '):
	return map(int, sys.stdin.readline().strip().split(x))

def charIN(x=' '):
	return sys.stdin.readline().strip().split(x)

def arrIN():
	return list(numIN())

def dis(x, y):
	a = y[0] - x[0]
	b = x[1] - y[1]
	return (a * a + b * b) ** 0.5

def lgcd(a):
	g = a[0]
	for i in range(1, len(a)):
		g = math.gcd(g, a[i])
	return g

def ms(a):
	msf = -MAX
	meh = 0
	st = en = be = 0
	for i in range(len(a)):
		meh += a[i]
		if msf < meh:
			msf = meh
			st = be
			en = i
		if meh < 0:
			meh = 0
			be = i + 1
	return (msf, st, en)

def res(ans, t):
	print('Case #{}: {}'.format(t, ans))

def divi(n):
	l = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l += [i, n // i]
	l.sort()
	return l

def lcm(a, b):
	return a * b // gcd(a, b)
n = int(input())
num = int(input())
s = [int(i) for i in str(num)]
f = [0] + arrIN()
fl = 0
for i in range(n):
	if s[i] < f[s[i]]:
		fl = 1
		s[i] = f[s[i]]
	elif s[i] == f[s[i]]:
		continue
	elif fl:
		break
print(''.join([str(i) for i in s]))


n = int(input())
a = input()
sp = list(map(str, input().split()))
new = ''
fl1 = True
fl2 = False
for x in a:
	if int(x) > int(sp[int(x) - 1]) and (not fl1) or fl2:
		fl2 = True
		new += x
	elif int(x) < int(sp[int(x) - 1]):
		new += sp[int(x) - 1]
		fl1 = False
	else:
		new += x
print(new)


n = int(input())
a = list(map(int, list(input())))
f = list(map(int, input().split()))
i = 0
while i < n and f[a[i] - 1] <= a[i]:
	print(a[i], end='')
	i += 1
while i < n and f[a[i] - 1] >= a[i]:
	print(f[a[i] - 1], end='')
	i += 1
while i < n:
	print(a[i], end='')
	i += 1


n = int(input())
t = str(input())
f = str(input()).split(' ')
s = []
c = []
p = ''
for i in t:
	s.append(int(i))
for i in range(n):
	c.append(-(s[i] - int(f[s[i] - 1])))
t = -1
for i in range(n):
	if c[i] > 0:
		break
t = i
if t == -1:
	for i in s:
		p = p + str(i)
	print(p)
else:
	q = n
	b = True
	for i in range(t, n):
		if c[i] < 0:
			b = False
			break
	if b == False:
		q = i
	for i in range(t, q):
		s[i] = int(f[int(s[i] - 1)])
	for i in s:
		p = p + str(i)
	print(p)


n = int(input())
s = input()
l = ['0'] + input().split()
for i in range(n):
	x = s[i]
	if l[int(x)] > x:
		break
c = ''
k = i
while k < n and l[int(s[k])] >= s[k]:
	c += l[int(s[k])]
	k += 1
s = s[:i] + c + s[k:]
print(s)


n = input()
a = input()
replacement = list(map(int, input().split()))
res = []
segment_started = False
segment_finished = False
for (i, digit) in enumerate(a):
	digit = int(digit)
	new_digit = replacement[digit - 1]
	if new_digit > digit and (not segment_finished):
		res.append(new_digit)
		segment_started = True
	else:
		res.append(digit)
		if segment_started and digit > new_digit:
			segment_finished = True
print(''.join(map(str, res)))


def main():
	n = int(input())
	num = list(input())
	f_num = list(input().split(' '))
	start_index = 0
	for i in range(len(num)):
		if int(num[i]) < int(f_num[int(num[i]) - 1]):
			break
		start_index += 1
	for j in range(start_index, len(num)):
		if int(num[j]) > int(f_num[int(num[j]) - 1]):
			break
		num[j] = f_num[int(num[j]) - 1]
	print(''.join(num))
main()


(n, a) = (int(input()), list(input()))
f = [0] + list(input().replace(' ', ''))
logo = True
for i in range(n):
	t = f[int(a[i])]
	if t > a[i]:
		a[i] = t
		logo = False
	elif t == a[i] or logo:
		continue
	else:
		break
print(''.join(a))


n = int(input())
s = list(input())
f = [int(elem) for elem in input().split()]
ind = -1
for i in range(n):
	if f[int(s[i]) - 1] > int(s[i]):
		ind = i
		break
if ind == -1:
	for i in range(n):
		print(s[i], end='')
else:
	x = i
	while x < n and f[int(s[x]) - 1] >= int(s[x]):
		s[x] = f[int(s[x]) - 1]
		x += 1
	for i in range(n):
		print(s[i], end='')


n = int(input())
a = list(map(lambda x: ord(x) - ord('0'), input()))
f = [0] + list(map(int, input().split(' ')))
for i in range(n):
	if f[a[i]] <= a[i]:
		continue
	for j in range(i, n):
		if f[a[j]] < a[j]:
			break
		a[j] = f[a[j]]
	break
print(''.join(map(lambda x: chr(x + ord('0')), a)))


import sys
from bisect import bisect_left, bisect_right, insort
from collections import deque, Counter
from math import gcd, sqrt, factorial, ceil, log10, log2
from itertools import permutations
from heapq import heappush, heappop, heapify
inf = float('inf')
mod = 1000000007

def factorial_p(n, p):
	ans = 1
	if n <= p // 2:
		for i in range(1, n + 1):
			ans = ans * i % p
	else:
		for i in range(1, p - n):
			ans = ans * i % p
		ans = pow(ans, p - 2, p)
		if n % 2 == 0:
			ans = p - ans
	return ans

def nCr_p(n, r, p):
	ans = 1
	while n != 0 or r != 0:
		(a, b) = (n % p, r % p)
		if a < b:
			return 0
		ans = ans * factorial_p(a, p) * pow(factorial_p(b, p), p - 2, p) * pow(factorial_p(a - b, p), p - 2, p) % p
		n //= p
		r //= p
	return ans

def prime_sieve(n):
	flag = n % 6 == 2
	sieve = bytearray((n // 3 + flag >> 3) + 1)
	for i in range(1, int(n ** 0.5) // 3 + 1):
		if not sieve[i >> 3] >> (i & 7) & 1:
			k = 3 * i + 1 | 1
			for j in range(k * k // 3, n // 3 + flag, 2 * k):
				sieve[j >> 3] |= 1 << (j & 7)
			for j in range(k * (k - 2 * (i & 1) + 4) // 3, n // 3 + flag, 2 * k):
				sieve[j >> 3] |= 1 << (j & 7)
	return sieve

def prime_list(n):
	res = []
	if n > 1:
		res.append(2)
	if n > 2:
		res.append(3)
	if n > 4:
		sieve = prime_sieve(n + 1)
		res.extend((3 * i + 1 | 1 for i in range(1, (n + 1) // 3 + (n % 6 == 1)) if not sieve[i >> 3] >> (i & 7) & 1))
	return res

def binary(number):
	result = 0
	while number:
		result = result + 1
		number = number & number - 1
	return result

def is_prime(n):
	if n < 5 or n & 1 == 0 or n % 3 == 0:
		return 2 <= n <= 3
	s = (n - 1 & 1 - n).bit_length() - 1
	d = n >> s
	for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022]:
		p = pow(a, d, n)
		if p == 1 or p == n - 1 or a % n == 0:
			continue
		for _ in range(s):
			p = p * p % n
			if p == n - 1:
				break
		else:
			return False
	return True

def is_inc_row(mat, n, m):
	for i in range(n):
		for j in range(1, m):
			if mat[i][j] <= mat[i][j - 1]:
				return False
	return True

def is_inc_col(mat, n, m):
	for i in range(m):
		for j in range(1, n):
			if mat[j][i] <= mat[j - 1][i]:
				return False
	return True

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
n = int(input())
string = input()
result = []
for i in string:
	result.append(i)
Arr = [0] + get_array()
mydict = {str(i): Arr[i] for i in range(1, 10)}
start = 0
for i in string:
	if mydict[i] > int(i):
		break
	start += 1
length = 1
for i in range(start + 1, n):
	if mydict[string[i]] >= int(string[i]):
		length += 1
	else:
		break
for i in range(n):
	if start <= i < start + length:
		result[i] = str(mydict[result[i]])
print(''.join(result))


import math
n = int(input().lstrip())
number = input().lstrip()
mapping = ['0'] + input().lstrip().split()
start_swap = False
end_swap = False
res = ''
for (i, num) in enumerate(number):
	digit = int(num)
	i += 1
	if not start_swap:
		if mapping[digit] > num:
			start_swap = True
			res += mapping[digit]
		else:
			res += num
	elif not end_swap:
		if mapping[digit] < num:
			end_swap = True
			res += num
		else:
			res += mapping[digit]
	else:
		res += num
print(res)


def process(A, B, n):
	a = len(B) - 1
	for i in range(1, len(B)):
		if B[i] < A[B[i]]:
			a = i
			break
	b = a
	for i in range(a, len(B)):
		if B[i] > A[B[i]]:
			break
		else:
			b = b + 1
	if a != b:
		for i in range(a, b):
			B[i] = A[B[i]]
	for i in range(1, len(B)):
		print(B[i], end='')
n = int(input())
A = [None]
B = [None]
for i in input():
	B.append(int(i))
for i in input().split():
	A.append(int(i))
process(A, B, n)


n = int(input())
stri = list((int(i) for i in input()))
mass = list(map(int, input().split()))
kk = 0
for i in range(n):
	if kk > 0:
		if mass[stri[i] - 1] < stri[i]:
			break
		stri[i] = mass[stri[i] - 1]
	elif stri[i] < mass[stri[i] - 1]:
		stri[i] = mass[stri[i] - 1]
		kk = 1
for i in stri:
	print(str(i), end='')
print()


n = int(input())
s = list(map(int, input()))
l = list(map(int, input().split()))
l = [0] + l
for i in range(n):
	if l[s[i]] > s[i]:
		while i < n and l[s[i]] >= s[i]:
			s[i] = l[s[i]]
			i += 1
		break
print(*s, sep='')


def get_biggest_number(num, mapping):
	segment_start = False
	segment_done = False
	result = list(map(int, num))
	for (idx, digit) in enumerate(num):
		digit = int(digit)
		if mapping[digit - 1] > digit:
			segment_start = True
			result[idx] = mapping[digit - 1]
		elif mapping[digit - 1] == digit:
			continue
		elif segment_start:
			segment_done = True
			break
	return ''.join(map(str, result))

def another(n, num, mapping):
	result = list(map(int, num))
	diff = []
	for d in result:
		diff.append(mapping[d - 1] - d)
	i = 0
	while i < n:
		if diff[i] > 0:
			while i < n and diff[i] >= 0:
				result[i] += diff[i]
				i += 1
			break
		i += 1
	return ''.join(map(str, result))
n = int(input())
num = input()
mapping_list = list(map(int, input().split()))
print(get_biggest_number(num, mapping_list))

def stress_test():
	from random import randint, shuffle
	digits = list(range(1, 10))
	i = 0
	while True:
		num = str(randint(1, 2 * 10 ** 2))
		n = len(num)
		shuffle(digits)
		one = get_biggest_number(num, digits)
		two = another(n, num, digits)
		if one != two:
			print(f'TEST #{i} - FAILED: num - {num}, digits - {digits}. One: {one}, two: {two}')
			break
		else:
			print(f'TEST #{i} - OK')
		i += 1


import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = input().rstrip().decode()
conv = [int(c) for c in input().split()]
res = []
startingtheinterval = -1
for x in range(n):
	o = int(a[x])
	ref = conv[o - 1]
	if ref > o:
		startingtheinterval = x
		break
endingtheinterval = -1
if startingtheinterval == -1:
	startingtheinterval1 = -1
else:
	startingtheinterval1 = startingtheinterval
i = startingtheinterval1
for i in range(startingtheinterval, n):
	if int(a[i]) <= conv[int(a[i]) - 1]:
		endingtheinterval = i
		i += 1
	else:
		break
if endingtheinterval == -1:
	endingtheinterval = n - 1
if startingtheinterval == -1:
	startingtheinterval = 0
for x in range(startingtheinterval):
	res.append(a[x])
for x in range(startingtheinterval, endingtheinterval + 1):
	res.append(str(max(conv[int(a[x]) - 1], int(a[x]))))
for x in range(endingtheinterval + 1, n):
	res.append(a[x])
print(''.join(res))


num = int(input())
n = list(input())
arr = ['0'] + list(map(str, input().split()))
i = 0
while i < num:
	if arr[int(n[i])] > n[i]:
		n[i] = arr[int(n[i])]
		i += 1
		while i < num and arr[int(n[i])] >= n[i]:
			n[i] = arr[int(n[i])]
			i += 1
		break
	i += 1
print(''.join((x for x in n)))


from sys import stdout
(n, a) = (int(input()), input())
f = enumerate([0] + input().split())
f = {str(k): v for (k, v) in f}
(i, tc) = (0, True)
while i < n:
	if tc and a[i] < f[a[i]]:
		while i < n and a[i] <= f[a[i]]:
			stdout.write(f[a[i]])
			i += 1
		tc = False
	else:
		stdout.write(a[i])
		i += 1
print()


n = int(input())
s1 = input()
num = []
ch = [int(i) for i in input().split()]
for i in s1:
	num.append(int(i))
p = 0
for i in range(n):
	if p == 0 and num[i] < ch[num[i] - 1]:
		num[i] = ch[num[i] - 1]
		p = 1
	elif p == 1 and num[i] < ch[num[i] - 1]:
		num[i] = ch[num[i] - 1]
	elif p == 1 and num[i] > ch[num[i] - 1]:
		break
for i in num:
	print(i, end='')


m = int(input())
num = input()
a = list(map(int, input().split(' ')))
n = list(num)
maxn = num
i = 0
start = 0
while i < len(n):
	if n[i] < str(a[int(n[i]) - 1]):
		n[i] = str(a[int(n[i]) - 1])
		start = 1
	elif n[i] > str(a[int(n[i]) - 1]) and start == 1:
		break
	i = i + 1
maxn = ''.join(n)
print(maxn)


import sys
fast_reader = sys.stdin.readline
fast_writer = sys.stdout.write

def input():
	return fast_reader().strip()

def print(*argv):
	fast_writer(' '.join((str(i) for i in argv)))
	fast_writer('\n')
for _ in range(1):
	n = int(input())
	s = list(input())
	for i in range(len(s)):
		s[i] = int(s[i])
	f = list(map(int, input().split()))
	f = [0] + f
	i = 0
	while i < n:
		if s[i] >= f[s[i]]:
			i += 1
		else:
			break
	while i < n:
		if s[i] <= f[s[i]]:
			s[i] = f[s[i]]
			i += 1
		else:
			break
	c = ''
	for i in s:
		c += str(i)
	print(c)


try:
	m = int(input())
	s = input()
	f = input().split()
	t = ''
	i = 0
	while i < m:
		if s[i] >= f[int(s[i]) - 1]:
			t = t + s[i]
			i += 1
		else:
			raise ValueError
	print(t)
except ValueError:
	while i < m:
		if s[i] <= f[int(s[i]) - 1]:
			t = t + f[int(s[i]) - 1]
			i += 1
		else:
			break
	t = t + s[i:m]
	print(t)


N = int(input())
Number = [int(i) for i in input()]
X = list(map(int, input().split()))
(Index, Check) = (0, False)
while Index < N:
	if Number[Index] < X[Number[Index] - 1]:
		Check = True
		while Index < N and Number[Index] <= X[Number[Index] - 1]:
			print(X[Number[Index] - 1], end='')
			Index += 1
	if Check:
		break
	print(Number[Index], end='')
	Index += 1
print(*Number[Index:], sep='')


n = int(input())
s = [int(x) for x in input()]
d = [0] + [int(x) for x in input().split()]
f = 0
for i in range(n):
	if f == 1 and d[s[i]] < s[i]:
		break
	if f < 2 and d[s[i]] > s[i]:
		f = 1
		s[i] = d[s[i]]
print(*(x for x in s), sep='')


n = int(input())
a = list(input())
f = list(map(int, input().split()))
f = {i + 1: f[i] for i in range(9)}
st = n
for i in range(n):
	c = int(a[i])
	if f[c] > c:
		st = i
		break
for i in range(st, n):
	c = int(a[i])
	if f[c] >= c:
		a[i] = str(f[c])
	else:
		break
print(''.join(a))


from sys import stdin, stdout
from itertools import combinations
from collections import defaultdict, OrderedDict
import math

def listIn():
	return list(map(int, stdin.readline().strip().split()))

def stringListIn():
	return [x for x in stdin.readline().split()]

def intIn():
	return int(stdin.readline())

def stringIn():
	return stdin.readline().strip()
n = intIn()
s = stringIn()
li = listIn()
arr = [0] * n
cnt = 0
for i in range(n):
	ss = int(s[i])
	if ss >= li[ss - 1]:
		arr[i] = 0
	else:
		cnt += 1
		arr[i] = 1
	if cnt == 1:
		p1 = i
		break
if cnt == 0:
	print(s)
else:
	cnt = 0
	ans = list(s)
	pos = -1
	for i in range(p1, n):
		ss = int(s[i])
		if ss < li[ss - 1]:
			cnt += 1
			ans[i] = str(li[ss - 1])
		elif cnt >= 1:
			if ss == li[ss - 1]:
				continue
			else:
				break
	print(''.join(ans))


n = int(input())
s = list(map(str, input()))
f = list(map(int, input().split()))
diff = []
for i in range(n):
	diff.append(f[int(s[i]) - 1] - int(s[i]))
i = 0
while i < n:
	if diff[i] > 0:
		while i < n and diff[i] >= 0:
			s[i] = str(f[int(s[i]) - 1])
			i += 1
		break
	i += 1
print(''.join(s))


import math
from collections import defaultdict, Counter, deque

def primeFactor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i ** 2 <= n:
		if n % i == 0:
			return i
		i += 1
	return n

def main():
	n = int(input())
	num = list(input())
	arr = list(map(int, input().split()))
	sub = False
	for i in range(n):
		if not sub:
			if arr[int(num[i]) - 1] > int(num[i]):
				sub = True
				num[i] = str(arr[int(num[i]) - 1])
			else:
				continue
		elif sub and arr[int(num[i]) - 1] >= int(num[i]):
			num[i] = str(arr[int(num[i]) - 1])
		else:
			break
	print(''.join(num))
t = 1
for _ in range(t):
	main()


_ = int(input())
s = str(input())
fun = list(map(int, input().split()))
dic = {}
for i in range(1, 10):
	dic[str(i)] = fun[i - 1]
started = False
s = list(s)
for i in range(len(s)):
	if dic[s[i]] > int(s[i]):
		if not started:
			s[i] = str(dic[s[i]])
			started = True
		else:
			s[i] = str(dic[s[i]])
	elif dic[s[i]] == int(s[i]):
		pass
	elif started:
		break
print(''.join(s))


length = int(input())
num = [int(x) for x in input()]
trans = [0] + list(map(int, input().split()))
flag = False
for i in range(length):
	if trans[num[i]] > num[i]:
		num[i] = trans[num[i]]
		flag = True
	elif flag and trans[num[i]] < num[i]:
		break
print(''.join([str(x) for x in num]))


def main():
	n = int(input())
	s = input()
	s = list(s)
	f = input()
	f = f.split(' ')
	i = 0
	while i < len(s) and f[int(s[i]) - 1] <= s[i]:
		i += 1
	for j in range(i, len(s)):
		if f[int(s[j]) - 1] >= s[j]:
			s[j] = str(f[int(s[j]) - 1])
		else:
			break
	s = ''.join(s)
	print(s)
main()


n = int(input())
s = list(input())
L = list(map(int, input().split()))
i = 0
for i in range(n):
	p = int(s[i])
	if L[p - 1] > p:
		break
for j in range(i, n):
	p = int(s[j])
	if L[p - 1] >= p:
		s[j] = str(L[p - 1])
	else:
		break
print(''.join(s))


import sys
from itertools import accumulate

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
N = INT()
S = list(map(int, input()))
D = [0] + LIST()
started = False
ans = S[:]
for (i, s) in enumerate(S):
	if started and s <= D[s]:
		ans[i] = D[s]
	elif started and s > D[s]:
		break
	elif not started and s < D[s]:
		ans[i] = D[s]
		started = True
ans = list(map(str, ans))
print(''.join(ans))


n = int(input())
a = list(map(int, input()))
f = {i: el for (i, el) in enumerate(map(int, input().split()), 1)}
big_changes_made = False
for (i, el) in enumerate(a):
	if el < f[el]:
		a[i] = f[el]
		big_changes_made = True
	elif el != f[el]:
		if big_changes_made:
			break
print(''.join(map(str, a)))


n = int(input())
s = input()
l = list(s)
l1 = list(map(int, input().split()))
i = 0
k = 0
while i < len(l):
	if int(l[i]) < l1[int(l[i]) - 1]:
		l[i] = l1[int(l[i]) - 1]
		k += 1
		i += 1
	elif k == 0 and int(l[i]) > l1[int(l[i]) - 1]:
		l[i] = int(l[i])
		i += 1
	elif int(l[i]) == l1[int(l[i]) - 1]:
		i += 1
	else:
		break
l = [int(i) for i in l]
p = ''
for i in range(len(l)):
	p += str(l[i])
print(p)


n = int(input())
a = [x for x in input()]
mapping = {}
for (i, x) in enumerate(input().split(), 1):
	mapping[i] = int(x)

def solve():
	for (i, number) in enumerate(a):
		current = int(a[i])
		if mapping[current] > current:
			a[i] = str(mapping[current])
			j = i + 1
			while j < len(a):
				if mapping[int(a[j])] >= int(a[j]):
					a[j] = str(mapping[int(a[j])])
					j += 1
				else:
					break
			return a
	return a
print(''.join(solve()))


ii = lambda : int(input())
mi = lambda : map(int, input().split())
li = lambda : list(mi())
si = lambda : input()
n = ii()
a = list(si())
l = si().split(' ')
d = {str(i): j for (i, j) in zip(range(1, 11), l)}
for i in range(n):
	if a[i] < d[a[i]]:
		break
for j in range(i, n):
	if a[j] > d[a[j]]:
		break
else:
	j = n
for k in range(i, j):
	a[k] = d[a[k]]
print(''.join(a))


n = int(input())
s = input()
a = list(map(int, input().split()))
start = -1
end = -1
for i in range(n):
	indi = ord(s[i]) - ord('1')
	fi = a[indi]
	if fi > indi + 1:
		if start == -1:
			start = i
			end = i
		else:
			end = i
	elif start != -1 and fi >= indi + 1:
		end = i
	elif start != -1:
		break
if start == -1:
	print(s)
	exit(0)
ans = []
for i in range(0, start):
	ans.append(s[i])
for i in range(start, end + 1):
	indi = ord(s[i]) - ord('1')
	fi = a[indi]
	ans.append(fi)
for i in range(end + 1, n):
	ans.append(s[i])
for c in ans:
	print(c, end='')
print()


n = int(input())
a = input()
b = list(map(int, input().split()))
replaceStart = False
output = [a[i] for i in range(n)]
for i in range(n):
	if a[i] < str(b[int(a[i]) - 1]):
		replaceStart = True
		output[i] = str(b[int(a[i]) - 1])
	elif replaceStart and a[i] > str(b[int(a[i]) - 1]):
		output[i] = a[i]
		break
	else:
		output[i] = a[i]
print(''.join(output))


t = int(input())
n = list(str(input()))
x = str(input()).split()
x = [int(i) for i in x]
n = [int(i) for i in n]
count1 = 0
count2 = 0
for (index, num) in enumerate(n):
	if num != 0:
		if x[num - 1] > num and count2 < 1:
			count1 += 1
			n[index] = x[num - 1]
		if count1 > 0 and x[num - 1] < num:
			count2 += 1
n = [str(i) for i in n]
print(''.join(n))


a = input()
a = int(a)
s = input()
mapi = input().split()
t = []
flag = False
c = 0
for i in range(a):
	if s[i] < mapi[int(s[i]) - 1]:
		print(mapi[int(s[i]) - 1], end='')
		flag = True
	elif s[i] == mapi[int(s[i]) - 1]:
		print(s[i], end='')
	elif flag:
		break
	else:
		print(s[i], end='')
	c += 1
for i in range(c, a):
	print(s[i], end='')
print()


import sys
a = int(input())
a = list(input())
c = [int(s) for s in input().split()]
j = ''
for i in range(len(a)):
	r = int(a[i])
	if r < c[r - 1]:
		for j in range(i, len(a)):
			r = int(a[j])
			if r <= c[r - 1]:
				a[j] = str(c[r - 1])
			else:
				print(''.join(a))
				sys.exit()
		print(''.join(a))
		sys.exit()
print(''.join(a))


from sys import stdin, stdout
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
P = lambda x: stdout.write(x)
hg = lambda x, y: (y + x - 1) // x * x
cu = lambda x: max(0, x - 1)
cd = lambda x: min(r - 1, x + 1)
cl = lambda x: max(0, x - 1)
cr = lambda x: min(c - 1, x + 1)
n = I()
a = list(map(int, S()))
f = list(R())
for i in range(n):
	if f[a[i] - 1] > a[i]:
		while i < n and f[a[i] - 1] >= a[i]:
			a[i] = f[a[i] - 1]
			i += 1
		break
print(*a, sep='')


n = int(input())
m = input()
s = list(m)
l = [0] + list(map(int, input().split()))
f = 0
for i in range(n):
	if int(s[i]) > l[int(s[i])]:
		if f == 1:
			break
	elif int(s[i]) < l[int(s[i])]:
		s[i] = str(l[int(s[i])])
		f = 1
print(''.join(s))


n = int(input())
S = input()
L = [int(l) for l in input().split()]
R = str()
cardused = 0
fstarted = 0
k = 0
while cardused == 0 and k < len(S):
	if L[int(S[k]) - 1] < int(S[k]):
		R = R + S[k]
		if fstarted == 1:
			cardused = 1
	elif L[int(S[k]) - 1] > int(S[k]):
		R = R + str(L[int(S[k]) - 1])
		if fstarted == 0:
			fstarted = 1
	elif fstarted == 1:
		R = R + str(L[int(S[k]) - 1])
	else:
		R = R + S[k]
	k += 1
R = R + S[k:]
print(R)


n = int(input())
l = list(input())
a = list(input().split())
for i in range(n):
	if int(l[i]) < int(a[int(l[i]) - 1]):
		for j in range(i, n):
			if int(l[j]) <= int(a[int(l[j]) - 1]):
				l[j] = a[int(l[j]) - 1]
			else:
				break
		break
print(''.join(l))


n = int(input())
a = input()
f = [int(x) for x in input().split()]
r = ''
flag = -1
for c in a:
	k = f[int(c) - 1]
	if flag == -1:
		if k > int(c):
			flag = 0
			r += str(k)
		else:
			r += c
	elif flag == 0:
		if k < int(c):
			r += c
			flag = 1
		else:
			r += str(k)
	else:
		r += c
print(r)


def getpos(start=0):
	if start >= len(num):
		return (None, None)
	flag = False
	i = start
	while i < len(num):
		if int(num[i]) <= int(trans[num[i]]):
			break
		i += 1
	j = i
	while j < len(num):
		if int(num[j]) <= int(trans[num[j]]):
			if int(num[j]) < int(trans[num[j]]):
				flag = True
		else:
			break
		j += 1
	if flag:
		return (i, j)
	else:
		return getpos(j)
dig = int(input())
num = input()
mapform = input().split()
trans = {}
for i in range(9):
	trans.update({str(i + 1): mapform[i]})
(i, j) = getpos()
if i != None:
	num2 = num[:i] + ''.join(list(map(lambda x: trans[x], num[i:j]))) + num[j:]
	print(num2)
else:
	print(num)


import sys
import collections as cc
import bisect as bi
I = lambda : list(map(int, input().split()))
(n,) = I()
s = list(input())
temp = I()
s = [int(i) for i in s]
f = 0
for i in range(n):
	if s[i] < temp[s[i] - 1] and (not f):
		f = 1
		s[i] = temp[s[i] - 1]
	elif f and s[i] <= temp[s[i] - 1]:
		s[i] = temp[s[i] - 1]
	elif f and s[i] > temp[s[i] - 1]:
		f = 0
		break
s = [str(i) for i in s]
print(''.join(s))


n = int(input())
a = [int(i) for i in list(input())]
x = list(map(int, input().split()))
ans = []
flag = -1
for i in range(n):
	if flag == -1 and a[i] < x[a[i] - 1]:
		ans.append(x[a[i] - 1])
		flag = 0
	elif flag == 0 and a[i] <= x[a[i] - 1]:
		ans.append(x[a[i] - 1])
	elif flag == 0:
		ans.append(a[i])
		flag = 1
	else:
		ans.append(a[i])
ans = [str(i) for i in ans]
print(''.join(ans))


digit = int(input())
num = str(input())
n = list(map(int, input().split()))
d = dict()
for (idx, i) in enumerate(n):
	d[idx + 1] = i
ans = []
is_break = False
is_prev_replace = False
for char in num:
	if not is_break:
		ch_num = int(char)
		replace_num = d[ch_num]
		if replace_num > ch_num:
			is_prev_replace = True
			ans.append(str(replace_num))
		elif replace_num == ch_num:
			ans.append(char)
		elif is_prev_replace:
			is_break = True
			ans.append(char)
		else:
			ans.append(char)
	else:
		ans.append(char)
print(*ans, sep='')


import sys
import math
import re
import io

def solve(s, m):
	n = len(s)
	my_list = [0] * n
	for i in range(n):
		my_list[i] = int(s[i])
	first = n
	last = n
	for i in range(n):
		a = int(s[i])
		if first == n:
			if m[a - 1] > a:
				first = i
		elif m[a - 1] < a:
			last = i
			break
	for i in range(first, last):
		a = my_list[i]
		my_list[i] = m[a - 1]
	ans = io.StringIO()
	for a in my_list:
		ans.write(str(a))
	return ans.getvalue()

def main():
	n = int(input())
	s = input()
	m = list(map(int, input().split()))
	ans = solve(s, m)
	print(ans)
main()


n = int(input())
num = list(map(int, list(input())))
func = [int(i) for i in input().split()]
flag = False
for i in range(n):
	if func[num[i] - 1] > num[i]:
		num[i] = func[num[i] - 1]
		flag = True
	elif flag and func[num[i] - 1] < num[i]:
		break
print(''.join([str(i) for i in num]))


_ = input()
s = input()
f = list(map(int, input().split()))
output = ''
out = False
start = 0
end = len(s)
for i in range(len(s)):
	x = int(s[i])
	if x < f[x - 1]:
		start = i
		output += str(f[x - 1])
		for j in range(i + 1, len(s)):
			x = int(s[j])
			if x <= f[x - 1]:
				output += str(f[x - 1])
			else:
				end = j
				break
		out = True
	if out:
		break
if not out:
	print(s)
else:
	print(s[:start] + output + s[end:])


n = int(input())
a = [int(i) for i in input()]
f = [int(i) for i in input().split()]
fl = False
i = 0
while i < n:
	if a[i] < f[a[i] - 1]:
		break
	i += 1
n1 = i
i += 1
while i < n:
	if a[i] > f[a[i] - 1]:
		break
	i += 1
n2 = i
for i in range(n1, min(n2, n)):
	a[i] = f[a[i] - 1]
a = [str(i) for i in a]
print(''.join(a))


n = int(input())
num = [int(x) for x in list(input())]
f = [int(x) for x in input().split()]
flag = 0
for i in range(n):
	if num[i] < f[num[i] - 1]:
		num[i] = f[num[i] - 1]
		flag = 1
	elif num[i] > f[num[i] - 1]:
		if flag == 1:
			break
print(*num, sep='')


n = int(input())
a = []
a = [int(x) for x in input()]
b = []
b = [int(x) for x in input().split()]
c = []
count = 0
for i in range(len(a)):
	if a[i] < b[a[i] - 1] and count == 0:
		c.append(b[a[i] - 1])
		count = 1
	elif a[i] <= b[a[i] - 1] and count == 1:
		c.append(b[a[i] - 1])
		count = 1
	else:
		c.append(a[i])
		if count == 1:
			count = 2
for i in range(n):
	print(c[i], end='')


n = int(input())
a = list(input())
arr = [int(x) for x in input().split()]
d = {}
changed = False
for i in range(9):
	d[i + 1] = arr[i]
for i in range(n):
	if int(a[i]) <= d[int(a[i])]:
		if int(a[i]) != d[int(a[i])]:
			changed = True
		a[i] = str(d[int(a[i])])
	elif changed:
		break
print(''.join(a))


def main():
	replace_started = False
	_ = input()
	number = [int(x) for x in input()]
	replace_map = ['0'] + list(input().split())
	for (i, current) in enumerate(number):
		candidate = int(replace_map[current])
		if candidate > current or (candidate == current and replace_started):
			replace_started = True
			number[i] = candidate
		elif replace_started:
			break
	for digit in number:
		print(digit, end='')
main()


n = int(input())
a = input()
l = list(map(int, input().split()))
s_index = 0
for i in range(n):
	cur = int(a[i])
	if cur < l[cur - 1]:
		s_index = i
		break
ans = a[:s_index]
for i in range(s_index, n):
	cur = int(a[i])
	if cur <= l[cur - 1]:
		ans += str(l[cur - 1])
	else:
		break
ans += a[i:]
ans = ans[:n]
print(ans)


n = int(input())
s = list(input())
lis = list(map(int, input().split()))
lis.insert(0, 0)
f = 0
for i in range(n):
	if int(s[i]) < lis[int(s[i])]:
		f = 1
		s[i] = str(lis[int(s[i])])
	elif f == 1 and int(s[i]) > lis[int(s[i])]:
		break
print(''.join(s))


import sys
import string
from heapq import *
from bisect import *
from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from operator import *
from itertools import accumulate
inf = float('inf')
flush = lambda : sys.stdout.flush
comb = lambda x, y: factorial(x) // factorial(y) // factorial(x - y)
en = lambda x: list(enumerate(x))
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())
n = ii()
x = list(ip())
a = rr()
arr = {str(i): str(j) for (i, j) in enumerate(a, 1)}
pos = n
for (j, i) in enumerate(x):
	if i < arr[i]:
		pos = j
		break
for i in range(pos, n):
	if x[i] > arr[x[i]]:
		break
	x[i] = arr[x[i]]
print(''.join(x))


n = int(input())
a = list(str(input()))
f = [int(x) for x in input().split()]
started = False
goodNums = []
for z in range(9):
	if f[z] >= z + 1:
		goodNums.append(str(z + 1))
mini = n + 0
for y in goodNums:
	if y in a:
		mini = min(mini, a.index(y))
for i in range(mini, n):
	if started:
		if a[i] in goodNums:
			a[i] = str(f[int(a[i]) - 1])
		else:
			break
	elif a[i] in goodNums and a[i] != str(f[int(a[i]) - 1]):
		a[i] = str(f[int(a[i]) - 1])
		started = True
print(''.join(a))


n = int(input())
str_num = input()
str_result = ''
f = [int(x) for x in input().split()]
k_1 = -1
for i in range(0, n):
	if f[int(str_num[i]) - 1] > int(str_num[i]):
		k_1 = i
		break
if k_1 < 0:
	print(str_num)
else:
	k_2 = n - 1
	for i in range(k_1 + 1, n):
		if f[int(str_num[i]) - 1] < int(str_num[i]):
			k_2 = i - 1
			break
	for i in range(0, n):
		if i >= k_1 and i <= k_2:
			print(f[int(str_num[i]) - 1], end='')
		else:
			print(str_num[i], end='')


keyMap = {}

def solve(a):
	a = list(a)
	i = 0
	for i in range(len(a)):
		if keyMap[a[i]] > a[i]:
			break
	if i == len(a):
		print(a)
		return ''.join(a)
	j = i
	while j < len(a):
		if keyMap[a[j]] < a[j]:
			break
		else:
			a[j] = keyMap[a[j]]
			j += 1
	return ''.join(a)
n = int(input())
a = input()
arr = input().split(' ')
for i in range(len(arr)):
	keyMap[str(i + 1)] = arr[i]
print(solve(a))


a = int(input())
b = input()
n = 0
z = list(map(int, input().split()))
for i in b:
	r = int(i)
	if n == 0:
		if r >= z[r - 1]:
			print(r, end='')
		else:
			n = 1
	if n == 1:
		if r <= z[r - 1]:
			print(z[r - 1], end='')
		else:
			n = 2
	if n == 2:
		print(r, end='')


a = int(input())
s = [int(x) for x in input()]
b = [int(x) for x in input().split()]
st = False
for i in range(a):
	if not st:
		if int(s[i]) < b[int(s[i]) - 1]:
			st = True
			s[i] = b[int(s[i]) - 1]
	elif int(s[i]) <= b[int(s[i]) - 1]:
		s[i] = b[int(s[i]) - 1]
	else:
		break
print(''.join([str(x) for x in s]))


l = input()
n = input()
map_str = input()
map_str = map_str.split(' ')
num_change = 0

def f(n_temp, subsegment):
	n_ = [str(x) for x in str(n_temp)]
	for i in subsegment:
		k = int(i)
		n_[k] = map_str[int(n_[k]) - 1]
	return ''.join(n_)

def f1(n_temp):
	n_ = [str(x) for x in str(n_temp)]
	for k in range(0, len(n_temp)):
		n_[k] = map_str[int(n_[k]) - 1]
	return ''.join(n_)

def find_max2(n):
	global num_change
	n__ = [int(x) for x in str(n)]
	l_ = len(n__)
	if n == '':
		return ''
	if l_ == 1:
		return n if int(n) >= int(map_str[int(n) - 1]) else int(map_str[int(n) - 1])
	pivot = -1
	for i in range(0, l_):
		if n__[i] > int(map_str[n__[i] - 1]):
			pivot = i
			break
	if pivot == -1:
		return str(f1(n))
	if pivot == 0:
		k = str(n[0]) if int(n[0]) > int(map_str[int(n[0]) - 1]) else str(map_str[n[0] - 1])
		if k == str(n[0]) and num_change == 0:
			return str(k) + str(find_max2(n[1:]))
		return str(k) + str(n[1:])
	else:
		max_change_value = f1(n[0:pivot])
		if max_change_value == n[0:pivot]:
			if num_change == 1:
				return n
			return n[0:pivot] + find_max2(n[pivot:])
		else:
			num_change = 1
			return max_change_value + find_max2(n[pivot:])
print(find_max2(str(n)))


import sys

class Main:

	def __init__(self):
		self.buff = None
		self.index = 0

	def next(self):
		if self.buff is None or self.index == len(self.buff):
			self.buff = sys.stdin.readline().split()
			self.index = 0
		val = self.buff[self.index]
		self.index += 1
		return val

	def next_int(self):
		return int(self.next())

	def solve(self):
		n = self.next_int()
		s = [int(x) for x in self.next()]
		x = [self.next_int() for _ in range(0, 9)]
		id = 0
		while id < n and s[id] >= x[s[id] - 1]:
			id += 1
		while id < n and s[id] <= x[s[id] - 1]:
			s[id] = x[s[id] - 1]
			id += 1
		print(''.join([str(x) for x in s]))
Main().solve()


n = int(input())
d = input()
s = list()
for i in d:
	s.append(i)
b = input().split()
a = dict()
for i in range(0, 9):
	a[str(i + 1)] = b[i]
for i in range(0, len(s)):
	if s[i] < a[s[i]]:
		while i < len(s) and s[i] <= a[s[i]]:
			s[i] = a[s[i]]
			i += 1
		break
print(''.join(s))


n = int(input())
s = input()
fx = list(map(int, input().split()))
l = []
for i in s:
	l1 = []
	l1.append(int(i))
	l1.append(fx[int(i) - 1])
	l.append(l1)
l1 = ['0'] * n
k = 0
for i in range(len(l)):
	if l[i][1] > l[i][0]:
		l1[i] = str(l[i][1])
		k = i
		break
for i in range(k + 1, len(l)):
	if l[i][1] >= l[i][0]:
		l1[i] = str(l[i][1])
	else:
		break
for i in range(len(l1)):
	if l1[i] == '0':
		l1[i] = str(s[i])
print(''.join(l1))


from io import StringIO

def problem():
	_ = int(input())
	a = input()
	f = input().split()
	res = StringIO()
	changed = False
	counter = 0
	for c in a:
		c1 = f[int(c) - 1]
		if c1 > c:
			res.write(c1)
			changed = True
		else:
			if changed and c1 < c:
				res.write(a[counter:])
				break
			res.write(c)
		counter += 1
	return res.getvalue()
result = problem()
print(result)


num = int(input())
number = input()
arr = list(map(int, input().split()))
flag = 0
for key in range(len(number)):
	h = ord(number[key]) - 48
	let = arr[h - 1]
	if h >= let:
		print(h, end='')
		continue
	else:
		j = key
		flag = 1
		break
if flag == 1:
	for i in range(j, len(number)):
		h = ord(number[i]) - 48
		let = arr[h - 1]
		if let >= h:
			print(let, end='')
			continue
		else:
			k = i
			flag = 2
			break
if flag == 2:
	for nim in range(k, len(number)):
		print(number[nim], end='')


n = int(input())
a = list(input())
d = dict()
s = input().split()
can = set()
for i in range(9):
	if i < int(s[i]):
		can.add(str(i + 1))
	d[str(i + 1)] = s[i]
for i in range(n):
	if a[i] < d[a[i]]:
		a[i] = d[a[i]]
		i += 1
		while i < n and a[i] in can:
			a[i] = d[a[i]]
			i += 1
		break
print(*a, sep='')


n = int(input())
a1 = list(map(int, list(input())))
u = list(map(int, input().split()))
a2 = []
for i in range(n):
	a2.append(u[a1[i] - 1])
ok = False
i1 = -1
for i in range(n):
	if a2[i] > a1[i]:
		i1 = i
		ok = True
		break
if ok:
	i2 = n
	for i in range(i1 + 1, n):
		if a2[i] < a1[i]:
			i2 = i
			break
	for i in range(i1, i2):
		a1[i] = a2[i]
print(''.join(map(str, a1)))


import sys

def rl():
	return sys.stdin.readline()
n = int(rl())
a = list(rl())
a.pop(len(a) - 1)
l = list(map(int, input().split()))
add = False
for ind in range(n):
	if l[int(a[ind]) - 1] > int(a[ind]):
		i = ind
		while i < n:
			if l[int(a[i]) - 1] < int(a[i]):
				break
			a[i] = l[int(a[i]) - 1]
			i += 1
		break
for letter in a:
	print(letter, end='')


a = int(input())
N = list(input())
f = list(map(int, input().split()))
ind = 0
for (i, n) in enumerate(N):
	if int(n) < f[int(n) - 1]:
		N[i] = f[int(n) - 1]
		ind = 1
	if int(n) > f[int(n) - 1] and ind == 1:
		break
print(''.join(list(map(str, N))))


def main():
	n = int(input())
	a = list(map(int, input()))
	f = [0] + list(map(int, input().split()))
	i = 0
	while f[a[i]] <= a[i]:
		i += 1
		if i == n:
			break
	for j in range(i, n):
		if f[a[j]] < a[j]:
			break
		else:
			a[j] = f[a[j]]
	print(''.join((str(x) for x in a)))
	return 0
main()


n = int(input())
s = list(map(int, list(input())))
d = dict(zip(range(1, 10), map(int, input().split())))

def is_greater_equal(digit):
	return d[digit] >= digit
i = 0
ran_once = False
while i < n and (not ran_once):
	if d[s[i]] > s[i]:
		while i < n and is_greater_equal(s[i]):
			ran_once = True
			s[i] = d[s[i]]
			i += 1
	i += 1
print(''.join(map(str, s)))


n = int(input())
c = input()
l = list(map(int, input().split()))
f = False
f1 = True
for i in c:
	x = int(i)
	if l[x - 1] >= x and f1:
		print(l[x - 1], end='')
		if l[x - 1] == x and (not f):
			pass
		else:
			f = True
	else:
		print(x, end='')
		if f:
			f1 = False


n = int(input())
(s, f) = (list(map(int, input())), [0] + list(map(int, input().split())))
i = 0
while i < n and f[s[i]] <= s[i]:
	i += 1
while i < n and f[s[i]] >= s[i]:
	s[i] = f[s[i]]
	i += 1
print(*s, sep='')


n = int(input())
num = input()
f = [0] + list(map(int, input().split()))
apply = False
start = 0
end = -1
for i in range(n):
	a = f[ord(num[i]) - ord('0')]
	b = ord(num[i]) - ord('0')
	if apply:
		if a < b:
			end = i - 1
			break
	elif a > b:
		start = i
		apply = True
if end == -1:
	end = n - 1
if apply:
	for i in range(n):
		if i >= start and i <= end:
			print(f[ord(num[i]) - ord('0')], end='')
		else:
			print(num[i], end='')
else:
	print(num)


import sys
n = int(input())
S = list(input())
P = list(map(int, input().split()))
for i in range(n):
	if P[int(S[i]) - 1] > int(S[i]):
		start = i
		break
else:
	print(''.join(S))
	sys.exit()
for i in range(start, n):
	if P[int(S[i]) - 1] >= int(S[i]):
		S[i] = str(P[int(S[i]) - 1])
	else:
		break
print(''.join(S))


n = int(input())
s = input()
num = []
for item in s:
	num.append(int(item))
func = list(map(int, input().split()))
flag = 0
perma = 0
for item in num:
	if flag == 0:
		if func[item - 1] <= item:
			print(item, end='')
		elif perma != 1:
			print(func[item - 1], end='')
			flag = 1
		else:
			print(item, end='')
			flag = 0
	elif flag == 1:
		if func[item - 1] >= item:
			print(func[item - 1], end='')
		else:
			print(item, end='')
			perma = 1
			flag = 0


def main():
	n = int(input())
	a = list((int(x) for x in input()))
	f = {i + 1: int(x) for (i, x) in enumerate(input().split())}
	once = True
	last = False
	res = []
	for x in a:
		if f[x] > x and once:
			res.append(f[x])
			last = True
		elif once and last and (f[x] == x):
			res.append(f[x])
			last = True
		else:
			res.append(x)
			if last == True:
				once = False
			last = False
	print(''.join(map(str, res)))
main()


n = int(input())
s = input()
f = [0] + [int(x) for x in input().split()]
i = 0
flag = False
ans = ''
while i < n:
	if f[int(s[i])] > int(s[i]):
		ans += str(f[int(s[i])])
		i += 1
		flag = True
		break
	else:
		ans += s[i]
		i += 1
for j in range(i, n):
	if flag:
		if f[int(s[j])] >= int(s[j]):
			ans += str(f[int(s[j])])
		else:
			flag = False
			ans += s[j]
	else:
		ans += s[j]
print(ans)


def main():
	n = int(input())
	number = [int(x) for x in input()]
	replace = [0] + [int(x) for x in input().split()]
	first = next((i for (i, x) in enumerate(number) if replace[x] > x), -1)
	if first != -1:
		for i in range(first, n):
			if replace[number[i]] < number[i]:
				break
			number[i] = replace[number[i]]
	for digit in number:
		print(digit, end='')
main()


n = int(input())
a = list(input())
it = list(map(int, input().split()))
import sys
ok = 0
for i in range(len(a)):
	ch = int(a[i])
	zam = it[ch - 1]
	p = i
	b = 0
	while zam >= ch and b > 0 or (zam > ch and b == 0):
		ok = 1
		while a[p] == str(ch):
			a[p] = str(zam)
			p += 1
			b += 1
			if p == n:
				print(''.join(a))
				sys.exit()
		ch = int(a[p])
		zam = it[ch - 1]
	if ok == 1:
		print(''.join(a))
		sys.exit()
print(''.join(a))


n = int(input())
s = list(input())
array = input().split()

def f(c):
	return array[ord(c) - 49]
i = 0
while i < len(s):
	if f(s[i]) > s[i]:
		while i < len(s) and f(s[i]) >= s[i]:
			s[i] = f(s[i])
			i += 1
		break
	i += 1
print(''.join(s))


n = int(input())
x = list(input())
f = [int(w) for w in input().split()]
ans = ''
t = False
for i in range(n):
	if f[int(x[i]) - 1] > int(x[i]):
		x[i] = str(f[int(x[i]) - 1])
		t = True
	elif t and f[int(x[i]) - 1] < int(x[i]):
		break
print(''.join(x))


n = int(input())
a = input()
a = list(map(lambda x: int(x), a))
idx = {}
for i in zip(range(1, 10), [int(x) for x in input().split()]):
	(k, v) = i
	idx[k] = v - int(k)
ans = ''
f = False
done = False
for j in a:
	if not f:
		if idx[j] > 0 and (not done):
			f = True
			done = True
			ans += f'{idx[j] + j}'
		else:
			ans += str(j)
	elif idx[j] >= 0:
		ans += f'{idx[j] + j}'
	else:
		f = False
		ans += str(j)
print(ans)


from sys import stdin, stdout, maxsize
import math
from copy import deepcopy
from collections import defaultdict
import heapq
n = int(stdin.readline().strip())
string = list(stdin.readline().strip())
string = list(map(int, string))
mymap = list(map(int, stdin.readline().split()))
flag = 0
for i in range(n):
	if flag == 0 and string[i] < mymap[string[i] - 1]:
		string[i] = mymap[string[i] - 1]
		flag = 1
	elif flag == 1:
		if string[i] <= mymap[string[i] - 1]:
			string[i] = mymap[string[i] - 1]
		else:
			break
for i in string:
	print(i, end='')


n = int(input())
s = list(map(int, input()))
f = [0] + list(map(int, input().split()))
state = 0
for i in range(len(s)):
	if state == 0:
		if f[s[i]] > s[i]:
			s[i] = f[s[i]]
			state = 1
	elif f[s[i]] >= s[i]:
		s[i] = f[s[i]]
	else:
		break
print(*s, sep='')


n = int(input())
num = input()
arr = list(map(int, input().split()))
ans = ''
flag = 0
for q in range(n):
	if arr[int(num[q]) - 1] > int(num[q]) and flag == 0:
		flag = 1
		ans += str(arr[int(num[q]) - 1])
	elif arr[int(num[q]) - 1] >= int(num[q]) and flag == 1:
		ans += str(arr[int(num[q]) - 1])
	else:
		if flag == 1:
			flag = 2
		ans += num[q]
print(ans)


n = int(input())
lnum = list(map(int, list(input())))
change = [0] + list(map(int, input().split()))
ans = []
for i in range(n):
	if lnum[i] < change[lnum[i]]:
		for j in range(n - i):
			if lnum[i + j] > change[lnum[i + j]]:
				break
			lnum[i + j] = change[lnum[i + j]]
		break
print(''.join(map(str, lnum)))


count_digits = int(input())
number = input()
FUNCTION = {str(arg): result for (arg, result) in zip(range(1, 10), input().split())}
result = []
start_index = 0
while start_index < count_digits and FUNCTION[number[start_index]] <= number[start_index]:
	start_index += 1
result.append(number[:start_index])
if start_index < count_digits:
	cur_index = start_index
	while cur_index < count_digits and FUNCTION[number[cur_index]] >= number[cur_index]:
		result.append(FUNCTION[number[cur_index]])
		cur_index += 1
	result.append(number[cur_index:])
	print(''.join(result))
else:
	print(number)


import sys
m = int(input())
n = list(input())
l = list(map(int, input().split()))
d = {}
k = sys.maxsize
for i in range(1, 10):
	d[i] = l[i - 1]
for i in range(m):
	if int(n[i]) < d[int(n[i])]:
		k = i
		break
if k < sys.maxsize:
	for i in range(k, m):
		if int(n[i]) < d[int(n[i])]:
			n[i] = str(d[int(n[i])])
		elif int(n[i]) > d[int(n[i])]:
			break
print(''.join(n))


def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
n = ii()
s = si()
s = list(s)
a = li()
m = {}
for i in range(1, 10):
	m[str(i)] = a[i - 1]
f = 0
for i in range(n):
	if f and int(s[i]) > m[s[i]]:
		break
	if int(s[i]) < m[s[i]]:
		s[i] = str(m[s[i]])
		f = 1
print(''.join(s))


def fun(n, f):
	t = False
	o = True
	s = ''
	for i in n:
		if int(i) <= int(f[i]) and t:
			s += str(f[i])
		elif int(i) < int(f[i]) and o:
			s += str(f[i])
			t = True
		else:
			s += i
			if t:
				t = False
				o = False
	return s
a = input()
n = input()
l = input().split()
f = {}
for i in range(1, 10):
	f[str(i)] = l[i - 1]
print(fun(n, f))


n = int(input())
a = list(input())
f = list(map(str, input().split()))
f = {str(i): f[i - 1] for i in range(1, 10)}
past_ind = -1
for i in range(len(a)):
	num = a[i]
	if past_ind == -1:
		if f[num] > num:
			a[i] = f[num]
			past_ind = i
	elif past_ind + 1 == i:
		if f[num] >= num:
			a[i] = f[num]
			past_ind = past_ind + 1
	else:
		break
print(''.join(a))


n = int(input())
a = [*input()]
l = list(map(int, input().split()))
i = 0
for p in range(n):
	a[p] = int(a[p])
while i < n:
	if a[i] < l[a[i] - 1]:
		a[i] = l[a[i] - 1]
		j = i + 1
		while j < n:
			if a[j] <= l[a[j] - 1]:
				a[j] = l[a[j] - 1]
				j += 1
			else:
				break
		break
	else:
		i += 1
ch = ''
for i in range(n):
	ch += str(a[i])
print(ch)


n = int(input())
num = list(map(int, list(input())))
f = list(map(int, input().split()))
f = [0] + f
out = -2
for i in range(n):
	if num[i] < f[num[i]]:
		out = i
		break
if out != -2:
	for i in range(out, n):
		if num[i] <= f[num[i]]:
			num[i] = f[num[i]]
		else:
			break
print(''.join(map(str, num)))


_ = input()
s = list(input())
translation_map = {str(i): (i <= int(x), x) for (i, x) in enumerate(input().split(), 1)}
was_changed = False
for (i, c) in enumerate(s):
	translation_option = translation_map[c]
	if not translation_option[0]:
		if was_changed:
			break
	else:
		if s[i] != translation_option[1]:
			was_changed = True
		s[i] = translation_option[1]
print(''.join(s))


def max_number(arr, f):
	ch = False
	for i in range(len(arr)):
		val = arr[i] - 1
		if ch and f[val] < val + 1:
			return arr
		if f[val] > val + 1:
			arr[i] = f[val]
			ch = True
	return arr

def main():
	n = int(input())
	st = input()
	arr = [0 for i in range(n)]
	for i in range(n):
		arr[i] = int(st[i])
	nx = input().split(' ')
	f = []
	for i in range(9):
		f.append(int(nx[i]))
	out = max_number(arr, f)
	ret = ''
	for v in out:
		ret = ret + str(v)
	print(ret)
main()


__version__ = '0.2'
__date__ = '2019-04-26'
import sys

def solve(a, f):
	mydict = dict(zip(map(str, range(1, 10)), f))
	ans = []
	flag = -1
	for ch in a:
		if mydict[ch] > ch and flag <= 0:
			ans.append(mydict[ch])
			flag = 0
		else:
			ans.append(ch)
			if flag >= 0 and mydict[ch] < ch:
				flag = 1
	return ''.join(ans)

def main(argv=None):
	n = int(input())
	a = list(input())
	f = list(input().split())
	print(solve(a, f))
	return 0
STATUS = main()
sys.exit(STATUS)


n = int(input())
string = input()
lst = list(map(int, input().split()))
pos = -1
for i in range(n):
	x = ord(string[i]) - 48
	if x < lst[x - 1]:
		pos = i
		break
if pos == -1:
	print(string)
else:
	lstrs = []
	for i in range(pos):
		lstrs.append(string[i])
	pos2 = n
	for i in range(pos, n):
		x = ord(string[i]) - 48
		if x > lst[x - 1]:
			pos2 = i
			break
		lstrs.append(str(lst[x - 1]))
	for i in range(pos2, n):
		lstrs.append(string[i])
	print(''.join(lstrs))


n = int(input())
l = list(map(int, input()))
f = list(map(int, input().split()))
out = []
flag = 0
flag2 = 0
for i in l:
	if i <= f[i - 1] and flag2 == 0:
		out.append(f[i - 1])
		if i != f[i - 1]:
			flag = 1
	else:
		out.append(i)
		if flag == 1:
			flag2 = 1
for i in out:
	print(i, end='')
print()


import sys
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *
n = int(input())
a = list(input())
b = list(map(str, input().split()))
d = defaultdict(int)
for (i, s) in enumerate(b):
	d[i + 1] = s
flg = False
for (i, s) in enumerate(a):
	if int(s) < int(d[int(s)]):
		a[i] = d[int(s)]
		flg = True
	elif flg and int(s) > int(d[int(s)]):
		break
print(''.join(a))


import sys
from itertools import dropwhile, takewhile
input = sys.stdin.readline

def main():
	n = int(input())
	a = list(map(int, input().rstrip()))
	f = [0] + list(map(int, input().split()))
	pre = list(dropwhile(lambda x: f[x] <= x, a))
	st = n - len(pre)
	pre = list(takewhile(lambda x: f[x] >= x, pre))
	nd = st + len(pre)
	pre = map(lambda x: f[x], pre)
	a[st:nd] = pre
	print(''.join(map(str, a)))
main()


import sys

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline().strip()

def main():
	n = int(input())
	a = list(input())
	f = get_array()
	flag = False
	for i in range(n):
		x = int(a[i])
		if x < f[x - 1]:
			flag = True
			a[i] = f[x - 1]
		elif flag == True and x > f[x - 1]:
			break
	print(*a, sep='')
main()


sz = input()
n = input()
lst = list(map(int, input().split(' ')))
b = False
str = list(n)
for i in range(len(n)):
	if lst[int(n[i]) - 1] > int(n[i]):
		str[i] = lst[int(n[i]) - 1]
		b = True
	elif lst[int(n[i]) - 1] < int(n[i]) and b:
		break
for e in str:
	print(e, end='')
print()


n = int(input())
l = list(input())
l = list(map(int, l))
l1 = [0] + list(map(int, input().split()))
i = 0
while i < n and l1[l[i]] <= l[i]:
	i += 1
while i < n and l1[l[i]] >= l[i]:
	l[i] = l1[l[i]]
	i += 1
l = list(map(str, l))
print(''.join(l))


n = int(input())
x = input()
a = list(map(int, input().split()))
s = ''
i = 0
while i < n and int(x[i]) >= a[int(x[i]) - 1]:
	i += 1
if i == n and int(x[i - 1]) >= a[int(x[i - 1]) - 1]:
	print(x)
	exit()
s += str(a[int(x[i]) - 1])
j = i
while j < n - 1 and int(x[j + 1]) <= a[int(x[j + 1]) - 1]:
	j += 1
	s += str(a[int(x[j]) - 1])
x = x[:i] + s + x[j + 1:]
print(x)


import sys
from math import *
import threading
from itertools import count
from pprint import pprint
from collections import defaultdict
from heapq import heapify, heappop, heappush
sys.setrecursionlimit(300000)
mod = 10 ** 9 + 7
inf = 10 ** 15
decision = ['NO', 'YES']
yes = 'YES'
no = 'NO'
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def npr(n, r):
	return factorial(n) // factorial(n - r) if n >= r else 0

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(n - r)) if n >= r else 0

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val):
	ans = -1
	lb = 0
	ub = len(li) - 1
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = mid
			break
	return ans

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = max(sum_so_far, current_sum)
	return sum_so_far

def pref(li):
	pref_sum = [0]
	for i in li:
		pref_sum.append(pref_sum[-1] + i)
	return pref_sum

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	li = []
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for p in range(2, len(prime)):
		if prime[p]:
			li.append(p)
	return li

def primefactors(n):
	factors = []
	while n % 2 == 0:
		factors.append(2)
		n //= 2
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0:
			factors.append(i)
			n //= i
	if n > 2:
		factors.append(n)
	return factors

def prod(li):
	ans = 1
	for i in li:
		ans *= i
	return ans

def sumk(a, b):
	print('called for', a, b)
	ans = a * (a + 1) // 2
	ans -= b * (b + 1) // 2
	return ans
karmanya = 0
for _ in range(int(input()) if karmanya else 1):
	n = int(input())
	s = [int(x) for x in input()]
	a = list(map(int, input().split()))
	(l, r) = (-1, -1)
	for i in range(n):
		if s[i] < a[s[i] - 1]:
			l = i
			break
	if l < 0:
		for i in s:
			print(i, end='')
		print('')
	else:
		r = l
		for i in range(l + 1, n):
			if s[i] <= a[s[i] - 1]:
				r = i
			else:
				break
		for i in range(l):
			print(s[i], end='')
		for i in range(l, r + 1):
			print(a[s[i] - 1], end='')
		for i in range(r + 1, n):
			print(s[i], end='')
		print('')


n = int(input())
s = input()
a = input().split()
b = []
started = False
stopped = False
for i in range(n):
	if stopped:
		b.append(s[i])
	else:
		repl = a[ord(s[i]) - ord('1')]
		if s[i] < repl and (not started):
			started = True
		if s[i] > repl and started:
			stopped = True
		b.append(max(s[i], repl))
print(''.join(b))


n = int(input())
a = [*input()]
l = list(map(int, input().split()))
ch = ''
t = 0
ini = 0
for i in range(n):
	if t == 0:
		if ini == 0:
			if int(a[i]) < int(l[int(a[i]) - 1]):
				ch = ch + str(l[int(a[i]) - 1])
				ini = 1
			else:
				ch = ch + a[i]
		elif ini == 1:
			if int(a[i]) <= int(l[int(a[i]) - 1]):
				ch = ch + str(l[int(a[i]) - 1])
			else:
				ch = ch + a[i]
				t = 1
	else:
		ch += a[i]
print(ch)


n = int(input())
st = input()
ar = list(map(int, input().split()))
ct = 0
for j in range(n):
	k = int(st[j])
	i = k - 1
	if ar[i] > k:
		st = st[:j] + str(ar[i]) + st[j + 1:]
		ct = 1
	if ar[i] < k and ct == 1:
		break
print(st)


n = int(input())
num = list(map(int, list(input().strip())))
s = map(int, input().split())
dic = {digit: res for (digit, res) in enumerate(s, 1)}
i = 0
for dig in num:
	if dic[dig] > dig:
		break
	else:
		i += 1
while i < n and dic[num[i]] >= num[i]:
	num[i] = dic[num[i]]
	i += 1
print(''.join(list(map(str, num))))


n = int(input())
a = list(input())
f = [int(x) for x in input().split()]
start = False
for (i, c) in enumerate(a):
	trans = f[int(c) - 1]
	if trans > int(c):
		a[i] = str(trans)
		start = True
	elif trans < int(c):
		if start:
			break
print(''.join(a))


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	n = int(input())
	s = str(input())
	f = list(map(int, input().split()))
	ans = []
	(l, r) = (-1, -1)
	for i in range(len(s)):
		x = ord(s[i]) - ord('1')
		if int(s[i]) < f[x]:
			l = i
			break
		ans.append(s[i])
	if l == -1:
		print(s)
		return
	for i in range(l, len(s)):
		x = ord(s[i]) - ord('1')
		if int(s[i]) > f[x]:
			r = i
			break
		ans.append(f[x])
	t = ''.join([str(x) for x in ans])
	if r == -1:
		print(t)
		return
	print(t + s[r:])
main()


n = int(input())
a = list(input())
f = list(map(int, input().split()))
started = False
for i in range(len(a)):
	cur_d = int(a[i])
	if not started:
		if int(f[cur_d - 1]) > cur_d:
			a[i] = f[cur_d - 1]
			started = True
	elif int(f[cur_d - 1]) < cur_d:
		break
	else:
		a[i] = f[cur_d - 1]
print(''.join(map(str, a)))


digits = int(input())
number = input()
list1 = input().split(' ')
list1 = list(map(int, list1))
ans = ''
flag = 0
for i in number:
	if list1[int(i) - 1] > int(i) and flag == 0:
		flag = 1
		ans = ans + str(list1[int(i) - 1])
	elif flag == 1:
		if list1[int(i) - 1] >= int(i):
			ans = ans + str(list1[int(i) - 1])
		else:
			ans = ans + i
			flag = 2
	else:
		ans = ans + i
print(ans)


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(m,) = I()
n = list(input())
f = [0] + [i for i in input().split()]
ans = int(''.join(n))
for i in range(m):
	if f[int(n[i])] > n[i]:
		while i < m and f[int(n[i])] >= n[i]:
			n[i] = f[int(n[i])]
			i += 1
		break
print(''.join(n))


input()
s = list(map(int, list(input())))
f = [0] + list(map(int, input().split()))
start = -1
end = -1
for i in range(len(s)):
	if f[s[i]] > s[i] and start == -1:
		start = i
	if f[s[i]] < s[i] and start != -1:
		end = i
		break
if start == -1:
	print(''.join(list(map(str, s))))
	exit()
if end == -1:
	end = len(s)
for i in range(start, end):
	s[i] = f[s[i]]
print(''.join(list(map(str, s))))


n = int(input())
s1 = input()
f = [int(x) for x in input().split()]
s2 = ''
i = 1
sign = 0
if f[int(s1[0]) - 1] > int(s1[0]):
	s2 += str(f[int(s1[0]) - 1])
	sign = 1
else:
	s2 += s1[0]
while i < n:
	if sign == 1 and f[int(s1[i]) - 1] >= int(s1[i]):
		s2 += str(f[int(s1[i]) - 1])
	elif sign == 1 and (not f[int(s1[i]) - 1] >= int(s1[i])):
		s2 += s1[i]
		sign = -1
	elif sign == 0 and f[int(s1[i]) - 1] > int(s1[i]):
		s2 += str(f[int(s1[i]) - 1])
		sign = 1
	else:
		s2 += s1[i]
	i += 1
print(s2)


n = int(input())
s = input()
new = list(s)
l = list(map(int, input().split()))
start = -1
for i in range(n):
	if int(s[i]) < l[int(s[i]) - 1]:
		start = i
		break
if start == -1:
	print(s)
else:
	for i in range(start, n):
		if int(s[i]) <= l[int(s[i]) - 1]:
			new[i] = str(l[int(s[i]) - 1])
		else:
			break
	for i in new:
		print(i, end='')
	print()


n = int(input())
a = input()
ok = False
b = list()
for d in a:
	b.append(int(d))
f = list(map(int, input().split()))
for i in range(n):
	m = int(a[i])
	if f[m - 1] > m:
		b[i] = f[m - 1]
		ok = True
	elif ok and f[m - 1] < m:
		break
for i in b:
	print(i, end='')


n = int(input())
arr = [int(x) for x in input().strip()]
funct = [int(x) for x in input().strip().split(' ')]
for i in range(n):
	if arr[i] < funct[arr[i] - 1]:
		while i < n and arr[i] <= funct[arr[i] - 1]:
			arr[i] = funct[arr[i] - 1]
			i += 1
		break
for i in arr:
	print(i, end='')


n = int(input())
s = [int(x) for x in input()]
f = [int(x) for x in input().split(' ')]
f = [0] + f
idx = n
for i in range(n):
	x = s[i]
	if x < f[x]:
		idx = i
		break
for i in range(idx, n):
	x = s[i]
	if x <= f[x]:
		s[i] = f[x]
	else:
		break
print(''.join(map(str, s)))


n = int(input())
a = list(input())
a = [int(i) for i in a]
arr = list(map(int, input().split()))
f = [0 for i in range(10)]
for i in range(10):
	if i != 0:
		f[i] = arr[i - 1]
o = False
for i in range(n):
	el = a[i]
	if el < f[el]:
		o = True
		a[i] = f[el]
	elif el > f[el]:
		if o == True:
			break
print(''.join((str(x) for x in a)))


n = int(input())
s = list(map(int, list(input())))
t = [0] + list(map(int, input().split()))
b = False
for i in range(len(s)):
	if s[i] < t[s[i]]:
		j = i
		while j < len(s) and s[j] <= t[s[j]]:
			s[j] = t[s[j]]
			j += 1
		break
print(*s, sep='')


n = int(input())
s = list(map(int, input()))
l = list(map(int, input().split()))
boo = True
for i in range(n):
	if boo == True:
		if s[i] < l[s[i] - 1]:
			s[i] = l[s[i] - 1]
			boo = False
	elif s[i] <= l[s[i] - 1]:
		s[i] = l[s[i] - 1]
	else:
		break
print(''.join(map(str, s)))


input()
s = [int(c) for c in input()]
m = {i + 1: j for (i, j) in enumerate(map(int, input().split()))}
f = next((i for i in range(len(s)) if s[i] < m[s[i]]), None)
if f is None:
	print(''.join(map(str, s)))
	exit(0)
s[f] = m[s[f]]
f += 1
while f < len(s) and s[f] <= m[s[f]]:
	s[f] = m[int(s[f])]
	f += 1
print(''.join(map(str, s)))


n = int(input())
st = list(map(int, list(input())))
li = list(map(int, input().split()))
lis = []
a = n
b = n
for i in range(n):
	if st[i] < li[st[i] - 1]:
		a = i
		b = i
		while st[b] <= li[st[b] - 1]:
			lis.append(li[st[b] - 1])
			b = b + 1
			if b == n:
				break
		break
print(''.join(list(map(str, st[0:a] + lis + st[b:]))))


n = int(input())
a = list(input())
a = [int(x) for x in a]
d = {}
replacements = list(map(int, input().split()))
for i in range(9):
	d[i + 1] = replacements[i]
b = [d[x] for x in a]
ans = ''
flag = False
i = 0
j = 0
k = 0
for i in range(len(a)):
	if a[i] >= b[i]:
		ans += str(a[i])
	else:
		flag = True
		break
if flag:
	flag = False
	for j in range(i, len(a)):
		if b[j] >= a[j]:
			ans += str(b[j])
		else:
			flag = True
			break
if flag:
	for k in range(j, len(a)):
		ans += str(a[k])
print(ans)


n = int(input())
a = input()
f = [int(x) for x in input().split()]
j = -1
for i in range(n):
	if f[int(a[i]) - 1] > int(a[i]):
		j = i
		break
	print(a[i], end='')
if j != -1:
	while j < n and f[int(a[j]) - 1] >= int(a[j]):
		print(f[int(a[j]) - 1], end='')
		j += 1
	for i in range(j, n):
		print(a[i], end='')


d = int(input())
b = input()
a = list(input().split())
c = []
i = 0
f = 0
while i < d:
	k = int(b[i])
	if int(a[k - 1]) > k:
		f = 1
		c.append(a[k - 1])
		i = i + 1
		continue
	if int(a[k - 1]) >= k and f == 1:
		c.append(a[k - 1])
		i = i + 1
		continue
	if f == 1:
		break
	c.append(b[i])
	i = i + 1
while i < d:
	c.append(b[i])
	i = i + 1
i = 0
while i < d:
	print(c[i], end='')
	i = i + 1


n = int(input())
s = input()
l = list(map(int, input().split()))
(start, end) = (None, None)
for i in range(0, n):
	if int(s[i]) < l[int(s[i]) - 1]:
		start = i
		end = i
		for j in range(i + 1, n):
			if int(s[j]) > l[int(s[j]) - 1]:
				break
			end = j
		break
if start is not None:
	print(s[:start], end='')
	for i in range(start, end + 1):
		print(l[int(s[i]) - 1], end='')
	print(s[end + 1:])
else:
	print(s)


l = int(input())
s = input()
s = list(s)
f = map(int, input().split())
f = [0] + list(f)
for i in range(l):
	d = int(s[i])
	if f[d] > d:
		for j in range(i, l):
			d1 = int(s[j])
			if f[d1] < d1:
				break
			s[j] = str(f[d1])
		break
print(''.join(s))


n = int(input())
num = input()
f = list(map(int, input().split()))
final = []
started = False
finished = False
for j in num:
	i = int(j)
	if not finished and (f[i - 1] > i and (not started) or (f[i - 1] >= i and started)):
		started = True
		final.append(f[i - 1])
	elif started and (not finished):
		finished = True
		final.append(i)
	elif not started:
		final.append(i)
	elif finished:
		final.append(i)
for i in final:
	print(i, end='')
print('')


n = int(input())
k = list(input())
l = list(map(int, input().split()))
p = -1
count = -1
flag = 0
for i in range(n):
	z = int(k[i])
	if l[z - 1] >= z and flag == 1:
		k[i] = l[z - 1]
		count += 1
	if l[z - 1] > z and flag == 0:
		k[i] = l[z - 1]
		count = i
		flag = 1
	if count != i and flag == 1:
		break
for i in range(n):
	print(k[i], end='')


n = int(input())
a = [int(a) for a in list(str(input()))]
f = [int(b) for b in str(input()).split(' ')]
r = list(a)
started = False
for i in range(n):
	ai = a[i]
	fi = f[ai - 1]
	if fi >= ai:
		r[i] = fi
		if fi > ai:
			started = True
	elif started:
		break
print(''.join(map(lambda i: str(i), r)))


import sys
input = sys.stdin.readline
import math
t = int(input())
n = input()
l = input().split()
d = {}
for i in range(9):
	if int(l[i]) > i + 1:
		d[str(i + 1)] = 1
	elif int(l[i]) == i + 1:
		d[str(i + 1)] = 2
	else:
		d[str(i + 1)] = 0
c = 0
for i in range(t):
	if d[n[i]] == 1:
		ans = l[int(n[i]) - 1]
		start = i
		j = i + 1
		c = 1
		while j < t:
			if d[n[j]] == 0:
				break
			else:
				ans += l[int(n[j]) - 1]
			j += 1
	if c == 1:
		if j == t:
			maxima = n[0:start] + ans
		else:
			maxima = n[0:start] + ans + n[j:]
		break
	else:
		maxima = n
print(maxima)


n = int(input())
a = input()
f = [int(x) for x in input().split()]
res = ''
have_changed = False
done_changing = False
for i in range(n):
	c = int(a[i])
	x = f[c - 1]
	if not done_changing and (have_changed and x >= c or x > c):
		res += str(x)
		have_changed = True
		continue
	elif have_changed:
		done_changing = True
	res += str(c)
print(res)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(a, b):
	ans = 1
	while b > 0:
		if b % 2 == 1:
			ans *= a
		a *= a
		b //= 2
	return ans

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve():
	n = int(inp())
	s = list(inp())
	a = lis()
	a.insert(0, 0)
	for i in range(n):
		s[i] = int(s[i])
	i = 0
	f = False
	j = -1
	ind = [-1, -1]
	while i < n:
		if s[i] >= a[s[i]]:
			i += 1
			continue
		else:
			j = i
			temp = []
			while j < n and s[j] <= a[s[j]]:
				temp.append(a[s[j]])
				j += 1
			ans = s[:i] + temp + s[j:]
			print(''.join((str(ans[p]) for p in range(len(ans)))))
			return
	print(''.join((str(s[p]) for p in range(len(s)))))
testcase(1)


n = int(input())
a = list(input())
F = dict(zip('123456789', input().split()))
i = 0
while i < n and a[i] >= F[a[i]]:
	i += 1
j = i
while j < n and a[j] <= F[a[j]]:
	j += 1
for k in range(i, j):
	a[k] = F[a[k]]
print(''.join(a))


n = int(input())
a = list(map(int, input()))
x = list(map(int, input().split()))
i = 0
while i < n and x[a[i] - 1] <= a[i]:
	i += 1
while i < n and x[a[i] - 1] >= a[i]:
	a[i] = x[a[i] - 1]
	i += 1
print(*a, sep='')


def mp():
	return map(int, input().split())
n = int(input())
a = [int(i) for i in input()]
f = list(mp())
idx = -1
for i in range(n):
	if f[a[i] - 1] > a[i]:
		idx = i
		break
if idx != -1:
	i = idx
	while i < n and f[a[i] - 1] >= a[i]:
		a[i] = f[a[i] - 1]
		i += 1
print(''.join(map(str, a)))


def main():
	input()
	number = [int(x) for x in input()]
	f = (0, *input().split())
	c = 0
	a = 0
	for b in number:
		n = int(f[b])
		if n > b or (n == b and c):
			c = 1
			number[a] = n
		elif c:
			break
		a += 1
	print(*number, sep='')
main()


n = int(input())
s = input()
f = list(map(int, input().split()))
res = ''
chng = 0
for i in range(n):
	if f[int(s[i]) - 1] > int(s[i]):
		res += str(f[int(s[i]) - 1])
		chng = 1
	elif f[int(s[i]) - 1] < int(s[i]):
		res += s[i]
		if chng == 1:
			break
	else:
		res += s[i]
res += s[i + 1:]
print(res)


n = int(input())
s = list(input())
k = 0
lis = list(map(int, input().split()))
for i in range(len(s)):
	if int(s[i]) <= lis[int(s[i]) - 1]:
		if int(s[i]) == lis[int(s[i]) - 1]:
			s[i] = lis[int(s[i]) - 1]
		else:
			k = 1
			s[i] = lis[int(s[i]) - 1]
	elif k == 1:
		break
for i in s:
	print(i, end='')


n = int(input())
x = list(input())
a = list(map(int, input().split()))
a = {str(i + 1): str(a[i]) for i in range(9)}
f = False
for i in range(n):
	if x[i] < a[x[i]]:
		for j in range(i, n):
			if x[j] <= a[x[j]]:
				x[j] = a[x[j]]
			else:
				break
		break
print(''.join(x))


from sys import stdin, stdout

def main():
	n = int(stdin.readline().strip())
	A = [int(num) for num in stdin.readline().strip()]
	B = [0] + [int(num) for num in stdin.readline().strip().split()]
	c_A = [str(num) for num in A]
	start = False
	outputs = []
	for (j, i) in enumerate(A):
		if B[i] - i > 0:
			if start:
				pass
			else:
				start = True
			c_A[j] = str(B[i])
		elif B[i] - i < 0:
			if start:
				break
			else:
				continue
		else:
			continue
	stdout.write(''.join(c_A))
main()


l = int(input())
n = input()
F = list(map(int, input().split()))
Comp = []
for i in n:
	if int(i) > F[int(i) - 1]:
		Comp.append(0)
	else:
		Comp.append(1)
start = 0
end = 0
for i in range(l):
	if Comp[i] == 1 and start == 0 and (int(n[i]) < F[int(n[i]) - 1]):
		start = i
		break
	elif i == l - 1:
		start = -1
if start == -1:
	print(n)
else:
	for i in range(start, l):
		if Comp[i] == 0 and end == 0:
			end = i
			break
		elif i == l - 1:
			end = l
	N = [i for i in n]
	for i in range(start, end):
		N[i] = F[int(N[i]) - 1]
	print(''.join((str(n) for n in N)))


import sys

def gao(n, s, f):
	diff = [f[i] - i - 1 for i in range(9)]
	if max(diff) <= 0:
		return s
	ret = [c for c in s]
	flag = 0
	for i in range(n):
		c = int(s[i])
		if flag == 0:
			if f[c - 1] > c:
				flag = 1
				ret[i] = str(f[c - 1])
		elif flag == 1:
			if f[c - 1] >= c:
				ret[i] = str(f[c - 1])
			else:
				break
	return ''.join(ret)

def main():
	lines = [sys.stdin.readline(), sys.stdin.readline(), sys.stdin.readline()]
	lines = [l.strip(' \r\n') for l in lines]
	n = int(lines[0])
	s = lines[1]
	f = [int(v) for v in lines[2].split(' ')]
	print(gao(n, s, f))
main()


n = int(input())
a = list(input())
f = input().split()
ch = 0
for k in range(n):
	m1 = a[k]
	m2 = f[int(m1) - 1]
	if m1 >= m2:
		if ch != 0 and m1 != m2:
			break
	else:
		ch = 1
		a[k] = m2
print(''.join(a))


n = int(input())
m = [int(x) for x in input()]
f = [0] + [int(x) for x in input().split(' ')]
used_digits = [False for i in range(10)]
used = [False for i in range(n)]
start = False
for i in range(n):
	if f[m[i]] > m[i]:
		start = True
		used[i] = True
	elif f[m[i]] < m[i] and start:
		break
ans = []
for i in range(n):
	if used[i]:
		ans.append(str(f[m[i]]))
	else:
		ans.append(str(m[i]))
print(''.join(ans))


n = int(input())
l = list(input())
val = list(map(str, input().split()))
mark = 0
for i in range(n):
	if val[int(l[i]) - 1] >= l[i]:
		if val[int(l[i]) - 1] > l[i]:
			mark += 1
		l[i] = val[int(l[i]) - 1]
	elif mark != 0:
		break
print(''.join(l))


n = int(input())
a = input()
start = 0
f = list(input().split())
for i in range(n):
	if a[i] < f[int(a[i]) - 1]:
		start = i
		break
j = start
end = n
while j < n:
	if a[j] > f[int(a[j]) - 1]:
		end = j
		break
	j += 1
ans = a[0:start]
for i in range(start, end):
	ans = ans + f[int(a[i]) - 1]
if end < n:
	ans = ans + a[end:]
print(ans)


n = int(input())
num = input()
f = [0] + list(map(int, input().split()))
right = 0
left = 0
for i in range(n):
	if f[int(num[i])] > int(num[i]):
		right = left = i
		while f[int(num[right])] >= int(num[right]):
			right += 1
			if right >= n:
				break
		break
newnum = list(map(int, list(num)))
for i in range(left, right):
	newnum[i] = f[newnum[i]]
print(''.join(list(map(str, newnum))))


from sys import stdin, stdout
for _ in range(1):
	n = int(stdin.readline())
	s = input()
	done = changed = 0
	a = [0] + list(map(int, stdin.readline().split()))
	ans = list(s)
	for i in range(n):
		cur = int(s[i])
		if not done:
			if cur < a[cur]:
				ans[i] = str(a[cur])
				changed = 1
			elif changed and cur > a[cur]:
				done = 1
	print(''.join(ans))


n = int(input())
a = list(map(int, list(input())))
alt = list(map(int, input().split()))
ans = 0
l = -1
r = -1
i = 0
while i < n:
	if alt[a[i] - 1] < a[i]:
		if l != -1:
			break
		i += 1
		continue
	if alt[a[i] - 1] == a[i] and l == -1:
		i += 1
		continue
	if l == -1:
		l = i
		r = i
	else:
		r = i
	i += 1
if l == -1:
	print(''.join(map(str, a)))
else:
	while l <= r:
		a[l] = alt[a[l] - 1]
		l += 1
	print(''.join(map(str, a)))


x = int(input())
y = input()
z = list(map(int, input().split()))

def f(x):
	global z
	return z[int(x) - 1]
k = False
for i in range(x):
	if int(y[i]) >= f(y[i]):
		if k == True and int(y[i]) > f(y[i]):
			print(y[i:])
			break
		print(y[i], end='')
	else:
		k = True
		print(f(y[i]), end='')


import math
T = int(input())
a = str(input())
f = list(map(int, input().split()))
d = []
flag = 0
for i in range(T):
	k = int(a[i])
	if f[k - 1] > k and flag < 2:
		d.append(f[k - 1])
		flag = 1
	elif f[k - 1] == k:
		d.append(f[k - 1])
	else:
		if flag == 1:
			flag = 2
		d.append(k)
for i in d:
	print(i, end='')


n = int(input())
a = list(input())
t = input().split()
f = {}
for i in range(9):
	f[str(i + 1)] = t[i]
del t
i = 0
while True:
	if i + 1 > n:
		print(''.join(a))
		break
	if f[a[i]] > a[i]:
		j = i
		while True:
			if j + 1 > n:
				break
			if f[a[j]] >= a[j]:
				a[j] = f[a[j]]
			else:
				break
			j += 1
		print(''.join(a))
		break
	i += 1


n = int(input())
s = list(map(int, list(input())))
l = list(map(int, input().split()))
flag = False
for i in range(n):
	if s[i] < l[s[i] - 1]:
		s[i] = l[s[i] - 1]
		flag = True
	elif flag and s[i] > l[s[i] - 1]:
		break
print(*s, sep='')


n = int(input())
a = list(input())
f = list(map(int, input().split()))
for j in range(len(a)):
	if f[int(a[j]) - 1] > int(a[j]):
		break
for i in range(j, len(a)):
	if f[int(a[i]) - 1] >= int(a[i]):
		a[i] = str(f[int(a[i]) - 1])
	else:
		break
print(''.join(a))


n = int(input())
s = input()
ans = ''
l = list(map(int, input().split()))
flag = 0
for i in range(n):
	if flag == 0:
		if l[int(s[i]) - 1] > int(s[i]):
			ans += str(l[int(s[i]) - 1])
			flag = 1
		else:
			ans += str(s[i])
	elif flag == 1:
		if l[int(s[i]) - 1] >= int(s[i]):
			ans += str(l[int(s[i]) - 1])
		else:
			ans += str(s[i])
			flag = 2
	elif flag == 2:
		ans += str(s[i])
print(ans)


n = int(input())
str = input()
value = []
strCopy = list(str)
value = list(map(int, input().split()))
count = 0
condition = True
for i in range(0, len(strCopy)):
	if int(strCopy[i]) == value[int(strCopy[i]) - 1]:
		continue
	if int(strCopy[i]) < value[int(strCopy[i]) - 1]:
		strCopy[i] = value[int(strCopy[i]) - 1]
		condition = False
	elif condition == False and int(strCopy[i]) > value[int(strCopy[i]) - 1]:
		count = 1
	if count == 1:
		break
for i in range(0, len(strCopy)):
	print(strCopy[i], end='')


import sys
from collections import defaultdict
strInp = lambda : input().strip().split()
intInp = lambda : list(map(int, strInp()))
n = int(input())
a = list(input())
arr = strInp()
digitMap = {}
j = 1
for i in arr:
	digitMap[j] = i
	j += 1
init = False
for i in range(len(a)):
	if int(a[i]) > int(digitMap[int(a[i])]) and init:
		break
	if int(a[i]) < int(digitMap[int(a[i])]):
		init = True
		a[i] = digitMap[int(a[i])]
print(''.join(a))


n = int(input())
s = input()
f = [int(x) for x in input().split()]
ptr1 = len(s) + 1
temp = 0
for i in range(0, len(s)):
	if f[int(s[i]) - 1] > int(s[i]):
		ptr1 = i
		temp = 1
		break
ptr2 = len(s)
for i in range(ptr1 + 1, len(s)):
	if f[int(s[i]) - 1] < int(s[i]):
		ptr2 = i
		break
if temp == 0:
	ptr1 = 0
	ptr2 = 0
L = []
for i in range(0, ptr1):
	L.append(s[i])
for i in range(ptr1, ptr2):
	L.append(str(f[int(s[i]) - 1]))
for i in range(ptr2, len(s)):
	L.append(s[i])
L = ''.join(L)
print(L)


n = int(input())
arr = list(map(int, input()))
image = list(map(int, input().split()))
flag = False
for i in range(n):
	if flag == False:
		if arr[i] < image[arr[i] - 1]:
			arr[i] = image[arr[i] - 1]
			flag = True
	elif arr[i] <= image[arr[i] - 1]:
		arr[i] = image[arr[i] - 1]
	else:
		break
print(''.join(map(str, arr)))


N = int(input())
num = input()
dic = {}
line = [int(x) for x in input().split()]
for k in range(1, 10):
	dic[str(k)] = line[k - 1]
size = len(num)
ret = []
s = 0
while s < size and dic[num[s]] <= int(num[s]):
	ret.append(num[s])
	s += 1
while s < size and dic[num[s]] >= int(num[s]):
	ret.append(str(dic[num[s]]))
	s += 1
while s < size:
	ret.append(num[s])
	s += 1
print(''.join(ret))


import sys
input = sys.stdin.readline

def main():
	n = int(input())
	l = list(map(int, list(input().strip())))
	f = [0] + list(map(int, input().split()))
	i = 0
	while i < n and l[i] >= f[l[i]]:
		i += 1
	while i < n and f[l[i]] >= l[i]:
		l[i] = f[l[i]]
		i += 1
	print(''.join(map(str, l)))
main()


from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))

def solve():
	n = ri()
	digits = list(map(int, list(input())))
	mapping = [0] + rl()
	first_i = -1
	for i in range(n):
		if digits[i] < mapping[digits[i]]:
			first_i = i
			break
	if first_i >= 0:
		for i in range(first_i, n):
			if digits[i] <= mapping[digits[i]]:
				digits[i] = mapping[digits[i]]
			else:
				break
	for d in digits:
		print(d, end='')
	print()
mode = 's'
if mode == 'T':
	t = ri()
	for i in range(t):
		solve()
else:
	solve()


n = int(input())
string = input()
key = input().split()
for x in range(9):
	key[x] = int(key[x])
bo = True
for x in range(n):
	cur = int(string[x])
	if cur < key[cur - 1]:
		bo = False
		break
if bo:
	print(string)
else:
	end = True
	for y in range(x + 1, n):
		cur = int(string[y])
		if cur > key[cur - 1]:
			end = False
			break
	print(string[:x], end='')
	if end:
		for z in range(x, n):
			cur = int(string[z])
			print(key[cur - 1], end='')
	else:
		for z in range(x, y):
			cur = int(string[z])
			print(key[cur - 1], end='')
		print(string[y:])


n = int(input())
arr = list(input())
array = list(map(int, input().split()))
has_started = False
for (i, c) in enumerate(arr):
	b = int(c)
	f_b = int(array[b - 1])
	if f_b > b:
		if not has_started:
			has_started = True
		arr[i] = str(f_b)
	elif f_b < b:
		if has_started:
			break
print(''.join(arr))


import sys
n = int(input())
a = list(input().rstrip())
f = [0] + list(map(int, input().split()))
flg = False
for i in range(n):
	ai = int(a[i])
	if flg:
		if f[ai] >= ai:
			a[i] = str(f[ai])
		else:
			break
	if f[ai] > ai:
		a[i] = str(f[ai])
		flg = True
print(''.join(a))


n = int(input())
s = input()
d = {}
li = []
flag = 0
for i in s:
	li.append(int(i))
a = list(map(int, input().split()))
for i in range(len(a)):
	d[i + 1] = a[i]
for i in range(len(li)):
	if li[i] < d[li[i]]:
		li[i] = d[li[i]]
		flag = 1
	elif li[i] > d[li[i]] and flag == 1:
		break
for i in range(len(li)):
	print(li[i], end='')


len1 = eval(input())
num = input()
f = input()
a = f.split(' ')
f = ['0']
for i in a:
	f.append(i)
max = int(num)
ans = ''
begin = 0
for i in range(0, len1):
	if int(f[int(num[i])]) > int(num[i]):
		begin = i
		ans += num[0:i]
		break
for x in range(begin, len1):
	if int(f[int(num[x])]) >= int(num[x]):
		ans += f[int(num[x])]
	else:
		ans += num[x:]
		break
print(ans)


n = int(input())
s = input()
s = [ord(i) - ord('0') for i in s]
l = list(map(int, input().split()))
i = 0
while i < n and l[s[i] - 1] <= s[i]:
	i += 1
while i < n and l[s[i] - 1] >= s[i]:
	s[i] = l[s[i] - 1]
	i += 1
print(*s, sep='')


input()
a = input()
fs = input().split(' ')
b = ''
start_mapping = False
end_mapping = False
for c in a:
	mapped = fs[ord(c) - ord('1')]
	if mapped > c and (not end_mapping):
		b += mapped
		start_mapping = True
	elif mapped == c:
		b += c
	else:
		b += c
		if start_mapping:
			end_mapping = True
print(b)


n = int(input())
s = list(input())
a = list(map(int, input().split()))
c = []
for i in range(len(s)):
	if int(s[i]) <= a[int(s[i]) - 1]:
		c.append(i)
z = []
p = []
k = 0
for i in range(len(c)):
	if int(s[c[i]]) != a[int(s[c[i]]) - 1]:
		k = i
		break
c = c[i:]
for i in range(len(c) - 1):
	if c[i + 1] - c[i] == 1:
		z.append(c[i])
	else:
		z.append(c[i])
		p.append(z)
		z = []
try:
	if c[-1] - c[-2] == 1:
		z.append(c[-1])
		p.append(z)
	else:
		p.append([c[-1]])
except:
	if len(c) > 0:
		p.append([c[-1]])
p = sorted(p)
if len(p) > 0:
	for i in range(len(p[0])):
		s[p[0][i]] = str(a[int(s[p[0][i]]) - 1])
print(''.join(s))


n = int(input())
s = list(input())
f = [0] + list(map(int, input().split()))
k = 0
j = 0
for i in range(n):
	if f[int(s[i])] > int(s[i]):
		k = i
		j = i + 1
		while j < n and f[int(s[j])] >= int(s[j]):
			j += 1
		break
for i in range(k, j):
	s[i] = str(f[int(s[i])])
print(*s, sep='')


x = int(input())
n = list(input())
a = list(map(int, input().split()))
f = 0
for i in range(x):
	if a[int(n[i]) - 1] > int(n[i]):
		n[i] = str(a[int(n[i]) - 1])
		f = 1
	elif f == 1 and a[int(n[i]) - 1] < int(n[i]):
		break
print(''.join(n))


N = int(input())
number = list(map(int, list(input())))
f = list(map(int, input().split()))
for i in range(N):
	if f[number[i] - 1] > number[i]:
		while i < N and f[number[i] - 1] >= number[i]:
			number[i] = f[number[i] - 1]
			i += 1
		break
print(''.join(map(str, number)))


import sys
from sys import stdin, stdout
import bisect
import math
import heapq

def modinv(n, p):
	return pow(n, p - 2, p)

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
import math

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l

def main():
	n = inin()
	s = sin()
	s = list(s)
	a = ain()
	k = 0
	for i in range(n):
		j = int(s[i])
		if a[j - 1] > j:
			k = 1
			s[i] = a[j - 1]
		elif a[j - 1] < j:
			if k == 1:
				break
	print(*s, sep='')
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def readnumbers(zero=0):
	conv = ord if py2 else lambda x: x
	A = []
	numb = zero
	sign = 1
	i = 0
	s = sys.stdin.buffer.read()
	try:
		while True:
			if s[i] >= b'0'[0]:
				numb = 10 * numb + conv(s[i]) - 48
			elif s[i] == b'-'[0]:
				sign = -1
			elif s[i] != b'\r'[0]:
				A.append(sign * numb)
				numb = zero
				sign = 1
			i += 1
	except:
		pass
	if s and s[-1] >= b'0'[0]:
		A.append(sign * numb)
	return A
main()


input()
s = input()
mapping = {i: v for (i, v) in enumerate([int(i) for i in input().split(' ')])}
res = []
start = False
end = False
for c in s:
	n = int(c)
	if mapping[n - 1] >= n and (not end):
		if not start and mapping[n - 1] > n:
			start = True
		res.append(str(mapping[n - 1]))
	else:
		if start:
			end = True
		res.append(c)
print(''.join(res))


def main(number, function):
	el = 0
	while el < len(number):
		if number[el] < function[int(number[el]) - 1]:
			while el < len(number) and number[el] <= function[int(number[el]) - 1]:
				print(function[int(number[el]) - 1], end='')
				el += 1
			print(number[el:])
			return
		print(number[el], end='')
		el += 1
digits = int(input())
number = input().strip()
function = input().strip().split()
main(number, function)


def main():
	n = int(input())
	num = list(map(int, list(input())))
	a = list(map(int, input().split()))
	stop = True
	s = True
	for i in range(n):
		elem = num[i]
		if s and stop and (a[elem - 1] > elem):
			stop = False
			print(a[elem - 1], end='')
		elif s and stop == False and (elem > a[elem - 1]):
			s = False
			print(elem, end='')
		elif s and stop == False:
			print(a[elem - 1], end='')
		else:
			print(elem, end='')
main()


def get_biggest_number(num, mapping):
	segment_start = False
	segment_done = False
	result = list(map(int, num))
	for (idx, digit) in enumerate(num):
		digit = int(digit)
		if mapping[digit - 1] > digit:
			segment_start = True
			result[idx] = mapping[digit - 1]
		elif segment_start:
			segment_done = True
			break
	return ''.join(map(str, result))

def another(n, num, mapping):
	result = list(map(int, num))
	diff = []
	for d in result:
		diff.append(mapping[d - 1] - d)
	i = 0
	while i < n:
		if diff[i] > 0:
			while i < n and diff[i] >= 0:
				result[i] += diff[i]
				i += 1
			break
		i += 1
	return ''.join(map(str, result))
n = int(input())
num = input()
mapping_list = list(map(int, input().split()))
print(another(n, num, mapping_list))


n = int(input())
s = list(map(int, input()))
a = list(map(int, input().split()))
first = 0
for (i, c) in enumerate(s):
	if first:
		break
	if a[c - 1] > c:
		s[i] = a[c - 1]
		j = i + 1
		first = 1
		while j < n and s[j] <= a[s[j] - 1]:
			s[j] = a[s[j] - 1]
			j += 1
print(''.join(map(str, s)))


n = int(input())
d = [x for x in input().strip()]
f = [-1] + [int(x) for x in input().split(' ')]
b = False
for (i, e) in enumerate(d):
	e = int(e)
	if not b:
		if f[e] > e:
			d[i] = f[e]
			b = True
	elif f[e] >= e:
		d[i] = f[e]
		b = True
	elif b:
		break
print(''.join([str(x) for x in d]))


l = int(input())
n = input()
tab = list(map(int, input().split()))
md = ''
first = -1
end = -1
for i in range(l):
	if int(n[i]) < tab[int(n[i]) - 1]:
		first = i
		end = i
		k = end
		md = str(tab[int(n[i]) - 1])
		for j in range(k + 1, l):
			if int(n[j]) <= tab[int(n[j]) - 1]:
				md += str(tab[int(n[j]) - 1])
				end += 1
			else:
				break
		break
if first == -1:
	print(n)
else:
	print(n[0:first] + md + n[end + 1:])


n = int(input())
tt = input()
t = list(tt)
a = list(map(int, input().split()))
flg = False
st = 0
gl = 0
for i in range(n):
	if a[int(t[i]) - 1] > int(t[i]):
		t[i] = str(a[int(t[i]) - 1])
		flg = True
	elif int(t[i]) == a[int(t[i]) - 1] or flg == False:
		continue
	else:
		break
print(''.join(t))


digits = int(input())
number = list(map(int, input()))
functions = list(map(int, input().split()))
functions = [0] + functions
for i in range(digits):
	if functions[number[i]] > number[i]:
		while i < digits and functions[number[i]] >= number[i]:
			number[i] = functions[number[i]]
			i += 1
		break
print(''.join(map(str, number)))


n = int(input())
a = input()
l = list(map(int, input().split(' ')))
b = True
i = 0
j = n
while i != n:
	x = int(a[i])
	if x < l[x - 1]:
		j = i
		while j != n:
			x = int(a[j])
			if x > l[x - 1]:
				break
			else:
				j += 1
		break
	i += 1
if i == 0:
	ch = ''
else:
	ch = a[0:i]
for k in a[i:j]:
	x = int(k)
	ch = ch + str(l[x - 1])
ch = ch + a[j:n]
print(ch)


n = int(input())
a = input()
f = ['']
f += [int(i) for i in input().split()]
started = 0
stopped = 0
for i in a:
	i = int(i)
	if f[i] < i or stopped:
		print(i, end='')
		if started:
			stopped = 1
	elif f[i] == i:
		print(i, end='')
	else:
		started = 1
		print(f[i], end='')


import sys

def trans(n, sw):
	res = []
	for x in n:
		res.append(x)
	check = False
	for i in range(0, len(res)):
		l = sw[int(res[i]) - 1]
		if l > res[i] and (check == False or check == True):
			res[i] = l
			check = True
		elif l < res[i] and check == True:
			break
	return ''.join(res)
p = sys.stdin.readline().strip()
n = sys.stdin.readline().strip()
sw = sys.stdin.readline().strip().split(' ')
print(trans(n, sw))


n = int(input())
s = list(input())
a = list(map(int, input().split()))
for i in range(n):
	k = int(s[i])
	if k < a[k - 1]:
		s[i] = str(a[k - 1])
		j = i + 1
		while j < n and int(s[j]) <= a[int(s[j]) - 1]:
			s[j] = str(a[int(s[j]) - 1])
			j += 1
		break
print(''.join(s))


n = int(input())
s = list(input())
a = [0] + list(map(int, input().split()))
in_process = False
for i in range(0, n):
	num = int(s[i])
	if a[num] > num:
		in_process = True
		s[i] = a[num]
	if in_process and a[num] < num:
		break
for x in s:
	print(x, end='')


n = int(input())
(s, f) = (list(map(int, input())), [0] + list(map(int, input().split())))
st = 0
while st < n and f[s[st]] <= s[st]:
	st += 1
fn = st
while fn < n and f[s[fn]] >= s[fn]:
	fn += 1
s[st:fn] = [f[s[i]] for i in range(st, fn)]
print(*s, sep='')


def f(x):
	x += 1
	while x % 10 == 0:
		x = x // 10
	return x
n = int(input())
s = input()
f = list(map(int, input().split()))
lastPos = -1
firstPos = False
for i in range(n):
	x = int(s[i])
	if f[x - 1] >= x:
		if f[x - 1] > x:
			firstPos = True
		print(f[x - 1], end='')
	elif firstPos:
		lastPos = i
		break
	else:
		print(x, end='')
if lastPos >= 0:
	print(s[lastPos:])


n = int(input())
s = list(input())
f = [0]
for c in input().split():
	f.append(int(c))
diff = [0] * n
for i in range(n):
	dig = ord(s[i]) - ord('0')
	diff[i] = f[dig] - dig
for i in range(n):
	if diff[i] > 0:
		j = i
		while j < n and diff[j] >= 0:
			s[j] = chr(ord('0') + f[ord(s[j]) - ord('0')])
			j += 1
		break
print(''.join(s))


input()
t = [*map(int, input())]
f = (0, *input().split())
c = 0
a = 0
for b in t:
	n = int(f[b])
	if n > b or (n == b and c):
		c = 1
		t[a] = n
	elif c:
		break
	a += 1
print(*t, sep='')


import sys
input = sys.stdin.readline
n = int(input())
st = input()
ar = list(map(int, input().split()))
ct = 0
ans = str()
for j in range(n):
	k = int(st[j])
	i = k - 1
	if ar[i] > k:
		ans += str(ar[i])
		ct = 1
	elif ar[i] < k and ct == 1:
		ans += st[j:]
		break
	else:
		ans += st[j]
print(ans)


n = int(input())
a = [int(x) for x in list(input())]
l = [int(x) for x in input().split()]
l.insert(0, -1)
s = 0
e = n
ans = ''
for i in range(n):
	if l[a[i]] > a[i]:
		s = i
		break
for i in range(s, n):
	if l[a[i]] < a[i]:
		e = i
		break
for i in range(n):
	if i < s or i >= e:
		ans += str(a[i])
	else:
		ans += str(l[a[i]])
print(ans)


n = int(input())
a = list(input())
for i in range(len(a)):
	a[i] = int(a[i])
f = [int(x) for x in input().split()]
diff = []
for i in range(len(a)):
	diff.append(f[int(a[i]) - 1] - int(a[i]))
for i in range(len(a)):
	if diff[i] > 0:
		while i < n and diff[i] >= 0:
			a[i] = int(a[i]) + diff[i]
			i += 1
		break
for i in a:
	print(i, end='')


def longnumber(n, m):
	found = False
	i = 0
	while not found and i < len(n):
		if m[n[i]] > n[i]:
			found = True
			while i < len(n) and m[n[i]] >= n[i]:
				n[i] = m[n[i]]
				i += 1
		i += 1
	print(''.join(n))
n = int(input())
inp = list(input().strip(' '))
mp = input().strip(' ').split(' ')
num_map = {}
for i in range(len(mp)):
	num_map[str(i + 1)] = mp[i]
longnumber(inp, num_map)


n = int(input())
d = [0] + [int(x) for x in input()]
f = [0] + [int(x) for x in input().split()]
flag = False
for i in range(1, n + 1):
	if f[d[i]] > d[i]:
		d[i] = f[d[i]]
		flag = True
	elif flag and d[i] > f[d[i]]:
		break
print(*d[1:], sep='')


from math import *
from collections import *
import sys
sys.setrecursionlimit(10 ** 9)
n = int(input())
s = list(input())
l = list(map(int, input().split()))
ind = n
for i in range(n):
	s[i] = int(s[i])
	if ind == n and s[i] < l[s[i] - 1]:
		ind = i
for i in range(ind, n):
	if s[i] <= l[s[i] - 1]:
		s[i] = l[s[i] - 1]
	else:
		break
for i in s:
	print(i, end='')
print()


from sys import stdin
input = stdin.readline
from collections import defaultdict
n = int(input())
s = list(input().strip())
t = list(map(int, input().split()))
t = [0] + t
dd = defaultdict(int)
tt = defaultdict(str)
for i in range(1, 10):
	if t[i] > i:
		dd[str(i)] = 2
		tt[str(i)] = str(t[i])
	elif t[i] == i:
		dd[str(i)] = 1
		tt[str(i)] = str(t[i])
i = 0
flag = True
while i < n and flag:
	if dd[s[i]] == 2:
		flag = False
		while i < n and dd[s[i]] > 0:
			s[i] = tt[s[i]]
			i += 1
	else:
		i += 1
print(''.join(s))


input()
nums = input()
corres = list(map(int, input().split()))
result = ''
i = 0
while i < len(nums) and int(nums[i]) >= corres[int(nums[i]) - 1]:
	result += nums[i]
	i += 1
while i < len(nums) and int(nums[i]) <= corres[int(nums[i]) - 1]:
	result += str(corres[int(nums[i]) - 1])
	i += 1
while i < len(nums):
	result += nums[i]
	i += 1
print(result)


a = int(input())
n = input()
n = [int(x) for x in list(n)]
f = input().split(' ')
f = list(map(int, f))
check = 0
for i in range(0, a):
	if f[n[i] - 1] > n[i]:
		n[i] = f[n[i] - 1]
		check = 1
	elif f[n[i] - 1] < n[i]:
		if check == 1:
			break
print(*n, sep='')


mod = 10 ** 9 + 7
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
n = ii()
l = dgl()
d = dict()
ind = -1
for (i, j) in zip(range(1, 11), il()):
	d[i] = j
for i in range(n):
	if l[i] < d[l[i]]:
		ind = i
		break
for i in range(ind, n):
	if l[i] <= d[l[i]]:
		l[i] = d[l[i]]
	else:
		break
print(''.join((str(i) for i in l)))


n = int(input())
m = list(input())
d = list(input().split())
f = False
for i in range(n):
	if not f:
		if m[i] < str(d[int(m[i]) - 1]):
			m[i] = str(d[int(m[i]) - 1])
			f = True
	elif m[i] > str(d[int(m[i]) - 1]):
		break
	else:
		m[i] = str(d[int(m[i]) - 1])
print(''.join(m))


n = int(input())
c = input()
fl = False
codes = input().split()
for i in range(n):
	if fl and int(c[i]) <= int(codes[int(c[i]) - 1]) or (not fl and int(c[i]) < int(codes[int(c[i]) - 1])):
		fl = True
		print(codes[int(c[i]) - 1], end='')
	elif not fl:
		print(c[i], end='')
	else:
		print(c[i:])
		exit()


n = int(input())
s = input()
f = list(map(int, input().split()))
result = []
flag = 1
for i in range(len(s)):
	x = f[int(s[i]) - 1]
	if x > int(s[i]):
		result.append(str(x))
		if flag == 1:
			flag = 0
	else:
		result.append(s[i])
		if flag != 1 and x < int(s[i]):
			break
for j in range(i + 1, len(s)):
	result.append(s[j])
l = len(result)
r = int(''.join(result))
print(r)


n = int(input())
x = list(input().strip())
mapping = list(input().split(' '))
tmp = eq = 0
ans = []
for (idx, i) in enumerate(x):
	if i <= mapping[int(i) - 1]:
		ans.append(mapping[int(i) - 1])
		tmp += 1
		if i == mapping[int(i) - 1]:
			eq += 1
	else:
		ans.append(i)
		if tmp != 0 and tmp != eq:
			break
		else:
			tmp = eq = 0
ans += x[len(ans):]
ans = ''.join(ans)
print(ans)


from sys import stdin
input = stdin.readline
n = int(input())
w = input().rstrip()
f = [0] + list(map(int, input().split()))
(l, r) = (0, n)
changed = ''
for (i, c) in enumerate(w):
	if int(c) < f[int(c)]:
		l = i
		changed += str(f[int(c)])
		for j in range(i + 1, n):
			if int(w[j]) > f[int(w[j])]:
				r = j
				break
			changed += str(f[int(w[j])])
		break
if not changed:
	print(w)
else:
	print(w[:l] + changed + w[r:])


n = int(input())
s = input()
s = list(s)
a = [int(o) for o in input().split()]
i = 0
flag = 0
while i < n and flag == 0:
	if a[int(s[i]) - 1] > int(s[i]):
		flag = 1
		while i < n and a[int(s[i]) - 1] >= int(s[i]):
			s[i] = str(a[int(s[i]) - 1])
			i += 1
	i += 1
print(''.join(s))


def get():
	n = int(input())
	a = input()
	keys = ('1', '2', '3', '4', '5', '6', '7', '8', '9')
	values = input().split()
	f = dict(zip(keys, values))
	return (n, a, f)

def first_greater(a, f):
	for (i, d) in enumerate(a):
		if f[d] > d:
			return i
	else:
		return i + 1

def main():
	(n, a, f) = get()
	fg = first_greater(a, f)
	if fg == len(a):
		print(a)
	else:
		bef = a[:fg]
		mid = ''
		aft = ''
		for (i, d) in enumerate(a[fg:], start=fg):
			if f[d] >= d:
				mid += f[d]
			else:
				aft = a[i:]
				break
		print(bef, mid, aft, sep='')
main()


n = int(input())
a = list(map(int, input()))
f = list(map(int, input().rstrip().split()))
seg_start = False
for i in range(0, n):
	if f[a[i] - 1] > a[i]:
		seg_start = True
	if f[a[i] - 1] >= a[i] and seg_start:
		a[i] = f[a[i] - 1]
	elif seg_start:
		break
print(''.join(map(str, a)))


n = int(input())
s = input()
l = []
for i in s:
	l.append(int(i))
f = list(map(int, input().split()))
ch = 0
c = 0
for i in range(0, len(l)):
	if f[l[i] - 1] > l[i]:
		l[i] = f[l[i] - 1]
		ch = 1
		continue
	if ch == 1 and f[l[i] - 1] < l[i]:
		break
for i in l:
	print(i, end='')


def main():
	n = int(input())
	s = input()
	vals = list(map(int, input().split()))
	ans = ''
	index = -1
	flag = False
	for i in range(n):
		if vals[int(s[i]) - 1] > int(s[i]):
			ans += str(vals[int(s[i]) - 1])
			flag = True
		else:
			if flag:
				if vals[int(s[i]) - 1] < int(s[i]):
					index = i
					break
			ans += s[i]
	if index != -1:
		for i in range(index, n):
			ans += s[i]
	print(ans)
main()


from sys import stdin, stdout
import bisect
import math
from collections import deque
mod = 10 ** 9 + 7

def st():
	return list(stdin.readline().strip())

def inp():
	return int(stdin.readline())

def li():
	return list(map(int, stdin.readline().split()))

def mp():
	return map(int, stdin.readline().split())

def pr(n):
	stdout.write(str(n) + '\n')

def DFS(dictionary, vertex, visited):
	visited[vertex] = True
	stack = [vertex]
	print(vertex)
	while stack:
		a = stack.pop()
		for i in dictionary[a]:
			if not visited[i]:
				print(i)
				visited[i] = True
				stack.append(i)

def BFS(dictionary, vertex, visited):
	visited[vertex] = True
	q = deque()
	q.append(vertex)
	while q:
		a = q.popleft()
		for i in dictionary[a]:
			if not visited[i]:
				visited[i] = True
				q.append(i)
				print(i)

def soe(limit):
	l = [1] * (limit + 1)
	l[0] = 0
	l[1] = 0
	prime = []
	for i in range(2, limit + 1):
		if l[i]:
			for j in range(i * i, limit + 1, i):
				l[j] = 0
	for i in range(2, limit + 1):
		if l[i]:
			prime.append(i)
	return prime

def segsoe(low, high):
	limit = int(high ** 0.5) + 1
	prime = soe(limit)
	n = high - low + 1
	l = [0] * (n + 1)
	for i in range(len(prime)):
		lowlimit = low // prime[i] * prime[i]
		if lowlimit < low:
			lowlimit += prime[i]
		if lowlimit == prime[i]:
			lowlimit += prime[i]
		for j in range(lowlimit, high + 1, prime[i]):
			l[j - low] = 1
	for i in range(low, high + 1):
		if not l[i - low]:
			if i != 1:
				print(i)

def gcd(a, b):
	while b:
		a = a % b
		(b, a) = (a, b)
	return a

def power(a, n):
	r = 1
	while n:
		if n & 1:
			r = r * a
		a *= a
		n = n >> 1
	return r

def solve():
	n = inp()
	s = list(input())
	l = li()
	l = [0] + l
	i = 0
	while i < n and int(s[i]) >= l[int(s[i])]:
		i += 1
	while i < n and int(s[i]) <= l[int(s[i])]:
		s[i] = str(l[int(s[i])])
		i += 1
	print(''.join(s))
for _ in range(1):
	solve()


n = int(input())
s = input()
arr = list(map(int, input().strip().split()))
ans = ''
fl = 0
for i in range(len(s)):
	x = int(s[i])
	if fl == 0:
		if x < arr[x - 1]:
			ans += str(arr[x - 1])
			fl = 1
		else:
			ans += str(x)
	else:
		if fl == 2:
			ans += s[i]
			continue
		if x <= arr[x - 1]:
			ans += str(arr[x - 1])
		else:
			fl = 2
			ans += s[i]
print(ans)


def main():
	n = int(input())
	m = input()
	f = [0] + list(map(int, input().split()))
	ans = list(map(int, m))
	ans.append(0)
	i = 0
	while ans[i] >= f[ans[i]] and i < n:
		i += 1
	while ans[i] <= f[ans[i]] and i < n:
		ans[i] = f[ans[i]]
		i += 1
	for i in range(n):
		print(ans[i], end='')
main()


n = int(input().strip())
num = input().strip()
f = [0] + [int(i) for i in input().strip().split()]
ans = []
consec = True
have_op = False
for i in range(n):
	j = int(num[i])
	if f[j] > j and consec:
		have_op = True
		ans.append(f[j])
	elif f[j] >= j and consec and have_op:
		ans.append(f[j])
	elif have_op:
		consec = False
		ans.append(j)
	else:
		ans.append(j)
print(''.join((str(i) for i in ans)))


import math
n = int(input())
s = input()
lst = list(map(int, input().strip().split(' ')))
s = list(s)
f = 0
for i in range(n):
	if f == 0 and lst[int(s[i]) - 1] > int(s[i]):
		f = 1
		s[i] = lst[int(s[i]) - 1]
	elif f == 1:
		if lst[int(s[i]) - 1] >= int(s[i]):
			s[i] = lst[int(s[i]) - 1]
		else:
			break
for j in range(len(s)):
	print(s[j], end='')


def convert(string):
	return string.split(sep=None, maxsplit=-1)

def printeach(lst):
	for i in lst:
		print(i, end=' ')
n = int(input())
nums = input()
num = int(nums)
myinput = convert(input())
lst = []
for i in myinput:
	lst += [int(i)]
flag = 0
for i in nums:
	j = int(i)
	h = lst[j - 1]
	if flag == 0:
		if h <= j:
			print(j, end='')
		else:
			print(h, end='')
			flag = 1
	elif flag == 1:
		if h < j:
			print(j, end='')
			flag = 2
		else:
			print(h, end='')
	else:
		print(j, end='')


n = int(input())
s = [int(item) for item in list(input())]
f = [0] + [int(item) for item in input().split()]
new_s = [0] * n
for i in range(n):
	new_s[i] = f[int(s[i])]
i = 0
idx = -1
for j in range(n):
	if new_s[j] > s[j]:
		idx = j
		break
if idx == -1:
	print(''.join((str(item) for item in s)))
else:
	start = idx
	while idx < n and new_s[idx] >= s[idx]:
		idx += 1
	u = [str(item) for item in s[:start]]
	v = [str(item) for item in new_s[start:idx]]
	w = [str(item) for item in s[idx:]]
	assert len(u) + len(v) + len(w) == n
	print(''.join(u) + ''.join(v) + ''.join(w))


import os
import heapq
import sys
import math
import operator
from collections import defaultdict
from io import BytesIO, IOBase

def inar():
	return [int(k) for k in input().split()]

def main():
	n = int(input())
	st = list(input())
	arr = inar()
	counter = 0
	for i in range(n):
		num = int(st[i])
		if num < arr[num - 1] and counter == 0:
			counter = 1
			st[i] = str(arr[num - 1])
			continue
		if num <= arr[num - 1] and counter == 1:
			st[i] = str(arr[num - 1])
		elif counter == 1:
			break
		else:
			continue
	print(''.join(st))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input())
c = input()
s = []
for i in c:
	s.append(i)
f = list(map(int, input().split()))
found = False
for i in range(len(s)):
	if int(s[i]) < f[int(s[i]) - 1]:
		found = True
		s[i] = str(f[int(s[i]) - 1])
	elif found and int(s[i]) == f[int(s[i]) - 1]:
		s[i] = str(f[int(s[i]) - 1])
	elif found:
		break
print(*s, sep='')


n = int(input())
a = list(map(int, list(input())))
f = list(map(int, input().split()))
for i in range(n):
	if a[i] < f[a[i] - 1]:
		a[i] = f[a[i] - 1]
		for j in range(i + 1, n):
			if a[j] <= f[a[j] - 1]:
				a[j] = f[a[j] - 1]
			else:
				break
		break
for i in a:
	print(i, end='')
print()


n = int(input())
s = list(input())
fs = [0] + list(map(int, input().split()))
in_process = False
for i in range(0, n):
	num = int(s[i])
	if fs[num] > num:
		in_process = True
		s[i] = fs[num]
	if in_process and fs[num] < num:
		break
for el in s:
	print(el, end='')


from itertools import permutations
from collections import defaultdict
from math import *
import os
import sys
from io import BytesIO, IOBase
from heapq import nlargest
from bisect import *
import copy
import itertools
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
n = int(input())
s = input()
arr = list(map(int, input().split()))
d = dict()
for i in range(1, 10):
	d[i] = arr[i - 1]
started = False
stop = False
for i in range(n):
	c = int(s[i])
	if c < d[c]:
		if stop:
			print(c, end='')
		else:
			print(d[c], end='')
		started = True
	else:
		if started and c != d[c]:
			stop = True
		print(c, end='')


n = int(input())
t = input()
d = list(map(int, input().split()))
r = []
p = n
for (pos, i) in enumerate(t):
	if int(i) < d[int(i) - 1]:
		p = pos
		break
a = t[:p]
i = p
while i < n and int(t[i]) <= d[int(t[i]) - 1]:
	r.append(d[int(t[i]) - 1])
	i += 1
print(a, end='')
[print(i, end='') for i in r]
print(t[i:])


n = int(input())
a = [int(i) for i in input()]
fn = [int(i) for i in input().split()]
fn.insert(0, 0)
f = 0
for i in range(n):
	if a[i] < fn[a[i]]:
		f = 1
		a[i] = fn[a[i]]
	elif a[i] > fn[a[i]] and f == 1:
		break
for i in range(n):
	print(a[i], end='')


import io, sys, atexit, os
import math as ma
from sys import exit
from decimal import Decimal as dec
from itertools import permutations

def li():
	return list(map(int, input().split()))

def num():
	return map(int, input().split())

def nu():
	return int(input())

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	gg = find_gcd(x, y)
	return x * y // gg
mm = 1000000007
yp = 0

def solve():
	t = 1
	for tt in range(t):
		n = nu()
		a = input()
		b = li()
		fl = False
		lst = -1
		xp = ''
		b = [0] + b
		for i in range(n):
			if fl == False:
				yp = ord(a[i]) - ord('0')
				if b[yp] > yp:
					fl = True
					lst = i
					xp += str(b[yp])
				else:
					xp += a[i]
			else:
				yp = ord(a[i]) - ord('0')
				if b[yp] > yp and lst == i - 1:
					lst = i
					xp += str(b[yp])
				elif b[yp] == yp and lst == i - 1:
					xp += a[i]
					lst = i
				else:
					xp += a[i]
		print(xp)
solve()


import math
n = int(input())
s = input()
s = [int(s[x]) for x in range(n)]
ans = [0 for i in range(n)]
f = [int(x) for x in input().split()]
flag = 0
for i in range(n):
	if s[i] >= f[s[i] - 1] and flag == 0:
		ans[i] = s[i]
	if s[i] < f[s[i] - 1] and flag == 0:
		flag = 1
		ans[i] = f[s[i] - 1]
	if flag == 1 and s[i] <= f[s[i] - 1]:
		ans[i] = f[s[i] - 1]
	if flag == 1 and s[i] > f[s[i] - 1]:
		flag = 2
		ans[i] = s[i]
	if flag == 2:
		ans[i] = s[i]
for i in range(n):
	ans[i] = str(ans[i])
print(''.join(ans))


n = str(input())
one = str(input())
l = list(map(str, input().split()))
s = ''
begin = 1
for i in one:
	if l[int(i) - 1] > i and begin != 0:
		begin = 2
		s += l[int(i) - 1]
	elif l[int(i) - 1] == i:
		s += i
	else:
		if begin == 2:
			begin = 0
		s += i
print(s)


import sys
import math
import bisect

def main():
	n = int(input())
	A = list((int(a) for a in list(input().strip())))
	B = [0]
	B.extend(list(map(int, input().split())))
	flag = False
	for i in range(n):
		if flag == False and B[A[i]] > A[i]:
			A[i] = B[A[i]]
			flag = True
		elif flag == True:
			if B[A[i]] >= A[i]:
				A[i] = B[A[i]]
			else:
				break
	print(''.join(list((str(a) for a in A))))
main()


n = int(input())
a = list(input())
fi = list(map(int, input().split()))
flag = 0
for i in range(n):
	if fi[int(a[i]) - 1] > int(a[i]) and flag != 2:
		flag = 1
		print(fi[int(a[i]) - 1], end='')
	elif fi[int(a[i]) - 1] == int(a[i]) and flag == 1:
		print(a[i], end='')
	else:
		if flag == 1:
			flag = 2
		print(a[i], end='')


n = int(input())
num = [*map(int, input())]
f = (0, *map(int, input().split()))
i = 0
while i < n and f[num[i]] <= num[i]:
	i += 1
while i < n and f[num[i]] >= num[i]:
	num[i] = f[num[i]]
	i += 1
print(*num, sep='')


n = input()
n = int(n)
digit = input()
digit = list(digit)
mapping = input()
mapping = mapping.split(' ')
mapp = {}
i = 1
for m in mapping:
	mapp[str(i)] = mapping[i - 1]
	i += 1
i = 0
start = 0
for d in digit:
	if mapp[str(d)] > d and start == 0:
		digit[i] = mapp[str(d)]
		start = 1
	elif mapp[str(d)] >= d and start == 1:
		digit[i] = mapp[str(d)]
	elif mapp[str(d)] < d and start == 1:
		break
	i += 1
print(''.join(digit))


n = int(input())
a = list(input())
ind = n
lol = 0
l = list(map(int, input().split()))
for i in range(n):
	if l[int(a[i]) - 1] > int(a[i]) and lol == 0:
		ind = i
		lol = 1
	if lol == 1:
		if l[int(a[i]) - 1] < int(a[i]):
			break
		a[i] = str(l[int(a[i]) - 1])
print(*a, sep='')


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

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 I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def main():
	n = I()
	s = [int(c) for c in S()]
	a = [0] + LI()

	def f(i):
		return a[i]
	t = [f(i) for i in range(10)]
	r = []
	k = 0
	for c in s:
		if k == 0 and c < t[c]:
			k = c
			r.append(t[c])
		elif 0 < k < 10:
			if t[c] < c:
				k = 10
				r.append(c)
			else:
				r.append(t[c])
		else:
			r.append(c)
	return JA(r, '')
print(main())


n = int(input())
s = input()
f = list(map(int, input().split()))
idx = 0
eidx = n
o = []
for i in range(n):
	c = int(s[i])
	if f[c - 1] > c:
		idx = i
		break
for i in range(idx):
	o.append(s[i])
for i in range(idx, n):
	c = int(s[i])
	if f[c - 1] < c:
		eidx = i
		break
	o.append(str(f[c - 1]))
for i in range(eidx, n):
	o.append(s[i])
print(''.join(o))


n = int(input())
num = input()
func = [0] + list(map(int, input().split()))
count = 0
start = 0
for i in range(len(num)):
	if func[int(num[i])] > int(num[i]):
		start = int(i)
		count = 1
		for j in range(i + 1, len(num)):
			if func[int(num[j])] >= int(num[j]):
				count += 1
			else:
				break
		break
tmp = list(num)
for i in range(count):
	tmp[start + i] = str(func[int(tmp[start + i])])
num = ''.join(tmp)
print(num)


n = int(input())
s = input()
perm = list(map(int, input().split()))
wyn = list(map(int, s))
c = 0
for i in range(n):
	if c == 1:
		break
	if perm[int(s[i]) - 1] > int(s[i]):
		wyn[i] = perm[int(s[i]) - 1]
		j = i
		c = 1
		while True:
			j += 1
			if j < n and perm[int(s[j]) - 1] >= int(s[j]):
				wyn[j] = perm[int(s[j]) - 1]
			else:
				break
for i in wyn:
	print(i, end='')


n = int(input())
s = input()
l = list(map(int, input().split()))
l1 = []
c = 0
for i in range(n):
	if int(s[i]) <= l[int(s[i]) - 1]:
		l1.append(l[int(s[i]) - 1])
		if int(s[i]) < l[int(s[i]) - 1]:
			c += 1
	elif int(s[i]) > l[int(s[i]) - 1] and c > 0:
		l1.append(s[i:])
		break
	else:
		l1.append(int(s[i]))
for i in l1:
	print(i, end='')


n = int(input())
word = list(input())
rep = input().split(' ')
i = 0
ans = True
while i < n and word[i] >= rep[int(word[i]) - 1]:
	i += 1
while i < n:
	x = word[i]
	y = rep[int(x) - 1]
	if x <= y:
		word[i] = y
		ans = False
	elif not ans:
		break
	i += 1
print(''.join(word))


n = int(input())
k = input()
d = -1
c = 0
a = list(map(int, input().split()))
for i in range(n):
	if int(k[i]) <= a[int(k[i]) - 1] and d != -1 or (int(k[i]) < a[int(k[i]) - 1] and d == -1):
		if d == -1:
			c = i
		d = i
	elif d != -1:
		break
k1 = k[0:c]
for i in range(c, d + 1):
	k1 += str(a[int(k[i]) - 1])
k1 += k[d + 1:len(k)]
print(k1)


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(n,) = I()
s = list(input().strip())
f = I()
x = 0
for i in range(n):
	if f[int(s[i]) - 1] > int(s[i]):
		x = 1
		s[i] = str(f[int(s[i]) - 1])
	elif x == 1:
		if f[int(s[i]) - 1] == int(s[i]):
			continue
		break
print(''.join(s))


n = int(input())
s = input()
no = [0] * n
arr = list(map(int, input().split()))
for i in range(n):
	no[i] = int(s[i])
for i in range(n):
	if arr[no[i] - 1] > no[i]:
		no[i] = arr[no[i] - 1]
		j = i + 1
		while j < n and arr[no[j] - 1] >= no[j]:
			no[j] = arr[no[j] - 1]
			j += 1
		break
for i in range(n):
	print(no[i], end='')


while True:
	try:
		n = int(input())
		s = list(map(int, list(input())))
		f = [0]
		f.extend(list(map(int, input().strip().split())))
		(l, r) = (0, len(s))
		for i in range(len(s)):
			n = s[i]
			if n < f[n]:
				l = i
				break
		for i in range(l, len(s)):
			n = s[i]
			if n > f[n]:
				r = i
				break
		for i in range(l, r):
			s[i] = f[s[i]]
		print(''.join(map(str, s)))
	except:
		break


n = int(input())
a = list(map(int, input()))
s = list(map(int, input().split()))
flag = True
flag2 = False
i = 0
while flag and i < n:
	if flag2:
		if a[i] <= s[a[i] - 1]:
			a[i] = s[a[i] - 1]
			flag2 = True
		elif flag2:
			flag = False
	elif a[i] < s[a[i] - 1]:
		a[i] = s[a[i] - 1]
		flag2 = True
	elif flag2:
		flag = False
	i += 1
print(*a, end='', sep='')


import sys
input = sys.stdin.readline
n = int(input())
a = list(input())
a.pop()
f = list(map(int, input().split()))
q = 0
for i in range(len(a)):
	if q == 0 and f[int(a[i]) - 1] > int(a[i]):
		q = 1
	if q == 1:
		if f[int(a[i]) - 1] >= int(a[i]):
			a[i] = str(f[int(a[i]) - 1])
		else:
			break
print(''.join(a))


_ = input()
a = list(input())
b = input().split()
using = False
for i in range(len(a)):
	n = int(a[i])
	if b[n - 1] > a[i]:
		using = True
		a[i] = b[n - 1]
	elif using and b[n - 1] < a[i]:
		break
print(''.join(a))


n = int(input())
d = list(map(str, input()))
l = list(map(int, input().split()))
k = ''
i = 0
while i < n:
	e = int(d[i])
	if e < l[e - 1]:
		e = int(d[i])
		while e <= l[e - 1] and i < n - 1:
			d[i] = str(l[e - 1])
			i += 1
			e = int(d[i])
		if e < l[e - 1]:
			d[i] = str(l[e - 1])
		break
	i += 1
print(''.join(d))


n = int(input())
p = input()
x = [i for i in p]
l = ['0'] + input().split()
f = 0
for i in range(n):
	if int(x[i]) < int(l[int(x[i])]):
		f = 1
		x[i] = l[int(x[i])]
	elif int(x[i]) == int(l[int(x[i])]):
		continue
	elif f == 1:
		break
print(''.join(x))


n = int(input())
d = list(input())
dic = {i + 1: int(j) for (i, j) in enumerate(input().split())}
flag = False
for (j, i) in enumerate(d):
	if dic[int(i)] > int(i):
		flag = True
		d[j] = dic[int(i)]
		continue
	elif dic[int(i)] == int(i):
		continue
	elif flag:
		break
for i in d:
	print(i, end='')
print()


length = int(input())
number = list(map(int, list(input())))
L = [int(item) for item in input().split(' ')]
index = 0
while index < length:
	current = index
	if number[index] < L[number[index] - 1]:
		while index < length and number[index] <= L[number[index] - 1]:
			number[index] = L[number[index] - 1]
			index += 1
	if index != current:
		break
	index += 1
print(''.join(map(str, number)))


n = int(input())
s = input()
a = []
for i in s:
	a.append(int(i))
f = list(map(int, input().split()))
l = 0
while l < n:
	if f[a[l] - 1] > a[l]:
		r = l
		while r < n:
			if f[a[r] - 1] >= a[r]:
				a[r] = f[a[r] - 1]
				r += 1
			else:
				break
		break
	l += 1
for i in a:
	print(i, end='')


n = int(input())
num = list(input())
pos = [int(i) for i in input().split()]
idx = 0
while idx < len(num) and int(num[idx]) >= pos[int(num[idx]) - 1]:
	idx += 1
while idx < len(num) and int(num[idx]) <= pos[int(num[idx]) - 1]:
	num[idx] = str(pos[int(num[idx]) - 1])
	idx += 1
print(''.join(num))


n = int(input())
s = input().strip()
d = input().split()
f = {str(i + 1): d[i] for i in range(9)}
up = False
answer = ''
for i in range(n):
	if s[i] < f[s[i]] or (s[i] == f[s[i]] and up == True):
		up = True
		answer = answer + f[s[i]]
	elif up == True:
		answer = answer + s[i:]
		break
	else:
		answer = answer + s[i]
print(answer)


n = int(input())
a = [int(i) for i in input()]
f = [int(i) for i in input().split()]
i = 0
while i < n and a[i] >= f[a[i] - 1]:
	i += 1
while i < n and a[i] <= f[a[i] - 1]:
	a[i] = f[a[i] - 1]
	i += 1
print(''.join((str(i) for i in a)))


n = int(input())
s = input()
l2 = list(s)
l1 = list(map(int, input().split()))
for i in range(len(s)):
	if int(s[i]) < l1[int(s[i]) - 1]:
		while i < len(s) and int(s[i]) <= l1[int(s[i]) - 1]:
			l2[i] = l1[int(s[i]) - 1]
			i += 1
		break
for i in l2:
	print(i, end='')


import atexit
import io
import sys
_I_B = sys.stdin.read().splitlines()
input = iter(_I_B).__next__
_O_B = io.StringIO()
sys.stdout = _O_B

@atexit.register
def write():
	sys.__stdout__.write(_O_B.getvalue())

def main():
	typ = dict()
	for i in '123456789':
		typ[i] = int(i) - 1
	l = int(input())
	n = input()
	m = input().split()
	nn = list()
	i = 0
	while i < l:
		if n[i] < m[typ[n[i]]]:
			while i < l and n[i] <= m[typ[n[i]]]:
				nn.append(m[typ[n[i]]])
				i += 1
			while i < l:
				nn.append(n[i])
				i += 1
		else:
			nn.append(n[i])
		i += 1
	print(''.join(nn))
main()


n = int(input())
s = input()
li = list(map(int, input().split()))
f = 0
ch = 0
for i in s:
	k = int(i)
	if not f and li[k - 1] > k:
		f = 1
		ch = 1
		print(li[k - 1], end='')
	elif ch and li[k - 1] < k:
		ch = 0
		print(k, end='')
	elif ch and li[k - 1] >= k:
		print(li[k - 1], end='')
	elif not ch:
		print(k, end='')


k = int(input())
numbers = str(input())
array = list(map(int, input().split()))
numbers = [x for x in numbers]
ls = []
used = False
change = False
for x in numbers:
	if used == True and change == True:
		ls.append(x)
	elif array[int(x) - 1] > int(x) and used == False:
		change = True
		ls.append(str(array[int(x) - 1]))
	elif array[int(x) - 1] < int(x) and change == True:
		used = True
		ls.append(x)
	else:
		ls.append(x)
print(''.join(ls))


c = int(input())
n = str(input())
switch = list(map(int, input().split()))
(flag, flag1) = (0, 0)
for i in range(c):
	if flag < flag1:
		break
	if switch[int(n[i]) - 1] > int(n[i]):
		n = n[0:i] + str(switch[int(n[i]) - 1]) + n[i + 1:c]
		flag += 1
		flag1 += 1
	elif switch[int(n[i]) - 1] != int(n[i]):
		flag = 0
print(n)


n = int(input())
a = list(input())
a = [int(i) for i in a]
b = list(map(int, input().split()))
i = 0
p = 0
flag = 0
while i < n:
	if a[i] <= b[a[i] - 1]:
		if flag == 0:
			if a[i] != b[a[i] - 1]:
				p = 1
			a[i] = b[a[i] - 1]
			i += 1
		else:
			break
	else:
		if p == 0:
			i += 1
			continue
		else:
			flag = 1
		i += 1
for i in a:
	print(i, end='')


n = int(input())
a = list(str(input()))
F = list(map(int, input().split()))
d = {}
e = set()
for i in range(9):
	if i + 1 < F[i]:
		d[str(i + 1)] = str(F[i])
	if i + 1 == F[i]:
		e.add(str(i + 1))
ans = [0] * n
for i in range(n):
	ans[i] = a[i]
flag = False
for i in range(n):
	if a[i] in d:
		flag = True
		ans[i] = d[a[i]]
	elif flag:
		if a[i] in e:
			continue
		else:
			break
	else:
		continue
print(''.join(ans))


s = []
n = int(input())
a = input()
for i in range(n):
	s.append(int(a[i]))
b = list(map(int, input().split()))
o = 0
for i in range(n):
	if s[i] < b[s[i] - 1] and o < 2:
		print(b[s[i] - 1], end='')
		o = 1
	if s[i] > b[s[i] - 1] and o == 1:
		o = 2
	if s[i] == b[s[i] - 1] and o == 1:
		print(s[i], end='')
	if o == 0 or o == 2:
		print(s[i], end='')


n = int(input())
x = input()
a = [int(x) for x in input().split()]
d = {}
for i in range(9):
	d[i + 1] = a[i]
new = ''
for i in range(n):
	k = int(x[i])
	if k < d[k]:
		new += str(d[k])
		j = i + 1
		if j < n:
			k = int(x[j])
		else:
			break
		while j < n and k <= d[k]:
			new += str(d[k])
			j += 1
			if j < n:
				k = int(x[j])
		new += x[j:n]
		break
	else:
		new += str(k)
print(new)


n = int(input())
s = input()
l = list(map(int, input().split()))
s1 = ''
for i in range(n):
	if l[int(s[i]) - 1] <= int(s[i]):
		s1 = s1 + s[i]
	if l[int(s[i]) - 1] > int(s[i]):
		while i < n and l[int(s[i]) - 1] >= int(s[i]):
			s1 = s1 + str(l[int(s[i]) - 1])
			i = i + 1
		break
if len(s1) == n:
	print(s1)
else:
	for i in range(i, n):
		s1 = s1 + s[i]
	print(s1)


n = int(input())
a = input()
a = list(a)
F = list(map(int, input().split()))
b = -1
for i in range(n):
	if F[int(a[i]) - 1] > int(a[i]):
		an = a[i]
		b = i
		break
if b != -1:
	for i in range(b, n):
		if F[int(a[i]) - 1] < int(a[i]):
			break
		a[i] = F[int(a[i]) - 1]
for i in a:
	print(i, end='')


n = int(input())
a = int(input())
A = [int(x) for x in list(str(a))]
D = list(map(int, input().split()))
Dict = {}
for (i, d) in enumerate(D):
	Dict[i + 1] = d
Ans = []
for (i, a) in enumerate(A):
	if Dict[a] > a:
		A[i] = Dict[a]
		break
if i < n - 1:
	for (j, a) in enumerate(A[i + 1:]):
		if Dict[a] >= a:
			A[j + i + 1] = Dict[a]
		else:
			break
print(*A, sep='')


n = int(input())
x = list(map(int, list(input())))
a = [int(s) for s in input().split()]
flag = True
for i in range(n):
	if not flag and x[i] < a[x[i] - 1]:
		x[i] = a[x[i] - 1]
	elif flag and x[i] < a[x[i] - 1]:
		x[i] = a[x[i] - 1]
		flag = False
	elif not flag and x[i] > a[x[i] - 1]:
		break
print(*x, sep='')


f = {}
n = int(input())
numero = input()
jaOperado = [False] * (n + 1)
valoresMapeados = [int(i) for i in input().split()]
for i in range(len(valoresMapeados)):
	f[str(i + 1)] = valoresMapeados[i]
res = ''
started = 0
for v in numero:
	if int(v) > f[v] or started == 2:
		res += v
		if started == 1:
			started = 2
	elif int(v) == f[v]:
		res += v
	else:
		res += str(f[v])
		started = 1
print(res)


t = int(input())
n = [int(x) for x in list(input())]
map = [int(x) for x in input().split()]
a = [0]
a = a + map
flag = 0
for i in range(len(n)):
	if a[n[i]] > n[i] and flag == 0:
		flag = 1
		n[i] = a[n[i]]
	elif a[n[i]] >= n[i] and flag == 1:
		n[i] = a[n[i]]
	elif a[n[i]] <= n[i] and flag == 0:
		continue
	elif a[n[i]] < n[i] and flag == 1:
		break
for i in n:
	print(i, end='')


def solve(n, s, l):
	res = ''
	start = 'None'
	for (pos, value) in enumerate(s):
		if value < l[int(value)]:
			start = pos
			break
	if start == 'None':
		return s
	else:
		i = start
		while i < n:
			if s[i] <= l[int(s[i])]:
				i = i + 1
			else:
				break
		stop = i
		for i in range(0, start):
			res = res + s[i]
		for i in range(start, stop):
			res = res + l[int(s[i])]
		for i in range(stop, n):
			res = res + s[i]
		return res
n = int(input())
s = input()
l = input()
l = '0' + l
l = l.replace(' ', '')
print(solve(n, s, l))


n = int(input())
s = input()
s1 = input().split()
a = [int(num) for num in s1]
ans = ''
j = n
for i in range(0, n):
	if a[int(s[i]) - 1] > int(s[i]):
		j = i
		while j < n and a[int(s[j]) - 1] >= int(s[j]):
			ans += str(a[int(s[j]) - 1])
			j += 1
		break
	ans += s[i]
for i in range(j, n):
	ans += s[i]
print(ans)


for _ in range(1):
	n = int(input())
	s = input()
	lst = list(map(int, input().split()))
	start = -1
	z = -1
	last = -1
	for i in range(n):
		x = int(s[i])
		if lst[x - 1] > x and z == -1:
			start = i
			z = 1
		if z == 1 and lst[x - 1] < x:
			last = i - 1
			break
	if start == -1:
		print(s)
	else:
		if last == -1:
			last = n - 1
		news = ''
		for i in range(start, last + 1, 1):
			x = int(s[i])
			news += str(lst[x - 1])
		news = s[:start] + news + s[last + 1:]
		print(news)


n = input()
s = input()
d = list(map(int, input().split()))
r = 0
c = ''
for i in s:
	if r == 0:
		if int(i) < d[int(i) - 1]:
			c += str(d[int(i) - 1])
			r = 1
		else:
			c += i
	elif r == 1:
		if int(i) > d[int(i) - 1]:
			c += i
			r = 2
		else:
			c += str(d[int(i) - 1])
	else:
		c += i
print(c)


n = int(input())
a = str(input())
l = list(map(str, input().strip().split()))
sol = []
i = 0
flag = False
noTouch = False
while not noTouch and i < n:
	if l[int(a[i]) - 1] >= a[i] and flag or l[int(a[i]) - 1] > a[i]:
		flag = True
		sol.append(l[int(a[i]) - 1])
	elif flag:
		noTouch = True
		sol.append(a[i:])
	else:
		sol.append(a[i])
	i += 1
print(''.join(sol))


n = int(input())
l = [*map(int, input())]
d = {}
for (i, j) in enumerate(map(int, input().split())):
	d[i + 1] = j
i = 0
while i < n and d[l[i]] <= l[i]:
	i += 1
while i < n and d[l[i]] >= l[i]:
	l[i] = d[l[i]]
	i += 1
print(''.join(map(str, l)))


import sys
from collections import defaultdict as dc
from collections import Counter
from bisect import bisect_right, bisect_left
import math
from operator import itemgetter
from heapq import heapify, heappop, heappush
n = int(input())
l = list(map(int, list(input())))
x = list(map(int, input().split()))
f = 0
for i in range(n):
	if l[i] > x[l[i] - 1]:
		if f:
			break
	elif l[i] < x[l[i] - 1]:
		f = 1
		l[i] = x[l[i] - 1]
l = list(map(str, l))
print(''.join(l))


n = int(input())
s = input().rstrip()
x = list(s)
v = -1
p = input().rstrip().split(' ')
for i in range(0, len(x)):
	if int(p[int(x[i]) - 1]) > int(x[i]):
		v = i
		break
if v == -1:
	print(s)
else:
	C = x[0:v]
	D = x[v:len(x)]
	for i in range(0, len(D)):
		if int(p[int(D[i]) - 1]) >= int(D[i]):
			D[i] = str(p[int(D[i]) - 1])
		else:
			break
	V = C + D
	print(''.join(V))


n = int(input())
a = list(input())
f = input().split()
i = 0
while i < n and int(f[int(a[i]) - 1]) <= int(a[i]):
	i += 1
while i < n and int(f[int(a[i]) - 1]) >= int(a[i]):
	a[i] = f[int(a[i]) - 1]
	i += 1
print(''.join(a))


def solve():
	N = int(input())
	A = list(map(lambda e: ord(e) - ord('0'), list(input())))
	F = [0] + list(map(int, input().split()))
	phase = 0
	for i in range(N):
		if phase == 0 and F[A[i]] > A[i]:
			phase = 1
			A[i] = F[A[i]]
		elif phase == 1 and F[A[i]] >= A[i]:
			A[i] = F[A[i]]
		elif phase == 1 and F[A[i]] < A[i]:
			phase = 2
	return ''.join(map(str, A))
print(solve())


n = int(input())
ar = list(map(int, list(input())))
li = list(map(int, input().split()))
flag = False
for i in range(n):
	if ar[i] < li[ar[i] - 1]:
		if not flag:
			flag = True
		ar[i] = li[ar[i] - 1]
	elif ar[i] > li[ar[i] - 1]:
		if flag:
			break
for i in ar:
	print(str(i), end='')


num = (1, 2, 3, 4, 5, 6, 7, 8, 9)
n = int(input())
x = input()
a = [int(d) for d in str(x)]
MAP = [int(d) for d in input().split()]
flag = 0
for i in range(n):
	x = a[i]
	if MAP[x - 1] > x:
		flag = i
		break
while flag < n:
	x = a[flag]
	if MAP[x - 1] >= x:
		a[flag] = MAP[x - 1]
		flag += 1
	else:
		break
for i in range(n):
	print(a[i], end='')


input()
A = list(map(int, input()))
f = tuple(map(int, input().split()))
for (i, a) in enumerate(A):
	if a < f[a - 1]:
		break
for (j, a) in enumerate(A[i:], start=i):
	if a > f[a - 1]:
		break
	else:
		A[j] = f[a - 1]
print(*A, sep='')


n = int(input())
l = list(input())
l2 = input().split()
l2.insert(0, 0)
f = 0
for i in range(0, n):
	if l[i] < l2[int(l[i])]:
		l[i] = l2[int(l[i])]
		break
for i in range(i + 1, n):
	if l[i] <= l2[int(l[i])]:
		l[i] = l2[int(l[i])]
	else:
		break
print(''.join(l))


n = int(input())
s = input()
l1 = list(map(int, input().split()))
l1.insert(0, 0)
ans = ''
f = 0
for i in range(len(s)):
	if l1[int(s[i])] > int(s[i]):
		if f == 0:
			a = i
			f = 1
		ans += str(l1[int(s[i])])
	elif l1[int(s[i])] == int(s[i]):
		if f == 0:
			continue
		else:
			ans += str(l1[int(s[i])])
	elif f == 0:
		continue
	else:
		break
if f == 0:
	print(s)
else:
	print(s[0:a] + ans + s[a + len(ans):n])


n = int(input())
num = [int(x) for x in input()]
mapping = [int(x) for x in input().split()]
flag = False
for (i, k) in enumerate(num):
	if k < int(mapping[k - 1]):
		num[i] = int(mapping[k - 1])
		flag = True
	elif flag and k > int(mapping[k - 1]):
		break
print(''.join(map(str, num)))


n = int(input())
a = [int(x) for x in list(input())]
vFuctin = [0] + list(map(int, input().split()))
start = 0
for i in range(n):
	if start:
		if vFuctin[a[i]] >= a[i]:
			a[i] = vFuctin[a[i]]
		else:
			break
	elif vFuctin[a[i]] > a[i]:
		a[i] = vFuctin[a[i]]
		start = 1
for v in a:
	print(v, end='')


n = int(input())
arr = list(input().strip())
brr = list(input().split())
brr = [-1] + brr
idx = None
for i in range(n):
	if brr[int(arr[i])] > arr[i]:
		idx = i
		break
if idx != None:
	for i in range(idx, n):
		if brr[int(arr[i])] >= arr[i]:
			arr[i] = brr[int(arr[i])]
		else:
			break
print(''.join(arr))


n = int(input())
a = input().strip()
f = {str(i + 1): c for (i, c) in enumerate(input().split())}
res = ''
mode = False
flag = True
for c in a:
	if mode:
		if c > f[c]:
			res += c
			flag = False
			mode = False
		else:
			res += f[c]
			mode = True
	elif f[c] > c and flag:
		res += f[c]
		mode = True
	else:
		res += c
		mode = False
print(res)


n = int(input())
s = list(input())
g = input().split()
for i in range(n):
	if g[int(s[i]) - 1] > s[i]:
		while i < n and g[int(s[i]) - 1] >= s[i]:
			s[i] = g[int(s[i]) - 1]
			i += 1
		break
print(''.join(s))


tam = int(input())
number = input()
num = [int(i) for i in number]
fun = [int(x) for x in input().split()]
ini = 0
for w in range(tam):
	if fun[num[w] - 1] > num[w]:
		ini = w
		break
for j in range(ini, tam):
	if fun[num[j] - 1] >= num[j]:
		num[j] = fun[num[j] - 1]
	else:
		break
num = [str(i) for i in num]
ans = ''.join(num)
print(ans)


input()
s = input()
f = {i + 1: int(x) for (i, x) in enumerate(input().split())}
flo = 0
for c in s:
	if flo == 1:
		if f[int(c)] >= int(c):
			if flo == 0:
				flo = 1
			print(f[int(c)] if flo == 1 else c, end='')
		else:
			if flo == 1:
				flo = 2
			print(c, end='')
	elif f[int(c)] > int(c):
		if flo == 0:
			flo = 1
		print(f[int(c)] if flo == 1 else c, end='')
	else:
		if flo == 1:
			flo = 2
		print(c, end='')
print()


n = int(input())
s = [*input()]
M = {str(i + 1): x for (i, x) in enumerate(input().split())}
b = 0
for i in range(n):
	if M[s[i]] > s[i]:
		break
for j in range(i, n):
	if M[s[j]] < s[j]:
		break
else:
	j = n
for k in range(i, j):
	s[k] = M[s[k]]
print(''.join(s))


import time
import cProfile
t = input()
n = input()
st = [int(i) for i in input().split(' ')]
ls = [int(i) for i in str(n)]
dt = {i: st[i - 1] for i in range(1, 10)}
gg = []
once = False
last = 0
i = 0
while i < int(t):
	if dt[ls[i]] <= ls[i]:
		i += 1
	else:
		break
while i < int(t):
	if dt[ls[i]] >= ls[i]:
		ls[i] = dt[ls[i]]
		i += 1
	else:
		break
print(*ls, sep='')


input()
number = input()
places = input().split()
places_ready = {'1': places[0], '2': places[1], '3': places[2], '4': places[3], '5': places[4], '6': places[5], '7': places[6], '8': places[7], '9': places[8]}
res_string = []
started = False
ended = False
for i in number:
	if places_ready[i] < i:
		res_string.append(i)
		if started:
			ended = True
		continue
	if ended:
		res_string.append(i)
		continue
	if places_ready[i] > i:
		started = True
		res_string.append(places_ready[i])
	if places_ready[i] == i:
		res_string.append(places_ready[i])
res_string = ''.join(res_string)
print(res_string)


n = int(input())
s = list(input())
f = {i + 1: int(x) for (i, x) in enumerate(input().split())}
for (i, k) in enumerate(s):
	q = int(k)
	if f[q] > q:
		s[i] = str(f[q])
		j = i + 1
		while j < len(s) and f[int(s[j])] >= int(s[j]):
			s[j] = str(f[int(s[j])])
			j += 1
		break
print(''.join(s))


def main():
	n = int(input())
	a = list(map(int, list(input())))
	c = list(map(int, input().split()))
	i = 0
	while i < n and c[a[i] - 1] <= a[i]:
		i += 1
	if i == n:
		print(''.join(map(str, a)))
	else:
		d = a[i]
		l = c[d - 1]
		while i < n and c[a[i] - 1] >= a[i]:
			a[i] = c[a[i] - 1]
			i += 1
		print(''.join(map(str, a)))
main()


n = int(input())
a_list = [int(elem) for elem in input()]
f = {i: int(elem) for (i, elem) in enumerate(input().split(), start=1)}
max_number = []
start = 0
while start < n and f[a_list[start]] <= a_list[start]:
	start += 1
end = start + 1
while end < n and f[a_list[end]] >= a_list[end]:
	end += 1
max_number.extend(a_list[:start])
max_number.extend((f[a] for a in a_list[start:end]))
max_number.extend(a_list[end:])
print(''.join((str(digit) for digit in max_number)))


n = int(input())
num = input()
l = list(map(int, input().split()))
i = 0
ans = ''
while i < n:
	if int(num[i]) < l[int(num[i]) - 1]:
		break
	else:
		ans += num[i]
	i += 1
while i < n:
	if int(num[i]) > l[int(num[i]) - 1]:
		break
	else:
		ans += str(l[int(num[i]) - 1])
	i += 1
if i < n:
	ans += num[i:]
print(ans)


n = int(input())
s = input()
f = list(map(int, input().split()))
cnt = 0
isNear = False
ans = []
for i in range(n):
	x = int(s[i])
	y = f[x - 1]
	if y > x:
		if cnt == 0:
			cnt = 1
			ans.append(y)
			isNear = True
		elif cnt == 1 and isNear == True:
			ans.append(y)
		else:
			ans.append(x)
	elif y == x:
		ans.append(y)
	else:
		ans.append(x)
		isNear = False
print(*ans, sep='')


import sys
read = sys.stdin.read
readlines = sys.stdin.readlines

def main():
	n = int(input())
	a2 = list(input())
	h = input().split()
	h2 = [0] * 9
	for j1 in range(9):
		h2[j1] = int(h[j1]) > j1 + 1
	h22 = [0] * 9
	for j1 in range(9):
		h22[j1] = int(h[j1]) >= j1 + 1
	a2i = [int(ae) for ae in a2]
	flag = False
	for (i1, a2ie) in enumerate(a2i):
		if not flag:
			if h2[a2ie - 1]:
				flag = True
				a2[i1] = h[a2ie - 1]
		elif h22[a2ie - 1]:
			a2[i1] = h[a2ie - 1]
		else:
			break
	r = ''.join(a2)
	print(r)
main()


N = int(input())
S = input()
L = list(map(int, input().split()))
S = [int(d) for d in S]
t = 0
while t < N:
	if S[t] >= L[S[t] - 1]:
		t += 1
	else:
		break
for k in range(t, N):
	if L[S[k] - 1] >= S[k]:
		S[k] = L[S[k] - 1]
	else:
		break
print(''.join(map(str, S)))


import sys
n = int(sys.stdin.readline().strip())
a = [int(x) for x in sys.stdin.readline().strip()]
mapper = {k + 1: v for (k, v) in enumerate([int(x) for x in sys.stdin.readline().strip().split()])}
result = a[:]
state = 0
for i in range(n):
	ch = a[i]
	if mapper[ch] > ch:
		while i < n:
			ch = a[i]
			if mapper[ch] >= ch:
				result[i] = mapper[ch]
				i += 1
			else:
				break
		break
print(''.join([str(x) for x in result]))


n = int(input())
num = list(map(int, list(input())))
maps = list(map(int, input().split()))
dic = {}
for i in range(1, 10):
	dic[i] = maps[i - 1]
start = False
end = False
for i in range(len(num)):
	if not start and (not end):
		if dic[num[i]] > num[i]:
			num[i] = dic[num[i]]
			start = True
	elif start and (not end):
		if dic[num[i]] >= num[i]:
			num[i] = dic[num[i]]
		else:
			end = True
	else:
		break
num = list(map(str, num))
print(''.join(num))


n = int(input())
a = input()
fs = input().split(' ')
dict = {}
for (x, f) in enumerate(fs):
	dict[x + 1] = int(f)
start = False
end = False
start_i = None
end_i = None
for i in range(n):
	c = int(a[i])
	if not start and dict[c] > c:
		start_i = i
		start = True
	if start and dict[c] < c:
		end_i = i - 1
		end = True
		break
if start and (not end):
	end_i = n - 1
if start:
	a_list = list(a)
	for i in range(start_i, end_i + 1):
		in_c = a_list[i]
		out_c = str(dict[int(in_c)])
		a_list[i] = out_c
	out = ''.join(a_list)
else:
	out = a
print(out)


def cal(n, f):
	change = False
	for i in range(len(n)):
		if f[n[i] - 1] > n[i] or (change == True and f[n[i] - 1] == n[i]):
			n[i] = f[n[i] - 1]
			change = True
		elif change:
			break
	print(*n, sep='')
input()
n = input()
arr = [int(i) for i in n]
s = list(map(int, input().split()))
cal(arr, s)


F = input()
number = list(input())
F = list(map(int, input().split()))
flag = 0
for (index, I) in enumerate(number):
	if F[int(I) - 1] > int(I):
		number[index] = str(F[int(I) - 1])
		flag = 1
	elif flag == 1:
		if F[int(I) - 1] < int(I):
			break
print(''.join(number))


from sys import stdin
from collections import defaultdict as dd
from collections import deque as dq
import itertools as it
from math import sqrt, log, log2
from fractions import Fraction
n = int(input())
num = list(map(int, list(input().strip())))
mapping = list(map(int, stdin.readline().split()))
mapping = [0] + mapping
start = 0
for (i, digit) in enumerate(num):
	if mapping[digit] > digit:
		start = 1
		num[i] = mapping[digit]
	elif mapping[digit] < digit and start:
		break
print(*num, sep='')


n = int(input())
a = map(int, input())
f = [v - i - 1 for (i, v) in enumerate(map(int, input().split()))]
state = 0
for v in a:
	if state == 0 and f[v - 1] > 0:
		state = 1
	if state == 1 and f[v - 1] < 0:
		state = 2
	if state == 1:
		print(v + f[v - 1], end='')
	else:
		print(v, end='')
print('')


n = int(input())
s = [int(x) for x in input()]
l = list(map(int, input().split()))
j = -1
for i in range(n):
	if l[s[i] - 1] > s[i]:
		j = i
		break
if j != -1:
	while j < n and l[s[j] - 1] >= s[j]:
		s[j] = l[s[j] - 1]
		j += 1
for x in s:
	print(x, end='')
print()


import os
import gc
import sys
from io import BytesIO, IOBase
from collections import Counter
from collections import deque
import heapq
import math
import statistics

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

def sain():
	return input().split()

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007

def readTree(n, m):
	adj = [deque([]) for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = ain()
		adj[u].append(v)
		adj[v].append(u)
	return adj

def solve(d):
	xy = [MIN, MAX]
	for rt in d:
		if xy[0] > rt[1]:
			return -1
		if xy[1] < rt[0]:
			return -1
		xy[0] = max(xy[0], rt[0])
		xy[1] = min(xy[1], rt[1])
	return xy

def main():
	n = iin()
	s = sin()
	l = ain()
	c = -1
	for i in range(n):
		if l[int(s[i]) - 1] < int(s[i]) and c != -1:
			break
		if l[int(s[i]) - 1] > int(s[i]):
			c = i
	s = list(s)
	for i in range(c + 1):
		if l[int(s[i]) - 1] > int(s[i]):
			s[i] = str(l[int(s[i]) - 1])
	print(''.join(s))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
index = {}
mark = False
n = int(input())
str = list(map(int, list(input())))
t_list = list(map(int, input().split()))
for i in range(9):
	index[i + 1] = t_list[i]
for i in range(n):
	if str[i] < index[str[i]]:
		for j in range(i, n):
			if str[j] > index[str[j]]:
				break
			str[j] = index[str[j]]
		break
for i in range(n):
	print(str[i], end='')
print()


n = int(input())
no = list(input())
l = list(map(int, input().split()))
f = 0
l = [0] + l
t = 0
for i in range(n):
	if l[int(no[i])] > int(no[i]):
		if f == 0:
			f = 1
		if t == 0:
			no[i] = l[int(no[i])]
	elif l[int(no[i])] < int(no[i]):
		if f == 1:
			t = 1
print(*no, sep='')


n = int(input())
c = 0
beg = -1
end = -1
a = input()
sti = ''
lst = [int(i) for i in input().split()]
while True:
	if c >= n:
		if beg != -1:
			end = n
		break
	if int(a[c]) < lst[int(a[c]) - 1]:
		if beg == -1:
			beg = c
	elif int(a[c]) <= lst[int(a[c]) - 1] and beg != -1:
		if beg == -1:
			beg = c
	elif beg != -1:
		end = c
		break
	c += 1
if beg == -1:
	print(a)
else:
	for i in range(n):
		if i in range(beg, end):
			sti += str(lst[int(a[i]) - 1])
		else:
			sti += a[i]
	print(sti)


a = input()
b = input()
c = input()
temp = -1
output = ''
for i in range(int(a)):
	if int(c.split(' ')[int(b[i]) - 1]) >= int(b[i]) and i == temp + 1:
		output = output + c.split(' ')[int(b[i]) - 1]
		temp = i
	else:
		output = output + b[i]
		if output[0:i] == b[0:i] or i == 0:
			temp = i
print(output)


t = int(input())
r = input()
n = list(map(int, r))
x = [int(n) for n in input().split()]
s = 0
for i in range(t):
	if x[int(n[i]) - 1] > int(n[i]):
		n[i] = x[int(n[i]) - 1]
		s = 1
		for j in range(i + 1, t):
			if x[int(n[j]) - 1] >= int(n[j]):
				n[j] = x[int(n[j]) - 1]
			else:
				break
	if s == 1:
		break
for j in range(t):
	print(n[j], end='')


n = int(input())
a = list(map(int, list(input())))
t = 0
f = 0
f = 0
q = list(map(int, input().split()))
for i in range(len(a)):
	if q[a[i] - 1] > a[i] and t == 0:
		a[i] = q[a[i] - 1]
		t = 1
	elif q[a[i] - 1] >= a[i] and t == 1:
		a[i] = q[a[i] - 1]
		t = 1
	elif t == 1:
		break
for i in range(len(a)):
	print(a[i], end='')


def main():
	input()
	n = [int(x) for x in input()]
	f = [0] + [int(x) for x in input().split()]
	for i in range(len(n)):
		if f[n[i]] > n[i]:
			n[i] = f[n[i]]
			for j in range(i + 1, len(n)):
				if f[n[j]] < n[j]:
					break
				n[j] = f[n[j]]
			break
	print(''.join((str(x) for x in n)))
main()


import math
read = lambda : map(int, input().split())
n = int(input())
a = [*map(int, input())]
f = (0, *map(int, input().split()))
i = 0
while i < n and f[a[i]] <= a[i]:
	i += 1
while i < n and f[a[i]] >= a[i]:
	a[i] = f[a[i]]
	i += 1
print(*a, sep='')


n = int(input())
num = input()
list_num = []
for i in num:
	list_num.append(int(i))
list9 = list(map(int, input().split()))
flag = False
for i in range(len(list_num)):
	if list9[list_num[i] - 1] > list_num[i]:
		list_num[i] = list9[list_num[i] - 1]
		flag = True
	elif flag == True and list9[list_num[i] - 1] < list_num[i]:
		break
for i in list_num:
	print(str(i), end='')


def solve():

	def repl(string, lst):
		ans = ''
		for c in string:
			intc = int(c)
			n = lst[intc - 1]
			ans += str(n)
		return ans
	N = int(input())
	s = input()
	rep_list = list(map(int, input().split()))
	ans_s = ''
	startIndex = None
	for (e, c) in enumerate(s):
		intc = int(c)
		if rep_list[intc - 1] > intc:
			startIndex = e
			break
	if startIndex == None:
		print(s)
		exit()
	endIdx = None
	for (e, c) in enumerate(s[startIndex + 1:], startIndex + 1):
		intc = int(c)
		if rep_list[intc - 1] < intc:
			endIdx = e
			break
	if endIdx == None:
		print(s[:startIndex] + repl(s[startIndex:], rep_list))
		exit()
	print(s[:startIndex] + repl(s[startIndex:endIdx], rep_list) + s[endIdx:])
solve()


nDigits = int(input())
a = list(map(int, list(input())))
f = list(map(int, input().split()))
for i in range(nDigits):
	if f[a[i] - 1] > a[i]:
		j = i
		while j < nDigits and f[a[j] - 1] >= a[j]:
			a[j] = f[a[j] - 1]
			j += 1
		break
print(''.join(map(str, a)))


def main():
	input()
	a = input()
	n = len(a)
	f = {f'{i}': x for (i, x) in enumerate(input().split(' '), start=1)}
	l = 0
	while l < n and int(f[a[l]]) <= int(a[l]):
		l += 1
	r = l
	res = [x for x in a]
	while r < n and int(f[res[r]]) >= int(res[r]):
		res[r] = f[res[r]]
		r += 1
	print(''.join(res))
main()


def run(n, a, f):
	temp = [int(i) for i in a]
	i = 0
	j = -2
	while i < n:
		if f[temp[i] - 1] > temp[i]:
			j = i
			while j < n and f[temp[j] - 1] >= temp[j]:
				j += 1
			break
		else:
			i += 1
	if j >= 0:
		for k in range(i, j):
			temp[k] = f[temp[k] - 1]
	return temp
n = int(input())
a = input()
f = list(map(int, input().split()))
res = run(n, a, f)
for i in res:
	print(i, end='')


n = int(input())
s = list(input())
f = [0] + list(map(int, input().split()))
flag = 0
for i in range(n):
	v = int(s[i])
	if f[v] > v and (not flag):
		s[i] = str(f[v])
		flag = 1
	elif flag and f[v] >= v:
		s[i] = str(f[v])
	elif flag and f[v] < v:
		break
print(''.join(s))


import math
import bisect
import heapq
from collections import defaultdict

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, x, y) = egcd(b % a, a)
		return (g, y - b // a * x, x)

def mulinv(b, n):
	(g, x, _) = egcd(b, n)
	if g == 1:
		return x % n
primes = []

def isprime(n):
	for d in range(2, int(math.sqrt(n)) + 1):
		if n % d == 0:
			return False
	return True

def argsort(ls):
	return sorted(range(len(ls)), key=ls.__getitem__)

def f(p=0):
	if p == 1:
		return map(int, input().split())
	elif p == 2:
		return list(map(int, input().split()))
	else:
		return int(input())
n = f()
a = input()
cl = f(2)
e = []
for i in range(n):
	e.append(int(a[i]))
a = e
st = False
for i in range(n):
	if cl[a[i] - 1] > a[i]:
		a[i] = cl[a[i] - 1]
		st = True
	elif cl[a[i] - 1] != a[i] and st:
		break
res = ''
for x in a:
	res += str(x)
print(res)


n = int(input())
m = list(map(int, input()))
rule = list(map(int, input().split()))
flag = False
for i in range(n):
	r = rule[m[i] - 1]
	if flag:
		if m[i] <= r:
			m[i] = r
		else:
			break
	elif m[i] < r:
		m[i] = r
		flag = True
print(''.join((str(i) for i in m)))


n = int(input())
l = list(map(int, input()))
j = True
List = list(map(int, input().split()))
for i in range(n):
	if j == True:
		if List[l[i] - 1] > l[i]:
			j = False
			l[i] = List[int(l[i]) - 1]
	elif List[l[i] - 1] >= l[i]:
		l[i] = List[int(l[i]) - 1]
	else:
		break
print(''.join(map(str, l)))


n = int(input())
a = [int(x) for x in input()]
f = [int(x) for x in input().split(' ')]
i = 0
out = []
while i < n and f[a[i] - 1] <= a[i]:
	out.append(a[i])
	i += 1
while i < n and f[a[i] - 1] >= a[i]:
	out.append(f[a[i] - 1])
	i += 1
while i < n:
	out.append(a[i])
	i += 1
print(''.join((str(x) for x in out)))


n = int(input())
a = list(input())
f = input().split()
d = {str(i + 1): f[i] for i in range(9)}
flag = False
for i in range(n):
	if a[i] < d[a[i]]:
		a[i] = d[a[i]]
		flag = True
	elif a[i] > d[a[i]] and flag == True:
		break
print(''.join(a))