#include <bits/stdc++.h>
using namespace std;
int n, m;
string s;
vector<int> res[30];
int tong[30];
int main() {
  cin >> n >> s >> m;
  for (int i = 0; i < n; i++) res[s[i] - 'a'].push_back(i);
  while (m--) {
    cin >> s;
    memset(tong, 0, sizeof(tong));
    for (int i = 0; i < s.length(); i++) tong[s[i] - 'a']++;
    int cnt = -1;
    for (int i = 0; i < 26; i++)
      if (tong[i]) cnt = max(cnt, res[i][tong[i] - 1]);
    cout << cnt + 1 << endl;
  }
  return 0;
}
