#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> v[30];
  for (int i = 0; i < n; i++) {
    v[s[i] - 96].push_back(i);
  }
  int m;
  cin >> m;
  while (m--) {
    string a;
    cin >> a;
    int h[30] = {0};
    for (int i = 0; i < a.size(); i++) {
      h[a[i] - 96]++;
    }
    int ma = -1;
    for (int i = 1; i <= 26; i++) {
      if (h[i] != 0) {
        ma = max(ma, v[i][h[i] - 1]);
      }
    }
    cout << ma + 1 << endl;
  }
  return 0;
}
