Xeno-archaeology (Large)

From tile positions and colors in infinite alternating square rings, find the fitting center nearest the origin or report the tiles as too damaged.

Hard8MathGeometryBrute forceNo attempts yetTime limit5sMemory limit512 MB

Problem

Long ago an alien civilization built a giant monument. The floor of the monument looked like this:

###############
#.............#
#.###########.#
#.#.........#.#
#.#.#######.#.#
#.#.#.....#.#.#
#.#.#.###.#.#.#
#.#.#.#.#.#.#.#
#.#.#.###.#.#.#
#.#.#.....#.#.#
#.#.#######.#.#
#.#.........#.#
#.###########.#
#.............#
###############

Each # is a red tile and each . is a blue tile. The block above is a 15 by 15 window of that floor, and the middle tile of the block sits at the center of the pattern. The pattern keeps the same rule in every direction, so treat the floor as infinite.

Today only a few tiles are left. Methane rain and dust storms destroyed the rest. Every tile sits at integer coordinates. Given the position and the color of each remaining tile, find the center of the pattern.

Input

The first line has one integer TT, the number of test cases. Each test case starts with a line holding NN, the number of remaining tiles. Each of the next NN lines holds XiX_i, YiY_i and the color of that tile, separated by spaces. The color is either # or ..

Limits

  • 1T501 \le T \le 50
  • 1N10001 \le N \le 1000
  • 1015Xi1015-10^{15} \le X_i \le 10^{15}
  • 1015Yi1015-10^{15} \le Y_i \le 10^{15}
  • The coordinates inside one test case are all different.

Output

For each test case print one line Case #c: X Y, where c is the case number starting from 1 and (X,Y)(X, Y) is the center of the pattern. If several centers explain the remaining tiles, print the one closest to (0,0)(0, 0) in Manhattan distance, which is the distance in x plus the distance in y. If several centers are still tied, print the one with the largest XX. If that is still a tie, print the one with the largest YY. If no center explains the tiles, print Case #c: Too damaged instead.