#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 getMaxAndBitwise(int *, int, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *, ...);
int main(void) {
  int T, N, Q, arr[100000], memoArr[230], i, q, queryInput;
  scanf("%d", &T);
  while (T) {
    scanf("%d %d", &N, &Q);
    for (i = 0; i < N; i++)
      scanf("%d", &arr[i]);
    for (i = 0; i < 230; i++)
      memoArr[i] = -1;
    for (q = 0; q < Q; q++) {
      scanf("%d", &queryInput);
      if (memoArr[queryInput] < 0) {
        memoArr[queryInput] = getMaxAndBitwise(arr, N, queryInput);
      }
      printf("%d\n", memoArr[queryInput]);
    }
    T--;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x61e40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
cmpl   $0x0,-0x8(%rbp)
je     12a8 <main+0x178>
lea    0xe9b(%rip),%rdi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
mov    $0x0,%al
call   1050 <scanf@plt>
movl   $0x0,-0x61e34(%rbp)
mov    -0x61e34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11c4 <main+0x94>
movslq -0x61e34(%rbp),%rax
lea    -0x61a90(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x61e34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x61e34(%rbp)
jmp    117e <main+0x4e>
movl   $0x0,-0x61e34(%rbp)
cmpl   $0xe6,-0x61e34(%rbp)
jge    1204 <main+0xd4>
movslq -0x61e34(%rbp),%rax
movl   $0xffffffff,-0x61e30(%rbp,%rax,4)
ff ff ff ff
mov    -0x61e34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x61e34(%rbp)
jmp    11ce <main+0x9e>
movl   $0x0,-0x61e38(%rbp)
mov    -0x61e38(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    129a <main+0x16a>
lea    0xddf(%rip),%rdi
lea    -0x61e3c(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
movslq -0x61e3c(%rbp),%rax
cmpl   $0x0,-0x61e30(%rbp,%rax,4)
jge    126c <main+0x13c>
lea    -0x61a90(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x61e3c(%rbp),%edx
call   1040 <getMaxAndBitwise@plt>
mov    %eax,%ecx
movslq -0x61e3c(%rbp),%rax
mov    %ecx,-0x61e30(%rbp,%rax,4)
movslq -0x61e3c(%rbp),%rax
mov    -0x61e30(%rbp,%rax,4),%esi
lea    0xd85(%rip),%rdi
call   1030 <printf@plt>
mov    -0x61e38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x61e38(%rbp)
jmp    120e <main+0xde>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    1154 <main+0x24>
xor    %eax,%eax
add    $0x61e40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x61e38,%rsp
lea    0xeab(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
cmpl   $0x0,0xc(%rsp)
je     1247 <main+0x107>
lea    0x3b0(%rsp),%r12
lea    0xe85(%rip),%rbx
lea    0x4(%rsp),%r14
lea    0xe7c(%rip),%r15
jmp    119b <main+0x5b>
nopl   0x0(%rax)
addl   $0xffffffff,0xc(%rsp)
je     1247 <main+0x107>
lea    0xe5e(%rip),%rdi
mov    %rsp,%rsi
lea    0x8(%rsp),%rdx
xor    %eax,%eax
call   1060 <scanf@plt>
cmpl   $0x0,(%rsp)
jle    11de <main+0x9e>
mov    %r12,%r13
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
add    $0x1,%rbp
movslq (%rsp),%rax
add    $0x4,%r13
cmp    %rax,%rbp
jl     11c0 <main+0x80>
mov    $0x398,%edx
lea    0x10(%rsp),%rdi
mov    $0xff,%esi
call   1050 <memset@plt>
cmpl   $0x0,0x8(%rsp)
jle    1190 <main+0x50>
xor    %ebp,%ebp
jmp    1215 <main+0xd5>
nopl   (%rax)
mov    %r15,%rdi
call   1030 <printf@plt>
add    $0x1,%ebp
cmp    0x8(%rsp),%ebp
jge    1190 <main+0x50>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
movslq 0x4(%rsp),%rdx
mov    0x10(%rsp,%rdx,4),%esi
test   %esi,%esi
jns    1200 <main+0xc0>
mov    (%rsp),%esi
mov    %r12,%rdi
call   1040 <getMaxAndBitwise@plt>
mov    %eax,%esi
movslq 0x4(%rsp),%rax
mov    %esi,0x10(%rsp,%rax,4)
jmp    1200 <main+0xc0>
xor    %eax,%eax
add    $0x61e38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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