#include<bits/stdc++.h> using namespace std; #define maxn 100005 #define ll long long #define ls l,mid,rt<<1 #define rs mid+1,r,rt<<1|1 typedef pair<int,int> PII; const int mod = 1e9+7; int sum[maxn],N; struct Point{ int x,y; char D; }points[maxn]; bool cmp(Point A,Point B){ return A.y<B.y; } bool cmp1(Point A,Point B){ return A.x<B.x; } inline int lowbits(int x){ return x&-x; } void update(int x,int p){ for(;x<N;x+=lowbits(x))sum[x]+=p; } int getsum(int x){ int ans = 0; for(;x>0;x-=lowbits(x))ans+=sum[x]; return ans; } int main(){ int T;cin>>T; while(T--){ int n,m,k; scanf("%d %d %d",&n,&m,&k); for(int i=0;i<k;i++){ int x,y;char D; scanf("%d %d %c",&x,&y,&D); getchar(); points[i]=Point{x,y,D}; } sort(points,points+k,cmp); for(int i=0;i<k;i++)points[i].y=i+1; sort(points,points+k,cmp1); for(int i=0;i<=k;i++)sum[i]=0; ll ans=0; for(int i=0;i<k;i++){ if(points[i].D=='U')update(points[i].y+1,1); else if(points[i].D=='D'){ update(1,1); update(points[i].y,-1); } else if(points[i].D=='L') ans+=(ll)(getsum(points[i].y)); } N=k+10; for(int i=0;i<=N;i++)sum[i]=0; for(int i=k-1;i>=0;i--){ if(points[i].D=='U')update(points[i].y+1,1); else if(points[i].D=='D'){ update(1,1); update(points[i].y,-1); } else if(points[i].D=='R') ans+=(ll)(getsum(points[i].y)); } printf("%lld ",ans+1); } return 0; }