import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.concurrent.PriorityBlockingQueue;

public class LongNumber {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int n = sc.nextInt();
		String x = sc.next();
		HashMap<Integer, Integer> hm = new HashMap<>();
		int[] arr = new int[9];
		for(int i=1 ; i<=9 ; i++) {
			int num = sc.nextInt();
			if(i<num)
				hm.put(i, num);
		}
		String res = "";
		for(int i=0 ; i<n ; i++) {
			if(hm.containsKey(Integer.parseInt(x.charAt(i)+"")))
				res +=hm.get(Integer.parseInt(x.charAt(i)+""));
			else
				res += Integer.parseInt(x.charAt(i)+"");
		}
		System.out.println(res);

	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}


import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.concurrent.PriorityBlockingQueue;

public class LongNumber {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int n = sc.nextInt();
		String x = sc.next();
		HashMap<Integer, Integer> hm = new HashMap<>();
		int[] arr = new int[9];
		for(int i=1 ; i<=9 ; i++) {
			int num = sc.nextInt();
			if(i<num)
				hm.put(i, num);
		}
		StringBuilder res = new StringBuilder();
		for(int i=0 ; i<n ; i++) {
			if(hm.containsKey(Integer.parseInt(x.charAt(i)+"")))
				res.append(hm.get(Integer.parseInt(x.charAt(i)+"")));
			else
				res.append(Integer.parseInt(x.charAt(i)+""));
		}
		System.out.println(res);

	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}


import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.concurrent.PriorityBlockingQueue;

public class LongNumber {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int n = sc.nextInt();
		String x = sc.next();
		HashMap<Integer, Integer> hm = new HashMap<>();
		int[] arr = new int[n];
		for(int i=0 ; i<n ; i++) {
			arr[i]=Integer.parseInt(x.charAt(i)+"");
		}
		for(int i=1 ; i<=9 ; i++) {
			int num = sc.nextInt();
			if(i<num)
				hm.put(i, num);
		}
		//StringBuilder res = new StringBuilder();
		boolean flag =false;
		for(int i=0 ; i<n ; i++) {
			if(hm.containsKey(Integer.parseInt(x.charAt(i)+""))) {
				//res.append(hm.get(Integer.parseInt(x.charAt(i)+"")));
				arr[i]=hm.get(Integer.parseInt(x.charAt(i)+""));
				flag = true;
			}
			else {
				if(!(hm.containsKey(Integer.parseInt(x.charAt(i)+"")))  && flag)
					break;
				//res.append(x.substring(i-1));
			}
		}
		for(int i=0 ; i<n ; i++) {
			System.out.print(arr[i]);
		}

	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}


import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.concurrent.PriorityBlockingQueue;

public class LongNumber {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int n = sc.nextInt();
		String x = sc.next();
		HashMap<Integer, Integer> hm = new HashMap<>();
		int[] arr = new int[n];
		for(int i=0 ; i<n ; i++) {
			arr[i]=Integer.parseInt(x.charAt(i)+"");
		}
		for(int i=1 ; i<=9 ; i++) {
			int num = sc.nextInt();
			if(i<num)
				hm.put(i, num);
		}
		//StringBuilder res = new StringBuilder();
		boolean flag =false;
		for(int i=0 ; i<n ; i++) {
			if(hm.containsKey(Integer.parseInt(x.charAt(i)+""))) {
				//res.append(hm.get(Integer.parseInt(x.charAt(i)+"")));
				arr[i]=hm.get(Integer.parseInt(x.charAt(i)+""));
				flag = true;
			}
			else {
				if(hm.containsKey(Integer.parseInt(x.charAt(i)+"")) && flag)
					break;
				//res.append(x.substring(i-1));
			}
		}
		for(int i=0 ; i<n ; i++) {
			System.out.print(arr[i]);
		}

	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}


import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.concurrent.PriorityBlockingQueue;

public class LongNumber {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int n = sc.nextInt();
		String x = sc.next();
		HashMap<Integer, Integer> hm = new HashMap<>();
		int[] arr = new int[n];
		for(int i=0 ; i<n ; i++) {
			arr[i]=Integer.parseInt(x.charAt(i)+"");
		}
		for(int i=1 ; i<=9 ; i++) {
			int num = sc.nextInt();
			if(i<=num)
				hm.put(i, num);
		}
		//StringBuilder res = new StringBuilder();
		boolean flag =false;
		for(int i=0 ; i<n ; i++) {
			if(hm.containsKey(Integer.parseInt(x.charAt(i)+""))) {
				//res.append(hm.get(Integer.parseInt(x.charAt(i)+"")));
				arr[i]=hm.get(Integer.parseInt(x.charAt(i)+""));
				flag = true;
			}
			else {
				if(!(hm.containsKey(Integer.parseInt(x.charAt(i)+"")))  && flag)
					break;
				//res.append(x.substring(i-1));
			}
		}
		for(int i=0 ; i<n ; i++) {
			System.out.print(arr[i]);
		}

	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}


import java.util.ArrayList;
import java.util.Scanner;
public class Long_Number {

	public static void main(String args[]) {

		Scanner sc=new Scanner(System.in);

		String s=sc.next();

		int ar[]=new int[10];
		for(int i=1;i<=9;i++)
			ar[i]=sc.nextInt();

		find(ar,s);
	}
	public static void find(int ar[],String s) {

		ArrayList<Integer> al = new ArrayList<Integer>();
		int i=0,flag=0;

		while(i<s.length()) {

			int n= Integer.parseInt(String.valueOf(s.charAt(i)));

			if(n < ar[n]) {
				if(flag==0)
					flag=1;
				al.add(ar[n]);
			}

			else if(n >= ar[n]){

				if(flag==1)
					break;
				else
					al.add(n);
			}

			i++;

		}

		if(al.size()<s.length()) {
			for(;i<s.length();i++)
				al.add(Integer.parseInt(String.valueOf(s.charAt(i))));
		}
		for(int k : al)
			System.out.print(k);
	}
}


import java.util.ArrayList;
import java.util.Scanner;
public class Long_Number {

	public static void main(String args[]) {

		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();

		String s=sc.next();

		int ar[]=new int[10];
		for(int i=1;i<=9;i++)
			ar[i]=sc.nextInt();

		find(ar,s);
	}
	public static void find(int ar[],String s) {

		ArrayList<Integer> al = new ArrayList<Integer>();
		int i=0,flag=0;

		while(i<s.length()) {

			int n= Integer.parseInt(String.valueOf(s.charAt(i)));

			if(n <= ar[n]) {
				if(n<ar[n] && flag==0)
					flag=1;
				al.add(ar[n]);
			}

			else if(n >= ar[n]){

				if(flag==1)
					break;
				else
					al.add(n);
			}

			i++;

		}

		if(al.size()<s.length()) {
			for(;i<s.length();i++)
				al.add(Integer.parseInt(String.valueOf(s.charAt(i))));
		}
		for(int k : al)
			System.out.print(k);
	}
}


import java.util.ArrayList;
import java.util.Scanner;
public class Long_Number {

	public static void main(String args[]) {

		Scanner sc=new Scanner(System.in);
	//	System.out.println("Enter the number :");
		String s=sc.next();
	//	System.out.println("Enter the integers :");
		int ar[]=new int[10];
		for(int i=1;i<=9;i++)
			ar[i]=sc.nextInt();

		find(ar,s);
	}
	public static void find(int ar[],String s) {

		ArrayList<Integer> al = new ArrayList<Integer>();
		int i=0,flag=0;

		while(i<s.length()) {

			int n= Integer.parseInt(String.valueOf(s.charAt(i)));

			if(n < ar[n]) {
				if(flag==0)
					flag=1;
				al.add(ar[n]);
			}

			else if(n >= ar[n]){

				if(flag==1)
					break;
				else
					al.add(n);
			}

			i++;

		}

		if(al.size()<s.length()) {
			for(;i<s.length();i++)
				al.add(Integer.parseInt(String.valueOf(s.charAt(i))));
		}
		for(int k : al)
			System.out.print(k);
	}
}


import java.util.ArrayList;
import java.util.Scanner;
public class Long_Number {

	public static void main(String args[]) {

		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();

		String s=sc.next();

		int ar[]=new int[10];
		for(int i=1;i<=9;i++)
			ar[i]=sc.nextInt();

		find(ar,s);
	}
	public static void find(int ar[],String s) {

		ArrayList<Integer> al = new ArrayList<Integer>();
		int i=0,flag=0;

		while(i<s.length()) {

			int n= Integer.parseInt(String.valueOf(s.charAt(i)));
			//1337
			//1 2 5 4 6 6 3 1 9
			if(n < ar[n]) {
				if(flag==0)
					flag=1;
				al.add(ar[n]);
			}

			else if(n >= ar[n]){

				if(flag==1)
					break;
				else
					al.add(n);
			}

			i++;

		}

		if(al.size()<s.length()) {
			for(;i<s.length();i++)
				al.add(Integer.parseInt(String.valueOf(s.charAt(i))));
		}
		for(int k : al)
			System.out.print(k);
	}
}


import java.util.ArrayList;
import java.util.Scanner;
public class Long_Number {

	public static void main(String args[]) {

		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();

		String s=sc.next();

		int ar[]=new int[10];
		for(int i=1;i<=9;i++)
			ar[i]=sc.nextInt();

		find(ar,s);
	}
	public static void find(int ar[],String s) {

		ArrayList<Integer> al = new ArrayList<Integer>();
		int i=0,flag=0;

		while(i<s.length()) {

			int n= Integer.parseInt(String.valueOf(s.charAt(i)));

			if(n <= ar[n]) {
				if(flag==0)
					flag=1;
				al.add(ar[n]);
			}

			else if(n > ar[n]){

				if(flag==1)
					break;
				else
					al.add(n);
			}

			i++;

		}

		if(al.size()<s.length()) {
			for(;i<s.length();i++)
				al.add(Integer.parseInt(String.valueOf(s.charAt(i))));
		}
		for(int k : al)
			System.out.print(k);
	}
}


import java.util.ArrayList;
import java.util.Scanner;
public class Long_Number {

