import sys,os,io input = sys.stdin.readline #input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline T = int(input()) for t in range(T): v, x = map(int, input().split()) p = v*x+1 ans = [] for i in range(1,p+1): if pow(i,x,p)==1: ans.append(i) print(*ans)