// MADE BY QT666
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=500050;
int n,trie[N][26],tt,sz,rt[N*20],ls[N*20],rs[N*20],sum[N*20],val[N],ed[N];
char s[N];
void ins(int &x,int l,int r,int v){
if(!x) x=++sz;
if(l==r){sum[x]++;return;}
int mid=(l+r)>>1;
if(v<=mid) ins(ls[x],l,mid,v);
else ins(rs[x],mid+1,r,v);
sum[x]=sum[ls[x]]+sum[rs[x]];
}
void insert(int id){
int len=strlen(s+1);int x=0;
for(int i=len;i;i--){
if(!trie[x][s[i]-‘a‘]) trie[x][s[i]-‘a‘]=++tt;
x=trie[x][s[i]-‘a‘];ins(rt[x],1,n,id);
}
ed[id]=x;
}
int query(int x,int l,int r,int k){
if(l==r){return l;}
int mid=(l+r)>>1;
if(sum[ls[x]]>=k) return query(ls[x],l,mid,k);
else return query(rs[x],mid+1,r,k-sum[ls[x]]);
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){scanf("%s",s+1);insert(i);}
for(int i=1;i<=n;i++){
int k;scanf("%d",&k);
if(sum[rt[ed[i]]]<k) puts("-1");
else printf("%d\n",query(rt[ed[i]],1,n,k));
}
return 0;
}