#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    int t;
    cin>>t;
    while(t--)
    {
        string d;
        int k;
        cin>>d;
        int f[130]={0},g[130]={0};
        for(int i=0;i<d.size();i++)
        {
           k=int(d[i]);
           f[k]++;
        }
        int flag=1,in;
        for(int i=0;i<n;i++)
        {
            k=int(s[i]);
            g[k]++;
            flag=1;
            for(int j=0;j<d.size();j++)
            {
              int q=(int)d[j];
              if(g[q]<f[q]){flag=0;break;}
            }
            if(flag==1){in=i;break;}

        }
        cout<<in+1<<endl;

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;

    cin>>n;
    string s;
    cin>>s;
    int t;
    cin>>t;
    while(t--)
    {
        int x,m=INT_MIN;
        string d;
        cin>>d;
        int c[130]={0},k,in;
        for(int i=0;i<d.size();i++)
        {
            k=(int)d[i];
            c[k]++;
        }
        int z[sizeof(c)]={0};

        for(int i=0;i<n;i++)
        {
             k=(int)s[i];
             if(c[k]>0)c[k]--;
           if(memcmp(c,z,sizeof(c))==0){in=i;break;}
        }
        cout<<in+1<<endl;

    }

    return 0;
}


#include <bits/stdc++.h>
#define NN 200010
using namespace std;
char s[NN],s0[NN];
int a[200][NN],cnt[200],b[200][NN],tol[200];
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
      int n,m;
      cin>>n>>s;
      for(int i=0;i<strlen(s);i++){
            cnt[s[i]-'0']++;
            a[s[i]-'0'][cnt[s[i]-'0']]=i+1;
      }
      cin>>m;
      for(int i=0;i<m;i++){
            int mx=0;
            scanf("%s",s0);
            memset(tol,0,sizeof(tol));
            sort(s0,s0+strlen(s0));
            for(int j=0;j<strlen(s0);j++){
                  tol[s0[j]-'0']++;
                  mx=max(a[s0[j]-'0'][tol[s0[j]-'0']],mx);
            }
            cout<<mx<<endl;
      }

  return 0;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back

using namespace std;

typedef long long INT;
typedef double DO;
typedef pair<char,int>pii;
typedef pair<pii,int> pi3;
typedef vector<int>VI;

template<typename T, typename U> inline void smin(T &a, U b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, U b) {if(a<b) a=b;}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}

#define NN 200010
int T, n, m;
int flag[NN], pos[30];
map<pii,int>mp;
char s[NN], t[NN];

int main () {
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	scanf("%d %s", &n, s+1);
	for (int i=1; i<=n; i++) flag[s[i]]++, mp[pii(s[i], flag[s[i]])]=i;
	scanf("%d", &m);
	for (int i=0; i<m; i++) {
		scanf("%s", t);
		for (int i=0; i<26; i++) pos[i]=0;
		int mx=0;
		for (int j=0; j<strlen(t); j++)  pos[t[j]-'a']++, smax(mx, mp[pii(t[j], pos[t[j]-'a'])]);
		printf("%d\n", mx);
	}
	return 0;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back

using namespace std;

typedef long long INT;
typedef double DO;
typedef pair<int,int>pii;
typedef pair<pii,int> pi3;
typedef vector<int>VI;

template<typename T, typename U> inline void smin(T &a, U b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, U b) {if(a<b) a=b;}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}

#define NN 200010
int T, n, q;
int flag[NN], last[NN];
char s[NN], t[NN];

int main () {
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	scanf("%d %s", &n, s+1);
	for (int i=1; i<=n; i++) if (!flag[s[i]]) flag[s[i]]=1, last[s[i]]=i;
	scanf("%d", &q);
	for (int i=0; i<q; i++) {
		scanf("%s", t);
		int mx=0;
		for (int j=0; j<strlen(t); j++) smax(mx, last[t[j]]);
		printf("%d\n", mx);
	}
	return 0;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back

using namespace std;

typedef long long INT;
typedef double DO;
typedef pair<int,int>pii;
typedef pair<pii,int> pi3;
typedef vector<int>VI;

template<typename T, typename U> inline void smin(T &a, U b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, U b) {if(a<b) a=b;}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}

#define NN 200010
int T, n, m;
int p[30][NN], flag[30], pos[30];
char s[NN], t[NN];

int main () {
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	scanf("%d %s", &n, s+1);
	for (int i=1; i<=n; i++) flag[s[i]-'a']++, p[s[i]-'a'][flag[s[i]-'a']]=i;
	scanf("%d", &m);
	while (m--) {
		scanf(" %s", t);
		for (int i=0; i<26; i++) pos[i]=0;
		int mx=0;
		for(int i=0; i<strlen(t); i++) pos[t[i]-'a']++, smax(mx, p[t[i]-'a'][pos[t[i]-'a']]);
		printf("%d\n", mx);
	}
	return 0;
}


#include<bits/stdc++.h>

#define pb push_back
#define sz(a) ((int)a.size())
#define x first
#define y second
#define bpt(x) (__builtin_popcount(x))
#define bit(x, y) (((x)>>(y))&1)
#define bclz(x) (__builtin_clz(x))
#define bctz(x) (__builtin_ctz(x))

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, U b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, U b) {if(a<b) a=b;}

