#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 */
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ quickSort(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ scanf(char *, ...);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int main() {
  int t, n, p, q, i, resl, j;
  char arr[1001];
  char res[10001];
  scanf("%d", &t);
  while (t--) {
    scanf("%s", arr);
    scanf("%d %d", &p, &q);
    for (i = 0; i < p; i++)
      strcat(res, arr);
    resl = p * strlen(arr);
    resl--;
    quickSort(res, 0, resl);
    for (i = 0; i < q; i++) {
      scanf("%d", &n);
      if (n - 1 > resl)
        printf("\n-1");
      else
        printf("\n%c", res[n - 1]);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2b40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9d(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
je     12a1 <main+0x151>
lea    -0x410(%rbp),%rsi
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1050 <scanf@plt>
lea    0xe5f(%rip),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
mov    $0x0,%al
call   1050 <scanf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11e7 <main+0x97>
lea    -0x2b30(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11ba <main+0x6a>
mov    -0x10(%rbp),%eax
mov    %eax,-0x2b34(%rbp)
lea    -0x410(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x2b34(%rbp),%eax
imul   %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
lea    -0x2b30(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1060 <quickSort@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    129c <main+0x14c>
lea    0xdc8(%rip),%rdi
lea    -0xc(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jle    126b <main+0x11b>
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1289 <main+0x139>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x2b30(%rbp,%rax,1),%esi
ff
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    128e <main+0x13e>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    122b <main+0xdb>
jmp    1174 <main+0x24>
xor    %eax,%eax
add    $0x2b40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2b18,%rsp
lea    0xe9e(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
subl   $0x1,0x8(%rsp)
jae    118f <main+0x3f>
xor    %eax,%eax
add    $0x2b18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%r14
lea    0x400(%rsp),%r15
lea    0xe63(%rip),%rbx
jmp    11b7 <main+0x67>
cs nopw 0x0(%rax,%rax,1)
nop
subl   $0x1,0x8(%rsp)
jb     117b <main+0x2b>
mov    %rbx,%r13
lea    0xe3f(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
lea    0xe31(%rip),%rdi
lea    0x4(%rsp),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1050 <scanf@plt>
mov    0x4(%rsp),%ebx
test   %ebx,%ebx
jle    1206 <main+0xb6>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   1070 <strcat@plt>
add    $0x1,%ebp
mov    0x4(%rsp),%ebx
cmp    %ebx,%ebp
jl     11f0 <main+0xa0>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%r12d
imul   %ebx,%r12d
lea    -0x1(%r12),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1060 <quickSort@plt>
cmpl   $0x0,(%rsp)
mov    %r13,%rbx
lea    0xc(%rsp),%rbp
jle    11b0 <main+0x60>
xor    %r13d,%r13d
jmp    1262 <main+0x112>
nopl   0x0(%rax,%rax,1)
movsbl -0x1(%rax,%r15,1),%esi
lea    0xdc0(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%r13d
cmp    (%rsp),%r13d
jge    11b0 <main+0x60>
mov    %rbx,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
movslq 0xc(%rsp),%rax
cmp    %r12d,%eax
jle    1240 <main+0xf0>
lea    0xd89(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1254 <main+0x104>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [6 x i8] c"%d %d\00"
@.str.3 = [4 x i8] c"\0A-1\00"
@.str.4 = [4 x i8] c"\0A%c\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 [1001 x i8]
%10 = alloca [10001 x i8]
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 = %65, %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 %66
16: ; preds = %12
%17 = getelementptr inbounds [1001 x i8], [1001 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 ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %4, i32* %5)
store i32 0, i32* %6
br label %20
20: ; preds = %28, %16
%21 = load i32, i32* %6
%22 = load i32, i32* %4
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %31
24: ; preds = %20
%25 = getelementptr inbounds [10001 x i8], [10001 x i8]* %10, i64 0, i64 0
%26 = getelementptr inbounds [1001 x i8], [1001 x i8]* %9, i64 0, i64 0
%27 = call i32 @strcat(i8* %25, i8* %26)
br label %28
28: ; preds = %24
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 1
store i32 %30, i32* %6
br label %20
31: ; preds = %20
%32 = load i32, i32* %4
%33 = getelementptr inbounds [1001 x i8], [1001 x i8]* %9, i64 0, i64 0
%34 = call i32 @strlen(i8* %33)
%35 = mul nsw i32 %32, %34
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = add nsw i32 %36, -1
store i32 %37, i32* %7
%38 = getelementptr inbounds [10001 x i8], [10001 x i8]* %10, i64 0, i64 0
%39 = load i32, i32* %7
%40 = call i32 @quickSort(i8* %38, i32 0, i32 %39)
store i32 0, i32* %6
br label %41
41: ; preds = %62, %31
%42 = load i32, i32* %6
%43 = load i32, i32* %5
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %65
45: ; preds = %41
%46 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%47 = load i32, i32* %3
%48 = sub nsw i32 %47, 1
%49 = load i32, i32* %7
%50 = icmp sgt i32 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %45
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %61
53: ; preds = %45
%54 = load i32, i32* %3
%55 = sub nsw i32 %54, 1
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [10001 x i8], [10001 x i8]* %10, i64 0, i64 %56
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %59)
br label %61
61: ; preds = %53, %51
br label %62
62: ; preds = %61
%63 = load i32, i32* %6
%64 = add nsw i32 %63, 1
store i32 %64, i32* %6
br label %41
65: ; preds = %41
br label %12
66: ; 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 = [6 x i8] c"%d %d\00"
@.str.3 = [4 x i8] c"\0A-1\00"
@.str.4 = [4 x i8] c"\0A%c\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1001 x i8]
%6 = alloca [10001 x i8]
%7 = bitcast i32* %1 to i8*
%8 = bitcast i32* %2 to i8*
%9 = bitcast i32* %3 to i8*
%10 = bitcast i32* %4 to i8*
%11 = getelementptr inbounds [1001 x i8], [1001 x i8]* %5, i64 0, i64 0
%12 = getelementptr inbounds [10001 x i8], [10001 x i8]* %6, i64 0, i64 0
%13 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%14 = load i32, i32* %1
%15 = add nsw i32 %14, -1
store i32 %15, i32* %1
%16 = icmp eq i32 %14, 0
br i1 %16, label %58, label %21
17: ; preds = %54, %32
%18 = load i32, i32* %1
%19 = add nsw i32 %18, -1
store i32 %19, i32* %1
%20 = icmp eq i32 %18, 0
br i1 %20, label %58, label %21
21: ; preds = %0, %17
%22 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%23 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %3, i32* %4)
%24 = load i32, i32* %3
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %21, %26
%27 = phi i32 [ %29, %26 ], [ 0, %21 ]
%28 = call i32 @strcat(i8* %12, i8* %11)
%29 = add nuw nsw i32 %27, 1
%30 = load i32, i32* %3
%31 = icmp slt i32 %29, %30
br i1 %31, label %26, label %32
32: ; preds = %26, %21
%33 = phi i32 [ %24, %21 ], [ %30, %26 ]
%34 = call i32 @strlen(i8* %11)
%35 = mul nsw i32 %34, %33
%36 = add nsw i32 %35, -1
%37 = call i32 @quickSort(i8* %12, i32 0, i32 %36)
%38 = load i32, i32* %4
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %17
40: ; preds = %32, %54
%41 = phi i32 [ %55, %54 ], [ 0, %32 ]
%42 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%43 = load i32, i32* %2
%44 = icmp sgt i32 %43, %35
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %54
47: ; preds = %40
%48 = add nsw i32 %43, -1
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [10001 x i8], [10001 x i8]* %6, i64 0, i64 %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %45, %47
%55 = add nuw nsw i32 %41, 1
%56 = load i32, i32* %4
%57 = icmp slt i32 %55, %56
br i1 %57, label %40, label %17
58: ; preds = %17, %0
ret i32 0
}