Polycarp wrote on the board a string containing only lowercase Latin letters (‘a’-‘z’). This string is known for you and given in the input.
After that, he erased some letters from the string , and he rewrote the remaining letters in any order. As a result, he got some new string . You have to find it with some additional information.
Suppose that the string has length and the characters are numbered from left to right from to . You are given a sequence of m integers: ,,,, where bi is the sum of the distances from the index to all such indices that (consider that ‘a’<‘b’<…<‘z’). In other words, to calculate , Polycarp finds all such indices j that the index j contains a letter that is later in the alphabet than and sums all the values .
For example, if “abzb”, then:
since ‘a’, all other indices contain letters which are later in the alphabet, that is: ;
since ‘b’, only the index $j=$3 contains the letter, which is later in the alphabet, that is: ;
since ‘z’, then there are no indexes such that , thus ;
since ‘b’, only the index contains the letter, which is later in the alphabet, that is: .
Thus, if “abzb”, then .
Given the string and the array , find any possible string for which the following two requirements are fulfilled simultaneously:
is obtained from by erasing some letters (possibly zero) and then writing the rest in any order;
the array, constructed from the string according to the rules above, equals to the array specified in the input data.
Input
The first line contains an integer — the number of test cases in the test. Then test cases follow.
Each test case consists of three lines:
the first line contains string , which has a length from to and consists of lowercase English letters;
the second line contains positive integer , where is the length of the string , and is the length of the array ;
the third line contains the integers .
It is guaranteed that in each test case an answer exists.
Output
Output lines: the -th of them should contain the answer (string ) to the -th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any.
Example
input
4
abac
3
2 1 0
abc
1
0
abba
3
1 0 1
ecoosdcefr
10
38 13 24 14 11 5 3 24 17 0
output
aac
b
aba
codeforces
Note
In the first test case, such strings are suitable: "aac’, “aab”.
In the second test case, such trings are suitable: “a”, “b”, “c”.
In the third test case, only the string equals to “aba” is suitable, but the character ‘b’ can be from the second or third position.
拓撲排序。。
#include<bits/stdc++.h>
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;
inline int qr() {
int f = 0, fu = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')fu = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + c - 48;
c = getchar();
}
return f * fu;
}
const int N = 55;
char a[N];
int b[N], T, n, m, cnt[30], ans[N];
vi tmp1, tmp2;
vi s;
int main() {
T = qr();
while (T--) {
ss(a + 1), n = strlen(a + 1);
ms(cnt);
repi(i, 1, n)cnt[a[i] - 'a' + 1]++;
s.clear();
repi(i, 1, 26)if (cnt[i])s.pb(i);
m = qr();
repi(i, 1, m)b[i] = qr();
tmp1.clear();
repi(i, 1, m)if (!b[i])tmp1.pb(i);
int now = s.size() - 1;
while (!tmp1.empty()) {
while (cnt[s[now]] < tmp1.size())now--;
for (auto it:tmp1)ans[it] = s[now];
now--;
tmp2.clear();
for (auto it:tmp1)
repi(i, 1, m)if (b[i]) {
b[i] -= abs(it - i);
if (!b[i])tmp2.pb(i);
}
swap(tmp1, tmp2);
}
repi(i, 1, m)pc(ans[i] - 1 + 'a');
puts("");
}
return 0;
}