import math I=input J=int R=range T=J(I()) P=998244353 L=100001 f=[1]*L g=[1]*L for i in R(1,L): f[i]=(f[i-1]*i)%P g[i]=(g[i-1]*pow(i,-1,P))%P for t in R(T): X=I().split() H=J(X[0]) W=J(X[1]) a=H*W G=math.gcd(H,W) if G>3: a+=G*2 if G>4 and H*W//G%3==0: a+=G*12 if G%2==0: for i in R(2): s=0 r=(G+i*3)%6 j=6 if r==0 else r k=(G//2-j*5)//3 while k>=0: s+=(f[j-1+k]*((g[j]*g[k])%P))%P j+=6 k=(G//2-j*5)//3 s=(s%P)*G%P a+=s*s if G%4==0: a+=16 print(a%P)