由于素数P很小,故先打阶乘的余数表(小于P的所有正数阶乘对P的余数表)。
/*
* soj3252.c
*
* Created on: 2011-10-10
* Author: bjfuwangzhu
*/
#include<stdio.h>
#define nmod 10007
int num[nmod];
void init() {
int i;
for (i = 1, num[0] = 1; i < nmod; i++) {
num[i] = num[i - 1] * i % nmod;
}
}
int modular_exp(int a, int b) {
int res, temp;
res = 1 % nmod,temp = a % nmod;
while (b) {
if (b & 1) {
res = res * temp % nmod;
}
temp = temp * temp % nmod;
b >>= 1;
}
return res;
}
int C(int a, int b) {
int res;
if (b > a) {
return 0;
}
b = num[b] * num[a - b] % nmod;
a = num[a];
res = modular_exp(b, nmod - 2);
res = res * a % nmod;
return res;
}
void solve(int n, int m) {
int res, a, b;
res = 1;
while (n || m) {
a = n % nmod,b = m % nmod;
res = res * C(a, b) % nmod;
n /= nmod,m /= nmod;
}
printf("%d\n", res);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int t, n, m;
init();
scanf("%d", &t);
while (t--) {
scanf("%d %d", &n, &m);
solve(n, m);
}
return 0;
}