#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n;
  vector<int> a[26];
  int i;
  char ch;
  for (i = 0; i < n; ++i) {
    cin >> ch;
    a[ch - 'a'].push_back(i + 1);
  }
  cin >> m;
  string s;
  int j, ot = 0;
  for (i = 0; i < m; ++i) {
    cin >> s;
    ot = 0;
    int b[26] = {0};
    for (j = 0; j < s.size(); ++j) {
      b[s[j] - 'a']++;
    }
    for (j = 0; j < 26; ++j) {
      if (b[j] != 0) ot = max(a[j][b[j] - 1], ot);
    }
    cout << ot << endl;
  }
  return 0;
}