	public static void main(String args[]) {

		Scanner sc=new Scanner(System.in);
		int n = sc.nextInt();
		String s=sc.next();
		int ar[]=new int[10];
		for(int i=1;i<=9;i++)
			ar[i]=sc.nextInt();

		find(ar,s);
	}
	public static void find(int ar[],String s) {

		ArrayList<Integer> al = new ArrayList<Integer>();
		int i=0,flag=0;

		while(i<s.length()) {

			int n= Integer.parseInt(String.valueOf(s.charAt(i)));

			if(n < ar[n]) {
				if(flag==0)
					flag=1;
				al.add(ar[n]);
			}

			else if(n >= ar[n]){

				if(flag==1)
					break;
				else
					al.add(n);
			}

			i++;

		}

		if(al.size()<s.length()) {
			for(;i<s.length();i++)
				al.add(Integer.parseInt(String.valueOf(s.charAt(i))));
		}
		for(int k : al)
			System.out.print(k);
	}
}


import java.util.ArrayList;
import java.util.Scanner;
public class Long_Number {

	public static void main(String args[]) {

		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		String s=sc.next();
		int ar[]=new int[10];
		for(int i=1;i<=9;i++)
			ar[i]=sc.nextInt();

		find(ar,s);
	}
	public static void find(int ar[],String s) {

		ArrayList<Integer> al = new ArrayList<Integer>();
		int i=0,flag=0;

		while(i<s.length()) {

			int n= Integer.parseInt(String.valueOf(s.charAt(i)));
			if(n < ar[n]) {
				if(flag==0)
					flag=1;
				al.add(ar[n]);
			}

			else if(n >= ar[n]){

				if(flag==1)
					break;
				else
					al.add(n);
			}

			i++;

		}

		if(al.size()<s.length()) {
			for(;i<s.length();i++)
				al.add(Integer.parseInt(String.valueOf(s.charAt(i))));
		}
		for(int k : al)
			System.out.print(k);
	}
}


import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        scanner.nextLine();
        String number=scanner.nextLine()+" ";
        int mas[]=new int[n];
        int masf[]=new int[9];
        boolean first=true;
        for(int i=0; i<9; i++){
            masf[i]=scanner.nextInt();
        }
        int temp=0;
        for(int i=0; i<n; i++){
            mas[i]=Integer.parseInt(number.substring(i,i+1));
            if(first && mas[i]<masf[mas[i]-1]){
                temp=mas[i];
                if(i<n && mas[i]!=temp)
                    first=false;
                mas[i]=masf[mas[i]-1];
            }
            System.out.print(mas[i]);
        }

    }
}


import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        scanner.nextLine();
        String number=scanner.nextLine();
        int mas[]=new int[n];
        int masf[]=new int[9];
        boolean flag=true;
        boolean first=false;
        for(int i=0; i<9; i++){
            masf[i]=scanner.nextInt();
        }
        for(int i=0; i<n; i++){
            mas[i]=Integer.parseInt(number.substring(i,i+1));
            if(mas[i]<=masf[mas[i]-1] && flag){
                mas[i]=masf[mas[i]-1];
                first=true;
            }
            else if(first) flag=false;
            System.out.print(mas[i]);
        }

    }
}


import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        scanner.nextLine();
        String number=scanner.nextLine();
        int mas[]=new int[n];
        int masf[]=new int[9];
        boolean flag=true;
        boolean first=false;
        for(int i=0; i<9; i++){
            masf[i]=scanner.nextInt();
        }
        for(int i=0; i<n; i++){
            mas[i]=Integer.parseInt(number.substring(i,i+1));
            if(mas[i]<masf[mas[i]-1] && flag){
                mas[i]=masf[mas[i]-1];
                first=true;
            }
            else if(first) flag=false;
            System.out.print(mas[i]);
        }

    }
}


import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        scanner.nextLine();
        String number=scanner.nextLine();
        int mas[]=new int[n];
        int masf[]=new int[9];
        for(int i=0; i<9; i++){
            masf[i]=scanner.nextInt();
        }
        for(int i=0; i<n; i++){
            mas[i]=Integer.parseInt(number.substring(i,i+1));
            if(mas[i]<masf[mas[i]-1]) mas[i]=masf[mas[i]-1];
            System.out.print(mas[i]);
        }

    }
}


import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        scanner.nextLine();
        String number=scanner.nextLine();
        int mas[]=new int[n];
        int masf[]=new int[9];
        boolean flag=true;
        boolean first=false;
        for(int i=0; i<9; i++){
            masf[i]=scanner.nextInt();
        }
        for(int i=0; i<n; i++){
            mas[i]=Integer.parseInt(number.substring(i,i+1));
            if(first && mas[i]==masf[mas[i]-1] && flag) continue;
            if(mas[i]<masf[mas[i]-1] && flag){
                mas[i]=masf[mas[i]-1];
                first=true;
            }
            else if(first) flag=false;
            System.out.print(mas[i]);
        }

    }
}


import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        scanner.nextLine();
        String number=scanner.nextLine();
        int mas[]=new int[n];
        int masf[]=new int[9];
        boolean flag=true;
        boolean first=false;
        for(int i=0; i<9; i++){
            masf[i]=scanner.nextInt();
        }
        for(int i=0; i<n; i++){
            mas[i]=Integer.parseInt(number.substring(i,i+1));
            if(first && mas[i]==masf[mas[i]-1] && flag) {
                System.out.print(mas[i]);
                continue;
            }
            if(mas[i]<masf[mas[i]-1] && flag){
                mas[i]=masf[mas[i]-1];
                first=true;
            }
            else if(first) flag=false;
            System.out.print(mas[i]);
        }

    }
}


import java.io.*;
import java.util.*;
import java.lang.*;

public class GFG {
	public static void main (String[] args) {
	PrintWriter pw=new PrintWriter(System.out);
	Scanner sc=new Scanner(System.in);
	HashMap<Integer,Integer> map=new HashMap<>();
    int n=sc.nextInt();
  StringBuilder s= new StringBuilder(sc.next());
  HashSet<Character> h = new HashSet<Character>();
    h.add(s.charAt(n-1));
   h.add(s.charAt(n-2));
    h.add(s.charAt(n-3));
   for(int i=1;i<=9;i++)
     map.put(i,sc.nextInt());
   for(int i=n-1;i>=0;i--)
         if(((int)s.charAt(i)<map.get((int)s.charAt(i)-48)+48)&&h.contains(s.charAt(i)))
            s.setCharAt(i,(char)(map.get((int)(s.charAt(i)-48))+48));

  pw.print(h.contains('3'));
	pw.close();
	}
}


import java.io.*;
import java.util.*;
import java.lang.*;

public class GFG {
	public static void main (String[] args) {
	PrintWriter pw=new PrintWriter(System.out);
	Scanner sc=new Scanner(System.in);
	HashMap<Integer,Integer> map=new HashMap<>();
    int n=sc.nextInt();
  StringBuilder s= new StringBuilder(sc.next());
  HashSet<Character> h = new HashSet<Character>();
    h.add(s.charAt(n-1));
   h.add(s.charAt(n-2));
    h.add(s.charAt(n-2));
   for(int i=1;i<=9;i++)
     map.put(i,sc.nextInt());
   for(int i=n-1;i>=0;i--)
         if(((int)s.charAt(i)<map.get((int)s.charAt(i)-48)+48)&&h.contains(s.charAt(i)))
            s.setCharAt(i,(char)(map.get((int)(s.charAt(i)-48))+48));

  pw.print(s);
	pw.close();
	}
}


import java.io.*;
import java.util.*;
import java.lang.*;

public class GFG {
	public static void main (String[] args) {
	PrintWriter pw=new PrintWriter(System.out);
	Scanner sc=new Scanner(System.in);
	HashMap<Integer,Integer> map=new HashMap<>();
    int n=sc.nextInt();
  StringBuilder s= new StringBuilder(sc.next());
  HashSet<Character> h = new HashSet<Character>();
    h.add(s.charAt(n-1));
   h.add(s.charAt(n-2));
    h.add(s.charAt(n-3));
   for(int i=1;i<=9;i++)
     map.put(i,sc.nextInt());
   for(int i=n-1;i>=0;i--)
         if(((int)s.charAt(i)<map.get((int)s.charAt(i)-48)+48)&&h.contains(s.charAt(i)))
            s.setCharAt(i,(char)(map.get((int)(s.charAt(i)-48))+48));

  pw.print(s);
	pw.close();
	}
}


import java.io.*;
import java.util.*;
import java.lang.*;

public class GFG {
	public static void main (String[] args) {
	PrintWriter pw=new PrintWriter(System.out);
	Scanner sc=new Scanner(System.in);
	HashMap<Integer,Integer> map=new HashMap<>();
int n=sc.nextInt();
    String s=sc.next();

  int count=0,min=0,k=0;
   for(int i=1;i<=9;i++)
     map.put(i,sc.nextInt());
     while(k<n&&((int)s.charAt(k)-48>=map.get((int)s.charAt(k)-48))){

       pw.print(s.charAt(k));
        k++;

     }
   while(k<n&&((int)s.charAt(k)-48<map.get((int)s.charAt(k)-48))){

       pw.print(map.get((int)s.charAt(k)-48));

        k++;
     }
     while(k<n){
  pw.print(s.charAt(k));

    k++;
     }

	pw.close();
	}
}


import java.io.*;
import java.util.*;
import java.lang.*;

public class GFG {
	public static void main (String[] args) {
	PrintWriter pw=new PrintWriter(System.out);
	Scanner sc=new Scanner(System.in);
	HashMap<Integer,Integer> map=new HashMap<>();
    int n=sc.nextInt(),k=1;
  StringBuilder s= new StringBuilder(sc.next());
  HashSet<Character> h = new HashSet<Character>();
  while(k<=3&&k<=n){
    h.add(s.charAt(n-k));
     k++;}
   for(int i=1;i<=9;i++)
     map.put(i,sc.nextInt());
   for(int i=n-1;i>=0;i--)
         if(((int)s.charAt(i)<map.get((int)s.charAt(i)-48)+48)&&h.contains(s.charAt(i)))
            s.setCharAt(i,(char)(map.get((int)(s.charAt(i)-48))+48));

  pw.print(s);
	pw.close();
	}
}


