Under construction

Still being built. Things may change

In Queries

Time limit1sMemory limit128 MB

Summary
Maintain a 5-column table with seeded initial rows and process insert, remove, max, min, and range-count queries over the columns.
Level

Easy3 of 10

Topics
Array, Implementation, Simulation, Brute force
Solved
No attempts yet

Problem

A student computing club needs a software module that answers queries over tabular data, and they have asked for your help. All values are integers, stored in a table with exactly 5 columns.

For development, the initial table is generated by the following function:

f(r, c) = (a · f(r-1, c) + b · f(r, c-1) + x) mod m
f(r, c) = 0   for all r = 0 or c = 0

Here f(r, c) is the value in row r and column c, for r = 1 … n and c = 1 … 5. The values a, b, x, m, and n are seeds given in the input.

Your module must handle the following queries:

QueryActionExample
insert a b c d eAppend the row {a, b, c, d, e} as a new last row (a is column 1, b is column 2, …, e is column 5).insert 2 10 3 4 7
remove rDelete row r.remove 3
max cOutput the largest value in column c.max 3
min cOutput the smallest value in column c.min 1
range c a bOutput how many rows have a column-c value between a and b, inclusive.range 2 1 10

A remove deletes the given row but does not shift the rows below it: the row stays in place and only its data is cleared. If the target row is already empty or out of range, the command does nothing. An insert always appends a new last row, even when earlier rows are empty (previously deleted).

You may assume that no output query (max, min, or range) is ever issued while the table is empty.

Input

The first line contains an integer T, the number of test cases.

Each test case begins with five non-negative integers a, b, x, m (1≤m≤100001 \le m \le 10000) and n (0≤n≤1000000 \le n \le 100000) — the seeds used to generate the initial table. The next line contains a single integer q (0≤q≤1000 \le q \le 100), the number of queries. Each of the following q lines contains one query in one of the formats described above.

Output

For each test case, first print Case #X: on its own line, where X is the test case number (starting from 1). Then print the result of each output query (max, min, range) on its own line, in the order the queries appear.

Examples1

  1. Example 1

    Input
    3
    1 1 1 5 6
    9
    max 5
    remove 2
    remove 4
    min 2
    insert 2 4 6 3 10
    range 4 1 5
    insert 0 0 0 0 0
    remove 7
    max 3
    3 3 3 3 3
    2
    remove 1
    insert 1 1 1 1 1
    2 4 3 10 2
    6
    remove 1
    remove 2
    insert 2 2 2 2 2
    remove 1
    remove 10
    max 3
    
    Expected output
    Case #1:
    1
    0
    4
    4
    Case #2:
    Case #3:
    2