#include <bits/stdc++.h>
using namespace std;
template <class T>
using max_pq = priority_queue<T>;
template <class T>
using min_pq = priority_queue<T, vector<T>, greater<T> >;
vector<string> split(const string& s, char c) {
  vector<string> v;
  stringstream second(s);
  string x;
  while (getline(second, x, c)) v.emplace_back(x);
  return move(v);
}
template <typename T, typename... Args>
inline string arrStr(T arr, long long n) {
  stringstream s;
  s << "[";
  for (long long i = 0; i < n - 1; i++) s << arr[i] << ",";
  s << arr[n - 1] << "]";
  return s.str();
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
long long oo = 0x3f3f3f3f;
long long n;
string str;
vector<long long> arr[26];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> n >> str;
  for (long long i = 0; i < n; i++) {
    arr[str[i] - 'a'].push_back(i + 1);
  }
  long long testcases;
  cin >> testcases;
  for (long long test = 1; test <= testcases; test++) {
    string str1;
    cin >> str1;
    long long f[26];
    for (long long i = 0; i < 26; i++) f[i] = 0;
    for (long long i = 0; i < str1.length(); i++) {
      f[str1[i] - 'a']++;
    }
    long long maxx = 0;
    for (long long i = 0; i < 26; i++) {
      if (f[i] != 0) {
        maxx = max(maxx, arr[i][f[i] - 1]);
      }
    }
    cout << maxx << "\n";
  }
  return 0;
}
