#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int count(int x) {
int ret = 0;
for (int i = 1; i <= x; i++) {
if (x % i == 0) {
ret++;
}
}
return ret;
}
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n;
vector<pair<int, int>> nums;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
nums.push_back(make_pair(x, count(x)));
}
sort(nums.begin(), nums.end(), [](const pair<int, int>& a, const pair<int, int>& b) -> bool {
if (a.second == b.second) {
return a.first > b.first;
}
return a.second < b.second;
});
cin >> k;
cout << nums[k - 1].first << "\n";
}
}