1. '''
  2. # Sample code to perform I/O:
  3.  
  4. name = input() # Reading input from STDIN
  5. print('Hi, %s.' % name) # Writing output to STDOUT
  6.  
  7. # Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
  8. '''
  9.  
  10. # Write your code here
  11.  
  12. test_cases = int(input())
  13. count = 0
  14. string_length = 0
  15. string = ''
  16. magic_word = ''
  17. magic_letters = [67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113]
  18.  
  19. def nearest_prime(num):
  20. smallest_difference = 1000
  21. nearest_prime = 0
  22.  
  23. for letter in magic_letters:
  24. difference = abs(num - letter)
  25. if difference < smallest_difference:
  26. smallest_difference = difference
  27. nearest_prime = letter
  28.  
  29. return nearest_prime
  30.  
  31. while(count < test_cases):
  32. string_length = int(input())
  33. string = input()
  34. count += 1
  35.  
  36. for x in string:
  37. magic_word += chr(nearest_prime(ord(x)))
  38.  
  39. print(magic_word)
  40. magic_word = ''
Language: Python 3.8