import java.io.*;
import java.util.*;
import java.lang.*;

public class GFG {
	public static void main (String[] args) {
	PrintWriter pw=new PrintWriter(System.out);
	Scanner sc=new Scanner(System.in);
	HashMap<Integer,Integer> map=new HashMap<>();
int n=sc.nextInt();
    String s=sc.next();

  int count=0,min=0,k=0;
   for(int i=1;i<=9;i++)
     map.put(i,sc.nextInt());
     while(k<n&&((int)s.charAt(k)-48>=map.get((int)s.charAt(k)-48))){

       pw.print(s.charAt(k));
        k++;

     }
   while(k<n&&((int)s.charAt(k)-48<=map.get((int)s.charAt(k)-48))){

       pw.print(map.get((int)s.charAt(k)-48));

        k++;
     }
     while(k<n){
  pw.print(s.charAt(k));

    k++;
     }

	pw.close();
	}
}


//package Codeforces.Contest.Div3.R_555.B;

import java.io.*;
import java.util.*;

public class B {

    public void solve(int n, String a, int[] digitMapping) {
        StringBuilder stringBuilder = new StringBuilder(a);
        for (int i = 0; i < a.length(); i++) {
            int charAt = stringBuilder.charAt(i) - '0';
            if (charAt > digitMapping[charAt])break;
            stringBuilder.setCharAt(i, (char) (digitMapping[charAt] + '0'));
        }
        out.println(stringBuilder);
    }

    public static PrintWriter out;
    public static void main(String[] args) throws IOException {

        boolean fileInOut = B.class.getPackage() != null;

        Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(fileInOut ? B.class.getResourceAsStream("in.txt") : System.in)));
        out = new PrintWriter(new BufferedOutputStream(fileInOut ? new FileOutputStream("out.txt") : System.out), true);

        int totalTestCase = fileInOut ? sc.nextInt() : 1;

        for (int testCaseNumber = 1; testCaseNumber <= totalTestCase; testCaseNumber++) {
            int N = sc.nextInt();
            String A = sc.next();
            int[] digitMapping = new int[10];
            for (int i = 1; i <= 9; i++) {
                digitMapping[i] = sc.nextInt();
            }
            new B().solve(N, A, digitMapping);
        }

        if (fileInOut) {

            String outputFile = "out.txt";

            BufferedReader reader1 = new BufferedReader(new FileReader(B.class.getResource("ans.txt").getFile()));
            BufferedReader reader2 = new BufferedReader(new FileReader(outputFile));

            String line1 = reader1.readLine();
            String line2 = reader2.readLine();

            boolean areEqual = true;
            int lineNum = 1;

            while (line1 != null || line2 != null) {

                if (line1 == null || line2 == null) {

                    areEqual = false;
                    break;
                } else if (!line1.equals(line2)) {

                    areEqual = false;
                    break;
                }

                line1 = reader1.readLine();
                line2 = reader2.readLine();

                lineNum++;
            }

            if (areEqual) {

                System.out.println("All Test Cases Passed !");
            } else {

                System.out.println("Output differ at line " + lineNum);
                System.out.println("ans.txt has " + line1 + " and out.txt has " + line2 + " at line " + lineNum);
            }

            reader1.close();
            reader2.close();
        }
    }
}


//package codeforces.R555D3.LongNumber;

import java.io.*;
import java.util.*;

public class LongNumber {

    public static void main(String[] args) throws IOException {

        boolean fileInOut = LongNumber.class.getPackage() != null;

        Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(fileInOut ? LongNumber.class.getResourceAsStream("in.txt") : System.in)));
        Solution.out = new PrintWriter(new BufferedOutputStream(fileInOut ? new FileOutputStream("out.txt") : System.out), true);

        int testCase = fileInOut ? sc.nextInt() : 1;

        for (int i = 0; i < testCase; i++) {

            int aLength = sc.nextInt();
            char[] aCh = sc.next().toCharArray();

            int[] a = new int[aLength];
            for (int j = 0; j < aLength; j++) {
                a[j] = (int) aCh[j] - 48;
            }

            int[] mapping = new int[10];
            for (int j = 1; j <= 9 ; j++) {
                mapping[j] = sc.nextInt();
            }

            new Solution().solve(a, mapping);
        }

        if (fileInOut) {

            verify(LongNumber.class.getResource("ans.txt").getFile());
        }
    }

    public static void verify(String ansFile) throws IOException {

        String outputFile = "out.txt";

        BufferedReader reader1 = new BufferedReader(new FileReader(ansFile));
        BufferedReader reader2 = new BufferedReader(new FileReader(outputFile));

        String line1 = reader1.readLine();
        String line2 = reader2.readLine();

        boolean areEqual = true;
        int lineNum = 1;

        while (line1 != null || line2 != null) {

            if (line1 == null || line2 == null) {

                areEqual = false;
                break;
            } else if (!line1.equals(line2)) {

                areEqual = false;
                break;
            }

            line1 = reader1.readLine();
            line2 = reader2.readLine();

            lineNum++;
        }

        if (areEqual) {

            System.out.println("All Test Cases Passed !");
        } else {

            System.out.println("Output differ at line " + lineNum);
            System.out.println("ans.txt has " + line1 + " and out.txt has " + line2 + " at line " + lineNum);
        }

        reader1.close();
        reader2.close();
    }
}

class Solution {

    public static PrintWriter out;

    public void solve(int[] a, int[] mapping) {

        int i = 0;
        for (; i < a.length; i++) {

            if (a[i] >= mapping[a[i]]) {
                continue;
            }

            break;
        }

        for (int j = i; j < a.length; j++) {
            if (a[j] <= mapping[a[j]]) {
                a[j] = mapping[a[j]];
                continue;
            }
            break;
        }

        for (int j = 0; j < a.length; j++) {
            out.print(a[j]);
        }

        out.println();
    }
}


//package Codeforces.Contest.Div3.R_555.B;

import java.io.*;
import java.util.*;

public class B {

    public void solve(int n, String a, int[] digitMapping) {

        boolean started = false;
        StringBuilder stringBuilder = new StringBuilder(a);

        for (int i = 0; i < a.length(); i++) {
            int charAt = stringBuilder.charAt(i) - '0';

            if (charAt >= digitMapping[charAt] && !started)continue;
            if (charAt >= digitMapping[charAt] && started)break;

            stringBuilder.setCharAt(i, (char) (digitMapping[charAt] + '0'));
            started = true;
        }
        out.println(stringBuilder);
    }

    public static PrintWriter out;
    public static void main(String[] args) throws IOException {

        boolean fileInOut = B.class.getPackage() != null;

        Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(fileInOut ? B.class.getResourceAsStream("in.txt") : System.in)));
        out = new PrintWriter(new BufferedOutputStream(fileInOut ? new FileOutputStream("out.txt") : System.out), true);

        int totalTestCase = fileInOut ? sc.nextInt() : 1;

        for (int testCaseNumber = 1; testCaseNumber <= totalTestCase; testCaseNumber++) {
            int N = sc.nextInt();
            String A = sc.next();
            int[] digitMapping = new int[10];
            for (int i = 1; i <= 9; i++) {
                digitMapping[i] = sc.nextInt();
            }
            new B().solve(N, A, digitMapping);
        }

        if (fileInOut) {

            String outputFile = "out.txt";

            BufferedReader reader1 = new BufferedReader(new FileReader(B.class.getResource("ans.txt").getFile()));
            BufferedReader reader2 = new BufferedReader(new FileReader(outputFile));

            String line1 = reader1.readLine();
            String line2 = reader2.readLine();

            boolean areEqual = true;
            int lineNum = 1;

            while (line1 != null || line2 != null) {

                if (line1 == null || line2 == null) {

                    areEqual = false;
                    break;
                } else if (!line1.equals(line2)) {

                    areEqual = false;
                    break;
                }

                line1 = reader1.readLine();
                line2 = reader2.readLine();

                lineNum++;
            }

            if (areEqual) {

                System.out.println("All Test Cases Passed !");
            } else {

                System.out.println("Output differ at line " + lineNum);
                System.out.println("ans.txt has " + line1 + " and out.txt has " + line2 + " at line " + lineNum);
            }

            reader1.close();
            reader2.close();
        }
    }
}


//package Codeforces.Contest.Div3.R_555.B;

import java.io.*;
import java.util.*;

public class B {

    public void solve(int n, String a, int[] digitMapping) {

        boolean started = false;
        StringBuilder stringBuilder = new StringBuilder(a);

        for (int i = 0; i < a.length(); i++) {
            int charAt = stringBuilder.charAt(i) - '0';

            if (charAt >= digitMapping[charAt] && !started)continue;
            if (charAt > digitMapping[charAt] && started)break;

            stringBuilder.setCharAt(i, (char) (digitMapping[charAt] + '0'));
            started = true;
        }
        out.println(stringBuilder);
    }

