#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main(void)
{
int test_case;
int T;
// setbuf(stdout, NULL);
scanf("%d", &T);
/*
여러 개의 테스트 케이스를 처리하기 위한 부분입니다.
*/
for (test_case = 1; test_case <= T; ++test_case)
{
int n, m;
cin >> n >> m;
int length = n / 4;
//대입
char* arr = new char[n];
char charstr[28];
scanf("%s", charstr);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < length; j++) {
arr[i*length + j] = charstr[i*length + j];
}
}
string str;
vector<string> vec;
for (int k = 0; k < length; k++) {
for (int i = 0; i < 4; i++) {
str = "";
for (int j = 0; j < length; j++) {
str.push_back(arr[i*length + j]);
}
// cout << str << "\n";
vec.push_back(str);
}
// printf("//////////\n");
char tmp = arr[n-1];
for (int i = n - 1; i > 0; i--) {
arr[i] = arr[i - 1];
// printf("%c ", *(arr+7));
}
arr[0] = tmp;
}
sort(vec.begin(), vec.end(), greater<string>());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
// for (auto a : vec) {
// cout << a << " ";
// }
// cout << "\n";
// printf("answer string ");
// cout << vec[m - 1] << "\n";
int ans= 0;
for (int i = 0; i < vec[m - 1].size(); i++) {
if (vec[m - 1][i] >= 'A' && vec[m - 1][i] <= 'F') {
ans = ans * 16 + vec[m - 1][i] - 'A' + 10;
}
else {
ans = ans * 16 + vec[m - 1][i] - '0';
}
}
printf("#%d %d\n", test_case, ans);
}
return 0;
}