#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    char kata[100];
    int m;
    int arr[100];
    int memo[100];
    bool placed[100];
    memset(placed, false, sizeof(placed));
    scanf("%s", &kata);
    scanf("%d", &m);
    int panjangkata = strlen(kata);
    for (int f = 0; f < panjangkata; f++) scanf("%d", &arr[f]);
    sort(kata, kata + panjangkata);
    vector<pair<char, int>> jumlah;
    jumlah.push_back(make_pair(kata[0], 1));
    int idjumlah = 0;
    for (int f = 1; f < panjangkata; f++) {
      if (kata[f] == kata[f - 1])
        jumlah[idjumlah].second++;
      else {
        jumlah.push_back(make_pair(kata[f], 1));
        idjumlah++;
      }
    }
    int banyak = jumlah.size();
    int idbanyak = banyak - 1;
    int index = m - 1;
    while (index >= 0) {
      vector<int> nol;
      for (int f = 0; f < m; f++) {
        if (arr[f] == 0 && placed[f] == false) nol.push_back(f);
      }
      int jumlahnol = nol.size();
      while (idbanyak >= 0 && jumlahnol > jumlah[idbanyak].second) idbanyak--;
      char hurufitu = jumlah[idbanyak].first;
      for (int f = 0; f < jumlahnol; f++) {
        memo[nol[f]] = hurufitu;
        placed[nol[f]] = true;
        for (int g = 0; g < nol[f]; g++) {
          if (arr[g] != 0) arr[g] -= (nol[f] - g);
        }
        for (int g = nol[f] + 1; g < m; g++) {
          if (arr[g] != 0) arr[g] -= (g - nol[f]);
        }
        arr[nol[f]]--;
      }
      index -= jumlahnol;
      idbanyak--;
      if (jumlahnol == 0) break;
    }
    for (int f = 0; f < m; f++) printf("%c", memo[f]);
    printf("\n");
  }
}
