mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.buffer.readline for _ in range(int(input())): v, x = map(int, input().split()) ans = [] p = x*v+1 for i in range(1, p+1): if pow(i, x, p) == 1: ans.append(i) print(*ans) if __name__ == '__main__': main()