#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long arr[26][200001];
int main() {
  long long test, n;
  long long dem[26];
  string s;
  memset(dem, 0, sizeof(dem));
  cin >> n >> s;
  for (int i = 0; i < s.size(); i++) {
    dem[s[i] - 97]++;
    arr[s[i] - 97][dem[s[i] - 97]] = i + 1;
  }
  cin >> test;
  while (test--) {
    cin >> s;
    long long ans = 0;
    memset(dem, 0, sizeof(dem));
    for (int i = 0; i < s.size(); i++) {
      dem[s[i] - 97]++;
      ans = max(ans, arr[s[i] - 97][dem[s[i] - 97]]);
    }
    cout << ans << "\n";
  }
  exit(0);
}
