#include<bits/stdc++.h>
using namespace std;
using LL = long long;
#define pii pair<int, int>
#define F first
#define S second
const int maxn =5e5+10;
const int mod=1e9+7;
int q, a, b, a2, b2;
string s;
bitset<6> w; //QRBNKP
map<char, int> ctoi={
{'Q', 0}, {'R', 1}, {'B', 2}, {'N', 3}, {'K', 4}, {'P', 5} };
bool chQ(){
if(w[0]==0) return 0;
if(abs(a-a2)==abs(b-b2)) return 1;
if(a==a2 or b==b2) return 1;
return 0;
}
bool chR(){
if(w[1]==0) return 0;
if(a==a2 or b==b2) return 1;
return 0;
}
bool chB(){
if(w[2]==0) return 0;
if(abs(a-a2)==abs(b-b2)) return 1;
return 0;
}
bool chN(){
if(w[3]==0) return 0;
if(abs(a-a2)==1 and abs(b-b2)==2) return 1;
if(abs(a-a2)==2 and abs(b-b2)==1) return 1;
return 0;
}
bool chK(){
if(w[4]==0) return 0;
if(abs(a-a2)<=1 and abs(b-b2)<=1) return 1;
return 0;
}
bool chP(){
if(w[5]==0) return 0;
if(a2-a == 1 and b==b2) return 1;
return 0;
}
void solve(){
cin>>s>>a>>b>>a2>>b2;
if(a==a2 and b==b2){
cout<<0<<"\n";
return;
}///
w.reset();
for(char c: s) w[ctoi[c]]=1;
if(chQ() or chR() or chB() or chN() or chK() or chP()){
cout<<1<<"\n";
return;
}
if(w[0] or w[1]) cout<<2<<"\n";
else if(w[2] and (abs(a-a2)+abs(b-b2))%2==0) cout<<2<<"\n";
else if(w[4] and abs(a-a2) + abs(b-b2)<=2) cout<<abs(a-a2) + abs(b-b2)<<"\n";
// else if(w[2] and w[3]){
// }
else if(w[4]) cout<<abs(a-a2) + abs(b-b2)<<"\n";
else if(w[5] and b==b2 and a2>a) cout<<a2-a<<"\n";
else cout<<-1<<"\n";
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>q;
while(q--) solve();
return 0;
}