#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 5;
vector<int> v[26];
string t, s;
void work() {
  int n;
  cin >> n >> s;
  for (int i = 0; i <= n - 1; ++i) v[s[i] - 'a'].push_back(i + 1);
  int m;
  cin >> m;
  while (m--) {
    int ans = 0;
    cin >> t;
    vector<int> a(26);
    for (auto &it : t) a[it - 'a']++;
    for (int i = 0; i <= 25; ++i) {
      if (a[i] > 0) ans = max(ans, v[i][a[i] - 1]);
    }
    printf("%d\n", ans);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  work();
  return 0;
}
