#include <bits/stdc++.h>
using namespace std;
int n, m, cnt[29];
string s;
vector<vector<int> > v(29);
int main() {
  cin >> n >> s >> m;
  for (int i = 0; i < s.size(); i++) v[s[i] - 'a'].push_back(i + 1);
  while (m--) {
    string t;
    cin >> t;
    int ans = 0;
    memset(cnt, 0, sizeof cnt);
    for (int i = 0; i < t.size(); i++) cnt[t[i] - 'a']++;
    for (int i = 0; i < 26; i++) {
      if (!cnt[i]) continue;
      int lst = (int)v[i].size() - 1;
      int can = min(lst, cnt[i] - 1);
      ans = max(ans, v[i][can]);
    }
    cout << ans << "\n";
  }
  return 0;
}
