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