#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n;
  cin >> n;
  string str;
  cin >> str;
  unordered_map<char, vector<long long int> > points;
  for (int i = n - 1; i >= 0; --i) {
    if (points.find(str[i]) == points.end()) {
      points[str[i]] = vector<long long int>();
      points[str[i]].emplace_back(i);
    } else {
      points[str[i]].emplace_back(i);
    }
  }
  long long int t;
  cin >> t;
  while (t--) {
    string name;
    cin >> name;
    long long int maxi = 0;
    unordered_map<char, long long int> indexes;
    for (auto c : name) {
      if (indexes.find(c) == indexes.end()) {
        indexes[c] = points[c].size() - 1;
      }
      maxi = max(maxi, points[c][indexes[c]] + 1);
      indexes[c]--;
    }
    cout << maxi << endl;
  }
  return 0;
}
