#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
for (int tc = 1; tc <= tt; tc++) {
int n;
cin >> n;
vector<int> a(n), b(n), c(n);
for (int i = 0; i < n; i++) {
c[i] = i;
cin >> a[i];
for (int j = 1; j * j <= a[i]; j++) {
if (a[i] % j == 0) {
b[i] += 2 - (j * j == a[i]);
}
}
}
sort(c.begin(), c.end(), [&](int i, int j) {
return b[i] ^ b[j] ? b[i] < b[j] : a[i] > a[j];
});
int k;
cin >> k;
cout << a[c[k - 1]] << '\n';
}
return 0;
}