#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  int n;
  string s;
  string name;
  vector<int> al[26];
  cin >> n;
  cin >> s;
  for (int i = 0; i < n; i++) al[s[i] - 'a'].push_back(i);
  cin >> t;
  while (t) {
    cin >> name;
    int cnt[26];
    int best = -1;
    memset(cnt, -1, 26 * sizeof(int));
    for (int i = 0; i < name.size(); i++) {
      cnt[name[i] - 'a']++;
      best = max(best, al[name[i] - 'a'][cnt[name[i] - 'a']]);
    }
    cout << best + 1 << endl;
    t--;
  }
}
