#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  string s;
  cin >> n;
  cin >> s;
  vector<long long> v[26];
  for (int i = 0; i < n; i += 1) {
    v[s[i] - 'a'].push_back(i);
  }
  long long q;
  cin >> q;
  while (q--) {
    long long ans = 0;
    string s1;
    cin >> s1;
    vector<long long> count(26, 0);
    for (int i = 0; i < s1.size(); i += 1) {
      count[s1[i] - 'a']++;
    }
    for (int i = 0; i < 26; i += 1) {
      if (count[i] != 0) {
        ans = max(ans, v[i][count[i] - 1]);
      }
    }
    ans++;
    cout << ans << endl;
  }
  return 0;
}
