#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stack>
#include <cstring>
#include <iomanip>
#include <ctime>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k));
inline void pisz(int n) { printf("%d\n",n); }
template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; }
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);

#include <sys/time.h>
double getTime(){
   timeval tv;
   gettimeofday(&tv, 0);
   return tv.tv_sec + tv.tv_usec * 1e-6;
}

vector<bool> sito (int m) { // m = max_inclusive
   vector<bool> pr(m+1,1);
   pr[0] = pr[1] = 0;
   for (int x = 2; x*x <= m; ++x) if (pr[x]) for (int y = x*x; y <= m; y += x) pr[y] = 0;
   return pr;
}

vi listaPierwszych (int m) { // m = max_inclusive
   vector<bool> pr = sito(m);
   vi ans;
   FORI(i,m) if (pr[i]) ans.pb(i);
   return ans;
}

#define N 1000007
vi pr;
int n;
ll t[N];

int main () {
   //pr = listaPierwszych(1000000);
   scanf("%d", &n);
   FOR(i,n) scanf("%I64d", &t[i]);
   srand(time(0));
   double starttime = getTime();
   ll best = 0;
   while (getTime() - starttime < 3.5) {
      ll g = 0;
      FOR(u,6) g = __gcd(g, t[(rand() + RAND_MAX * rand()) % n]);
      int cnt = 0;
      FOR(i,n) if (t[i] % g == 0) ++cnt;
      if (2*cnt >= n) REMAX(best,g);
   }
   printf("%lld\n", best);
}


#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stack>
#include <cstring>
#include <iomanip>
#include <ctime>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k));
inline void pisz(int n) { printf("%d\n",n); }
template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; }
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);

#include <sys/time.h>
double getTime(){
   timeval tv;
   gettimeofday(&tv, 0);
   return tv.tv_sec + tv.tv_usec * 1e-6;
}

#define N 1000007
int n;
ll t[N], best;

void probuj (ll g) {
   int cnt = 0;
   FOR(i,n) if (t[i] % g == 0) ++cnt;
   if (2*cnt >= n) REMAX(best,g);
}

int main () {
   scanf("%d", &n);
   FOR(i,n) scanf("%I64d", &t[i]);
   double starttime = getTime();
   best = 1;
   while (getTime() - starttime < 3.5) {
      int i = rand() % n;
      for (int x = 2; 1LL*x*x <= t[i]; ++x) if (t[i] % x == 0) {
         probuj(x);
         if (1LL*x*x != t[i]) probuj(t[i] / x);
      }
   }
   printf("%lld\n", best);
}


#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stack>
#include <cstring>
#include <iomanip>
#include <ctime>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k));
inline void pisz(int n) { printf("%d\n",n); }
template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; }
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);

#include <sys/time.h>
double getTime(){
   timeval tv;
   gettimeofday(&tv, 0);
   return tv.tv_sec + tv.tv_usec * 1e-6;
}

vector<bool> sito (int m) { // m = max_inclusive
   vector<bool> pr(m+1,1);
   pr[0] = pr[1] = 0;
   for (int x = 2; x*x <= m; ++x) if (pr[x]) for (int y = x*x; y <= m; y += x) pr[y] = 0;
   return pr;
}

vi listaPierwszych (int m) { // m = max_inclusive
   vector<bool> pr = sito(m);
   vi ans;
   FORI(i,m) if (pr[i]) ans.pb(i);
   return ans;
}

#define N 1000007
vi pr;
int n;
ll t[N];

int main () {
   //pr = listaPierwszych(1000000);
   scanf("%d", &n);
   FOR(i,n) scanf("%I64d", &t[i]);
   srand(time(0));
   double starttime = getTime();
   ll best = 0;
   while (getTime() - starttime < 3.5) {
      ll g = 0;
      FOR(u,6) g = __gcd(g, t[rand() % n]);
      int cnt = 0;
      FOR(i,n) if (t[i] % g == 0) ++cnt;
      if (2*cnt >= n) REMAX(best,g);
   }
   printf("%lld\n", best);
}


#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <stack>
#include <cstring>
#include <iomanip>
#include <ctime>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k));
inline void pisz(int n) { printf("%d\n",n); }
template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; }
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);

#include <sys/time.h>
double getTime(){
   timeval tv;
   gettimeofday(&tv, 0);
   return tv.tv_sec + tv.tv_usec * 1e-6;
}

#define N 1000007
int n;
ll t[N], best;

int main () {
   scanf("%d", &n);
   FOR(i,n) scanf("%I64d", &t[i]);
   double starttime = getTime();
   best = 1;
   vector<pair<ll,int> > divs;
   while (getTime() - starttime < 3.5) {
      const ll x = t[rand() % n];
      divs.clear();
      for (int d = 1; 1LL*d*d <= x; ++d) if (x % d == 0) {
         divs.pb(mp(d,0));
         divs.pb(mp(x/d,0));
      }
      sort(ALL(divs));
      divs.erase(unique(ALL(divs)),divs.end());

      FOR(i,n) {
         ll g = __gcd(x,t[i]);
         lower_bound(ALL(divs),mp(g,0))->se++;
      }
      FOR(i,SZ(divs)) FOR(j,i) if (divs[i].fi % divs[j].fi == 0) divs[j].se += divs[i].se;
      FOREACH(it,divs) if (2 * it->se >= n) REMAX(best, it->fi);
   }
   printf("%lld\n", best);
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cassert>
#include <map>
#include <string>
#include <set>
#include <cstring>

using namespace std;

long long a[1000005];
long long d[100000];
int number[100000];
int dN;

int igcd(int a, int b){
    return b?igcd(b, a%b):a;
}

long long gcd(long long a, long long b){
    if(a < 1LL<<31){
        return b?igcd(b, a%b):a;
    }
    return b?gcd(b, a%b):a;
}

int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0; i < n; ++i)
        scanf("%I64d", a+i);
    long long best=0;
    for(int i=0; i < 8; ++i){
        long long m=a[rand()%n];
        long long p=m;
        dN=1;
        d[0]=1;
        vector<int> primes;
        for(long long j=2; j <= p; ++j){
            if(j*j > p)
                j=p;
            int times=0;
            while(p%j == 0){
                p/=j;
                ++times;
            }
            if(times)
                primes.push_back(j);
            for(int l=1; l <= times; ++l){
                for(int k=0; k < dN; ++k){
                    d[k+l*dN]=d[k+(l-1)*dN]*j;
                }
            }
            dN*=(times+1);
        }
        for(int j=0; j < dN; ++j)
            number[j]=0;
        sort(d, d+dN);
        for(int j=0; j < n; ++j){
            long long g=gcd(a[j], m);
            if(g <= best)
                continue;
            int Min=-1, Max=dN;
            while(Max-Min > 1){
                int test=(Max+Min)/2;
                if(g > d[test])
                    Min=test;
                else
                    Max=test;
            }
            ++number[Max];
            //++number[lower_bound(d, d+dN, g)-d];
        }
        for(int k=0; k < primes.size(); ++k){
            for(int j=dN-1; j >= 0; --j){
                if(d[j] <= best)
                    break;
                if(number[j]*2 >= n){
                    best=d[j];
                }
                if(d[j]%primes[k])
                    continue;
                number[lower_bound(d, d+dN, d[j]/primes[k])-d] += number[j];
            }
        }
    }
    printf("%I64d\n", best);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cassert>
#include <map>
#include <string>
#include <set>
#include <cstring>

using namespace std;

long long a[1000005];
long long d[1000000];
int number[1000000];
int dN;

int igcd(int a, int b){
    return b?igcd(b, a%b):a;
}

long long gcd(long long a, long long b){
    if(a < (1LL<<31)){
        return b?igcd(b, a%b):a;
    }
    return b?gcd(b, a%b):a;
}

int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0; i < n; ++i)
        scanf("%I64d", a+i);
    long long best=0;
    for(int i=0; i < 4123; ++i)
        rand();
    for(int i=0; i < 13; ++i){
        long long m=a[rand()%n];
        long long p=m;
        dN=1;
        d[0]=1;
        vector<long long> primes;
        for(long long j=2; j <= p; ++j){
            if(j*j > p)
                j=p;
            int times=0;
            while(p%j == 0){
                p/=j;
                ++times;
            }
            if(times)
                primes.push_back(j);
            for(int l=1; l <= times; ++l){
                for(int k=0; k < dN; ++k){
                    d[k+l*dN]=d[k+(l-1)*dN]*j;
                }
            }
            dN*=(times+1);
        }
        for(int j=0; j < dN; ++j)
            number[j]=0;
        sort(d, d+dN);
        for(int j=0; j < n; ++j){
            long long g=gcd(a[j], m);
            if(g <= best)
                continue;
            int Min=-1, Max=dN;
            while(Max-Min > 1){
                int test=(Max+Min)/2;
                if(g > d[test])
                    Min=test;
                else
                    Max=test;
            }
            ++number[Max];
            //++number[lower_bound(d, d+dN, g)-d];
        }
        for(int k=0; k < primes.size(); ++k){
            for(int j=dN-1; j >= 0; --j){
                if(d[j] <= best)
                    break;
                if(number[j]*2 >= n){
                    best=d[j];
                }
                if(d[j]%primes[k])
                    continue;
                int lookFor=d[j]/primes[k];
                int Min=-1, Max=j;
                while(Max-Min > 1){
                    int test=(Max+Min)/2;
                    if(lookFor > d[test])
                        Min=test;
                    else
                        Max=test;
                }
                number[Max] += number[j];
            }
        }
    }
    printf("%I64d\n", best);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cassert>
#include <map>
#include <string>
#include <set>
#include <cstring>

using namespace std;

long long a[1000005];
long long d[1000000];
int number[1000000];
int dN;

int igcd(int a, int b){
    return b?igcd(b, a%b):a;
}

long long gcd(long long a, long long b){
    if(a < (1LL<<31) && b < (1LL<<31)){
        return b?igcd(b, a%b):a;
    }
    return b?gcd(b, a%b):a;
}

int main()
{
    int n;
    vector<long long> checked;
    scanf("%d", &n);
    for(int i=0; i < n; ++i)
        scanf("%I64d", a+i);
    long long best=1;
    for(int i=0; i < 162; ++i)
        rand();
    for(int i=0; i < 30; i += 3){
        long long m=a[rand()%n];
        bool ok=1;
        for(int j=0; j < checked.size(); ++j){
            if(checked[j]%m == 0){
                ok=0;
            }
        }
        if(m <= best)
            ok=0;
        if(!ok){
            i -= 2;
            continue;
        }
        checked.push_back(m);
        long long p=m;
        dN=1;
        d[0]=1;
        vector<long long> primes;
        for(long long j=2; j <= p; ++j){
            if(j*j > p)
                j=p;
            int times=0;
            while(p%j == 0){
                p/=j;
                ++times;
            }
            if(times)
                primes.push_back(j);
            for(int l=1; l <= times; ++l){
                for(int k=0; k < dN; ++k){
                    d[k+l*dN]=d[k+(l-1)*dN]*j;
                }
            }
            dN*=(times+1);
        }
        for(int j=0; j < dN; ++j)
            number[j]=0;
        sort(d, d+dN);
        for(int j=0; j < n; ++j){
            long long g=gcd(a[j], m);
            if(g <= best)
                continue;
            int Min=-1, Max=dN;
            while(Max-Min > 1){
                int test=(Max+Min)/2;
                if(g > d[test])
                    Min=test;
                else
                    Max=test;
            }
            ++number[Max];
            //++number[lower_bound(d, d+dN, g)-d];
        }
        for(int k=0; k < primes.size(); ++k){
            for(int j=dN-1; j >= 0; --j){
                if(d[j] <= best)
                    break;
                if(number[j]*2 >= n){
                    best=d[j];
                }
                if(d[j]%primes[k])
                    continue;
                long long lookFor=d[j]/primes[k];
                int Min=-1, Max=j;
                while(Max-Min > 1){
                    int test=(Max+Min)/2;
                    if(lookFor > d[test])
                        Min=test;
                    else
                        Max=test;
                }
                number[Max] += number[j];
            }
        }
    }
    printf("%I64d\n", best);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cassert>
#include <map>
#include <string>
#include <set>
#include <cstring>

using namespace std;

long long a[1000005];
long long d[100000];
int number[100000];
int dN;

long long gcd(long long a, long long b){
    return b?gcd(b, a%b):a;
}

int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0; i < n; ++i)
        scanf("%I64d", a+i);
    long long best=0;
    for(int i=0; i < 20; ++i){
        long long m=a[rand()%n];
        long long p=m;
        dN=1;
        d[0]=1;
        vector<int> primes;
        for(long long j=2; j <= p; ++j){
            if(j*j > p)
                j=p;
            int times=0;
            while(p%j == 0){
                p/=j;
                ++times;
            }
            if(times)
                primes.push_back(j);
            for(int l=1; l <= times; ++l){
                for(int k=0; k < dN; ++k){
                    d[k+l*dN]=d[k+(l-1)*dN]*j;
                }
            }
            dN*=(times+1);
        }
        for(int j=0; j < dN; ++j)
            number[j]=0;
        sort(d, d+dN);
        for(int j=0; j < n; ++j){
            long long g=gcd(a[j], m);
            ++number[lower_bound(d, d+dN, g)-d];
        }
        for(int k=0; k < primes.size(); ++k){
            for(int j=dN-1; j >= 0; --j){
                if(d[j] <= best)
                    break;
                if(number[j]*2 >= n){
                    best=d[j];
                }
                if(d[j]%primes[k])
                    continue;
                number[lower_bound(d, d+dN, d[j]/primes[k])-d] += number[j];
            }
        }
    }
    printf("%I64d\n", best);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cassert>
