#include <bits/stdc++.h>
using namespace std;
vector<int> vitri[30];
int dem[30];
int main() {
  int n, m;
  string s;
  cin >> n;
  cin >> s;
  cin >> m;
  for (int i = 0; i < n; i++) vitri[s[i] - 97].push_back(i);
  for (int i = 1; i <= m; i++) {
    cin >> s;
    int kq = 0;
    memset(dem, 0, sizeof(dem));
    int len = s.size();
    for (int i = 0; i < len; i++) dem[s[i] - 97] += 1;
    for (int i = 0; i <= 25; i++) {
      int tmp = vitri[i][dem[i] - 1];
      if (dem[i] != 0) kq = max(kq, vitri[i][dem[i] - 1]);
    }
    cout << kq + 1 << endl;
  }
  return 0;
}
