fork(2) download
  1. dig = {1:1, 3:7, 7:3, 9:9}
  2.  
  3. def solve(n, x):
  4. y = dig[x % 10]
  5.  
  6. for i in range(1, n): # to add (n-1) zeroes in prod
  7. for j in range(10): # checks whether j * 10^i + y is ok or not
  8.  
  9. temp = pow(10, i) * j + y
  10. prod = str(x * temp)[::-1] # [::-1] -> string in reverse order
  11.  
  12. if i < len(prod) and prod[i] == '0':
  13. y = temp
  14. continue
  15.  
  16. return y if x*y % 10**n == 1 else '0'
  17.  
  18. for _ in range(int(input())):
  19. n, x = map(int, input().split())
  20.  
  21. print(solve(n, x))
Success #stdin #stdout 0.02s 27664KB
stdin
5
1 9
1 7
2 11
3 17
5 11327
stdout
9
3
91
353
23263