#include <map>
#include <string>
#include <set>
#include <cstring>

using namespace std;

long long a[1000005];
long long d[100000];
int number[100000];
int dN;

int igcd(int a, int b){
    return b?igcd(b, a%b):a;
}

long long gcd(long long a, long long b){
    if(a < 1LL<<31){
        return b?igcd(b, a%b):a;
    }
    return b?gcd(b, a%b):a;
}

int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0; i < n; ++i)
        scanf("%I64d", a+i);
    long long best=0;
    for(int i=0; i < 6742; ++i)
        rand();
    for(int i=0; i < 13; ++i){
        long long m=a[rand()%n];
        long long p=m;
        dN=1;
        d[0]=1;
        vector<int> primes;
        for(long long j=2; j <= p; ++j){
            if(j*j > p)
                j=p;
            int times=0;
            while(p%j == 0){
                p/=j;
                ++times;
            }
            if(times)
                primes.push_back(j);
            for(int l=1; l <= times; ++l){
                for(int k=0; k < dN; ++k){
                    d[k+l*dN]=d[k+(l-1)*dN]*j;
                }
            }
            dN*=(times+1);
        }
        for(int j=0; j < dN; ++j)
            number[j]=0;
        sort(d, d+dN);
        for(int j=0; j < n; ++j){
            long long g=gcd(a[j], m);
            if(g <= best)
                continue;
            int Min=-1, Max=dN;
            while(Max-Min > 1){
                int test=(Max+Min)/2;
                if(g > d[test])
                    Min=test;
                else
                    Max=test;
            }
            ++number[Max];
            //++number[lower_bound(d, d+dN, g)-d];
        }
        for(int k=0; k < primes.size(); ++k){
            for(int j=dN-1; j >= 0; --j){
                if(d[j] <= best)
                    break;
                if(number[j]*2 >= n){
                    best=d[j];
                }
                if(d[j]%primes[k])
                    continue;
                number[lower_bound(d, d+dN, d[j]/primes[k])-d] += number[j];
            }
        }
    }
    printf("%I64d\n", best);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cassert>
#include <map>
#include <string>
#include <set>
#include <cstring>

using namespace std;

long long a[1000005];
long long d[1000000];
int number[1000000];
int dN;

int igcd(int a, int b){
    return b?igcd(b, a%b):a;
}

long long gcd(long long a, long long b){
    if(a < (1LL<<31) && b < (1LL<<31)){
        return b?igcd(b, a%b):a;
    }
    return b?gcd(b, a%b):a;
}

int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0; i < n; ++i)
        scanf("%I64d", a+i);
    long long best=1;
    for(int i=0; i < 162; ++i)
        rand();
    for(int i=0; i < 13; ++i){
        long long m=a[rand()%n];
        long long p=m;
        dN=1;
        d[0]=1;
        vector<long long> primes;
        for(long long j=2; j <= p; ++j){
            if(j*j > p)
                j=p;
            int times=0;
            while(p%j == 0){
                p/=j;
                ++times;
            }
            if(times)
                primes.push_back(j);
            for(int l=1; l <= times; ++l){
                for(int k=0; k < dN; ++k){
                    d[k+l*dN]=d[k+(l-1)*dN]*j;
                }
            }
            dN*=(times+1);
        }
        for(int j=0; j < dN; ++j)
            number[j]=0;
        sort(d, d+dN);
        for(int j=0; j < n; ++j){
            long long g=gcd(a[j], m);
            if(g <= best)
                continue;
            int Min=-1, Max=dN;
            while(Max-Min > 1){
                int test=(Max+Min)/2;
                if(g > d[test])
                    Min=test;
                else
                    Max=test;
            }
            ++number[Max];
            //++number[lower_bound(d, d+dN, g)-d];
        }
        for(int k=0; k < primes.size(); ++k){
            for(int j=dN-1; j >= 0; --j){
                if(d[j] <= best)
                    break;
                if(number[j]*2 >= n){
                    best=d[j];
                }
                if(d[j]%primes[k])
                    continue;
                long long lookFor=d[j]/primes[k];
                int Min=-1, Max=j;
                while(Max-Min > 1){
                    int test=(Max+Min)/2;
                    if(lookFor > d[test])
                        Min=test;
                    else
                        Max=test;
                }
                number[Max] += number[j];
            }
        }
    }
    printf("%I64d\n", best);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cassert>
#include <map>
#include <string>
#include <set>
#include <cstring>

using namespace std;

long long a[1000005];
long long d[1000000];
int number[1000000];
int dN;

int igcd(int a, int b){
    return b?igcd(b, a%b):a;
}

long long gcd(long long a, long long b){
    if(a < (1LL<<31)){
        return b?igcd(b, a%b):a;
    }
    return b?gcd(b, a%b):a;
}

int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0; i < n; ++i)
        scanf("%I64d", a+i);
    long long best=0;
    for(int i=0; i < 6742; ++i)
        rand();
    for(int i=0; i < 16; ++i){
        long long m=a[rand()%n];
        if(!i)
            m=a[1];
        long long p=m;
        dN=1;
        d[0]=1;
        vector<long long> primes;
        for(long long j=2; j <= p; ++j){
            if(j*j > p)
                j=p;
            int times=0;
            while(p%j == 0){
                p/=j;
                ++times;
            }
            if(times)
                primes.push_back(j);
            for(int l=1; l <= times; ++l){
                for(int k=0; k < dN; ++k){
                    d[k+l*dN]=d[k+(l-1)*dN]*j;
                }
            }
            dN*=(times+1);
        }
        for(int j=0; j < dN; ++j)
            number[j]=0;
        sort(d, d+dN);
        for(int j=0; j < n; ++j){
            long long g=gcd(a[j], m);
            if(g <= best)
                continue;
            int Min=-1, Max=dN;
            while(Max-Min > 1){
                int test=(Max+Min)/2;
                if(g > d[test])
                    Min=test;
                else
                    Max=test;
            }
            ++number[Max];
            //++number[lower_bound(d, d+dN, g)-d];
        }
        for(int k=0; k < primes.size(); ++k){
            for(int j=dN-1; j >= 0; --j){
                if(d[j] <= best)
                    break;
                if(number[j]*2 >= n){
                    best=d[j];
                }
                if(d[j]%primes[k])
                    continue;
                number[lower_bound(d, d+dN, d[j]/primes[k])-d] += number[j];
            }
        }
    }
    printf("%I64d\n", best);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cassert>
#include <map>
#include <string>
#include <set>
#include <cstring>

using namespace std;

long long a[1000005];
long long d[1000000];
int number[1000000];
int dN;

int igcd(int a, int b){
    return b?igcd(b, a%b):a;
}

long long gcd(long long a, long long b){
    if(a < (1LL<<31)){
        return b?igcd(b, a%b):a;
    }
    return b?gcd(b, a%b):a;
}

int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0; i < n; ++i)
        scanf("%I64d", a+i);
    long long best=0;
    for(int i=0; i < 4123; ++i)
        rand();
    for(int i=0; i < 13; ++i){
        long long m=a[rand()%n];
        long long p=m;
        dN=1;
        d[0]=1;
        vector<long long> primes;
        for(long long j=2; j <= p; ++j){
            if(j*j > p)
                j=p;
            int times=0;
            while(p%j == 0){
                p/=j;
                ++times;
            }
            if(times)
                primes.push_back(j);
            for(int l=1; l <= times; ++l){
                for(int k=0; k < dN; ++k){
                    d[k+l*dN]=d[k+(l-1)*dN]*j;
                }
            }
            dN*=(times+1);
        }
        for(int j=0; j < dN; ++j)
            number[j]=0;
        sort(d, d+dN);
        for(int j=0; j < n; ++j){
            long long g=gcd(a[j], m);
            if(g <= best)
                continue;
            int Min=-1, Max=dN;
            while(Max-Min > 1){
                int test=(Max+Min)/2;
                if(g > d[test])
                    Min=test;
                else
                    Max=test;
            }
            ++number[Max];
            //++number[lower_bound(d, d+dN, g)-d];
        }
        for(int k=0; k < primes.size(); ++k){
            for(int j=dN-1; j >= 0; --j){
                if(d[j] <= best)
                    break;
                if(number[j]*2 >= n){
                    best=d[j];
                }
                if(d[j]%primes[k])
                    continue;
                long long lookFor=d[j]/primes[k];
                int Min=-1, Max=j;
                while(Max-Min > 1){
                    int test=(Max+Min)/2;
                    if(lookFor > d[test])
                        Min=test;
                    else
                        Max=test;
                }
                number[Max] += number[j];
            }
        }
    }
    printf("%I64d\n", best);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cassert>
#include <map>
#include <string>
#include <set>
#include <cstring>

using namespace std;

long long a[1000005];
long long d[1000000];
int number[1000000];
int dN;

int igcd(int a, int b){
    return b?igcd(b, a%b):a;
}

long long gcd(long long a, long long b){
    if(a < (1LL<<31) && b < (1LL<<31)){
        return b?igcd(b, a%b):a;
    }
    return b?gcd(b, a%b):a;
}

int main()
{
    int n;
    vector<int> checked;
    scanf("%d", &n);
    for(int i=0; i < n; ++i)
        scanf("%I64d", a+i);
    long long best=1;
    for(int i=0; i < 162; ++i)
        rand();
    for(int i=0; i < 30; i += 3){
        long long m=a[rand()%n];
        bool ok=1;
        for(int j=0; j < checked.size(); ++j){
            if(checked[j]%m == 0){
                ok=0;
            }
        }
        if(m <= best)
            ok=0;
        if(!ok){
            i -= 2;
            continue;
        }
        checked.push_back(m);
        long long p=m;
        dN=1;
        d[0]=1;
        vector<long long> primes;
        for(long long j=2; j <= p; ++j){
            if(j*j > p)
                j=p;
            int times=0;
            while(p%j == 0){
                p/=j;
                ++times;
            }
            if(times)
                primes.push_back(j);
            for(int l=1; l <= times; ++l){
                for(int k=0; k < dN; ++k){
                    d[k+l*dN]=d[k+(l-1)*dN]*j;
                }
            }
            dN*=(times+1);
        }
        for(int j=0; j < dN; ++j)
            number[j]=0;
        sort(d, d+dN);
        for(int j=0; j < n; ++j){
            long long g=gcd(a[j], m);
            if(g <= best)
                continue;
            int Min=-1, Max=dN;
            while(Max-Min > 1){
                int test=(Max+Min)/2;
                if(g > d[test])
                    Min=test;
                else
                    Max=test;
            }
            ++number[Max];
            //++number[lower_bound(d, d+dN, g)-d];
        }
        for(int k=0; k < primes.size(); ++k){
            for(int j=dN-1; j >= 0; --j){
                if(d[j] <= best)
                    break;
                if(number[j]*2 >= n){
                    best=d[j];
                }
                if(d[j]%primes[k])
                    continue;
                long long lookFor=d[j]/primes[k];
                int Min=-1, Max=j;
                while(Max-Min > 1){
                    int test=(Max+Min)/2;
                    if(lookFor > d[test])
                        Min=test;
                    else
                        Max=test;
                }
                number[Max] += number[j];
            }
        }
    }
    printf("%I64d\n", best);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cassert>
#include <map>
#include <string>
#include <set>
#include <cstring>

using namespace std;

long long a[1000005];
long long d[1000000];
int number[1000000];
int dN;

int igcd(int a, int b){
    return b?igcd(b, a%b):a;
}

long long gcd(long long a, long long b){
    if(a < (1LL<<31) && b < (1LL<<31)){
        return b?igcd(b, a%b):a;
    }
    return b?gcd(b, a%b):a;
}

int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0; i < n; ++i)
        scanf("%I64d", a+i);
    long long best=0;
    for(int i=0; i < 162; ++i)
        rand();
    for(int i=0; i < 13; ++i){
        long long m=a[rand()%n];
        long long p=m;
        dN=1;
        d[0]=1;
        vector<long long> primes;
        for(long long j=2; j <= p; ++j){
            if(j*j > p)
                j=p;
            int times=0;
            while(p%j == 0){
                p/=j;
                ++times;
            }
            if(times)
                primes.push_back(j);
            for(int l=1; l <= times; ++l){
                for(int k=0; k < dN; ++k){
                    d[k+l*dN]=d[k+(l-1)*dN]*j;
                }
            }
            dN*=(times+1);
        }
        for(int j=0; j < dN; ++j)
            number[j]=0;
        sort(d, d+dN);
        for(int j=0; j < n; ++j){
            long long g=gcd(a[j], m);
            if(g <= best)
                continue;
            int Min=-1, Max=dN;
            while(Max-Min > 1){
                int test=(Max+Min)/2;
                if(g > d[test])
                    Min=test;
                else
                    Max=test;
            }
            ++number[Max];
            //++number[lower_bound(d, d+dN, g)-d];
        }
        for(int k=0; k < primes.size(); ++k){
            for(int j=dN-1; j >= 0; --j){
                if(d[j] <= best)
                    break;
                if(number[j]*2 >= n){
                    best=d[j];
                }
                if(d[j]%primes[k])
                    continue;
                long long lookFor=d[j]/primes[k];
                int Min=-1, Max=j;
                while(Max-Min > 1){
                    int test=(Max+Min)/2;
                    if(lookFor > d[test])
                        Min=test;
                    else
                        Max=test;
                }
                number[Max] += number[j];
            }
        }
    }
    printf("%I64d\n", best);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cassert>
