#P9889. Reasoning

Reasoning

Reasoning

Problem Description

The reasoning system consists of the following symbols:

  • Parentheses: (( and ));
  • Logical connectives: ¬\neg and \to;
  • Universal quantifier: \forall;
  • Variables: uzu-z;
  • Constants: aea-e;
  • Functions: fhf-h;
  • Predicates: PTP-T. This reasoning system also includes concepts such as term, formula, free occurrence, and replacement. Based on these concepts, we can define whether a certain term tt can replace a certain variable xx without contradiction. This is one of the basis for reasoning, and you wants to solve this problem first. Term is defined as follows:
  • Every variable vv is a term.
  • Every constant cc is a term.
  • If t1,,tnt_1,\dots,t_n are terms and ff is a function, then ft1tnft_1\dots t_n is a term. Formula (well-formed formula) is defined as follows:
  • If t1,,tnt_1,\dots,t_n are terms and PP is a predicate, then Pt1tnPt_1\dots t_n is a formula. This kind of formula is also called the atomic formula.
  • If φ\varphi and ψ\psi are formulas, then (¬φ)(\neg\varphi) and (φψ)(\varphi\to\psi) are both formulas.
  • If φ\varphi is a formula, and vv is a variable, then vφ\forall v\varphi is also a formula. Free occurrence of a variable xx in formula φ\varphi is defined as follows:
  • If φ\varphi is an atomic formula, then xx occurs free in φ\varphi if and only if xx occurs in φ\varphi (which means there is a char xx inside the string φ\varphi).
  • If φ\varphi is (¬ψ)(\neg\psi), then xx occurs free in φ\varphi if and only if xx occurs free in ψ\psi.
  • If φ\varphi is (ψγ)(\psi\to\gamma), then xx occurs free in φ\varphi if and only if xx occurs free in ψ\psi or xx occurs free in γ\gamma.
  • If φ\varphi is vψ\forall v\psi, then xx occurs free in φ\varphi if and only if xx occurs free in ψ\psi and xvx\ne v. For every formula φ\varphi, every variable xx, and every term tt, the replacement φtx\varphi_t^x is defined as:
  • If φ\varphi is an atomic formula, then φtx\varphi_t^x is the expression formed by simply replacing every char xx to string tt.
  • If φ\varphi is (¬ψ)(\neg\psi), then (¬ψ)tx=(¬ψtx)(\neg\psi)^x_t=(\neg\psi^x_t).
  • If φ\varphi is (ψγ)(\psi\to\gamma), then (ψγ)tx=(ψtxγtx)(\psi\to\gamma)^x_t=(\psi^x_t\to\gamma^x_t)
  • If φ\varphi is yψ\forall y\psi, then $(\forall y\psi)^x_t= \begin{cases} \forall y(\psi^x_t),&\text{if } x\ne y;\\\\ \forall y\psi,&\text{if } x=y. \end{cases}$ And finally, we can now define the rule of zero-contradiction replacement:
  • For atomic formula φ\varphi, tt can always replace xx in φ\varphi without contradiction.
  • If φ\varphi is (¬ψ)(\neg\psi), then tt can replace xx in φ\varphi without contradiction if and only if tt can replace xx without contradiction in ψ\psi.
  • If φ\varphi is (ψγ)(\psi\to\gamma), then tt can replace xx in φ\varphi without contradiction if and only if tt can replace xx without contradiction in both ψ\psi and γ\gamma.
  • If φ\varphi is yψ\forall y\psi, then tt can replace xx in φ\varphi without contradiction if and only if
  1. xx doesn't occur free in φ\varphi, or
  2. yy doesn't occur in tt, and tt can replace xx in ψ\psi without contradiction. Now, by programming, Jack wants to judge whether xx is replaceable by the term tt without contradiction.

Input

At the beginning of the input section, there is an integer T(1T10)T(1\le T\le 10), indicating the number of test cases. For every test case, the first line gives a formula AA, which has length L(1L100)L(1\le L\le 100). In the input formula, ¬\neg will be replaced by char NN, \to will be replaced by char II, and \forall will be replaced by char AA. The next line consists of an integer mm, denoting the number of function and predicate. Then there followed mm lines, each line consists of a char and an integer, representing the parameter number of the function or predicate. The next line consists of an integer q(1q100)q(1\le q\le 100), denoting qq queries. There follow qq lines, each of which consists of a term tt and a variable xx, denoting the query of whether the term tt can replace the variable xx in the formula A without replacement. It is guaranteed that the length of string tt is no more than 100100.

Output

The output consists of qq sections. In each section, you need to print a character Y/NY/N to answer whether the variable is replaceable without contradiction. If the answer is yes, you should print the formula AA' after replacement.

Sample Input

1
AxAy(PzvfxygxyI(NQ))
5
f 3
g 2
P 3
Q 0
h 2
4
hxx x
hxy y
hzz z
hxz z

Sample Output

Y
AxAy(PzvfxygxyI(NQ))
Y
AxAy(PzvfxygxyI(NQ))
Y
AxAy(PhzzvfxygxyI(NQ))
N

Hint

The given formula is $\forall x\forall y(P(z, v, f(x, y, g(x, y)))\to(\neg Q))$. For the first and the second query, xx and yy don't occur free in the formula, because they are constrained by \forall. Thus the answer is the original formula. For the third query, zz occurs free in the formula, because it isn't constrained by \forall. Thus the answer is to replace zz by h(z,z)h(z, z). For the last query, zz occurs free in the formula, and after the replacement, xx in h(x,z)h(x, z) will be constrained by x\forall x, making the xx in h(x,z)h(x, z) no longer occur free. Thus the replacement is illegal.

Source

2023“钉耙编程”中国大学生算法设计超级联赛(9)