#include<cassert>
#include<cstdio>
#include<cmath>
#include<vector>
#include<tuple>
using namespace std;
#ifndef M_PI
const double M_PI = acos(-1);
#endif
const double eps=1e-10, fps=10, dt=1/fps;
int sgn(double c){
return (c>eps)-(c<-eps);
}
#define NP 25 // maximum number of platforms
int np; // number of platforms
double py[NP], pl[NP], pr[NP]; // y-coordinates, left x-coordinates, right x-coordinates of platforms
#define NM 25 // maximum number of monsters
struct point{
double x, y;
point(double x=0, double y=0): x(x), y(y){}
point operator +(const point &q) const{
return point(x+q.x, y+q.y);
}
point operator -() const{
return point(-x, -y);
}
point operator -(const point &q) const{
return (*this)+(-q);
}
point operator *(const double &c) const{
return point(c*x, c*y);
}
double len() const{
return hypot(x, y);
}
};
point operator *(const double &c, const point &p){
return p*c;
}
int nm; // number of monsters
bool mt[NM]; // monster type, 0 for SHM, 1 for circular motion
point mp0[NM], mp1[NM]; // p0: initial position (SHM) or center (circular motion), p1: farthest position (SHM only)
double momega[NM], mr[NM]; // omega: angular velocity, r: radius (circular motion only)
point mp(int i, double t){ // monster i's position at time t
if(mt[i]){
return mp0[i] + mr[i]*point(cos(momega[i]*t), sin(momega[i]*t));
}else{
return .5*(mp0[i]+mp1[i]) + .5*(mp0[i]-mp1[i])*cos(momega[i]*t);
}
}
#define L 10000
char str[L]; // input string composed of commands
struct state{
point p, v; // p: position, v: velocity
int pno; // 0: aloft; otherwise: platform no.
state(const point &p=point(), const point &v=point(), const int &pno=0): p(p), v(v), pno(pno){}
};
pair<int, double> loli(const state &s){ // return what platform the player jumps to and how long it takes to reach that platform
if(s.pno){
return make_pair(s.pno, 0.);
}
double x0=s.p.x, y0=s.p.y, vx=s.v.x, vy=s.v.y;
if(sgn(vx) == 0){
double h = y0;
if(sgn(vy) >= 0) h += vy*vy/4;
int no = 0;
for(int i=1; i<=np; i++) if(sgn(x0-pl[i])>=0 && sgn(pr[i]-x0)>=0 && sgn(h-py[i])>=0){
if(!no || sgn(py[i]-py[no])>0){
no = i;
}
}
assert(no);
if(sgn(vy)<0) h += vy*vy/4;
h -= py[no];
if(sgn(h)==0) h = 0;
return make_pair(no, sqrt(h)+vy/2);
}
double a=-1/(vx*vx), b=vy/vx+2*x0/(vx*vx), c=y0-vy*x0/vx-x0*x0/(vx*vx); // y=ax^2+bx+c
int no = 0;
double time;
for(int i=1; i<=np; i++){
double D = b*b-4*a*(c-py[i]);
if(sgn(D)<0) continue;
if(sgn(D)==0) D = 0;
double xt;
if(sgn(vx)>0) xt=(-b-sqrt(D))/(2*a);
else xt=(-b+sqrt(D))/(2*a);
double t = (xt-x0)/vx;
if(sgn(xt-pl[i])>=0 && sgn(pr[i]-xt)>=0 && sgn(t)>0){
if(!no || sgn(time-t)>0){
no=i, time=t;
}
}
}
assert(no);
return make_pair(no, time);
}
state ugoku(const state &s, double t){ // the command and s.pno are not allowed to change in the following t seconds
if(s.pno){
return state(s.p+s.v*t, s.v, s.pno);
}else{
return state(s.p+s.v*t-point(0, t*t), s.v-point(0, 2*t), s.pno);
}
}
int main(){
scanf("%d", &np);
for(int i=1; i<=np; i++){
scanf("%lf%lf%lf", py+i, pl+i, pr+i);
}
scanf("%d", &nm);
for(int i=1; i<=nm; i++){
char t;
scanf(" %c", &t);
double T;
if(t == 'h'){
mt[i] = 0;
scanf("%lf%lf%lf%lf%lf", &mp0[i].x, &mp0[i].y, &mp1[i].x, &mp1[i].y, &T);
}else{
mt[i] = 1;
scanf("%lf%lf%lf%lf", &mp0[i].x, &mp0[i].y, mr+i, &T);
}
momega[i] = 2*M_PI/T;
}
point start;
int T;
scanf("%lf%lf%d", &start.x, &start.y, &T);
fgets(str, L, stdin);
vector<state> traj(1, state(start)); // trajectory
for(int i=1; i<=np; i++){
if(sgn(start.y-py[i])==0 && sgn(start.x-pl[i])>=0 && sgn(pr[i]-start.x)>=0){
traj[0].pno = i; break;
}
}
while(T--){
fgets(str, L, stdin);
int offset=0, delta;
char cmd[10];
double tc;
state s = traj[0];
for(; ~sscanf(str+offset, "%s%lf%n", cmd, &tc, &delta); offset+=delta){
int cnt = round(fps*tc);
assert(sgn(cnt-fps*tc) == 0);
if(cmd[0]=='n'){
int no; double tr; // tr: reach a platform
tie(no, tr) = loli(s);
if(sgn(tr)==0){
s.v = point();
s.pno = no;
}
while(cnt--){
if(sgn(tr-dt)>0){
s = ugoku(s, dt);
tr -= dt;
}else if(sgn(tr)>0){
s = ugoku(s, tr);
s.v = point();
s.pno = no;
tr = 0;
}
traj.push_back(s);
}
}else if(cmd[0]=='l' && cmd[1]=='w'){
int no; double tr, tl=0, tt=dt; // tl: leave a platform, tt: next tick
tie(no, tr) = loli(s);
if(sgn(tr)==0){
s.v = point(-1, 0);
s.pno = no;
tl = s.p.x-pl[no];
}
while(cnt){
if(sgn(tr-tt)>0){
s = ugoku(s, tt);
traj.push_back(s);
tr-=tt; tt=dt; cnt--;
}else if(sgn(tr)>0){
s = ugoku(s, tr);
s.v = point(-1, 0);
s.pno = no;
tt-=tr; tr=0; tl=s.p.x-pl[no];
if(sgn(tt)==0){
traj.push_back(s);
tt=dt; cnt--;
}
}else if(sgn(tl-tt)>0){
s = ugoku(s, tt);
traj.push_back(s);
tl-=tt; tt=dt; cnt--;
}else{
s = ugoku(s, tl);
s.pno = 0;
tt-=tl; tl=0; tie(no, tr)=loli(s);
if(sgn(tt)==0){
traj.push_back(s);
tt=dt; cnt--;
}
}
}
}else if(cmd[0]=='r' && cmd[1]=='w'){
int no; double tr, tl=0, tt=dt;
tie(no, tr) = loli(s);
if(sgn(tr)==0){
s.v = point(1, 0);
s.pno = no;
tl = pr[no]-s.p.x;
}
while(cnt){
if(sgn(tr-tt)>0){
s = ugoku(s, tt);
traj.push_back(s);
tr-=tt; tt=dt; cnt--;
}else if(sgn(tr)>0){
s = ugoku(s, tr);
s.v = point(1, 0);
s.pno = no;
tt-=tr; tr=0; tl=pr[no]-s.p.x;
if(sgn(tt)==0){
traj.push_back(s);
tt=dt; cnt--;
}
}else if(sgn(tl-tt)>0){
s = ugoku(s, tt);
traj.push_back(s);
tl-=tt; tt=dt; cnt--;
}else{
s = ugoku(s, tl);
s.pno = 0;
tt-=tl; tl=0; tie(no, tr)=loli(s);
if(sgn(tt)==0){
traj.push_back(s);
tt=dt; cnt--;
}
}
}
}else if(cmd[0]=='u'){
int no; double tr, tt=dt;
tie(no, tr) = loli(s);
while(cnt){
if(sgn(tr-tt)>0){
s = ugoku(s, tt);
traj.push_back(s);
tr-=tt; tt=dt; cnt--;
}else if(sgn(tr)>0){
s = ugoku(s, tr);
s.v = point(0, 2);
tt-=tr; tie(no, tr)=loli(s);
if(sgn(tt)==0){
traj.push_back(s);
tt=dt; cnt--;
}
}else{
s.v = point(0, 2);
s.pno = 0;
tie(no, tr) = loli(s);
}
}
}else if(cmd[0]=='l' && cmd[1]=='j'){
int no; double tr, tt=dt;
tie(no, tr) = loli(s);
while(cnt){
if(sgn(tr-tt)>0){
s = ugoku(s, tt);
traj.push_back(s);
tr-=tt; tt=dt; cnt--;
}else if(sgn(tr)>0){
s = ugoku(s, tr);
s.v = point(-sqrt(2), sqrt(2));
tt-=tr; tie(no, tr)=loli(s);
if(sgn(tt)==0){
traj.push_back(s);
tt=dt; cnt--;
}
}else{
s.v = point(-sqrt(2), sqrt(2));
s.pno = 0;
tie(no, tr) = loli(s);
}
}
}else{
int no; double tr, tt=dt;
tie(no, tr) = loli(s);
while(cnt){
if(sgn(tr-tt)>0){
s = ugoku(s, tt);
traj.push_back(s);
tr-=tt; tt=dt; cnt--;
}else if(sgn(tr)>0){
s = ugoku(s, tr);
s.v = point(sqrt(2), sqrt(2));
tt-=tr; tie(no, tr)=loli(s);
if(sgn(tt)==0){
traj.push_back(s);
tt=dt; cnt--;
}
}else{
s.v = point(sqrt(2), sqrt(2));
s.pno = 0;
tie(no, tr) = loli(s);
}
}
}
}
int no; double tr;
tie(no, tr) = loli(s);
if(sgn(tr)==0){
s.v = point();
s.pno = no;
}
while(sgn(tr)>0){
if(sgn(tr-dt)>0){
s = ugoku(s, dt);
tr -= dt;
}else if(sgn(tr)>0){
s = ugoku(s, tr);
s.v = point();
s.pno = no;
tr = 0;
}
traj.push_back(s);
}
for(int i=1; i<=fps; i++){
traj.push_back(s);
}
bool win = true;
for(int i=0; i<(int)traj.size(); i++){
for(int j=1; j<=nm; j++){
if(sgn((traj[i].p-mp(j, i*dt)).len()-.5) < 0){
win = false; break;
}
}
if(!win){
break;
}
}
printf("%c\n", "NY"[win]);
traj.resize(1);
}
return 0;
}
I2luY2x1ZGU8Y2Fzc2VydD4KI2luY2x1ZGU8Y3N0ZGlvPgojaW5jbHVkZTxjbWF0aD4KI2luY2x1ZGU8dmVjdG9yPgojaW5jbHVkZTx0dXBsZT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNpZm5kZWYgTV9QSQpjb25zdCBkb3VibGUgTV9QSSA9IGFjb3MoLTEpOwojZW5kaWYKY29uc3QgZG91YmxlIGVwcz0xZS0xMCwgZnBzPTEwLCBkdD0xL2ZwczsKaW50IHNnbihkb3VibGUgYyl7CiAgICByZXR1cm4gKGM+ZXBzKS0oYzwtZXBzKTsKfQoKI2RlZmluZSBOUCAyNSAvLyBtYXhpbXVtIG51bWJlciBvZiBwbGF0Zm9ybXMKaW50IG5wOyAvLyBudW1iZXIgb2YgcGxhdGZvcm1zCmRvdWJsZSBweVtOUF0sIHBsW05QXSwgcHJbTlBdOyAvLyB5LWNvb3JkaW5hdGVzLCBsZWZ0IHgtY29vcmRpbmF0ZXMsIHJpZ2h0IHgtY29vcmRpbmF0ZXMgb2YgcGxhdGZvcm1zCgojZGVmaW5lIE5NIDI1IC8vIG1heGltdW0gbnVtYmVyIG9mIG1vbnN0ZXJzCnN0cnVjdCBwb2ludHsKICAgIGRvdWJsZSB4LCB5OwogICAgcG9pbnQoZG91YmxlIHg9MCwgZG91YmxlIHk9MCk6IHgoeCksIHkoeSl7fQogICAgcG9pbnQgb3BlcmF0b3IgKyhjb25zdCBwb2ludCAmcSkgY29uc3R7CiAgICAgICAgcmV0dXJuIHBvaW50KHgrcS54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jApewogICAgICAgICAgICAgICAgbm8gPSBpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGFzc2VydChubyk7CiAgICAgICAgaWYoc2duKHZ5KTwwKSBoICs9IHZ5KnZ5LzQ7CiAgICAgICAgaCAtPSBweVtub107CiAgICAgICAgaWYoc2duKGgpPT0wKSBoID0gMDsKICAgICAgICByZXR1cm4gbWFrZV9wYWlyKG5vLCBzcXJ0KGgpK3Z5LzIpOwogICAgfQogICAgZG91YmxlIGE9LTEvKHZ4KnZ4KSwgYj12eS92eCsyKngwLyh2eCp2eCksIGM9eTAtdnkqeDAvdngteDAqeDAvKHZ4KnZ4KTsgLy8geT1heF4yK2J4K2MKICAgIGludCBubyA9IDA7CiAgICBkb3VibGUgdGltZTsKICAgIGZvcihpbnQgaT0xOyBpPD1ucDsgaSsrKXsKICAgICAgICBkb3VibGUgRCA9IGIqYi00KmEqKGMtcHlbaV0pOwogICAgICAgIGlmKHNnbihEKTwwKSBjb250aW51ZTsKICAgICAgICBpZihzZ24oRCk9PTApIEQgPSAwOwogICAgICAgIGRvdWJsZSB4dDsKICAgICAgICBpZihzZ24odngpPjApIHh0PSgtYi1zcXJ0KEQpKS8oMiphKTsKICAgICAgICBlbHNlIHh0PSgtYitzcXJ0KEQpKS8oMiphKTsKICAgICAgICBkb3VibGUgdCA9ICh4dC14MCkvdng7CiAgICAgICAgaWYoc2duKHh0LXBsW2ldKT49MCAmJiBzZ24ocHJbaV0teHQpPj0wICYmIHNnbih0KT4wKXsKICAgICAgICAgICAgaWYoIW5vIHx8IHNnbih0aW1lLXQpPjApewogICAgICAgICAgICAgICAgbm89aSwgdGltZT10OwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgYXNzZXJ0KG5vKTsKICAgIHJldHVybiBtYWtlX3BhaXIobm8sIHRpbWUpOwp9CnN0YXRlIHVnb2t1KGNvbnN0IHN0YXRlICZzLCBkb3VibGUgdCl7IC8vIHRoZSBjb21tYW5kIGFuZCBzLnBubyBhcmUgbm90IGFsbG93ZWQgdG8gY2hhbmdlIGluIHRoZSBmb2xsb3dpbmcgdCBzZWNvbmRzCiAgICBpZihzLnBubyl7CiAgICAgICAgcmV0dXJuIHN0YXRlKHMucCtzLnYqdCwgcy52LCBzLnBubyk7CiAgICB9ZWxzZXsKICAgICAgICByZXR1cm4gc3RhdGUocy5wK3Mudip0LXBvaW50KDAsIHQqdCksIHMudi1wb2ludCgwLCAyKnQpLCBzLnBubyk7CiAgICB9Cn0KCmludCBtYWluKCl7CiAgICBzY2FuZigiJWQiLCAmbnApOwogICAgZm9yKGludCBpPTE7IGk8PW5wOyBpKyspewogICAgICAgIHNjYW5mKCIlbGYlbGYlbGYiLCBweStpLCBwbCtpLCBwcitpKTsKICAgIH0KICAgIHNjYW5mKCIlZCIsICZubSk7CiAgICBmb3IoaW50IGk9MTsgaTw9bm07IGkrKyl7CiAgICAgICAgY2hhciB0OwogICAgICAgIHNjYW5mKCIgJWMiLCAmdCk7CiAgICAgICAgZG91YmxlIFQ7CiAgICAgICAgaWYodCA9PSAnaCcpewogICAgICAgICAgICBtdFtpXSA9IDA7CiAgICAgICAgICAgIHNjYW5mKCIlbGYlbGYlbGYlbGYlbGYiLCAmbXAwW2ldLngsICZtcDBbaV0ueSwgJm1wMVtpXS54LCAmbXAxW2ldLnksICZUKTsKICAgICAgICB9ZWxzZXsKICAgICAgICAgICAgbXRbaV0gPSAxOwogICAgICAgICAgICBzY2FuZigiJWxmJWxmJWxmJWxmIiwgJm1wMFtpXS54LCAmbXAwW2ldLnksIG1yK2ksICZUKTsKICAgICAgICB9CiAgICAgICAgbW9tZWdhW2ldID0gMipNX1BJL1Q7CiAgICB9CiAgICBwb2ludCBzdGFydDsKICAgIGludCBUOwogICAgc2NhbmYoIiVsZiVsZiVkIiwgJnN0YXJ0LngsICZzdGFydC55LCAmVCk7CiAgICBmZ2V0cyhzdHIsIEwsIHN0ZGluKTsKICAgIHZlY3RvcjxzdGF0ZT4gdHJhaigxLCBzdGF0ZShzdGFydCkpOyAvLyB0cmFqZWN0b3J5CiAgICBmb3IoaW50IGk9MTsgaTw9bnA7IGkrKyl7CiAgICAgICAgaWYoc2duKHN0YXJ0LnktcHlbaV0pPT0wICYmIHNnbihzdGFydC54LXBsW2ldKT49MCAmJiBzZ24ocHJbaV0tc3RhcnQueCk+PTApewogICAgICAgICAgICB0cmFqWzBdLnBubyA9IGk7IGJyZWFrOwogICAgICAgIH0KICAgIH0KICAgIHdoaWxlKFQtLSl7CiAgICAgICAgZmdldHMoc3RyLCBMLCBzdGRpbik7CiAgICAgICAgaW50IG9mZnNldD0wLCBkZWx0YTsKICAgICAgICBjaGFyIGNtZFsxMF07CiAgICAgICAgZG91YmxlIHRjOwogICAgICAgIHN0YXRlIHMgPSB0cmFqWzBdOwogICAgICAgIGZvcig7IH5zc2NhbmYoc3RyK29mZnNldCwgIiVzJWxmJW4iLCBjbWQsICZ0YywgJmRlbHRhKTsgb2Zmc2V0Kz1kZWx0YSl7CiAgICAgICAgICAgIGludCBjbnQgPSByb3VuZChmcHMqdGMpOwogICAgICAgICAgICBhc3NlcnQoc2duKGNudC1mcHMqdGMpID09IDApOwogICAgICAgICAgICBpZihjbWRbMF09PSduJyl7CiAgICAgICAgICAgICAgICBpbnQgbm87IGRvdWJsZSB0cjsgLy8gdHI6IHJlYWNoIGEgcGxhdGZvcm0KICAgICAgICAgICAgICAgIHRpZShubywgdHIpID0gbG9saShzKTsKICAgICAgICAgICAgICAgIGlmKHNnbih0cik9PTApewogICAgICAgICAgICAgICAgICAgIHMudiA9IHBvaW50KCk7CiAgICAgICAgICAgICAgICAgICAgcy5wbm8gPSBubzsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHdoaWxlKGNudC0tKXsKICAgICAgICAgICAgICAgICAgICBpZihzZ24odHItZHQpPjApewogICAgICAgICAgICAgICAgICAgICAgICBzID0gdWdva3UocywgZHQpOwogICAgICAgICAgICAgICAgICAgICAgICB0ciAtPSBkdDsKICAgICAgICAgICAgICAgICAgICB9ZWxzZSBpZihzZ24odHIpPjApewogICAgICAgICAgICAgICAgICAgICAgICBzID0gdWdva3UocywgdHIpOwogICAgICAgICAgICAgICAgICAgICAgICBzLnYgPSBwb2ludCgpOwogICAgICAgICAgICAgICAgICAgICAgICBzLnBubyA9IG5vOwogICAgICAgICAgICAgICAgICAgICAgICB0ciA9IDA7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIHRyYWoucHVzaF9iYWNrKHMpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9ZWxzZSBpZihjbWRbMF09PSdsJyAmJiBjbWRbMV09PSd3Jyl7CiAgICAgICAgICAgICAgICBpbnQgbm87IGRvdWJsZSB0ciwgdGw9MCwgdHQ9ZHQ7IC8vIHRsOiBsZWF2ZSBhIHBsYXRmb3JtLCB0dDogbmV4dCB0aWNrCiAgICAgICAgICAgICAgICB0aWUobm8sIHRyKSA9IGxvbGkocyk7CiAgICAgICAgICAgICAgICBpZihzZ24odHIpPT0wKXsKICAgICAgICAgICAgICAgICAgICBzLnYgPSBwb2ludCgtMSwgMCk7CiAgICAgICAgICAgICAgICAgICAgcy5wbm8gPSBubzsKICAgICAgICAgICAgICAgICAgICB0bCA9IHMucC54LXBsW25vXTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHdoaWxlKGNudCl7CiAgICAgICAgICAgICAgICAgICAgaWYoc2duKHRyLXR0KT4wKXsKICAgICAgICAgICAgICAgICAgICAgICAgcyA9IHVnb2t1KHMsIHR0KTsKICAgICAgICAgICAgICAgICAgICAgICAgdHJhai5wdXNoX2JhY2socyk7CiAgICAgICAgICAgICAgICAgICAgICAgIHRyLT10dDsgdHQ9ZHQ7IGNudC0tOwogICAgICAgICAgICAgICAgICAgIH1lbHNlIGlmKHNnbih0cik+MCl7CiAgICAgICAgICAgICAgICAgICAgICAgIHMgPSB1Z29rdShzLCB0cik7CiAgICAgICAgICAgICAgICAgICAgICAgIHMudiA9IHBvaW50KC0xLCAwKTsKICAgICAgICAgICAgICAgICAgICAgICAgcy5wbm8gPSBubzsKICAgICAgICAgICAgICAgICAgICAgICAgdHQtPXRyOyB0cj0wOyB0bD1zLnAueC1wbFtub107CiAgICAgICAgICAgICAgICAgICAgICAgIGlmKHNnbih0dCk9PTApewogICAgICAgICAgICAgICAgICAgICAgICAgICAgdHJhai5wdXNoX2JhY2socyk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB0dD1kdDsgY250LS07CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9ZWxzZSBpZihzZ24odGwtdHQpPjApewogICAgICAgICAgICAgICAgICAgICAgICBzID0gdWdva3UocywgdHQpOwogICAgICAgICAgICAgICAgICAgICAgICB0cmFqLnB1c2hfYmFjayhzKTsKICAgICAgICAgICAgICAgICAgICAgICAgdGwtPXR0OyB0dD1kdDsgY250LS07CiAgICAgICAgICAgICAgICAgICAgfWVsc2V7CiAgICAgICAgICAgICAgICAgICAgICAgIHMgPSB1Z29rdShzLCB0bCk7CiAgICAgICAgICAgICAgICAgICAgICAgIHMucG5vID0gMDsKICAgICAgICAgICAgICAgICAgICAgICAgdHQtPXRsOyB0bD0wOyB0aWUobm8sIHRyKT1sb2xpKHMpOwogICAgICAgICAgICAgICAgICAgICAgICBpZihzZ24odHQpPT0wKXsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHRyYWoucHVzaF9iYWNrKHMpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgdHQ9ZHQ7IGNudC0tOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9ZWxzZSBpZihjbWRbMF09PSdyJyAmJiBjbWRbMV09PSd3Jyl7CiAgICAgICAgICAgICAgICBpbnQgbm87IGRvdWJsZSB0ciwgdGw9MCwgdHQ9ZHQ7CiAgICAgICAgICAgICAgICB0aWUobm8sIHRyKSA9IGxvbGkocyk7CiAgICAgICAgICAgICAgICBpZihzZ24odHIpPT0wKXsKICAgICAgICAgICAgICAgICAgICBzLnYgPSBwb2ludCgxLCAwKTsKICAgICAgICAgICAgICAgICAgICBzLnBubyA9IG5vOwogICAgICAgICAgICAgICAgICAgIHRsID0gcHJbbm9dLXMucC54OwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgd2hpbGUoY250KXsKICAgICAgICAgICAgICAgICAgICBpZihzZ24odHItdHQpPjApewogICAgICAgICAgICAgICAgICAgICAgICBzID0gdWdva3UocywgdHQpOwogICAgICAgICAgICAgICAgICAgICAgICB0cmFqLnB1c2hfYmFjayhzKTsKICAgICAgICAgICAgICAgICAgICAgICAgdHItPXR0OyB0dD1kdDsgY250LS07CiAgICAgICAgICAgICAgICAgICAgfWVsc2UgaWYoc2duKHRyKT4wKXsKICAgICAgICAgICAgICAgICAgICAgICAgcyA9IHVnb2t1KHMsIHRyKTsKICAgICAgICAgICAgICAgICAgICAgICAgcy52ID0gcG9pbnQoMSwgMCk7CiAgICAgICAgICAgICAgICAgICAgICAgIHMucG5vID0gbm87CiAgICAgICAgICAgICAgICAgICAgICAgIHR0LT10cjsgdHI9MDsgdGw9cHJbbm9dLXMucC54OwogICAgICAgICAgICAgICAgICAgICAgICBpZihzZ24odHQpPT0wKXsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHRyYWoucHVzaF9iYWNrKHMpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgdHQ9ZHQ7IGNudC0tOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfWVsc2UgaWYoc2duKHRsLXR0KT4wKXsKICAgICAgICAgICAgICAgICAgICAgICAgcyA9IHVnb2t1KHMsIHR0KTsKICAgICAgICAgICAgICAgICAgICAgICAgdHJhai5wdXNoX2JhY2socyk7CiAgICAgICAgICAgICAgICAgICAgICAgIHRsLT10dDsgdHQ9ZHQ7IGNudC0tOwogICAgICAgICAgICAgICAgICAgIH1lbHNlewogICAgICAgICAgICAgICAgICAgICAgICBzID0gdWdva3UocywgdGwpOwogICAgICAgICAgICAgICAgICAgICAgICBzLnBubyA9IDA7CiAgICAgICAgICAgICAgICAgICAgICAgIHR0LT10bDsgdGw9MDsgdGllKG5vLCB0cik9bG9saShzKTsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoc2duKHR0KT09MCl7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB0cmFqLnB1c2hfYmFjayhzKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHR0PWR0OyBjbnQtLTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfWVsc2UgaWYoY21kWzBdPT0ndScpewogICAgICAgICAgICAgICAgaW50IG5vOyBkb3VibGUgdHIsIHR0PWR0OwogICAgICAgICAgICAgICAgdGllKG5vLCB0cikgPSBsb2xpKHMpOwogICAgICAgICAgICAgICAgd2hpbGUoY250KXsKICAgICAgICAgICAgICAgICAgICBpZihzZ24odHItdHQpPjApewogICAgICAgICAgICAgICAgICAgICAgICBzID0gdWdva3UocywgdHQpOwogICAgICAgICAgICAgICAgICAgICAgICB0cmFqLnB1c2hfYmFjayhzKTsKICAgICAgICAgICAgICAgICAgICAgICAgdHItPXR0OyB0dD1kdDsgY250LS07CiAgICAgICAgICAgICAgICAgICAgfWVsc2UgaWYoc2duKHRyKT4wKXsKICAgICAgICAgICAgICAgICAgICAgICAgcyA9IHVnb2t1KHMsIHRyKTsKICAgICAgICAgICAgICAgICAgICAgICAgcy52ID0gcG9pbnQoMCwgMik7CiAgICAgICAgICAgICAgICAgICAgICAgIHR0LT10cjsgdGllKG5vLCB0cik9bG9saShzKTsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoc2duKHR0KT09MCl7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB0cmFqLnB1c2hfYmFjayhzKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHR0PWR0OyBjbnQtLTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH1lbHNlewogICAgICAgICAgICAgICAgICAgICAgICBzLnYgPSBwb2ludCgwLCAyKTsKICAgICAgICAgICAgICAgICAgICAgICAgcy5wbm8gPSAwOwogICAgICAgICAgICAgICAgICAgICAgICB0aWUobm8sIHRyKSA9IGxvbGkocyk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9ZWxzZSBpZihjbWRbMF09PSdsJyAmJiBjbWRbMV09PSdqJyl7CiAgICAgICAgICAgICAgICBpbnQgbm87IGRvdWJsZSB0ciwgdHQ9ZHQ7CiAgICAgICAgICAgICAgICB0aWUobm8sIHRyKSA9IGxvbGkocyk7CiAgICAgICAgICAgICAgICB3aGlsZShjbnQpewogICAgICAgICAgICAgICAgICAgIGlmKHNnbih0ci10dCk+MCl7CiAgICAgICAgICAgICAgICAgICAgICAgIHMgPSB1Z29rdShzLCB0dCk7CiAgICAgICAgICAgICAgICAgICAgICAgIHRyYWoucHVzaF9iYWNrKHMpOwogICAgICAgICAgICAgICAgICAgICAgICB0ci09dHQ7IHR0PWR0OyBjbnQtLTsKICAgICAgICAgICAgICAgICAgICB9ZWxzZSBpZihzZ24odHIpPjApewogICAgICAgICAgICAgICAgICAgICAgICBzID0gdWdva3UocywgdHIpOwogICAgICAgICAgICAgICAgICAgICAgICBzLnYgPSBwb2ludCgtc3FydCgyKSwgc3FydCgyKSk7CiAgICAgICAgICAgICAgICAgICAgICAgIHR0LT10cjsgdGllKG5vLCB0cik9bG9saShzKTsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoc2duKHR0KT09MCl7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB0cmFqLnB1c2hfYmFjayhzKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHR0PWR0OyBjbnQtLTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH1lbHNlewogICAgICAgICAgICAgICAgICAgICAgICBzLnYgPSBwb2ludCgtc3FydCgyKSwgc3FydCgyKSk7CiAgICAgICAgICAgICAgICAgICAgICAgIHMucG5vID0gMDsKICAgICAgICAgICAgICAgICAgICAgICAgdGllKG5vLCB0cikgPSBsb2xpKHMpOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfWVsc2V7CiAgICAgICAgICAgICAgICBpbnQgbm87IGRvdWJsZSB0ciwgdHQ9ZHQ7CiAgICAgICAgICAgICAgICB0aWUobm8sIHRyKSA9IGxvbGkocyk7CiAgICAgICAgICAgICAgICB3aGlsZShjbnQpewogICAgICAgICAgICAgICAgICAgIGlmKHNnbih0ci10dCk+MCl7CiAgICAgICAgICAgICAgICAgICAgICAgIHMgPSB1Z29rdShzLCB0dCk7CiAgICAgICAgICAgICAgICAgICAgICAgIHRyYWoucHVzaF9iYWNrKHMpOwogICAgICAgICAgICAgICAgICAgICAgICB0ci09dHQ7IHR0PWR0OyBjbnQtLTsKICAgICAgICAgICAgICAgICAgICB9ZWxzZSBpZihzZ24odHIpPjApewogICAgICAgICAgICAgICAgICAgICAgICBzID0gdWdva3UocywgdHIpOwogICAgICAgICAgICAgICAgICAgICAgICBzLnYgPSBwb2ludChzcXJ0KDIpLCBzcXJ0KDIpKTsKICAgICAgICAgICAgICAgICAgICAgICAgdHQtPXRyOyB0aWUobm8sIHRyKT1sb2xpKHMpOwogICAgICAgICAgICAgICAgICAgICAgICBpZihzZ24odHQpPT0wKXsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHRyYWoucHVzaF9iYWNrKHMpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgdHQ9ZHQ7IGNudC0tOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfWVsc2V7CiAgICAgICAgICAgICAgICAgICAgICAgIHMudiA9IHBvaW50KHNxcnQoMiksIHNxcnQoMikpOwogICAgICAgICAgICAgICAgICAgICAgICBzLnBubyA9IDA7CiAgICAgICAgICAgICAgICAgICAgICAgIHRpZShubywgdHIpID0gbG9saShzKTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaW50IG5vOyBkb3VibGUgdHI7CiAgICAgICAgdGllKG5vLCB0cikgPSBsb2xpKHMpOwogICAgICAgIGlmKHNnbih0cik9PTApewogICAgICAgICAgICBzLnYgPSBwb2ludCgpOwogICAgICAgICAgICBzLnBubyA9IG5vOwogICAgICAgIH0KICAgICAgICB3aGlsZShzZ24odHIpPjApewogICAgICAgICAgICBpZihzZ24odHItZHQpPjApewogICAgICAgICAgICAgICAgcyA9IHVnb2t1KHMsIGR0KTsKICAgICAgICAgICAgICAgIHRyIC09IGR0OwogICAgICAgICAgICB9ZWxzZSBpZihzZ24odHIpPjApewogICAgICAgICAgICAgICAgcyA9IHVnb2t1KHMsIHRyKTsKICAgICAgICAgICAgICAgIHMudiA9IHBvaW50KCk7CiAgICAgICAgICAgICAgICBzLnBubyA9IG5vOwogICAgICAgICAgICAgICAgdHIgPSAwOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHRyYWoucHVzaF9iYWNrKHMpOwogICAgICAgIH0KICAgICAgICBmb3IoaW50IGk9MTsgaTw9ZnBzOyBpKyspewogICAgICAgICAgICB0cmFqLnB1c2hfYmFjayhzKTsKICAgICAgICB9CiAgICAgICAgYm9vbCB3aW4gPSB0cnVlOwogICAgICAgIGZvcihpbnQgaT0wOyBpPChpbnQpdHJhai5zaXplKCk7IGkrKyl7CiAgICAgICAgICAgIGZvcihpbnQgaj0xOyBqPD1ubTsgaisrKXsKICAgICAgICAgICAgICAgIGlmKHNnbigodHJhaltpXS5wLW1wKGosIGkqZHQpKS5sZW4oKS0uNSkgPCAwKXsKICAgICAgICAgICAgICAgICAgICB3aW4gPSBmYWxzZTsgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYoIXdpbil7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBwcmludGYoIiVjXG4iLCAiTlkiW3dpbl0pOwogICAgICAgIHRyYWoucmVzaXplKDEpOwogICAgfQogICAgcmV0dXJuIDA7Cn0K