#include <bits/stdc++.h>
using namespace std;
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, 1};
int dy4[] = {1, -1, 1, -1};
int main() {
  int n, m, mx;
  string s;
  cin >> n >> s >> m;
  int a[100][n + 1];
  map<char, int> make_pair;
  for (int i = 0; i < n; i++) {
    make_pair[s[i] - 97]++;
    a[s[i] - 97][make_pair[s[i] - 97]] = i + 1;
  }
  for (int i = 0; i < m; i++) {
    cin >> s;
    mx = -1;
    map<char, int> cnt;
    for (int j = 0; j < s.size(); j++) {
      cnt[s[j] - 97]++;
      mx = max(mx, a[s[j] - 97][cnt[s[j] - 97]]);
    }
    cout << mx << endl;
  }
  return 0;
}
