1 /**************************************************************
2 Problem: 3439
3 User: Tunix
4 Language: C++
5 Result: Accepted
6 Time:1240 ms
7 Memory:51688 kb
8 ****************************************************************/
9
10 //BZOJ 3439
11 #include<vector>
12 #include<cstdio>
13 #include<cstring>
14 #include<cstdlib>
15 #include<iostream>
16 #include<algorithm>
17 #define rep(i,n) for(int i=0;i<n;++i)
18 #define F(i,j,n) for(int i=j;i<=n;++i)
19 #define D(i,j,n) for(int i=j;i>=n;--i)
20 #define pb push_back
21 using namespace std;
22 inline int getint(){
23 int v=0,sign=1; char ch=getchar();
24 while(ch<‘0‘||ch>‘9‘){ if (ch==‘-‘) sign=-1; ch=getchar();}
25 while(ch>=‘0‘&&ch<=‘9‘){ v=v*10+ch-‘0‘; ch=getchar();}
26 return v*sign;
27 }
28 const int N=1e5+10,INF=~0u>>2;
29 typedef long long LL;
30 /******************tamplate*********************/
31 struct Trie{
32 int ch[26],num;
33 bool sign;
34 }T[N];
35 int cnt=0,n,tot,pos[N];
36 inline int id(char c){return c-‘a‘;}
37 vector<int>v[N];
38 void Insert(char *s,int num){
39 int x=0,y;
40 D(i,strlen(s)-1,0){
41 y=id(s[i]);
42 if (T[x].ch[y]==0)
43 T[x].ch[y]=++cnt;
44 x=T[x].ch[y];
45 }
46 T[x].sign=1;
47 v[x].pb(num);
48 pos[num]=x;
49 }
50 int st[N],ed[N],a[N];
51 void dfs(int x){
52 st[x]=tot+1;
53 rep(i,v[x].size()) a[++tot]=v[x][i];
54 rep(i,26)
55 if (T[x].ch[i]!=0)
56 dfs(T[x].ch[i]);
57 ed[x]=tot;
58 }
59 struct Tree{
60 int cnt,l,r;
61 }t[N*30];
62 int root[N],num=0;
63 #define mid (l+r>>1)
64 void update(int &o,int l,int r,int pos){
65 t[++num]=t[o], o=num, ++t[o].cnt;
66 if (l==r) return;
67 if (pos<=mid) update(t[o].l,l,mid,pos);
68 else update(t[o].r,mid+1,r,pos);
69 }
70 int query(int i,int j,int rank){
71 i=root[i]; j=root[j];
72 int l=1,r=n;
73 while(l!=r){
74 if (t[t[j].l].cnt-t[t[i].l].cnt>=rank)
75 r=mid,i=t[i].l,j=t[j].l;
76 else{
77 rank-=t[t[j].l].cnt-t[t[i].l].cnt;
78 l=mid+1,i=t[i].r,j=t[j].r;
79 }
80 }
81 if (t[j].cnt-t[i].cnt<rank) return -1;
82 return l;
83 }
84 #undef mid
85 char s[N];
86 int main(){
87 #ifndef ONLINE_JUDGE
88 freopen("3439.in","r",stdin);
89 freopen("3439.out","w",stdout);
90 #endif
91 n=getint();
92 F(i,1,n){
93 scanf("%s",s);
94 Insert(s,i);
95 }
96 dfs(0);
97 #ifdef debug
98 F(i,1,tot) printf("%d ",a[i]);puts("");
99 F(i,1,n) printf("%d %d\n",st[pos[i]],ed[pos[i]]);
100 #endif
101 F(i,1,tot){
102 root[i]=root[i-1];
103 update(root[i],1,n,a[i]);
104 }
105 #ifdef debug
106 F(i,1,num) printf("%d %d %d %d\n",i,t[i].l,t[i].r,t[i].cnt);
107 #endif
108 int k;
109 F(i,1,n){
110 k=getint();
111 int ans=query(st[pos[i]]-1,ed[pos[i]],k);
112 // printf("%d\n",ans>n ? -1 : ans);
113 printf("%d\n",ans);
114 }
115 return 0;
116 }