#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
string encrypt(string str, int alpha, int beta){
int size = str.size();
string ret = "";
for(int i=0;i<size;i++){
if('a' <= str[i] && str[i] <= 'z'){
ret += (alpha * (str[i] - 'a') + beta) % 26 + 'a';
}
}
//cout << str << " is encrypted to: "<< ret << endl;
return ret;
}
int main(void){
string thiss("this");
string that("that");
int n;
cin >> n;
cin.ignore(); //CR+LF
for(int i=0;i<n;i++){
vector<string> strv;
string str, tmp;
getline(cin, str);
int numwords;
istringstream iss(str);
int alpha, beta;
bool decided = false;
decided = false;
while(iss >> tmp){
strv.push_back(tmp);
}
numwords = strv.size();
for(int j=0;j<numwords;j++){
/* test */
//cout << "checking: " << strv[j] << endl;
/* */
if(strv[j].size() == 4){
if(strv[j][0] == strv[j][3]){// strv[j] is a candidate of that
//cout << "candidate of that" << endl;
beta = strv[j][2] - 'a';
//cout << "beta is " << beta << endl;
//calc alpha
int k=0;
while(strv[j][0] - 'a' +k*26 < beta || (k*26 + strv[j][0] - 'a' - beta) % 19 != 0){ // 19 == t
k++;
}
alpha = (k*26+(strv[j][0] - 'a' - beta)) / 19;
//cout << "alpha is " << alpha << endl;
string encrypted = "";
encrypted = encrypt("that", alpha, beta);
if(strv[j] == encrypted){
decided = true;
}
}else{// strv[j] is a candidate of this
//cout << "candidate of this" << endl;
tmp = strv[j];
if(tmp[0] < tmp[3]) tmp[0] += 26;
alpha = tmp[0] - tmp[3];
//cout << "alpha is " << alpha << endl;
if(tmp[3] - 'a' >= alpha * 18 % 26){
beta = tmp[3] - 'a' - alpha * 18 % 26;
}else{
beta = tmp[3] - 'a' + 26 - alpha * 18 % 26;
}
//cout << "beta is " << beta << endl;
string encrypted = encrypt("this", alpha, beta);
if(strv[j] == encrypted){
decided = true;
}
}
}
if(decided) break;
}
//decrypt
for(int j=0;j<str.size();j++){
int k=0;
if('a' <= str[j] && str[j] <= 'z'){
while(str[j]-'a' + k*26 < beta || (k*26 + str[j] - 'a' - beta) % alpha != 0) k++;
str[j] = ((k*26 + str[j] - 'a' - beta) / alpha) + 'a';
}
}
cout << str << endl;
}
return 0;
}
最終更新:2011年06月16日 23:35