    public static PrintWriter out;
    public static void main(String[] args) throws IOException {

        boolean fileInOut = B.class.getPackage() != null;

        Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(fileInOut ? B.class.getResourceAsStream("in.txt") : System.in)));
        out = new PrintWriter(new BufferedOutputStream(fileInOut ? new FileOutputStream("out.txt") : System.out), true);

        int totalTestCase = fileInOut ? sc.nextInt() : 1;

        for (int testCaseNumber = 1; testCaseNumber <= totalTestCase; testCaseNumber++) {
            int N = sc.nextInt();
            String A = sc.next();
            int[] digitMapping = new int[10];
            for (int i = 1; i <= 9; i++) {
                digitMapping[i] = sc.nextInt();
            }
            new B().solve(N, A, digitMapping);
        }

        if (fileInOut) {

            String outputFile = "out.txt";

            BufferedReader reader1 = new BufferedReader(new FileReader(B.class.getResource("ans.txt").getFile()));
            BufferedReader reader2 = new BufferedReader(new FileReader(outputFile));

            String line1 = reader1.readLine();
            String line2 = reader2.readLine();

            boolean areEqual = true;
            int lineNum = 1;

            while (line1 != null || line2 != null) {

                if (line1 == null || line2 == null) {

                    areEqual = false;
                    break;
                } else if (!line1.equals(line2)) {

                    areEqual = false;
                    break;
                }

                line1 = reader1.readLine();
                line2 = reader2.readLine();

                lineNum++;
            }

            if (areEqual) {

                System.out.println("All Test Cases Passed !");
            } else {

                System.out.println("Output differ at line " + lineNum);
                System.out.println("ans.txt has " + line1 + " and out.txt has " + line2 + " at line " + lineNum);
            }

            reader1.close();
            reader2.close();
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) {
        FastReader sc = new FastReader();

        int n = sc.nextInt();
        char[] number = sc.next().toCharArray();
        int[] map = new int[9];

        for (int i = 0; i < 9; i++) {
            map[i] = sc.nextInt();
        }

        boolean start = false;

        for (int i = 0; i < number.length; i++) {

            int newValue = map[Character.getNumericValue(number[i]) - 1];

            if (newValue > Character.getNumericValue(number[i])) {
                if (!start) {
                    start = true;
                }

                number[i] = Character.forDigit(newValue, 10);

            } else {
                if (start) {
                    System.out.println(new String(number));
                    return;
                }
            }

        }

        System.out.println(new String(number));
    }

    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) {
        FastReader sc = new FastReader();

        int n = sc.nextInt();
        char[] number = sc.next().toCharArray();
        int[] map = new int[9];

        for (int i = 0; i < 9; i++) {
            map[i] = sc.nextInt();
        }

        boolean start = false;

        for (int i = 0; i < number.length; i++) {

            int newValue = map[Character.getNumericValue(number[i]) - 1];

            if (newValue > Character.getNumericValue(number[i])) {
                if (!start) {
                    start = true;
                }

                number[i] = (char) (newValue + 48);

            } else {
                if (start) {
                    System.out.println(new String(number));
                    return;
                }
            }

        }

        System.out.println(new String(number));
    }

    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) {
        FastReader sc = new FastReader();

        int n = sc.nextInt();
        char[] number = sc.next().toCharArray();
        int[] map = new int[9];

        for (int i = 0; i < 9; i++) {
            map[i] = sc.nextInt();
        }

        boolean start = false;

        for (int i = 0; i < number.length; i++) {

            int newValue = map[Character.getNumericValue(number[i]) - 1];
            int oldValue = Character.getNumericValue(number[i]);

            if (newValue > oldValue) {
                if (!start) {
                    start = true;
                }

                number[i] = (char) (newValue + 48);

            } else {
                if (start && newValue != oldValue) {
                    System.out.println(new String(number));
                    return;
                }
            }

        }

        System.out.println(new String(number));
    }

    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) {

        FastReader sc = new FastReader();

        int n = sc.nextInt();
        char[] number = sc.next().toCharArray();
        int[] map = new int[9];

        for (int i = 0; i < 9; i++) {
            map[i] = sc.nextInt();
        }

        boolean start = false;

        for (int i = 0; i < number.length; i++) {

            int newValue = map[Character.getNumericValue(number[i]) - 1];

            if (newValue >= Character.getNumericValue(number[i])) {
                if (!start) {
                    start = true;
                }

                number[i] = Character.forDigit(newValue, 10);

            } else {
                if (start) {
                    System.out.println(new String(number));
                    return;
                }
            }

        }

        System.out.println(new String(number));
    }

    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.util.*;

public class MainB {
   public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      int count = s.nextInt();
      // System.out.println(count);
      String NUM = s.next();
      int[] func = new int[10];
      func[0] = 0;
      for (int i = 1; i <= 9; i++) {
         func[i] = s.nextInt();
      //    System.out.println(i + " " + func[i]);
      }

      int FirstGain = 0;
      // System.out.println(func[Integer.parseInt(String.valueOf(NUM.charAt(FirstGain)))]);
      while (FirstGain < count && (func[NUM.charAt(FirstGain)-'0'] <= NUM.charAt(FirstGain)-'0' ) ) {
         FirstGain++;
      }

      int SecondGain = FirstGain;

      while (SecondGain < count && (func[NUM.charAt(SecondGain)-'0'] >= NUM.charAt(SecondGain)-'0')) {
         SecondGain++;
      }

      // System.out.println(FirstGain);
      // System.out.println(SecondGain);

      // switch between F and S
      NUM = NUM.substring(0, FirstGain) + helper(NUM.substring(FirstGain, SecondGain), func) + NUM.substring(SecondGain);

      System.out.println(NUM);
   }

   public static String helper(String s, int[] func) {
      String result = "";
      int l = s.length();
      for (int i = 0; i < l; i++) {
         result+= func[s.charAt(i)-'0'];
      }
      return result;
   }
}


import java.util.*;

public class MainB {
   public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      int count = s.nextInt();
      // System.out.println(count);
      String NUM = s.next();
      int[] func = new int[10];
      func[0] = 0;
      for (int i = 1; i <= 9; i++) {
         func[i] = s.nextInt();
      //    System.out.println(i + " " + func[i]);
      }

      int FirstGain = 0;
      // System.out.println(func[Integer.parseInt(String.valueOf(NUM.charAt(FirstGain)))]);
      while (FirstGain < count && (func[NUM.charAt(FirstGain)-'0'] <= NUM.charAt(FirstGain)-'0' ) ) {
         FirstGain++;
      }

      int SecondGain = FirstGain;

      while (SecondGain < count && (func[NUM.charAt(SecondGain)-'0'] >= NUM.charAt(SecondGain)-'0')) {
         SecondGain++;
      }

      // System.out.println(FirstGain);
      // System.out.println(SecondGain);

      // switch between F and S
      System.out.print(NUM.substring(0, FirstGain));
      helper(NUM.substring(FirstGain, SecondGain), func);
      System.out.print(NUM.substring(SecondGain));

     //  System.out.println(NUM);
   }

   public static void helper(String s, int[] func) {
      // String result = "";
      int l = s.length();
      for (int i = 0; i < l; i++) {
         System.out.print(func[s.charAt(i)-'0']);
      }
      // return result;
   }
}


import java.util.*;
import java.lang.*;

public class MainB {
   public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      int count = s.nextInt();
      // System.out.println(count);
      String NUM = s.next();
      int[] func = new int[10];
      func[0] = 0;
      for (int i = 1; i <= 9; i++) {
         func[i] = s.nextInt();
      //    System.out.println(i + " " + func[i]);
      }

      int FirstGain = 0;
      // System.out.println(func[Integer.parseInt(String.valueOf(NUM.charAt(FirstGain)))]);
      while (FirstGain < count && (func[Integer.parseInt(String.valueOf(NUM.charAt(FirstGain)))] <= Integer.parseInt(String.valueOf(NUM.charAt(FirstGain))))) {
         FirstGain++;
      }

      int SecondGain = FirstGain;

      while (SecondGain < count && (func[Integer.parseInt(String.valueOf(NUM.charAt(SecondGain)))] >= Integer.parseInt(String.valueOf(NUM.charAt(SecondGain))))) {
         SecondGain++;
      }

      // System.out.println(FirstGain);
      // System.out.println(SecondGain);

      // switch between F and S
      NUM = NUM.substring(0, FirstGain) + helper(NUM.substring(FirstGain, SecondGain), func) + NUM.substring(SecondGain);

      System.out.println(NUM);
   }

   public static String helper(String s, int[] func) {
      String result = "";
      for (int i = 0; i < s.length(); i++) {
         result+= func[Integer.parseInt(String.valueOf(s.charAt(i)))];
      }
      return result;
   }
}


import java.util.*;
import java.lang.*;

public class MainB {
   public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      int count = s.nextInt();
      // System.out.println(count);
      String NUM = s.next();
      int[] func = new int[10];
      func[0] = 0;
      for (int i = 1; i <= 9; i++) {
         func[i] = s.nextInt();
      //    System.out.println(i + " " + func[i]);
      }

      int FirstGain = 0;
      // System.out.println(func[Integer.parseInt(String.valueOf(NUM.charAt(FirstGain)))]);
      while (FirstGain < count && (func[Integer.parseInt(String.valueOf(NUM.charAt(FirstGain)))] <= Integer.parseInt(String.valueOf(NUM.charAt(FirstGain))))) {
         FirstGain++;
      }

      int SecondGain = FirstGain;

      while (SecondGain < count && (func[Integer.parseInt(String.valueOf(NUM.charAt(SecondGain)))] >= Integer.parseInt(String.valueOf(NUM.charAt(SecondGain))))) {
         SecondGain++;
      }

      // System.out.println(FirstGain);
      // System.out.println(SecondGain);

      // switch between F and S
      while (FirstGain != SecondGain) {
         NUM = NUM.substring(0, FirstGain) + func[Integer.parseInt(String.valueOf(NUM.charAt(FirstGain)))] + NUM.substring(FirstGain + 1);
         FirstGain++;
      }

      System.out.println(NUM);
   }
}


import java.util.*;
import java.lang.*;

public class MainB {
   public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      int count = s.nextInt();
      // System.out.println(count);
      String NUM = s.next();
      int[] func = new int[10];
      func[0] = 0;
      for (int i = 1; i <= 9; i++) {
         func[i] = s.nextInt();
      //    System.out.println(i + " " + func[i]);
      }

      int FirstGain = 0;
      // System.out.println(func[Integer.parseInt(String.valueOf(NUM.charAt(FirstGain)))]);
      while (FirstGain < count && (func[NUM.charAt(FirstGain)-'0'] <= NUM.charAt(FirstGain)-'0' ) ) {
         FirstGain++;
      }

      int SecondGain = FirstGain;

      while (SecondGain < count && (func[NUM.charAt(SecondGain)-'0'] >= NUM.charAt(SecondGain)-'0')) {
         SecondGain++;
      }

      // System.out.println(FirstGain);
      // System.out.println(SecondGain);

