Nasty Calculations

Time limit1sMemory limit128 MB

Summary
Evaluate a postfix arithmetic formula modulo B for up to 100000 given base-B values of x and print only the last digit each time.
Level

Medium5 of 10

Topics
Stack, Math, Implementation
Solved
No attempts yet

Problem

You are given a formula ff of a single variable xx, together with many values of xx. For each value you must evaluate f(x)f(x) and report only its last digit.

Because f(x)f(x) can be very large, its exact value is not needed — only its last digit. Every number in this task (the formula, each value of xx, and every answer) is written in the base-BB positional numeral system for a given integer BB.

Postfix notation

The formula is given in postfix notation, also known as Reverse Polish Notation (RPN). In ordinary infix notation an operator is written between its operands, e.g. 1 + 2, 1 + 2 * 3, or (1 + 2) * 3. In postfix notation the operator is written directly after its operands, so the same expressions become 1 2 +, 1 2 3 * +, and 1 2 + 3 *. RPN needs no parentheses, because the order of the operands and operators already determines the expression uniquely.

Base-BB numeral systems

In the base-BB system a digit string dkdk−1…d1d0d_k d_{k-1} \dots d_1 d_0 represents dkBk+dk−1Bk−1+⋯+d1B+d0d_k B^k + d_{k-1} B^{k-1} + \dots + d_1 B + d_0, where every digit did_i satisfies 0≤di≤B−10 \le d_i \le B - 1. Digits greater than 9 are written with uppercase letters: A = 10, B = 11, ..., Z = 35. For example, 29 in base 10 is written 45 in base 6 and 1D in base 16.

Input

The first line contains two integers BB and NN separated by a single space, where 2≤B≤362 \le B \le 36 is the base of the numeral system and 1≤N≤1000001 \le N \le 100000 is the number of values of xx.

The second line describes the formula ff in postfix notation as a sequence of elements separated by single spaces. Each element is one of:

  • A string of digits and uppercase letters, denoting a non-negative integer written in base BB (its value does not exceed 2000000000).
  • The lowercase letter x, standing for the variable.
  • + (addition), - (subtraction), or * (multiplication).

Each of the next NN lines contains one value of xx, written in base BB in the same way (its value does not exceed 2000000000).

The input is guaranteed to be valid: the second line is a well-formed postfix expression, and every string of digits and uppercase letters is a valid integer in base BB. The second line contains at most 100000 characters.

Output

Print exactly NN lines. The ii-th line must contain a single character — a digit or an uppercase letter — equal to the last digit, in base BB, of f(x)f(x) for the value of xx on the (i+2)(i + 2)-th line of the input. It is guaranteed that f(x)f(x) is non-negative for every given value of xx.

Examples7

  1. Example 1

    Input
    15 4
    2 x * 123A +
    1
    2
    3
    4
    
    Expected output
    C
    E
    1
    3
    
  2. Example 2

    Input
    2 3
    x x *
    10
    11
    1
    
    Expected output
    0
    1
    1
    
  3. Example 3

    Input
    10 3
    x x + x *
    5
    7
    3
    
    Expected output
    0
    8
    8
    
  4. Example 4

    Input
    7 3
    x x * 3 -
    2
    3
    5
    
    Expected output
    1
    6
    1
    
  5. Example 5

    Input
    36 3
    x x *
    Z
    10
    ZZ
    
    Expected output
    1
    0
    1
    
  6. Example 6

    Input
    16 3
    x ABCDEF +
    F
    1
    8
    
    Expected output
    E
    0
    7
    
  7. Example 7

    Input
    3 6
    x x + x +
    1
    2
    10
    11
    12
    100
    
    Expected output
    0
    0
    0
    0
    0
    0