#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  vector<int> strl[26];
  int p[26] = {0};
  string inp;
  cin >> n;
  cin >> inp;
  for (int i = 0; i < inp.length(); i++) {
    strl[inp[i] - 97].push_back(i + 1);
  }
  cin >> n;
  int idx;
  int res = 0;
  while (n--) {
    for (int i = 0; i < 26; i++) {
      p[i] = 0;
    }
    string str;
    res = 0;
    cin >> str;
    for (int i = 0; i < str.length(); i++) {
      idx = str[i] - 97;
      res = max(strl[idx][p[idx]], res);
      p[idx]++;
    }
    printf("%d\n", res);
  }
  return 0;
}