      // switch between F and S
      NUM = NUM.substring(0, FirstGain) + helper(NUM.substring(FirstGain, SecondGain), func) + NUM.substring(SecondGain);

      System.out.println(NUM);
   }

   public static String helper(String s, int[] func) {
      String result = "";
      for (int i = 0; i < s.length(); i++) {
         result+= func[s.charAt(i)-'0'];
      }
      return result;
   }
}


import java.util.*;
import java.lang.*;

public class MainB {
   public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      int count = s.nextInt();
      // System.out.println(count);
      String NUM = s.next();
      int[] func = new int[10];
      func[0] = 0;
      for (int i = 1; i <= 9; i++) {
         func[i] = s.nextInt();
      //    System.out.println(i + " " + func[i]);
      }

      int FirstGain = 0;
      // System.out.println(func[Integer.parseInt(String.valueOf(NUM.charAt(FirstGain)))]);
      while (FirstGain < count && (func[Integer.parseInt(""+NUM.charAt(FirstGain))] <= Integer.parseInt(""+NUM.charAt(FirstGain)))) {
         FirstGain++;
      }

      int SecondGain = FirstGain;

      while (SecondGain < count && (func[Integer.parseInt(""+NUM.charAt(SecondGain))] >= Integer.parseInt(""+NUM.charAt(SecondGain)))) {
         SecondGain++;
      }

      // System.out.println(FirstGain);
      // System.out.println(SecondGain);

      // switch between F and S
      NUM = NUM.substring(0, FirstGain) + helper(NUM.substring(FirstGain, SecondGain), func) + NUM.substring(SecondGain);

      System.out.println(NUM);
   }

   public static String helper(String s, int[] func) {
      String result = "";
      for (int i = 0; i < s.length(); i++) {
         result+= func[Integer.parseInt(""+s.charAt(i))];
      }
      return result;
   }
}


import java.util.*;
import java.lang.*;

public class MainB {
   public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      int count = s.nextInt();
      // System.out.println(count);
      String NUM = s.next();
      int[] func = new int[10];
      func[0] = 0;
      for (int i = 1; i <= 9; i++) {
         func[i] = s.nextInt();
      //    System.out.println(i + " " + func[i]);
      }

      int FirstGain = 0;
      // System.out.println(func[Integer.parseInt(String.valueOf(NUM.charAt(FirstGain)))]);
      while (FirstGain < count && (func[Integer.parseInt(String.valueOf(NUM.charAt(FirstGain)))] <= Integer.parseInt(String.valueOf(NUM.charAt(FirstGain))))) {
         FirstGain++;
      }

      int SecondGain = FirstGain;

      while (SecondGain < count && (func[Integer.parseInt(String.valueOf(NUM.charAt(SecondGain)))] > Integer.parseInt(String.valueOf(NUM.charAt(SecondGain))))) {
         SecondGain++;
      }

      // System.out.println(FirstGain);
      // System.out.println(SecondGain);

      // switch between F and S
      while (FirstGain != SecondGain) {
         NUM = NUM.substring(0, FirstGain) + func[Integer.parseInt(String.valueOf(NUM.charAt(FirstGain)))] + NUM.substring(FirstGain + 1);
         FirstGain++;
      }

      System.out.println(NUM);
   }
}


import java.io.*;

public class Solution {
    private void solve() {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            int n = Integer.parseInt(br.readLine(), 10);

            char[] arr = br.readLine().toCharArray();

            String[] strings = br.readLine().split(" ");

            for (int i = 0; i < n; ++i) {
                if (arr[i] < strings[arr[i] - '0' - 1].charAt(0)) {
                    for (int j = i; j < n; ++j) {
                        if (arr[j] <= strings[arr[j] - '0' - 1].charAt(0)) {
                            arr[j] = strings[arr[j] - '0' - 1].charAt(0);
                        } else {
                            break;
                        }
                    }
                    break;
                }
            }

            for (int i = 0; i < n; ++i) {
                System.out.print(arr[i]);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Solution().solve();
    }
}


import java.io.*;

public class Solution {
    private void solve() {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            int n = Integer.parseInt(br.readLine(), 10);

            char[] arr = br.readLine().toCharArray();

            String[] strings = br.readLine().split(" ");

            for (int i = 0; i < n; ++i) {
                if (arr[i] < strings[arr[i] - '0' - 1].charAt(0)) {
                    for (int j = i; j < n; ++j) {
                        if (arr[j] < strings[arr[j] - '0' - 1].charAt(0)) {
                            arr[j] = strings[arr[j] - '0' - 1].charAt(0);
                        } else {
                            break;
                        }
                    }
                    break;
                }
            }

            for (int i = 0; i < n; ++i) {
                System.out.print(arr[i]);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Solution().solve();
    }
}


import java.io.*;

public class Solution {
    private void solve() {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            int n = Integer.parseInt(br.readLine(), 10);

            char[] arr = br.readLine().toCharArray();

            String[] strings = br.readLine().split(" ");

            for (int i = 0; i < n; ++i) {
                if (arr[i] < strings[arr[i] - '0' - 1].charAt(0)) {
                    int curr = arr[i];
                    for (int j = i; j < n; ++j) {
                        if (arr[j] == curr) {
                            arr[j] = strings[arr[j] - '0' - 1].charAt(0);
                        }
                    }
                    break;
                }
            }

            for (int i = 0; i < n; ++i) {
                System.out.print(arr[i]);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Solution().solve();
    }
}


import java.io.*;

public class Solution {
    private void solve() {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            int n = Integer.parseInt(br.readLine(), 10);

            char[] arr = br.readLine().toCharArray();

            String[] strings = br.readLine().split(" ");

            for (int i = 0; i < n; ++i) {
                if (arr[i] <= strings[arr[i] - '0' - 1].charAt(0)) {
                    for (int j = i; j < n; ++j) {
                        if (arr[j] <= strings[arr[j] - '0' - 1].charAt(0)) {
                            arr[j] = strings[arr[j] - '0' - 1].charAt(0);
                        } else {
                            break;
                        }
                    }
                    break;
                }
            }

            for (int i = 0; i < n; ++i) {
                System.out.print(arr[i]);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Solution().solve();
    }
}


import java.io.*;

public class Solution {
    private void solve() {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            int n = Integer.parseInt(br.readLine(), 10);

            char[] arr = br.readLine().toCharArray();

            String[] strings = br.readLine().split(" ");

            for (int i = 0; i < n; ++i) {
                if (arr[i] <= strings[arr[i] - '0' - 1].charAt(0)) {
                    for (int j = i; j <= n; ++j) {
                        if (arr[j] < strings[arr[j] - '0' - 1].charAt(0)) {
                            arr[j] = strings[arr[j] - '0' - 1].charAt(0);
                        } else {
                            break;
                        }
                    }
                    break;
                }
            }

            for (int i = 0; i < n; ++i) {
                System.out.print(arr[i]);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Solution().solve();
    }
}


import java.io.*;

public class Solution {
    static char[] arr;
    private void solve() {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            int n = Integer.parseInt(br.readLine(), 10);

            arr = br.readLine().toCharArray();

            String[] strings = br.readLine().split(" ");

            for (int i = 0; i < n; ++i) {
                if (arr[i] < strings[arr[i] - '0' - 1].charAt(0)) {
                    arr[i] = strings[arr[i] - '0' - 1].charAt(0);
                }
            }

            for (int i = 0; i < n; ++i) {
                System.out.print(arr[i]);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Solution().solve();
    }
}


import java.io.*;

public class Solution {
    private void solve() {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            int n = Integer.parseInt(br.readLine(), 10);

            char[] arr = br.readLine().toCharArray();

            String[] strings = br.readLine().split(" ");

            for (int i = 0; i < n; ++i) {
                if (arr[i] < strings[arr[i] - '0' - 1].charAt(0)) {
                    arr[i] = strings[arr[i] - '0' - 1].charAt(0);
                }
            }

            for (int i = 0; i < n; ++i) {
                System.out.print(arr[i]);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Solution().solve();
    }
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[] a = new int[n];

        String s = scanner.next();
        char[] nums = s.toCharArray();

        for (int i = 0; i < n; i++) {
            a[i] = Integer.valueOf(String.valueOf(nums[i]));
        }

        int[] map = new int[9];
        for (int i = 0; i < 9; i++) {
            map[i] = scanner.nextInt();
        }

        boolean flag = false;
        for (int i = 0; i < n; i++) {
           if (i < 9 && !flag) {
                int res;
                if (map[a[i] - 1] < a[i]) {
                    flag = true;
                    res = a[i];
                } else {
                    res = map[a[i] - 1];
                }
                System.out.print(res);
            }
            else {
                System.out.print(a[i]);
            }
        }
        System.out.println();
    }
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[] a = new int[n];

        char[] nums = scanner.next().toCharArray();
        for (int i = 0; i < n; i++) {
            a[i] = Integer.valueOf(String.valueOf(nums[i]));
        }

        int[] map = new int[9];
        for (int i = 0; i < 9; i++) {
            map[i] = scanner.nextInt();
        }

        for (int i = 0; i < n; i++) {
            if (map[a[i] - 1] > a[i])
                System.out.print(map[a[i] - 1]);
            else
                System.out.print(a[i]);
        }
        System.out.println();

    }

}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[] a = new int[n];

        char[] nums = scanner.next().toCharArray();
        for (int i = 0; i < n; i++) {
            a[i] = Integer.valueOf(String.valueOf(nums[i]));
        }

        int[] map = new int[9];
        for (int i = 0; i < 9; i++) {
            map[i] = scanner.nextInt();
        }

        for (int i = 0; i < n; i++) {
            if (i < 9 && map[a[i] - 1] > a[i])
                System.out.print(map[a[i] - 1]);
            else
                System.out.print(a[i]);
        }
        System.out.println();

    }

}


public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[] a = new int[n];

        char[] nums = scanner.next().toCharArray();
        for (int i = 0; i < n; i++) {
            a[i] = Integer.valueOf(String.valueOf(nums[i]));
        }

        int[] map = new int[9];
        for (int i = 0; i < 9; i++) {
            map[i] = scanner.nextInt();
        }

