#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long double eps = 1e-7;
const int inf = 1000000010;
const long long INF = 10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 100010, LOG = 20;
int n, m, k, u, v, x, y, t, a, b, ans;
string s;
vector<int> pos[26];
int cnt[26];
void solve() {
  memset(cnt, 0, sizeof(cnt));
  ans = 0;
  cin >> s;
  for (char ch : s) cnt[ch - 'a']++;
  for (int i = 0; i < 26; i++) ans = max(ans, pos[i][cnt[i]]);
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s >> m;
  for (int i = 0; i < 26; i++) pos[i].push_back(0);
  for (int i = 1; i <= n; i++) pos[s[i - 1] - 'a'].push_back(i);
  while (m--) solve();
  return 0;
}
