Bracket Strings

No attempts yetTime limit10sMemory limit512 MB

Problem

A string made only of ( and ) is a bracket string. Among bracket strings, a correct bracket string is defined as follows.

  1. The empty string is a correct bracket string.
  2. If a string SS is a correct bracket string, then (S)(S) is also a correct bracket string.
  3. If strings SS and TT are correct bracket strings, then STST is also a correct bracket string.

A bracket string that is not correct is called an incorrect bracket string. (())(), ()()(), (()()) are correct bracket strings, and ())((), (, (()()() are incorrect bracket strings.

A string SS is periodic if some prefix of SS that is neither empty nor SS itself is also a suffix of SS. A string with no such prefix is aperiodic. abcabcab, ()(, aaaaaa are periodic, and abcd, (())(), a are aperiodic.

Write a program that counts the bracket strings of length LL satisfying the given conditions.

Input

The first line contains four integers pp, qq, mm, QQ separated by spaces. (0p10 \le p \le 1, 0q10 \le q \le 1, 1m1061 \le m \le 10^6, 1Q1051 \le Q \le 10^5)

Each of the next QQ lines contains one natural number LL. (1L1061 \le L \le 10^6)

Let ALA_L be the set of all bracket strings of length LL, let BLB_L be the set of incorrect bracket strings of length LL, and let CLC_L be the set of aperiodic bracket strings of length LL. If p=0p = 0 then P=ALP = A_L, and if p=1p = 1 then P=BLP = B_L. If q=0q = 0 then R=ALR = A_L, and if q=1q = 1 then R=CLR = C_L.

In other words, for p=0p = 0, q=0q = 0 count the bracket strings of length LL; for p=1p = 1, q=0q = 0 count the incorrect ones; for p=0p = 0, q=1q = 1 count the aperiodic ones; for p=1p = 1, q=1q = 1 count the ones that are incorrect and also aperiodic.

Output

For each LL, print PR|P \cap R| modulo mm on its own line. Print QQ lines in the order the lengths are given.

Note

Among the bracket strings of length 4, exactly two are correct, ()() and (()), so 162=1416 - 2 = 14 of them are incorrect. The aperiodic bracket strings of length 4 are (((), (()), ())), )(((, ))((, )))(, six in total, and only (()) among them is correct. So 5 bracket strings of length 4 are incorrect and aperiodic at the same time.