#include #include #include using namespace std; using lint = long long; #define rep(i,n) for(lint i=0;i<(n);++i) using mint=atcoder::static_modint<998244353>; int main(){ vectortable(1000000,1),inv(1000000); rep(i,1000000)table[i+1]=table[i]*(i+1); inv[999999]=table[999999].inv(); for(lint i=999998;i>=0;--i)inv[i]=inv[i+1]*(i+1); auto comb=[&](auto a,auto b){ return table[a]*inv[b]*inv[a-b]; }; lint t; cin>>t; while(t--){ lint h,w; cin>>h>>w; lint n=gcd(h,w); mint ans=mint(h)*w; if(n>3){ ans+=2*n*(h/n)*(w/n); } if(h*w/n%3==0){ if(n>4)ans+=n*12; } if(h%4==0&&w%4==0){ ans+=16; } if(n%2==0){ rep(p,2){ mint x=0; for(lint i=(n+3*p+5)%6+1;i<=n;i+=6){ if(0<=n/2-5*i){ x+=comb((n/2-5*i)/3+i-1,i-1)/i; } } ans+=x*x*n*n; } } cout<