#include<bits/stdc++.h> usingnamespace std; intpowmod(int a, int b, int m) { int ans = 1; while(b) { if(b&1) ans = 1ll*ans*a%m; a = 1ll*a*a%m; b >>= 1; } return ans; } intphi(int x) { int ans = x; for(int i=2; i*i<=x; i++) { if(x%i==0) { ans -= ans / i; while(x%i==0) x /= i; } } if(x>1) ans -= ans / x; return ans; } voidsolve() { int n; scanf("%d", &n); int c = phi(n+1), g = 0; bool ok = 0; while(++g) { ok = (powmod(g, c, n+1)==1); int tmpc = c; for(int i=2; i*i<=tmpc&&ok; i++) { if(tmpc%i==0) { while(tmpc%i==0) tmpc /= i; if(powmod(g, c/i, n+1)==1) ok = 0; } } if(tmpc>1&&powmod(g, c/tmpc, n+1)==1) ok = 0; if(ok) break; } int ans = g; for(int i=1; i<=n; i++) { printf("%d %d\n", i, ans); ans = ans*g%(n+1); } } intmain() { int t; scanf("%d", &t); while(t--) solve(); return0; }