import java.util.*;
import java.io.*;
import java.text.*;
//Solution Credits: Taranpreet Singh
public class Main{
//SOLUTION BEGIN
int n, q, m = 1, cnt, MX = (int)1e6+1;
int[] root;
int[] t, le, ri;
int[] maxPref, tt;
int[][] bit;
n = ni(); q = ni();
int[] count = new int[MX];
for(int i = 1; i<= n; i++){
a[i] = n();b[i] = i;
}
if(a
[i1
].
length()!=a
[i2
].
length())return Integer.
compare(a
[i1
].
length(), a
[i2
].
length()); return a[i1].compareTo(a[i2]);
});
for(int i = 1; i<= n; i++)
for(int j = 0; j< a[b[i]].length(); j++)
if(a[b[i]].charAt(j)=='1')
count[a[b[i]].length()-j-1]++;
bit = new int[MX][];
for(int i = 0; i< MX; i++){bit[i] = new int[count[i]];count[i] = 0;}
for(int i = 1; i<= n; i++)
for(int j= 0; j< a[b[i]].length(); j++)
if(a[b[i]].charAt(j)=='1')
bit[a[b[i]].length()-j-1][count[a[b[i]].length()-j-1]++] = i;
buildt();
buildtt();
while(q-->0){
int l = ni(), r = ni();
x = new StringBuilder(n()).reverse().toString();
int ll = 1, rr = n, len = x.length();
while(ll<rr-1){
int mid = (ll+rr)>>1;
if(a[b[mid]].length()>len)rr = mid;
else ll = mid;
}
if(a[b[ll]].length()>len)rr=ll;
if(a[b[rr]].length()<=len || !check(rr, n, l,r)){
if(a[b[rr]].length()>len)rr--;
pn(solve(1,rr,l,r,len-1));
}else{
int z = get_last_pos(1,m,root[l],r);
int zz = get_pos_second(1,m,1,z,1,a[b[z]].length()-len);
pn(solve(zz,z,l,r,len-1));
}
}
}
void buildt(){
while(m<n)m<<=1;
cnt = 1+(m<<1);
t = new int[MX*6];le = new int[MX*6];ri = new int[MX*6];
root = new int[n+2];root[n+1] = 1;
for(int i = 1; i< m; i++){le[i] = i<<1;ri[i] = i<<1|1;}
for(int i = 1; i<= m<<1; i++)t[i] = INF;
int[] pos = new int[n+1];
for(int i = 1; i<= n; i++)pos[b[i]] = i;
for(int i = n; i>=1; i--){
root[i] = createCopy(root[i+1]);
update(1, m, pos[i], root[i], i);
}
}
void update(int l, int r, int pos, int node, int z){
if(l==r)t[node] = z;
else{
int mid = (l+r)/2;
if(pos<=mid){
le[node] = createCopy(le[node]);
update(l, mid, pos, le[node], z);
}else{
ri[node] = createCopy(ri[node]);
update(mid+1,r,pos,ri[node], z);
}
t
[node
] = Math.
min(t
[le
[node
]], t
[ri
[node
]]); }
}
int get(int l, int r, int ll, int rr, int node){
if(l==ll && r==rr)return t[node];
int mid = (ll+rr)/2;
if(r<=mid)return get(l,r,ll,mid,le[node]);
else if(l>mid)return get(l,r,mid+1,rr,ri[node]);
else return Math.
min(get
(l,mid,ll,mid,le
[node
]), get
(mid
+1,r,mid
+1,rr,ri
[node
])); }
boolean check(int l, int r, int ll, int rr){
return get(l,r,1,m,root[ll])<=rr;
}
int get_last_pos(int l, int r, int node, int rr){
if(l==r)return l;
int mid = (l+r)/2;
if(t[ri[node]]<=rr)return get_last_pos(mid+1,r,ri[node],rr);
return get_last_pos(l,mid,le[node], rr);
}
int get_pos(int l, int r, int x){
if(bit[x].length==0)return r+1;
int ll = 0, rr = bit[x].length-1;
while(ll<rr-1){
int mid = (ll+rr)>>1;
if(bit[x][mid]>=l)rr=mid;
else ll = mid;
}
if(bit[x][ll]>=l)rr=ll;
if(bit[x][rr]<l)return r+1;
return bit[x][rr];
}
int solve(int l, int r, int ll, int rr, int bit){
if(bit==-1)return get(l,r,1,m,root[ll]);
int pos = get_pos(l,r,bit);
pos
= Math.
min(pos, r
+1); if(x.charAt(bit)=='1'){
if(pos>l && check(l,pos-1, ll,rr))return solve(l,pos-1,ll,rr,bit-1);
else return solve(pos, r, ll, rr, bit-1);
}else{
if(pos<=r && check(pos,r,ll,rr))return solve(pos,r,ll,rr,bit-1);
else return solve(l,pos-1,ll,rr,bit-1);
}
}
int get_pos_second(int l, int r, int ll, int rr, int i, int y){
if (l>r || ll>r || l>rr || ll>rr) return 0;
if(l>=ll && r<=rr && tt[i]>=y)return 0;
if(l==r)return l;
int mid = (l+r)/2;
int x = get_pos_second(mid+1,r,ll,rr,i<<1|1, y);
if(x>0)return x;
return get_pos_second(l,mid,ll,rr,i<<1,y);
}
void buildtt(){
maxPref = new int[m<<1];
for(int i = 1; i<= n; i++){
int x = b[i-1], y = b[i];
if(a[x].length()==a[y].length())while(maxPref[i]<a[x].length() && a[x].charAt(maxPref[i]) == a[y].charAt(maxPref[i]))maxPref[i]++;
}
tt = new int[m<<1];
for(int i = 1; i< m<<1; i++)tt[i] = INF;
for(int i = m; i< m+n; i++)tt[i] = maxPref[i-m+1];
for(int i
= m
-1; i
>0; i
--)tt
[i
] = Math.
min(tt
[i
<<1], tt
[i
<<1|1]); }
int createCopy(int x){
t[cnt] = t[x];
le[cnt] = le[x];
ri[cnt] = ri[x];
return cnt++;
}
//SOLUTION END
long mod = (long)1e9+7, IINF = (long)5e18;
final int INF = (int)2e9;
double PI = 3.1415926535897932384626433832792884197169399375105820974944, eps = 1e-8;
static boolean multipleTC = false, memory = false;
in = new FastReader();
int T = (multipleTC)?ni():1;
//Solution Credits: Taranpreet Singh
for(int i = 1; i<= T; i++)solve(i);
out.flush();
out.close();
}
if(memory
)new Thread(null,
new Runnable() {public void run
(){try{new Main
().
run();}catch(Exception e
){e.
printStackTrace();}}},
"1",
1 << 28).
start(); else new Main().run();
}
long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);}
int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}
int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));}
void p
(Object o
){out.
print(o
);} void pn
(Object o
){out.
println(o
);} void pni
(Object o
){out.
println(o
);out.
flush();} String nln
(){return in.
nextLine();} int ni
(){return Integer.
parseInt(in.
next());} long nl
(){return Long.
parseLong(in.
next());} double nd
(){return Double.
parseDouble(in.
next());}
class FastReader{
public FastReader(){
}
}
while (st == null || !st.hasMoreElements()){
try{
e.printStackTrace();
}
}
return st.nextToken();
}
try{
str = br.readLine();
e.printStackTrace();
}
return str;
}
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5pby4qOyAKaW1wb3J0IGphdmEudGV4dC4qOwovL1NvbHV0aW9uIENyZWRpdHM6IFRhcmFucHJlZXQgU2luZ2gKcHVibGljIGNsYXNzIE1haW57CiAgICAvL1NPTFVUSU9OIEJFR0lOCiAgICBpbnQgbiwgcSwgbSA9IDEsIGNudCwgTVggPSAoaW50KTFlNisxOwogICAgU3RyaW5nW10gYTsKICAgIEludGVnZXJbXSBiOwogICAgaW50W10gcm9vdDsKICAgIGludFtdIHQsIGxlLCByaTsKICAgIGludFtdIG1heFByZWYsIHR0OwogICAgaW50W11bXSBiaXQ7CiAgICBTdHJpbmcgeDsKICAgIHZvaWQgc29sdmUoaW50IFRDKSB0aHJvd3MgRXhjZXB0aW9uewogICAgICAgIG4gID0gbmkoKTsgcSA9IG5pKCk7CiAgICAgICAgYSA9IG5ldyBTdHJpbmdbbisxXTtiID0gbmV3IEludGVnZXJbMStuXTtiWzBdID0gMDthWzBdID0gIiI7CiAgICAgICAgaW50W10gY291bnQgPSBuZXcgaW50W01YXTsKICAgICAgICBmb3IoaW50IGkgPSAxOyBpPD0gbjsgaSsrKXsKICAgICAgICAgICAgYVtpXSA9IG4oKTtiW2ldID0gaTsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgQXJyYXlzLnNvcnQoYiwgMSwgbisxLCAoSW50ZWdlciBpMSwgSW50ZWdlciBpMikgLT57CiAgICAgICAgICAgIGlmKGFbaTFdLmxlbmd0aCgpIT1hW2kyXS5sZW5ndGgoKSlyZXR1cm4gSW50ZWdlci5jb21wYXJlKGFbaTFdLmxlbmd0aCgpLCBhW2kyXS5sZW5ndGgoKSk7CiAgICAgICAgICAgIHJldHVybiBhW2kxXS5jb21wYXJlVG8oYVtpMl0pOwogICAgICAgIH0pOwogICAgICAgIGZvcihpbnQgaSA9IDE7IGk8PSBuOyBpKyspCiAgICAgICAgICAgIGZvcihpbnQgaiA9IDA7IGo8IGFbYltpXV0ubGVuZ3RoKCk7IGorKykKICAgICAgICAgICAgICAgIGlmKGFbYltpXV0uY2hhckF0KGopPT0nMScpCiAgICAgICAgICAgICAgICAgICAgY291bnRbYVtiW2ldXS5sZW5ndGgoKS1qLTFdKys7CiAgICAgICAgYml0ID0gbmV3IGludFtNWF1bXTsKICAgICAgICBmb3IoaW50IGkgPSAwOyBpPCBNWDsgaSsrKXtiaXRbaV0gPSBuZXcgaW50W2NvdW50W2ldXTtjb3VudFtpXSA9IDA7fQogICAgICAgIGZvcihpbnQgaSA9IDE7IGk8PSBuOyBpKyspCiAgICAgICAgICAgIGZvcihpbnQgaj0gMDsgajwgYVtiW2ldXS5sZW5ndGgoKTsgaisrKQogICAgICAgICAgICAgICAgaWYoYVtiW2ldXS5jaGFyQXQoaik9PScxJykKICAgICAgICAgICAgICAgICAgICBiaXRbYVtiW2ldXS5sZW5ndGgoKS1qLTFdW2NvdW50W2FbYltpXV0ubGVuZ3RoKCktai0xXSsrXSA9IGk7CiAgICAgICAgYnVpbGR0KCk7CiAgICAgICAgYnVpbGR0dCgpOwogICAgICAgIHdoaWxlKHEtLT4wKXsKICAgICAgICAgICAgaW50IGwgPSBuaSgpLCByID0gbmkoKTsKICAgICAgICAgICAgeCA9IG5ldyBTdHJpbmdCdWlsZGVyKG4oKSkucmV2ZXJzZSgpLnRvU3RyaW5nKCk7CiAgICAgICAgICAgIGludCBsbCA9IDEsIHJyID0gbiwgbGVuID0geC5sZW5ndGgoKTsKICAgICAgICAgICAgd2hpbGUobGw8cnItMSl7CiAgICAgICAgICAgICAgICBpbnQgbWlkID0gKGxsK3JyKT4+MTsKICAgICAgICAgICAgICAgIGlmKGFbYlttaWRdXS5sZW5ndGgoKT5sZW4pcnIgPSBtaWQ7CiAgICAgICAgICAgICAgICBlbHNlIGxsID0gbWlkOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmKGFbYltsbF1dLmxlbmd0aCgpPmxlbilycj1sbDsKICAgICAgICAgICAgaWYoYVtiW3JyXV0ubGVuZ3RoKCk8PWxlbiB8fCAhY2hlY2socnIsIG4sIGwscikpewogICAgICAgICAgICAgICAgaWYoYVtiW3JyXV0ubGVuZ3RoKCk+bGVuKXJyLS07CiAgICAgICAgICAgICAgICBwbihzb2x2ZSgxLHJyLGwscixsZW4tMSkpOwogICAgICAgICAgICB9ZWxzZXsKICAgICAgICAgICAgICAgIGludCB6ID0gZ2V0X2xhc3RfcG9zKDEsbSxyb290W2xdLHIpOwogICAgICAgICAgICAgICAgaW50IHp6ID0gZ2V0X3Bvc19zZWNvbmQoMSxtLDEseiwxLGFbYlt6XV0ubGVuZ3RoKCktbGVuKTsKICAgICAgICAgICAgICAgIHBuKHNvbHZlKHp6LHosbCxyLGxlbi0xKSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICB2b2lkIGJ1aWxkdCgpewogICAgICAgIHdoaWxlKG08biltPDw9MTsKICAgICAgICBjbnQgPSAxKyhtPDwxKTsKICAgICAgICB0ID0gbmV3IGludFtNWCo2XTtsZSA9IG5ldyBpbnRbTVgqNl07cmkgPSBuZXcgaW50W01YKjZdOwogICAgICAgIHJvb3QgPSBuZXcgaW50W24rMl07cm9vdFtuKzFdID0gMTsKICAgICAgICBmb3IoaW50IGkgPSAxOyBpPCBtOyBpKyspe2xlW2ldID0gaTw8MTtyaVtpXSA9IGk8PDF8MTt9CiAgICAgICAgZm9yKGludCBpID0gMTsgaTw9IG08PDE7IGkrKyl0W2ldID0gSU5GOwogICAgICAgIGludFtdIHBvcyA9IG5ldyBpbnRbbisxXTsKICAgICAgICBmb3IoaW50IGkgPSAxOyBpPD0gbjsgaSsrKXBvc1tiW2ldXSA9IGk7CiAgICAgICAgZm9yKGludCBpID0gbjsgaT49MTsgaS0tKXsKICAgICAgICAgICAgcm9vdFtpXSA9IGNyZWF0ZUNvcHkocm9vdFtpKzFdKTsKICAgICAgICAgICAgdXBkYXRlKDEsIG0sIHBvc1tpXSwgcm9vdFtpXSwgaSk7CiAgICAgICAgfQogICAgfQogICAgdm9pZCB1cGRhdGUoaW50IGwsIGludCByLCBpbnQgcG9zLCBpbnQgbm9kZSwgaW50IHopewogICAgICAgIGlmKGw9PXIpdFtub2RlXSA9IHo7CiAgICAgICAgZWxzZXsKICAgICAgICAgICAgaW50IG1pZCA9IChsK3IpLzI7CiAgICAgICAgICAgIGlmKHBvczw9bWlkKXsKICAgICAgICAgICAgICAgIGxlW25vZGVdID0gY3JlYXRlQ29weShsZVtub2RlXSk7CiAgICAgICAgICAgICAgICB1cGRhdGUobCwgbWlkLCBwb3MsIGxlW25vZGVdLCB6KTsKICAgICAgICAgICAgfWVsc2V7CiAgICAgICAgICAgICAgICByaVtub2RlXSA9IGNyZWF0ZUNvcHkocmlbbm9kZV0pOwogICAgICAgICAgICAgICAgdXBkYXRlKG1pZCsxLHIscG9zLHJpW25vZGVdLCB6KTsKICAgICAgICAgICAgfQogICAgICAgICAgICB0W25vZGVdID0gTWF0aC5taW4odFtsZVtub2RlXV0sIHRbcmlbbm9kZV1dKTsKICAgICAgICB9CiAgICB9CiAgICAKICAgIGludCBnZXQoaW50IGwsIGludCByLCBpbnQgbGwsIGludCByciwgaW50IG5vZGUpewogICAgICAgIGlmKGw9PWxsICYmIHI9PXJyKXJldHVybiB0W25vZGVdOwogICAgICAgIGludCBtaWQgPSAobGwrcnIpLzI7CiAgICAgICAgaWYocjw9bWlkKXJldHVybiBnZXQobCxyLGxsLG1pZCxsZVtub2RlXSk7CiAgICAgICAgZWxzZSBpZihsPm1pZClyZXR1cm4gZ2V0KGwscixtaWQrMSxycixyaVtub2RlXSk7CiAgICAgICAgZWxzZSByZXR1cm4gTWF0aC5taW4oZ2V0KGwsbWlkLGxsLG1pZCxsZVtub2RlXSksIGdldChtaWQrMSxyLG1pZCsxLHJyLHJpW25vZGVdKSk7CiAgICB9CiAgICAKICAgIGJvb2xlYW4gY2hlY2soaW50IGwsIGludCByLCBpbnQgbGwsIGludCBycil7CiAgICAgICAgcmV0dXJuIGdldChsLHIsMSxtLHJvb3RbbGxdKTw9cnI7CiAgICB9CgogICAgaW50IGdldF9sYXN0X3BvcyhpbnQgbCwgaW50IHIsIGludCBub2RlLCBpbnQgcnIpewogICAgICAgIGlmKGw9PXIpcmV0dXJuIGw7CiAgICAgICAgaW50IG1pZCA9IChsK3IpLzI7CiAgICAgICAgaWYodFtyaVtub2RlXV08PXJyKXJldHVybiBnZXRfbGFzdF9wb3MobWlkKzEscixyaVtub2RlXSxycik7CiAgICAgICAgcmV0dXJuIGdldF9sYXN0X3BvcyhsLG1pZCxsZVtub2RlXSwgcnIpOwogICAgfQogICAgCiAgICBpbnQgZ2V0X3BvcyhpbnQgbCwgaW50IHIsIGludCB4KXsKICAgICAgICBpZihiaXRbeF0ubGVuZ3RoPT0wKXJldHVybiByKzE7CiAgICAgICAgaW50IGxsID0gMCwgcnIgPSBiaXRbeF0ubGVuZ3RoLTE7CiAgICAgICAgd2hpbGUobGw8cnItMSl7CiAgICAgICAgICAgIGludCBtaWQgPSAobGwrcnIpPj4xOwogICAgICAgICAgICBpZihiaXRbeF1bbWlkXT49bClycj1taWQ7CiAgICAgICAgICAgIGVsc2UgbGwgPSBtaWQ7CiAgICAgICAgfQogICAgICAgIGlmKGJpdFt4XVtsbF0+PWwpcnI9bGw7CiAgICAgICAgaWYoYml0W3hdW3JyXTxsKXJldHVybiByKzE7CiAgICAgICAgcmV0dXJuIGJpdFt4XVtycl07CiAgICB9CiAgICBpbnQgc29sdmUoaW50IGwsIGludCByLCBpbnQgbGwsIGludCByciwgaW50IGJpdCl7CiAgICAgICAgaWYoYml0PT0tMSlyZXR1cm4gZ2V0KGwsciwxLG0scm9vdFtsbF0pOwogICAgICAgIGludCBwb3MgPSBnZXRfcG9zKGwscixiaXQpOwogICAgICAgIHBvcyA9IE1hdGgubWluKHBvcywgcisxKTsKICAgICAgICBpZih4LmNoYXJBdChiaXQpPT0nMScpewogICAgICAgICAgICBpZihwb3M+bCAmJiBjaGVjayhsLHBvcy0xLCBsbCxycikpcmV0dXJuIHNvbHZlKGwscG9zLTEsbGwscnIsYml0LTEpOwogICAgICAgICAgICBlbHNlIHJldHVybiBzb2x2ZShwb3MsIHIsIGxsLCByciwgYml0LTEpOwogICAgICAgIH1lbHNlewogICAgICAgICAgICBpZihwb3M8PXIgJiYgY2hlY2socG9zLHIsbGwscnIpKXJldHVybiBzb2x2ZShwb3MscixsbCxycixiaXQtMSk7CiAgICAgICAgICAgIGVsc2UgcmV0dXJuIHNvbHZlKGwscG9zLTEsbGwscnIsYml0LTEpOwogICAgICAgIH0KICAgIH0KICAgIGludCBnZXRfcG9zX3NlY29uZChpbnQgbCwgaW50IHIsIGludCBsbCwgaW50IHJyLCBpbnQgaSwgaW50IHkpewogICAgICAgIGlmIChsPnIgfHwgbGw+ciB8fCBsPnJyIHx8IGxsPnJyKSByZXR1cm4gMDsKICAgICAgICBpZihsPj1sbCAmJiByPD1yciAmJiB0dFtpXT49eSlyZXR1cm4gMDsKICAgICAgICBpZihsPT1yKXJldHVybiBsOwogICAgICAgIGludCBtaWQgPSAobCtyKS8yOwogICAgICAgIGludCB4ID0gZ2V0X3Bvc19zZWNvbmQobWlkKzEscixsbCxycixpPDwxfDEsIHkpOwogICAgICAgIGlmKHg+MClyZXR1cm4geDsKICAgICAgICByZXR1cm4gZ2V0X3Bvc19zZWNvbmQobCxtaWQsbGwscnIsaTw8MSx5KTsKICAgIH0KICAgIHZvaWQgYnVpbGR0dCgpewogICAgICAgIG1heFByZWYgPSBuZXcgaW50W208PDFdOwogICAgICAgIGZvcihpbnQgaSA9IDE7IGk8PSBuOyBpKyspewogICAgICAgICAgICBpbnQgeCA9IGJbaS0xXSwgeSA9IGJbaV07CiAgICAgICAgICAgIGlmKGFbeF0ubGVuZ3RoKCk9PWFbeV0ubGVuZ3RoKCkpd2hpbGUobWF4UHJlZltpXTxhW3hdLmxlbmd0aCgpICYmIGFbeF0uY2hhckF0KG1heFByZWZbaV0pID09IGFbeV0uY2hhckF0KG1heFByZWZbaV0pKW1heFByZWZbaV0rKzsKICAgICAgICB9CiAgICAgICAgdHQgPSBuZXcgaW50W208PDFdOwogICAgICAgIGZvcihpbnQgaSA9IDE7IGk8IG08PDE7IGkrKyl0dFtpXSA9IElORjsKICAgICAgICBmb3IoaW50IGkgPSBtOyBpPCBtK247IGkrKyl0dFtpXSA9IG1heFByZWZbaS1tKzFdOwogICAgICAgIGZvcihpbnQgaSA9IG0tMTsgaT4wOyBpLS0pdHRbaV0gPSBNYXRoLm1pbih0dFtpPDwxXSwgdHRbaTw8MXwxXSk7CiAgICB9CiAgICBpbnQgY3JlYXRlQ29weShpbnQgeCl7CiAgICAgICAgdFtjbnRdID0gdFt4XTsKICAgICAgICBsZVtjbnRdID0gbGVbeF07CiAgICAgICAgcmlbY250XSA9IHJpW3hdOwogICAgICAgIHJldHVybiBjbnQrKzsKICAgIH0KICAgIC8vU09MVVRJT04gRU5ECiAgICBsb25nIG1vZCA9IChsb25nKTFlOSs3LCBJSU5GID0gKGxvbmcpNWUxODsKICAgIGZpbmFsIGludCBJTkYgPSAoaW50KTJlOTsKICAgIERlY2ltYWxGb3JtYXQgZGYgPSBuZXcgRGVjaW1hbEZvcm1hdCgiMC4wMDAwMDAwMDAiKTsKICAgIGRvdWJsZSBQSSA9IDMuMTQxNTkyNjUzNTg5NzkzMjM4NDYyNjQzMzgzMjc5Mjg4NDE5NzE2OTM5OTM3NTEwNTgyMDk3NDk0NCwgZXBzID0gMWUtODsKICAgIHN0YXRpYyBib29sZWFuIG11bHRpcGxlVEMgPSBmYWxzZSwgbWVtb3J5ID0gZmFsc2U7CiAgICBGYXN0UmVhZGVyIGluO1ByaW50V3JpdGVyIG91dDsKICAgIHZvaWQgcnVuKCkgdGhyb3dzIEV4Y2VwdGlvbnsKICAgICAgICBpbiA9IG5ldyBGYXN0UmVhZGVyKCk7CiAgICAgICAgb3V0ID0gbmV3IFByaW50V3JpdGVyKFN5c3RlbS5vdXQpOwogICAgICAgIGludCBUID0gKG11bHRpcGxlVEMpP25pKCk6MTsKICAgICAgICAvL1NvbHV0aW9uIENyZWRpdHM6IFRhcmFucHJlZXQgU2luZ2gKICAgICAgICBmb3IoaW50IGkgPSAxOyBpPD0gVDsgaSsrKXNvbHZlKGkpOwogICAgICAgIG91dC5mbHVzaCgpOwogICAgICAgIG91dC5jbG9zZSgpOwogICAgfQogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgdGhyb3dzIEV4Y2VwdGlvbnsKICAgICAgICBpZihtZW1vcnkpbmV3IFRocmVhZChudWxsLCBuZXcgUnVubmFibGUoKSB7cHVibGljIHZvaWQgcnVuKCl7dHJ5e25ldyBNYWluKCkucnVuKCk7fWNhdGNoKEV4Y2VwdGlvbiBlKXtlLnByaW50U3RhY2tUcmFjZSgpO319fSwgIjEiLCAxIDw8IDI4KS5zdGFydCgpOwogICAgICAgIGVsc2UgbmV3IE1haW4oKS5ydW4oKTsKICAgIH0KICAgIGxvbmcgZ2NkKGxvbmcgYSwgbG9uZyBiKXtyZXR1cm4gKGI9PTApP2E6Z2NkKGIsYSViKTt9CiAgICBpbnQgZ2NkKGludCBhLCBpbnQgYil7cmV0dXJuIChiPT0wKT9hOmdjZChiLGElYik7fQogICAgaW50IGJpdChsb25nIG4pe3JldHVybiAobj09MCk/MDooMStiaXQobiYobi0xKSkpO30KICAgIHZvaWQgcChPYmplY3Qgbyl7b3V0LnByaW50KG8pO30KICAgIHZvaWQgcG4oT2JqZWN0IG8pe291dC5wcmludGxuKG8pO30KICAgIHZvaWQgcG5pKE9iamVjdCBvKXtvdXQucHJpbnRsbihvKTtvdXQuZmx1c2goKTt9CiAgICBTdHJpbmcgbigpe3JldHVybiBpbi5uZXh0KCk7fQogICAgU3RyaW5nIG5sbigpe3JldHVybiBpbi5uZXh0TGluZSgpO30KICAgIGludCBuaSgpe3JldHVybiBJbnRlZ2VyLnBhcnNlSW50KGluLm5leHQoKSk7fQogICAgbG9uZyBubCgpe3JldHVybiBMb25nLnBhcnNlTG9uZyhpbi5uZXh0KCkpO30KICAgIGRvdWJsZSBuZCgpe3JldHVybiBEb3VibGUucGFyc2VEb3VibGUoaW4ubmV4dCgpKTt9CgogICAgY2xhc3MgRmFzdFJlYWRlcnsKICAgICAgICBCdWZmZXJlZFJlYWRlciBicjsKICAgICAgICBTdHJpbmdUb2tlbml6ZXIgc3Q7CiAgICAgICAgcHVibGljIEZhc3RSZWFkZXIoKXsKICAgICAgICAgICAgYnIgPSBuZXcgQnVmZmVyZWRSZWFkZXIobmV3IElucHV0U3RyZWFtUmVhZGVyKFN5c3RlbS5pbikpOwogICAgICAgIH0KCiAgICAgICAgcHVibGljIEZhc3RSZWFkZXIoU3RyaW5nIHMpIHRocm93cyBFeGNlcHRpb257CiAgICAgICAgICAgIGJyID0gbmV3IEJ1ZmZlcmVkUmVhZGVyKG5ldyBGaWxlUmVhZGVyKHMpKTsKICAgICAgICB9CgogICAgICAgIFN0cmluZyBuZXh0KCl7CiAgICAgICAgICAgIHdoaWxlIChzdCA9PSBudWxsIHx8ICFzdC5oYXNNb3JlRWxlbWVudHMoKSl7CiAgICAgICAgICAgICAgICB0cnl7CiAgICAgICAgICAgICAgICAgICAgc3QgPSBuZXcgU3RyaW5nVG9rZW5pemVyKGJyLnJlYWRMaW5lKCkpOwogICAgICAgICAgICAgICAgfWNhdGNoIChJT0V4Y2VwdGlvbiAgZSl7CiAgICAgICAgICAgICAgICAgICAgZS5wcmludFN0YWNrVHJhY2UoKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm4gc3QubmV4dFRva2VuKCk7CiAgICAgICAgfQoKICAgICAgICBTdHJpbmcgbmV4dExpbmUoKXsKICAgICAgICAgICAgU3RyaW5nIHN0ciA9ICIiOwogICAgICAgICAgICB0cnl7ICAgIAogICAgICAgICAgICAgICAgc3RyID0gYnIucmVhZExpbmUoKTsKICAgICAgICAgICAgfWNhdGNoIChJT0V4Y2VwdGlvbiBlKXsKICAgICAgICAgICAgICAgIGUucHJpbnRTdGFja1RyYWNlKCk7CiAgICAgICAgICAgIH0gICAKICAgICAgICAgICAgcmV0dXJuIHN0cjsKICAgICAgICB9CiAgICB9Cn0gIA==