template<class T>inline void gn(T &x) {char c, sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;}
template<class T>inline void print(T x) {if (x<0) {putchar('-');return print(-x);}if (x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}

int power(int a, int b, int m, int ans=1) {
	for(; b; b>>=1, a=1LL*a*a%m) if(b&1) ans=1LL*ans*a%m;
	return ans;
}

const int NN = 200011;
char s[NN], t[NN];
int cnt[2222];
int pos[NN][26];

int calc() {
    scanf("%s", t);
    for(int i=0; i<26; i++) cnt[i] = 0;
    for(int i=0; t[i]; i++) cnt[t[i]-'a']++;
    int mx=0;
    for(int i=0; i<26; i++) smax(mx, pos[cnt[i]][i]);
    return mx;
}

int solve() {
    int n;
    cin >> n;
    scanf("%s", s+1);
    for(int i=1; i<=n; i++) {
        s[i] -= 'a';
        cnt[s[i]]++;
        pos[cnt[s[i]]][s[i]] = i;
    }
    int m;
    cin >> m;
    while(m--) {
        printf("%d\n", calc());
    }

}
int main() {
#ifndef	ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	solve();
	return 0;
}


#include <bits/stdc++.h>
#define NN 200010
using namespace std;
char s[NN],s0[NN];
int a[30][NN],cnt[30],tol[30];
int main() {
      int n,m;
      cin>>n>>s>>m;
      int ll=strlen(s);
      for(int i=0;i<ll;i++){
            int ss=s[i]-'a';
            cnt[ss]++;
            a[ss][cnt[ss]]=i+1;
      }
      for(int i=0;i<m;i++){
            scanf("%s",s0);
            int len=strlen(s0);
            int mx=0;
            for(int j=0;j<len;j++){
                  tol[s0[j]-'a']++;
            }
            for(int j=0;j<26;j++){
                  mx=max(a[j][tol[j]],mx);
            }

            cout<<mx<<endl;
            memset(tol,0,sizeof(tol));
      }
      return 0;
}


#include <bits/stdc++.h>
#define NN 200000
using namespace std;
char s[NN],a[NN];
int A[26][NN],ee[26];
int main() {
	int n,m;
	scanf("%d%s%d",&n,s,&m);
	for(int i=0;i<n;i++) {int ss=s[i]-'a';ee[ss]++;A[ss][ee[ss]]=i+1;}
	for(int i=0;i<m;i++){
		memset(ee,0,sizeof ee);
		scanf("%s",a);
		int ll=strlen(a);
		int ans=0;
		for(int j=0;j<ll;j++){
			int r=a[j]-'a';
			ee[r]++;
			ans=max(ans,A[r][ee[r]]);
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define NN 200010
using namespace std;
char s[NN],s0[NN];
int a[30][NN],cnt[30],tol[30];
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
      int n,m;
      cin>>n>>s;
      for(int i=0;i<strlen(s);i++){
            cnt[s[i]-'a']++;
            a[s[i]-'a'][cnt[s[i]-'a']]=i+1;
      }
      cin>>m;
      for(int i=0;i<m;i++){
            int mx=0;
            scanf("%s",s0);
            memset(tol,0,sizeof(tol));
            sort(s0,s0+strlen(s0));
            for(int j=0;j<strlen(s0);j++){
                  tol[s0[j]-'a']++;
                  mx=max(a[s0[j]-'a'][tol[s0[j]-'a']],mx);
            }
            cout<<mx<<endl;
      }

  return 0;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back

using namespace std;

typedef long long INT;
typedef double DO;
typedef pair<int,int>pii;
typedef pair<pii,int> pi3;
typedef vector<int>VI;

template<typename T, typename U> inline void smin(T &a, U b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, U b) {if(a<b) a=b;}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}

#define NN 200010
int T, n, m;
int p[30][NN], flag[30], pos[30];
char s[NN], t[NN];

int main () {
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	scanf("%d %s", &n, s+1);
	for (int i=1; i<=n; i++) flag[s[i]-'a']++, p[s[i]-'a'][flag[s[i]-'a']]=i;
	scanf("%d", &m);
	while (m--) {
		scanf(" %s", t);
		VI pos(30, 0);
		int mx=0;
		for(int i=0; i<strlen(t); i++) pos[t[i]-'a']++, smax(mx, p[t[i]-'a'][pos[t[i]-'a']]);
		printf("%d\n", mx);
	}
	return 0;
}


#include <bits/stdc++.h>
#define NN 200010
using namespace std;
char s[NN],s0[NN];
int a[30][NN],cnt[30],tol[30];
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
      int n,m;
      cin>>n>>s>>m;
      int ll=strlen(s);
      for(int i=0;i<ll;i++){
            int ss=s[i]-'a';
            cnt[ss]++;
            a[ss][cnt[ss]]=i+1;
      }
      for(int i=0;i<m;i++){
            scanf("%s",s0);
            int len=strlen(s0);
            sort(s0,s0+len);

            int mx=0;
            for(int j=0;j<len;j++){
                  int ss=s0[j]-'a';
                  tol[ss]++;
                  mx=max(a[ss][tol[ss]],mx);
            }

            cout<<mx<<endl;
            memset(tol,0,sizeof(tol));
      }

  return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long INT;
typedef double DO;
typedef pair<int, int> pii;
typedef vector<int> VI;

#define x first
#define y second
#define pb push_back

int power(int a, int b, int c, int ans=1) {
	for (; b; b>>=1, a=(INT)a*a%c) if (b&1) ans=(INT)ans*a%c;
	return ans;
}

const int NN = 202020;

char s[NN];
int cnt[NN][26],cc[26];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n, T;
	cin>>n>>(s+1);
	for (int i=1; i<=n; i++) {
		memcpy(cnt[i], cnt[i-1], sizeof cnt[i-1]);
		cnt[i][s[i]-'a']++;
	}

	for(cin>>T; T--; ) {
		scanf("%s", s);
		memset(cc, 0, sizeof cc);
		for (int i=0; s[i]; i++) cc[s[i]-'a']++;
		int st = 0, ed = n;
		while (ed-st>1) {
			int md=st+ed>>1;
			int ok=1;
			for (int i=0; i<26 && ok; i++) if (cnt[md][i]<cc[i]) ok=0;
			if (ok) ed=md;
			else st=md;
		}
		printf("%d\n", ed);
	}

	return 0;
}


#include <bits/stdc++.h>
#define NN 200010
using namespace std;
char s[NN],s0[NN];
int a[30][NN],cnt[30],tol[30];
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
      int n,m;
      cin>>n>>s;
      int ll=strlen(s);
      for(int i=0;i<ll;i++){
            cnt[s[i]-'a']++;
            a[s[i]-'a'][cnt[s[i]-'a']]=i+1;
      }
      cin>>m;
      for(int i=0;i<m;i++){
            int mx=0;
            scanf("%s",s0);
            int len=strlen(s0);
            memset(tol,0,sizeof(tol));
            sort(s0,s0+len);
            for(int j=0;j<len;j++){
                  tol[s0[j]-'a']++;
                  mx=max(a[s0[j]-'a'][tol[s0[j]-'a']],mx);
            }
            cout<<mx<<endl;
      }

  return 0;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back

using namespace std;

typedef long long INT;
typedef double DO;
typedef pair<char,int>pii;
typedef pair<pii,int> pi3;
typedef vector<int>VI;

template<typename T, typename U> inline void smin(T &a, U b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, U b) {if(a<b) a=b;}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}

#define NN 200010
int T, n, q;
int flag[NN], pos[30];
map<pii,int>mp;
char s[NN], t[NN];

int main () {
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	scanf("%d %s", &n, s+1);
	for (int i=1; i<=n; i++) flag[s[i]]++, mp[pii(s[i], flag[s[i]])]=i;
	scanf("%d", &q);
	for (int i=0; i<q; i++) {
		scanf("%s", t);
		memset (pos, 0, sizeof(pos));
		int mx=0;
		for (int j=0; j<strlen(t); j++)  pos[t[j]-'a']++, smax(mx, mp[pii(t[j], pos[t[j]-'a'])]);
		printf("%d\n", mx);
	}
	return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sq(x) ((x)*(x))
using namespace std;
typedef double DO;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;

const int inf=0x3f3f3f3f,mod=1000000007;
const INT INF=0x3f3f3f3f3f3f3f3fLL;
const DO pi=acosl(-1.),eps=1e-9;

inline int power(int a, int b, int m=mod, int ans=1) {
  for(; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
  return ans;
}

const int NN = 2e5+10;

int p[26][NN];
char s[NN];
int cnt[26];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  int n;
  scanf("%d %s", &n, s+1);
  for(int i=1; i<=n; i++) {
    cnt[s[i]-'a']++;
    p[s[i]-'a'][cnt[s[i]-'a']]=i;
  }
  int tes;
  scanf("%d", &tes);
  while(tes--) {
    scanf("%s", s);
    for(int i=0; i<26; i++) cnt[i]=0;
    for(int i=0; s[i]; i++) cnt[s[i]-'a']++;
    int ans=0;
    for(int i=0; i<26; i++) {
      ans=max(ans, p[i][cnt[i]]);
    }
    printf("%d\n", ans);
  }

  return 0;
}


#include <bits/stdc++.h>
#define NN 200010
using namespace std;
char s[NN],s0[NN];
int a[30][NN],cnt[30],tol[30];
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
      int n,m;
      cin>>n>>s;
      for(int i=0;i<strlen(s);i++){
            cnt[s[i]-'a']++;
            a[s[i]-'a'][cnt[s[i]-'a']]=i+1;
      }
      cin>>m;
      for(int i=0;i<m;i++){
            int mx=0;
            scanf("%s",s0);
            int len=strlen(s0);
            memset(tol,0,sizeof(tol));
            sort(s0,s0+len);
            for(int j=0;j<len;j++){
                  tol[s0[j]-'a']++;
                  mx=max(a[s0[j]-'a'][tol[s0[j]-'a']],mx);
            }
            cout<<mx<<endl;
      }

  return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second

using namespace std;
typedef long long INT;
typedef pair <int, int> pii;
typedef vector <int> VI;

#define NN 200200
char s[NN], t[NN];
int cnt[26][NN];

int main (){
#ifndef ONLINE_JUDGE
	freopen ("in.in", "r", stdin);
	freopen ("out.out", "w", stdout);
#endif

	int n;
	cin >> n;
	scanf("%s", s+1);
	for(int i=1; i<=n; i++){
		for(int j=0; j<26; j++) cnt[j][i]=cnt[j][i-1];
		cnt[s[i]-'a'][i]++;
	}

	int m;
	cin >> m;
	while(m--){
		scanf("%s", t);
		int ct[26]={0, };
		for(int i=0; t[i]; i++) ct[t[i]-'a']++;
		int ans=0;
		for(int i=0; i<26; i++){
			int j=lower_bound(cnt[i], cnt[i]+n+1, ct[i])-cnt[i];
			ans=max(ans, j);
		}
		printf("%d\n", ans);
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long INT;
template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

#define NN 200200

char s[NN];
int num[NN][30], cnt[30];

inline bool can(int x) {
	for(int i=0; i<26; i++) if(num[x][i]<cnt[i]) return false;
	return true;
}

main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n; scanf("%d %s", &n, s+1);
	for(int i=1; i<=n; i++) {
		for(int j=0; j<26; j++) num[i][j]=num[i-1][j];
		num[i][s[i]-'a']++;
	}

	int m; scanf("%d", &m);
	while(m--) {
		scanf("%s", s);
		for(int i=0; i<26; i++) cnt[i]=0;
		int N=strlen(s);
		for(int i=0; i<N; i++) cnt[s[i]-'a']++;

		int st=0, ed=n;
		while(st+1<ed) {
			int md=st+ed>>1;
			if(can(md)) ed=md;
			else st=md;
		}
		printf("%d\n", ed);
	}
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back

using namespace std;

typedef long long INT;
typedef double DO;
typedef pair<int,int>pii;
typedef pair<pii,int> pi3;
typedef vector<int>VI;

template<typename T, typename U> inline void smin(T &a, U b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, U b) {if(a<b) a=b;}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}

#define NN 200010
int T, n, m;
int p[30][NN], flag[30], pos[30];
char s[NN], t[NN];

int main () {
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	scanf("%d %s", &n, s+1);
	for (int i=1; i<=n; i++) flag[s[i]-'a']++, p[s[i]-'a'][flag[s[i]-'a']]=i;
	scanf("%d", &m);
	while (m--) {
		scanf(" %s", t);
		for (int i=0; i<26; i++) pos[i]=0;
		int mx=0;
		for(int i=0,N=strlen(t); i<N; i++) pos[t[i]-'a']++, smax(mx, p[t[i]-'a'][pos[t[i]-'a']]);
		printf("%d\n", mx);
	}
	return 0;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back

using namespace std;

typedef long long INT;
typedef double DO;
typedef pair<int,int>pii;
typedef pair<pii,int> pi3;
typedef vector<int>VI;

template<typename T, typename U> inline void smin(T &a, U b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, U b) {if(a<b) a=b;}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}

#define NN 200010
int T, n, m;
int p[30][NN], flag[30], pos[30];
char s[NN], t[NN];

int main () {
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	scanf("%d %s", &n, s+1);
	for (int i=1; i<=n; i++) flag[s[i]-'a']++, p[s[i]-'a'][flag[s[i]-'a']]=i;
	scanf("%d", &m);
	while (m--) {
		scanf(" %s", t);
		memset (pos, 0, sizeof(pos));
		int mx=0;
		for(int i=0; i<strlen(t); i++) pos[t[i]-'a']++, smax(mx, p[t[i]-'a'][pos[t[i]-'a']]);
		printf("%d\n", mx);
	}
	return 0;
}


#include<bits/stdc++.h>

#define x first
#define y second

using namespace std;

typedef long long INT;

#define NN 200010

char s[NN];
char t[NN];
int cnt[30];
vector<int> res[30];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	scanf("%d", &n);
	scanf("%s", s+1);

	for(int i=1; i<=n; i++) {
		cnt[s[i] - 'a']++;
		for(int j=0; j<26; j++) res[j].push_back(cnt[j]);
	}

	memset(cnt, 0, sizeof cnt);
	int T;scanf("%d", &T);

	while(T--) {
		scanf("%s", t+1);
		int m = strlen(t+1);
		memset(cnt, 0, sizeof cnt);
		for(int i=1; i<=m; i++) cnt[t[i] - 'a']++;
		int id = 0;
		for(int j=0; j<=25; j++) id = max(id, lower_bound(res[j].begin(), res[j].end(), cnt[j]) - res[j].begin());
		printf("%d\n", id+1);
	}

	return 0;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back

using namespace std;

typedef long long INT;
typedef double DO;
typedef pair<int,int>pii;
typedef pair<pii,int> pi3;
typedef vector<int>VI;

template<typename T, typename U> inline void smin(T &a, U b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, U b) {if(a<b) a=b;}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}

#define NN 200010
int T, n, m;
int flag[NN], pos[30];
int last[30][NN];
char s[NN], t[NN];

int main () {
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	scanf("%d %s", &n, s+1);
	for (int i=1; i<=n; i++) flag[s[i]-'a']++, last[s[i]-'a'][flag[s[i]-'a']]=i;
	scanf("%d", &m);
	for (int i=0; i<m; i++) {
		scanf("%s", t);
		for (int i=0; i<26; i++) pos[i]=0;
		int mx=0;
		for (int j=0; j<strlen(t); j++)  pos[t[j]-'a']++, smax(mx, last[t[j]-'a'][pos[t[j]-'a']]);
		printf("%d\n", mx);
	}
	return 0;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back

using namespace std;

typedef long long INT;
typedef double DO;
typedef pair<int,int>pii;
typedef pair<pii,int> pi3;
typedef vector<int>VI;

template<typename T, typename U> inline void smin(T &a, U b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, U b) {if(a<b) a=b;}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}

#define NN 200010
int T, n, m;
int p[30][NN], flag[30], pos[30];
char s[NN], t[NN];

int main () {
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	scanf("%d %s", &n, s+1);
	for (int i=1; i<=n; i++) flag[s[i]-'a']++, p[s[i]-'a'][flag[s[i]-'a']]=i;
	scanf("%d", &m);
	while (m--) {
		scanf(" %s", t);
		for (int i=0; i<26; i++) pos[i]=0;
		int mx=0;
		for(int i=0, N=strlen(t); i<N; i++) pos[t[i]-'a']++, smax(mx, p[t[i]-'a'][pos[t[i]-'a']]);
		printf("%d\n", mx);
	}
	return 0;
}


#include<bits/stdc++.h>
#define NN 200200
#define x first
#define y second
using namespace std;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int,int> pii;
char s[NN],a[NN];
int vst[30][NN],dd[30],ee[30];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	scanf("%d",&n);
	scanf("%s",s);
	for(int i=0;i<n;i++){
		int r=s[i]-'a';
		dd[r]++;
		vst[r][dd[r]]=i+1;
	}
	int m;
	scanf("%d",&m);
	for(int i=1;i<=m;i++){
		scanf("%s",a);
		int ll=strlen(a);
		sort(a,a+ll);
		int ans=0;
		for(int j=0;j<ll;j++){
			int r=a[j]-'a';
			ee[r]++;
			ans=max(ans,vst[r][ee[r]]);
		}
		cout<<ans<<endl;
		memset(ee,0,sizeof ee);
	}

	return 0;
}


#include<bits/stdc++.h>
#define NN 200200
#define x first
#define y second
using namespace std;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int,int> pii;
char s[NN],a[NN];
int vst[30];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	scanf("%d",&n);
	scanf("%s",s);
	for(int i=0;i<n;i++){
		if(!vst[s[i]-'a']) vst[s[i]-'a']=i+1;
	}
	int m;
	scanf("%d",&m);
	for(int i=1;i<=m;i++){
		scanf("%s",a);
		int ll=strlen(a);
		sort(a,a+ll);
		ll=unique(a,a+ll)-a;
		int ans=0;
		for(int j=0;j<ll;j++){
			if(vst[a[j]-'a']) ans=max(ans,vst[a[j]-'a']);
		}
		cout<<ans<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back

using namespace std;

typedef long long INT;
typedef double DO;
typedef pair<int,int>pii;
typedef pair<pii,int> pi3;
typedef vector<int>VI;

template<typename T, typename U> inline void smin(T &a, U b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, U b) {if(a<b) a=b;}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}

#define NN 200010
int T, n, m;
int flag[NN];
int last[30][NN];
char s[NN], t[NN];
VI pos(30, 0);

int main () {
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	scanf("%d %s", &n, s+1);
	for (int i=1; i<=n; i++) flag[s[i]-'a']++, last[s[i]-'a'][flag[s[i]-'a']]=i;
	scanf("%d", &m);
	for (int i=0; i<m; i++) {
		scanf("%s", t);
		int mx=0;
		VI pos(30, 0);
		for (int j=0; j<strlen(t); j++) {
			pos[t[j]-'a']++, smax(mx, last[t[j]-'a'][pos[t[j]-'a']]);
		}
		printf("%d\n", mx);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define NN 200010
char s[NN];
char t[NN];
int dp[NN][26];
int cnt[26];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	scanf("%d",&n);
	scanf("%s",s+1);
	for(int i=1;i<=n;i++){
		for(int j=0;j<26;j++){
			dp[i][j]=dp[i-1][j];
			if(s[i]-'a'==j) dp[i][j]++;
		}
	}
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%s",t+1);
		int m=strlen(t+1);
		for(int i=0;i<26;i++) cnt[i]=0;
		for(int i=1;i<=m;i++){
			cnt[t[i]-'a']++;
		}
		int st=0,ed=n;
		while(st+1<ed){
			int mid=(st+ed)>>1;
			int flag=0;
			for(int j=0;j<26;j++){
				if(dp[mid][j]<cnt[j]){
					flag=1;
					break;
				}
			}
			if(flag) st=mid;
			else ed=mid;
		}
		printf("%d\n",ed);
	}
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

//Data types macros
typedef long long  ll;
typedef vector<int> vec;
typedef unsigned long long ull;
typedef unsigned int ui;

//loop macros
#define forn(n) for(ll i=0;i<int(n);i++)
#define forne(n) for(ll i=0;i<=int(n);i++)

//Input macros
#define get(n) cin>>n;
#define get2(n,m) cin>>n>>m;
#define get3(t,n,m) cin>>t>>n>>m;
#define wez(n) ll (n); cin>>(n);
#define wez2(n,m) ll (n),(m); cin>>(n)>>(m);
#define wez3(n,m,k) ll (n),(m),(k); cin>>(t)>>(n)>>(m);

//Action macro
#define all(c) (c).begin(),(c).end()
#define setmax(a,b) (a)=max((a),(b))
#define setmin(a,b) (a)=min((a),(b))
#define pb(x) push_back(x)

//Specific values macros
const ll inf = 1e18+1;
const ll mod = 1e9+7;
const double pi = 3.14159265358979323846264338;

//output macros
#define outa(a,s,e) f(i,s,e) cout << a[i] << (i==e-1?'\n':' ')
#define out(n) cout << n << " ";
#define oute(n) cout<<n<<"\n"
#define out2e(n,m) cout<<n<<" "<<m<<"\n"
#define out3e(n,m,o) cout<<n<<" "<<m<<" "<<o<<"\n"

//your declarations
string s,t;
vector<int>v[26];
//

//your work

//

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    wez(n);
    cin>>s;
    forn(n){
        v[s[i]-'a'].pb(i+1);
    }
    wez(m);
    while(m--){
        cin>>t;
        int cnt[26];
        for(auto&c:t){
            ++cnt[c-'a'];
        }
        int ans=-1;
        forn(26){
            ans=max(ans,v[i][cnt[i]-1]);
        }
        oute(ans);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

//Data types macros
typedef long long  ll;
typedef vector<int> vec;
typedef unsigned long long ull;
typedef unsigned int ui;

//Input macros
#define get(n) cin>>n;
#define get2(n,m) cin>>n>>m;
#define get3(t,n,m) cin>>t>>n>>m;
#define wez(n) ll (n); cin>>(n);
#define wez2(n,m) ll (n),(m); cin>>(n)>>(m);
#define wez3(n,m,k) ll (n),(m),(k); cin>>(t)>>(n)>>(m);

//Action macro
#define all(c) (c).begin(),(c).end()
#define setmax(a,b) (a)=max((a),(b))
#define setmin(a,b) (a)=min((a),(b))
#define pb push_back

//Specific values macros
const ll inf = 1e18+1;
const ll mod = 1e9+7;
const double pi = 3.14159265358979323846264338;

//output macros
#define outa(a,s,e) f(i,s,e) cout << a[i] << (i==e-1?'\n':' ')
#define out(n) cout << n << " ";
#define oute(n) cout<<n<<"\n"
#define out2e(n,m) cout<<n<<" "<<m<<"\n"
#define out3e(n,m,o) cout<<n<<" "<<m<<" "<<o<<"\n"

//your declarations

//

//your work

//

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    wez(n);
    string s,in;
    ll pointer;
    cin>>s;
    wez(t);
    while(t--){
        cin>>in;
        sort(in.begin(),in.end());
        ll tmp=-1,cnt=0;
        for(ll i=0;i<(int)(in.size());i++){
            char p=in[i];
            //if(i>=1 && p==in[i-1])pointer=tmp+1;
            //else pointer =0;
            for(ll j=tmp+1;j<(int)(s.size());j++){
                    if(p==s[j]){
                        tmp=j;
                        cnt=max(cnt,tmp);
                        break;
                    }
            }
        }
        cout<<cnt+1<<"\n";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

//Data types macros
typedef long long  ll;
typedef vector<int> vec;
typedef unsigned long long ull;
typedef unsigned int ui;

//loop macros
#define forn(i,n) for(ll i=0;i<int(n);i++)
#define forne(i,n) for(ll i=0;i<=int(n);i++)

//Input macros
#define get(n) cin>>n;
#define get2(n,m) cin>>n>>m;
#define get3(t,n,m) cin>>t>>n>>m;
#define wez(n) ll (n); cin>>(n);
#define wez2(n,m) ll (n),(m); cin>>(n)>>(m);
#define wez3(n,m,k) ll (n),(m),(k); cin>>(t)>>(n)>>(m);

//Action macro
#define all(c) (c).begin(),(c).end()
#define setmax(a,b) (a)=max((a),(b))
#define setmin(a,b) (a)=min((a),(b))
#define pb(x) push_back(x)

//Specific values macros
const ll inf = 1e18+1;
const ll mod = 1e9+7;
const double pi = 3.14159265358979323846264338;

//output macros
#define outa(a,s,e) f(i,s,e) cout << a[i] << (i==e-1?'\n':' ')
#define out(n) cout << n << " ";
#define oute(n) cout<<n<<"\n"
#define out2e(n,m) cout<<n<<" "<<m<<"\n"
#define out3e(n,m,o) cout<<n<<" "<<m<<" "<<o<<"\n"

//your declarations
string s,t;
vector<int>v[26];
//

//your work

//

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    wez(n);
    cin>>s;
	forn(i, n)
		v[s[i] - 'a'].pb(i + 1);

	wez(m);
	while(m--){
		cin >> t;
		int cnt[26]={0};
		for (auto &c : t)
			++cnt[c - 'a'];
		int ans = -1;
		forn(j, 26){
		    if (cnt[j] > 0)
                ans = max(ans, v[j][cnt[j] - 1]);
		}
		oute(ans);
	}

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

//Data types macros
typedef long long  ll;
typedef vector<int> vec;
typedef unsigned long long ull;
typedef unsigned int ui;

//Input macros
#define get(n) cin>>n;
#define get2(n,m) cin>>n>>m;
#define get3(t,n,m) cin>>t>>n>>m;
#define wez(n) ll (n); cin>>(n);
#define wez2(n,m) ll (n),(m); cin>>(n)>>(m);
#define wez3(n,m,k) ll (n),(m),(k); cin>>(t)>>(n)>>(m);

//Action macro
#define all(c) (c).begin(),(c).end()
#define setmax(a,b) (a)=max((a),(b))
#define setmin(a,b) (a)=min((a),(b))
#define pb push_back

//Specific values macros
const ll inf = 1e18+1;
const ll mod = 1e9+7;
const double pi = 3.14159265358979323846264338;

//output macros
#define outa(a,s,e) f(i,s,e) cout << a[i] << (i==e-1?'\n':' ')
#define out(n) cout << n << " ";
#define oute(n) cout<<n<<"\n"
#define out2e(n,m) cout<<n<<" "<<m<<"\n"
#define out3e(n,m,o) cout<<n<<" "<<m<<" "<<o<<"\n"

//your declarations

//

//your work

//

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    wez(n);
    string s,in;
    ll pointer;
    cin>>s;
    wez(t);
    while(t--){
        cin>>in;
        sort(in.begin(),in.end());
        ll tmp=-1,cnt=0;
        for(ll i=0;i<(int)(in.size());i++){
            char p=in[i];
            if(i>=1 && p==in[i-1])pointer=tmp+1;
            else pointer =0;
            for(ll j=pointer;j<(int)(s.size());j++){
                    if(p==s[j]){
                        tmp=j;
                        cnt=max(cnt,tmp);
                        break;
                    }
            }
        }
        cout<<cnt+1<<"\n";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

//Data types macros
typedef long long  ll;
typedef vector<int> vec;
typedef unsigned long long ull;
typedef unsigned int ui;

//loop macros
#define forn(i,n) for(ll i=0;i<int(n);i++)
#define forne(i,n) for(ll i=0;i<=int(n);i++)

//Input macros
#define get(n) cin>>n;
#define get2(n,m) cin>>n>>m;
#define get3(t,n,m) cin>>t>>n>>m;
#define wez(n) ll (n); cin>>(n);
#define wez2(n,m) ll (n),(m); cin>>(n)>>(m);
#define wez3(n,m,k) ll (n),(m),(k); cin>>(t)>>(n)>>(m);

//Action macro
#define all(c) (c).begin(),(c).end()
#define setmax(a,b) (a)=max((a),(b))
#define setmin(a,b) (a)=min((a),(b))
#define pb(x) push_back(x)

//Specific values macros
const ll inf = 1e18+1;
const ll mod = 1e9+7;
const double pi = 3.14159265358979323846264338;

//output macros
#define outa(a,s,e) f(i,s,e) cout << a[i] << (i==e-1?'\n':' ')
#define out(n) cout << n << " ";
#define oute(n) cout<<n<<"\n"
#define out2e(n,m) cout<<n<<" "<<m<<"\n"
#define out3e(n,m,o) cout<<n<<" "<<m<<" "<<o<<"\n"

//your declarations
string s,t;
vector<int>v[26];
//

//your work

//

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    wez(n);
    cin>>s;
    forn(i,n){
        v[s[i]-'a'].pb(i+1);
    }
    wez(m);
    while(m--){
        cin>>t;
        int cnt[26];
        for(auto&c:t){
            ++cnt[c -'a'];
        }
        int ans=-1;
        forn(j,26){
            if(cnt[j]>0)
                ans=max(ans,v[j][cnt[j]-1]);
        }
        oute(ans);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

//Data types macros
typedef long long  ll;
typedef vector<int> vec;
typedef unsigned long long ull;
typedef unsigned int ui;

//loop macros
#define forn(i,n) for(ll i=0;i<int(n);i++)
#define forne(i,n) for(ll i=0;i<=int(n);i++)

//Input macros
#define get(n) cin>>n;
#define get2(n,m) cin>>n>>m;
#define get3(t,n,m) cin>>t>>n>>m;
#define wez(n) ll (n); cin>>(n);
#define wez2(n,m) ll (n),(m); cin>>(n)>>(m);
#define wez3(n,m,k) ll (n),(m),(k); cin>>(t)>>(n)>>(m);

//Action macro
#define all(c) (c).begin(),(c).end()
#define setmax(a,b) (a)=max((a),(b))
#define setmin(a,b) (a)=min((a),(b))
#define pb(x) push_back(x)

//Specific values macros
const ll inf = 1e18+1;
const ll mod = 1e9+7;
const double pi = 3.14159265358979323846264338;

//output macros
#define outa(a,s,e) f(i,s,e) cout << a[i] << (i==e-1?'\n':' ')
#define out(n) cout << n << " ";
#define oute(n) cout<<n<<"\n"
#define out2e(n,m) cout<<n<<" "<<m<<"\n"
#define out3e(n,m,o) cout<<n<<" "<<m<<" "<<o<<"\n"

//your declarations
string s,t;
vector<int>v[26];
//

//your work

//

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    wez(n);
    cin>>s;
	forn(i, n)
		v[s[i] - 'a'].pb(i + 1);

	wez(m);
	while(m--){
		cin >> t;
		vector<int> cnt(26);
		for (auto &c : t)
			++cnt[c - 'a'];
		int ans = -1;
		forn(j, 26){
		    if (cnt[j] > 0)
                ans = max(ans, v[j][cnt[j] - 1]);
		}
		oute(ans);
	}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
	int n,m;
	string s;
	cin>>n>>s>>m;
	int count[26];
	for(int i=0;i<25;i++)
	{
		count[i]=0;
	}
	int ind[26][n+1];
	for(int i=0;i<n;i++)
	{
		int temp= int(s.at(i));
		temp-=97;
		count[temp]++;
		ind[temp][count[temp]]=i+1;
	}
	while(m--)
	{
		string a;
		cin>>a;
		int max;
		int flag=0;
		int c[26];
		for(int i=0;i<25;i++)
		{
			c[i]=0;
		}
		for(int i=0;i<a.size();i++)
		{
			int temp= int(a.at(i))-97;
			c[temp]++;
			if(flag==0)
			{
				max= ind[temp][c[temp]];
				flag=1;
			}
			else
			{
				if(max<ind[temp][c[temp]])
					max=ind[temp][c[temp]];
			}
			// cout<<max<<'\n';
		}
		cout<<max<<'\n';
	}
	// cout<<ind[17][3]<<'\n';
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
	int n,m;
	string s;
	cin>>n>>s>>m;
	int count[26];
	for(int i=0;i<25;i++)
	{
		count[i]=0;
	}
	int ind[26][n+n];
	for(int i=0;i<n;i++)
	{
		int temp= int(s.at(i));
		temp-=97;
		count[temp]++;
		ind[temp][count[temp]]=i+1;
	}
	while(m--)
	{
		string a;
		cin>>a;
		int max;
		int flag=0;
		int c[26];
		for(int i=0;i<25;i++)
		{
			c[i]=0;
		}
		for(int i=0;i<a.size();i++)
		{
			int temp= int(a.at(i))-97;
			c[temp]++;
			if(flag==0)
			{
				max= ind[temp][c[temp]];
				flag=1;
			}
			else
			{
				if(max<ind[temp][c[temp]])
					max=ind[temp][c[temp]];
			}
			// cout<<max<<'\n';
		}
		cout<<max<<'\n';
	}
	// cout<<ind[17][3]<<'\n';
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
	int n,m;
	string s;
	cin>>n>>s>>m;
	int count[100];
	for(int i=0;i<99;i++)
	{
		count[i]=0;
	}
	int ind[100][n+1];
	for(int i=0;i<n;i++)
	{
		int temp= int(s.at(i));
		temp-=97;
		count[temp]++;
		// if(count[temp]<2*n)
		ind[temp][count[temp]]=i+1;
	}
	while(m--)
	{
		string a;
		cin>>a;
		int max;
		int flag=0;
		int c[100];
		for(int i=0;i<99;i++)
		{
			c[i]=0;
		}
		for(int i=0;i<a.size();i++)
		{
			int temp= int(a.at(i))-97;
			c[temp]++;
			if(flag==0)
			{
					max= ind[temp][c[temp]];
					flag=1;
			}
			else
			{
				if(max<ind[temp][c[temp]])
					max=ind[temp][c[temp]];
			}
		}
		cout<<max<<'\n';
	}
	// cout<<ind[17][3]<<'\n';
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
	int n,m;
	string s;
	cin>>n>>s>>m;
	int count[26];
	for(int i=0;i<25;i++)
	{
		count[i]=0;
	}
	int ind[26][2*n];
	for(int i=0;i<n;i++)
	{
		int temp= int(s.at(i));
		temp-=97;
		count[temp]++;
		if(count[temp]<2*n)
		ind[temp][count[temp]]=i+1;
	}
	while(m--)
	{
		string a;
		cin>>a;
		int max;
		int flag=0;
		int c[26];
		for(int i=0;i<25;i++)
		{
			c[i]=0;
		}
		for(int i=0;i<a.size();i++)
		{
			int temp= int(a.at(i))-97;
			c[temp]++;
			if(flag==0)
			{
				max= ind[temp][c[temp]];
				flag=1;
			}
			else
			{
				if(c[temp]<2*n&&max<ind[temp][c[temp]])
					max=ind[temp][c[temp]];
			}
		}
		cout<<max<<'\n';
	}
	// cout<<ind[17][3]<<'\n';
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
	int n,m;
	string s;
	cin>>n>>s>>m;
	int count[26];
	for(int i=0;i<25;i++)
	{
		count[i]=0;
	}
	int ind[26][2*n];
	for(int i=0;i<n;i++)
	{
		int temp= int(s.at(i));
		temp-=97;
		count[temp]++;
		ind[temp][count[temp]]=i+1;
	}
	while(m--)
	{
		string a;
		cin>>a;
		int max;
		int flag=0;
		int c[26];
		for(int i=0;i<25;i++)
		{
			c[i]=0;
		}
		for(int i=0;i<a.size();i++)
		{
			int temp= int(a.at(i))-97;
			c[temp]++;
			if(flag==0)
			{
				max= ind[temp][c[temp]];
				flag=1;
			}
			else
			{
				if(c[temp]<2*n&&max<ind[temp][c[temp]])
					max=ind[temp][c[temp]];
			}
		}
		cout<<max<<'\n';
	}
	// cout<<ind[17][3]<<'\n';
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
	int n,m;
	string s;
	cin>>n>>s>>m;
	int count[26];
	for(int i=0;i<25;i++)
	{
		count[i]=0;
	}
	int ind[26][2*n];
	for(int i=0;i<n;i++)
	{
		int temp= int(s.at(i));
		temp-=97;
		count[temp]++;
		if(count[temp]<2*n)
		ind[temp][count[temp]]=i+1;
	}
	while(m--)
	{
		string a;
		cin>>a;
		int max;
		int flag=0;
		int c[26];
		for(int i=0;i<25;i++)
		{
			c[i]=0;
		}
		for(int i=0;i<a.size();i++)
		{
			int temp= int(a.at(i))-97;
			c[temp]++;
			if(flag==0)
			{
				if(c[temp]<2*n)
				{
					max= ind[temp][c[temp]];
					flag=1;
				}
			}
			else
			{
				if(c[temp]<2*n&&max<ind[temp][c[temp]])
					max=ind[temp][c[temp]];
			}
		}
		cout<<max<<'\n';
	}
	// cout<<ind[17][3]<<'\n';
	return 0;
}


static auto speedup = [](){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    return nullptr;
}();
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int s;
    cin>>s;
    string st;
    cin>>st;
    unordered_map<char,vector<int> > m,m1;

    for(int i=0;i<s;i++)
    {
        m[st[i]].push_back(i);
    }
    m1=m;
  int q;
  cin>>q;
  while(q--)
  {

  unordered_map<char,int> m2,m3;
  unordered_map<char,int>::iterator it;
      string a;
      cin>>a;
      for(int i=0;i<a.size();i++)
        m2[a[i]]++;
      int sum=0;
      for(it=m2.begin();it!=m2.end();it++)
      {
            if(m[it->first][m2[it->first]-1]>sum)
            sum=m[it->first][m2[it->first]-1];

      }
      cout<<sum+1<<endl;
      m=m1;
      m2=m3;

  }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int s;
    cin>>s;
    string st;
    cin>>st;
    unordered_map<char,vector<int> > m,m1;

    for(int i=0;i<s;i++)
    {
        m[st[i]].push_back(i);
    }
    m1=m;
  int q;
  cin>>q;
  while(q--)
  {

  unordered_map<char,int> m2,m3;
  unordered_map<char,int>::iterator it;
      string a;
      cin>>a;
      for(int i=0;i<a.size();i++)
        m2[a[i]]++;
      int sum=0;
      for(it=m2.begin();it!=m2.end();it++)
      {
            if(m[it->first][m2[it->first]-1]>sum)
            sum=m[it->first][m2[it->first]-1];

      }
      cout<<sum+1<<endl;

  }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int s;
    cin>>s;
    string st;
    cin>>st;
    map<char,vector<int> > m,m1;
    for(int i=0;i<s;i++)
    {
        m[st[i]].push_back(i);
    }
    m1=m;
  int q;
  cin>>q;
  while(q--)
  {

      string a;
      cin>>a;
      int sum=0;
      for(int j=0;j<a.size();j++)
      {
          if(m[a[j]][0]>sum)
            sum=m[a[j]][0];
          m[a[j]].erase(m[a[j]].begin());
      }
      cout<<sum+1<<endl;
      m=m1;
  }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int s;
    cin>>s;
    string st;
    cin>>st;
    map<char,vector<int> > m,m1;
    for(char in='A';in<='Z';in++)
    {
        m[in].push_back(0);
    }
    for(char in='A';in<='Z';in++)
    {
        m[in].pop_back();
    }
    for(int i=0;i<s;i++)
    {
        m[st[i]].push_back(i);
    }
    m1=m;
  int q;
  cin>>q;
  map<char,int> m2,m3;
  while(q--)
  {

      string a;
      cin>>a;
      int sum=0;
      for(int j=0;j<a.size();j++)
      {
          if(m[a[j]][m2[a[j]]]>sum)
            sum=m[a[j]][m2[a[j]]];
            m2[a[j]]++;
      }
      cout<<sum+1<<endl;
      m=m1;
      m2=m3;
  }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int s;
    cin>>s;
    string st;
    cin>>st;
    unordered_map<char,vector<int> > m,m1;

    for(int i=0;i<s;i++)
    {
        m[st[i]].push_back(i);
    }
    m1=m;
  int q;
  cin>>q;
  while(q--)
  {

  unordered_map<char,int> m2,m3;
  unordered_map<char,int>::iterator it;
      string a;
      cin>>a;
      for(int i=0;i<a.size();i++)
        m2[a[i]]++;
      int sum=0;
      for(it=m2.begin();it!=m2.end();it++)
      {
            if(m[it->first][m2[it->first]-1]>sum)
            sum=m[it->first][m2[it->first]-1];

      }
      cout<<sum+1<<endl;
      m=m1;
      m2=m3;

  }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int s;
    cin>>s;
    string st;
    cin>>st;
    unordered_map<char,vector<int> > m,m1;

    for(int i=0;i<s;i++)
    {
        m[st[i]].push_back(i);
    }
    m1=m;
  int q;
  cin>>q;
  while(q--)
  {

  unordered_map<char,int> m2,m3;
  unordered_map<char,int>::iterator it;
      string a;
      cin>>a;
      for(int i=0;i<a.size();i++)
        m2[a[i]]++;
      int sum=0;
      for(it=m2.begin();it!=m2.end();it++)
      {
            if(m[it->first][m2[it->first]]>sum)
            sum=m[it->first][m2[it->first]];

      }
      cout<<sum+1<<endl;
      m=m1;
      m2=m3;

  }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;

int n;
int m;
string s;

main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> s >> m;
    while(m--)
    {
        vector<int> de(35,0), q(35,0);
        vector<bool> dd(35,true);
        string qq;
        cin >> qq;
        int l = qq.size();
        for(auto  v : qq)
        {
            de[v-'a']++;
        }
        int kq=0;
        for(int i = 0; i < s.size(); ++i)
        {
            if(l==0)break;
            if(de[s[i]-'a'])
            {
                de[s[i]-'a']--;
                l--;
                kq++;
            }
            else
            {
                kq++;
            }
        }
        cout << kq << "\n";
    }
}


#include <bits/stdc++.h>
#define int long long
using namespace std;

int n;
int m;
string s;

main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> s >> m;
    while(m--)
    {
        vector<int> de(35,0), q(35,0);
        vector<bool> dd(35,true);
        string qq;
        set<int> r;
        cin >> qq;
        for(auto ee : qq)
        {
            de[ee-'a'+1]++;
            if(!r.count(ee-'a'+1))r.insert(ee-'a'+1);
        }
        int kq = 0;
        for(auto v : s)
        {
            if(r.empty()) break;
            if(dd[v-'a'+1])
            {
                kq++;
                q[v-'a'+1]++;
                if(de[v-'a'+1]==q[v-'a'+1]) {dd[v-'a'+1]=false;
                r.erase(v-'a'+1);}
                //if(r.empty()) break;
            }
            else if(!dd[v-'a'+1]&&!r.empty())
            {
                kq++;
            }
        }
        cout << kq << "\n";
    }
}


#include <bits/stdc++.h>
#define int long long
using namespace std;

int n;
int m;
string s;

main()
{
    cin >> n >> s >> m;
    while(m--)
    {
        vector<int> de(35,0), q(35,0);
        vector<bool> dd(35,true);
        string qq;
        set<int> r;
        cin >> qq;
        for(auto ee : qq)
        {
            de[ee-'a'+1]++;
            r.insert(ee-'a'+1);
        }
        int kq = 0;
        for(auto v : s)
        {
            if(r.empty()) break;
            if(dd[v-'a'+1])
            {
                kq++;
                q[v-'a'+1]++;
                if(de[v-'a'+1]==q[v-'a'+1]) {dd[v-'a'+1]=false;
                r.erase(v-'a'+1);}
                //if(r.empty()) break;
            }
            else if(!dd[v-'a'+1]&&!r.empty())
            {
                kq++;
            }
        }
        cout << kq << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

int n;
int m;
string s;
int cnt[200005][30];

main()
{
    cin >> n >> s >> m;
    while(m--)
    {
        vector<int> de(30,0), q(30,0);
        vector<bool> dd(30,true);
        string qq;
        set<int> r;
        cin >> qq;
        for(auto ee : qq)
        {
            de[ee-'a']++;
            r.insert(ee-'a');
        }
        int kq = 0;
        for(auto v : s)
        {
            if(dd[v-'a'])
            {
                kq++;
                q[v-'a']++;
                if(de[v-'a']<=q[v-'a']) dd[v-'a']=false;
                r.erase(v-'a');
            }
            else if(!dd[v-'a']&&!r.empty()) kq++;
            if(r.empty()) break;
        }
        cout << kq << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

int n;
string s;
vector<int> v[30];

main()
{
    cin >> n >> s;
    for(int i = 0; i < s.size(); ++i)
    {
        v[s[i]-'a'].push_back(i);
    }
    int m;
    cin >> m;
    while(m--)
    {
        string t;
        cin >> t;
        vector<int> d(30,0);
        int re = -1;
        for(int i = 0; i < t.size(); ++i)
        {
            int ck = t[i]-'a';
            re=max(re,v[ck][d[ck]++]);
        }
        cout << re+1 << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;

int n;
int m;
string s;

main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> s >> m;
    while(m--)
    {
        vector<int> de(35,0), q(35,0);
        vector<bool> dd(35,true);
        string qq;
        cin >> qq;
        int l = int(qq.size());
        for(auto  v : qq)
        {
            de[v-'a']++;
        }
        int kq=0;
        for(int i = 0; i < int(s.size()); ++i)
        {
            if(l==0)break;
            if(de[s[i]-'a'])
            {
                de[s[i]-'a']--;
                l--;
                kq++;
            }
            else
            {
                kq++;
            }
        }
        cout << kq << "\n";
    }
}


#include <bits/stdc++.h>
#define int long long
using namespace std;

int n;
int m;
string s;

main()
{
    cin >> n >> s >> m;
    while(m--)
    {
        vector<int> de(30,0), q(30,0);
        vector<bool> dd(30,true);
        string qq;
        set<int> r;
        cin >> qq;
        for(auto ee : qq)
        {
            de[ee-'a'+1]++;
            r.insert(ee-'a'+1);
        }
        int kq = 0;
        for(auto v : s)
        {
            if(dd[v-'a'+1])
            {
                kq++;
                q[v-'a'+1]++;
                if(de[v-'a'+1]<=q[v-'a'+1]) dd[v-'a'+1]=false;
                r.erase(v-'a'+1);
                if(r.empty()) break;
            }
            else if(!dd[v-'a'+1]&&!r.empty()) kq++;
            if(r.empty()) break;
        }
        cout << kq << "\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int m;
    cin>>m;
    string str;
    cin>>str;
    int t;
    cin>>t;
    while(t--)
    {
    string pattern;
    cin>>pattern;
    long long int n=pattern.size();
    map<char,long long int> mp;
    for(int i=0;i<n;i++)
    {
        mp[pattern[i]]++;
    }
    int f=0;
    for(long long int i=0;i<m;i++)
    {
        if(mp.find(str[i])!=mp.end())
        {
            if(mp[str[i]]==1)
            {
                mp.erase(str[i]);
            }
            else
            {
                mp[str[i]]--;
            }
        }
        if(mp.size()==0)
        {
            f=1;
            cout<<i+1<<"\n";break;
        }
    }
    if(f==0)
    {
        cout<<"-1\n";
    }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

vector<unordered_map<char, int>> m;

bool ge(int idx, unordered_map<char, int> &b)
{
    for(auto x:b)
    {
        if(m[idx][x.first]<x.second)
            return false;
    }
    return true;
}

int check(string s)
{
    unordered_map<char, int> mm;
    for(auto x:s)
        mm[x]++;
    int ans;
    int st=0, en=m.size()-1;
    while(st<=en)
    {
        // int mid=st+((en-st)>>1);
        int mid=(st+en)/2;
        if(ge(mid, mm))
        {
            ans=mid;
            en=mid-1;
        }
        else
            st=mid+1;
    }
    return ans;
}

int main()
{
	int n;
	cin>>n;
	m.resize(n);
	string s;
	cin>>s;
	m[0][s[0]]=1;
	for(int i=1; i<n; i++)
	{
	    m[i]=m[i-1];
	    m[i][s[i]]++;
	}
	int q;
	cin>>q;
	while(q--)
	{
	    string t;
	    cin>>t;
	    cout<<check(t)+1<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

vector<unordered_map<char, int>> m;

bool ge(unordered_map<char, int> a, unordered_map<char, int> b)
{
    for(auto x:b)
    {
        if(a[x.first]<x.second)
            return false;
    }
    return true;
}

int check(string s)
{
    unordered_map<char, int> mm;
    for(auto x:s)
        mm[x]++;
    int ans;
    int st=0, en=m.size()-1;
    while(st<=en)
    {
        int mid=st+((en-st)>>1);
        // int mid=(st+en)/2;
        if(ge(m[mid], mm))
        {
            ans=mid;
            en=mid-1;
        }
        else
            st=mid+1;
    }
    return ans;
}

int main()
{
	int n;
	cin>>n;
	m.resize(n);
	string s;
	cin>>s;
	m[0][s[0]]=1;
	for(int i=1; i<n; i++)
	{
	    m[i]=m[i-1];
	    m[i][s[i]]++;
	}
	int q;
	cin>>q;
	while(q--)
	{
	    string t;
	    cin>>t;
	    cout<<check(t)+1<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int m;
    cin>>m;
    string str;
    cin>>str;
    int t;
    cin>>t;
    while(t--)
    {
    string pattern;
    cin>>pattern;
    long long int n=pattern.size();
    unordered_map<char,long long int> mp;
    for(int i=0;i<n;i++)
    {
        mp[pattern[i]]++;
    }
    int f=0;
    for(long long int i=0;i<m;i++)
    {
        if(mp.find(str[i])!=mp.end())
        {
            if(mp[str[i]]==1)
            {
                mp.erase(str[i]);
            }
            else
            {
                mp[str[i]]--;
            }
        }
        if(mp.size()==0)
        {
            f=1;
            cout<<i+1<<"\n";break;
        }
    }
    if(f==0)
    {
        cout<<"-1\n";
    }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int m;
    cin>>m;
    string str;
    cin>>str;
    int t;
    cin>>t;
    while(t--)
    {
    string pattern;
    cin>>pattern;
    int n=pattern.size();
    unordered_map<char,int> mp;
    for(int i=0;i<n;i++)
    {
        mp[pattern[i]]++;
    }
    int f=0;
    for(int i=0;i<m;i++)
    {
        if(mp.find(str[i])!=mp.end())
        {
            if(mp[str[i]]==1)
            {
                mp.erase(str[i]);
            }
            else
            {
                mp[str[i]]--;
            }
        }
        if(mp.size()==0)
        {
            f=1;
            cout<<i+1<<"\n";break;
        }
    }
    if(f==0)
    {
        cout<<"-1\n";
    }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

vector<unordered_map<char, int>> m;

bool ge(unordered_map<char, int> a, unordered_map<char, int> b)
{
    for(auto x:b)
    {
        if(a[x.first]<x.second)
            return false;
    }
    return true;
}

int check(string s)
{
    unordered_map<char, int> mm;
    for(auto x:s)
        mm[x]++;
    int ans;
    int st=0, en=m.size()-1;
    while(st<=en)
    {
        // int mid=st+((en-st)>>1);
        int mid=(st+en)/2;
        if(ge(m[mid], mm))
        {
            ans=mid;
            en=mid-1;
        }
        else
            st=mid+1;
    }
    return ans;
}

int main()
{
	int n;
	cin>>n;
	m.resize(n);
	string s;
	cin>>s;
	m[0][s[0]]=1;
	for(int i=1; i<n; i++)
	{
	    m[i]=m[i-1];
	    m[i][s[i]]++;
	}
	int q;
	cin>>q;
	while(q--)
	{
	    string t;
	    cin>>t;
	    cout<<check(t)+1<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

vector<unordered_map<char, int>> m;

bool ge(unordered_map<char, int> a, unordered_map<char, int> b)
{
    for(auto x:b)
    {
        if(a[x.first]<x.second)
            return false;
    }
    return true;
}

int check(string s)
{
    unordered_map<char, int> mm;
    for(auto x:s)
        mm[x]++;
    int ans;
    int st=0, en=m.size()-1;
    while(st<=en)
    {
        // int mid=st+(en-st)>>1;
        int mid=(st+en)/2;
        if(ge(m[mid], mm))
        {
            ans=mid;
            en=mid-1;
        }
        else
            st=mid+1;
    }
    return ans;
}

int main()
{
	int n;
	cin>>n;
	m.resize(n);
	string s;
	cin>>s;
	m[0][s[0]]=1;
	for(int i=1; i<n; i++)
	{
	    m[i]=m[i-1];
	    m[i][s[i]]++;
	}
	int q;
	cin>>q;
	while(q--)
	{
	    string t;
	    cin>>t;
	    cout<<check(t)+1<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,p;
    cin>>n;
    string s,m,q;
    cin>>s;
    int t;
    cin>>t;
    while(t--)
    {
        cin>>q;
        p=q.size();
        m=s;

        for(int i=0;i<p;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(q[i]==m[j])
                {
                    m[j]='*';
                    break;
                }
            }

        }
        for(int i=n-1;i>=0;i--)
        {
            if(m[i]=='*')
            {
                cout<<i+1<<endl;
                break;
            }
        }
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

vector<map<char, int>> m;

bool ge(map<char, int> a, map<char, int> b)
{
    for(auto x:b)
    {
        if(a[x.first]<x.second)
            return false;
    }
    return true;
}

int check(string s)
{
    map<char, int> mm;
    for(auto x:s)
        mm[x]++;
    int ans;
    int st=0, en=m.size()-1;
    while(st<=en)
    {
        // int mid=st+(en-st)>>1;
        int mid=(st+en)/2;
        if(ge(m[mid], mm))
        {
            ans=mid;
            en=mid-1;
        }
        else
            st=mid+1;
    }
    return ans;
}

int main()
{
	int n;
	cin>>n;
	m.resize(n);
	string s;
	cin>>s;
	m[0][s[0]]=1;
	for(int i=1; i<n; i++)
	{
	    m[i]=m[i-1];
	    m[i][s[i]]++;
	}
	int q;
	cin>>q;
	while(q--)
	{
	    string t;
	    cin>>t;
	    cout<<check(t)+1<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define pb push_back

int main()
{
    ll n;
    string s;
    cin>>n>>s;

    ll m;
    cin>>m;
    while(m--)
    {
        string a;
        cin>>a;

        map<char,int>mp;
        for(int i=0; i<a.size(); i++)
        {
            mp[a[i]]++;
        }

        int ans=0;
        ll sz=0;
        for(int i=0; i<s.size(); i++)
        {
            if(mp[s[i]])
            {
                mp[s[i]]--;
                ans=max(i+1,ans);
                sz--;
            }

            if(sz==0)
                break;
        }

        cout<<ans<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define pb push_back

int main()
{
    ll n;
    string s;
    cin>>n>>s;

    ll m;
    cin>>m;
    while(m--)
    {
        string a;
        cin>>a;

        ll ans=0;
        for(ll i=0; i<a.size(); i++)
        {
            for(ll j=0; j<s.size(); j++)
            {
                if(a[i]==s[j])
                {
                    ans=max(j+1,ans);
                    break;
                }
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define pb push_back

int main()
{
    ll n;
    string s;
    cin>>n>>s;

    ll m;
    cin>>m;
    while(m--)
    {
        string a;
        cin>>a;

        map<char,int>mp;
        for(int i=0; i<a.size(); i++)
        {
            mp[a[i]]++;
        }

        int ans=0;
        ll sz=a.size();
        for(int i=0; i<s.size(); i++)
        {
            if(mp[s[i]])
            {
                mp[s[i]]--;
                ans=max(i+1,ans);
                sz--;
            }

            if(sz==0)
                break;
        }

        cout<<ans<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define pb push_back

int main()
{
    ll n;
    string s;
    cin>>n>>s;

    ll m;
    cin>>m;
    while(m--)
    {
        string a;
        cin>>a;

        map<char,int>mp;
        for(int i=0; i<a.size(); i++)
        {
            mp[a[i]]++;
        }

        int ans=0;
        for(int i=0; i<s.size(); i++)
        {
            if(mp[s[i]])
            {
                mp[s[i]]--;
                ans=max(i+1,ans);
            }
        }

        cout<<ans<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define pb push_back

int main()
{
    ll n;
    string s;
    cin>>n>>s;

    ll m;
    cin>>m;
    while(m--)
    {
        string a;
        cin>>a;

        ll ans=0;
        string p=s;
        for(ll i=0; i<a.size(); i++)
        {
            for(ll j=0; j<p.size(); j++)
            {
                if(a[i]==p[j])
                {
                    ans=max(j+1,ans);
                    p[j]-=32;
                    break;
                }
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define pb push_back

int main()
{
    ll n;
    string s;
    cin>>n>>s;

    vector<ll>pos[30];
    for(ll i=0; i<s.size(); i++)
        pos[s[i]-'a'].pb(i+1);

    ll m;
    cin>>m;
    while(m--)
    {
        string a;
        cin>>a;

        map<char,ll>mp;
        for(ll i=0; i<a.size(); i++)
        {
            mp[a[i]]++;
        }

        ll ans=0;
        for(auto it:mp)
            ans=max(ans,pos[it.first-'a'][it.second-1]);

        cout<<ans<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define pb push_back

int main()
{
    ll n;
    string s;
    cin>>n>>s;

    ll m;
    cin>>m;
    while(m--)
    {
        string a;
        cin>>a;
        map<int,int>mp;
        for(int i=0; i<a.size(); i++)
        {
            mp[a[i]]=1;
        }

        int ans=0;
        for(int i=0; i<s.size(); i++)
        {
            if(mp[s[i]] && mp[s[i+1]] && i==0)
                ans++;
            else if(mp[s[i]] && mp[s[i-1]] && i==n)
                ans++;
            else if((mp[s[i]] && mp[s[i-1]]) || (mp[s[i]] && mp[s[i+1]]))
                ans++;
        }

        cout<<ans<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define pb push_back

int main()
{
    ll n;
    string s;
    cin>>n>>s;

    ll m;
    cin>>m;
    while(m--)
    {
        string a;
        cin>>a;

        ll ans=0;
        string p=s;
        for(ll i=0; i<a.size(); i++)
        {
            int cnt=1;
            for(ll j=0; j<p.size(); j++)
            {
                if(a[i]==p[j])
                {
                    ans=max(j+1,ans);
                    break;
                }
                cnt++;
            }
            p.erase(0,cnt);

            if(p.size()==0)
                break;
        }
        cout<<ans<<endl;
    }

    return 0;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{
    int n,m,ans=0; cin>>n>>m; string grade[n]; for(int i=0;i<n;i++)cin>>grade[i];
    int max[101]={0};
    for(int i=0;i<n;i++)for(int j=0;j<m;j++){if(grade[i][j]-'0'>max[j])max[j]=grade[i][j]-'0';}
    for(int i=0;i<n;i++)for(int j=0;j<m;j++){if(grade[i][j]-'0'==max[j]){ans++; j=m;}}
    cout<<ans;

}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 123;
string x[maxn];
int n, m, a[maxn][maxn], cnt;
bool mark[maxn];

int main()
{
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        cin >> x[i];

    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
         a[j][i] = x[i][j] - '0';

    for(int i = 0; i < m; i++)
        for(int j = 0; j < n; j++)
            if(a[i][j] == *max_element(a[i], a[i] + n))
                mark[j] = true;

    for(int i = 0; i < n; i++)
        if(mark[i])
            cnt++;
    cout << cnt;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 123;
string x[maxn];
int n, m, a[maxn][maxn], cnt;
bool mark[maxn];

int main()
{
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        cin >> x[i];
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
         a[j][i] = x[i][j] - '0';
    for(int i = 0; i < m; i++)
        mark[max_element(a[i], a[i] + n) - a[i]] = true;
    for(int i = 0; i < n; i++)
        if(mark[i])
            cnt++;
    cout << cnt;
}


#include <iostream>
#include <unordered_set>
using namespace std;

int main()
{
    int S,C,max,max_pos,temp_;
    char temp;

    unordered_set<int> final;

    cin >> C >> S;

    for(int i=0;i<S;i++)
    {
        max = -1;
        max_pos = -666;
        for(int j=1;j<=C;j++)
        {
            cin >> temp;
            temp_ = temp - '0';
            if(temp > max){
                max = temp;
                max_pos = j;
            }
        }
        if(max_pos != -666)
            final.insert(max_pos);
    }

    cout << final.size() << endl;
    return 0;
}


#include <iostream>
#include <unordered_set>
using namespace std;

int main()
{
    int S,C,max,max_pos,temp_,overhead=0,fack=0;
    char temp;

    unordered_set<int> final;

    cin >> C >> S;

    for(int i=0;i<S;i++)
    {
        max = -1;
        overhead = 0;
        for(int j=1;j<=C;j++)
        {
            cin >> temp;
            temp_ = temp - '0';
            if(temp_ >= max){
                if (max == temp_)
                {
                    overhead++;
                }
                else
                    overhead=0;
                max = temp_;
                max_pos = j;
            }
        }
        fack += overhead;
        final.insert(max_pos);
    }

    cout << final.size() + fack<< endl;
    return 0;
}


#include <iostream>
#include <unordered_set>
using namespace std;

int main()
{
    int S,C,max,max_pos,temp_;
    char temp;

    unordered_set<int> final;

    cin >> C >> S;

    for(int i=0;i<S;i++)
    {
        max = -1;
        max_pos = -666;
        for(int j=1;j<=C;j++)
        {
            cin >> temp;
            temp_ = temp - '0';
            if(temp_ >= max){
                max = temp_;
                max_pos = j;
            }
        }
        if(max_pos != -666)
            final.insert(max_pos);
    }

    cout << final.size() << endl;
    return 0;
}


#include <iostream>
#include <unordered_set>
#include <vector>

using namespace std;

int main()
{
    int stud,sub;
    unordered_set<int> sol;
    vector<int> sub_sol;
    vector<int>::iterator it;

    cin >> stud >> sub;

    char data[stud][sub];

    for(int i=0;i<stud;i++)
    {
        for(int j=0;j<sub;j++)
        {
            cin >> data[i][j];
        }
    }

    for(int i=0;i<sub;i++)
    {
        int temp,max=1;
        for(int j=0;j<stud;j++)
        {
            temp = data[j][i]  - '0';

            if(temp == max)
            {
                sub_sol.push_back(j);
            }

            if(temp > max)
            {
                max = temp;
                sub_sol.clear();
                sub_sol.push_back(j);
            }

        }
        for(it = sub_sol.begin();it != sub_sol.end();it++)
        {
            sol.insert(*(it));
        }
        sub_sol.clear();
    }

    cout << sol.size() << endl;

    return 0;
}


#include <iostream>
#include <unordered_set>
using namespace std;

int main()
{
    int S,C,max,max_pos,temp_;
    char temp;

    unordered_set<int> final;

    cin >> C >> S;

    for(int i=0;i<S;i++)
    {
        max = -1;
        max_pos = -666;
        for(int j=1;j<=C;j++)
        {
            cin >> temp;
            temp_ = temp - '0';
            if(temp >= max){
                max = temp;
                max_pos = j;
            }
        }
        if(max_pos != -666)
            final.insert(max_pos);
    }

    cout << final.size() << endl;
    return 0;
}


#include <iostream>
#include <unordered_set>
#include <cstdlib>
#include <cstdio>
using namespace std;

int main()
{
    int S,C,max,max_pos,temp_;
    char temp;

    unordered_set<int> final;

    cin >> C >> S;

    for(int i=0;i<S;i++)
    {
        max = -1;
        for(int j=0;j<C;j++)
        {
            temp = getchar();
            temp_ = temp - '0';
            if(temp > max){
                max = temp;
                max_pos = j+1;
                max = -200;
            }
        }
        if(max == -200)
            final.insert(max_pos);
    }

    cout << final.size() << endl;
    return 0;
}


#include <iostream>
#include <unordered_set>
#include <cstdlib>
#include <cstdio>
using namespace std;

int main()
{
    int S,C,max,max_pos,temp_;
    char temp;

    unordered_set<int> final;

    cin >> C >> S;

    for(int i=0;i<S;i++)
    {
        max = -1;
        max_pos = -666;
        for(int j=0;j<C;j++)
        {
            temp = getchar();
            temp_ = temp - '0';
            if(temp > max){
                max = temp;
                max_pos = j;
            }
        }
        if(max_pos != -666)
            final.insert(max_pos);
    }

    cout << final.size() << endl;
    return 0;
}


#include <iostream>
#include <unordered_set>
#include <cstdlib>
#include <cstdio>
using namespace std;

int main()
{
    int S,C,max,max_pos,temp_;
    char temp;

    unordered_set<int> final;

    cin >> C >> S;

    for(int i=0;i<S;i++)
    {
        max = -1;
        for(int j=0;j<C;j++)
        {
            temp = getchar();
            temp_ = temp - '0';
            if(temp > max){
                max = temp;
                max_pos = j+1;
            }
        }
        final.insert(max_pos);
    }

    cout << final.size() << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
  int i,j,k;
  k=0;
  int np,ns;
  cin>>np;
  cin>>ns;
  int mm[100];
  char c,gb[100][100];
  getchar();
  for(i=0;i<np;i++)
  {
     //j=0;
     gets(gb[i]);
     //while((c=getchar())!='\n'){gb[i][j++]=c;}
     //gb[i][j]='\0';

     //if(j==0)i--;
  }
   int mark[100];

   for(i=0;i<ns;i++)
   {
       char x=0;
       for(j=0;j<np;j++){mark[j]=0;}
       for(j=0;j<np;j++)
       {
           if(x<gb[j][i])x=gb[j][i];
       }
       for(j=0;j<np;j++)
       {
           if(x==gb[j][i])mark[j]=1;
       }
       for(j=0;j<np;j++)
       {
           if(mark[j]==1)mm[j]=1;
       }
   }

          for(j=0;j<np;j++)
       {
           if(mm[j]==1)k++;
       }
       printf("%d\n",k);
       return 0;
}


#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
  int i,j,k;
  k=0;
  int np,ns;
  cin>>np;
  cin>>ns;
  int mm[100];
  char c,gb[100][100];
  getchar();
  for(i=0;i<np;i++)
  {
     //j=0;
     gets(gb[i]);
     //while((c=getchar())!='\n'){gb[i][j++]=c;}
     //gb[i][j]='\0';

     //if(j==0)i--;
  }
   int mark[100];

   for(i=0;i<ns;i++)
   {
       char x=0;
       for(j=0;j<np;j++){mark[j]=1;}
       for(j=0;j<np;j++)
       {
           //cout<<gb[j][i]<<'\t';
           if(x>gb[j][i]){mark[j]=0;}
           else{x=gb[j][i];}
       }
        //cout<<endl;
       for(j=0;j<np;j++)
       {
           if(mark[j]==1)mm[j]=1;
       }
   }

          for(j=0;j<np;j++)
       {
           if(mm[j]==1)k++;
       }
       printf("%d\n",k);
       return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    int n, m; cin>>n>>m;
    string arr[100] = {""}; char maxi[100] = {0};
    for(int i = 0 ; i < n ; ++i){
    cin>>arr[i];
    }
    for(int i = 0 ; i < arr[0].length() ; ++i){
        for(int j = 0 ; j < n ; ++j){
            maxi[i] = max(maxi[i], arr[j][i]);
        }
    }
    //cout<<maxi[0]<<maxi[1]<<maxi[2]<<maxi[3]<<maxi[4];
    int ans = 0;
    for(int i = 0 ; i < n ; ++i){
        int counter = 0;
        for(int k = 0 ; k < arr[i].length() ; ++k){
            if(arr[i][k] == maxi[k]){ans++; break;}
        }
    }cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, m, ans;
char a[150][150], mx[150];

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
        {
            cin >> a[i][j];
            mx[j] = max(mx[j], a[i][j]);
        }
    for (int i = 0; i < n; i++)
    {
        bool fl = 0;
        for (int j = 0; j < m; j++)
            if (a[i][j] == mx[j])
            {
                fl = 1;
                break;
            }
        ans += fl;
    }
    printf("%d", ans);
}


#include<iostream>
using namespace std;
char best(char **a,int i,int n)
{
    char max=a[0][i];
    for(int j=1;j<n;j++)
    if(max<a[j][i])
    max=a[j][i];
    return max;
}
int main()
{
    int n,m;
    char *x,**a;
    cin>>n>>m;
    int r=0;
    x=new char[m];
    a=new char*[n];
    for(int i=0;i<n;i++)
    {
        *(a+i)=new char[m+1];
        cin>>*(a+i);
    }
    for(int i=0;i<m;i++)
    x[i]=best(a,i,n);
    for(int j=0;j<n;j++)
    for(int i=0;i<m;i++)
    if(x[i]==a[j][i])
    {
        r++;
        break;
    }
    cout<<r;
    return 0;
}


#include<iostream>
using namespace std;
char best(char **a,int i,int n)
{
    char max=a[0][i];
    for(int j=1;j<n;j++)
    if(max<a[j][i])
    max=a[j][i];
    return max;
}
int main()
{
    int n,m;
    char *x,**a;
    cin>>n>>m;
    int r=0;
    x=new char[m];
    a=new char*[n];
    for(int i=0;i<n;i++)
    {
        *(a+i)=new char[m];
        cin>>*(a+i);
    }
    for(int i=0;i<m;i++)
    x[i]=best(a,i,n);
    for(int j=0;j<n;j++)
    for(int i=0;i<m;i++)
    if(x[i]==a[j][i])
    {
        r++;
        break;
    }
    cout<<r;
    return 0;
}


#include<iostream>
using namespace std;
int best(char **a,int i,int n)
{
    int max=a[0][i],k=0;
    for(int j=1;j<n;j++)
    {
        if(max<a[j][i])
        {
            max=a[j][i];
            k=j;
        }
        else if(max==a[j][i])
        k=-1;
    }
    return k;
}
int main()
{
    int n,m;
    char **a;
    cin>>n>>m;
    int *x,r=0;
    x=new int[m];
    a=new char*[n];
    for(int i=0;i<n;i++)
    {
        *(a+i)=new char[m];
        cin>>*(a+i);
    }
    for(int i=0;i<m;i++)
    x[i]=best(a,i,n);
    for(int i=0;i<m;i++)
    if(x[i]!=-1)
    {
        r++;
        for(int j=i+1;j<m;j++)
        if(x[i]==x[j])
        x[j]=-1;
    }
    cout<<r;
    return 0;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <list>
#include <bitset>
#include <algorithm>
#include <functional>
#include <cstring>
#include <numeric>
#include <iomanip>
#include <cmath>
#include <climits>
#include <cstring>
#include <cctype>

using namespace std;

typedef vector<int> vi;
typedef vector<vi > vvi;
typedef pair<int,int> ii;
typedef vector<ii > vii;
typedef vector<vii > vvii;
typedef set<ii > sii;
int mov[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1,};

#define pb push_back
#define mp(a, b) make_pair((a), (b))
#define all(c) (c).begin(),(c).end()
#define PI (acos(-1))
#define EPS 1e-7
#define INF 1000000000
#define MOD 1000000007
// #define CUS_DEBUG

#ifdef CUS_DEBUG
#include "debug.h"
#else
#define debug(a)
#define debug_array(a, n)
#define debug_array2(a, n, m)
#endif

template<typename T>
struct number_iterator : std::iterator<random_access_iterator_tag, T>{
    T v;
    number_iterator(T _v) : v(_v) {}
    operator T&(){return v;}
    T operator *() const {return v;}
};
template <typename T>
struct number_range {
    T b,e;
    number_range(T b, T e):b(b),e(e){}
    number_iterator<T> begin(){return b;}
    number_iterator<T> end(){return e;}
};
template<typename T> number_range<T> range(T e) {return number_range<T>(0, e);}
template<typename T> number_range<T> range(T b, T e) {return number_range<T>(b, e);}

int main()
{
    int n, m;
    cin >> n >> m;
    vvi v(n, vi(m));
    for (auto i: range(n)) {
        string line;
        cin >> line;
        for (auto j: range(m))
            v[i][j] = line[j] - '0';
    }
    debug(v);

    vi high(m, 0);
    for (auto i: range(n))
        for (auto j: range(m))
            high[j] = max(high[j], v[i][j]);
    debug(high);

    vi suc(n, 0);
    for (auto i: range(n))
        for (auto j: range(m))
            if (v[i][j] == high[j])
                suc[i] = 1;
    debug(suc);

    int total = 0;
    for (auto i: range(n))
        total += suc[i];
    cout << total << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <vector>

int suc[101];

using namespace std;

int main()
{
	int n, m;
	cin >> n >> m;

	vector<string> s(n);

	for (int i = 0; i<n; i++)
		cin>>s[i];

	for (int i = 0; i<n; i++)
		suc[i] = 0;

	char max_val;
	int ans = 0;
	for (int j = 0; j<m; j++)
	{
		max_val = '0';
		for (int i = 0; i<n; i++)
		{
			if (s[i][j]>max_val)
			{
				max_val = s[i][j];
			}
		}
		for (int i = 0; i<n; i++)
		{
			if (s[i][j] == max_val)
			{
				suc[i] = 1;
			}
		}
	}

	for (int i = 0; i<n; i++)
	{
		if (suc[i] == 1)
			ans++;
	}
	cout<<ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "rt", stdin);
#endif
    int n;
    int m;
    while (cin >> n >> m) {
        bool students[101] = { 0 };
        char marks[101][101];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                cin >> marks[i][j];
        for (int j = 0; j < m; j++) {
            char mx = '0';
            for (int i = 0; i < n; i++) {
                mx = max(mx, marks[i][j]);
            }
            for (int i = 0; i < n; i++) {
                if (mx == marks[i][j])
                    students[i] = true;
            }
        }
        cout << count(students, students + n, true) << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "rt", stdin);
#endif
    int n;
    int m;
    while (cin >> n >> m) {
        bool students[n] = { 0 };
        char marks[n][m];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                cin >> marks[i][j];
        for (int j = 0; j < m; j++) {
            char mx = '0';
            for (int i = 0; i < n; i++) {
                mx = max(mx, marks[i][j]);
            }
            for (int i = 0; i < n; i++) {
                if (mx == marks[i][j])
                    students[i] = true;
            }
        }
        cout << count(students, students + n, true) << endl;
    }
    return 0;
}


#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    vector<string> s(n);
    string b(m, 0);
    for (auto &e : s) {
        cin >> e;
        transform(begin(e), end(e), begin(b), begin(b),
                  [](char x, char y){ return max(x, y); });
    }
    cout << count_if(begin(s), end(s),
                     [b](string &e) {
                         return end(e) != mismatch(begin(e), end(e), begin(b),
                                                   not_equal_to<char>()).first;
                     });
}


#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <unordered_map>
#include <stdint.h>
#include <cmath>
#include <algorithm>

using namespace std;
typedef int64_t Int;
typedef uint64_t UInt;

int main(int argc, char **argv) {
    UInt n = 0;
    UInt m = 0;
    // Read the first line
    string line = "";
    if (getline(cin, line)) {
        stringstream ss(line);

        ss >> n >> m;
    }

    UInt intArr[n][m];
    UInt intArrRowIdx = 0;
    // Read the next n lines
    while (getline(cin, line)) {
        for (UInt i = 0; i != line.length(); ++i) {
            intArr[intArrRowIdx][i] = line[i] - '0';
        }

        ++intArrRowIdx;
        if (intArrRowIdx == n) break;
    }

    // Logic starts here
    bool sucStudentsArr[n];
    for (UInt i = 0; i != n; ++i) {
        sucStudentsArr[i] = false;
    }
    for (UInt i = 0; i != m; ++i) {
        UInt maxOfThisSubject = 0;
        for (UInt j = 0; j != n; ++j) {
            if (intArr[j][i] > maxOfThisSubject) {
                maxOfThisSubject = intArr[j][i];
            }
        }

        for (UInt j = 0; j != n; ++j) {
            if (intArr[j][i] == maxOfThisSubject) {
                sucStudentsArr[j] = true;
            }
        }
    }

    UInt sucStudentsCount = 0;
    for (UInt i = 0; i != n; ++i) {
        if (sucStudentsArr[i]) {
            ++sucStudentsCount;
        }
    }

    cout << sucStudentsCount;

    return 0;
}


#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;

    string s[n];
    for(int i = 0; i < n; ++i)
        cin >> s[i];

    char used[n];
    memset(used, 0, sizeof(used));
    for(int j = 0; j < m; ++j)
    {
        char c = '0';
        for(int i = 0; i < n; ++i)
        {
            if (c < s[i][j])
                c = s[i][j];
        }

        for(int i = 0; i < n; ++i)
        {
            if (c == s[i][j])
                used[i] = 1;
        }
    }

    int ans = 0;
    for(int i = 0; i < n; ++i)
        ans += used[i];
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int r, c;
char grid[105][105];

bool isBest(int i)
{
	char best, cmp;

	if(i == 0)
	{
		for(int j = 0; j < c; j++)
		{
			best = cmp = grid[i][j];

			for(int k = 1; k < r; k++)
				best = max(best, grid[k][j]);

			if(best == cmp)
				return true;
		}
		return false;
	}
	else if(i == r-1)
	{
		for(int j = 0; j < c; j++)
		{
			best = cmp = grid[i][j];

			for(int k = r-1; k >= 0; k--)
				best = max(best,grid[k][j]);

			if(best == cmp)
				return true;
		}
		return false;
	}
	else
	{
		for(int j = 0; j < c; j++)
		{
			best = cmp = grid[i][j];

			for(int k = i-1; k >= 0; k--)
				best = max(best,grid[k][j]);

			for(int k = i+1; k < r; k++)
				best = max(best,grid[k][j]);

			if(best == cmp)
				return true;
		}
		return false;
	}
	return false;
}

int main()
{
	int ans;

	while(cin >> r >> c)
	{
		ans = 0;

		for(int i = 0; i < r; i++)
			for(int j = 0; j < c; j++)
				cin >> grid[i][j];

		for(int i = 0; i < r; i++)
			if(isBest(i))
				ans++;

		cout << ans << endl;
	}
}


#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <vector>
#include <unordered_map>
#define ff(i,s,n) for(int i=(s);i<(n);i++)
#define FF(i,s,n) for(int i=(s);i<=(n);i++)
inline void swap(int &a, int &b){a=a^b;b=a^b;a=a^b;}
inline void smax(int &a, int b){if(b>a)a=b;}
inline void smin(int &a, int b){if(b<a)a=b;}

using namespace std;
typedef vector<pair<string,string> > vpss;typedef unordered_map<int,int> imap;
inline int sgd(int a, int b){if(a<b)swap(a,b);while(b>0){a-=a/b*b;swap(a,b);}return a;}

int n,m,o,u[100][100],v[100];
int main(){
    cin>>n>>m;
    ff(i,0,n){
        ff(j,0,m){
            char c;
            cin>>c;
            u[i][j]=c;
            smax(v[j],c);
        }
    }
    int co=0;
    ff(i,0,n){
        ff(j,0,m){
            if(u[i][j]==v[j]){co++;break;}
        }
    }
    cout<<co;
}


//152875TP
//solution by IHKIM
//WRONG ANSWER
#include<iostream>
#include<cmath>
#include<fstream>
#include<cstdio>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<math.h>
#include<time.h>
#include<queue>
#include<stack>
#include<deque>

//TEMPLATE STARTS HERE
using namespace std;

//constants
const int MAXN=(int)(1e5+1e1);
const double PI=acos(-1.0);

//defines
#define ll long long
#define F first
#define S second
#define fr freopen("input.txt","r",stdin);
#define fw freopen("output.txt","w",stdout);
#define mp make_pair
#define pb push_back
#define eof (-1)
#define forr(xx,yy,zz) for(int zz=xx;zz<=yy;zz++)
#define forl(xx,yy,zz) for(int zz=xx;zz>=yy;zz--)
#define sqr(x) ((x)*(x))
#define str string
#define sz(x) (int)x.size()
#define len(s) (int)(s.length())
#define all(a) a.begin(),a.end()
ll n,m,q;
char c[111][111];
set<int> ans;
int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>m;
	forr(1,n,i)
	{
		forr(1,m,j)
		{
			cin>>c[i][j];
		}
	}
	forr(1,n,i)
	{
		q=-1;
		forr(1,m,j)
		{
			if(c[i][j]>q)
				q=c[i][j];
		}
		forr(1,m,j)
			if(c[i][j]==q)
			{
					ans.insert(j);
			}
	}
	cout<<sz(ans);
	return 0;
}


//152875TP
//solution by IHKIM
//WRONG ANSWER
#include<iostream>
#include<cmath>
#include<fstream>
#include<cstdio>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<math.h>
#include<time.h>
#include<queue>
#include<stack>
#include<deque>

//TEMPLATE STARTS HERE
using namespace std;

//constants
const int MAXN=(int)(1e5+1e1);
const double PI=acos(-1.0);

//defines
#define ll long long
#define F first
#define S second
#define fr freopen("input.txt","r",stdin);
#define fw freopen("output.txt","w",stdout);
#define mp make_pair
#define pb push_back
#define eof (-1)
#define forr(xx,yy,zz) for(int zz=xx;zz<=yy;zz++)
#define forl(xx,yy,zz) for(int zz=xx;zz>=yy;zz--)
#define sqr(x) ((x)*(x))
#define str string
#define sz(x) (int)x.size()
#define len(s) (int)(s.length())
#define all(a) a.begin(),a.end()
ll n,m,q;
char c[111][111];
set<int> ans;
int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>m;
	forr(1,n,i)
	{
		forr(1,m,j)
		{
			cin>>c[i][j];
		}
	}
	forr(1,m,i)
	{
		q=-1;
		forr(1,n,j)
		{
			if(c[i][j]>q)
				q=c[i][j];
		}
		forr(1,n,j)
			if(c[i][j]==q)
			{
					ans.insert(j);
			}
	}
	cout<<sz(ans);
	return 0;
}


//152875TP
//solution by IHKIM
//WRONG ANSWER
#include<iostream>
#include<cmath>
#include<fstream>
#include<cstdio>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<math.h>
#include<time.h>
#include<queue>
#include<stack>
#include<deque>

//TEMPLATE STARTS HERE
using namespace std;

//constants
const int MAXN=(int)(1e5+1e1);
const double PI=acos(-1.0);

//defines
#define ll long long
#define F first
#define S second
#define fr freopen("input.txt","r",stdin);
#define fw freopen("output.txt","w",stdout);
#define mp make_pair
#define pb push_back
#define eof (-1)
#define forr(xx,yy,zz) for(int zz=xx;zz<=yy;zz++)
#define forl(xx,yy,zz) for(int zz=xx;zz>=yy;zz--)
#define sqr(x) ((x)*(x))
#define str string
#define sz(x) (int)x.size()
#define len(s) (int)(s.length())
#define all(a) a.begin(),a.end()
ll n,m,q;
char c[111][111];
set<int> ans;
int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>m;
	forr(1,n,i)
	{
		forr(1,m,j)
		{
			cin>>c[i][j];
		}
	}
	forr(1,m,i)
	{
		q=-1;
		forr(1,n,j)
		{
			if(c[j][i]>q)
				q=c[j][i];
		}
		forr(1,n,j)
			if(c[j][i]==q)
			{
					ans.insert(j);
			}
	}
	cout<<sz(ans);
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <string.h>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <math.h>
#include <fstream>
using namespace std;

int main()
{
	int students, subjects;
	cin >> students >> subjects;

	string* s = new string[students];
	char* maxi = new char[subjects];
	bool* success = new bool[students];

	for (int i = 0; i < subjects; i++)
		maxi[i] = 0;

	for (int i = 0; i < students; i++)
	{
		cin >> s[i];
		success[i] = false;
		for (int j = 0; j < subjects; j++)
			if (s[i][j] > maxi[j])
				maxi[j] = s[i][j];
	}

	for (int i = 0; i < subjects; i++)
		for (int j = 0; j < students; j++)
				if (s[j][i] == maxi[i])
				success[j] = true;

	int successes = 0;
	for (int i = 0; i < students; i++)
		if (success[i]) successes++;

	cout << successes << endl;
 //cout << endl; system("pause"); system("cls"); main();
}


#include <iostream>
using namespace std ;

int main ()
{
    int n,m;
    cin>>n>>m;
    char p[n+1][m+1] ;
    for (int i=0 ; i< n ; i++)
    {
        for (int j=0 ; j< m ; j++)
            cin>>p[i][j];
    }
    int counter=0;
    for (int i=0 ; i< n ; i++)
    {
        for (int j=0 ; j< m ; j++)
        {
            bool ok=true;
            for (int k = 0 ; k < n ; k++)
            {
                if (k!= i)
                {
                    if (p[i][j] < p[k][j])
                    {
                        ok=false;
                        break;
                    }
                }
            }
            if (ok)
            {
                counter++;
                break;
            }

        }
    }
    cout<<counter<<endl;

    return 0;
}


#include<iostream>
#include<set>
using namespace std;
int main()
{
    char arr[100][100];
    int n, m;
    while (cin >> n >> m)
    {
        for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> arr[i][j];

        set<int>s;
        for (int i = 0; i < m; i++)
        {
            char MAX = 47;
            int a,counter = 0;
            for (int j = 0; j < n; j++)
            {
                if (arr[j][i] >= MAX)
                {
                    counter++;
                    MAX = arr[j][i];
                    a = j;
                }
            }
            for (int k = 0; k < n; k++)
            {
                if (MAX == arr[k][i])
                s.insert(k);
            }

        }
        cout << s.size() << endl;
    }
}


#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <iterator>
#include <algorithm>
#include <string>
#include <ctime>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <sstream>
using namespace std;
#define INF 1000000000
#define MOD 1000000007
#define MAXN 10000000

int main()
{
    int n,m,cnt=0;
    cin >> n >> m;
    string s[100];
    for(int i=0;i<n;i++)
        cin >> s[i];
    bool isSuccessful[100];
    fill_n(isSuccessful,100,false);
    for(int j=0;j<m;j++){
        int mmax=0;
        for(int i=0;i<n;i++)
            mmax = max(mmax,s[i][j]-'0');
        for(int i=0;i<n;i++)
            if(mmax == s[i][j]-'0')
                isSuccessful[i] = true;
    }
    for(int i=0;i<n;i++)
        if(isSuccessful[i])
            cnt++;
    cout << cnt << endl;
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

#define all(t) t.begin(), t.end()
#define lla(t) t.rend(), t.rbegin()
#define forn(i, m) for (int i = 0, i##e = sz(m); i < i##e; ++i)
#define sz(v) (int(v.size()))
#define FOR(I, A ,B) for(int I = (A); I < (B); ++ I)
#define erbg(m,a,b) m.erase(m.begin()+a,m.begin()+b)
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;
typedef long long int ll;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector <vector <int>> vvi;
typedef vector<double> vd;
typedef stringstream ss;

int main() {
	int n, m, ans = 0;
	cin >> n >> m;
	vvi p(n, vi (m));
	forn(i, p){
		forn(j, p[i]){
			char a;
			cin >> a;
			p[i][j] = a - '0';
		}
	}
	rep(i, n){
		bool wasBest = false;
		rep(j, m){
			bool isBest = true;
			rep(k, n){
				if (p[k][j] > p[i][j])
					isBest = false;
			}
			if (isBest)
				wasBest = true;
		}
		if (wasBest)
			++ans;
	}
	cout << ans;

}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <sstream>
#include <cstdio>

using namespace std;
#define SZ(X) (int)(X).size()
#define ALL(X) (X).begin(),(X).end()
#define ALLR(X) (X).rbegin(),(X).rend()
typedef vector<int> vi;

int main(){
    int n,m;
    cin>>n>>m;
    int **mat=new int*[n];
    for(int i=0;i<n;i++){
        mat[i]=new int[m];
    }
    char temp;
    set<int> se;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>temp;
            mat[i][j]=temp-'0';
        }
    }
    int max;
    for(int i=0;i<m;i++){
        max=mat[0][i];
        vi indices;
        for(int j=0;j<n;j++){
            if(mat[j][i]==max){
                indices.push_back(j);
            }
            if(mat[j][i]>max){
                indices.clear();
                max=mat[j][i];
                indices.push_back(j);
            }
        }
        se.insert(ALL(indices));
    }
    cout<<se.size()<<endl;
    return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
int main ()
{
    int a,b;
    cin>>a>>b;
    vector<string> x;
    x.assign(a,"0");
    for(int i=0;i<a;i++)
    {
        cin>>x[i];
        //cout<<x[i].at(i);
    }
    int res1=0 , fres=0 , eq=0;
    for(int u=0;u<a;u++)
    {
        eq=0;
        for(int k=0;k<b;k++)
        {
            eq=0;
            string s="";
            int com=0;
            s=x[u].at(k);
            stringstream ss;
            ss<<s;
            ss>>com;
            for(int i=0;i<a;i++)
            {
                string m="";
                int test=0;
                m=x[i].at(k);
                stringstream st;
                st<<m;
                st>>test;
                if(com>=test)
                    eq++;
            }
            if(eq==a)
            {
                fres+=1;
                break;
            }
        }
    }
    cout<<fres<<endl;
    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
int main ()
{
    int a,b;
    cin>>a>>b;
    vector<int> x ,v,ba;
    x.assign(a,0);
    v.assign(a,0);
    ba.assign(a,0);
    for(int i=0;i<a;i++)
    {
        cin>>x[i];
        v[i]=x[i];
        ba[i]=x[i];
    }
    int res1=0,fres=0,eq=0;
    for(int u=0;u<a;u++)
    {
        res1=0;

        for(int i=0;i<b;i++)
        {
            eq=0;
            int com;
            com=v[u]%10;
            v[u]/=10;
            for(int k=0;k<a;k++)
            {
                int test;
                test=x[k]%10;
                x[k]/=10;
                if(com>test)
                {
                    res1=1;
                    break;
                }
                else if (com==test)
                    eq+=1;
            }
            fres+=res1;
            if(res1==1)
                    break;
            else if (eq==a)
            {
                fres+=1;
                break;
            }

        }
        for(int i=0;i<a;i++)
        {

            x[i]=ba[i];
            v[i]=ba[i];
        }
    }
    cout<<fres<<endl;
    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
int main ()
{
    int a,b;
    cin>>a>>b;
    vector<int> x ,v,ba;
    x.assign(a,0);
    v.assign(a,0);
    ba.assign(a,0);
    for(int i=0;i<a;i++)
    {
        cin>>x[i];
        v[i]=x[i];
        ba[i]=x[i];
    }
    int res1=0,fres=1,eq=0;
    for(int u=0;u<a;u++)
    {
        res1=0;

        for(int i=0;i<b;i++)
        {
            int com;
            com=v[u]%10;
            v[u]/=10;
            for(int k=0;k<a;k++)
            {
                int test;
                test=x[k]%10;
                x[k]/=10;
                if(com>test)
                {
                    res1=1;
                    break;
                }
                 else if(com==test)
                {
                    eq+=1;
                }

            }
            fres+=res1;
            if(res1==1)
                    break;
            if(eq==a)
            {
                res1=1;
                break;
            }
        }
        for(int i=0;i<a;i++)
        {

            x[i]=ba[i];
            v[i]=ba[i];
        }
    }
    cout<<fres<<endl;
    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
int main ()
{
    int a,b;
    cin>>a>>b;
    vector<int> x ,v,ba;
    x.assign(a,0);
    v.assign(a,0);
    ba.assign(a,0);
    for(int i=0;i<a;i++)
    {
        cin>>x[i];
        v[i]=x[i];
        ba[i]=x[i];
    }
    int res1=0,fres=0,eq=0;
    for(int u=0;u<a;u++)
    {
        res1=0;

        for(int i=0;i<b;i++)
        {
            eq=0;
            int com;
            com=v[u]%10;
            v[u]/=10;
            for(int k=0;k<a;k++)
            {
                int test;
                test=x[k]%10;
                x[k]/=10;
                if(com>=test)
                {
                    eq+=1;

                }
            }
            if (eq==a)
            {
                fres+=1;
                break;

            }

        }
        for(int i=0;i<a;i++)
        {

            x[i]=ba[i];
            v[i]=ba[i];
        }
    }
    cout<<fres<<endl;
    return 0;
}


#include <algorithm>

#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>

using namespace std;

#define SZ(X) (int)(X).size()
#define ALL(X) (X).begin(),(X).end()
#define ALLR(X) (X).rbegin(),(X).rend()

int sub_mark[102];
int mat[102][102];
int main()
{

    int st,sub;
    cin>>st>>sub;
    for(int i=0;i<sub;++i)
        sub_mark[i] = -1;
    for(int i=0;i<st;++i)
        for(int j=0;j<sub;++j)
        {
            char x;
            cin>>x;
            mat[i][j] = x-'0';
            sub_mark[j] = max(sub_mark[j],mat[i][j]);
            cout<<sub_mark[j]<<endl;
        }
    int c=0;
    for(int i=0;i<st;++i)
    {
        int suc =0;
        for(int j=0;j<sub;++j)
            if(mat[i][j]==sub_mark[j])
          //cout<<mat[i][j];
                suc++;
               // cout<<"suc"<<suc<<endl;
               if(suc>0)
               {  c+=1;
               }

       // cout<<"c"<<c<<endl;
    }
    cout<<c<<endl;
}


#include <algorithm>

#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>

using namespace std;

#define SZ(X) (int)(X).size()
#define ALL(X) (X).begin(),(X).end()
#define ALLR(X) (X).rbegin(),(X).rend()

int sub_mark[102];
int mat[102][102];
int main()
{

    int st,sub;
    cin>>st>>sub;
    for(int i=0;i<sub;++i)
        sub_mark[i] = -1;
    for(int i=0;i<st;++i)
        for(int j=0;j<sub;++j)
        {
            char x;
            cin>>x;
            mat[i][j] = x;
            sub_mark[j] = max(sub_mark[j],mat[i][j]);

        }
    int c=0;
    for(int i=0;i<st;++i)
    {
        int suc =0;
        for(int j=0;j<sub;++j)
            if(mat[i][j]==sub_mark[j])
          //cout<<mat[i][j];
                suc++;
        c+=(suc>0);
    }
    cout<<c<<endl;
}


#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;
int n, m, ans, best[400];
char a[400][400];

 main() {
    cin>>n>>m;
    for (int i = 0; i < n; i++) {
        cin>>a[i];
    }

    for (int j = 0; j < m; j++) {
        char bchar = 0;
        for (int i = 0; i < n; i++)
            if (bchar < a[i][j])
                bchar = a[i][j];
        for (int i = 0; i < n; i++)
            if (bchar == a[i][j])
                best[i] = 1;
    }

    for (int i = 0; i < n; i++) {
        ans+=best[i];
    }
    cout<<ans<<endl;

    return 0;
}


#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#includ<stdio.h>
using namespace std;
int n, m, ans, best[400];
char a[400][400];

 main() {
    cin>>n>>m;
    for (int i = 0; i < n; i++) {
        cin>>a[i];
    }

    for (int j = 0; j < m; j++) {
        char bchar = 0;
        for (int i = 0; i < n; i++)
            if (bchar < a[i][j])
                bchar = a[i][j];
        for (int i = 0; i < n; i++)
            if (bchar == a[i][j])
                best[i] = 1;
    }

    for (int i = 0; i < n; i++) {
        ans+=best[i];
    }
    cout<<ans<<endl;

    return 0;
}


#include <algorithm>

#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>

using namespace std;

#define SZ(X) (int)(X).size()
#define ALL(X) (X).begin(),(X).end()
#define ALLR(X) (X).rbegin(),(X).rend()

int sub_mark[102];
int mat[102][102];
int main()
{

    int st,sub;
    cin>>st>>sub;
    for(int i=0;i<sub;++i)
        sub_mark[i] = -1;
    for(int i=0;i<st;++i)
        for(int j=0;j<sub;++j)
        {
            char x;
            cin>>x;
            mat[i][j] = x-'0';
            sub_mark[j] = max(sub_mark[j],mat[i][j]);
          //  cout<<sub_mark[j]<<endl;
        }
    int c=0;
    for(int i=0;i<st;++i)
    {
        int suc =0;
        for(int j=0;j<sub;++j)
            if(mat[i][j]==sub_mark[j])
          //cout<<mat[i][j];
                suc++;
               // cout<<"suc"<<suc<<endl;
               if(suc>0)
               {  c+=1;
               }

       // cout<<"c"<<c<<endl;
    }
    cout<<c<<endl;
}


#include <algorithm>

#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>

using namespace std;

#define SZ(X) (int)(X).size()
#define ALL(X) (X).begin(),(X).end()
#define ALLR(X) (X).rbegin(),(X).rend()

int sub_mark[102];
int mat[102][102];
int main()
{

    int st,sub;
    cin>>st>>sub;
    for(int i=0;i<sub;++i)
        sub_mark[i] = -1;
    for(int i=0;i<st;++i)
        for(int j=0;j<sub;++j)
        {
            char x;
            cin>>x;
            mat[i][j] = x;
            sub_mark[j] = max(sub_mark[j],mat[i][j]);

        }
    int c=0;
    for(int i=0;i<sub;++i)
    {
        int suc =0;
        for(int j=0;j<st;++j)
            if(mat[i][j]==sub_mark[j])
          //cout<<mat[i][j];
                suc++;
        c+=(suc>0);
    }
    cout<<c<<endl;
}


#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#includ<stdio.h>
using namespace std;
int n, m, ans, best[400];
char a[400][400];

 main() {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) {
        scanf("%s", a[i]);
    }

    for (int j = 0; j < m; j++) {
        char bchar = 0;
        for (int i = 0; i < n; i++)
            if (bchar < a[i][j])
                bchar = a[i][j];
        for (int i = 0; i < n; i++)
            if (bchar == a[i][j])
                best[i] = 1;
    }

    for (int i = 0; i < n; i++) {
        ans+=best[i];
    }
    printf("%d\n", ans);

    return 0;
}