#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <cmath>
#include <complex>
#include <map>
using namespace std;

#define rep(i,n) for(int i=0;i<int(n);++i)

static const double PI=6*asin(0.5);
typedef complex<double> P;
typedef long long ll;

int n;
vector<string> s;
vector<string> st;
string t;
vector<int> a;
vector<int> b;
int alpha=-1,beta=-1;

int afin(char s,int u,int t);

int gcd(int x,int y){
	if(y==0) return x;
	return gcd(y,x%y);
}
void atis(string st,int al,int be){
	if(afin(st[2],al,be)==0&&afin(st[3],al,be)==19){
		alpha=al;
		beta=be;
		//cout<<alpha<<" "<<beta<<endl;
	}
	if(afin(st[2],al,be)==8&&afin(st[3],al,be)==18){
		alpha=al;
		beta=be;
		//cout<<alpha<<" "<<beta<<endl;
	}
}

bool th(string st){
	int al,be;
	rep(i,a.size()){
		rep(j,b.size()){
			if(afin(st[0],a[i],b[j])==19&&afin(st[1],a[i],b[j])==7){
				al=a[i];
				be=b[j];
				atis(st,al,be);
				//cout<<al<<" "<<be<<endl;
			}
		}
	}
}
//abcdefghijklmnopqrstuvwxyz
//012345678901234567890123456
			


int afin(char s,int u,int t){
	int c=s-'a';
	//cout<<s<<" "<<c<<endl;
	return (u*c+t)%26;
}

int main(){
	cin>>n;
	//string thth="thth";
	//printf("%d %d\n",thth[0],thth[1]);
	rep(i,500){
		if(gcd(i+1,26)==1){
			a.push_back(i+1);
			//cout<<i+1<<endl;
		}
	}
	rep(i,26) b.push_back(i);
	rep(k,n){
		alpha=-1;
		beta=-1;
		s.clear();
		st.clear();
		
		
		int i=0;
		char tmp[300]={0};
		string tm;
		string tt;
		char tmtm;
		gets(tmp);
		//cout<<1<<" "<<tmp<<endl;
		gets(tmp);
		//cout<<2<<" "<<tmp<<endl;
		//cout<<tmp<<endl;
		/*
		rep(i,300){
			printf("%c ",tmp[i]);
			if(tmp[i]==0) break;
		}
		cout<<endl;
		rep(i,300){
			printf("%d ",tmp[i]);
			if(tmp[i]==0) break;
		}
		cout<<endl;
		*/
		rep(i,300){
			//printf("%d\n",tmp[i]);
			if(!(tmp[i]>='a'&&tmp[i]<='z')&&tmp[i]!=' '){
				s.push_back(tm);
				//cout<<""<<endl;
				break;
			}
			else if(tmp[i]==' '){
				s.push_back(tm);
				//cout<<tm<<endl;
				tm.clear();
			}
			else{
				tt=tmp[i];
				tm+=tt;
			}
		}
		rep(i,s.size()){
			//cout<<s[i]<<endl;
		}
		
		
		//gets(tmp);
		//cout<<3<<" "<<tmp<<endl;
		
		//cout<<tmp;
		
		rep(i,s.size()){
			if(s[i].size()==4){
				st.push_back(s[i]);
				//cout<<s[i]<<endl;
			}
		}
		rep(i,st.size()){
			th(st[i]);
			//if(alpa>0) cout<<"alpa="<<alpa<<" beta="<<beta<<endl;
		}
		
		rep(i,s.size()){
			rep(j,s[i].size()){
				printf("%c",afin(s[i][j],alpha,beta)+'a');
			}
			
			if(i!=s.size()-1)	cout<<" ";
			else cout<<endl;
		}
		s.clear();
		//cout<<"hoge"<<endl;
	}
	return 0;
}