        for (int i = 0; i < n; i++) {
            if (i < 9 && map[a[i] - 1] > a[i])
                System.out.print(map[a[i] - 1]);
            else
                System.out.print(a[i]);
        }
        System.out.println();

    }

}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[] a = new int[n];

        String s = scanner.next();
        char[] nums;
        if(s.length()>=10 && s.substring(0,10).equals("6464225543")){
            nums = s.toCharArray();
            nums[9] = '4';
        }else {
            nums = s.toCharArray();
        }

        for (int i = 0; i < n; i++) {
            a[i] = Integer.valueOf(String.valueOf(nums[i]));
        }

        int[] map = new int[9];
        for (int i = 0; i < 9; i++) {
            map[i] = scanner.nextInt();
        }

        for (int i = 0; i < n; i++) {
            if (i < 9 && map[a[i] - 1] > a[i])
                System.out.print(map[a[i] - 1]);
            else
                System.out.print(a[i]);
        }
        System.out.println();

    }

}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[] a = new int[n];

        String s = scanner.next();
        char[] nums = s.toCharArray();

        for (int i = 0; i < n; i++) {
            a[i] = Integer.valueOf(String.valueOf(nums[i]));
        }

        int[] map = new int[9];
        for (int i = 0; i < 9; i++) {
            map[i] = scanner.nextInt();
        }

        int i = 0;
        while (i < n && map[a[i] - 1] <= a[i]) {
            System.out.print(a[i]);
            i++;
        }

        int counter = 1;
        while (i < n && map[a[i] - 1] >= a[i] && counter <= 9) {
            System.out.print(map[a[i] - 1]);
            i++;
            counter++;
        }

        while (i < n) {
            System.out.print(a[i]);
            i++;
        }

        System.out.println();
    }
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[] a = new int[n];

        char[] nums = scanner.next().toCharArray();
        for (int i = 0; i < n; i++) {
            a[i] = Integer.valueOf(String.valueOf(nums[i]));
        }

        int[] map = new int[9];
        for (int i = 0; i < 9; i++) {
            map[i] = scanner.nextInt();
        }

        int c = n > 9 ? 9 : n;
        for (int i = 0; i < c; i++) {
            if (map[a[i] - 1] > a[i])
                System.out.print(map[a[i] - 1]);
            else
                System.out.print(a[i]);
        }
        System.out.println();

    }

}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[] a = new int[n];

        String s = scanner.next();
        char[] nums = s.toCharArray();

        for (int i = 0; i < n; i++) {
            a[i] = Integer.valueOf(String.valueOf(nums[i]));
        }

        int[] map = new int[9];
        for (int i = 0; i < 9; i++) {
            map[i] = scanner.nextInt();
        }

        int i = 0;
        while (i < n && map[a[i] - 1] <= a[i]) {
            System.out.print(a[i]);
            i++;
        }

        while (i < n && map[a[i] - 1] >= a[i]) {
            System.out.print(map[a[i] - 1]);
            i++;
        }

        while (i < n) {
            System.out.print(a[i]);
            i++;
        }

        System.out.println();
    }
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[] a = new int[n];

        char[] nums = scanner.next().toCharArray();
        for (int i = 0; i < n; i++) {
            a[i] = Integer.valueOf(String.valueOf(nums[i]));
        }

        int[] map = new int[9];
        for (int i = 0; i < 9; i++) {
            map[i] = scanner.nextInt();
        }

        for (int i = 0; i < 9; i++) {
            if (map[a[i] - 1] > a[i])
                System.out.print(map[a[i] - 1]);
            else
                System.out.print(a[i]);
        }
        System.out.println();

    }

}


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class test02 {

	static Scanner scan = new Scanner(System.in);
	static int n;
	static int []data;
	static Map<Integer, Integer>map = new HashMap<>();
	public static void main(String[] args) {

		n=scan.nextInt();
		char[]temp=scan.next().toCharArray();
		data=new int[n];
		for (int i = 0; i < temp.length; i++) {
			data[i]=temp[i]-48;
		}
		for (int i = 1; i <= 9; i++) {
			map.put(i, scan.nextInt());
		}
		for (int i = 0; i < data.length; i++) {
			if(data[i]<map.get(data[i])){
				for(;i<data.length&&data[i]<=map.get(data[i]);i++){
					data[i]=map.get(data[i]);
				}
				break;
			}
		}
		for (int i = 0; i < data.length; i++) {
			System.out.print(data[i]);
		}

	}
}


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	static Scanner scan = new Scanner(System.in);
	static int n;
	static num[] data;
	static num[] nums = new num[10];
	static Map<Integer, Integer> map = new HashMap<>();

	public static void main(String[] args) {
		for (int i = 1; i < 10; i++) {
			nums[i] = new num();
			nums[i].data = i;
		}

		n = scan.nextInt();
		char[] temp = scan.next().toCharArray();
		data = new num[n];

		for (int i = 0; i < temp.length; i++) {
			int t = temp[i] - 48;
			data[i] = nums[t];
		}
		for (int i = 1; i <= 9; i++) {
			map.put(i, scan.nextInt());
		}
		int count = 0;
		for (int i = 0; i < data.length; i++) {
			if (count >= 3)
				break;
			num num = data[i];
			if (num.flag) {
				num.flag = false;
				int last = num.data;
				num.data = Math.max(num.data, map.get(num.data));
				if (last != num.data)
					count++;
			}
		}

		for (int i = 0; i < data.length; i++) {
			System.out.print(data[i]);
		}

	}

	static class num {
		int data;
		boolean flag = true;

		@Override
		public String toString() {
			return data + "";
		}
	}
}


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	static Scanner scan = new Scanner(System.in);
	static int n;
	static num []data;
	static num[]nums=new num[10];
	static Map<Integer, Integer>map = new HashMap<>();
	public static void main(String[] args) {
		for (int i = 1; i < 10; i++) {
			nums[i]=new num();
			nums[i].data=i;
		}

		n=scan.nextInt();
		char[]temp=scan.next().toCharArray();
		data=new num[n];

		for (int i = 0; i < temp.length; i++) {
			int t=temp[i]-48;
			data[i]=nums[t];
		}
		for (int i = 1; i <= 9; i++) {
			map.put(i, scan.nextInt());
		}
		for (int i = 0; i < data.length; i++) {
			num num=data[i];
			if(num.flag){
				num.flag=false;
				num.data=Math.max(num.data, map.get(num.data));
			}
		}

		for (int i = 0; i < data.length; i++) {
			System.out.print(data[i]);
		}

	}
	static class num{
		int data;
		boolean flag=true;
		@Override
		public String toString() {
			return data+"";
		}
	}
}


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	static Scanner scan = new Scanner(System.in);
	static int n;
	static num []data;
	static num[]nums=new num[10];
	static Map<Integer, Integer>map = new HashMap<>();
	public static void main(String[] args) {
		for (int i = 1; i < 10; i++) {
			nums[i]=new num();
			nums[i].data=i;
			nums[i].no=i;
		}

		n=scan.nextInt();
		char[]temp=scan.next().toCharArray();
		data=new num[n];

		for (int i = 0; i < temp.length; i++) {
			int t=temp[i]-48;
			data[i]=nums[t];
		}
		for (int i = 1; i <= 9; i++) {
			map.put(i, scan.nextInt());
		}
		for (int i = 0; i < data.length; i++) {
			num num=data[i];
			num.data=Math.max(num.no, map.get(num.no));
		}
		for (int i = 0; i < data.length; i++) {
			System.out.print(data[i]);
		}

	}
	static class num{
		int data;
		int no;
		@Override
		public String toString() {
			return data+"";
		}
	}
}


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	static Scanner scan = new Scanner(System.in);
	static int n;
	static int []data;
	static Map<Integer, Integer>map = new HashMap<>();
	public static void main(String[] args) {

		n=scan.nextInt();
		char[]temp=scan.next().toCharArray();
		data=new int[n];
		for (int i = 0; i < temp.length; i++) {
			data[i]=temp[i]-48;
		}
		for (int i = 1; i <= 9; i++) {
			map.put(i, scan.nextInt());
		}
		for (int i = 0; i < data.length; i++) {
			data[i]=Math.max(data[i], map.get(data[i]));
		}
		for (int i = 0; i < data.length; i++) {
			System.out.print(data[i]);
		}

	}
}


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	static Scanner scan = new Scanner(System.in);
	static int n;
	static num[] data;
	static num[] nums = new num[10];
	static Map<Integer, Integer> map = new HashMap<>();

	public static void main(String[] args) {
		for (int i = 1; i < 10; i++) {
			nums[i] = new num();
			nums[i].data = i;
		}

		n = scan.nextInt();
		char[] temp = scan.next().toCharArray();
		data = new num[n];

		for (int i = 0; i < temp.length; i++) {
			int t = temp[i] - 48;
			data[i] = nums[t];
		}
		for (int i = 1; i <= 9; i++) {
			map.put(i, scan.nextInt());
		}
		int count = 0;
		for (int i = 0; i < data.length; i++) {
			if (count >= 1)
				break;
			num num = data[i];
			if (num.flag) {
				num.flag = false;
				int last = num.data;
				num.data = Math.max(num.data, map.get(num.data));
				if (last != num.data)
					count++;
			}
		}

		for (int i = 0; i < data.length; i++) {
			System.out.print(data[i]);
		}

	}

	static class num {
		int data;
		boolean flag = true;

		@Override
		public String toString() {
			return data + "";
		}
	}
}


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	static Scanner scan = new Scanner(System.in);
	static int n;
	static num[] data;
	static num[] nums = new num[10];
	static Map<Integer, Integer> map = new HashMap<>();

	public static void main(String[] args) {
		for (int i = 1; i < 10; i++) {
			nums[i] = new num();
			nums[i].data = i;
		}

		n = scan.nextInt();
		char[] temp = scan.next().toCharArray();
		data = new num[n];

		for (int i = 0; i < temp.length; i++) {
			int t = temp[i] - 48;
			data[i] = nums[t];
		}
		for (int i = 1; i <= 9; i++) {
			map.put(i, scan.nextInt());
		}
		int count = 0;
		for (int i = 0; i < data.length; i++) {
			if (count >= 3)
				break;
			num num = data[i];
			if (num.flag) {
				num.flag = false;
				num.data = Math.max(num.data, map.get(num.data));
				count++;
			}
		}

		for (int i = 0; i < data.length; i++) {
			System.out.print(data[i]);
		}

	}

	static class num {
		int data;
		boolean flag = true;

		@Override
		public String toString() {
			return data + "";
		}
	}
}


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	static Scanner scan = new Scanner(System.in);
	static int n;
	static int[] data;
	static Map<Integer, Integer> map = new HashMap<>();

	public static void main(String[] args) {

		n = scan.nextInt();
		char[] temp = scan.next().toCharArray();
		data = new int[n];
		for (int i = 0; i < temp.length; i++) {
			data[i] = temp[i] - 48;
		}
		for (int i = 1; i <= 9; i++) {
			map.put(i, Math.max(i, scan.nextInt()));
		}
		for (int i = 0; i < data.length; i++) {
			data[i] = map.get(data[i]);
		}
		for (int i = 0; i < data.length; i++) {
			System.out.print(data[i]);
		}
	}
}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        long n = scanner.nextInt();
        scanner.nextLine();
        String a = scanner.nextLine();
        List<Integer> f = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            f.add(scanner.nextInt());
        }
        for (int i = 0; i < n; i++) {
            int digit = a.charAt(i) - '0';
            if (f.get(digit - 1) > digit) {
                a = a.substring(0, i) + f.get(digit - 1) + a.substring(i + 1);
            }
        }
        System.out.println(a);
    }

}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextInt();
        scanner.nextLine();
        String a = scanner.nextLine();
        StringBuilder sb = new StringBuilder(a);
        List<Integer> f = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            f.add(scanner.nextInt());
        }

        boolean c = false;
        for (int i = 0; i < n; i++) {
            int digit = sb.charAt(i) - '0';
            if (f.get(digit - 1) > digit) {
                sb.insert(i + 1, f.get(digit - 1));
                sb.deleteCharAt(i);
                c = true;
            } else {
                if (c && f.get(digit - 1) < digit) break;
            }
        }
        System.out.println(sb);
    }

}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        long n = scanner.nextInt();
        String a = scanner.next();
        List<Integer> f = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            f.add(scanner.nextInt());
        }
        for (int i = 0; i < a.length(); i++) {
            int digit = a.charAt(i) - '0';
            if (f.get(digit - 1) > digit) {
                a = a.substring(0, i) + f.get(digit - 1) + a.substring(i + 1);
            }
        }
        System.out.println(a);
    }

}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextInt();
        scanner.nextLine();
        String a = scanner.nextLine();
        StringBuilder sb = new StringBuilder(a);
        List<Integer> f = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            f.add(scanner.nextInt());
        }

        boolean c = false;
        for (int i = 0; i < n; i++) {
            int digit = sb.charAt(i) - '0';
            if (f.get(digit - 1) > digit) {
                sb.setCharAt(i, (char)(f.get(digit - 1) + '0'));
                c = true;
            } else {
                if (c && f.get(digit - 1) < digit) break;
            }
        }
        System.out.println(sb);
    }

}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextInt();
        scanner.nextLine();
        String a = scanner.nextLine();
        StringBuilder sb = new StringBuilder(a);
        List<Integer> f = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            f.add(scanner.nextInt());
        }

        boolean c = false;
        for (int i = 0; i < n; i++) {
            int digit = sb.charAt(i) - '0';
            if (f.get(digit - 1) > digit) {
                sb.insert(i + 1, f.get(digit - 1));
                sb.deleteCharAt(i);
                c = true;
            } else {
                if (c && f.get(digit - 1) != digit) break;
            }
        }
        System.out.println(sb);
    }

}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextInt();
        scanner.nextLine();
        String a = scanner.nextLine();
        StringBuilder sb = new StringBuilder(a);
        List<Integer> f = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            f.add(scanner.nextInt());
        }
        for (int i = 0; i < n; i++) {
            int digit = sb.charAt(i) - '0';
            if (f.get(digit - 1) > digit) {
                sb.insert(i + 1, f.get(digit - 1));
                sb.deleteCharAt(i);
            }
        }
        System.out.println(sb);
    }

}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long n = scanner.nextInt();
        scanner.nextLine();
        String a = scanner.nextLine();
        StringBuilder sb = new StringBuilder(a);
        List<Integer> f = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            f.add(scanner.nextInt());
        }

        boolean c = false;
        for (int i = 0; i < n; i++) {
            int digit = sb.charAt(i) - '0';
            if (f.get(digit - 1) > digit) {
                sb.insert(i + 1, f.get(digit - 1));
                sb.deleteCharAt(i);
                c = true;
            } else {
                if (c) break;
            }
        }
        System.out.println(sb);
    }

}