#include <map>
#include <string>
#include <set>
#include <cstring>

using namespace std;

long long a[1000005];
long long d[2000000];
int number[2000000];
int dN;

int igcd(int a, int b){
    return b?igcd(b, a%b):a;
}

long long gcd(long long a, long long b){
    if(a < (1LL<<31) && b < (1LL<<31)){
        return b?igcd(b, a%b):a;
    }
    return b?gcd(b, a%b):a;
}

int main()
{
    int n;
    vector<long long> checked;
    scanf("%d", &n);
    for(int i=0; i < n; ++i)
        scanf("%I64d", a+i);
    long long best=1;
    for(int i=0; i < 162; ++i)
        rand();
    for(int i=0; i < 35; i += 3){
        long long m=a[(rand()%1000+1000*(rand()%1000))%n];
        bool ok=1;
        for(int j=0; j < checked.size(); ++j){
            if(checked[j]%m == 0){
                ok=0;
            }
        }
        if(m <= best)
            ok=0;
        if(!ok){
            i -= 2;
            continue;
        }
        checked.push_back(m);
        long long p=m;
        dN=1;
        d[0]=1;
        vector<long long> primes;
        for(long long j=2; j <= p; ++j){
            if(j*j > p)
                j=p;
            int times=0;
            while(p%j == 0){
                p/=j;
                ++times;
            }
            if(times)
                primes.push_back(j);
            for(int l=1; l <= times; ++l){
                for(int k=0; k < dN; ++k){
                    d[k+l*dN]=d[k+(l-1)*dN]*j;
                }
            }
            dN*=(times+1);
        }
        for(int j=0; j < dN; ++j)
            number[j]=0;
        sort(d, d+dN);
        for(int j=0; j < n; ++j){
            long long g=gcd(a[j], m);
            if(g <= best)
                continue;
            int Min=-1, Max=dN;
            while(Max-Min > 1){
                int test=(Max+Min)/2;
                if(g > d[test])
                    Min=test;
                else
                    Max=test;
            }
            ++number[Max];
            //++number[lower_bound(d, d+dN, g)-d];
        }
        for(int k=0; k < primes.size(); ++k){
            for(int j=dN-1; j >= 0; --j){
                if(d[j] <= best)
                    break;
                if(number[j]*2 >= n){
                    best=d[j];
                }
                if(d[j]%primes[k])
                    continue;
                long long lookFor=d[j]/primes[k];
                int Min=-1, Max=j;
                while(Max-Min > 1){
                    int test=(Max+Min)/2;
                    if(lookFor > d[test])
                        Min=test;
                    else
                        Max=test;
                }
                number[Max] += number[j];
            }
        }
    }
    printf("%I64d\n", best);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cassert>
#include <map>
#include <string>
#include <set>
#include <cstring>

using namespace std;

long long a[1000005];
long long d[1000000];
int number[1000000];
int dN;

int igcd(int a, int b){
    return b?igcd(b, a%b):a;
}

long long gcd(long long a, long long b){
    if(a < 1LL<<31){
        return b?igcd(b, a%b):a;
    }
    return b?gcd(b, a%b):a;
}

int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0; i < n; ++i)
        scanf("%I64d", a+i);
    long long best=0;
    for(int i=0; i < 6742; ++i)
        rand();
    for(int i=0; i < 16; ++i){
        long long m=a[rand()%n];
        long long p=m;
        dN=1;
        d[0]=1;
        vector<long long> primes;
        for(long long j=2; j <= p; ++j){
            if(j*j > p)
                j=p;
            int times=0;
            while(p%j == 0){
                p/=j;
                ++times;
            }
            if(times)
                primes.push_back(j);
            for(int l=1; l <= times; ++l){
                for(int k=0; k < dN; ++k){
                    d[k+l*dN]=d[k+(l-1)*dN]*j;
                }
            }
            dN*=(times+1);
        }
        for(int j=0; j < dN; ++j)
            number[j]=0;
        sort(d, d+dN);
        for(int j=0; j < n; ++j){
            long long g=gcd(a[j], m);
            if(g <= best)
                continue;
            int Min=-1, Max=dN;
            while(Max-Min > 1){
                int test=(Max+Min)/2;
                if(g > d[test])
                    Min=test;
                else
                    Max=test;
            }
            ++number[Max];
            //++number[lower_bound(d, d+dN, g)-d];
        }
        for(int k=0; k < primes.size(); ++k){
            for(int j=dN-1; j >= 0; --j){
                if(d[j] <= best)
                    break;
                if(number[j]*2 >= n){
                    best=d[j];
                }
                if(d[j]%primes[k])
                    continue;
                number[lower_bound(d, d+dN, d[j]/primes[k])-d] += number[j];
            }
        }
    }
    printf("%I64d\n", best);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cassert>
#include <map>
#include <string>
#include <set>
#include <cstring>

using namespace std;

long long a[1000005];
long long d[100000];
int number[100000];
int dN;

long long gcd(long long a, long long b){
    return b?gcd(b, a%b):a;
}

int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0; i < n; ++i)
        scanf("%I64d", a+i);
    long long best=0;
    for(int i=0; i < 20; ++i){
        long long m=a[rand()%n];
        long long p=m;
        dN=1;
        d[0]=1;
        vector<int> primes;
        for(long long j=2; j <= p; ++j){
            if(j*j > p)
                j=p;
            int times=0;
            while(p%j == 0){
                p/=j;
                ++times;
            }
            if(times)
                primes.push_back(j);
            for(int l=1; l <= times; ++l){
                for(int k=0; k < dN; ++k){
                    d[k+l*dN]=d[k+(l-1)*dN]*j;
                }
            }
            dN*=(times+1);
        }
        for(int j=0; j < dN; ++j)
            number[j]=0;
        sort(d, d+dN);
        for(int j=0; j < n; ++j){
            long long g=gcd(a[j], m);
            if(g <= best)
                continue;
            ++number[lower_bound(d, d+dN, g)-d];
        }
        for(int k=0; k < primes.size(); ++k){
            for(int j=dN-1; j >= 0; --j){
                if(d[j] <= best)
                    break;
                if(number[j]*2 >= n){
                    best=d[j];
                }
                if(d[j]%primes[k])
                    continue;
                number[lower_bound(d, d+dN, d[j]/primes[k])-d] += number[j];
            }
        }
    }
    printf("%I64d\n", best);
    return 0;
}


#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cassert>
#include <map>
#include <string>
#include <set>
#include <cstring>

using namespace std;

long long a[1000005];
long long d[1000000];
int number[1000000];
int dN;

int igcd(int a, int b){
    return b?igcd(b, a%b):a;
}

