# #925753 (PyPy3) No.2529 Treasure Hunter - yukicoder # https://yukicoder.me/submissions/925753 import sys from itertools import permutations from heapq import heappop,heappush from collections import deque import random import bisect input = lambda :sys.stdin.readline().rstrip() mi = lambda :map(int,input().split()) li = lambda :list(mi()) def cmb(n, r, mod): if ( r<0 or r>n ): return 0 return (g1[n] * g2[r] % mod) * g2[n-r] % mod mod = 998244353 N = 2*10**5 g1 = [1]*(N+1) g2 = [1]*(N+1) inverse = [1]*(N+1) for i in range( 2, N + 1 ): g1[i]=( ( g1[i-1] * i ) % mod ) inverse[i]=( ( -inverse[mod % i] * (mod//i) ) % mod ) g2[i]=( (g2[i-1] * inverse[i]) % mod ) inverse[0]=0 def solve(N,M): if N <= 3: dp = [1,0] for _ in range(M): dp = [(dp[0]+dp[1]) % mod,(dp[0]*N+dp[1]*(N-1)) % mod] return (dp[0] + dp[1]) % mod dp = [1,0,0] n = N for _ in range(M): # 解説 No.2529 Treasure Hunter - yukicoder --- 解说第2529期 宝藏猎人 - yukicoder # https://yukicoder.me/problems/no/2529/editorial # 任选 2 个,去掉相邻的情况(n 个) a = n*(n-1)//2-n # n-1 个任选 2 个,去掉相邻的情况 n-2*j b = (n-1)*(n-2)//2-(n-2) c = (n-2)*(n-3)//2-(n-4) dp = [ (dp[0]+dp[1]+dp[2]) % mod, (dp[0]*N + dp[1]*(N-1) + dp[2]*(N-2)) % mod, (dp[0]*a % mod + dp[1] * b % mod + dp[2] * c % mod) % mod ] return sum(dp) % mod for _ in range(int(input())): N,M = mi() print(solve(N,M))