代码:
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
int p[N],tot;
bitset<N> vis;
void init(){
for (int i=2;i<=1e5;++i){
if (!vis[i]){
p[++tot]=i;
}
for (int j=1;p[j]*i<=1e5;++j){
vis[i*p[j]]=1;
if (i%p[j]==0) break;
}
}
}
void solve(){
int n;cin>>n;
int tmp1=lower_bound(p+1,p+1+tot,n/2)-p;
int tmp2=upper_bound(p+1,p+1+tot,n/2)-p;
int num;
if (abs(tmp1-n/2)<abs(tmp2-n/2)) num=tmp1;
else num=tmp2;
cout<<p[num]<<" ";
int l=p[num]-1,r=p[num]+1;
while (l>=1&&r<=n) cout<<l--<<" "<<r++<<" ";
while (l>=1) cout<<l--<<" ";
while (r<=n) cout<<r++<<" ";
cout<<"\n";
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
init();
int T;cin>>T;
while (T--) solve();
return 0;
}
```