#include <bits/stdc++.h>
using namespace std;
struct shop {
  int l;
  int val[200005];
} a[30];
char ch[200005];
long long kk;
int finds(int r[], int x) { return r[x - 1]; }
int main() {
  int s[30];
  memset(s, 0, sizeof(s));
  for (int i = 0; i <= 26; i++) a[i].l = 0;
  int n;
  scanf("%d\n", &n);
  for (int i = 0; i < n; i++) {
    char x;
    scanf("%c", &x);
    a[x - 'a'].val[a[x - 'a'].l] = i + 1;
    a[x - 'a'].l++;
  }
  int m;
  scanf("%d", &m);
  while (m--) {
    scanf(" %s", ch);
    int ll = strlen(ch);
    for (int i = 0; i < ll; i++) {
      s[ch[i] - 'a']++;
    }
    int res = 0;
    for (int i = 0; i < 26; i++) {
      if (s[i] > 0) {
        res = max(res, finds(a[i].val, s[i]));
        s[i] = 0;
      }
    }
    printf("%d\n", res);
  }
  return 0;
}
