#line 1 "Main.cpp" #include #include #include #include #include #include #line 3 "nachia\\math\\combination.hpp" namespace nachia{ template class Comb{ private: std::vector F; std::vector iF; public: void extend(int newN){ int prevN = (int)F.size() - 1; if(prevN >= newN) return; F.resize(newN+1); iF.resize(newN+1); for(int i=prevN+1; i<=newN; i++) F[i] = F[i-1] * Modint::raw(i); iF[newN] = F[newN].inv(); for(int i=newN; i>prevN; i--) iF[i-1] = iF[i] * Modint::raw(i); } Comb(int n = 1){ F.assign(2, Modint(1)); iF.assign(2, Modint(1)); extend(n); } Modint factorial(int n) const { return F[n]; } Modint invFactorial(int n) const { return iF[n]; } Modint invOf(int n) const { return iF[n] * F[n-1]; } Modint comb(int n, int r) const { if(n < 0 || n < r || r < 0) return Modint(0); return F[n] * iF[r] * iF[n-r]; } Modint invComb(int n, int r) const { if(n < 0 || n < r || r < 0) return Modint(0); return iF[n] * F[r] * F[n-r]; } Modint perm(int n, int r) const { if(n < 0 || n < r || r < 0) return Modint(0); return F[n] * iF[n-r]; } Modint invPerm(int n, int r) const { if(n < 0 || n < r || r < 0) return Modint(0); return iF[n] * F[n-r]; } Modint operator()(int n, int r) const { return comb(n,r); } }; } // namespace nachia #line 8 "Main.cpp" using namespace std; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; #define rep(i,n) for(int i=0; i<(int)(n); i++) const i64 INF = 1001001001001001001; using Modint = atcoder::static_modint<998244353>; int GCD(int a, int b){ return b ? GCD(b,a%b) : a; } nachia::Comb comb(300000); void testcase(){ int H, W; cin >> H >> W; Modint ans = 0; ans += Modint(H) * W; int G = GCD(H,W); if(G > 3) ans += Modint(G) * 2; if(G > 4 && G%3 == 0) ans += Modint(G) * 8; if(G % 4 == 0) ans += 16; if(G % 2 == 0){ int g = G/2; Modint parity[2] = {}; for(int n=1; n*5<=g; n++){ int m = (g - n*5) / 3; if(n*5 + m*3 == g){ parity[n%2] += comb(n+m-1, m-1) * 3; parity[n%2] += comb(n+m-1, m) * 5; } } for(int t=0; t<2; t++) ans += parity[t] * parity[t] * 4; } cout << ans.val() << '\n'; } int main(){ int T; cin >> T; rep(t,T) testcase(); return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;