#include <bits/stdc++.h>
using namespace std;
const int maxn = 55;
int level_need[30];
char res[maxn];
int b[maxn];
int nums[30];
char ch[maxn];
int thisDec[maxn], ID_thisDec;
int levelTochar[30];
int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    getchar();
    fgets(ch, maxn, stdin);
    int len = strlen(ch);
    ch[len - 1] = 0, len--;
    ID_thisDec = 0;
    memset(level_need, 0, sizeof(level_need));
    memset(res, 0, sizeof(res));
    memset(nums, 0, sizeof(nums));
    for (int i = 0; i < len; i++) nums[ch[i] - 'a']++;
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", b + i);
    bool needCon = 1;
    int level = 0;
    while (needCon) {
      level++;
      bool thisCon = 0;
      for (int i = 1; i <= n; i++) {
        if (res[i]) continue;
        if (b[i] == 0) {
          res[i] = level;
          level_need[level]++;
          thisDec[++ID_thisDec] = i;
        } else
          thisCon = 1;
      }
      if (!thisCon) needCon = 0;
      for (int i = 1; i <= n; i++) {
        if (b[i] == 0) continue;
        for (int j = 1; j <= ID_thisDec; j++) b[i] -= abs(i - thisDec[j]);
      }
      ID_thisDec = 0;
    }
    for (int i = 25; i >= 0; i--) {
      bool thisCan = 1;
      for (int ii = i, j = 1; j <= level && ii >= 0;) {
        if (nums[ii] < level_need[j])
          ii--;
        else
          levelTochar[j] = ii, j++, ii--;
        if (j != level + 1 && ii == -1) thisCan = 0;
      }
      if (thisCan) {
        for (int ii = 1; ii <= n; ii++) {
          res[ii] = levelTochar[res[ii]];
        }
        for (int i = 1; i <= n; i++) printf("%c", res[i] + 'a');
        printf("\n");
        break;
      }
    }
  }
}
