#include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

const int INF = INT_MAX / 4;

void catMove(const vector<vector<int> >& edges, int start, vector<bool>& move)
{
    int n = edges.size();
    move.assign(n, false);
    move[start] = true;
    queue<int> q;
    q.push(start);

    while(!q.empty()){
        int s = q.front();
        q.pop();
        for(unsigned i=0; i<edges[s].size(); ++i){
            int t = edges[s][i];
            if(!move[t]){
                move[t] = true;
                q.push(t);
            }
        }
    }
}

void humanMove(const vector<vector<int> >& edges, const vector<bool>& start, vector<int>& cost)
{
    int n = edges.size();
    cost.assign(n, INF);
    queue<int> q;
    for(int i=0; i<n; ++i){
        if(start[i]){
            cost[i] = 0;
            q.push(i);
        }
    }

    while(!q.empty()){
        int s = q.front();
        q.pop();
        for(unsigned i=0; i<edges[s].size(); ++i){
            int t = edges[s][i];
            if(cost[t] == INF){
                cost[t] = cost[s] + 1;
                q.push(t);
            }
        }
    }
}

int main()
{
    int d;
    cin >> d;

    while(--d >= 0){
        int n, m, sh, sc;
        cin >> n >> m >> sh >> sc;

        vector<vector<int> > edgesH(n+1), edgesC(n+1);
        for(int i=0; i<m; ++i){
            int a, b;
            char c;
            cin >> a >> b >> c;

            if(c == 'N'){
                edgesH[a].push_back(b);
                edgesH[b].push_back(a);
            }else{
                edgesC[a].push_back(b);
                edgesC[b].push_back(a);
            }
        }

        vector<bool> move1, move2;
        catMove(edgesC, 0, move1);
        catMove(edgesC, sc, move2);

        vector<bool> move3(n+1);
        move3[sh] = true;

        if(move1[sc]){
            cout << 0 << endl;
            continue;
        }

        vector<int> cost1, cost2, cost3;
        humanMove(edgesH, move1, cost1);
        humanMove(edgesH, move2, cost2);
        humanMove(edgesH, move3, cost3);

        int ret = INF;
        for(int i=0; i<=n; ++i)
            ret = min(ret, cost1[i] + cost2[i] + cost3[i]);
        cout << ret << endl;
    }

    return 0;
}