Under construction

Still being built. Things may change

Fast Debugger

Time limit1sMemory limit2048 MB

Summary
Given a program of bitwise instructions on four 8-bit registers with nested repeat blocks, find the register values after the first k instructions.
Level

Hard8 of 10

Topics
Bit manipulation, Divide and conquer, Simulation
Solved
No attempts yet

Statement

Recently you received an old computer. It has an 8-bit CPU with four registers, ax, bx, cx, and dx, and supports only a few simple instructions. This guide is for readers who are not familiar with assembly language.

Each register is 8 bits, so its value is an integer in [0,255][0, 255].

The CPU supports three bitwise operations: or, and, and xor. Each operation has two kinds of instructions.

Type 1 takes two registers, written as or r1 r2, where r1 and r2 are each one of ax, bx, cx, or dx. The two may be the same register. It sets r1 to the bitwise or of r1 and r2. The and and xor instructions are written as and r1 r2 and xor r1 r2.

Type 2 takes one immediate operand, written as ori r imm, where r is a register and imm is a constant in [0,255][0, 255] given in the instruction. It sets r to the bitwise or of r and imm. The and and xor versions are andi r imm and xori r imm.

The assembler supports a simple loop. When it reads a repeat m statement, it runs the contents of the repeat block a total of mm times. The format is:

repeat m
<repeat block>
end

Here mm is a constant in [2,255][2, 255]. The <repeat block> has one or more statements, each either a bitwise instruction or a nested repeat-end statement.

You are writing a simulator for this CPU. You receive a valid program and qq queries. Each query gives five integers k,a0,b0,c0,d0k, a_0, b_0, c_0, d_0. At the start, ax =a0= a_0, bx =b0= b_0, cx =c0= c_0, and dx =d0= d_0. Output the values of the four registers after the program executes kk bitwise instructions.

Input

The first line contains two integers nn and qq (1≤n≤120001 \leq n \leq 12000, 1≤q≤100001 \leq q \leq 10000), the number of instructions and the number of queries.

The next nn lines each contain one instruction in the format described above.

Each of the next qq lines contains five integers k,a0,b0,c0,d0k, a_0, b_0, c_0, d_0 (1≤k≤1091 \leq k \leq 10^9, 0≤a0,b0,c0,d0≤2550 \leq a_0, b_0, c_0, d_0 \leq 255). The program is guaranteed not to terminate before it executes kk bitwise instructions.

Output

Print qq lines. The ii-th line contains four integers ak,bk,ck,dka_k, b_k, c_k, d_k, the values of ax, bx, cx, and dx after the program executes kk bitwise instructions.

Examples1

  1. Example 1

    Input
    6 2
    repeat 5
    xor ax bx
    xori ax 3
    and cx ax
    xor cx dx
    end
    10 1 2 4 3
    8 4 1 2 3
    
    Expected output
    0 2 2 3
    4 1 3 3