$c = ('a'..'z').to_a
def c2n(char)
  return $c.index(char)
end

def n2c(i)
  return $c[i]
end

def dec(c, ai, b)
  return n2c((ai*(c2n(c)-b)) % 26)
end

p1 = c2n("t")
p2 = c2n("h")

a  = [1,3, 5, 7,9,11,15,17,19,21,23,25] # candidate of alpha
ai = [1,9,21,15,3,19, 7,23,11, 5,17,25] # inverse of the alpha in mod 26

n = gets.to_i
n.times{
  line = gets.chomp
  words = line.split
  ainv = 1
  b = 0
  words.each{|w|
    if w.size == 4
      #candidate of that or this
      c1 = c2n(w[0])
      c2 = c2n(w[1])
      i = 0
      while i < ai.size && (p1-p2) % 26 != (ai[i] * (c1-c2)) % 26
        i += 1
      end
      next if i == ai.size
      ainv = ai[i]
      b = (c1 - a[i] * p1) % 26
      # confirm
      break if dec(w[2], ainv, b) == "a" && w[0] == w[3] 
      break if dec(w[2], ainv, b) == "i" && dec(w[3], ainv, b) == "s"
    end
  }
  line.split("").each{|c|
    if c == " "
      print c
    else
      print dec(c, ainv, b)
    end
  }
  puts
}