#include <bits/stdc++.h>
using namespace std;
string s;
int bfs(int ac, int db) {
queue<pair<pair<int, int>, int>> q;
q.push({{0, 0}, 0});
bool visited[61][61];
memset(visited, false, sizeof(visited));
visited[30][30] = true;
while (!q.empty()) {
auto curr = q.front(); q.pop();
int r = curr.first.first;
int c = curr.first.second;
int d = curr.second;
if (r == ac && c == db) return d;
if (d >= 15) continue;
if (s.find('K') != string::npos) {
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
if (i || j) {
int nr = r + i, nc = c + j;
if (abs(nr) <= 30 && abs(nc) <= 30 && !visited[nr + 30][nc + 30]) {
visited[nr + 30][nc + 30] = true;
q.push({{nr, nc}, d + 1});
}
}
}
if (s.find('N') != string::npos) {
int dr[] = {2, 2, -2, -2, 1, 1, -1, -1}, dc[] = {1, -1, 1, -1, 2, -2, 2, -2};
for (int i = 0; i < 8; i++) {
int nr = r + dr[i], nc = c + dc[i];
if (abs(nr) <= 30 && abs(nc) <= 30 && !visited[nr + 30][nc + 30]) {
visited[nr + 30][nc + 30] = true;
q.push({{nr, nc}, d + 1});
}
}
}
if (s.find('P') != string::npos) {
int nr = r + 1, nc = c;
if (abs(nr) <= 30 && abs(nc) <= 30 && !visited[nr + 30][nc + 30]) {
visited[nr + 30][nc + 30] = true;
q.push({{nr, nc}, d + 1});
}
}
}
return -1;
}
int solve() {
int a, b, c, d;
cin >> s >> a >> b >> c >> d;
int ac = c - a;
int db = d - b;
if (ac == 0 && db == 0) return 0;
bool Q = s.find('Q') != string::npos;
bool R = s.find('R') != string::npos || Q;
bool B = s.find('B') != string::npos || Q;
bool N = s.find('N') != string::npos;
bool K = s.find('K') != string::npos;
bool P = s.find('P') != string::npos;
if (R && (ac == 0 || db == 0)) return 1;
if (B && abs(ac) == abs(db)) return 1;
if (N && ((abs(ac) == 1 && abs(db) == 2) || (abs(ac) == 2 && abs(db) == 1))) return 1;
if (K && max(abs(ac), abs(db)) == 1) return 1;
if (P && ac == 1 && db == 0) return 1;
if (R) return 2;
if (B && (abs(ac + db) % 2 == 0)) return 2;
return bfs(ac, db);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int t;
cin >> t;
while (t--) cout << solve() << "\n";
return 0;
}