#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
vector<int> v[30];
int b[30];
int main() {
  string s;
  int n;
  cin >> n;
  cin >> s;
  for (int i = 0; i < s.size(); i++) v[s[i] - 'a' + 1].push_back(i);
  int m;
  cin >> m;
  while (m--) {
    string ss;
    cin >> ss;
    int ans = 0;
    memset(b, 0, sizeof b);
    for (int i = 0; i < ss.size(); i++) b[ss[i] - 'a' + 1]++;
    for (int i = 1; i <= 26; i++) {
      if (b[i] != 0) ans = max(ans, v[i][b[i] - 1]);
    }
    cout << ans + 1 << endl;
  }
  return 0;
}
