#include <bits/stdc++.h>
using namespace std;
int ai[1000000][30];
int ti[30];
int len;
int le;
const int inf = 10000000;
int ui(int id, int num) {
  int l = 1, r = len;
  while (l < r) {
    int m = (l + r) / 2;
    if (ai[m][id] >= num)
      r = m;
    else
      l = m + 1;
  }
  return l + 1;
}
int uu(int id, int num) {
  for (int i = 1; i <= len; i++) {
    if (ai[i][id] >= num) return i;
  }
}
int main() {
  scanf("%d", &len);
  char s[1000000];
  scanf("%s", s);
  for (int i = 1; i <= len; i++) {
    for (int j = 1; j <= 26; j++) {
      ai[i][j] = ai[i - 1][j];
    }
    ai[i][s[i - 1] - 'a' + 1]++;
  }
  int n;
  scanf("%d", &n);
  char tt[1000000];
  while (n--) {
    memset(ti, 0, sizeof(ti));
    scanf("%s", tt);
    le = strlen(tt);
    int ans = 0;
    for (int i = 1; i <= le; i++) {
      ti[tt[i - 1] - 'a' + 1]++;
    }
    for (int i = 1; i <= 26; i++) {
      if (ti[i] != 0) ans = max(ans, ui(i, ti[i]));
    }
    printf("%d\n", ans - 1);
  }
}
