#include <stdio.h>
#include <string.h>

int al[] = { 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 };
int alen = 11;

int affine(int a, int b, char y){
	return (a*(y-'a') + b) % 26;
}

int affine(int a, int b, char *d, char *s, int len){
	for(int i=0;i<len; ++i){
		if(s[i] == ' '){
			d[i] = ' ';
		}else{
			d[i] = affine(a, b, s[i]) + 'a';
		}
	}
	d[len] = '\0';
	return 0;
}

bool is_true(char *x, int *a, int *b){
	char tmp[5];
	if(x[0] == x[3]){
		*b = x[2] - 'a';
		*b += *b >= 0 ? 0 : 26;
		for(int i=0; i<alen; ++i){
			if(x[0] - 'a' == affine(al[i], *b, 't')){
				*a = al[i];
				affine(*a, *b, tmp, "that", 4);
				if(strcmp(tmp, x) == 0){
					return true;
				}else{
					return false;
				}
			}
		}
	}else{
		for(int i=0; i<alen; ++i){
			*b = ((x[0]-'a') - ('t'-'a') * al[i]) % 26;
			*b += *b >= 0 ? 0 : 26;
			affine(al[i], *b, tmp, "this", 4);
			if(strcmp(tmp, x) == 0){
				return true;
			}
		}
	}
	return false;
}

void raffine(char *dest, const char *source, int a, int b, int len){
	int table[26];
	for(int i=0; i<26; ++i){
		table[affine(a, b, i+'a')] = i + 'a';
	}
	for(int i=0; i<len; ++i){
		if(source[i] == ' ') { 
			dest[i] = ' ';
		}else{
			dest[i] = table[source[i]-'a'];
		}
	}
	dest[len] = '\0';
}

int main(void){
	int n;
	char in[257];
	char cin[257];
	char *tmp;
	int len;
	int a=0, b=0;
	bool o = false;

	scanf("%d", &n);
	scanf("%c", in);
	for(int i=0; i<n; ++i){
		fgets(in, 256, stdin);
		len = strlen(in);
		in[--len] = '\0';

		//a = al[3];
		//b = 0;
		//affine(a, b, cin, in, len);
		//puts(cin);
		//strcpy(in, cin);
		strcpy(cin, in);

		o = false;
		tmp = strtok(cin, " ");
		if(strlen(tmp) == 4){
			if(is_true(tmp, &a, &b)){
				raffine(cin, in, a, b, len);
				o = true;
			}
		}
		while(o==false && (tmp = strtok(NULL, " ")) != NULL){
			if(strlen(tmp) == 4 && is_true(tmp, &a, &b)){
				raffine(cin, in, a, b, len);
				o = true;
			}
		}
		
		puts(cin);
	}
	//getchar();getchar();
	return 0;
}