#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, q, ans;
  cin >> n;
  string str, sec;
  cin >> str;
  int arr[26][n], cnt[26] = {0}, c2[26];
  for (int i = 0; i < n; i++) {
    cnt[str[i] - 'a']++;
    arr[str[i] - 'a'][cnt[str[i] - 'a']] = i + 1;
  }
  cin >> q;
  while (q--) {
    memset(c2, 0, sizeof(c2));
    ans = 0;
    cin >> sec;
    for (int i = 0; i < sec.size(); i++) {
      c2[sec[i] - 'a']++;
    }
    for (int i = 0; i < 26; i++) {
      if (c2[i] > 0 && ans < arr[i][c2[i]]) ans = arr[i][c2[i]];
    }
    cout << ans << '\n';
  }
  return 0;
}
