#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
char *permute(char *, int /*<<< orphan*/, int, int *, int, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ scanf(char *, ...);
int strlen(char *);
int main() {
  char str[1000];
  char minstr[1000];
  int t, B[1000], len, i, N;
  scanf("%d", &t);
  while (t--) {
    scanf("%s", &str);
    scanf("%d", &N);
    len = strlen(str);
    for (i = 0; i < N; i++)
      scanf("%d", &B[i]);
    if (len < 20)
      printf("%s\n", permute(str, 0, len, B, N, minstr));
    else
      printf("%s\n", str);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x17a0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea7(%rip),%rdi
lea    -0x7e4(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
mov    -0x7e4(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x7e4(%rbp)
cmp    $0x0,%eax
je     1272 <main+0x132>
lea    0xe7b(%rip),%rdi
lea    -0x3f0(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
lea    0xe63(%rip),%rdi
lea    -0x179c(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
lea    -0x3f0(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1794(%rbp)
movl   $0x0,-0x1798(%rbp)
mov    -0x1798(%rbp),%eax
cmp    -0x179c(%rbp),%eax
jge    1210 <main+0xd0>
movslq -0x1798(%rbp),%rax
lea    -0x1790(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1060 <scanf@plt>
mov    -0x1798(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1798(%rbp)
jmp    11c7 <main+0x87>
cmpl   $0x14,-0x1794(%rbp)
jge    125a <main+0x11a>
lea    -0x3f0(%rbp),%rdi
mov    -0x1794(%rbp),%edx
lea    -0x1790(%rbp),%rcx
mov    -0x179c(%rbp),%r8d
lea    -0x7e0(%rbp),%r9
xor    %esi,%esi
call   1030 <permute@plt>
mov    %rax,%rsi
lea    0xdb6(%rip),%rdi
call   1050 <printf@plt>
jmp    126d <main+0x12d>
lea    -0x3f0(%rbp),%rsi
lea    0xd9e(%rip),%rdi
call   1050 <printf@plt>
jmp    1167 <main+0x27>
xor    %eax,%eax
add    $0x17a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1798,%rsp
lea    0xea8(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
subl   $0x1,0xc(%rsp)
jae    117f <main+0x3f>
xor    %eax,%eax
add    $0x1798,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x400(%rsp),%r15
lea    0xe72(%rip),%rbx
lea    0x7f0(%rsp),%r12
jmp    11b6 <main+0x76>
nopl   0x0(%rax,%rax,1)
lea    0xe5f(%rip),%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
subl   $0x1,0xc(%rsp)
jb     116b <main+0x2b>
lea    0xe46(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
mov    %rbx,%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    0x8(%rsp),%r8d
test   %r8d,%r8d
jle    120f <main+0xcf>
mov    %r12,%r13
xor    %ebp,%ebp
mov    %rbx,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
add    $0x1,%rbp
movslq 0x8(%rsp),%r8
add    $0x4,%r13
cmp    %r8,%rbp
jl     11f0 <main+0xb0>
cmp    $0x13,%r14d
jg     11a0 <main+0x60>
mov    %r15,%rdi
xor    %esi,%esi
mov    %r14d,%edx
mov    %r12,%rcx
lea    0x10(%rsp),%r9
call   1030 <permute@plt>
lea    0xdd5(%rip),%rdi
mov    %rax,%rsi
jmp    11aa <main+0x6a>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1000 x i8]
%3 = alloca [1000 x i8]
%4 = alloca i32
%5 = alloca [1000 x i32]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
br label %10
10: ; preds = %45, %0
%11 = load i32, i32* %4
%12 = add nsw i32 %11, -1
store i32 %12, i32* %4
%13 = icmp ne i32 %11, 0
br i1 %13, label %14, label %46
14: ; preds = %10
%15 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), [1000 x i8]* %2)
%16 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %8)
%17 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %6
store i32 0, i32* %7
br label %19
19: ; preds = %28, %14
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %31
23: ; preds = %19
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [1000 x i32], [1000 x i32]* %5, i64 0, i64 %25
%27 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %26)
br label %28
28: ; preds = %23
%29 = load i32, i32* %7
%30 = add nsw i32 %29, 1
store i32 %30, i32* %7
br label %19
31: ; preds = %19
%32 = load i32, i32* %6
%33 = icmp slt i32 %32, 20
br i1 %33, label %34, label %42
34: ; preds = %31
%35 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%36 = load i32, i32* %6
%37 = getelementptr inbounds [1000 x i32], [1000 x i32]* %5, i64 0, i64 0
%38 = load i32, i32* %8
%39 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%40 = call i8* @permute(i8* %35, i32 0, i32 %36, i32* %37, i32 %38, i8* %39)
%41 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %40)
br label %45
42: ; preds = %31
%43 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%44 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %42, %34
br label %10
46: ; preds = %10
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [1000 x i8]
%2 = alloca [1000 x i8]
%3 = alloca i32
%4 = alloca [1000 x i32]
%5 = alloca i32
%6 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 0
%7 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%8 = bitcast i32* %3 to i8*
%9 = bitcast [1000 x i32]* %4 to i8*
%10 = bitcast i32* %5 to i8*
%11 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%12 = load i32, i32* %3
%13 = add nsw i32 %12, -1
store i32 %13, i32* %3
%14 = icmp eq i32 %12, 0
br i1 %14, label %43, label %15
15: ; preds = %0
%16 = getelementptr inbounds [1000 x i32], [1000 x i32]* %4, i64 0, i64 0
br label %17
17: ; preds = %15, %39
%18 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), [1000 x i8]* %1)
%19 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %5)
%20 = call i32 @strlen(i8* %6)
%21 = load i32, i32* %5
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %17, %23
%24 = phi i64 [ %27, %23 ], [ 0, %17 ]
%25 = getelementptr inbounds [1000 x i32], [1000 x i32]* %4, i64 0, i64 %24
%26 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %25)
%27 = add nuw nsw i64 %24, 1
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = icmp slt i64 %27, %29
br i1 %30, label %23, label %31
31: ; preds = %23, %17
%32 = phi i32 [ %21, %17 ], [ %28, %23 ]
%33 = icmp slt i32 %20, 20
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = call i8* @permute(i8* %6, i32 0, i32 %20, i32* %16, i32 %32, i8* %7)
%36 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %35)
br label %39
37: ; preds = %31
%38 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %6)
br label %39
39: ; preds = %37, %34
%40 = load i32, i32* %3
%41 = add nsw i32 %40, -1
store i32 %41, i32* %3
%42 = icmp eq i32 %40, 0
br i1 %42, label %43, label %17
43: ; preds = %39, %0
ret i32 0
}