题解见更高级的版本:http://blog.csdn.net/braketbn/article/details/50668414
#include <cstdio>
using namespace std;
typedef long long LL;
const int maxn = 1000001, p = 1000003;
int a, b, n, pow[maxn], phi[maxn], prime[maxn], cnt, fact[maxn], alpha[maxn], tot;
bool isnotprime[maxn];
inline int mul(int a, int b) {
return ((LL)a * b) % p;
}
inline int iread() {
int f = 1, x = 0; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return f * x;
}
void getphi() {
isnotprime[1] = 1; phi[1] = 1;
for(int i = 2; i < maxn; i++) {
if(!isnotprime[i]) prime[++cnt] = i, phi[i] = (i - 1) % p;
for(int j = 1; j <= cnt && i * prime[j] < maxn; j++) {
isnotprime[i * prime[j]] = 1;
if(i % prime[j] == 0) {
phi[i * prime[j]] = phi[i] * prime[j] % p;
break;
}
phi[i * prime[j]] = phi[i] * phi[prime[j]] % p;
}
}
}
void factor(int x) {
tot = 0;
for(int i = 1; i <= cnt && prime[i] * prime[i] <= n; i++) if(x % prime[i] == 0) {
++tot;
fact[tot] = prime[i];
for(alpha[tot] = 0; x % prime[i] == 0; x /= prime[i]) alpha[tot]++;
}
if(x > 1) ++tot, fact[tot] = x, alpha[tot] = 1;
}
int ans, L, g;
void dfs(int x, int d) {
if(d > tot) {
ans = (ans + mul(pow[x * g], phi[L / x])) % p;
return;
}
for(int i = 0; i <= alpha[d]; i++) {
dfs(x, d + 1);
x *= fact[d];
}
}
int gcd(int a, int b) {
for(; b; b ^= a ^= b ^= a %= b);
return a;
}
void exgcd(int a, int b, LL &x, LL &y) {
b ? (exgcd(b, a % b, y, x), y -= a / b * x) : (x = 1, y = 0);
}
int main() {
getphi();
pow[0] = 1; for(int i = 1; i < maxn; i++) pow[i] = (pow[i - 1] << 1) % p;
int T = iread();
while(T--) {
a = iread(); b = iread();
if(!a || !b) {
printf("0\n");
continue;
}
n = a + b;
g = gcd(a, b);
L = n / g; factor(L);
ans = 0;
dfs(1, 1);
LL x, y;
exgcd(L, p, x, y); x = (x % p + p) % p;
ans = mul(ans, x);
ans = pow[n] - ans; ans = (ans % p + p) % p;
printf("%d\n", ans);
}
return 0;
}