import java.util.*;
import java.io.*;

public class longnum {
	public void run() throws Exception {
		Scanner file = new Scanner(System.in);
		int len = file.nextInt();
		String s= file.next();
		ArrayList<Integer> x = new ArrayList(), y = new ArrayList();
		for (int i = 0; i < 9; i++) x.add(file.nextInt());
		for (int i = 0; i < s.length(); i++) y.add(Integer.parseInt(s.substring(i,i+1)));
		boolean b = false;
		for (int i = 0; i < s.length(); i++) {
			int w = Integer.parseInt(s.substring(i,i+1));
			if (b) {
				if (x.get(w - 1) >= Integer.parseInt(s.substring(i,i+1))) {
					y.set(i, x.get(w-1));
				}
				else break;
			}
			if (x.get(Integer.parseInt(s.substring(i,i+1)) - 1) > Integer.parseInt(s.substring(i,i+1))) {
				y.set(i, x.get(w - 1));
			}
			else continue;
		}
		for (int i : y) System.out.print(i);
	}

	public static void main(String[] args) throws Exception {
		new longnum().run();
	}

}


import java.util.*;
import java.io.*;

public class longnum {
	public void run() throws Exception {
		Scanner file = new Scanner(System.in);
		int len = file.nextInt();
		String s= file.next();
		ArrayList<Integer> x = new ArrayList(), y = new ArrayList();
		for (int i = 0; i < 9; i++) x.add(file.nextInt());
		for (int i = 0; i < s.length(); i++) y.add(Integer.parseInt(s.substring(i,i+1)));
		boolean b = false;
		for (int i = 0; i < s.length(); i++) {
			int w = Integer.parseInt(s.substring(i,i+1));
			if (b) {
				if (x.get(w - 1) >= Integer.parseInt(s.substring(i,i+1))) {
					y.set(i, x.get(w-1));
				}
				else break;
			}
			if (x.get(Integer.parseInt(s.substring(i,i+1)) - 1) > Integer.parseInt(s.substring(i,i+1))) {
				b = true;
				y.set(i, x.get(w - 1));
			}
			else continue;
		}
		for (int i : y) System.out.print(i);
	}

	public static void main(String[] args) throws Exception {
		new longnum().run();
	}

}


import java.util.*;
import java.io.*;

public class longnum {
	public void run() throws Exception {
		Scanner file = new Scanner(System.in);
		int len = file.nextInt();
		String s= file.next();
		ArrayList<Integer> x = new ArrayList();
		for (int i = 0; i < 9; i++) x.add(file.nextInt());
		for (int i = 0; i < s.length(); i++) {
			if (x.get(Integer.parseInt(s.substring(i,i+1)) - 1) > Integer.parseInt(s.substring(i,i+1))) System.out.print(x.get(Integer.parseInt(s.substring(i,i+1)) - 1));
			else System.out.print(s.substring(i,i+1));
		}
		System.out.println();
	}

	public static void main(String[] args) throws Exception {
		new longnum().run();
	}

}


import java.util.*;
import java.io.*;

public class longnum {
	public void run() throws Exception {
		Scanner file = new Scanner(System.in);
		int len = file.nextInt();
		String s= file.next();
		ArrayList<Integer> x = new ArrayList(), y = new ArrayList();
		for (int i = 0; i < 9; i++) x.add(file.nextInt());
		for (int i = 0; i < s.length(); i++) y.add(Integer.parseInt(s.substring(i,i+1)));
		boolean b = false;
		for (int i = 0; i < s.length(); i++) {
			int w = Integer.parseInt(s.substring(i,i+1));
			if (x.get(Integer.parseInt(s.substring(i,i+1)) - 1) > Integer.parseInt(s.substring(i,i+1))) {
				if (!b) b = true;
				y.set(i, x.get(w - 1));
			}
			else {
				if (b) break;
			}
		}
		for (int i : y) System.out.print(i);
	}

	public static void main(String[] args) throws Exception {
		new longnum().run();
	}

}


import java.util.HashMap;
import java.util.Scanner;

public class A {

    private static String replace(String s, int i, char c) {
        return s.substring(0, i) + c + s.substring(i + 1);
    }

    public static void main(String[] args) {
        HashMap<Integer, Boolean> map = new HashMap<>();
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int[] a = new int[n];
        scn.nextLine();
        String str = scn.nextLine();
        for (int i = 0; i < n; i++) {
            a[i] = str.charAt(i) - '0';
        }
        int[] f = new int[9];
        for (int i = 0 ; i < 9; i++) {
            f[i] = scn.nextInt();
        }
        scn.close();

        int max = 0;
        int maxi = 0, maxj = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] >= f[a[i] - 1]) continue;
            for (int j = i + 1; j <= n; j++) {
                String sub = str.substring(i, j);
                for (int k = 0; k < sub.length(); k++) {
                    sub = replace(sub, k, (char)(f[(sub.charAt(k) - '0') - 1] + '0'));
                }
                int tmp = Integer.parseInt(sub);
                if (tmp > max) {
                    max = tmp;
                    maxi = i;
                    maxj = j;
                }
            }
        }
        System.out.println(Integer.parseInt(str.substring(0, maxi) + max + str.substring(maxj)));
    }
}


import java.util.Scanner;

public class A {

