#include<bits/stdc++.h>
using namespace std;

constexpr int64_t MAX_LEN = 200000;
set<int64_t> up[MAX_LEN + 1], right_st[MAX_LEN + 1], down[MAX_LEN + 1], left_st[MAX_LEN + 1];
set<int64_t> downs[MAX_LEN * 2 + 1], lefts[MAX_LEN * 2 + 1];

int main() {
    int64_t N;
    cin >> N;
    vector<int64_t> X(N), Y(N), dir_num(N);
    vector<char> U(N);

    map<char, int64_t> dirChar2dirNum = {
            { 'U', 0 },
            { 'R', 1 },
            { 'D', 2 },
            { 'L', 3 }
    };

    for (int64_t i = 0; i < N; i++) {
        cin >> X[i] >> Y[i] >> U[i];
        dir_num[i] = dirChar2dirNum[U[i]];
    }

    int64_t ans = LLONG_MAX;

    //まず正面衝突があるか調べる
    for (int64_t i = 0; i < N; i++) {
        if (U[i] == 'U') {
            up[X[i]].insert(Y[i]);
        } else if (U[i] == 'R') {
            right_st[Y[i]].insert(X[i]);
        } else if (U[i] == 'D') {
            down[X[i]].insert(Y[i]);
        } else if (U[i] == 'L') {
            left_st[Y[i]].insert(X[i]);
        }
    }

    //下向きの一番大きい位置と,上向きの一番小さい位置を比べて交差するかを検証
    for (int64_t x = 0; x <= MAX_LEN; x++) {
        if (down[x].empty() || up[x].empty()) {
            continue;
        }

        for (int64_t up_p : up[x]) {
            //これより大きいのがあれば一番近いやつを見る
            auto itr = down[x].lower_bound(up_p);
            if (itr != down[x].end()) {
                ans = min(ans, 5 * (*itr - up_p));
            }
        }
    }

    for (int64_t y = 0; y <= MAX_LEN; y++) {
        if (left_st[y].empty() || right_st[y].empty()) {
            continue;
        }

        for (int64_t right_p : right_st[y]) {
            auto itr = left_st[y].lower_bound(right_p);
            if (itr != left_st[y].end()) {
                ans = min(ans, 5 * (*itr - right_p));
            }
        }
    }
    
    //90度での衝突を調べる
    for (int64_t j = 0; j < 4; j++) {
        //downとleftの衝突だけを調べる
        //x + yが一致しているものがぶつかりそう
        for (int64_t xy = 0; xy <= MAX_LEN * 2; xy++) {
            downs[xy].clear();
            lefts[xy].clear();
        }

        for (int64_t i = 0; i < N; i++) {
            if (dir_num[i] == dirChar2dirNum['D']) {
                downs[X[i] + Y[i]].insert(Y[i]);
            } else if (dir_num[i] == dirChar2dirNum['L']) {
                lefts[X[i] + Y[i]].insert(Y[i]);
            }
        }

        //
        for (int64_t xy = 0; xy <= MAX_LEN * 2; xy++) {
            for (int64_t left_p : lefts[xy]) {
                auto itr = downs[xy].lower_bound(left_p);
                if (itr != downs[xy].end()) {
                    ans = min(ans, 10 * (*itr - left_p));
                }
            }
        }

        //90度回転
        for (uint64_t i = 0; i < X.size(); i++) {
            swap(X[i], Y[i]);
            Y[i] = MAX_LEN - Y[i];
            (++dir_num[i]) %= 4;
        }
    }

    if (ans == LLONG_MAX) {
        cout << "SAFE" << endl;
    } else {
        cout << ans << endl;
    }
}
#include <bits/stdc++.h>
using namespace std;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

using ll = long long;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1<<30;
const ll mod = 1000000007LL;

template<class T>struct dijkstra{//T=int or ll
  struct Node{
    vector<T> edges_to;
    vector<T> edges_cost;
    bool done;
    T cost=-1;
  };
  vector<Node> graph;
  void init(T N){
    graph.resize(N);
  }
  void add(T from, T to, T cost){
    graph[from].edges_to.push_back(to);
    graph[from].edges_cost.push_back(cost);
  }
  void shortest(T start){
    priority_queue<pair<T,T>,vector<pair<T,T>>,greater<pair<T,T>>> d;
    d.push(make_pair(0,start));
    while(!d.empty()){
      pair<T,T> p = d.top();
      d.pop();
      Node &point = graph[p.second];
      if(point.done) continue;
      point.done = true;
      point.cost = p.first;
      for(T i = 0; i < (T)point.edges_to.size(); i++){
        if(graph[point.edges_to[i]].done)continue;
        d.push(make_pair(point.cost+point.edges_cost[i],point.edges_to[i]));
      }
    }
  }
};

int main() {
  ll N;
  cin>>N;
  dijkstra<ll> D;
  D.init(N);
  for(ll i = 0; i < N-1; i++) {
    ll x,y,z;
    cin>>x>>y>>z;
    D.add(x-1,y-1,z);
    D.add(y-1,x-1,z);
  }
  ll Q,K;
  cin>>Q>>K;
  D.shortest(K-1);
  for(ll i = 0; i < Q; i++){
    ll x,y;
    cin>>x>>y;
    cout<<D.graph[x-1].cost+D.graph[y-1].cost<<endl;
  }
}