402928: GYM100952 E Arrange Teams

Memory Limit:64 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

E. Arrange Teamstime limit per test2 secondsmemory limit per test64 megabytesinputstandard inputoutputstandard output

Syrian Collegiate Programming Contest (SCPC) is the qualified round for the Arab Collegiate Programming Contest. Each year SCPC organizers face a problem that wastes a lot of time to solve it, it is about how should they arrange the teams in the contest hall.

Organizers know that they have t teams and n*m tables allocated in n rows and m columns in the contest hall. They don't want to put teams in a random way. Each year SCPC chief judge puts a list of paired teams (list of a,b teams) that should not sit next to each other (because they are so good or so bad!).

if pair (a,b) is in chief judge list, this means that:

- team number a should not sit in-front of team number b

- team number b should not sit in-front of team number a

- team number a should not sit right to team number b

- team number b should not sit right to team number a

Organizers wastes a lot of time to find a good team arrangement that satisfy all chief judge needs. This year they are asking you to write a program that can help them.

Input

First line contains number of test cases. The first line in each test case contains three numbers: (1  ≤  n,m  ≤  11) and (1  ≤  t  ≤  10). Second line in each test case contains (0  ≤  p  ≤  40) number of pairs. Then there are p lines, each one of them has two team numbers a and b (1  ≤  a,b  ≤  t) where a is different than b.

Output

For each test case, print one line contains the total number of teams arrangements that satisfy all chief judge needs (We guarantee that it will be less than 9,000,000 for each test case). If there is no suitable arrangements print "impossible".

ExamplesInput
2
1 3 2
1
1 2
2 2 4
2
1 2
1 3
Output
2
impossible
Note

In test case 1 there are 2 teams and 3 tables in one row at the contest hall. There are only one pair (1,2), so there are 2 solutions:

team1 then empty table then team2

team2 then empty table then team1

In test case 2 there are 4 tables in 2 rows and 2 columns, and there are 4 teams. There is no arrangement that can satisfy chief judge needs.

加入题单

算法标签: