#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int arr[28];
vector<int> input;
void rotate_square(int line_num, int count) {
if (count != 0) {
int tmp1 = arr[0];
int tmp2;
for (int i = 0; i < (line_num * 4) - 1; i++) {
tmp2 = arr[i + 1];
arr[i + 1] = tmp1;
tmp1 = tmp2;
}
arr[0] = tmp2;
}
for (int i = 0; i < 4; i++) {
int number = 0;
int mul = 1;
for (int j = line_num - 1; j >= 0; j--) {
number += arr[line_num * i + j] * mul;
mul *= 16;
}
input.push_back(number);
}
}
int main() {
int T;
cin >> T;
for (int t = 1; t <= T; t++) {
int N, K;
string nums;
cin >> N >> K;
cin.ignore();
getline(cin, nums);
for (int n = 0; n < N; n++) {
if (nums[n] >= '0'&& nums[n] <= '9')
arr[n] = nums[n] - '0';
else if (nums[n] >= 'A' && nums[n] <= 'F')
arr[n] = nums[n] - 'A' + 0xa;
}
for (int i = 0; i < N/4; i++) {
rotate_square(N/4, i);
}
sort(input.begin(), input.end(), greater<int>());
input.erase(unique(input.begin(), input.end()), input.end());
cout << "#" << t << " " << input[K - 1] << endl;
input.clear();
}
return 0;
}