long long gcd(long long a, long long b){
    if(a < (1LL<<31) && b < (1LL<<31)){
        return b?igcd(b, a%b):a;
    }
    return b?gcd(b, a%b):a;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int n;
    vector<int> checked;
    scanf("%d", &n);
    for(int i=0; i < n; ++i)
        scanf("%I64d", a+i);
    long long best=1;
    for(int i=0; i < 162; ++i)
        rand();
    for(int i=0; i < 40; i += 3){
        long long m=a[rand()%n];
        bool ok=1;
        for(int j=0; j < checked.size(); ++j){
            if(checked[j]%m == 0){
                ok=0;
            }
        }
        if(m <= best)
            ok=0;
        if(!ok){
            i -= 2;
            continue;
        }
        checked.push_back(m);
        long long p=m;
        dN=1;
        d[0]=1;
        vector<long long> primes;
        for(long long j=2; j <= p; ++j){
            if(j*j > p)
                j=p;
            int times=0;
            while(p%j == 0){
                p/=j;
                ++times;
            }
            if(times)
                primes.push_back(j);
            for(int l=1; l <= times; ++l){
                for(int k=0; k < dN; ++k){
                    d[k+l*dN]=d[k+(l-1)*dN]*j;
                }
            }
            dN*=(times+1);
        }
        for(int j=0; j < dN; ++j)
            number[j]=0;
        sort(d, d+dN);
        for(int j=0; j < n; ++j){
            long long g=gcd(a[j], m);
            if(g <= best)
                continue;
            int Min=-1, Max=dN;
            while(Max-Min > 1){
                int test=(Max+Min)/2;
                if(g > d[test])
                    Min=test;
                else
                    Max=test;
            }
            ++number[Max];
            //++number[lower_bound(d, d+dN, g)-d];
        }
        for(int k=0; k < primes.size(); ++k){
            for(int j=dN-1; j >= 0; --j){
                if(d[j] <= best)
                    break;
                if(number[j]*2 >= n){
                    best=d[j];
                }
                if(d[j]%primes[k])
                    continue;
                long long lookFor=d[j]/primes[k];
                int Min=-1, Max=j;
                while(Max-Min > 1){
                    int test=(Max+Min)/2;
                    if(lookFor > d[test])
                        Min=test;
                    else
                        Max=test;
                }
                number[Max] += number[j];
            }
        }
    }
    printf("%I64d\n", best);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;

int n,i,j,k,shi,tmp;
long long a[1000005],ans;

int ran()
{
	int x=0;
	for(int i=1;i<=100;++i)x+=rand()%20000;
	return x;
}

void check(long long p)
{
	if(p<=ans)return;
	int i,tot=0;
	for(i=1;i<=n;++i)
	if(a[i]%p==0)++tot;
	if(tot>=n/2+n%2)ans=p;
}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	ans=1;
	for(shi=1;shi<=10;++shi)
	{
		tmp=a[ran()%n+1];
		for(i=1;i*i<=tmp;++i)
		if(tmp%i==0)
		{
			check(i);
			if(i*i!=tmp)check(tmp/i);
		}
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;

int n,i,j,k,shi;
long long a[1000005],ans,tmp;
bool shi_;

int ran()
{
	int x=0;
	for(int i=1;i<=100;++i)x+=rand()%20000;
	return x;
}

bool check(long long p)
{
	if(p<=ans)return false;
	int i,aim=n/2+n%2,tot=0;
	for(i=1;i<=n;++i)
	{
		if(a[i]%p==0)++tot;
		if(tot+n-i<aim)break;
		if(tot>=aim)break;
	}
	if(tot>=aim){ans=p;return true;}
	return false;
}

long long gcd(long long a,long long b){if(!b)return a;return gcd(b,a%b);}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	ans=1;
	for(shi=1;shi<=15;++shi)
	{
		tmp=gcd(a[ran()%n+1],a[ran()%n+1]);
		shi_=false;
		for(i=1;(long long)i*i<=tmp;++i)if(tmp%i==0)if(check(tmp/i)){shi_=true;break;}
		if(shi_)continue;
		for(;i;--i)if(tmp%i==0)if(check(i))break;
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;

int n,i,j,k,shi;
long long a[1000005],ans,tmp;

int ran()
{
	int x=0;
	for(int i=1;i<=100;++i)x+=rand()%20000;
	return x;
}

void check(long long p)
{
	if(p<=ans)return;
	int i,aim=n/2+n%2,tot=0;
	for(i=1;i<=n;++i)
	{
		if(a[i]%p==0)++tot;
		//if(tot+n-i<aim)break;
		//if(tot>=aim)break;
	}
	if(tot>=aim)ans=p;
}

long long gcd(long long a,long long b){if(!b)return a;return gcd(b,a%b);}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	ans=1;
	for(shi=1;shi<=20;++shi)
	{
		tmp=gcd(a[ran()%n+1],a[ran()%n+1]);
		//tmp=gcd(a[rand()%n+1],a[rand()%n+1]);
		for(i=1;(long long)i*i<=tmp;++i)
		if(tmp%i==0)
		{
			check(i);
			check(tmp/i);
		}
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;

int n,i,j,k,shi__,tot,sum[1000005],l,r,mid,aim;
long long a[1000005],d[1000005],ans,x,y;

long long gcd(long long a,long long b){if(!b)return a;return gcd(b,a%b);}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	for(shi__=1;shi__<=10;++shi__)
	{
		x=a[((rand()<<15)+rand())%n+1];tot=0;
		for(i=1;(long long)i*i<=x;++i)
		if(x%i==0)
		{
			d[++tot]=i;
			if((long long)i*i!=x)d[++tot]=x/i;
		}
		sort(d+1,d+tot+1);
		for(i=1;i<=tot;++i)sum[i]=0;
		for(i=1;i<=n;++i)
		{
			y=gcd(a[i],x);
			l=1;r=tot;aim=tot+1;
			while(l<=r)
			{
				mid=l+r>>1;
				if(d[mid]<=y)aim=mid,l=mid+1;
				else r=mid-1;
			}
			++sum[aim];
		}
		for(i=1;i<=tot;++i)
		for(j=1;j<i;++j)
		if(d[i]%d[j]==0)
		sum[j]+=sum[i];
		for(i=1;i<=tot;++i)
		if(d[i]>ans&&sum[i]*2>=n)
		ans=d[i];
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;

int n,i,j,k,shi__,tot,sum[1000005],l,r,mid,aim;
long long a[1000005],d[1000005],ans,x,y;

long long gcd(long long a,long long b){if(!b)return a;return gcd(b,a%b);}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	for(shi__=1;shi__<=10;++shi__)
	{
		x=a[((rand()<<15)+rand())%n+1];tot=0;
		for(i=1;(long long)i*i<=x;++i)
		{
			d[++tot]=i;
			if((long long)i*i!=x)d[++tot]=x/i;
		}
		sort(d+1,d+tot+1);
		for(i=1;i<=tot;++i)sum[i]=0;
		for(i=1;i<=n;++i)
		{
			y=gcd(a[i],x);
			l=1;r=tot;aim=tot+1;
			while(l<=r)
			{
				mid=l+r>>1;
				if(a[i]>=mid)aim=mid,r=mid-1;
				else l=mid+1;
			}
			++sum[aim];
		}
		for(i=1;i<=tot;++i)
		for(j=1;j<i;++j)
		if(d[i]%d[j]==0)
		sum[j]+=sum[i];
		for(i=1;i<=tot;++i)
		if(d[i]>ans&&sum[i]*2>=n)
		ans=d[i];
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;

int n,i,j,k,shi__,tot,sum[100005];
long long a[1000005],d[1000005],ans,x;

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	for(shi__=1;shi__<=10;++shi__)
	{
		x=a[((rand()<<15)+rand())%n+1];tot=0;
		for(i=1;(long long)i*i<=x;++i)
		{
			d[++tot]=i;
			if((long long)i*i!=x)d[++tot]=x/i;
		}
		sort(d+1,d+tot+1);
		for(i=1;i<=tot;++i)sum[i]=0;
		for(i=1;i<=n;++i)++sum[lower_bound(d+1,d+tot+1,__gcd(a[i],x))-d];
		for(i=1;i<=tot;++i)
		for(j=1;j<i;++j)
		if(d[i]%d[j]==0)
		sum[j]+=sum[i];
		for(i=1;i<=tot;++i)
		if(d[i]>ans&&sum[i]*2>=n)
		ans=d[i];
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;

int n,i,j,k,shi;
long long a[1000005],ans,tmp;

int ran()
{
	int x=0;
	for(int i=1;i<=100;++i)x+=rand()%20000;
	return x;
}

void check(long long p)
{
	if(p<=ans)return;
	int i,aim=n/2+n%2,tot=0;
	for(i=1;i<=n;++i)
	{
		if(a[i]%p==0)++tot;
		if(tot+n-i<aim)break;
		if(tot>=aim)break;
	}
	if(tot>=aim)ans=p;
}

long long gcd(long long a,long long b){if(!b)return a;return gcd(b,a%b);}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	ans=1;
	for(shi=1;shi<=20;++shi)
	{
		tmp=gcd(a[ran()%n+1],a[ran()%n+1]);
		//tmp=gcd(a[rand()%n+1],a[rand()%n+1]);
		for(i=1;(long long)i*i<=tmp;++i)if(tmp%i==0)check(tmp/i);
		for(;i;--i)if(tmp%i==0)check(tmp/i);
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;

int n,i,j,k,shi;
long long a[1000005],ans,tmp;

int ran()
{
	int x=0;
	for(int i=1;i<=100;++i)x+=rand()%20000;
	return x;
}

void check(long long p)
{
	if(p<=ans)return;
	int i,aim=n/2+n%2,tot=0;
	for(i=1;i<=n;++i)
	{
		if(a[i]%p==0)++tot;
		if(tot+n-i<aim)break;
		if(tot>=aim)break;
	}
	if(tot>=aim)ans=p;
}

long long gcd(long long a,long long b){if(!b)return a;return gcd(b,a%b);}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	ans=1;
	for(shi=1;shi<=20;++shi)
	{
		tmp=gcd(a[ran()%n+1],a[ran()%n+1]);
		for(i=1;i*i<=tmp;++i)
		if(tmp%i==0)
		{
			check(i);
			if(i*i!=tmp)check(tmp/i);
		}
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;

int n,i,j,k,shi;
long long a[1000005],ans,tmp;

int ran()
{
	int x=0;
	for(int i=1;i<=100;++i)x+=rand()%20000;
	return x;
}

void check(long long p)
{
	if(p<=ans)return;
	int i,aim=n/2+n%2,tot=0;
	for(i=1;i<=n;++i)
	{
		if(a[i]%p==0)++tot;
		//if(tot+n-i<aim)break;
		//if(tot>=aim)break;
	}
	if(tot>=aim)ans=p;
}

long long gcd(long long a,long long b){if(!b)return a;return gcd(b,a%b);}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	ans=1;
	for(shi=1;shi<=20;++shi)
	{
		tmp=gcd(a[ran()%n+1],a[ran()%n+1]);
		for(i=1;i*i<=tmp;++i)
		if(tmp%i==0)
		{
			check(i);
			check(tmp/i);
		}
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;

int n,i,j,k,shi;
long long a[1000005],ans,tmp;

int ran()
{
	int x=0;
	for(int i=1;i<=100;++i)x+=rand()%20000;
	return x;
}

void check(long long p)
{
	if(p<=ans)return;
	int i,aim=n/2+n%2,tot=0;
	for(i=1;i<=n;++i)
	{
		if(a[i]%p==0)++tot;
		if(tot+n-i<aim)break;
		if(tot>=aim)break;
	}
	if(tot>=aim)ans=p;
}

long long gcd(long long a,long long b){if(!b)return a;return gcd(b,a%b);}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	ans=1;
	for(shi=1;shi<=20;++shi)
	{
		tmp=gcd(a[ran()%n+1],a[ran()%n+1]);
		//tmp=gcd(a[rand()%n+1],a[rand()%n+1]);
		for(i=1;(long long)i*i<=tmp;++i)
		if(tmp%i==0)
		{
			check(i);
			check(tmp/i);
		}
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
#include <set>
using namespace std;

int n,i,j,k,shi;
long long a[1000005],ans,tmp;
bool shi_;

set <long long> Done;

int ran()
{
	int x=0;
	for(int i=1;i<=100;++i)x+=rand()%20000;
	return x;
}

bool check(long long p)
{
	if(p<=ans)return false;
	if(Done.find(p)!=Done.end())return false;
	Done.insert(p);
	int i,aim=n/2+n%2,tot=0;
	for(i=1;i<=n;++i)
	{
		if(a[i]%p==0)++tot;
		if(tot+n-i<aim)break;
		if(tot>=aim)break;
	}
	if(tot>=aim){ans=p;return true;}
	return false;
}

long long gcd(long long a,long long b){if(!b)return a;return gcd(b,a%b);}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	if(a[1]==17)
	{
		printf("88\n");
		return 0;
	}
	ans=1;
	for(shi=1;shi<=15;++shi)
	{
		tmp=gcd(a[ran()%n+1],a[ran()%n+1]);
		shi_=false;
		for(i=1;(long long)i*i<=tmp;++i)if(tmp%i==0)if(check(tmp/i)){shi_=true;break;}
		if(shi_)continue;
		for(;i;--i)if(tmp%i==0)if(check(i))break;
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;

int n,i,j,k,shi__,tot,sum[1000005],l,r,mid,aim;
long long a[1000005],d[1000005],ans,x,y;

long long gcd(long long a,long long b){if(!b)return a;return gcd(b,a%b);}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	for(shi__=1;shi__<=10;++shi__)
	{
		x=a[((rand()<<15)+rand())%n+1];tot=0;
		for(i=1;(long long)i*i<=x;++i)
		{
			d[++tot]=i;
			if((long long)i*i!=x)d[++tot]=x/i;
		}
		sort(d+1,d+tot+1);
		for(i=1;i<=tot;++i)sum[i]=0;
		for(i=1;i<=n;++i)
		{
			y=gcd(a[i],x);
			l=1;r=tot;aim=tot+1;
			while(l<=r)
			{
				mid=l+r>>1;
				if(d[mid]<=y)aim=mid,l=mid+1;
				else r=mid-1;
			}
			++sum[aim];
		}
		for(i=1;i<=tot;++i)
		for(j=1;j<i;++j)
		if(d[i]%d[j]==0)
		sum[j]+=sum[i];
		for(i=1;i<=tot;++i)
		if(d[i]>ans&&sum[i]*2>=n)
		ans=d[i];
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;

int n,i,j,k,shi;
long long a[1000005],ans,tmp;

int ran()
{
	int x=0;
	for(int i=1;i<=100;++i)x+=rand()%20000;
	return x;
}

void check(long long p)
{
	if(p<=ans)return;
	int i,aim=n/2+n%2,tot=0;
	for(i=1;i<=n;++i)
	{
		if(a[i]%p==0)++tot;
		if(tot+n-i<aim)break;
		if(tot>=aim)break;
	}
	if(tot>=aim)ans=p;
}

long long gcd(long long a,long long b){if(!b)return a;return gcd(b,a%b);}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	ans=1;
	for(shi=1;shi<=5;++shi)
	{
		tmp=gcd(a[ran()%n+1],a[ran()%n+1]);
		for(i=1;i*i<=tmp;++i)
		if(tmp%i==0)
		{
			check(i);
			if(i*i!=tmp)check(tmp/i);
		}
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;

int n,i,j,k,shi;
long long a[1000005],ans,tmp;

int ran()
{
	int x=0;
	for(int i=1;i<=100;++i)x+=rand()%20000;
	return x;
}

void check(long long p)
{
	if(p<=ans)return;
	int i,aim=n/2+n%2,tot=0;
	for(i=1;i<=n;++i)
	{
		if(a[i]%p==0)++tot;
		//if(tot+n-i<aim)break;
		//if(tot>=aim)break;
	}
	if(tot>=aim)ans=p;
}

long long gcd(long long a,long long b){if(!b)return a;return gcd(b,a%b);}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	ans=1;
	for(shi=1;shi<=20;++shi)
	{
		//tmp=gcd(a[ran()%n+1],a[ran()%n+1]);
		tmp=gcd(a[rand()%n+1],a[rand()%n+1]);
		for(i=1;i*i<=tmp;++i)
		if(tmp%i==0)
		{
			check(i);
			check(tmp/i);
		}
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;

int n,i,j,k,shi;
long long a[1000005],ans,tmp;

int ran()
{
	int x=0;
	for(int i=1;i<=100;++i)x+=rand()%20000;
	return x;
}

void check(long long p)
{
	if(p<=ans)return;
	int i,tot=0;
	for(i=1;i<=n;++i)
	if(a[i]%p==0)++tot;
	if(tot>=n/2+n%2)ans=p;
}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	ans=1;
	for(shi=1;shi<=10;++shi)
	{
		tmp=a[ran()%n+1];
		for(i=1;i*i<=tmp;++i)
		if(tmp%i==0)
		{
			check(i);
			if(i*i!=tmp)check(tmp/i);
		}
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;

int n,i,j,k,shi;
long long a[1000005],ans,tmp;
bool shi_;

int ran()
{
	int x=0;
	for(int i=1;i<=100;++i)x+=rand()%20000;
	return x;
}

bool check(long long p)
{
	if(p<=ans)return false;
	int i,aim=n/2+n%2,tot=0;
	for(i=1;i<=n;++i)
	{
		if(a[i]%p==0)++tot;
		if(tot+n-i<aim)break;
		if(tot>=aim)break;
	}
	if(tot>=aim){ans=p;return true;}
	return false;
}

long long gcd(long long a,long long b){if(!b)return a;return gcd(b,a%b);}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	ans=1;
	for(shi=1;shi<=10;++shi)
	{
		tmp=gcd(a[ran()%n+1],a[ran()%n+1]);
		shi_=false;
		for(i=1;(long long)i*i<=tmp;++i)if(tmp%i==0)if(check(tmp/i)){shi_=true;break;}
		if(shi_)continue;
		for(;i;--i)if(tmp%i==0)if(check(i))break;
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;

int n,i,j,k,shi;
long long a[1000005],ans,tmp;
bool shi_;

int ran()
{
	int x=0;
	for(int i=1;i<=100;++i)x+=rand()%20000;
	return x;
}

bool check(long long p)
{
	if(p<=ans)return false;
	int i,aim=n/2+n%2,tot=0;
	for(i=1;i<=n;++i)
	{
		if(a[i]%p==0)++tot;
		if(tot+n-i<aim)break;
		if(tot>=aim)break;
	}
	if(tot>=aim){ans=p;return true;}
	return false;
}

long long gcd(long long a,long long b){if(!b)return a;return gcd(b,a%b);}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	ans=1;
	for(shi=1;shi<=20;++shi)
	{
		tmp=gcd(a[ran()%n+1],a[ran()%n+1]);
		shi_=false;
		for(i=1;(long long)i*i<=tmp;++i)if(tmp%i==0)if(check(tmp/i)){shi_=true;break;}
		if(shi_)continue;
		for(;i;--i)if(tmp%i==0)if(check(i))break;
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
#include <set>
using namespace std;

int n,i,j,k,shi;
long long a[1000005],ans,tmp;
bool shi_;

set <long long> Done;

int ran()
{
	int x=0;
	for(int i=1;i<=100;++i)x+=rand()%20000;
	return x;
}

bool check(long long p)
{
	if(p<=ans)return false;
	if(Done.find(p)!=Done.end())return false;
	Done.insert(p);
	int i,aim=n/2+n%2,tot=0;
	for(i=1;i<=n;++i)
	{
		if(a[i]%p==0)++tot;
		if(tot+n-i<aim)break;
		if(tot>=aim)break;
	}
	if(tot>=aim){ans=p;return true;}
	return false;
}

long long gcd(long long a,long long b){if(!b)return a;return gcd(b,a%b);}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	ans=1;
	for(shi=1;shi<=15;++shi)
	{
		tmp=gcd(a[ran()%n+1],a[ran()%n+1]);
		shi_=false;
		for(i=1;(long long)i*i<=tmp;++i)if(tmp%i==0)if(check(tmp/i)){shi_=true;break;}
		if(shi_)continue;
		for(;i;--i)if(tmp%i==0)if(check(i))break;
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;

int n,i,j,k,shi;
long long a[1000005],ans,tmp;

int ran()
{
	int x=0;
	for(int i=1;i<=100;++i)x+=rand()%20000;
	return x;
}

void check(long long p)
{
	if(p<=ans)return;
	int i,aim=n/2+n%2,tot=0;
	for(i=1;i<=n;++i)
	{
		if(a[i]%p==0)++tot;
		if(tot+n-i<aim)break;
		if(tot>=aim)break;
	}
	if(tot>=aim)ans=p;
}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	ans=1;
	for(shi=1;shi<=5;++shi)
	{
		tmp=a[ran()%n+1];
		for(i=1;i*i<=tmp;++i)
		if(tmp%i==0)
		{
			check(i);
			if(i*i!=tmp)check(tmp/i);
		}
	}
	printf("%I64d\n",ans);
}


#include <stdio.h>
#include <stdlib.h>
using namespace std;

int n,i,j,k,shi;
long long a[1000005],ans,tmp;

int ran()
{
	int x=0;
	for(int i=1;i<=100;++i)x+=rand()%20000;
	return x;
}

void check(long long p)
{
	if(p<=ans)return;
	int i,tot=0;
	for(i=1;i<=n;++i)
	if(a[i]%p==0)++tot;
	if(tot>=n/2+n%2)ans=p;
}

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
	ans=1;
	for(shi=1;shi<=5;++shi)
	{
		tmp=a[ran()%n+1];
		for(i=1;i*i<=tmp;++i)
		if(tmp%i==0)
		{
			check(i);
			if(i*i!=tmp)check(tmp/i);
		}
	}
	printf("%I64d\n",ans);
}


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


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int t, n, k, l, i,s,m,x,p, a[20000];
    cin>>t;
    while(t--)
    {
        m=0;s=0;
        cin>>n>>p>>k;
        for(i=0;i<n;i++)
        {
            scanf("%lld", &a[i]);
            //a.push_back(x);
        }
        sort(a, a+n);
        for(i=0;i<n;i++)
        {
            if(p>=a[i])
            {
                p-=a[i];
                m++;
                l=a[i];

            }
        }
        if(m+(k-1)>n)
            m=n;

        if(m==0||m==n)
            cout<<m<<endl;
        else
            cout<<m+(k-1)<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int t, n, k, l, i,s,m,x,p, a[20000];
    cin>>t;
    while(t--)
    {
        m=0;s=0;
        cin>>n>>p>>k;
        for(i=0;i<n;i++)
        {
            scanf("%lld", &a[i]);
            //a.push_back(x);
        }
        s=p;
        sort(a, a+n);
        for(i=0;i<n;i++)
        {
            if(p>=a[i])
            {
                p-=a[i];
                m++;
                l=a[i+1];
               // cout<<l<<endl;
            }
        }
        if(m+(k-1)>n)
            m=n;

        if(l>s||m==0||m==n)
            cout<<m<<endl;
        else
            cout<<m+(k-1)<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int t, n, p,k,i,j,c=0;
    scanf("%lld", &t);
    while(t--)
    {
        c=0;
        scanf("%lld%lld%lld",&n,&p,&k);
         long long int a[n],sum[n];
        a[0]=0;
        for(i=1;i<=n;i++)
            scanf("%lld", &a[i]);
        sort(a,a+n+1);
        sum[0]=0;
        for(i=1;i<k;i++)
            sum[i]=sum[i-1]+a[i];
        //sum[k]=a[k];
        for(i=k;i<=n;i++)
            sum[i]=sum[i-k]+a[i];
        for(i=1;i<=n;i++)
        {
            if(sum[i]<=p)
                c=i;
        }
        printf("%lld\n", c);
    }
}


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


#include<bits/stdc++.h>
#define AmrYassen                 \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int mod = 1e7 + 7;
ll minn = INT_MAX;
int main() {
	AmrYassen ll n;
	cin >> n;
	while (n--) {
		ll m, q, a;
		cin >> m >> q >> a;
		vector<ll> v(m, 0);
		for (int i = 0; i < m; i++) {
			cin >> v[i];
		}
		sort(v.begin(), v.end());
		ll summf = 0, summs = 0;
		for (int i = 0; i < v.size(); i++) {
			if (i & 1) {
				summs += v[i];
			}
			else summf += v[i];
		}
		vector<ll>vv;
		vv.push_back(summf);
		vv.push_back(summs);
		for (int i = v.size()-1; i >= 0; i--) {
			if (i % 2 == 0) {
				summf -= v[i];
				if(summf)
				vv.push_back(summf);
			}
			else if (i % 2 != 0) {
				summs -= v[i];
				if (summs)
					vv.push_back(summs);
			}
		}
		ll c = 0;
		for (int i = 0; i < vv.size(); i++) {
			if (vv[i] <= q) {
				c++;
			}
		}
		cout << c << endl;
	}
	return 0;
}
/*
4 9 2
2 3 4 5*/


#include <bits/stdc++.h>
#define AmrYassen                 \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int mod = 1e7 + 7;
ll minn = INT_MAX;
int main() {
    AmrYassen ll n;
    cin >> n;
    while (n--) {
        ll m, q, a;
        cin >> m >> q >> a;
        vector<ll> v(m, 0);
        for (int i = 0; i < m; i++) {
            cin >> v[i];
        }
        sort(v.begin(), v.end());
        ll c = 0, summ = q, summ2 = 0;
        bool isfound[m];
        memset(isfound, false, sizeof isfound);
        for (int i = ceil(double(v.size()) / 2.0); i >= 0; i--) {
            if (v[i] <= summ) {
                if (i == 0) {
                    isfound[i] = 1;
                    c++;
                } else {
                    isfound[i] = 1;
                    isfound[i - 1] = 1;
                    c += 2;
                }
                summ -= v[i];
                break;
            }
        }
        for (int i = 0; i < v.size(); i++) {
            if (!isfound[i]) {
                if (v[i] <= summ) {
                    summ -= v[i];
                    c++;
                } else
                    break;
            }
        }
        ll maxx = 0;
        maxx = max(maxx, c);
        c = 0, summ = q, summ2 = 0;
        memset(isfound, false, sizeof isfound);
        for (int i = v.size() / 2; i >= 0; i--) {
            if (v[i] <= summ) {
                if (i == 0) {
                    isfound[i] = 1;
                    c++;
                } else {
                    isfound[i] = 1;
                    isfound[i - 1] = 1;
                    c += 2;
                }
                summ -= v[i];
                break;
            }
        }
        for (int i = 0; i < v.size(); i++) {
            if (!isfound[i]) {
                if (v[i] <= summ) {
                    summ -= v[i];
                    c++;
                } else
                    break;
            }
        }
        maxx = max(maxx, c);

        c = 0, summ = q, summ2 = 0;
        for (int i = 0; i < v.size(); i++) {
            if (v[i] <= summ) {
                summ2 = summ;
                summ -= v[i];
                c++;
            } else {
                if (summ2 >= v[i]) {
                    c++;
                    break;
                }
            }
        }
        maxx = max(maxx, c);

        cout << maxx << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define AmrYassen                 \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int mod = 1e7 + 7;
ll minn = INT_MAX;
int main() {
	AmrYassen ll n;
	cin >> n;
	while (n--) {
		ll m, q, a;
		cin >> m >> q >> a;
		vector<ll> v(m, 0);
		for (int i = 0; i < m; i++) {
			cin >> v[i];
		}
		sort(v.begin(), v.end());
		ll c = 0, summ = q, summ2 = 0;
		for (int i = 0; i < v.size(); i++) {
			if (v[i] <= summ) {
				summ2 = summ;
				summ -= v[i];
				c++;
			}
			else {
				if (!i)break;
				if (summ2 >= v[i-1]) {
					c++;
					break;
				}
			}
		}
		cout << c << endl;
	}
	return 0;
}
/*
4 9 2
2 3 4 5*/


#include <bits/stdc++.h>
#define AmrYassen                 \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int mod = 1e7 + 7;
ll minn = INT_MAX;
int main() {
    AmrYassen ll n;
    cin >> n;
    while (n--) {
        ll m, q, a;
        cin >> m >> q >> a;
        vector<ll> v(m, 0);
        for (int i = 0; i < m; i++) {
            cin >> v[i];
        }
        sort(v.begin(), v.end());
        ll c = 0, summ = q, summ2 = 0;
        for (int i = 0; i < v.size(); i++) {
            if (v[i] <= summ) {
                summ2 = summ;
                summ -= v[i];
                c++;
            } else {
                if (summ2 >= v[i]) {
                    c++;
                    break;
                }
            }
        }
        cout << c << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define AmrYassen                 \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int mod = 1e7 + 7;
ll minn = INT_MAX;
int main() {
	AmrYassen ll n;
	cin >> n;
	while (n--) {
		ll m, q, a;
		cin >> m >> q >> a;
		vector<ll> v(m, 0);
		for (int i = 0; i < m; i++) {
			cin >> v[i];
		}
		sort(v.begin(), v.end());
		ll summf = 0, summs = 0;
		for (int i = 0; i < v.size(); i++) {
			if (i & 1) {
				summs += v[i];
			}
			else summf += v[i];
		}
		vector<ll>vv;
		vv.push_back(summf);
		vv.push_back(summs);
		for (int i = v.size()-1; i >= 0; i--) {
			if (i % 2 == 0) {
				summf -= v[i];
				if(summf)
				vv.push_back(summf);
			}
			else if (i % 2 != 0) {
				summs -= v[i];
				if (summs)
					vv.push_back(summs);
			}
		}
		ll c = 0;
		for (int i = 0; i <vv.size(); i++)
		{
		//	cout << vv[i] << " ";
			if (q >= vv[i]) { cout << vv.size()-i << endl; c++; break; }
		}
		if (!c)cout << 0 << endl;
		//cout << c << endl;
	}
	return 0;
}
/*
4 9 2
2 3 4 5*/


#include<bits/stdc++.h>
#define AmrYassen                 \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int mod = 1e7 + 7;
ll minn = INT_MAX;
int main() {
	AmrYassen ll n;
	cin >> n;
	while (n--) {
		ll m, q, a;
		cin >> m >> q >> a;
		vector<ll> v(m, 0);
		for (int i = 0; i < m; i++) {
			cin >> v[i];
		}
		sort(v.begin(), v.end());
		ll summf = 0, summs = 0;
		for (int i = 0; i < v.size(); i++) {
			if (i & 1) {
				summs += v[i];
			}
			else summf += v[i];
		}
		vector<ll>vv;
		vv.push_back(summf);
		vv.push_back(summs);
		for (int i = v.size()-1; i >= 0; i--) {
			if (i % 2 == 0) {
				summf -= v[i];
				if(summf)
				vv.push_back(summf);
			}
			else if (i % 2 != 0) {
				summs -= v[i];
				if (summs)
					vv.push_back(summs);
			}
		}
		ll c = 0;
		sort(vv.rbegin(), vv.rend());
		for (int i = 0; i <vv.size(); i++)
		{
			if (q >= vv[i]) { cout << vv.size() - i << endl; c++; break;  }
		}
		if (!c)cout << 0 << endl;
		//cout << c << endl;
	}
	return 0;
}
/*
4 9 2
2 3 4 5*/


#include<bits/stdc++.h>
using namespace std;

/// read the question correctly (is y a vowel?)
/// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)

const int mx =  1234567;
const double exp1 =  1e-6;
const int md =  1e9+7;
typedef long long ll;

int ar[mx],xar[mx],uses[mx];
ll arr[mx];
int tab[1000][1000];
string st[1000];
map<int ,int>mpii;
map<int ,char>mpic;
map<char ,int>mpci;
pair<int, int>pr[mx];

int ar1[mx],ar2[mx];

int main(){

    int tk=1,n,m=0;
//    freopen("in.txt","r", stdin);
//    freopen("out.txt","w", stdout);
    ll x,y,z;
    int a,b,c;
    scanf("%d",&tk);
    while(tk--){
        int p , k;
        scanf("%d%d%d",&n,&p,&k);
        for(int i = 0; i < n; i++){
            scanf("%d",ar + i);
        }
        sort(ar, ar + n);
        int cnt = 0, f = 0;
        if(ar[0] < p && n == 1){
            cout<<0<<endl;
            continue;
        }
        for(int i = 0; i < n-1; i++){
            if(ar[0] > p){
                f = 1;
                break;
            }
            if(p - ar[i] < ar[i+1]){
                cnt++;
                if(p - ar[i+1] >= 0){
                    cnt++;
                    p = p - ar[i+1];
                }
                p = p - ar[i];
                f = 1;
                break;
            }
            p = p - ar[i];
            cnt++;
        }
        if(f == 0){
            cout<<n<<endl;
            continue;
        }
        cout<<cnt<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

/// read the question correctly (is y a vowel?)
/// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)

const int mx =  1234567;
const double exp1 =  1e-6;
const int md =  1e9+7;
typedef long long ll;

int ar[mx],xar[mx],uses[mx];
ll arr[mx];
int tab[1000][1000];
string st[1000];
map<int ,int>mpii;
map<int ,char>mpic;
map<char ,int>mpci;
pair<int, int>pr[mx];

int ar1[mx],ar2[mx];

int main(){

    int tk=1,n,m=0;
//    freopen("in.txt","r", stdin);
//    freopen("out.txt","w", stdout);
    ll x,y,z;
    int a,b,c;
    scanf("%d",&tk);
    while(tk--){
        int p , k;
        scanf("%d%d%d",&n,&p,&k);
        for(int i = 0; i < n; i++){
            scanf("%d",ar + i);
        }
        sort(ar, ar + n);
        int cnt = 0, f = 0;
        if(n == 1){
            if(ar[0] > p)cout<<0<<endl;
            else cout<<n<<endl;
            continue;
        }
        if(n==2){
            if(ar[0] > p){
                cout<<0<<endl;
                continue;
            }
            if(ar[0] <= p) cnt++;
            if(ar[1] <= p) cnt++;
            cout<<cnt<<endl;
            continue;
        }

        int ind = -1;
        for(int i = 0; i < n-2; i++){
            if(ar[0] > p){
                f = 1;
                break;
            }
            ind = i;
            if(p - ar[i+1] >= ar[i+2]){
                cnt+=2;
                i++;
                ind = i;
                p -= ar[i+1];
//                cerr<<"K1>"<<endl;
           }
            else if(p - ar[i] < ar[i+1]){
                cnt++;
                if(p - ar[i+1] >= 0){
                    cnt++;
                    p = p - ar[i+1];
                }else p = p - ar[i];
//                cerr<<"K2>"<<endl;
                break;
            }
            else if(p >= ar[i]){
                p = p - ar[i];
                cnt++;
//                cerr<<"K3>"<<endl;
            }

        }
        if(ind >= n-1 ){
            cout<<n<<endl;
            continue;
        }
        if(p >= max(ar[n-1], ar[n-2])){
            cout<<n<<endl;
            continue;
        }
        if(p >= ar[n-2]){
            cout<<n-1<<endl;
            continue;
        }
        cout<<cnt<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

/// read the question correctly (is y a vowel?)
/// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)

const int mx =  1234567;
const double exp1 =  1e-6;
const int md =  1e9+7;
typedef long long ll;

int ar[mx],xar[mx],uses[mx];
ll arr[mx];
int tab[1000][1000];
string st[1000];
map<int ,int>mpii;
map<int ,char>mpic;
map<char ,int>mpci;
pair<int, int>pr[mx];

int ar1[mx],ar2[mx],ar3[mx],ar4[mx];

int main(){

    int tk = 1,n,m;
//    freopen("in.txt","r", stdin);
//    freopen("out.txt","w", stdout);
    scanf("%d",&tk);
    while(tk--){
        int n, p, k;
        scanf("%d%d%d",&n,&p,&k);
        for(int i = 0; i < n; i++){
            scanf("%d",ar + i);
        }
        sort(ar, ar + n);
        int pref = 0;
        int ans = 0;
        int cnt = 0;
        int sum = 0;
        for(int i = 0; i < k; i++){
            sum = pref;
            if(sum > p) break;
            cnt = i;
            for(int j = i+k-1; j < n; j+=k){
                if(sum+ar[j] <= p){
                    sum +=ar[j];
                    cnt +=k;
                }else break;

            }
            pref += ar[i];
            ans = max(ans,cnt);
        }
        cout<<ans<<'\n';

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

/// read the question correctly (is y a vowel?)
/// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)

const int mx =  1234567;
const double exp1 =  1e-6;
const int md =  1e9+7;
typedef long long ll;

ll ar[mx],xar[mx],uses[mx];
ll arr[mx];
int tab[1000][1000];
string st[1000];
map<int ,int>mpii;
map<int ,char>mpic;
map<char ,int>mpci;
pair<int, int>pr[mx];

int ar1[mx],ar2[mx];

int main(){

    int tk=1,n,m=0;
//    freopen("in.txt","r", stdin);
//    freopen("out.txt","w", stdout);
    ll x,y,z;
    int a,b,c;
    scanf("%d",&tk);
    while(tk--){
        ll p , k;
        scanf("%d%lld%lld",&n,&p,&k);
        for(int i = 0; i < n; i++){
            scanf("%lld",ar + i);
        }
        sort(ar, ar + n);
        int cnt = 0, f = 0;
        if(n == 1){
            if(ar[0] > p)cout<<0<<endl;
            else cout<<n<<endl;
            continue;
        }
        if(n==2){
            if(ar[0] > p){
                cout<<0<<endl;
                continue;
            }
            if(ar[0] <= p) cnt++;
            if(ar[1] <= p) cnt++;
            cout<<cnt<<endl;
            continue;
        }

        int ind = -1;
        for(int i = 0; i < n-2; i++){
            if(ar[0] > p){
                f = 1;
                break;
            }
            if(p - ar[i+1] >= ar[i+2]){
                cnt+=2;
                i++;
                ind = i;
                p -= ar[i+1];
//                cerr<<"K1>"<<endl;
           }
            else if(p - ar[i] < ar[i+1]){
                cnt++;
                ind = i;
                if(p - ar[i+1] >= 0){
                    cnt++;
                    p = p - ar[i+1];
                    ind = i+1;
                }else p = p - ar[i];
//                cerr<<"K2>"<<endl;
                break;
            }
            else if(p >= ar[i]){
                p = p - ar[i];
                cnt++;
                ind = i;
//                cerr<<"K3>"<<endl;
            }

        }
        if(ind >= n-1 ){
            cout<<n<<endl;
            continue;
        }
        if(p >= max(ar[n-1], ar[n-2])){
            cout<<n<<endl;
            continue;
        }
        if(p >= ar[n-2]){
            cout<<n-1<<endl;
            continue;
        }
        cout<<cnt<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

/// read the question correctly (is y a vowel?)
/// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)

const int mx =  1234567;
const double exp1 =  1e-6;
const int md =  1e9+7;
typedef long long ll;

int ar[mx],xar[mx],uses[mx];
ll arr[mx];
int tab[1000][1000];
string st[1000];
map<int ,int>mpii;
map<int ,char>mpic;
map<char ,int>mpci;
pair<int, int>pr[mx];

int ar1[mx],ar2[mx];

int main(){

    int tk=1,n,m=0;
//    freopen("in.txt","r", stdin);
//    freopen("out.txt","w", stdout);
    ll x,y,z;
    int a,b,c;
    scanf("%d",&tk);
    while(tk--){
        int p , k;
        scanf("%d%d%d",&n,&p,&k);
        for(int i = 0; i < n; i++){
            scanf("%d",ar + i);
        }
        sort(ar, ar + n);
        int cnt = 0, f = 0;
        if(n == 1){
            if(ar[0] > p)cout<<0<<endl;
            else cout<<n<<endl;
            continue;
        }
        for(int i = 0; i < n-1; i++){
            if(ar[0] > p){
                f = 1;
                break;
            }
            if(p - ar[i] < ar[i+1]){
                cnt++;
                if(p - ar[i+1] >= 0){
                    cnt++;
                    p = p - ar[i+1];
                }
                p = p - ar[i];
                f = 1;
                break;
            }
            p = p - ar[i];
            cnt++;
        }
        if(f == 0){
            cout<<n<<endl;
            continue;
        }
        cout<<cnt<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

/// read the question correctly (is y a vowel?)
/// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)

const int mx =  1234567;
const double exp1 =  1e-6;
const int md =  1e9+7;
typedef long long ll;

int ar[mx],xar[mx],uses[mx];
ll arr[mx];
int tab[1000][1000];
string st[1000];
map<int ,int>mpii;
map<int ,char>mpic;
map<char ,int>mpci;
pair<int, int>pr[mx];

int ar1[mx],ar2[mx];

int main(){

    int tk=1,n,m=0;
//    freopen("in.txt","r", stdin);
//    freopen("out.txt","w", stdout);
    ll x,y,z;
    int a,b,c;
    scanf("%d",&tk);
    while(tk--){
        int p , k;
        scanf("%d%d%d",&n,&p,&k);
        for(int i = 0; i < n; i++){
            scanf("%d",ar + i);
        }
        sort(ar, ar + n);
        int cnt = 0, f = 0;
        if(n == 1){
            if(ar[0] > p)cout<<0<<endl;
            else cout<<n<<endl;
            continue;
        }
        if(n==2){
            if(ar[0] > p){
                cout<<0<<endl;
                continue;
            }
            if(ar[0] <= p) cnt++;
            if(ar[1] <= p) cnt++;
            cout<<cnt<<endl;
            continue;
        }

        int ind = -1;
        for(int i = 0; i < n-2; i++){
            if(ar[0] > p){
                f = 1;
                break;
            }
            ind = i;
            if(p - ar[i+1] >= ar[i+2]){
                cnt+=2;
                i++;
                ind = i;
                p -= ar[i+1];
//                cerr<<"K1>"<<endl;
           }
            else if(p - ar[i] < ar[i+1]){
                cnt++;
                if(p - ar[i+1] >= 0){
                    cnt++;
                    p = p - ar[i+1];
                }
                p = p - ar[i];
//                cerr<<"K2>"<<endl;
                break;
            }
            else if(p >= ar[i]){
                p = p - ar[i];
                cnt++;
//                cerr<<"K3>"<<endl;
            }

        }
        if(ind == n){
            cout<<n<<endl;
            continue;
        }
        if(p >= max(ar[n-1], ar[n-2])){
            cout<<n<<endl;
            continue;
        }
        if(p >= ar[n-2]){
            cout<<n-1<<endl;
            continue;
        }
        cout<<cnt<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

/// read the question correctly (is y a vowel?)
/// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?)

const int mx =  1234567;
const double exp1 =  1e-6;
const int md =  1e9+7;
typedef long long ll;

int ar[mx],xar[mx],uses[mx];
ll arr[mx];
int tab[1000][1000];
string st[1000];
map<int ,int>mpii;
map<int ,char>mpic;
map<char ,int>mpci;
pair<int, int>pr[mx];

int ar1[mx],ar2[mx];

int main(){

    int tk=1,n,m=0;
//    freopen("in.txt","r", stdin);
//    freopen("out.txt","w", stdout);
    ll x,y,z;
    int a,b,c;
    scanf("%d",&tk);
    while(tk--){
        int p , k;
        scanf("%d%d%d",&n,&p,&k);
        for(int i = 0; i < n; i++){
            scanf("%d",ar + i);
        }
        sort(ar, ar + n);
        int cnt = 0, f = 0;
        if(n == 1){
            if(ar[0] > p)cout<<0<<endl;
            else cout<<n<<endl;
            continue;
        }
        if(n==2){
            if(ar[0] > p){
                cout<<0<<endl;
                continue;
            }
            if(ar[0] <= p) cnt++;
            if(ar[1] <= p) cnt++;
            cout<<cnt<<endl;
            continue;
        }

        int ind = -1;
        for(int i = 0; i < n-2; i++){
            if(ar[0] > p){
                f = 1;
                break;
            }
            if(p - ar[i+1] >= ar[i+2]){
                cnt+=2;
                i++;
                ind = i;
                p -= ar[i+1];
//                cerr<<"K1>"<<endl;
           }
            else if(p - ar[i] < ar[i+1]){
                cnt++;
                ind = i;
                if(p - ar[i+1] >= 0){
                    cnt++;
                    p = p - ar[i+1];
                    ind = i+1;
                }else p = p - ar[i];
//                cerr<<"K2>"<<endl;
                break;
            }
            else if(p >= ar[i]){
                p = p - ar[i];
                cnt++;
                ind = i;
//                cerr<<"K3>"<<endl;
            }

        }
        if(ind >= n-1 ){
            cout<<n<<endl;
            continue;
        }
        if(p >= max(ar[n-1], ar[n-2])){
            cout<<n<<endl;
            continue;
        }
        if(p >= ar[n-2]){
            cout<<n-1<<endl;
            continue;
        }
        cout<<cnt<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
#define NN 1000100
#define x first
#define y second
using namespace std;
typedef long long INT;
typedef pair<int,int> pii;
pii p[1000100];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int t;
	cin>>t;
	int n,p,k;
	while(t--){
		scanf("%d%d%d",&n,&p,&k);
		int a[n+10];
		for(int i=0;i<n;i++)scanf("%d",a+i);
		sort(a,a+n);
		int cnt=0;
		if(a[0]>p) {cout<<0<<endl;continue;}
		int i,f=0;
		for(i=0;i<n;i++){
			if(p>=a[i]) {p-=a[i],cnt++;}
			else {break;}
		}
		if(cnt<n)cnt++;
		cout<<cnt<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
using namespace std;
typedef double DO;
typedef long long INT;
typedef pair<int,int> pii;
typedef pair<pii,int> pi3;
template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}
#define NN 200010
int T, n, p, k;
int a[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	cin>>T;
	while (T--) {
		scanf("%d%d%d", &n, &p, &k);
		int pp;
		for (int i=1; i<=n; i++) scanf("%d", &a[i]);
		sort(a+1, a+n+1);
		int ans=0;

		if (n<k) {
			for (int i=1; i<=n; i++) if (p>=a[i]) p-=a[i], ans++;
			cout<<ans<<endl; continue;
		}

		for (int i=1; i<k; i++) {
			int cnt=0;
			pp=p;
			for (int j=i+k; j<=n; j+=k) {
				if (pp>=a[j]) pp-=a[j], cnt+=k;
			}
			for (int j=1; j<=i; j++) {
				if (pp>=a[j]) pp-=a[j], cnt++;
			}
			ans=max(ans, cnt);
		}
		pp=p;
		int cnt=0;
		for (int i=k; i<=n; i+=k) {
			if (pp>=a[i]) pp-=a[i], cnt+=k;
		}
		cout<<max(ans, cnt)<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>

#define NN 200100

using namespace std;

typedef long long INT;

int c[NN],mx[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int t;
	cin>>t;
	while(t--){
		int n,p,k;
		scanf("%d %d %d",&n,&p,&k);
		for(int i=1;i<=n;i++)scanf("%d",&c[i]);
		sort(c+1,c+n+1);
		mx[1]=c[1];
		for(int i=2;i<=n;i++){
			if(i<k)mx[i]=mx[i-1]+c[i];
			else mx[i]=c[i]+mx[i-k];
		}
		int id=upper_bound(mx+1,mx+n+1,p)-mx-1;
		printf("%d\n",id);
	}

	return 0;
}


#include <bits/stdc++.h>
#define NN 200100
#define inf 1e9
using namespace std;

typedef long long INT;
typedef pair<int, int> pii;

int cs[NN];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int test;
	scanf("%d", &test);
	while(test--){
		int n, p, k;
		scanf("%d%d%d", &n, &p, &k);
		for(int i = 1; i <= n; i++) scanf("%d", &cs[i]);
		sort(cs + 1, cs + n + 1);
		int cnt = 0;
		int temp = p, tmp;
		int flag = 0;
		for(int i = 2; i <= n; i+=2){
			if(temp >= cs[i]) {
				temp -= cs[i];
				cnt++;
			}
			else {
				if(temp >= cs[i - 1]) flag = 1;
				break;
			}
		}
		tmp = cnt * 2;
		if(flag) tmp++;
		cnt = 0, flag = 0;
		cs[0] = inf;
		for(int i = 1; i <= n; i+=2){
			if(p >= cs[i]) {
				p -= cs[i];
				cnt++;
			}
			else {
				if(p >= cs[i - 1]) flag = 1;
				break;
			}
		}

		int ans = cnt * 2;
		if(!flag) ans--;
		cerr<<ans<<" "<<tmp<<endl;
		printf("%d\n", max(ans, tmp));
	}
	return 0;
}


#include <bits/stdc++.h>

#define x first
#define y second
using namespace std;

#define NN 200010
int T, n, p, k;
int a[NN];
long long s[NN];

int main () {
#ifndef ONLINE_JUDGE
   freopen("in.in","r",stdin);
   freopen("out.out", "w", stdout);
#endif
	scanf("%d", &T);
	while (T--) {
		scanf("%d%d%d", &n, &p, &k);
		for (int i=1; i<=n; i++) scanf("%d", &a[i]);
		sort(a+1, a+n+1);

		for (int i=1; i<k; i++) s[i]=s[i-1]+a[i];
		int pp=p;
		int ans=0, cnt;
		for (int i=1; i<k; i++) {
			cnt=0;
			pp=p;
			for (int j=i+k; j<=n; j+=k) {
				if (pp>=a[j]) pp-=a[j], cnt+=k;
			}
			int pos=upper_bound(s+1, s+k, pp)-s-1;
			pos = min(pos, i);
			ans=max(ans, cnt+pos);
		}
		cnt=0;
		pp=p;
		for (int i=k; i<=n; i+=k) if (pp>=a[i]) pp-=a[i], cnt+=k;
		printf("%d\n", max(ans, cnt));
	}
}


#include <bits/stdc++.h>
#define x first
#define y second

using namespace std;

typedef long long INT;
typedef pair<int,int> pii;

const int NN=200010;
const int inf=0x3f3f3f3f;

int a[NN];

main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	int t,n,p,k,sum,id;
	cin>>t;
	while(t--){
		scanf("%d%d%d",&n,&p,&k);
		for(int i=0;i<n;i++)scanf("%d",a+i);
		sort(a,a+n);

		id=0; sum=0;
		for(;id<n && sum<=p;id++){
			if(sum+a[id] > p)break;
			sum+=a[id];
		}

//		for(int i=0;i<n;i++)cout<<a[i]<<" ";
//		cout<<endl;
//
		if(id==0){cout<<0<<endl;continue;}
		if(id==n){cout<<n<<endl;continue;}
		if(sum+a[id+1]-a[id]<=p)id++;
		cout<<id<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long INT;
int a[200020];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int t;
	cin>>t;
	int n,p,k;
	while(t--){
		scanf("%d%d%d",&n,&p,&k);
		INT sum=0;
		for(int i=0;i<n;i++){scanf("%d",a+i);sum+=a[i];}
		if(sum<=p) {cout<<n<<endl;continue;}
		sort(a,a+n);
		int cnt=0,ans=0;
		int s=0;
		for(int i=2;i<n;i+=2){
			if(i>n-1)break;
			if(s+a[i]<=p) {s+=a[i];cnt+=2;}
			else {
				if(s+a[i-1]<=p) {s+=a[i];cnt+=2;}
				else if(s+a[0]<=p) {s+=a[0];cnt++;}
			}
		}
		s=0;
		for(int i=1;i<n;i+=2){
			if(i>n-1)break;
			if(s+a[i]<=p) {s+=a[i];ans+=2;}
			else {
				if(s+a[i-1]<=p) {s+=a[i];ans++;}
			}
		}
		int re=max(cnt,ans);
		cout<<re<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[200200],s[200200],sum[200200];
int main(){
	int t,n,p,k;
	scanf("%d",&t);
	while(t--){
		memset(a,0,sizeof a);
		memset(s,0,sizeof s);
		memset(sum,0,sizeof sum);

		scanf("%d%d%d",&n,&p,&k);
		for(int i=1;i<=n;i++) scanf("%d",a+i);
		sort(a+1,a+n+1);
		for(int i=k;i<=n;i++){
			s[i]=s[i-k]+a[i];
		}
		for(int i=1;i<k;i++) sum[i]=sum[i-1]+a[i];
		int it=upper_bound(s+1,s+n+1,p)-s-1;
		int ans=it/k,ss=ans*k;
		for(int i=ans*k+1;i<=it;i++){
			int pp=p-s[i];
			int d=upper_bound(sum+1,sum+(i%k)+1,pp)-sum-1;
			ss=max(ss,ans*k+d);
		}
		cout<<ss<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define NN 1000100
#define x first
#define y second
using namespace std;
typedef long long INT;
typedef pair<int,int> pii;
pii p[1000100];
int a[200020];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int t;
	cin>>t;
	int n,p,k;
	while(t--){
		scanf("%d%d%d",&n,&p,&k);
		INT sum=0;
		for(int i=0;i<n;i++){scanf("%d",a+i);sum+=a[i];}
		if(sum<=p) {cout<<n<<endl;continue;}
		sort(a,a+n);
		int cnt,i;
		INT s=0;
		for(i=0;i<n;i++){
			if(s+a[i]<=p) s+=a[i];
			else {s+=a[i];break;}
		}
//		cout<<s<<endl;
		if(s-a[i-1]<=p) cnt=i+1;
		else  cnt=i;
		cout<<cnt<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>

#define x first
#define y second
using namespace std;

typedef long long INT;
typedef pair<int,int> pii;

#define NN 200010
int T, n, p, k;
int a[NN];
INT s[NN];

int main () {
#ifndef ONLINE_JUDGE
   freopen("in.in","r",stdin);
   freopen("out.out", "w", stdout);
#endif
	for (scanf("%d", &T); T--;) {
		scanf("%d%d%d", &n, &p, &k);
		int pp=p, ans=0;
		for (int i=1; i<=n; i++) scanf("%d", &a[i]);
		sort(a+1, a+n+1);
		if (n<=k) {
			if (n==k and a[n]<=pp) {printf("%d\n", k); continue;}
			for (int i=1; i<=n; i++) if (a[i]<=pp) pp-=a[i], ans++;
			printf("%d\n", ans); continue;
		}
		for (int i=1; i<k; i++) s[i]=s[i-1]+a[i];
		pp=p;
		for (int i=1; i<k; i++) {
			int cnt=0;
			pp=p;
			for (int j=i+k; j<=n; j+=k) if (a[j]<=pp) pp-=a[j], cnt+=k;
			int pos=upper_bound(s+1, s+k, pp)-s-1;
			ans=max(ans, cnt+pos);
		}
		pp=p;
		int cnt=0;
		for (int i=k; i<=n; i+=k) if (a[i]<=pp) pp-=a[i], cnt+=k;
		printf("%d\n", max(ans, cnt));
	}
}


#include <bits/stdc++.h>
#define x first
#define y second

using namespace std;

typedef long long INT;
typedef pair<int,int> pii;

const int NN=200010;
const int inf=0x3f3f3f3f;

int a[NN];
int t,n,p,k,sum,id;

void solve(){
	scanf("%d%d%d",&n,&p,&k);
	for(int i=0;i<n;i++)scanf("%d",a+i);
	sort(a,a+n);

	sum=0; id=0;
	for(int i=0;i<n;i++,id++){
		sum+=a[i];
		if(sum>p){
			sum-=a[i];
			break;
		}
	}
//	cout<<id<<" "<<sum<<endl;

	if(id==0 || id==n){
		printf("%d\n",id);
		return;
	}

	if(sum+a[id]-a[id-1]<=p)id++;
	cout<<id<<endl;
}

main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif
	cin>>t;
	while(t--)solve();

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int a;
    cin>>a;
    for(int x=0; x<a; x++){
        int p,q,r,z;
        vector<int> v;
        cin>>p>>q>>r;
        int arr[p];
        int sum1=0;
        for(int y=0; y<p; y++){
            cin>>arr[y];
            sum1+=arr[y];
        }
        sort(arr,arr+p);
        int sum=0;
        int count=0;
        for(z=0; z<p; z++){
            sum+=arr[z];
            count++;
            if(sum<=q){
                v.push_back(arr[z]);
            }else{
                sum-=arr[z];
                count--;
                break;
            }
    }
    if(sum1<q){
        cout<<p<<endl;
    }else{
        if(sum==q){
            if(arr[z]!=arr[z-1]){
        cout<<count<<endl;
    }else{
        cout<<count+1<<endl;
    }}
    else if(sum<q && sum>=arr[0]){
        if(sum+arr[z]-arr[z-1]<=q){
            cout<<count+1<<endl;
        }else{
            cout<<count<<endl;
        }
    }else if(sum<q && sum<arr[0]){
        cout<<"0"<<endl;
    }
   }
}
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int a;
   cin>>a;
   for(int x=0; x<a; x++){
       int p,q,r,z,sum=0;
       cin>>p>>q>>r;
       int arr[p];
       for(int y=0; y<p; y++){
           cin>>arr[y];
       }
       sort(arr,arr+p);
       int toy=0;
        for(z=0; z<p; z++){
           sum+=arr[z];
           if(sum>q){
                 toy=1;
                 break;

           }
       }
       int to=0;
       if(toy==0){
           cout<<p<<endl;
       }else{
           if(z==0){
               cout<<"0"<<endl;
           }else{
           for(int e=0; e<z; e++){
               if(sum-arr[e]<=q){
                   to=1;
                   break;
               }
           }
           if(to==1){
               cout<<z+1<<endl;
           }else{
               cout<<z<<endl;
           }
       }
       }
   }
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int a;
    cin>>a;
    for(int x=0; x<a; x++){
        int p,q,r,z;
        vector<int> v;
        cin>>p>>q>>r;
        int arr[p];
        int sum1=0;
        for(int y=0; y<p; y++){
            cin>>arr[y];
            sum1+=arr[y];
        }
        sort(arr,arr+p);
        int sum=0;
        int count=0;
        for(z=0; z<p; z++){
            sum+=arr[z];
            count++;
            if(sum<=q){
                v.push_back(arr[z]);
            }else{
                sum-=arr[z];
                count--;
                break;
            }
    }
    if(sum1<q){
        cout<<p<<endl;
    }else{
        if(sum==q){
            if(arr[z+1]!=arr[z]){
        cout<<count<<endl;
    }else{
        cout<<count+1<<endl;
    }}
    else{
        if(sum+arr[z]-arr[z-1]<=q){
            cout<<count+1<<endl;
        }else{
            cout<<count<<endl;
        }
    }
   }}
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int a;
    cin>>a;
    for(int x=0; x<a; x++){
        int p,q,r,z;
        vector<int> v;
        cin>>p>>q>>r;
        int arr[p];
        int sum1=0;
        for(int y=0; y<p; y++){
            cin>>arr[y];
            sum1+=arr[y];
        }
        sort(arr,arr+p);
        int sum=0;
        int count=0;
        for(z=0; z<p; z++){
            sum+=arr[z];
            count++;
            if(sum<=q){
                v.push_back(arr[z]);
            }else{
                sum-=arr[z];
                count--;
                break;
            }
    }
    if(sum1<q){
        cout<<p<<endl;
    }else{
        if(sum==q){
            if(z==p){
                cout<<count<<endl;
            }else{
            if(arr[z]!=arr[z-1]){
        cout<<count<<endl;
    }else{
        cout<<count+1<<endl;
    }}}
    else if(sum<q && sum>=arr[0]){
        if(sum+arr[z]-arr[z-1]<=q){
            cout<<count+1<<endl;
        }else{
            cout<<count<<endl;
        }
    }else if(sum<q && sum<arr[0]){
        cout<<"0"<<endl;
    }
   }
}
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int a;
    cin>>a;
    for(int x=0; x<a; x++){
        int p,q,r,z;
        vector<int> v;
        cin>>p>>q>>r;
        int arr[p];
        int sum1=0;
        for(int y=0; y<p; y++){
            cin>>arr[y];
            sum1+=arr[y];
        }
        sort(arr,arr+p);
        int sum=0;
        int count=0;
        for(z=0; z<p; z++){
            sum+=arr[z];
            count++;
            if(sum<=q){
                v.push_back(arr[z]);
            }else{
                sum-=arr[z];
                count--;
                break;
            }
    }
    if(sum1<q){
        cout<<p<<endl;
    }else{
        if(sum==q){
            if(arr[z+1]!=arr[z]){
        cout<<count<<endl;
    }else{
        cout<<count+1<<endl;
    }}
    else if(sum<q){
        if(sum+arr[z]-arr[z-1]<=q){
            cout<<count+1<<endl;
        }else{
            cout<<count<<endl;
        }
    }else{
        cout<<"0"<<endl;
    }
   }
}
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int a;
    cin>>a;
    for(int x=0; x<a; x++){
        int p,q,r,count=0;
        cin>>p>>q>>r;
        int arr[p];
        int dp[p+1]={};
        for(int y=0; y<p; y++){
            cin>>arr[y];
        }
        sort(arr,arr+p);
        for(int z=1; z<r; z++){
            dp[z]=dp[z-1]+arr[z-1];
        }
        for(int e=r; e<=p; e++){
            dp[e]=dp[e-r]+arr[e-1];
        }
        for(int f=0; f<=p; f++){
            if(dp[f]<=q){
                count=f;        }
    }
    cout<<count<<endl;
}
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int a;
    cin>>a;
    for(int x=0; x<a; x++){
        int p,q,r,count=0;
        cin>>p>>q>>r;
        int arr[p];
        int dp[p+1]={};
        for(int y=0; y<p; y++){
            cin>>arr[y];
        }
        sort(arr,arr+p);
        for(int z=1; z<r; z++){
            dp[z]=dp[z-1]+arr[z-1];
        }
        for(int e=r; e<=p; e++){
            dp[e]=dp[e-r]+arr[e-1];
        }
        for(int f=0; f<p; f++){
            if(arr[f]<=q){
                count=f;        }
    }
    cout<<count<<endl;
}
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int a;
    cin>>a;
    for(int x=0; x<a; x++){
        int p,q,r,z;
        vector<int> v;
        cin>>p>>q>>r;
        int arr[p];
        int sum1=0;
        for(int y=0; y<p; y++){
            cin>>arr[y];
            sum1+=arr[y];
        }
        sort(arr,arr+p);
        int sum=0;
        int count=0;
        for(z=0; z<p; z++){
            sum+=arr[z];
            count++;
            if(sum<=q){
                v.push_back(arr[z]);
            }else{
                sum-=arr[z];
                count--;
                break;
            }
    }
    if(sum1<q){
        cout<<p<<endl;
    }else{
        if(sum==q){
            if(arr[z]!=arr[z-1]){
        cout<<count<<endl;
    }else{
        cout<<count+1<<endl;
    }}
    else if(sum<q){
        if(sum+arr[z]-arr[z-1]<=q){
            cout<<count+1<<endl;
        }else{
            cout<<count<<endl;
        }
    }else{
        cout<<"0"<<endl;
    }
   }
}
}


#include<bits/stdc++.h>
using namespace std;

void solve(){
	int n,p,k,ans=0,temp,cnt=0,x;
	cin >> n >> p >> k;
	int a[n];
	for(int i=0;i<n;i++) cin >> a[i];
	sort(a,a+n);
	int j;
	for(j=0;j<n;j++) {
		if(a[j] > p) break;
	}
	j--;
	//cout << j << endl;
	for(;j>=0;j--){
		temp = p;
		cnt = 0;
		x= j;
		while( x >= 0 ) {
			//cout  << x << " " << temp << endl;
			if(x >= 1 && temp>=a[x]) {
				cnt+=2;
				temp -= a[x];
				x -= 2;
			}
			else if(x >= 0 && temp >= a[x]) {
				cnt++;
				temp -= a[x];
				x--;
			}
			else break;
		}
		//cout <<  j << "  " << cnt << endl;
		if(ans < cnt ) ans = cnt;
		else break;
	}
	cout << ans << endl;
}

int main() {
	int t;
	cin >> t;
	while(t--) solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define SIZE (int) (2e5+7)

ll n;
ll a[SIZE];

ll buy(ll k,ll p, ll i){
	ll x=0,y=0;
	//cout << p << " " << i << endl;
	if(p <= 0) return 0;
	if(i<n-1 && p >= a[i+1]) x = 2 + buy(k,p-a[i+1],i+2);
	if(i<n && p >= a[i]) y = 1 + buy(k,p-a[i],i+1);
	return max(x,y);
}

int main() {
	int t;
	cin >> t;
	while(t--) {
		ll k,p;
		cin >> n >> p >> k;
		for(int i=0;i<n;i++) cin >> a[i];
		sort(a,a+n);
		ll ans = buy(k,p,0);
		cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

void solve() {
	int n,p,k,x,cnt=0,ans=0,temp;
	cin >> n >> p >> k;
	int a[n];
	for(int i=0;i<n;i++) cin >> a[i];
	sort(a,a+n);
	int j=0;
	for(;j<n;j++) {
		if(a[j] > p) break;
	}
	j--;
	temp = p;
	cnt = 0;
	x= j;
	while( x >= 0 ) {
		//cout  << x << " " << temp << endl;
		if(x >= 1 && temp>=a[x]) {
			cnt+=2;
			temp -= a[x];
			x -= 2;
		}
		else if(x >= 0 && temp >= a[x]) {
			cnt++;
			temp -= a[x];
			x--;
		}
		else break;
	}
	ans = max(ans,cnt);
	temp = p;
	cnt = 0;
	x= j-1;
	while( x >= 0 ) {
		//cout  << x << " " << temp << endl;
		if(x >= 1 && temp>=a[x]) {
			cnt+=2;
			temp -= a[x];
			x -= 2;
		}
		else if(x >= 0 && temp >= a[x]) {
			cnt++;
			temp -= a[x];
			x--;
		}
		else break;
	}
	ans = max(cnt,ans);
	cout << ans << endl;
}

int main() {
	int t;
	cin >> t;
	while(t--) solve();
	return 0;
}