#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
  int n, m;
  string s;
  cin >> n >> s >> m;
  int count[100];
  for (int i = 0; i < 99; i++) {
    count[i] = 0;
  }
  int ind[100][n + 1];
  for (int i = 0; i < n; i++) {
    int temp = int(s.at(i));
    temp -= 97;
    count[temp]++;
    ind[temp][count[temp]] = i + 1;
  }
  while (m--) {
    string a;
    cin >> a;
    int max;
    int flag = 0;
    int c[100];
    for (int i = 0; i < 99; i++) {
      c[i] = 0;
    }
    for (int i = 0; i < a.size(); i++) {
      int temp = int(a.at(i)) - 97;
      c[temp]++;
      if (flag == 0) {
        max = ind[temp][c[temp]];
        flag = 1;
      } else {
        if (max < ind[temp][c[temp]]) max = ind[temp][c[temp]];
      }
    }
    cout << max << '\n';
  }
  return 0;
}
