#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int n;
  cin >> n;
  string a;
  cin >> a;
  char v[9];
  int flag = 0;
  for (int i = 0; i < 9; i++) {
    cin >> v[i];
  }
  for (int i = 0; i < n; i++) {
    int x = a[i] - '1';
    if (flag == 1 && v[x] < a[i]) {
      break;
    } else if (v[x] > a[i]) {
      a[i] = v[x];
      flag = 1;
    }
  }
  cout << a << endl;
  return 0;
}