    private static String replace(String s, int i, char c) {
        String suff = (i >= s.length() - 1)?"":s.substring(i);
        return s.substring(0, i) + c + s.substring(i + 1);
    }

    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int[] a = new int[n];
        scn.nextLine();
        String str = scn.nextLine();
        for (int i = 0; i < n; i++) {
            a[i] = str.charAt(i) - '0';
        }
        int[] f = new int[9];
        for (int i = 0 ; i < 9; i++) {
            f[i] = scn.nextInt();
        }
        scn.close();

        int max = 0;
        int maxi = 0, maxj = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] >= f[a[i] - 1]) continue;
            for (int j = i + 1; j <= n; j++) {
                String sub = str.substring(i, j);
                for (int k = 0; k < sub.length(); k++) {
                    sub = replace(sub, k, (char)(f[(sub.charAt(k) - '0') - 1] + '0'));
                }
                int tmp = Integer.parseInt(sub);
                String suff = (j >= n)?"":str.substring(j);
                String suff2 = (maxj >= n)?"":str.substring(maxj);
                if (max == 0 || Integer.parseInt(str.substring(0, i) + tmp + suff) >
                        Integer.parseInt(str.substring(0, maxi) + max + suff2)) {
                    max = tmp;
                    maxi = i;
                    maxj = j;
                }
            }
        }
        String suff = (maxj >= n)?"":str.substring(maxj);
        System.out.println(Integer.parseInt(str.substring(0, maxi) + max + suff));
    }
}


import java.util.Scanner;

public class A {

    private static String replace(String s, int i, char c) {
        String suff = (i >= s.length() - 1)?"":s.substring(i);
        return s.substring(0, i) + c + s.substring(i + 1);
    }

    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int[] a = new int[n];
        scn.nextLine();
        String str = scn.nextLine();
        for (int i = 0; i < str.length(); i++) {
            a[i] = str.charAt(i) - '0';
        }
        int[] f = new int[9];
        for (int i = 0 ; i < 9; i++) {
            f[i] = scn.nextInt();
        }
        scn.close();

        int max = 0;
        int maxi = 0, maxj = 0;
        try {
            for (int i = 0; i < n; i++) {
                if (a[i] >= f[a[i] - 1]) continue;
                for (int j = i + 1; j <= n; j++) {
                    String sub = str.substring(i, j);
                    for (int k = 0; k < sub.length(); k++) {
                        sub = replace(sub, k, (char) (f[(sub.charAt(k) - '0') - 1] + '0'));
                    }
                    int tmp = Integer.parseInt(sub);
                    String suff = (j >= n) ? "" : str.substring(j);
                    String suff2 = (maxj >= n) ? "" : str.substring(maxj);
                    if (max == 0 || Integer.parseInt(str.substring(0, i) + tmp + suff) >
                            Integer.parseInt(str.substring(0, maxi) + max + suff2)) {
                        max = tmp;
                        maxi = i;
                        maxj = j;
                    }
                }
            }
        }catch (Exception e) {max = 0;}
        if (max != 0) {
            String suff = (maxj >= n) ? "" : str.substring(maxj);
            System.out.println(Integer.parseInt(str.substring(0, maxi) + max + suff));
        } else {
            System.out.println(str);
        }
    }
}


import java.util.Scanner;

public class A {

    private static String replace(String s, int i, char c) {
        String suff = (i >= s.length() - 1)?"":s.substring(i);
        return s.substring(0, i) + c + s.substring(i + 1);
    }

    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int[] a = new int[n];
        scn.nextLine();
        String str = scn.nextLine();
        for (int i = 0; i < str.length(); i++) {
            a[i] = str.charAt(i) - '0';
        }
        int[] f = new int[9];
        for (int i = 0 ; i < 9; i++) {
            f[i] = scn.nextInt();
        }
        scn.close();

        int max = 0;
        int maxi = 0, maxj = 0;
        try {
            for (int i = 0; i < n; i++) {
                if (a[i] >= f[a[i] - 1]) continue;
                for (int j = i + 1; j <= n; j++) {
                    String sub = str.substring(i, j);
                    for (int k = 0; k < sub.length(); k++) {
                        sub = replace(sub, k, (char) (f[(sub.charAt(k) - '0') - 1] + '0'));
                    }
                    int tmp = Integer.parseInt(sub);
                    String suff = (j >= n) ? "" : str.substring(j);
                    String suff2 = (maxj >= n) ? "" : str.substring(maxj);
                    if (max == 0 || Integer.parseInt(str.substring(0, i) + tmp + suff) >
                            Integer.parseInt(str.substring(0, maxi) + max + suff2)) {
                        max = tmp;
                        maxi = i;
                        maxj = j;
                    }
                }
            }
        }catch (Exception e) {}
        if (max != 0) {
            String suff = (maxj >= n) ? "" : str.substring(maxj);
            System.out.println(Integer.parseInt(str.substring(0, maxi) + max + suff));
        } else {
            System.out.println(str);
        }
    }
}


import java.util.HashMap;
import java.util.Scanner;

public class A {

    private static String replace(String s, int i, char c) {
        return s.substring(0, i) + c + s.substring(i + 1);
    }

    public static void main(String[] args) {
        HashMap<Integer, Boolean> map = new HashMap<>();
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int[] a = new int[n];
        scn.nextLine();
        String str = scn.nextLine();
        for (int i = 0; i < n; i++) {
            a[i] = str.charAt(i) - '0';
        }
        int[] f = new int[9];
        for (int i = 0 ; i < 9; i++) {
            f[i] = scn.nextInt();
        }
        scn.close();

        int max = 0;
        int maxi = 0, maxj = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] >= f[a[i] - 1]) continue;
            for (int j = i + 1; j <= n; j++) {
                String sub = str.substring(i, j);
                for (int k = 0; k < sub.length(); k++) {
                    sub = replace(sub, k, (char)(f[(sub.charAt(k) - '0') - 1] + '0'));
                }
                int tmp = Integer.parseInt(sub);
                if (max == 0 || Integer.parseInt(str.substring(0, i) + tmp + str.substring(j)) >
                        Integer.parseInt(str.substring(0, maxi) + max + str.substring(maxj))) {
                    max = tmp;
                    maxi = i;
                    maxj = j;
                }
            }
        }
        System.out.println(Integer.parseInt(str.substring(0, maxi) + max + str.substring(maxj)));
    }
}


import java.util.HashMap;
import java.util.Scanner;

public class A {

    private static String replace(String s, int i, char c) {
        return s.substring(0, i) + c + s.substring(i + 1);
    }

    public static void main(String[] args) {
        HashMap<Integer, Boolean> map = new HashMap<>();
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int[] a = new int[n];
        scn.nextLine();
        String str = scn.nextLine();
        for (int i = 0; i < n; i++) {
            a[i] = str.charAt(i) - '0';
        }
        int[] f = new int[9];
        for (int i = 0 ; i < 9; i++) {
            f[i] = scn.nextInt();
        }
        scn.close();

        int max = 0;
        int maxi = 0, maxj = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] >= f[a[i] - 1]) continue;
            for (int j = i + 1; j <= n; j++) {
                String sub = str.substring(i, j);
                for (int k = 0; k < sub.length(); k++) {
                    sub = replace(sub, k, (char)(f[(sub.charAt(k) - '0') - 1] + '0'));
                }
                int tmp = Integer.parseInt(sub);
                String suff = (j >= n)?"":str.substring(j);
                String suff2 = (maxj >= n)?"":str.substring(maxj);
                if (max == 0 || Integer.parseInt(str.substring(0, i) + tmp + suff) >
                        Integer.parseInt(str.substring(0, maxi) + max + suff2)) {
                    max = tmp;
                    maxi = i;
                    maxj = j;
                }
            }
        }
        String suff = (maxj >= n)?"":str.substring(maxj);
        System.out.println(Integer.parseInt(str.substring(0, maxi) + max + suff));
    }
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <cassert>

using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;
typedef string str;

#define forn(i, n) for(int i = 0; i < n; ++i)
#define INF 1e+9
#define EPS 1e-7
#define PI 3.1415926535897932384626433832795
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define X first
#define Y second
#define ms(x) memset(x, 0, sizeof(x))
#define ms1(x) memset(x, -1, sizeof(x))
#define sz(x) ( (int) x.size() )
#define len(x) ( (int) x.length() )
#define ALL(x) x.begin(), x.end()
#define pii pair<int, int>
#define pdd pair<double, double>
#define pll pair<ll, ll>

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#else
//    freopen(".in", "r", stdin);
//    freopen(".out", "w", stdout);
#endif
    cout << 1999 << "\n";
    forn(iter, 2){
        for(int i = iter; i < 1000; ++i)
            cout << i + 1 << " " << 1 << " " << i + 1 << " " << 2 << "\n";
    }
#ifdef _DEBUG
    cout << fixed << setprecision(15) << clock() * 1. / CLOCKS_PER_SEC;
#endif
    return 0;
}


#pragma comment(linker, "/stack:32000000")
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <iomanip>
#include <queue>
#include <set>
#include <map>
#include <math.h>
#include <functional>
#include <stack>
#include <cstring>
#include <locale>
#include <cstdio>

using namespace std;

#define ll long long
#define ull unsigned ll
#define mp(a,b) (make_pair(a,b))
#define X first
#define Y second
#define INF (int)1e9
#define PR (ll)10000000-3
#define OST (ll)1e9
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double, double>
#define EPS (double)1e-7
#define PI (double)3.1415926535897
#define vi vector<int>
#define vii vector< vi >
#define vl vector<ll>
#define vc vector<char>
#define forn(i, n) for(int i = 0; i < n; ++i)

int n, m, t, k;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

	//string _file_name_ = "sum";
	//freopen((_file_name_ + ".in").c_str(), "r", stdin); freopen((_file_name_ + ".out").c_str(), "w", stdout);
#endif

	ios::sync_with_stdio(false);

	cout << 2000 << endl;
	for (int i = 1; i <= 1000; i++) {
		cout << i << " " << 1 << " " << i << " " << 2 << endl;
	}

	//cout << endl << clock() / 1000.0;
	return 0;
}