#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 *, int *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ scanf(char *, ...);
int strlen(char *);
int main() {
  int t, len, i, j, min, sum, N;
  char str[1000];
  int B[1000];
  scanf("%d", &t);
  while (t--) {
    min = 1000000;
    scanf("%s", str);
    scanf("%d", &N);
    for (i = 0; i < N; i++)
      scanf("%d", &B[i]);
    len = strlen(str);
    if (len < 12)
      printf("%s\n", permute(str, 0, len - 1, N, B, &min));
    else
      printf("%s\n", str);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x13b0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea7(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
je     124e <main+0x10e>
movl   $0xf4240,-0x18(%rbp)
lea    -0x410(%rbp),%rsi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1060 <scanf@plt>
lea    0xe65(%rip),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11e7 <main+0xa7>
movslq -0x10(%rbp),%rax
lea    -0x13b0(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1060 <scanf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11ad <main+0x6d>
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xc,-0xc(%rbp)
jge    1236 <main+0xf6>
lea    -0x410(%rbp),%rdi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
mov    -0x20(%rbp),%ecx
lea    -0x13b0(%rbp),%r8
xor    %esi,%esi
lea    -0x18(%rbp),%r9
call   1030 <permute@plt>
mov    %rax,%rsi
lea    0xdda(%rip),%rdi
call   1050 <printf@plt>
jmp    1249 <main+0x109>
lea    -0x410(%rbp),%rsi
lea    0xdc2(%rip),%rdi
call   1050 <printf@plt>
jmp    1164 <main+0x24>
xor    %eax,%eax
add    $0x13b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x13a8,%rsp
lea    0xea8(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
subl   $0x1,0x8(%rsp)
jae    117f <main+0x3f>
xor    %eax,%eax
add    $0x13a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%r15
lea    0xe75(%rip),%rbx
lea    0x400(%rsp),%r14
lea    0xe6c(%rip),%rbp
jmp    11b2 <main+0x72>
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
subl   $0x1,0x8(%rsp)
jb     116b <main+0x2b>
movl   $0xf4240,0xc(%rsp)
lea    0xe42(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
mov    %rbx,%rdi
lea    0x4(%rsp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
cmpl   $0x0,0x4(%rsp)
jle    120f <main+0xcf>
mov    %r14,%r13
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
add    $0x1,%r12
movslq 0x4(%rsp),%rax
add    $0x4,%r13
cmp    %rax,%r12
jl     11f0 <main+0xb0>
mov    %r15,%rdi
call   1040 <strlen@plt>
cmp    $0xb,%eax
jg     11a0 <main+0x60>
add    $0xffffffff,%eax
mov    0x4(%rsp),%ecx
mov    %r15,%rdi
xor    %esi,%esi
mov    %eax,%edx
mov    %r14,%r8
lea    0xc(%rsp),%r9
call   1030 <permute@plt>
mov    %rbp,%rdi
mov    %rax,%rsi
jmp    11a6 <main+0x66>

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 i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1000 x i8]
%10 = alloca [1000 x i32]
store i32 0, i32* %1
%11 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
br label %12
12: ; preds = %48, %0
%13 = load i32, i32* %2
%14 = add nsw i32 %13, -1
store i32 %14, i32* %2
%15 = icmp ne i32 %13, 0
br i1 %15, label %16, label %49
16: ; preds = %12
store i32 1000000, i32* %6
%17 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%18 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %8)
store i32 0, i32* %4
br label %20
20: ; preds = %29, %16
%21 = load i32, i32* %4
%22 = load i32, i32* %8
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %32
24: ; preds = %20
%25 = load i32, i32* %4
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [1000 x i32], [1000 x i32]* %10, i64 0, i64 %26
%28 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %27)
br label %29
29: ; preds = %24
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1
store i32 %31, i32* %4
br label %20
32: ; preds = %20
%33 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%34 = call i32 @strlen(i8* %33)
store i32 %34, i32* %3
%35 = load i32, i32* %3
%36 = icmp slt i32 %35, 12
br i1 %36, label %37, label %45
37: ; preds = %32
%38 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%39 = load i32, i32* %3
%40 = sub nsw i32 %39, 1
%41 = load i32, i32* %8
%42 = getelementptr inbounds [1000 x i32], [1000 x i32]* %10, i64 0, i64 0
%43 = call i8* @permute(i8* %38, i32 0, i32 %40, i32 %41, i32* %42, i32* %6)
%44 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %43)
br label %48
45: ; preds = %32
%46 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%47 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %45, %37
br label %12
49: ; preds = %12
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 i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1000 x i8]
%5 = alloca [1000 x i32]
%6 = bitcast i32* %1 to i8*
%7 = bitcast i32* %2 to i8*
%8 = bitcast i32* %3 to i8*
%9 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%10 = bitcast [1000 x i32]* %5 to i8*
%11 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%12 = load i32, i32* %1
%13 = add nsw i32 %12, -1
store i32 %13, i32* %1
%14 = icmp eq i32 %12, 0
br i1 %14, label %44, label %15
15: ; preds = %0
%16 = getelementptr inbounds [1000 x i32], [1000 x i32]* %5, i64 0, i64 0
br label %17
17: ; preds = %15, %40
store i32 1000000, i32* %2
%18 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%19 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%20 = load i32, i32* %3
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %17, %22
%23 = phi i64 [ %26, %22 ], [ 0, %17 ]
%24 = getelementptr inbounds [1000 x i32], [1000 x i32]* %5, i64 0, i64 %23
%25 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %24)
%26 = add nuw nsw i64 %23, 1
%27 = load i32, i32* %3
%28 = sext i32 %27 to i64
%29 = icmp slt i64 %26, %28
br i1 %29, label %22, label %30
30: ; preds = %22, %17
%31 = call i32 @strlen(i8* %9)
%32 = icmp slt i32 %31, 12
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = add nsw i32 %31, -1
%35 = load i32, i32* %3
%36 = call i8* @permute(i8* %9, i32 0, i32 %34, i32 %35, i32* %16, i32* %2)
%37 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %36)
br label %40
38: ; preds = %30
%39 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %9)
br label %40
40: ; preds = %38, %33
%41 = load i32, i32* %1
%42 = add nsw i32 %41, -1
store i32 %42, i32* %1
%43 = icmp eq i32 %41, 0
br i1 %43, label %44, label %17
44: ; preds = %40, %0
ret i32 0
}