// https://www.geeksforgeeks.org/kth-smallestlargest-element-unsorted-array/
#include <iostream>
using namespace std;
void minHeap (int a[], int i, int n) {
int l=2*i+1, r = 2*i+2, smallest = i;
if (l<n) {
if (a[l] < a[smallest])
smallest = l;
if (r<n && a[r] < a[smallest])
smallest = r;
if (smallest!= i) {
swap(a[i], a[smallest]);
minHeap(a, smallest, n);
}
}
}
void extractMin (int a[], int n, int k) {
int s;
for (int i=0; i<k;i++) {
s= a[0];
a[0]= a[n-1-i];
minHeap(a, 0, n-i);
}
cout<< s<< "\n";
}
int main() {
int t;
cin>>t;
while (t-->0) {
int n,k;
cin>>n;
int a[n];
for (int i=0;i<n;i++)
cin>>a[i];
cin>>k;
for (int i=n/2-1; i>=0; i--)
minHeap(a, i, n);
extractMin (a, n, k);
}
}