#include using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = (ll)s; i < (ll)(t); i++) #define rrep(i, s, t) for (ll i = (ll)(t) - 1; i >= (ll)(s); i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define TT template template bool chmin(T1& x, T2 y) { return x > y ? (x = y, true) : false; } template bool chmax(T1& x, T2 y) { return x < y ? (x = y, true) : false; } struct io_setup { io_setup() { ios::sync_with_stdio(false); std::cin.tie(nullptr); cout << fixed << setprecision(15); srand(time(NULL)); } } io_setup; const ll MOD = 998; template struct Matrix { vector> A; Matrix(int n, int m) : A(n, vector(m, 0)) {} Matrix(int n) : A(n, vector(n, 0)) {} int height() const { return (A.size()); } int width() const { return (A[0].size()); } const vector& operator[](int k) const { return (A[k]); } vector& operator[](int k) { return (A[k]); } static Matrix I(int n) { Matrix mat(n); for (int i = 0; i < n; i++) mat[i][i] = 1; return mat; } Matrix& operator+=(const Matrix& B) { int n = height(), m = width(); assert(n == B.height() && m == B.width()); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { (*this)[i][j] += B[i][j]; } } return (*this); } Matrix& operator-=(const Matrix& B) { int n = height(), m = width(); assert(n == B.height() && m == B.width()); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { (*this)[i][j] -= B[i][j]; } } return (*this); } Matrix& operator*=(const Matrix& B) { int n = height(), m = B.width(), p = width(); assert(p == B.height()); vector> C(n, vector(m, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int k = 0; k < p; k++) { C[i][j] += (*this)[i][k] * B[k][j]; } } } A.swap(C); return (*this); } Matrix& operator^=(long long k) { int n = height(); assert(n == width()); Matrix B = Matrix::I(n); while (k > 0) { if (k & 1) B *= *this; *this *= *this; k >>= 1LL; } A.swap(B.A); return (*this); } Matrix operator+(const Matrix& B) const { return (Matrix(*this) += B); } Matrix operator-(const Matrix& B) const { return (Matrix(*this) -= B); } Matrix operator*(const Matrix& B) const { return (Matrix(*this) *= B); } Matrix operator^(const long long k) const { return (Matrix(*this) ^= k); } }; #include using mint = atcoder::modint; void solve() { ll N, M; cin >> N >> M; while (M--) { string K; cin >> K; Matrix mt(2); mt[0][0] = N, mt[0][1] = 1, mt[1][0] = 0, mt[1][1] = 1; Matrix I(2); rep(i, 0, 2) I[i][i] = 1; rrep(i, 0, (int)K.size()) { rep(t, 0, K[i] - '0') I *= mt; Matrix nxt(2); rep(t, 0, 2) nxt[t][t] = 1; rep(t, 0, 10) nxt *= mt; swap(mt, nxt); } mint ans = I[0][0] + I[0][1]; cout << ans.val() << "\n"; } } int main() { atcoder::modint::set_mod(998); int T; cin >> T; while (T--) { solve(); } }