计算出n-1
对n-1进行质因数分解
// Problem: Prime Land
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/21094/D
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
bool cmp(const pair<int,int> a,const pair<int,int> b){
return a.first>b.first;
}
void solve(){
int k;
cin>>k;
ll n=1;
for(int i=1;i<=k;i++){
int p,e;
cin>>p>>e;
n*=pow(p,e);
}
n--;
vector<pair<int,int>> ans;
for(int i=2;i<=n/i;i++){
if(n%i==0){
ll cnt=0;
while(n%i==0){
n/=i;
cnt++;
}
ans.push_back({i,cnt});
}
}
if(n>1){
ans.push_back({n,1});
}
sort(ans.begin(),ans.end(),cmp);
for(auto &[x,y] : ans){
cout<<x<<" "<<y<<" ";
}
cout<<'\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}