题解:http://bestcoder.hdu.edu.cn/blog/
多校6 HDU5793 A Boring Question // #pragma comment(linker, "/STACK:102c000000,102c000000")
// #include <conio.h>
#include <bits/stdc++.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
const double inf = 0x3f3f3f3f;
#define lson l,mid,rt<<1
// #define rson mid+1,r,rt<<1|1
const int N = ;
const int MOD = 1e9+;
#define LL long long
#define LB long double
// #define mi() (l+r)>>1
double const pi = acos(-);
const double eps = 1e-;
void fre(){freopen("in.txt","r",stdin);}
void freout(){freopen("out.txt","w",stdout);}
inline int read(){int x=,f=;char ch=getchar();while(ch>''||ch<'') {if(ch=='-') f=-;ch=getchar();}while(ch>=''&&ch<='') { x=x*+ch-'';ch=getchar();}return x*f;} LL q_MOD(LL a,LL n)
{
LL ans=;
while(n){
if(n&)ans=ans*a%MOD;
n>>=;
a=a*a%MOD;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
LL n,m;
scanf("%LLd%LLd",&n,&m);
printf("%LLd\n",(q_MOD(m,n+)-)*q_MOD(m-,MOD-)%MOD);
}
return ;
}