#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m, k, i, r, z, p = 0, ng = 0, j;
  string s, s1;
  vector<long long> a[30];
  cin >> m;
  cin >> s;
  for (i = 0; i < m; i++) {
    a[s[i] - 'a'].push_back(i);
  }
  cin >> n;
  while (n--) {
    cin >> s1;
    long long ar[130] = {0}, c = 0, r = 0;
    for (i = 0; i < s1.size(); i++) ar[s1[i] - 'a']++;
    for (i = 0; i < 26; i++) {
      if (ar[i]) r = max(a[i][ar[i] - 1] + 1, r);
    }
    cout << r << endl;
  }
  return 0;
}
