#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3;
vector<int> v[27];
string s;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int i, j, n, m, x, a[27], mx;
  cin >> n >> s;
  for (i = 0; i < n; i++) {
    x = s[i] - 97;
    v[x].push_back((i + 1));
  }
  cin >> m;
  while (m--) {
    cin >> s;
    n = s.size();
    mx = 0;
    memset(a, 0, sizeof(a));
    for (i = 0; i < n; i++) {
      x = s[i] - 97;
      a[x]++;
    }
    for (i = 0; i < 26; i++) {
      if (a[i] > 0) {
        mx = max(mx, v[i][a[i] - 1]);
      }
    }
    cout << mx << "\n";
  }
  return 0;
}
