On of Vance’s favourite hero is Invoker, Kael. As many people knows Kael can control the elements and combine them to invoke a powerful skill. Vance like Kael very much so he changes the map to make Kael more powerful.
In his new map, Kael can control n kind of elements and he can put m elements equal-spacedly on a magic ring and combine them to invoke a new skill. But if a arrangement can change into another by rotate the magic ring or reverse the ring along the axis, they will invoke the same skill. Now give you n and m how many different skill can Kael invoke? As the number maybe too large, just output the answer mod 1000000007.
Input
The first line contains a single positive integer T( T <= 500 ), indicates the number of test cases.
For each test case: give you two positive integers n and m. ( 1 <= n, m <= 10000 )
Output
For each test case: output the case number as shown and then output the answer mod 1000000007 in a line. Look sample for more information.
Sample Input
2
3 4
1 2
Sample Output
Case #1: 21
Case #2: 1
Hint
For Case #1: we assume a,b,c are the 3 kinds of elements.
Here are the 21 different arrangements to invoke the skills
/ aaaa / aaab / aaac / aabb / aabc / aacc / abab /
/ abac / abbb / abbc / abcb / abcc / acac / acbc /
/ accc / bbbb / bbbc / bbcc / bcbc / bccc / cccc /
#include<iostream>
#include<cstdio>
#define mod 1000000007
using namespace std;
int gcd(int x,int y)
{
return x%y==0?y:gcd(y,x%y);
}
long long P(long long a,long long b)
{
long long ans=1;
while(b)
{
if(b&1)
ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
int main()
{
int t;
int n,m;
scanf("%d",&t);
int ca=1;
while(t--)
{
scanf("%d%d",&m,&n);
long long ans=0;
for(int i=1;i<=n;i++)
ans=(ans+P(m,gcd(i,n)))%mod;
if(n&1)
ans=(ans+P(m,n/2+1)*n)%mod;
else
{
ans=(ans+P(m,n/2)*n/2)%mod;
ans=(ans+P(m,n/2+1)*n/2)%mod;
}
printf("Case #%d: %lld\n",ca++,ans*P(2*n,mod-2)%mod);
}
}