#include int Mod; long long pow_mod(int n, int k) { long long N, ans = 1; for (N = n; k > 0; k >>= 1, N = N * N % Mod) if (k & 1) ans = ans * N % Mod; return ans; } int main() { int i, j, k, l, m, t, T, v, x, flag[100001]; scanf("%d", &T); for (t = 1; t <= T; t++) { scanf("%d %d", &v, &x); Mod = v * x + 1; for (i = 1; i < Mod; i++) flag[i] = 0; for (i = 1, k = 0; i < Mod && k < x; i++) { if (flag[i] != 0) continue; if (pow_mod(i, x) == 1) flag[i] = 1; else flag[i] = -1; if (flag[i] == 1) { for (j = i * i % Mod, k++; j != i; j = j * i % Mod, k++) flag[j] = 1; } } for (i = 1, j = 0; i < Mod && j < x; i++) { if (flag[i] == 1) { if (j++ > 0) printf(" "); printf("%d", i); } } printf("\n"); } fflush(stdout); return 0; }