## Yet Another Contest 7 P2 - Coprime Grid

View as PDF

Points: 10 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type

Consider a grid with rows and columns, where each cell contains a positive integer. The grid is called coprime if every cell contains an integer which is coprime with at least two of the integers written in adjacent cells. Cells are considered adjacent if they share a side, so each cell is adjacent to at most four other cells. Also, two integers are consider coprime if no integer greater than divides both of the integers.

Can you find an coprime grid that contains each integer between and (inclusive) exactly once?

To ensure the integrity of your solution, there may be up to test cases.

#### Constraints  #### Input Specification

The first line contains a single integer .

Each of the following lines contains two space-separated integers, and .

#### Output Specification

For each test case, if no such grid exists, output -1. Otherwise, output lines, each containing space-separated integers, representing the integers contained in the cells of the grid. If there are multiple correct solutions, any will be accepted.

#### Sample Input

2
3 3
1 3

#### Sample Output

2 3 4
1 9 7
6 5 8
-1