#include<bits/stdc++.h>
using namespace std;
#define ll long long
map<ll,ll> m;
map<ll,ll> :: iterator it;
int main(){
     ll test,n,i,coin,k,a[200001],ans,s;
     cin>>test;
     while(test--){
          ans=0;
          cin>>n>>coin>>k;
          for(i=1; i<=n; i++){
               cin>>a[i];
          }
          sort(a+1,a+n+1);
          if(n%2==0){
               if(a[2]<=coin && n==2){
                    cout<<2;
               }
               else{
                    if(a[1]<=coin && n==2){
                         cout<<1;
                    }
                    else{
                         ll s1,s2,x=0;
                         ans=0;
                         for(i=2; i<=n; i=i+2){
                              if(ans+a[i]<=coin) ans+=a[i];
                              else {x=1;break;}
                         }
                         if(x==0){
                         s1=n;
                         }
                         else{
                              if(a[i-1]+ans<=coin){
                              s1=i-1;
                              }
                              else s1=i-2;
                         }
                         ans=0;
                         x=0;
                         if(a[1]>coin) s2=0;
                         else{
                              ans+=a[1];
                              for(int i=3; i<=n; i=i+2){
                                   if(ans+a[i]<=coin) ans+=a[i];
                                   else {x=1;break;}
                              }
                              if(x=0){
                                   if(ans+a[n]<=coin) s2=n;
                                   else s2=n-1;
                              }
                              if(ans+a[i-1]<=coin) s2=i-1;
                              else s2=i-2;
                         }
                         cout<<max(s1,s2);
                    }
               }
          }
          else{
               ans=0;
               int x=0;
               if(a[1]>coin){
                    cout<<0;
               }
               else{
                    if(a[2]<=coin && n==2) cout<<2;
                    else{
                    if(a[1]<=coin && n==1) cout<<1;
                    else{
                    ll s,s1;
                    ans+=a[1];
                    for(i=3; i<=n; i=i+2){
                         if(ans+a[i]<=coin) {ans+=a[i];}
                         else {x=1;break;}
                    }
                    if(x==0){
                         s=n;
                    }
                    else{
                         if(ans+a[i-1]<=coin){
                              s=i-1;
                         }
                         else s=i-2;
                    }
                    x=0;
                    ans=0;
                    for(int i=2; i<=n; i=i+2){
                         if(ans+a[i]<=coin) ans+=a[i];
                         else{x=1;break;}
                    }
                    if(x==0){
                         if(ans+a[n]<=coin) s1=n;
                         else s1=n-1;
                    }
                    else{
                         if(ans+a[i-1]<=coin) s1=i-1;
                         else s1=i-2;
                    }
                    cout<<max(s1,s);
               }
                    }}
          }
          cout<<endl;
     }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
     ll t,n,p,k,a[200001],ans=0,s1,s2,i;
     cin>>t;
     while(t--){
          cin>>n>>p>>k;
          for(i=1; i<=n; i++){
               cin>>a[i];
          }
          ans=0;
          sort(a+1,a+n+1);
          int x=0;
          if(a[1]>p){
               cout<<0<<endl;
          }
          else{
               for(i=2; i<=n; i=i+2){
                    if(ans+a[i]<=p) ans+=a[i];
                    else {x=1;break;}
               }
               if(x==0){
                    if(i==n+2) s1=n;
                    if(i==n+1){
                         if(ans+a[n]<=p) s1=n;
                         else s1=n-1;
                    }
               }
               else{
                    if(ans+a[i-1]<=p) s1=i-1;
                    else s1=i-2;
               }
               ans=0;
               ans=a[1];
               x=0;
               for(i=3; i<=n; i=i+2){
                    if(ans+a[i]<=p) ans+=a[i];
                    else {x=1;break;}
               }
               if(x==0){
                    if(i==n+2){
                         s2=n;
                    }
                    else {
                         if(ans+a[n]<=p) s2=n;
                         else s2=n-1;
                    }
               }
               else{
                    if(ans+a[i-1]<=p) s2=i-1;
                    else s2=i-2;
               }

          cout<<max(s1,s2)<<endl;
          }
     }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
map<ll,ll> m;
map<ll,ll> :: iterator it;
int main(){
     ll test,n,i,coin,k,a[200001],ans,s;
     cin>>test;
     while(test--){
          ans=0;
          cin>>n>>coin>>k;
          for(i=1; i<=n; i++){
               cin>>a[i];
          }
          sort(a+1,a+n+1);
          if(n%2==0){
               if(a[2]<=coin && n==2){
                    cout<<2;
               }
               else{
                    if(a[1]<=coin && n==2){
                         cout<<1;
                    }
                    else{
                         ll s1,s2,x=0;
                         ans=0;
                         for(i=2; i<=n; i=i+2){
                              if(ans+a[i]<=coin) ans+=a[i];
                              else {x=1;break;}
                         }
                         if(x==0){
                         s1=n;
                         }
                         else{
                              if(a[i-1]+ans<=coin){
                              s1=i-1;
                              }
                              else s1=i-2;
                         }
                         ans=0;
                         x=0;
                         if(a[1]>coin) s2=0;
                         else{
                              ans+=a[1];
                              for(int i=3; i<=n; i=i+2){
                                   if(ans+a[i]<=coin) ans+=a[i];
                                   else {x=1;break;}
                              }
                              if(x=0){
                                   if(ans+a[n]<=coin) s2=n;
                                   else s2=n-1;
                              }
                              if(ans+a[i-1]<=coin) s2=i-1;
                              else s2=i-2;
                         }
                         cout<<max(s1,s2);
                    }
               }
          }
          else{
               ans=0;
               int x=0;
               if(a[1]>coin){
                    cout<<0;
               }
               else{
                    ll s,s1;
                    ans+=a[1];
                    for(i=3; i<=n; i=i+2){
                         if(ans+a[i]<=coin) {ans+=a[i];}
                         else {x=1;break;}
                    }
                    if(x==0){
                         s=n;
                    }
                    else{
                         if(ans+a[i-1]<=coin){
                              s=i-1;
                         }
                         else s=i-2;
                    }
                    x=0;
                    ans=0;
                    for(int i=2; i<=n; i=i+2){
                         if(ans+a[i]<=coin) ans+=a[i];
                         else{x=1;break;}
                    }
                    if(x==0){
                         if(ans+a[n]<=coin) s1=n;
                         else s1=n-1;
                    }
                    else{
                         if(ans+a[i-1]<coin) s1=i-1;
                         else s1=i-2;
                    }
                    cout<<max(s1,s);
               }

          }
          cout<<endl;
     }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
map<ll,ll> m;
map<ll,ll> :: iterator it;
int main(){
     ll test,n,i,coin,k,a[200001],ans,s;
     cin>>test;
     while(test--){
          ans=0;
          cin>>n>>coin>>k;
          for(i=1; i<=n; i++){
               cin>>a[i];
          }
          sort(a+1,a+n+1);
          if(n%2==0){
               if(a[2]<=coin && n==2){
                    cout<<2;
               }
               else{
                    if(a[1]<=coin && n==2){
                         cout<<1;
                    }
                    else{

               ll s1,s2,x=0;
               for( i=2; i<=n; i=i+2){
                    if(ans+a[i]<=coin) ans+=a[i];
                    else {x=1;break;}
               }
               if(x==0){
                    s1=n;
               }
               else{
                    if(a[i-1]+ans<=coin){
                         s1=i-1;
                    }
                    else s1=i-2;
               }
               ans=0;
               x=0;
               if(a[1]>coin) s2=0;
               else{
                    for(int i=3; i<=n; i++){
                         if(ans+a[i]<=coin) ans+=a[i];
                         else {x=1;break;}
                    }
                    if(x=0){
                         s2=n;
                    }
                    if(ans+a[i-1]<=coin) s2=i-1;
                    else s2=i-2;
               }
               cout<<max(s1,s2);
                    }
               }
          }
          else{
               ans=0;
               int x=0;
               if(a[1]>coin){
                    cout<<0;
               }
               else{
                    ll s,s1;
                    ans+=a[1];
                    for(i=3; i<=n; i=i+2){
                         if(ans+a[i]<=coin) {ans+=a[i];}
                         else {x=1;break;}
                    }
                    if(x==0){
                         s=n;
                    }
                    else{
                         if(ans+a[i-1]<=coin){
                              s=i-1;
                         }
                         else s=i-2;
                    }
                    x=0;
                    ans=0;
                    for(int i=2; i<=n; i=i+2){
                         if(ans+a[i]<=coin) ans+=a[i];
                         else{x=1;break;}
                    }
                    if(x==0){
                         if(ans+a[n]<=coin) s1=n;
                         else s1=n-1;
                    }
                    else{
                         if(ans+a[i-1]<coin) s1=i-1;
                         else s1=i-2;
                    }
                    cout<<max(s1,s);
               }

          }
          cout<<endl;
     }
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

inline lngint traverse(lngint n,lngint p,lngint k,vector<lngint> vec){
	lngint mx=0,sum=0;
	for(lngint i=0;i<n;i++){
		if(sum>p) break;

		lngint total=i+1;
		for(lngint j=i+(k-1);j<n;j+=k){
			if((sum+vec[j])<=p)){
				total+=k;
				sum+=vec[j];
				continue;
			}
			break;
		}

		mx=max(mx,total);
		sum+=vec[i];
	}

	return mx;
}

int main(){
	lngint n,p,k;
	vector<lngint> vec;
	int t;
	cin>>t;

	for(lngint i=0; i < t; i++){
		cin>>n>>p>>k;

		vec.resize(n);
		for(lngint j=0;j<n;) cin>>vec[j];

		sort(vec);
		cout<<traverse(n,p,k,vec)<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

int main(){
	lngint n,p,k;
	int t;
	cin>>t;

	for(lngint x=0; x < t; x++){
		cin>>n>>p>>k;

		vector<lngint> vec(n);
		for(lngint j=0;j<n;j++) cin>>vec[j];
		sort(vec.begin(),vec.end());

		lngint mx=0,sum=0;
		lngint pref=0;
		for(lngint i=0;i<n;i++){
			if(pref>p) break;

			lngint total=i;
			for(lngint j=i+(k-1);j<n;j+=k){
				if((sum+vec[j])<=p){
					total+=k;
					sum+=vec[j];
				}
				else break;
			}

			pref+=vec[i];
			mx=max(mx,total);
		}

		cout<<mx<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

inline lngint traverse(lngint n,lngint p,lngint k,const vector<lngint>& vec){
	lngint mx=0,sum=0;
	for(lngint i=0;i<n;i++){
		if(sum>p) break;

		lngint total=i+1;
		for(lngint j=i+(k-1);j<n;j+=k){
			if((sum+=vec[j])<=p){
				total+=k;
				continue;
			}
			else break;
		}

		mx=max(mx,total);
		sum+=vec[i];
	}

	return mx;
}

int main(){
	lngint n,p,k;
	int t;
	cin>>t;

	for(lngint i=0; i < t; i++){
		cin>>n>>p>>k;

		vector<lngint> vec(n);
		for(lngint j=0;j<n;) cin>>vec[j];
		sort(vec.begin(),vec.end());
		cout<<traverse(n,p,k,vec)<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

int main(){
	lngint n,p,k;
	int t;
	cin>>t;

	for(lngint i=0; i < t; i++){
		cin>>n>>p>>k;

		vector<lngint> vec(n);
		for(lngint j=0;j<n;j++) cin>>vec[j];
		sort(vec.begin(),vec.end());

		lngint mx=0,sum=0;
		for(lngint i=0;i<=k;i++){
			if(sum>p) break;

			lngint total=i;
			for(lngint j=i+(k-1);j<n;j+=k){
				if((sum+=vec[j])<=p){
					total+=k;
				}
				else break;
			}

			mx=max(mx,total);
			sum+=vec[i];
		}

		cout<<mx<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lgint=long long int;

inline lngint traverse(lngint n,lngint p,lngint k,vector<lngint> vec){
	lngint max=sum=0;
	for(lngint i=0;i<n;i++){
		if(sum>p) break;

		lngint total=i+1;
		for(lngint j=i+(k-1);j<n;j+=k){
			if((sum+=vec[j])<=p)){
				total+=k;
				continue;
			}
			break;
		}

		max=max(max,total);
		sum+=vec[i];
	}

	return max;
}

int main(){
	lngint n,p,k;
	vector<lngint> vec;
	int t;
	cin>>t;

	for(lngint i=0; i < t; i++){
		cin>>n>>p>>k;

		vec.resize(n);
		for(lngint j=0;j<n;) cin>>vec[j];

		sort(vec);
		cout<<traverse(n,p,k,vec)<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

int main(){
	lngint n,p,k;
	int t;
	cin>>t;

	for(lngint i=0; i < t; i++){
		cin>>n>>p>>k;

		vector<lngint> vec(n);
		for(lngint j=0;j<n;j++) cin>>vec[j];
		sort(vec.begin(),vec.end());

		lngint mx=0,sum=0;
		for(lngint i=0;i<n;i++){
			if(sum>p) break;

			lngint total=i;
			for(lngint j=i+(k-1);j<n;j+=k){
				if((sum+=vec[j])<=p){
					total+=k;
				}
				else break;
			}

			mx=max(mx,total);
			sum+=vec[i];
		}

		cout<<mx<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

int main() {
    int cntTest;
    cin >> cntTest;
    for (int test = 0; test < cntTest; test++) {
        int n, p, k;
        cin >> n >> p >> k;
        int pref = 0;
        int ans = 0;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        sort(a.begin(), a.end());
        for (int i = 0; i <= k; i++) {
            int sum = pref;
            if (sum > p) break;
            int cnt = i;
            for (int j = i + k - 1; j < n; j += k) {
                if (sum + a[j] <= p) {
                    cnt += k;
                    sum += a[j];
                } else {
                    break;
                }
            }
            pref += a[i];
            ans = max(ans, cnt);
        }
        cout << ans << "\n";
    }
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

inline lngint traverse(lngint n,lngint p,lngint k,const vector<lngint>& vec){
	lngint mx=0,sum=0;
	for(lngint i=0;i<n;i++){
		if(sum>p) break;

		lngint total=i+1;
		for(lngint j=i+(k-1);j<n;j+=k){
			if((sum+=vec[j])<=p){
				total+=k;
				continue;
			}
			else break;
		}

		mx=max(mx,total);
		sum+=vec[i];
	}

	return mx;
}

int main(){
	lngint n,p,k;
	int t;
	cin>>t;

	for(lngint i=0; i < t; i++){
		cin>>n>>p>>k;

		for(lngint j=0;j<n;) cin>>vec[j];
		vector<lngint> vec(n);
		sort(vec.begin(),vec.end());
		cout<<traverse(n,p,k,vec)<<endl;
	}
	return 0;
}


Solution (MikeMirzayanov)
1282B1 - K for the Price of One (Easy Version), 1282B2 - K for the Price of One (Hard Version)

Idea: MikeMirzayanov, unreal.eugene Preparation: Supermagzzz

Editorial
Solution (Supermagzzz)
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

int main() {
    int cntTest;
    cin >> cntTest;
    for (int test = 0; test < cntTest; test++) {
        int n, p, k;
        cin >> n >> p >> k;
        int pref = 0;
        int ans = 0;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        sort(a.begin(), a.end());
        for (int i = 0; i <= k; i++) {
            int sum = pref;
            if (sum > p) break;
            int cnt = i;
            for (int j = i + k - 1; j < n; j += k) {
                if (sum + a[j] <= p) {
                    cnt += k;
                    sum += a[j];
                } else {
                    break;
                }
            }
            pref += a[i];
            ans = max(ans, cnt);
        }
        cout << ans << "\n";
    }
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

inline lngint traverse(lngint n,lngint p,lngint k,vector<lngint> vec){
	lngint max=sum=0;
	for(lngint i=0;i<n;i++){
		if(sum>p) break;

		lngint total=i+1;
		for(lngint j=i+(k-1);j<n;j+=k){
			if((sum+=vec[j])<=p)){
				total+=k;
				continue;
			}
			break;
		}

		max=max(max,total);
		sum+=vec[i];
	}

	return max;
}

int main(){
	lngint n,p,k;
	vector<lngint> vec;
	int t;
	cin>>t;

	for(lngint i=0; i < t; i++){
		cin>>n>>p>>k;

		vec.resize(n);
		for(lngint j=0;j<n;) cin>>vec[j];

		sort(vec);
		cout<<traverse(n,p,k,vec)<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

int main(){
	lngint n,p,k;
	int t;
	cin>>t;

	for(lngint x=0; x < t; x++){
		cin>>n>>p>>k;

		vector<lngint> vec(n);
		for(lngint j=0;j<n;j++) cin>>vec[j];
		sort(vec.begin(),vec.end());

		lngint mx=0,sum=0;
		for(lngint i=0;i<n;i++){
			if(sum>p) break;

			lngint total=i;
			for(lngint j=i+(k-1);j<n;j+=k){
				if((sum+vec[j])<=p){
					total+=k;
					sum+=vec[i];
				}
				else break;
			}

			mx=max(mx,total);
			sum+=vec[i];
		}

		cout<<mx<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

int main(){
	lngint n,p,k;
	int t;
	cin>>t;

	for(lngint x=0; x < t; x++){
		cin>>n>>p>>k;

		vector<lngint> vec(n);
		for(lngint j=0;j<n;j++) cin>>vec[j];
		sort(vec.begin(),vec.end());

		lngint mx=0,sum=0;
		for(lngint i=0;i<n;i++){
			if(sum>p) break;

			lngint total=i;
			for(lngint j=i+(k-1);j<n;j+=k){
				if((sum+vec[j])<=p){
					total+=k;
					sum+=vec[i];
				}
				else break;
			}

			sum+=vec[i];
			mx=max(mx,total);
		}

		cout<<mx<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

int main(){
	lngint n,p,k;
	int t;
	cin>>t;

	for(lngint x=0; x < t; x++){
		cin>>n>>p>>k;

		vector<lngint> vec(n);
		for(lngint j=0;j<n;j++) cin>>vec[j];
		sort(vec.begin(),vec.end());

		lngint mx=0,sum=0;
		for(lngint i=0;i<n;i++){
			if(sum>p) break;

			lngint total=i;
			for(lngint j=i+(k-1);j<n;j+=k){
				if((sum+vec[j])<=p){
					total+=k;
					sum+=vec[i];
				}
				else break;
			}

			sum+=a[i];
			mx=max(mx,total);
		}

		cout<<mx<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

int main(){
	lngint n,p,k;
	int t;
	cin>>t;

	for(lngint x=0; x < t; x++){
		cin>>n>>p>>k;

		vector<lngint> vec(n);
		for(lngint j=0;j<n;j++) cin>>vec[j];
		sort(vec.begin(),vec.end());

		lngint mx=0,sum=0;
		lngint pref=0;
		for(lngint i=0;i<n;i++){
			sum=pref;
			if(sum>p) break;

			lngint total=i;
			for(lngint j=i+(k-1);j<n;j+=k){
				if((sum+vec[j])<=p){
					total+=k;
					sum+=vec[j];
				}
				else break;
			}

			pref+=vec[i];
			mx=max(mx,total);
		}

		cout<<mx<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

int main(){
	lngint n,p,k;
	int t;
	cin>>t;

	for(lngint i=0; i < t; i++){
		cin>>n>>p>>k;

		vector<lngint> vec(n);
		for(lngint j=0;j<n;j++) cin>>vec[j];
		sort(vec.begin(),vec.end());

		lngint mx=0,sum=0;
		for(lngint i=0;i<n;i++){
			if(sum>p) break;

			lngint total=i+1;
			for(lngint j=i+(k-1);j<n;j+=k){
				if((sum+=vec[j])<=p){
					total+=k;
					continue;
				}
				else break;
			}

			mx=max(mx,total);
			sum+=vec[i];
		}

		cout<<mx<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

inline lngint traverse(lngint n,lngint p,lngint k,const vector<lngint>& vec){
	lngint mx=0,sum=0;
	for(lngint i=0;i<=k;i++){
		if(sum>p) break;

		lngint total=i+1;
		for(lngint j=i+(k-1);j<n;j+=k){
			if((sum+=vec[j])<=p){
				total+=k;
				continue;
			}
			else break;
		}

		mx=max(mx,total);
		sum+=vec[i];
	}

	return mx;
}

int main(){
	lngint n,p,k;
	int t;
	cin>>t;

	for(lngint i=0; i < t; i++){
		cin>>n>>p>>k;

		vector<lngint> vec(n);
		for(lngint j=0;j<n;) cin>>vec[j];
		sort(vec.begin(),vec.end());
		cout<<traverse(n,p,k,vec)<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

int main(){
	lngint n,p,k;
	int t;
	cin>>t;

	for(lngint i=0; i < t; i++){
		cin>>n>>p>>k;

		vector<lngint> vec(n);
		for(lngint j=0;j<n;) cin>>vec[j];
		sort(vec.begin(),vec.end());

		lngint mx=0,sum=0;
		for(lngint i=0;i<=k;i++){
			if(sum>p) break;

			lngint total=i;
			for(lngint j=i+(k-1);j<n;j+=k){
				if((sum+=vec[j])<=p){
					total+=k;
					continue;
				}
				else break;
			}

			mx=max(mx,total);
			sum+=vec[i];
		}

		cout<<mx<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

int main(){
	lngint n,p,k;
	int t;
	cin>>t;

	for(lngint x=0; x < t; x++){
		cin>>n>>p>>k;

		vector<lngint> vec(n);
		for(lngint j=0;j<n;j++) cin>>vec[j];
		sort(vec.begin(),vec.end());

		lngint mx=0,sum=0;
		for(lngint i=0;i<=n;i++){
			if(sum>p) break;

			lngint total=i;
			for(lngint j=i+(k-1);j<n;j+=k){
				if((sum+=vec[j])<=p){
					total+=k;
				}
				else break;
			}

			mx=max(mx,total);
			sum+=vec[i];
		}

		cout<<mx<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

int main(){
	lngint n,p,k;
	int t;
	cin>>t;

	for(lngint i=0; i < t; i++){
		cin>>n>>p>>k;

		vector<lngint> vec(n);
		for(lngint j=0;j<n;j++) cin>>vec[j];
		sort(vec.begin(),vec.end());

		lngint mx=0,sum=0;
		for(lngint i=0;i<n;i++){
			if(sum>p) break;

			lngint total=i;
			for(lngint j=i+(k-1);j<n;j+=k){
				if((sum+=vec[j])<=p){
					total+=k;
					continue;
				}
				else break;
			}

			mx=max(mx,total);
			sum+=vec[i];
		}

		cout<<mx<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

inline lngint traverse(lngint n,lngint p,lngint k,vector<lngint> vec){
	lngint mx=0,sum=0;
	for(lngint i=0;i<n;i++){
		if(sum>p) break;

		lngint total=i+1;
		for(lngint j=i+(k-1);j<n;j+=k){
			if((sum+=vec[j])<=p){
				total+=k;
				continue;
			}
			break;
		}

		mx=max(mx,total);
		sum+=vec[i];
	}

	return mx;
}

int main(){
	lngint n,p,k;
	vector<lngint> vec;
	int t;
	cin>>t;

	for(lngint i=0; i < t; i++){
		cin>>n>>p>>k;

		vec.resize(n);
		for(lngint j=0;j<n;) cin>>vec[j];

		sort(vec.begin(),vec.end());
		cout<<traverse(n,p,k,vec)<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

int main(){
	lngint n,p,k;
	int t;
	cin>>t;

	for(lngint x=0; x < t; x++){
		cin>>n>>p>>k;

		vector<lngint> vec(n);
		for(lngint j=0;j<n;j++) cin>>vec[j];
		sort(vec.begin(),vec.end());

		lngint mx=0,sum=0;
		for(lngint i=0;i<n;i++){
			if(sum>p) break;

			lngint total=i;
			for(lngint j=i+(k-1);j<n;j+=k){
				if((sum+vec[j])<=p){
					total+=k;
					sum+=vec[j];
				}
				else break;
			}

			sum+=vec[i];
			mx=max(mx,total);
		}

		cout<<mx<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

using lngint=long long int;

int main(){
	lngint n,p,k;
	int t;
	cin>>t;

	for(lngint x=0; x < t; x++){
		cin>>n>>p>>k;

		vector<lngint> vec(n);
		for(lngint j=0;j<n;j++) cin>>vec[j];
		sort(vec.begin(),vec.end());

		lngint mx=0,sum=0;
		lngint no_offer=0;
		for(lngint i=0;i<k;i++){
			if(no_offer>p) break;

			sum=no_offer;
			no_offer+=vec[i];
			lngint total=i;
			for(lngint j=i+(k-1);j<n;j+=k){
				if((sum+=vec[j])<=p){
					total+=k;
				}
				else break;
			}
			mx=max(mx,total);
		}

		cout<<mx<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	ll n,p,k;
	cin>>n>>p>>k;
	ll arr[n];
	for(int i=0;i<n;i++)
		cin>>arr[i];
	sort(arr,arr+n);
	ll cnt=0;
	ll val=p,l;
	int f=0;
	for(int i=0;i<n;i++)
	{
		if(p>=arr[i])
		{
			val=p;
			p=p-arr[i];
			cnt++;

			l=i;
		}
		else if(val>=arr[i])
		{		val=val-arr[i];
				cnt++;
		}
		else
		{
			break;
		}
	}
	//cout<<val<<endl;

		cout<<cnt<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	ll n,p,k;
	cin>>n>>p>>k;
	ll arr[n];
	for(int i=0;i<n;i++)
		cin>>arr[i];
	sort(arr,arr+n);
	ll cnt=0;
	ll val=p,l;
	int f=0;
	for(int i=0;i<n;i++)
	{
		if(arr[i]<=p)
		{
				val=p;
			p-=arr[i];
			cnt++;

			l=i;
		}
		else if(val>=arr[i] && !f)
			{		f=1;
				cnt++;
		}
		else
		{
			break;
		}
	}
	//cout<<val<<endl;

		cout<<cnt<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define fr(a,b) for(int i = a; i < b; i++)
#define mod 1000000007
#define triplet pair<int,pair<int,int>>
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
using namespace std;
ll dp[200001];
int main(){
    ll i,j,t,n,m,p,k;
    cin>>t;
    while(t--)
    {
    cin>>n>>p>>k;
    vector<ll> v(n);
    for(int i =0; i<n;i++)
    {
        cin>>v[i];
    }
    sort(v.begin(), v.end());

    //dp[0] = v[0];
    int ans =0;
    for(int i = 0; i < k - 1; i++)
        {dp[i] = v[i] + dp[i-1];if(dp[i] <= p)
            ans = i+1;}

    for(int i = k -1 ; i < n ; i++)
    {
        dp[i] = v[i] + dp[i - k];
        if(dp[i] <= p)
            ans = i + 1;
    }

    cout<<ans<<'\n';
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	int n,p,k;
	cin>>n>>p>>k;
	int arr[n];
	for(int i=0;i<n;i++)
		cin>>arr[i];
	sort(arr,arr+n);
	int cnt=0;
	for(int i=0;i<n;i++)
	{
		if(arr[i]<=p)
		{
			p-=arr[i];
			cnt++;
		}
	}
	if(cnt==0)
		cout<<0<<endl;
	else if(cnt<=n-1)
		cout<<cnt+1<<endl;
	else
		cout<<cnt<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	ll n,p,k;
	cin>>n>>p>>k;
	ll arr[n];
	for(int i=0;i<n;i++)
		cin>>arr[i];
	sort(arr,arr+n);
	ll cnt=0;
	int val=p,l;
	for(int i=0;i<n;i++)
	{
		if(arr[i]<=p)
		{
				val=p;
			p-=arr[i];
			cnt++;

			l=i;
		}
		else
		{
			break;
		}
	}
	//cout<<val<<endl;

	if(arr[l+1]<=val && l<=n-1)
		cnt++;
	if(cnt==0)
		cout<<0<<endl;
	else
		cout<<cnt<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	ll n,p,k;
	cin>>n>>p>>k;
	ll arr[n];
	for(int i=0;i<n;i++)
		cin>>arr[i];
	sort(arr,arr+n);
	ll cnt=0;
	for(int i=0;i<n;i++)
	{
		if(arr[i]<=p)
		{
			p-=arr[i];
			cnt++;
		}
		else
		{
			if(i-1<n-1 && cnt!=0)
				cnt++;
			break;
		}
	}
	if(cnt==0)
		cout<<0<<endl;
	else
		cout<<cnt<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	ll n,p,k;
	cin>>n>>p>>k;
	ll arr[n];
	for(int i=0;i<n;i++)
		cin>>arr[i];
	sort(arr,arr+n);
	ll cnt=0;
	ll val=p,l;
	for(int i=0;i<n;i++)
	{
		if(arr[i]<=p)
		{
				val=p;
			p-=arr[i];
			cnt++;

			l=i;
		}
		else
		{
			break;
		}
	}
	//cout<<val<<endl;

	if(arr[l+1]<=val && l<n-1)
		cnt++;

		cout<<cnt<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef unsigned long long ull;
typedef long double ld;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI 3.14159265
const int MOD=1e9+7;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a)  (a).begin(),(a).end()

const int N=2e5+10;
int a[3][N];
int32_t main()
{
	IOS;
    int t;
    cin>>t;
    while(t--)
    {
        int n,p,k;
        cin>>n>>p>>k;
        int a[n+1],pre[n+1];
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        sort(a,a+n);
        int sum=0;
        for(int i=0;i<n;i++)
        {
            sum+=a[i];
            pre[i]=sum;
        }
        // for(int i=0;i<n;i++)
        // {
        //     cout<<pre[i]<<" ";
        // }
        // cout<<endl;
        int mx=0;
        for(int i=0;i<n;i++)
        {
            if(pre[i]>p) break;
            auto it=lower_bound(pre,pre+n,p+a[i]);
            int x=it-pre+1;
            // cout<<p+a[i]<<" "<<it-pre<<endl;
            if(pre[x]==p+a[i])
            {
                mx=max(mx,x);
            }
            else
            {
                mx=max(x-1,mx);
            }
        }
        cout<<mx<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef unsigned long long ull;
typedef long double ld;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI 3.14159265
const int MOD=1e9+7;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a)  (a).begin(),(a).end()

const int N=2e5+10;
int a[3][N];
int32_t main()
{
	IOS;
    int t;
    cin>>t;
    while(t--)
    {
        int n,p,k;
        cin>>n>>p>>k;
        int a[n+1],pre[n+1];
        a[n]=0;
        pre[n]=0;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        sort(a,a+n);
        int sum=0;
        for(int i=0;i<n;i++)
        {
            sum+=a[i];
            pre[i]=sum;
        }
        int mx=0;
        for(int i=0;i<n;i++)
        {
            if(pre[i]>p) break;
            auto it=lower_bound(pre,pre+n,p+a[i]);
            int x=it-pre+1;
            if(pre[x-1]==p+a[i])
            {
                mx=max(mx,x);
            }
            else
            {
                mx=max(x-1,mx);
            }
        }
        cout<<mx<<endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 1e6 + 5;
const ll INF = 1e9 + 7;

ll n;
ii a[N];

bool check(const ll &x) {
    for (ll i = 1; i < n; i++)
        if (a[i].first % x != 0 && a[i].second % x != 0) return false;
    return true;
}

int main() {
    //freopen("1025B.INP", "r", stdin);
    //freopen("1025B.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i < n; i++) cin >> a[i].first >> a[i].second;
    //********************
    ll x, y; cin >> x >> y;
    for (ll i = 2; i <= sqrt(x); i++) if (x % i == 0) {
        if (check(x)) cout << x, exit(0);
        if (check(x / i)) cout << x / i, exit(0);
    }
    for (ll i = 2; i <= sqrt(y); i++) if (y % i == 0) {
        if (check(y)) cout << y, exit(0);
        if (check(y / i)) cout << y / i, exit(0);
    }
    //********************
    if (x > 1 && check(x)) cout << x, exit(0);
    if (y > 1 && check(y)) cout << y, exit(0);
    cout << -1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 1e6 + 5;
const ll INF = 1e9 + 7;

ll n;
ii a[N];
vector<ll> tsnt;
bool check(const ll &x) {
    for (ll i = 1; i < n; i++)
        if (a[i].first % x != 0 && a[i].second % x != 0) return false;
    return true;
}

int main() {
    //freopen("1025B.INP", "r", stdin);
    //freopen("1025B.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i < n; i++) cin >> a[i].first >> a[i].second;
    //********************
    ll x, y; cin >> x >> y;
    if(n == 1) cout << x,exit(0);
    for(int i = 2;i<=sqrt(x);i++){
        if(x % i == 0){
            while(x % i == 0) x/=i;
            tsnt.push_back(i);
        }
    }
    for(int i = 2;i<=sqrt(y);i++){
        if(y % i == 0){
            while(y % i == 0) y/=i;
            tsnt.push_back(i);
        }
    }
    for(int i = 0;i<tsnt.size();i++)
        if(check(tsnt[i])) cout << tsnt[i],exit(0);
    if(x > 1 && check(x)) cout << x,exit(0);
    if(Y > 1 && check(y)) cout << y,exit(0);
    cout << -1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 1e6 + 5;
const ll INF = 1e9 + 7;

ll n;

bitset <2000000005> dd;
map <ll, ll> C;

int main() {
    //freopen("1025B.INP", "r", stdin);
    //freopen("1025B.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i <= n - 1; i++) {
        ll x, y; cin >> x >> y;
        dd.reset();
        for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (x % j == 0) {
            if (dd[j] == false) {
                C[j]++; dd[j] = true;
            }
            if (dd[x / j] == false && j != x / j) {
                C[x / j]++; dd[j] = true;
            }
        }
        for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (y % j == 0) {
            if (dd[j] == false) {
                C[j]++; dd[j] = true;
            }
            if (dd[y / j] == false && j != y / j) {
                C[y / j]++; dd[j] = true;
            }
        }
    }
    //********************
    ll x, y; cin >> x >> y;
    dd.reset();
    for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (x % j == 0) {
        if (dd[j] == false) {
            C[j]++; dd[j] = true;
            if (C[j] == n) cout << j, exit(0);
        }
        if (dd[x / j] == false && j != x / j) {
            C[x / j]++; dd[j] = true;
            if (C[x / j] == n) cout << x / j, exit(0);
        }
    }
    for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (y % j == 0) {
        if (dd[j] == false) {
            C[j]++; dd[j] = true;
            if (C[j] == n) cout << j, exit(0);
        }
        if (dd[y / j] == false && j != y / j) {
            C[y / j]++; dd[j] = true;
            if (C[y / j] == n) cout << y / j, exit(0);
        }
    }
    cout << -1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 1e6 + 5;
const ll INF = 1e9 + 7;

ll n;
ii a[N];

bool check(const ll &x) {
    for (ll i = 1; i < n; i++)
        if (a[i].first % x != 0 && a[i].second % x != 0) return false;
    return true;
}

int main() {
    //freopen("1025B.INP", "r", stdin);
    //freopen("1025B.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i < n; i++) cin >> a[i].first >> a[i].second;
    //********************
    ll x, y; cin >> x >> y;
    for (ll i = 2; i <= sqrt(x); i++) if (x % i == 0) {
        if (check(x)) cout << x, exit(0);
        if (check(x / i)) cout << x / i, exit(0);
    }
    for (ll i = 2; i <= sqrt(y); i++) if (y % i == 0) {
        if (check(y)) cout << y, exit(0);
        if (check(y / i)) cout << y / i, exit(0);
    }
    //********************
    if (check(x)) cout << x, exit(0);
    if (check(y)) cout << y, exit(0);
    cout << -1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 1e6 + 5;
const ll INF = 1e9 + 7;

ll n;

bitset <2000000005> dd;
map <ll, ll> C;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i <= n - 1; i++) {
        ll x, y; cin >> x >> y;
        dd.reset();
        for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (x % j == 0) {
            if (dd[j] == false) {
                C[j]++; dd[j] = true;
            }
            if (dd[x / j] == false && j != x / j) {
                C[x / j]++; dd[j] = true;
            }
        }
        for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (y % j == 0) {
            if (dd[j] == false) {
                C[j]++; dd[j] = true;
            }
            if (dd[y / j] == false && j != y / j) {
                C[y / j]++; dd[j] = true;
            }
        }
    }
    //********************
    ll x, y; cin >> x >> y;
    dd.reset();
    for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (x % j == 0) {
        if (dd[j] == false) {
            C[j]++; dd[j] = true;
            if (C[j] == n) cout << j, exit(0);
        }
        if (dd[x / j] == false && j != x / j) {
            C[x / j]++; dd[j] = true;
            if (C[x / j] == n) cout << x / j, exit(0);
        }
    }
    for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (y % j == 0) {
        if (dd[j] == false) {
            C[j]++; dd[j] = true;
            if (C[j] == n) cout << j, exit(0);
        }
        if (dd[y / j] == false && j != y / j) {
            C[y / j]++; dd[j] = true;
            if (C[y / j] == n) cout << y / j, exit(0);
        }
    }
    cout << -1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 1e8 + 5;
const ll INF = 1e9 + 7;

ll n;
bool dd[N];

map <ll, ll> C;

int main() {
    //freopen("1025B.INP", "r", stdin);
    //freopen("1025B.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i <= n - 1; i++) {
        ll x, y; cin >> x >> y;
        memset(dd, false, sizeof dd);
        for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (x % j == 0) {
            if (dd[j] == false) {
                C[j]++; dd[j] = true;
            }
            if (dd[x / j] == false && j != x / j) {
                C[x / j]++; dd[j] = true;
            }
        }
        for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (y % j == 0) {
            if (dd[j] == false) {
                C[j]++; dd[j] = true;
            }
            if (dd[y / j] == false && j != y / j) {
                C[y / j]++; dd[j] = true;
            }
        }
    }
    //********************
    ll x, y; cin >> x >> y;
    memset(dd, false, sizeof dd);
    for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (x % j == 0) {
        if (dd[j] == false) {
            C[j]++; dd[j] = true;
            if (C[j] == n) cout << j, exit(0);
        }
        if (dd[x / j] == false && j != x / j) {
            C[x / j]++; dd[j] = true;
            if (C[x / j] == n) cout << x / j, exit(0);
        }
    }
    for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (y % j == 0) {
        if (dd[j] == false) {
            C[j]++; dd[j] = true;
            if (C[j] == n) cout << j, exit(0);
        }
        if (dd[y / j] == false && j != y / j) {
            C[y / j]++; dd[j] = true;
            if (C[y / j] == n) cout << y / j, exit(0);
        }
    }
    cout << -1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 1e6 + 5;
const ll INF = 1e9 + 7;

ll n;
ii a[N];

bool check(const ll &x) {
    for (ll i = 1; i < n; i++)
        if (a[i].first % x != 0 && a[i].second % x != 0) return false;
    return true;
}

int main() {
    //freopen("1025B.INP", "r", stdin);
    //freopen("1025B.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i < n; i++) cin >> a[i].first >> a[i].second;
    //********************
    ll x, y; cin >> x >> y;
    for (ll i = 2; i <= sqrt(x); i++) if (x % i == 0) {
        if (check(x)) cout << x, exit(0);
        if (check(x / i)) cout << x / i, exit(0);
    }
    for (ll i = 2; i <= sqrt(y); i++) if (y % i == 0) {
        if (check(y)) cout << y, exit(0);
        if (check(y / i)) cout << y / i, exit(0);
    }
    cout << -1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 1e6 + 5;
const ll INF = 1e9 + 7;

ll n;
ii a[N];
vector<ll> tsnt;
bool check(const ll &x) {
    for (ll i = 1; i < n; i++)
        if (a[i].first % x != 0 && a[i].second % x != 0) return false;
    return true;
}

int main() {
    //freopen("1025B.INP", "r", stdin);
    //freopen("1025B.OUT", "w", stdout);

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i < n; i++) cin >> a[i].first >> a[i].second;
    //********************
    ll x, y; cin >> x >> y;
    if(n == 1) cout << x,exit(0);
    for(int i = 2;i<=sqrt(x);i++){
        if(x % i == 0){
            while(x % i == 0) x/=i;
            tsnt.push_back(i);
        }
    }
    for(int i = 2;i<=sqrt(y);i++){
        if(y % i == 0){
            while(y % i == 0) y/=i;
            tsnt.push_back(i);
        }
    }
    for(int i = 0;i<tsnt.size();i++)
        if(check(tsnt[i])) cout << tsnt[i],exit(0);
    cout << -1;
    return 0;
}


#include <bits/stdc++.h>
#include <bitset>

using namespace std;

typedef int ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 1e6 + 5;
const ll INF = 1e9 + 7;

ll n;

bitset <1000000005> dd;
map <ll, ll> C;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i <= n - 1; i++) {
        ll x, y; cin >> x >> y;
        dd.reset();
        for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (x % j == 0) {
            if (dd[j] == false) {
                C[j]++; dd[j] = true;
            }
            if (dd[x / j] == false && j != x / j) {
                C[x / j]++; dd[j] = true;
            }
        }
        for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (y % j == 0) {
            if (dd[j] == false) {
                C[j]++; dd[j] = true;
            }
            if (dd[y / j] == false && j != y / j) {
                C[y / j]++; dd[j] = true;
            }
        }
    }
    //********************
    ll x, y; cin >> x >> y;
    dd.reset();
    for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (x % j == 0) {
        if (dd[j] == false) {
            C[j]++; dd[j] = true;
            if (C[j] == n) cout << j, exit(0);
        }
        if (dd[x / j] == false && j != x / j) {
            C[x / j]++; dd[j] = true;
            if (C[x / j] == n) cout << x / j, exit(0);
        }
    }
    for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (y % j == 0) {
        if (dd[j] == false) {
            C[j]++; dd[j] = true;
            if (C[j] == n) cout << j, exit(0);
        }
        if (dd[y / j] == false && j != y / j) {
            C[y / j]++; dd[j] = true;
            if (C[y / j] == n) cout << y / j, exit(0);
        }
    }
    cout << -1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 1e6 + 5;
const ll INF = 1e9 + 7;

ll n;
ii a[N];
vector<ll> tsnt;
bool check(const ll &x) {
    for (ll i = 1; i < n; i++)
        if (a[i].first % x != 0 && a[i].second % x != 0) return false;
    return true;
}

int main() {
    //freopen("1025B.INP", "r", stdin);
    //freopen("1025B.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i < n; i++) cin >> a[i].first >> a[i].second;
    //********************
    ll x, y; cin >> x >> y;
    if(n == 1) cout << x,exit(0);
    for(int i = 2;i<=sqrt(x);i++){
        if(x % i == 0){
            while(x % i == 0) x/=i;
            tsnt.push_back(i);
        }
    }
    for(int i = 2;i<=sqrt(y);i++){
        if(y % i == 0){
            while(y % i == 0) y/=i;
            tsnt.push_back(i);
        }
    }
    for(int i = 0;i<tsnt.size();i++)
        if(check(tsnt[i])) cout << tsnt[i],exit(0);
    if(x > 1 && check(x)) cout << x,exit(0);
    if(y > 1 && check(y)) cout << y,exit(0);
    cout << -1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 1e8 + 5;
const ll INF = 1e9 + 7;

ll n;
ii a[N];

bool check(const ll &x) {
    for (ll i = 1; i < n; i++)
        if (a[i].first % x != 0 && a[i].second % x != 0) return false;
    return true;
}

int main() {
    //freopen("1025B.INP", "r", stdin);
    //freopen("1025B.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i < n; i++) cin >> a[i].first >> a[i].second;
    //********************
    ll x, y; cin >> x >> y;
    for (ll i = 2; i <= sqrt(x); i++) if (x % i == 0) {
        if (check(x)) cout << x, exit(0);
        if (check(x / i)) cout << x / i, exit(0);
    }
    for (ll i = 2; i <= sqrt(y); i++) if (y % i == 0) {
        if (check(y)) cout << y, exit(0);
        if (check(y / i)) cout << y / i, exit(0);
    }
    cout << -1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 1e6 + 5;
const ll INF = 1e9 + 7;

ll n;

map <ll, bool> dd;
map <ll, ll> C;

int main() {
    //freopen("1025B.INP", "r", stdin);
    //freopen("1025B.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i <= n - 1; i++) {
        ll x, y; cin >> x >> y;
        dd.clear();
        for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (x % j == 0) {
            if (dd[j] == false) {
                C[j]++; dd[j] = true;
            }
            if (dd[x / j] == false && j != x / j) {
                C[x / j]++; dd[j] = true;
            }
        }
        for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (y % j == 0) {
            if (dd[j] == false) {
                C[j]++; dd[j] = true;
            }
            if (dd[y / j] == false && j != y / j) {
                C[y / j]++; dd[j] = true;
            }
        }
    }
    //********************
    ll x, y; cin >> x >> y;
    dd.clear();
    for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (x % j == 0) {
        if (dd[j] == false) {
            C[j]++; dd[j] = true;
            if (C[j] == n) cout << j, exit(0);
        }
        if (dd[x / j] == false && j != x / j) {
            C[x / j]++; dd[j] = true;
            if (C[x / j] == n) cout << x / j, exit(0);
        }
    }
    for (ll j = 2; j <= (ll)(sqrt(x)); j++) if (y % j == 0) {
        if (dd[j] == false) {
            C[j]++; dd[j] = true;
            if (C[j] == n) cout << j, exit(0);
        }
        if (dd[y / j] == false && j != y / j) {
            C[y / j]++; dd[j] = true;
            if (C[y / j] == n) cout << y / j, exit(0);
        }
    }
    cout << -1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 1e6 + 5;
const ll INF = 1e9 + 7;

ll n;
ii a[N];

bool check(const ll &x) {
    for (ll i = 1; i < n; i++)
        if (a[i].first % x != 0 && a[i].second % x != 0) return false;
    return true;
}

int main() {
    //freopen("1025B.INP", "r", stdin);
    //freopen("1025B.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i < n; i++) cin >> a[i].first >> a[i].second;
    //********************
    ll x, y; cin >> x >> y;
    for (ll i = 2; i <= sqrt(x); i++) if (x % i == 0) {
        if (check(x)) cout << x, exit(0);
        if (check(x / i)) cout << x / i, exit(0);
    }
    for (ll i = 2; i <= sqrt(y); i++) if (y % i == 0) {
        if (check(y)) cout << y, exit(0);
        if (check(y / i)) cout << y / i, exit(0);
    }
    //********************
    if (x > 1 && check(x)) cout << x, exit(0);
    if (y > 1 && check(y)) cout << y, exit(0);
    cout << -1;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll, ll> ii;
typedef pair <ll, ii> iii;

const ll N = 1e6 + 5;
const ll INF = 1e9 + 7;

ll n;
ii a[N];
vector<ll> tsnt;
bool check(const ll &x) {
    for (ll i = 1; i < n; i++)
        if (a[i].first % x != 0 && a[i].second % x != 0) return false;
    return true;
}

int main() {
    //freopen("1025B.INP", "r", stdin);
    //freopen("1025B.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (ll i = 1; i < n; i++) cin >> a[i].first >> a[i].second;
    //********************
    ll x, y; cin >> x >> y;
    for(int i = 2;i<=sqrt(x);i++){
        if(x % i == 0){
            while(x % i == 0) x/=i;
            tsnt.push_back(i);
        }
    }
    for(int i = 2;i<=sqrt(y);i++){
        if(y % i == 0){
            while(y % i == 0) y/=i;
            tsnt.push_back(i);
        }
    }
    for(int i = 0;i<tsnt.size();i++)
        if(check(tsnt[i])) cout << tsnt[i],exit(0);
    cout << -1;
    return 0;
}


/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
const ll maxn = 2e5;
ll a[maxn],b[maxn];
int main(){
    FAST
    ll i;
    int n;
    cin >> n;
    for(i = 0; i < n; ++i)cin >> a[i] >> b[i];
    V<ll> v;
    for(i = 2; i < maxn/2; ++i){
        if(a[0]%i == 0 && a[0]){
            v.pb(i);
            while(a[0]%i == 0 && a[0])a[0]/=i;
        }
        if(b[0]%i == 0 && b[0]){
            v.pb(i);
            while(b[0]%i == 0 && b[0])b[0]/=i;
        }
    }
    if(a[0]>1)v.pb(a[0]);
    if(b[0]>1)v.pb(b[0]);
    for(int i = 0; i < v.size(); ++i){
        int ok = 1;
        for(int j = 1; j < n; ++j){
            if(__gcd(a[j],v[i]) == 1 && __gcd(b[j],v[i])){
                ok = 0;
                break;
            }
        }
        if(ok){
            cout << v[i];
            return 0;
        }
    }
    return 0;
}


/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
const ll maxn = sqrt(3e9);
int main(){
    FAST
    int tp[maxn] = {0};
    for(int i = 2; i < maxn; ++i)if(tp[i] == 0)for(int j = 2; i*j <= maxn; ++j)tp[i*j]++;
    V<ll> pr;
    for(int i = 2; i < maxn; ++i)if(tp[i] == 0)pr.pb(i);
    int n;
    cin >> n;
    set<ll> s;
    ll a,b;
    cin >> a >> b;
    for(ll j = 2; j*j <= a; ++j){
        if(a%j == 0){
            s.insert(j);
        }
    }
    for(ll j = 2; j*j <= b; ++j){
        if(b%j == 0){
            s.insert(j);
        }
    }
    for(int i = 1; i < n; ++i){
        cin >> a >> b;
        set<ll> tmp;
        for(ll j = 2; j*j <= a; ++j){
            if(a%j == 0){
                if(binary_search(pr.begin(),pr.end(),j) && binary_search(s.begin(),s.end(),j))tmp.insert(j);
            }
        }
        for(ll j = 2; j*j <= b; ++j){
            if(b%j == 0){
                if(binary_search(pr.begin(),pr.end(),j) && binary_search(s.begin(),s.end(),j))tmp.insert(j);
            }
        }
        s = tmp;
        if(s.size() == 0){
            cout << "-1";
            return 0;
        }
    }
    cout << *s.begin();
    return 0;
}


/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
const ll maxn = 2e5;
ll a[maxn],b[maxn];
int main(){
    FAST
    ll i;
    int n;
    cin >> n;
    for(i = 0; i < n; ++i)cin >> a[i] >> b[i];
    set<ll> s;
    for(int i = 2; i <= 1000000; ++i){
        if(a[0]%i == 0 && a[0]){
            s.insert(i);
            while(a[0] && a[0]%i == 0)a[0]/=i;
        }
        if(b[0]%i == 0 && b[0]){
            s.insert(i);
            while(b[0] && b[0]%i == 0)b[0]/=i;
        }
    }
    if(a[0] > 1)s.insert(a[0]);
    if(b[0] > 1)s.insert(b[0]);
    for(set<ll>::iterator itr = s.begin(); itr != s.end(); ++itr){
        ll no = *itr;
        int ok = 1;
        for(int i = 1; i < n; ++i){
            if(a[i]%no != 0 && b[i]%no != 0){
                ok = 0;
                break;
            }
        }
        if(ok){
            cout << *itr;
            return 0;
        }
    }
    cout << "-1";
    return 0;
}


/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
const ll maxn = 2e5;
ll a[maxn],b[maxn];
int main(){
    FAST
    ll i;
    int n;
    cin >> n;
    for(i = 0; i < n; ++i)cin >> a[i] >> b[i];
    set<ll> s;
    for(int i = 2; i <= 100000; ++i){
        if(a[0]%i == 0 && a[0]){
            s.insert(i);
            while(a[0] && a[0]%i == 0)a[0]/=i;
        }
        if(b[0]%i == 0 && b[0]){
            s.insert(i);
            while(b[0] && b[0]%i == 0)b[0]/=i;
        }
    }
    for(set<ll>::iterator itr = s.begin(); itr != s.end(); ++itr){
        ll no = *itr;
        int ok = 1;
        for(int i = 1; i < n; ++i){
            if(a[i]%no != 0 && b[i]%no != 0){
                ok = 0;
                break;
            }
        }
        if(ok){
            cout << *itr;
            return 0;
        }
    }
    cout << "-1";
    return 0;
}


/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define MAXN 25
using namespace std;
int main(){
    int n;
    cin >> n;
    PLL p[200000];
    cin >> p[0].first >> p[0].second;
    ll g1,g2,g3,g4;
    g1 = p[0].first;
    g2 = p[0].first;
    g3 = p[0].second;
    g4 = p[0].second;
    loop(i,1,n){
        cin >> p[i].first >> p[i].second;
        g1 = __gcd(p[i].first,g1);
        g2 = __gcd(p[i].first,g2);
        g3 = __gcd(p[i].second,g3);
        g4 = __gcd(p[i].second,g4);
    }
    if(g1 == 1 && g2 == 1 && g3 == 1 && g4 == 1){
        cout << "-1";
    }else{
        if(g1!=1)cout << g1;
        else if(g2!=1)cout << g2;
        else if(g3!=1)cout << g3;
        else if(g4!=1)cout << g4;
    }
    return 0;
}


/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define MAXN 25
using namespace std;
int main(){
    FAST
    int n;
    cin >> n;
    PLL p[200000];
    set<ll> s;
    cin >> p[0].first >> p[0].second;
    s.insert(p[0].first);
    s.insert(p[0].second);
    loop(i,1,n){
        cin >> p[i].first >> p[i].second;
        // cout << i << endl;
        set<ll> new_set;
        for(set<ll>::iterator itr = s.begin(); itr != s.end(); ++itr){
            ll a = __gcd(*itr,p[i].first);
            ll b = __gcd(*itr,p[i].second);
            if(a != 1)new_set.insert(a);
            if(b != 1)new_set.insert(b);
        }
        s = new_set;
        if(s.size() == 0){
            cout << "-1";
            return 0;
        }
    }
    cout << *s.begin();
    return 0;
}


/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
const ll maxn = 2e5;
ll a[maxn],b[maxn];
int main(){
    FAST
    ll i;
    int n;
    cin >> n;
    for(i = 0; i < n; ++i)cin >> a[i] >> b[i];
    V<ll> v;
    for(i = 2; i < maxn/2; ++i){
        if(a[0]%i == 0 && a[0]){
            v.pb(i);
            while(a[0]%i == 0 && a[0])a[0]/=i;
        }
        if(b[0]%i == 0 && b[0]){
            v.pb(i);
            while(b[0]%i == 0 && b[0])b[0]/=i;
        }
    }
    if(a[0]>1)v.pb(a[0]);
    if(b[0]>1)v.pb(b[0]);
    for(int i = 0; i < v.size(); ++i){
        int ok = 1;
        for(int j = 1; j < n; ++j){
            if(__gcd(a[j],v[i]) == 1 && __gcd(b[j],v[i])){
                ok = 0;
                break;
            }
        }
        if(ok){
            cout << v[i];
            return 0;
        }
    }
    cout << "-1";
    return 0;
}


/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
const ll maxn = 2e5;
ll a[maxn],b[maxn];
int main(){
    FAST
    ll i;
    int n;
    cin >> n;
    for(i = 0; i < n; ++i)cin >> a[i] >> b[i];
    set<ll> s;
    for(int i = 2; i <= 1000000; ++i){
        if(a[0]%i == 0 && a[0]){
            s.insert(i);
            while(a[0] && a[0]%i == 0)a[0]/=i;
        }
        if(b[0]%i == 0 && b[0]){
            s.insert(i);
            while(b[0] && b[0]%i == 0)b[0]/=i;
        }
    }
    for(set<ll>::iterator itr = s.begin(); itr != s.end(); ++itr){
        ll no = *itr;
        int ok = 1;
        for(int i = 1; i < n; ++i){
            if(a[i]%no != 0 && b[i]%no != 0){
                ok = 0;
                break;
            }
        }
        if(ok){
            cout << *itr;
            return 0;
        }
    }
    cout << "-1";
    return 0;
}


/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
const ll maxn = sqrt(3e9);
int main(){
    FAST
    int tp[maxn] = {0};
    for(int i = 2; i < maxn; ++i)if(tp[i] == 0)for(int j = 2; i*j <= maxn; ++j)tp[i*j]++;
    V<ll> pr;
    for(int i = 2; i < maxn; ++i)if(tp[i] == 0)pr.pb(i);
    int n;
    cin >> n;
    set<ll> s;
    ll a,b;
    cin >> a >> b;
    for(ll j = 0; pr[j] <= a && j < pr.size(); ++j){
        if(a%pr[j] == 0){
            s.insert(pr[j]);
        }
        if(b%pr[j] == 0){
            s.insert(pr[j]);
        }
    }
    for(int i = 1; i < n; ++i){
        cin >> a >> b;
        set<ll> tmp;
        for(ll j = 0; pr[j] <= a && j < pr.size(); ++j){
            if(a%pr[j] == 0){
                if(binary_search(s.begin(),s.end(),pr[j]))tmp.insert(pr[j]);
            }
            if(b%pr[j] == 0){
                if(binary_search(s.begin(),s.end(),pr[j]))tmp.insert(pr[j]);
            }
        }
        s = tmp;
        if(s.size() == 0){
            cout << "-1";
            return 0;
        }
    }
    cout << *s.begin();
    return 0;
}


/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define MAXN 25
using namespace std;
int main(){
    FAST
    int n;
    cin >> n;
    set<ll> s;
    ll a,b;
    cin >> a >> b;
    for(ll j = 2; j*j <= a; ++j){
        if(a%j == 0){
            s.insert(j);
        }
    }
    for(ll j = 2; j*j <= b; ++j){
        if(b%j == 0){
            s.insert(j);
        }
    }
    for(int i = 1; i < n; ++i){
        cin >> a >> b;
        set<ll> tmp;
        for(ll j = 2; j*j <= a; ++j){
            if(a%j == 0){
                if(binary_search(s.begin(),s.end(),j))tmp.insert(j);
            }
        }
        for(ll j = 2; j*j <= b; ++j){
            if(b%j == 0){
                if(binary_search(s.begin(),s.end(),j))tmp.insert(j);
            }
        }
        s = tmp;
        if(s.size() == 0){
            cout << "-1";
            return 0;
        }
    }
    cout << *s.begin();
    return 0;
}


/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
const ll maxn = 2e5;
ll a[maxn],b[maxn];
int main(){
    FAST
    ll i;
    int n;
    cin >> n;
    for(i = 0; i < n; ++i)cin >> a[i] >> b[i];
    V<ll> v;
    for(i = 2; i < maxn/2; ++i){
        if(a[0]%i == 0 && a[0]){
            v.pb(i);
            while(a[0]%i == 0 && a[0])a[0]/=i;
        }
        if(b[0]%i == 0 && b[0]){
            v.pb(i);
            while(b[0]%i == 0 && b[0])b[0]/=i;
        }
    }
    if(a[0]>1)v.pb(a[0]);
    if(b[0]>1)v.pb(b[0]);
    for(int i = 0; i < v.size(); ++i){
        int ok = 1;
        for(int j = 1; j < n; ++j){
            if(a[j]%v[i] != 0 && a[j]%v[i] != 0){
                ok = 0;
                break;
            }
        }
        if(ok){
            cout << v[i];
            return 0;
        }
    }
    cout << "-1";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define mp make_pair
#define pb push_back
#define f first
#define s second

const ll MAX = 2000000001;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n = 0;
    cin >> n;
    ll x, y;
    set<ll> left, right, oldleft, oldright;
    for (int i = 0; i < n; i++) {
        cin >> x >> y;
        if (i == 0) {
            left.insert(x);
            right.insert(y);
        }
        else {
            oldleft = left, oldright = right;
            left.clear();
            right.clear();
            for (auto &a : oldleft) {
                left.insert(gcd(x, a));
                right.insert(gcd(y, a));
            }
            for (auto &b : oldright) {
                left.insert(gcd(y, b));
                right.insert(gcd(y, b));
            }
        }
    }
    left.erase(1); right.erase(1);
    if (left.size() > 0) cout << *left.begin() << endl;
    else if (right.size() > 0) cout << *right.begin() << endl;
    else cout << -1 << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define mp make_pair
#define pb push_back
#define f first
#define s second

const ll MAX = 2000000001;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n = 0;
    cin >> n;
    int x, y;
    int valx, valy, oldvalx, oldvaly;
    for (int i = 0; i < n; i++) {
        cin >> x >> y;
        if (i == 0) valx = x, valy = y;
        else {
            oldvalx = valx, oldvaly = valy;
            valx = max(gcd(x, valx), gcd(x, valy));
            valy = max(gcd(y, valx), gcd(y, valy));
        }
    }
    if (max(valx, valy) == 1) cout << -1 << endl;
    else cout << max(valx, valy) << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define mp make_pair
#define pb push_back
#define f first
#define s second

const ll MAX = 2000000001;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n = 0;
    cin >> n;
    ll x, y;
    vl c(n);
    set<ll> left, right, oldleft, oldright;
    for (int i = 0; i < n; i++) {
        cin >> x >> y;
        c[i] = lcm(x, y);
    }
    ll g = c[0];
    for (int i = 1; i < n; i++) {
        g = gcd(g, c[i]);
    }
    cout << g << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define mp make_pair
#define pb push_back
#define f first
#define s second

const ll MAX = 2000000001;
const ll MAX_SQRT = sqrt(MAX) + 1;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    bool sieve[MAX_SQRT];
    memset(sieve, true, sizeof(sieve));
    set<int> primes;
    for (int p = 2; p < MAX_SQRT; p++) {
        if (sieve[p]) {
            primes.insert(p);
            for (int i = p * 2; i < MAX_SQRT; i += p) {
                sieve[i] = false;
            }
        }
    }
    set<int> ans(primes);

    int n = 0;
    cin >> n;
    int x, y;
    int smallest = MAX + 1;
    for (int i = 0; i < n; i++) {
        cin >> x >> y;
        smallest = min(smallest, min(x, y));
        for (auto &a : primes) {
            if (a > smallest) break;
            if ((x % a != 0) and (y % a != 0)) {
                ans.erase(a);
            }
        }
    }

    if (*ans.begin() > smallest) cout << -1 << endl;
    else cout << *ans.begin() << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define mp make_pair
#define pb push_back
#define f first
#define s second

const ll MAX = 2000000001;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n = 0;
    cin >> n;
    ll x, y;
    vi c(n);
    set<ll> left, right, oldleft, oldright;
    for (int i = 0; i < n; i++) {
        cin >> x >> y;
        c[i] = lcm(x, y);
    }
    int g = c[0];
    for (int i = 1; i < n; i++) {
        g = gcd(g, c[i]);
    }
    if (g == 1) cout << -1 << endl;
    else cout << g << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define mp make_pair
#define pb push_back
#define f first
#define s second

const ll MAX = 2000000001;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n = 0;
    cin >> n;

    unordered_set<int> okay;
    int x, y;

    cin >> x >> y;
    if (x % 2 == 0 or y % 2 == 0) okay.insert(2);
    for (int j = 3; j <= sqrt(max(x, y)); j += 2) {
        if (x % j == 0 or y % j == 0) okay.insert(j);
    }
    for (int i = 1; i < n; i++) {
        cin >> x >> y;
        for (auto &a : okay) if (x % a and y % a) okay.erase(a);
    }

    for (auto &x : okay) {
        cout << x << endl;
        break;
    }
    if (okay.size() == 0) cout << -1 << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define mp make_pair
#define pb push_back
#define f first
#define s second

const ll MAX = 2000000001;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n = 0;
    cin >> n;
    ll x, y;
    set<ll> left, right, oldleft, oldright;
    for (int i = 0; i < n; i++) {
        cin >> x >> y;
        if (i == 0) {
            left.insert(x);
            right.insert(y);
        }
        else {
            oldleft = left, oldright = right;
            left.clear();
            right.clear();
            for (auto &a : oldleft) left.insert(gcd(x, a));
            for (auto &b : oldright) right.insert(gcd(y, b));
        }
    }
    left.erase(1); right.erase(1);
    if (left.size() > 0) cout << *left.begin() << endl;
    else if (right.size() > 0) cout << *right.begin() << endl;
    else cout << -1 << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define mp make_pair
#define pb push_back
#define f first
#define s second

const ll MAX = 2000000001;
const int MAX_SQRT = sqrt(MAX) + 1;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    bool sieve[MAX_SQRT];
    memset(sieve, true, sizeof(sieve));
    set<int> primes;
    for (int p = 2; p*p < MAX_SQRT; p++) {
        if (sieve[p]) {
            primes.insert(p);
            for (int i = p * 2; i < MAX_SQRT; i += p) {
                sieve[i] = false;
            }
        }
    }
    set<int> ans(primes);

    int n = 0;
    cin >> n;
    int x, y;
    for (int i = 0; i < n; i++) {
        cin >> x >> y;
        for (auto &a : primes) {
            if ((x % a != 0) and (y % a != 0)) {
                ans.erase(a);
                cout << a << endl;
            }
        }
        if (ans.size() == 0) {
            cout << -1 << endl;
            return 0;
        }
    }

    for (auto &x : ans) {
        cout << x << endl;
        break;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define mp make_pair
#define pb push_back
#define f first
#define s second

const ll MAX = 2000000001;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n = 0;
    cin >> n;
    ll x, y;
    vl c(n);
    set<ll> left, right, oldleft, oldright;
    for (int i = 0; i < n; i++) {
        cin >> x >> y;
        c[i] = lcm(x, y);
    }
    ll g = c[0];
    for (int i = 1; i < n; i++) {
        g = gcd(g, c[i]);
    }
    if (g == 1) cout << -1 << endl;
    else cout << g << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define mp make_pair
#define pb push_back
#define f first
#define s second

const ll MAX = 2000000001;
const ll MAX_SQRT = sqrt(MAX) + 1;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    bool sieve[MAX_SQRT];
    memset(sieve, true, sizeof(sieve));
    set<int> primes;
    for (int p = 2; p < MAX_SQRT; p++) {
        if (sieve[p]) {
            primes.insert(p);
            for (int i = p * 2; i < MAX_SQRT; i += p) {
                sieve[i] = false;
            }
        }
    }
    set<int> ans(primes);

    int n = 0;
    cin >> n;
    int x, y;
    for (int i = 0; i < n; i++) {
        cin >> x >> y;
        for (auto &a : primes) {
            if ((x % a != 0) and (y % a != 0)) {
                ans.erase(a);
            }
        }
        if (ans.size() == 0) {
            cout << -1 << endl;
            return 0;
        }
    }

    for (auto &x : ans) {
        cout << x << endl;
        break;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define mp make_pair
#define pb push_back
#define f first
#define s second

const ll MAX = 2000000001;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n = 0;
    cin >> n;
    ll x, y;
    vl a(n), b(n);
    set<ll> left, right, oldleft, oldright;
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    ll g = lcm(a[0], b[0]);
    for (int i = 1; i < n; i++) {
        g = gcd(g, lcm(a[i], b[i]));
    }
    if (g == 1) {
        cout << -1 << endl;
        return 0;
    }
    for (int i = 0; i < n; ++i) {
		if (gcd(g, a[i]) > 1) g = gcd(g, a[i]);
		else g = gcd(g, b[i]);
	}
    cout << g << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define mp make_pair
#define pb push_back
#define f first
#define s second

const ll MAX = 2000000001;
const int MAX_SQRT = sqrt(MAX) + 1;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    bool sieve[MAX_SQRT];
    memset(sieve, true, sizeof(sieve));
    set<int> primes;
    for (int p = 2; p*p < MAX_SQRT; p++) {
        if (sieve[p]) {
            primes.insert(p);
            for (int i = p * 2; i < MAX_SQRT; i += p) {
                sieve[i] = false;
            }
        }
    }
    set<int> ans(primes);

    int n = 0;
    cin >> n;
    int x, y;
    for (int i = 0; i < n; i++) {
        cin >> x >> y;
        for (auto &a : primes) {
            if ((x % a != 0) and (y % a != 0)) {
                ans.erase(a);
            }
        }
        if (ans.size() == 0) {
            cout << -1 << endl;
            return 0;
        }
    }

    for (auto &x : ans) {
        cout << x << endl;
        break;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define mp make_pair
#define pb push_back
#define f first
#define s second

const ll MAX = 2000000001;
const ll MAX_SQRT = sqrt(MAX) + 1;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    bool sieve[MAX_SQRT];
    memset(sieve, true, sizeof(sieve));
    set<int> primes;
    for (int p = 2; p < MAX_SQRT; p++) {
        if (sieve[p]) {
            primes.insert(p);
            for (int i = p * 2; i < MAX_SQRT; i += p) {
                sieve[i] = false;
            }
        }
    }
    set<int> ans(primes);

    int n = 0;
    cin >> n;
    vii pairs;
    int x, y;
    int smallest = MAX + 1;
    for (int i = 0; i < n; i++) {
        cin >> x >> y;
        smallest = min(smallest, min(x, y));
        pairs.pb(mp(x, y));
    }

    if (smallest == 1) {
        cout << -1 << endl;
        return 0;
    }

    for (auto &p : pairs) {
        for (auto &a : primes) {
            if (a > smallest) break;
            if ((p.f % a != 0) and (p.s % a != 0)) {
                ans.erase(a);
            }
        }
    }

    if (*ans.begin() > smallest) cout << -1 << endl;
    else cout << *ans.begin() << endl;
}


#include <bits/stdc++.h>
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define MAXN
using namespace std;
typedef long long LL;
vector<LL> vec;

int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(NULL);
    //cout.tie(NULL);
    int n;
    cin>>n;
    LL a,b;
    scanf("%lld%lld",&a,&b);
    LL now=2;
    vec.push_back(a);
    vec.push_back(b);
    LL ta=sqrt(a)+1,tb=sqrt(b)+1;
    while(a!=1&&now<=ta){
        if(a%now==0){
            vec.push_back(now);
            while(a%now==0){
                a/=now;
            }
        }
        ++now;
    }
    now=2;
    while(b!=1&&now<=tb){
        if(b%now==0){
            vec.push_back(now);
            while(b%now==0){
                b/=now;
            }
        }
        ++now;
    }
    for(int i=0;i<n;++i){
        scanf("%lld%lld",&a,&b);
        for(int j=0;j<vec.size();++j){
            if(a%vec[j]!=0&&b%vec[j]!=0){
                vec.erase(vec.begin()+j);
                --j;
            }
        }
    }
    if(!vec.empty()){
        cout<<vec[0]<<endl;
    }else{
        cout<<-1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define MAXN
using namespace std;
typedef long long LL;

LL gcd(LL a, LL b){
    if(b == 0) return a;
    else return gcd(b, a%b);
}

int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(NULL);
    //cout.tie(NULL);
    int n;
    cin>>n;
    LL a,b,tmp=0;
    for(int i=0;i<n;++i){
        scanf("%lld%lld",&a,&b);
        LL t=a*b/gcd(a,b);
        if(i==0){
            tmp=t;
        }else{
            tmp=gcd(tmp,t);
        }
    }
    if(tmp==1){
        cout<<-1<<endl;
        return 0;
    }else{
        for(LL i=2;i<=sqrt(tmp);++i){
            if(tmp%i==0){
                cout<<i<<endl;
                return 0;
            }
        }
        cout<<tmp<<endl;
        return 0;
    }
}


#include <bits/stdc++.h>
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define MAXN
using namespace std;
typedef long long LL;
vector<int> vec;
bool vis[100000];

int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(NULL);
    //cout.tie(NULL);
    int n;
    cin>>n;
    int a,b;
    scanf("%d%d",&a,&b);
    int now=2;
    while(a!=1&&now<=sqrt(a)+1){
        if(a%now==0){
            vis[now]=true;
            vec.push_back(now);
            while(a%now==0){
                a/=now;
            }
        }
        ++now;
    }
    vec.push_back(a);
    now=2;
    while(b!=1&&now<=sqrt(b)+1){
        if(b%now==0){
            if(!vis[now])
            vec.push_back(now);
            while(b%now==0){
                b/=now;
            }
        }
        ++now;
    }
    vec.push_back(b);
    for(int i=0;i<n;++i){
        scanf("%d%d",&a,&b);
        for(int j=0;j<vec.size();++j){
            if(a%vec[j]!=0&&b%vec[j]!=0){
                vec.erase(vec.begin()+j);
                --j;
            }
        }
    }
    if(vec.size()!=0){
        cout<<vec[0]<<endl;
    }else{
        cout<<-1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define MAXN
using namespace std;
typedef long long LL;
vector<int> vec;
bool vis[100000];

int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(NULL);
    //cout.tie(NULL);
    int n;
    cin>>n;
    int a,b;
    scanf("%d%d",&a,&b);
    int now=2;
    while(a!=1){
        if(a%now==0){
            vis[now]=true;
            vec.push_back(now);
            while(a%now==0){
                a/=now;
            }
        }
        ++now;
    }
    now=2;
    while(b!=1){
        if(b%now==0){
            if(!vis[now])
            vec.push_back(now);
            while(b%now==0){
                b/=now;
            }
        }
        ++now;
    }
    for(int i=0;i<n;++i){
        scanf("%d%d",&a,&b);
        for(int j=0;j<vec.size();++j){
            if(a%vec[j]!=0&&b%vec[j]!=0){
                vec.erase(vec.begin()+j);
                --j;
            }
        }
    }
    if(vec.size()!=0){
        cout<<vec[0]<<endl;
    }else{
        cout<<-1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define MAXN
using namespace std;
typedef long long LL;
vector<LL> vec;

int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(NULL);
    //cout.tie(NULL);
    int n;
    cin>>n;
    LL a,b;
    scanf("%lld%lld",&a,&b);
    LL now=2;
    vec.push_back(a);
    vec.push_back(b);
    LL ta=sqrt(a)+1,tb=sqrt(b)+1;
    while(a!=1&&now<=ta){
        if(a%now==0){
            vec.push_back(now);
            while(a%now==0){
                a/=now;
            }
        }
        ++now;
    }
    now=2;
    while(b!=1&&now<=tb){
        if(b%now==0){
            vec.push_back(now);
            while(b%now==0){
                b/=now;
            }
        }
        ++now;
    }
    for(int i=1;i<n;++i){
        scanf("%lld%lld",&a,&b);
        for(int j=0;j<vec.size();){
            if(a%vec[j]!=0&&b%vec[j]!=0){
                vec.erase(vec.begin()+j);
            }else{
                ++j;
            }
        }
    }
    if(!vec.empty()){
        cout<<vec[0]<<endl;
    }else{
        cout<<-1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define MAXN
using namespace std;
typedef long long LL;
vector<LL> vec;

LL gcd(LL a, LL b){
    if(b == 0) return a;
    else return gcd(b, a%b);
}

int main(){
    int n;
    cin>>n;
    LL a,b;
    scanf("%lld%lld",&a,&b);
    LL tmp=gcd(a,b);
    if(n==1){
        if(tmp==1){
            cout<<-1<<endl;
        }else{
            cout<<tmp<<endl;
        }
        return 0;
    }
    LL now=2;
    vec.push_back(a);
    vec.push_back(b);
    LL ta=sqrt(a)+2,tb=sqrt(b)+2;
    while(a!=1&&now<=min(a,ta)){
        if(a%now==0){
            vec.push_back(now);
            while(a%now==0){
                a/=now;
            }
        }
        ++now;
    }
    now=2;
    while(b!=1&&now<=min(b,tb)){
        if(b%now==0){
            vec.push_back(now);
            while(b%now==0){
                b/=now;
            }
        }
        ++now;
    }
    for(int i=1;i<n;++i){
        scanf("%lld%lld",&a,&b);
        for(int j=0;j<vec.size();){
            if(a%vec[j]!=0&&b%vec[j]!=0){
                vec.erase(vec.begin()+j);
            }else{
                ++j;
            }
        }
    }
    if(!vec.empty()){
        cout<<vec[0]<<endl;
    }else{
        cout<<-1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define MAXN
using namespace std;
typedef long long LL;
vector<int> vec;

int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(NULL);
    //cout.tie(NULL);
    int n;
    cin>>n;
    int a,b;
    scanf("%d%d",&a,&b);
    int now=2;
    vec.push_back(a);
    vec.push_back(b);
    int ta=sqrt(a)+1,tb=sqrt(b)+1;
    while(a!=1&&now<=ta){
        if(a%now==0){
            vec.push_back(now);
            while(a%now==0){
                a/=now;
            }
        }
        ++now;
    }
    now=2;
    while(b!=1&&now<=tb){
        if(b%now==0){
            vec.push_back(now);
            while(b%now==0){
                b/=now;
            }
        }
        ++now;
    }
    for(int i=0;i<n;++i){
        scanf("%d%d",&a,&b);
        for(int j=0;j<vec.size();++j){
            if(a%vec[j]!=0&&b%vec[j]!=0){
                vec.erase(vec.begin()+j);
                --j;
            }
        }
    }
    if(!vec.empty()){
        cout<<vec[0]<<endl;
    }else{
        cout<<-1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define MAXN
using namespace std;
typedef long long LL;
vector<int> vec;
bool vis[100000];

int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(NULL);
    //cout.tie(NULL);
    int n;
    cin>>n;
    int a,b;
    scanf("%d%d",&a,&b);
    int now=2;
    vec.push_back(a);
    vec.push_back(b);
    while(a!=1&&now<=sqrt(a)+1){
        if(a%now==0){
            vis[now]=true;
            vec.push_back(now);
            while(a%now==0){
                a/=now;
            }
        }
        ++now;
    }
    now=2;
    while(b!=1&&now<=sqrt(b)+1){
        if(b%now==0){
            if(!vis[now])
            vec.push_back(now);
            while(b%now==0){
                b/=now;
            }
        }
        ++now;
    }
    for(int i=0;i<n;++i){
        scanf("%d%d",&a,&b);
        for(int j=0;j<vec.size();++j){
            if(a%vec[j]!=0&&b%vec[j]!=0){
                vec.erase(vec.begin()+j);
                --j;
            }
        }
    }
    if(vec.size()!=0){
        cout<<vec[0]<<endl;
    }else{
        cout<<-1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define MAXN
using namespace std;
typedef long long LL;
vector<LL> vec;

LL gcd(LL a, LL b){
    if(b == 0) return a;
    else return gcd(b, a%b);
}

int main(){
    int n;
    cin>>n;
    LL a,b;
    scanf("%lld%lld",&a,&b);
    LL tmp=gcd(a,b);
    if(n==1){
        cout<<a<<endl;
        return 0;
    }
    LL now=2;
    LL ta=sqrt(a)+2,tb=sqrt(b)+2;
    while(a!=1&&now<=min(a,ta)){
        if(a%now==0){
            vec.push_back(now);
            while(a%now==0){
                a/=now;
            }
        }
        ++now;
    }
    if(a>1)vec.push_back(a);
    now=2;
    while(b!=1&&now<=min(b,tb)){
        if(b%now==0){
            vec.push_back(now);
            while(b%now==0){
                b/=now;
            }
        }
        ++now;
    }
    if(b>1)
    vec.push_back(b);
    for(int i=1;i<n;++i){
        scanf("%lld%lld",&a,&b);
        for(int j=0;j<vec.size();){
            if(a%vec[j]!=0&&b%vec[j]!=0){
                vec.erase(vec.begin()+j);
            }else{
                ++j;
            }
        }
    }
    if(!vec.empty()){
        cout<<vec[0]<<endl;
    }else{
        cout<<-1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define MAXN
using namespace std;
typedef long long LL;
vector<LL> vec;

LL gcd(LL a, LL b){
    if(b == 0) return a;
    else return gcd(b, a%b);
}

int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(NULL);
    //cout.tie(NULL);
    int n;
    cin>>n;
    LL a,b;
    if(n==1){
        cin>>a>>b;
        LL tmp=gcd(a,b);
        if(tmp==1){
            cout<<-1<<endl;
        }else{
            cout<<tmp<<endl;
        }
        return 0;
    }
    scanf("%lld%lld",&a,&b);
    LL now=2;
    vec.push_back(a);
    vec.push_back(b);
    LL ta=sqrt(a)+1,tb=sqrt(b)+1;
    while(a!=1&&now<=ta){
        if(a%now==0){
            vec.push_back(now);
            while(a%now==0){
                a/=now;
            }
        }
        ++now;
    }
    now=2;
    while(b!=1&&now<=tb){
        if(b%now==0){
            vec.push_back(now);
            while(b%now==0){
                b/=now;
            }
        }
        ++now;
    }
    for(int i=1;i<n;++i){
        scanf("%lld%lld",&a,&b);
        for(int j=0;j<vec.size();){
            if(a%vec[j]!=0&&b%vec[j]!=0){
                vec.erase(vec.begin()+j);
            }else{
                ++j;
            }
        }
    }
    if(!vec.empty()){
        cout<<vec[0]<<endl;
    }else{
        cout<<-1<<endl;
    }
    return 0;
}