#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int len, n, ans;
  string str, s;
  cin >> len >> str >> n;
  vector<int> v[26];
  for (int i = 0; i < len; i++) {
    v[str[i] - 'a'].push_back(i + 1);
  }
  for (int i = 0; i < n; i++) {
    cin >> s;
    vector<int> freq(26, 0);
    ans = 0;
    for (int j = 0; j < s.size(); j++) {
      ans = max(ans, v[s[j] - 'a'][freq[s[j] - 'a']]);
      freq[s[j] - 'a']++;
    }
    cout << ans << endl;
  }
  return 0;
}
