Colorful Buildings

3

2 votes
Combinatorics, Mathematics, Combinatorics basics, Easy
Problem

There are  buildings in a row. Each of these buildings needs to be painted by one of the  colors. Buildings look beautiful only if no adjacent buildings are painted with the same color. Find the number of ways to paint these buildings such that they are beautiful. Since the number could be very large output it modulo .

Input Format

The first line of the input contains an integer , the total number of test cases.
Then  lines follow, each containing two space-separated integers  and , the total number of buildings and the number of colors available.

Output Format

For each test case output the number of ways to paint the buildings such that they are beautiful modulo .

Constraints



Sample Input
2
4 2
4 6
Sample Output
2
750
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Sample test case  Let A and B be the two available colors then there are just two possible ways of coloring the buildings as ABAB and BABA.

Editor Image

?