Ping Pong Balls (Small)

Given two fixed displacement vectors, simulate the chain reaction from one starting trap and count how many distinct traps fire.

Easy3SimulationBFSQueueMatrixInterviewNo attempts yetTime limit5sMemory limit512 MB

Problem

A large room is filled with mousetraps arranged in a grid. Every mousetrap holds two ping pong balls, placed so that when the trap goes off the balls fly out, land on other mousetraps and set those off as well. The walls of the room are sticky, so a ball that reaches a wall is absorbed and does nothing.

Every mousetrap throws its two balls the same way. The flight of a ball is a fixed X displacement and Y displacement measured from the trap that launched it. You throw a single ping pong ball into the room. It hits a mousetrap, that trap goes off and launches two balls, those two balls set off two more mousetraps, four balls fly out, and the chain continues. When everything stops, many traps have gone off and some were never hit by any ball.

Count the mousetraps that go off.

One mousetrap sits at every grid position (x, y) with 0x<W0 \le x < W and 0y<H0 \le y < H, and (0, 0) is the bottom left trap. A ball landing on a trap that has already gone off does nothing.

The figure below shows a room of width 5 and height 3 where the two displacements are (-1, 0) and (-1, -1). The first ball hits the mousetrap at (4, 2), and 12 traps go off in the end.

Input

The first line contains the number of test cases, CC. Each test case is four lines. The first line has the width WW and the height HH of the mousetrap grid, which is also the size of the room. The next two lines give the flight of each of the two balls as an X displacement and a Y displacement. For example, if those two lines are 0 1 and 1 1, then a trap that goes off launches one ball onto the trap directly above it and one ball onto the trap above and to the right of it. The last line has two integers: the column and the row of the mousetrap that the first ball hits, where 0 0 is the bottom left trap.

Limits

  • 1C1001 \le C \le 100
  • 2W,H1002 \le W, H \le 100
  • every displacement is between 20-20 and 2020
  • neither displacement vector is (0,0)(0, 0)
  • the column is between 00 and W1W - 1, and the row is between 00 and H1H - 1

Output

For each test case print one line in the form Case #A: B, where AA is the 1-based number of the test case and BB is the number of mousetraps that go off, counting the first one.