Ginny’s birthday is coming soon. Harry Potter is preparing a birthday present for his new girlfriend. The present is a magic bracelet which consists of n magic beads. The are m kinds of different magic beads. Each kind of beads has its unique characteristic. Stringing many beads together a beautiful circular magic bracelet will be made. As Harry Potter’s friend Hermione has pointed out, beads of certain pairs of kinds will interact with each other and explode, Harry Potter must be very careful to make sure that beads of these pairs are not stringed next to each other.

There infinite beads of each kind. How many different bracelets can Harry make if repetitions produced by rotation around the center of the bracelet are neglected? Find the answer taken modulo 9973.


The first line of the input contains the number of test cases.

Each test cases starts with a line containing three integers n (1 ≤ n ≤ 109, gcd(n, 9973) = 1), m (1 ≤ m ≤ 10), k (1 ≤ k ≤ m(m − 1) ⁄ 2). The next k lines each contain two integers a and b (1 ≤ a, b ≤ m), indicating beads of kind a cannot be stringed to beads of kind b.


Output the answer of each test case on a separate line.

Sample Input

3 2 0
3 2 1
1 2
3 2 2
1 1
1 2
3 2 3
1 1
1 2
2 2
Sample Output


POJ Monthly–2006.07.30, cuiaoxiang

考虑如果旋转i个单位 那么会有gcd(i,n)个长度为n/gcd(i,n)的循环节


证明可以首先考虑如果要完整的置换我需要lcm(i,n) 那么lcm(i,n)/i就是置换所需要的次数


那么因为只和前一个有关 所以两维的dp即可 又因为n太大所以考虑矩阵优化即可

那么我们考虑sqrt时间枚举区间的个数那么显然区间个数这么多的区间有多少种呢 就是n/i种 即n/i种置换 所以像前面的套路做一下即可



辣鸡蒟蒻一枚qwq 欢迎加qq qwq 2922945330