#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int N_MAX = 5002;
int p=998244353;
short c[5002][5002];
typedef long long ll;
int main()
{
  int h,w,k,hh,ww,i,j,l,t;
  ll m[N_MAX];
  char cc;
  cin >> h >> w >> k;
  vector<ll> res(h+w-1),b(h+w-1);
  for(i=0;i<h;i++)
    fill(c[i],c[i]+w,0);
  for(i=0;i<k;i++){
    cin >> hh >> ww >> cc;
    if(cc=='R')
      c[hh-1][ww-1]=1;
    else if(cc=='D')
      c[hh-1][ww-1]=2;
    else
      c[hh-1][ww-1]=3;
  }
  m[0]=1;
  for(i=1;i<=min(h,w)+1;i++)
    m[i]=(m[i-1]*3)%p;
  res[0]=1;
  for(l=0;l<h+w-2;l++){
    swap(res,b);
    fill(res.begin(),res.end(),0);
    t=0;
    for(i=0;i<=l;i++){
      j=l-i;
      if(i>=h) break;
      if(j>=w) continue;
      if(c[i][j]==0)
        t++;
    }
    for(i=0;i<=l;i++){
      j=l-i;
      if(i>=h) break;
      if(j>=w) continue;
      if(c[i][j]==1){
        if(j<w-1){
          res[i]+=b[i]*m[t];
          res[i]%=p;
        }
      }else if(c[i][j]==2){
        if(i<h-1){
          res[i+1]+=b[i]*m[t];
          res[i+1]%=p;
        }
      }else if(c[i][j]==3){
        if(j<w-1){
          res[i]+=b[i]*m[t];
          res[i]%=p;
        }
        if(i<h-1){
          res[i+1]+=b[i]*m[t];
          res[i+1]%=p;
        }
      }else{
        if(j<w-1){
          res[i]+=b[i]*m[t-1]*2;
          res[i]%=p;
        }
        if(i<h-1){
          res[i+1]+=b[i]*m[t-1]*2;
          res[i+1]%=p;
        }
      }
    }
  }
  if(c[h-1][w-1]==0){
    res[h-1]*=3;
    res[h-1]%=p;
  }
  cout << res[h-1] << endl;
  return 0;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1e9+7;

int solve_naive(const int n, const int s, const int k){
    int cnt = 0;
    ll now = s;
    while (now % n != 0 && cnt < n){
        now += k;
        cnt += 1;
    }
    if (cnt >= n){
        return -1;
    }
    return cnt;
}

ll _gcd(ll a, ll b){ return (a%b) ? _gcd(b, a%b) : b; }
pair<ll, ll> extgcd(ll a, ll b){
    // ax+by=gcd(a, b)なるx, yを求める
    if(a%b==0)return {0, 1}; // a*0+b*1=b
    const auto [x, y] = extgcd(b, a%b); // b*x + (a-a/b*b)*y = gcd(a, b)
    return {y, x-a/b*y}; // a*y + b*(x+a/b*y) = gcd(a, b)
}
ll powmod(ll x, ll n, ll p){
    // return (x ** n) mod p
    if (n==0)return 1;
    if(n==1)return x%p;
    const auto y = powmod(x, n/2, p);
    return (y*y%p)*powmod(x, n%2, p)%p;
}

int solve(int n, int s, int k){
    // s + a * k == 0 mod n なる最小の a を求める
    const auto g = _gcd(k, n);
    if(s%g!=0){
        // k, n が g の倍数なので s は g の倍数である必要がある
        return -1;
    }
    if(g>1){
        return solve(n/g, s/g, k/g);
    }
    // k*a == 1 mod n なる a を求める
    const auto [a, _] = extgcd(k, n);
    return ((n-s)*a%n+n)%n;
}

int main()
{
    int t;
    cin >> t;
    for(int ti=0; ti<t; ti++){
        int n,s,k;
        cin >> n >> s >> k;
        cout << solve(n, s, k) << endl;
    }
    return 0;
}