#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  char transformations[10];
  vector<char> number;
  for (int i = 0; i < n; i++) {
    char symbol;
    cin >> symbol;
    symbol = symbol - '0';
    number.push_back(symbol);
  }
  for (int i = 1; i <= 9; i++) {
    cin >> transformations[i];
    transformations[i] -= '0';
  }
  bool stop = false;
  for (int i = 0; i < number.size(); i++) {
    if (stop && number[i] == transformations[number[i]] ||
        number[i] < transformations[number[i]]) {
      number[i] = transformations[number[i]];
      stop = true;
    } else {
      if (stop) break;
    }
  }
  for (auto &el : number) {
    cout << (int)el;
  }
  return 0;
}
