#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int N = 10000 + 5;
int tc, n, m, cnt[N];
struct R {
int x, cnt;
bool operator<(const R &r) const {
if (cnt == r.cnt) {
return x > r.x;
}
return cnt < r.cnt;
}
};
void calculateDivisorCount() {
for (int i = 1; i < N; ++i) {
for (int j = i; j < N; j += i) {
cnt[j]++;
}
}
}
int main() {
calculateDivisorCount();
cin >> tc;
while (tc--) {
vector<R> vp;
cin >> n;
while (n--) {
int x;
cin >> x;
vp.push_back({x, cnt[x]});
}
sort(vp.begin(), vp.end());
int k;
cin >> k;
cout << vp[k - 1].x << endl;
}
return 0;
}