#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[300005];
char s[300005], b[300005];

bool check(int val) {
    int dem = 0;
    char last = 'R';
    for (int i = 1; i <= n; i++) {
        if (a[i] > val) {
            if (s[i] == 'B' && last != 'B') {
                dem++;
            }
            last = s[i];
        }
    }
    return (dem <= k);
}

main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int test;
    cin >> test;

    while (test--) {
        cin >> n >> k;
        for (int i = 1; i <= n; i++) cin >> s[i];
        for (int i = 1; i <= n; i++) cin >> a[i];

        int l = -1;
        int r = 1e9 + 1;
        while (l < r - 1) {
            int mid = (l + r) / 2;
            if (check(mid)) r = mid;
            else l = mid;
        }

        cout << r << '\n';

    }

    return 0;
}
