#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  string s;
  cin >> s;
  map<char, vector<int>> m;
  for (int i = 0; i < n; i++) {
    m[s[i]].push_back(i);
  }
  long long r;
  cin >> r;
  string t;
  while (r--) {
    int ret = 0;
    cin >> t;
    map<char, int> a;
    for (const auto &c : t) ret = max(ret, m[c][a[c]++]);
    cout << ret + 1 << "\n";
  }
}
