#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 2e9;
map<char, vector<int>> m;
string s;
int main() {
  int n;
  char x;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> x;
    m[x].push_back(i + 1);
  }
  int t;
  cin >> t;
  while (t--) {
    cin >> s;
    int mx = 0;
    map<char, int> freq;
    for (char c : s) {
      freq[c]++;
      mx = max(mx, m[c][freq[c] - 1]);
    }
    cout << mx << '\n';
  }
  return 0;
}
