#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b, int& x, int& y) {
  if (!a) {
    x = 0, y = 1;
    return b;
  }
  int xx, yy, g = gcd(b % a, a, xx, yy);
  x = yy - b / a * xx;
  y = xx;
  return g;
}
inline int normal(int n) {
  n %= 1000000007;
  (n < 0) && (n += 1000000007);
  return n;
}
inline int inv(int a) {
  int x, y;
  assert(gcd(a, 1000000007, x, y) == 1);
  return normal(x);
}
inline int add(int a, int b) {
  return a + b >= 1000000007 ? a + b - 1000000007 : a + b;
}
inline int sub(int a, int b) { return a - b < 0 ? a - b + 1000000007 : a - b; }
inline int mul(int a, int b) { return int(a * 1ll * b % 1000000007); }
inline int _div(int a, int b) { return mul(a, inv(b)); }
inline int binPow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = mul(ans, a);
    a = mul(a, a);
    b >>= 1;
  }
  return ans;
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<unordered_map<int, int>> ma(26);
  vector<int> co(26, 0);
  for (int i = 0; i <= n - 1; i++) {
    co[s[i] - 'a']++;
    ma[s[i] - 'a'][co[s[i] - 'a']] = i + 1;
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    string name;
    cin >> name;
    vector<int> co2(26, 0);
    int ans = 0;
    for (auto i : name) co2[i - 'a']++;
    for (int i = 0; i <= 25; i++)
      if (co2[i] != 0) ans = max(ans, ma[i][co2[i]]);
    cout << ans << "\n";
  }
  return 0;
}
