#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> pos[26];
  for (int i = 0; i < n; i++) {
    pos[s[i] - 'a'].push_back(i);
  }
  int m;
  cin >> m;
  while (m--) {
    string fd;
    cin >> fd;
    vector<int> cnt(26);
    for (int i = 0; i < fd.size(); i++) {
      cnt[fd[i] - 'a']++;
    }
    int ans = 0;
    for (int i = 0; i < 26; i++)
      if (cnt[i]) ans = max(ans, pos[i][cnt[i] - 1]);
    cout << ans + 1 << "\n";
  }
  return 0;
}
