Go: package main

import (
	"bufio"
	"os"
	"fmt"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	var n int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		s := scanner.Text()
		rs := []rune(s)
		flag := false
		for b := 0; b <= 26; b++ {
			for a := 0; a <= 26; a++ {
				ans := rs
				if a%2 != 0 && a%13 != 0 {
					for j := 0; j < len(ans); j++ {
						if ans[j] > 'a' && ans[j] <= 'z' {
							ans[j] = 'a' + ('a'*(ans[j]-'a')+'b')%26
						}
					}
					sans := runeSlice2String(ans)
					if strings.Contains(sans, "that") || strings.Contains(sans, "this") {
						fmt.Println(sans)
						flag = true
						break
					}
				}
			}
			if flag {
				break
			}
		}
	}
}

func runeSlice2String(input []rune) string {
	tmp := make([]string, len(input))
	for i := 0; i < len(input); i++ {
		tmp[i] = string(input[i])
	}
	return strings.Join(tmp, "")
}

C++: import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

//Affine Cipher
public class AOJ0040 {

	static long gcd(long a, long b){
		if(a < b){
			long tmp = a;
			a = b;
			b = tmp;
		}
		while(b!=0){
			long r = a%b;
			a = b;
			b = r;
		}
		return a;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		sc.nextLine();
		Set<String> valid = new HashSet<String>();
		for(int a=1;a<=100;a++){
			if(gcd(a,26)>1)continue;
			for(int b=1;b<=100;b++){
				boolean f = true;
				boolean[] c = new boolean[26];
				for(int j=0;j<26;j++){
					int x = (a*j+b)%26;
					if(c[x])f=false;
					c[x] = true;
				}
				if(f)valid.add(a+" "+b);
			}
		}
		while(t--!=0){
			String[] s = sc.nextLine().split(" ");
			for(String vv:valid){
				String[] v = vv.split(" ");
				int a = Integer.parseInt(v[0]);
				int b = Integer.parseInt(v[1]);
				boolean f = false;
				StringBuilder sb = new StringBuilder();
				boolean head = true;
				for(String m : s){
					if(!head)sb.append(' ');
					head = false;
					char[] ch = new char[m.length()];
					for(int i=0;i<ch.length;i++)ch[i]=(char)(((m.charAt(i)-'a')*a+b)%26 + 'a');
					String x = new String(ch);
					if(x.equals("that")||x.equals("this"))f=true;
					sb.append(x);
				}
				if(f){
					System.out.println(sb);
					break;
				}
			}
		}
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int gcd(int m,int n){
  return n?gcd(n,m%n):m;
}
int main(void){
  int n;
  cin>>n;
  cin.ignore();
  while(n--){
    string line;
    stringstream ss;
    getline(cin,line);
    int decode[26];
    char t1[] = "that";
    char t2[] = "this";
    char test[] = "that";
    bool finde = false;
    for(int a = 3; a<=1126; a++){
      if(a==1126) puts("a");
      if(gcd(26,a)!=1) continue; 
      for(int b=0; b<26; b++){

        for(int i=0; i<26; i++){
          decode[i] = (a*i+b)%26;
        }
        
        for(int i=0;i<4; i++){
          test[i] = 'a' + decode[t1[i]-'a'];
        }
        if(line.find(test)!=string::npos){
          //cout<<test<<endl;
          finde = true;
          break;
        }
        for(int i=0;i<4; i++){
          test[i] = 'a' + decode[t2[i]-'a'];
        }
        if(line.find(test)!=string::npos){
          //cout<<test<<endl;
          finde = true;
          break;
        }
      }
      if(finde) break;
    }
    ss<<line;
    bool f = false;
    while(ss>>line){
      if(f) cout<<" ";
      f = true;
      for(int i=0;i<(int)line.size();i++){
        for(int j=0; j<26; j++){
          if(decode[j]+'a'==line[i]){
            line[i] = 'a' + j;
            break;
          }
        }
      }
      cout<<line;
    }
    cout<<endl;
    //for(int i=0;i<26; i++) cout<<decode[i]<<" ";
  } 
  return 0;
}

C++: #include <iostream>
#include <array>
#include <string>
#include <vector>
#include <cstdint>

uint32_t
search_this_and_that(uint32_t const a, std::vector<std::string> const& len4words)
{
	using namespace std;

	char const ciphered_t = ('t' - 'a') * a % 26;
	char const ciphered_h = ('h' - 'a') * a % 26;
	char const ciphered_a = ('a' - 'a') * a % 26;
	char const ciphered_i = ('i' - 'a') * a % 26;
	char const ciphered_s = ('s' - 'a') * a % 26;

	for (string const& word : len4words) {
		if ((word[0] + 26 - word[1]) % 26 == (ciphered_t + 26 - ciphered_h) % 26) {
			if (word[0] == word[3] &&
					(word[1] + 26 - word[2]) % 26 == (ciphered_h + 26 - ciphered_a) % 26) {
				return (word[0] - 'a' + 26 - ciphered_t) % 26;
			}
			else if ((word[1] + 26 - word[2]) % 26 == (ciphered_h + 26 - ciphered_i) % 26 &&
					(word[2] + 26 - word[3]) % 26 == (ciphered_i + 26 - ciphered_s) % 26) {
				return (word[0] - 'a' + 26 - ciphered_t) % 26;
			}
		}
	}
	return 26;
}

int main()
{
	using namespace std;

	string input;
	getline(cin, input);
	uint32_t const n = stoul(input);

	for (uint32_t i = 0; i < n; ++i) {
		getline(cin, input);

		vector<string> len4words;
		string::size_type pos = 0;
		string::size_type next_pos = 0;
		while ((next_pos = input.find(' ', pos)) != string::npos) {
			if (next_pos - pos == 4) {
				len4words.push_back(input.substr(pos, 4));
			}
			pos = next_pos + 1;
		}
		if (input.size() - pos == 4) {
			len4words.push_back(input.substr(pos, 4));
		}

		for (uint32_t a = 1; a < 26; a += 2) {
			if (a == 13) {
				continue;
			}

			uint32_t const b = search_this_and_that(a, len4words);
			if (b != 26) {
				std::array<char, 26> ciphered_to_org;
				for (char c = 'a'; c < 'z' + 1; ++c) {
					ciphered_to_org[(a * (c - 'a') + b) % 26] = c;
				}
				for (char c : input) {
					if (c == ' ') {
						cout << c;
					}
					else {
						cout << ciphered_to_org[c - 'a'];
					}
				}
				cout << endl;
				break;
			}
		}
	}
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>

using namespace std;

char affine(int a, int b, char c)
{
    if(c == ' ') return c;
    else return (a*(c-'a')+b)%26+'a';
}
int gcd(int a, int b)
{
    if(a<b) swap(a,b);
    while(b!=0){
        int t = a%b;
        a = b;
        b = t;
    }
    return a;
}
int main()
{
    int n;
    string str;
    cin >> n;
    getline(cin, str);
    while(n)
    {
        bool f=0;
        getline(cin, str);
        string ss="";
        for(int i=0;i<100&&!f;++i)
        {
            for(int j=0;j<26&&!f;++j)
            {
                if(gcd(i,j)!=1) continue;
                for(int k=0;k<str.length();++k){
                    ss += affine(i,j,str[k]);
                }
                if(ss.find("that")!=string::npos ||
                   ss.find("this")!=string::npos)
                {
                    cout << ss << endl;
                    f = 1;
                }
                ss.clear();
            }
        }
        --n;
    }

}

C++: #include<iostream>
#include<stdio.h>
#include<string.h>

int main()
{
	int n;
	std::cin >> n;
	
	while( n-- ){
		char s[260], t[260];
		char a[29];
		
		fgets( s, 257, stdin );
		
		strcpy( t, s );
		
		for( int i = 1; i <= 26; i += 2 ){
			for( int j = 0; j < 26; j++ ){
				if( i % 13 == 0 ) continue;
				
 				for( int k = 0; k < 26; k++ ){
				 	a[ ( i * k + j ) % 26 ] = k + 'a';
				}
				
				for( int k = 0; k < strlen( t ); k++ ){
					if( s[k] == ' ' || s[k] == '\n' ) continue;
					
					s[k] = a[ t[k] - 'a' ];
				}
				
				if( strstr( s, "that" ) != NULL || strstr( s, "this" ) != NULL ){
					std::cout << s;
				}
				
				strcpy( s, t );
			}
		}
	}

 	return 0;
}