#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef long double ld;
typedef pair<int, int> P;

ostream& operator<<(ostream& os, const modint& a) {os << a.val(); return os;}
template <int m> ostream& operator<<(ostream& os, const static_modint<m>& a) {os << a.val(); return os;}
template<typename T> istream& operator>>(istream& is, vector<T>& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;}
template<typename U, typename T> ostream& operator<<(ostream& os, const pair<U, T>& p){os << p.first << ' ' << p.second << "\n"; return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;}
template <typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;}

using mint = modint998244353;
void solve(){
	int n, m;
	cin >> n >> m;
	auto c2 = [&](int k){
		if(k <= 3) return (mint)0;
		return (mint)k * (k - 3) / 2;
	};
	vector<mint> dp(3);
	dp[0] = 1;
	dp[1] = n;
	dp[2] = c2(n);
	rep(i, m - 1){
		vector<mint> dp_old(3);
		swap(dp, dp_old);
		dp[0] = dp_old[0] + dp_old[1] + dp_old[2];
		dp[1] = dp_old[0] * n + dp_old[1] * (n - 1) + dp_old[2] * (n - 2);
		dp[2] += dp_old[0] * c2(n);
		dp[2] += dp_old[1] * ((mint)(n - 1) * (n - 2) / 2 - (n - 2));
		if(n >= 4) dp[2] += dp_old[2] * ((mint)(n - 2) * (n - 3) / 2 - (n - 4));
	}
	mint ans = dp[0] + dp[1] + dp[2];
	cout << ans << "\n";
}

int main(){
	int t;
	cin >> t;
	rep(i, t) solve();
	
	return 0;
}