import sys
t=int(input().strip())
for a0 in range(t):
    n=int(input())
    s=input()
    if(s.count('1')==0):
        print(n)
    elif(s.count('1')==1):
        a=1
        for i in s:
            if(i=='1'):
                break
            else:
                a+=1
        print(max(2*a,2*(n-a+1)))
    elif(s[0]=='1' or s[len(s)-1]=='1'):
        print(2*n)
    else:
        m=s[::-1]
        a=0
        for i in s:
            if(i=='1'):
                break
            else:
                a+=1
        b=n-1
        for i in m:
            if(i=='1'):
                break
            else:
                b-=1
        x=s[0:a+1]
        y=s[b::]
        t=max(x.count('0'),y.count('0'))
        print(2*(b-a+1)+t)


t = int(input())
for i in range(t):
    n = int(input())
    s = input()
    b = list()
    maxim = 0
    for i in range(n):
        if s[i] == '1':
            if maxim < max(i+1, n-i):
                maxim = max(i+1, n-i)
    print(max(maxim*2,n))


n = int(input())
s = input()
b = list()
maxim = 0
for i in range(n):
    if s[i] == '1':
        if maxim < max(i+1, n-i):
            maxim = max(i+1, n-i)
print(max(maxim*2,n))


# cook your dish here
for _ in range(int(input())):
    n=int(input())
    a=input()
    b=a[::-1]
    c=[]
    d=a.find("1")
    e=b.find("1")
    if d==-1:
        d=n
        e=n
    c.append(2*(n-d))
    c.append(2*(n-e))
    c.append(n+a.count("1"))
    print(max(c))


# cook your dish here
for _ in range(int(input())):
    n=int(input())
    a=input()
    b=a[::-1]
    c=[]
    c.append(2*(a.find("1")+1))
    c.append(2*(b.find("1")+1))
    c.append(n+a.count("1"))
    print(max(c))


# from debug import debug
# inf = int(1e8)

t = int(input())

while t:
	t-=1
	n = int(input())
	s = list(map(int, input()))
	v = 0
	g = 0
	i, j = 0, n-1
	while i<j:
		if s[i] == 1:
			v = n-i
			g = 1
			break

		if s[n-i-1] == 1:
			v = n-i
			g = 1
			break
		i+=1
		j-=1
	if g:
		print(2*v)
	else:
		print(n)


# from debug import debug
# inf = int(1e8)

t = int(input())

while t:
	t-=1
	n = int(input())
	s = list(map(int, input()))
	v = n
	for i in range(n):
		if s[i] == 1:
			v = max(v, 2*(i+1), 2*(n-i))
	print(v)


for _ in range(int(input())):
    n=int(input())
    a=input()
    f=10001
    m=0
    if '1' not in a:
        print(n)
    else:
        for i in range(n):
            if a[i]=='1':
                f=min(i,f)
                m=max(i,m)
        print(((n-f))*2 if (n-f)>m else (m)*2)


for _ in range(int(input())):
    n=int(input())
    a=input()
    f=10001
    m=0
    if '1' not in a:
        print(n)
    else:
        for i in range(n):
            if a[i]=='1':
                f=min(i,f)
                m=max(i,m)
        print(((n-f))*2 if (n-f)>m else (m+1)*2)


import re

no_of_lines = int(input())
input_lines = []
for i in range(2*no_of_lines):
    input_lines.append(input())
flag = False
count = 0
for line in input_lines:
    if flag is False:
        stories = int(line)
        flag = True
    else:
        # answer = 1
        stairs = [m.start() for m in re.finditer("1", line)]
        if stairs:
            if stairs[0] == 0 or stairs[-1] == stories - 1:
                print(2 * stories)
            else:
                if stairs[0] == min(stairs[0], stories - stairs[-1]):
                    most_reachable = stairs[0]
                else:
                    most_reachable = stairs[-1]
                print(
                    max(
                        2 * (max(stories - most_reachable - 1, most_reachable) + 1),
                        stories + len(stairs),
                    )
                )
        else:
            print(stories)

        flag = False


import re

no_of_lines = int(input())
input_lines = []
for i in range(2*no_of_lines):
    input_lines.append(input())
flag = False
count = 0
for line in input_lines:
    if flag is False:
        stories = int(line)
        flag = True
    else:
        # answer = 1
        stairs = [m.start() for m in re.finditer("1", line)]
        if stairs:
            if stairs[0] == min(stairs[0], stories - stairs[-1]):
                most_reachable = stairs[0]
            else:
                most_reachable = stairs[-1]
            print(
                max(
                    2 * (max(stories - most_reachable - 1, most_reachable) + 1),
                    stories + len(stairs),
                )
            )
        else:
            print(stories)

        flag = False


import re

def solution(input):
    input_lines = input.split("\n")
    flag = False
    count = 0
    for line in input_lines[1:]:
        print("count:", count)
        if flag is False:
            stories = int(line)
            flag = True
        else:
            # answer = 1
            stairs = [m.start() for m in re.finditer("1", line)]
            if stairs:
                if stairs[0] == min(stairs[0], stories - stairs[-1]):
                    most_reachable = stairs[0]
                else:
                    most_reachable = stairs[-1]
                print(
                    max(
                        2 * (max(stories - most_reachable - 1, most_reachable) + 1),
                        stories + len(stairs),
                    )
                )
            else:
                print(stories)
            flag = False

        count += 1


import re

no_of_lines = int(input())
input_lines = []
for i in range(2 * no_of_lines):
    input_lines.append(input())
flag = False
count = 0
for line in input_lines:
    if flag is False:
        stories = int(line)
        flag = True
    else:
        stairs = [m.start() for m in re.finditer("1", line)]
        if stairs:
            if stairs[0] == 0 or stairs[-1] == stories - 1:
                print(2 * stories)
            else:
                c1 = stairs[0]
                c2 = stairs[-1]
                c1_max = 2 * (max(stories - c1, c1 + 1))
                c2_max = 2 * (max(stories - c2, c2 + 1))
                print(max(c1_max, c2_max, stories + len(stairs)))
        else:
            print(stories)

        flag = False


input_lines = input().split()
flag = False
for line in input_lines[1:]:
    if flag is False:
        stories = int(line)
        flag = True
    else:
        stairs = [m.start() for m in re.finditer("1", line)]
        if stairs:
            if stairs[0] == min(stairs[0], stories - stairs[-1]):
                most_reachable = stairs[0]
            else:
                most_reachable = stairs[-1]
            print(
                max(
                    2 * (max(stories - most_reachable - 1, most_reachable) + 1),
                    stories + len(stairs),
                )
            )
        else:
            print(stories)

        flag = False


import re

def solution(input):
    input_lines = input.split()
    flag = False
    count = 0
    for line in input_lines[1:]:
        if flag is False:
            stories = int(line)
            flag = True
        else:
            stairs = [m.start() for m in re.finditer("1", line)]
            if stairs:
                if stairs[0] == min(stairs[0], stories - stairs[-1]):
                    most_reachable = stairs[0]
                else:
                    most_reachable = stairs[-1]
                print(
                    max(
                        2 * (max(stories - most_reachable - 1, most_reachable) + 1),
                        stories + len(stairs),
                    )
                )
            else:
                print(stories)
            flag = False
    return


import math
am = int(input())
for i in range(am):
    rooms = int(input())
    ladders = input()
    if "1" in ladders:
        ind = int(ladders.find("1"))
        rind = int(ladders.rfind("1"))+1

        print(max( rind * 2 , (rooms - ind) *2))

    else:
        print(rooms)


import math
am = int(input())
for i in range(am):
    rooms = int(input())
    ladders = input()
    if "1" in ladders:
        ind = int(ladders.index("1")) + 1
        if ind*2 > (rooms-ind+1)*2:
            print(ind*2)
            continue
        print((rooms-ind+1)*2)
    else:
        print(rooms)


import math
am = int(input())
for i in range(am):
    rooms = int(input())
    ladders = input()
    if "1" in ladders:
        ind = int(ladders.find("1"))
        rind = int(ladders.rfind("1"))

        print(max( rind * 2 , (rooms - ind) *2))

    else:
        print(rooms)


import math
am = int(input())
for i in range(am):
    rooms = int(input())
    ladders = input()
    if "1" in ladders:
        ind = int(ladders.index("1")) + 1
        if ind*2 > (rooms-ind+1)*2:
            if (ind*2 + (ladders.rfind("1") - ind) > rooms):
                print(rooms*2)
                continue
            print(ind*2 + (ladders.rfind("1") - ind))
            continue
        if((rooms-ind+1)*2 + (ladders.rfind("1") - ind) > rooms):
            print (rooms*2)
            continue
        print((rooms-ind+1)*2 + (ladders.rfind("1") - ind))
    else:
        print(rooms)


for i in range(int(input())):
    n = int(input())
    s = list(input())
    if s[0] == '1' or s[-1] == '1':
        print(n * 2)
    else:
        index = -1
        s.reverse()
        for i in range(n):
            if s[i] == '1':
                index = n - (i + 1) + 1
                break
        print(index * 2 if index != -1 else n)


for i in range(int(input())):
    n = int(input())
    s = list(input())
    if '1' in s:
        index1 = n - s.index('1')
        s.reverse()
        index2 = n - s.index('1')
        print(max(index1 * 2, index2 * 2, n))
    else:
        print(n)


for i in range(int(input())):
    n = int(input())
    s = list(input())
    print(n * 2 if s[0] == '1' or s[-1] == '1' else (s.count('1') * 2) + s.count('0'))


t = int(input())
for i in range(t):
    n = int(input())
    s = str(input())
    if s == "0" * n:
        print(n)
    else:
        for j in range(len(s)):
            if s[j] == "1":
                m1 = j + 1
                break
        for k in range(1, len(s)):
            if s[-k] == "1":
                m2 = k
                break
        m = min(m1, m2)
        ans = 2 * abs(n - m + 1)
        print(ans)


t = int(input())
for i in range(t):
    n = int(input())
    s = str(input())
    if s == "0" * n:
        print(n)
    elif s == "1":
        print(2)
    else:
        for j in range(len(s)):
            if s[j] == "1":
                m1 = j + 1
                break
        for k in range(1, len(s)):
            if s[-k] == "1":
                m2 = k
                break
        m = min(m1, m2)
        ans = 2 * abs(n - m + 1)
        print(ans)


t = int(input())
for i in range(t):
    n = int(input())
    s = str(input())
    if s == "0" * n:
        print(n)
    else:
        for j in range(len(s)):
            if s[j] == "1":
                m1 = j + 1
                break
        for k in range(1, len(s)+1):
            if s[-k] == "1":
                m2 = k
                break
        m = min(m1, m2)
        ans = 2 * abs(n - m + 1)
        print(ans)


for t in range(int(input())):
    n=int(input())
    str1=input()
    mini=n-1
    maxi=0
    #maxi>=mini
    bol=True
    for i in range(n):
        if(str1[i]=='1'):
            bol=False
        if(str1[i]=='1' and i>maxi):
            maxi=i
        if(str1[i]=='1' and i<mini):
            mini=i
    if(bol==True):
        print(n)
    mini=mini+1
    maxi=maxi+1
   3 print(mini)
   # print(maxi)
    diffmin=(n-mini+1)
    diffmax=(maxi)
  #  print("ans")
    if(diffmax>=diffmin and bol==False):
        print(2*diffmax)
    elif(diffmax<diffmin and bol==False):
        print(2*diffmin)


for t in range(int(input())):
    n=int(input())
    str1=input()
    mini=n-1
    maxi=0
    #maxi>=mini
    bol=True
    for i in range(n):
        if(str1[i]=='1' and i>maxi):
            maxi=i
            bol=False
        if(str1[i]=='1' and i<mini):
            mini=i
            bol=False
    if(bol==True):
        print(n)
    mini=mini+1
    maxi=maxi+1
    #print(mini)
    #print(maxi)
    diffmin=(n-mini+1)
    diffmax=(maxi)
    #print("ans")
    if(diffmax>=diffmin and bol==False):
        print(2*diffmax)
    elif(diffmax<diffmin and bol==False):
        print(2*diffmin)


for t in range(int(input())):
    n=int(input())
    str1=input()
    mini=n-1
    maxi=0
    #maxi>=mini
    bol=True
    for i in range(n):
        if(str1[i]=='1'):
            bol=False
        if(str1[i]=='1' and i>maxi):
            maxi=i
        if(str1[i]=='1' and i<mini):
            mini=i
    if(bol==True):
        print(n)
    mini=mini+1
    maxi=maxi+1
   #print(mini)
   # print(maxi)
    diffmin=(n-mini+1)
    diffmax=(maxi)
  #  print("ans")
    if(diffmax>=diffmin and bol==False):
        print(2*diffmax)
    elif(diffmax<diffmin and bol==False):
        print(2*diffmin)


t = int(input())

for _ in range(t):
    n = int(input())
    a = input()
    f = a.find("1")
    if f == -1:
        print(n)
    else:
        print(2*max(n-f, a.rfind("1")+1))


t = int(input())

for _ in range(t):
    n = int(input())
    a = input()
    s = a.count("1")
    if s == 0:
        print(n)
    else:
        print(2*n-2*min(a.find("1"), n-a.rfind("1")))


def algo(n, s):
    for i in range(n):
        if s[i] == '1':
            if i < n / 2:
                print((n - i) * 2)
                return
            else:
                print((i + 1) * 2)
                return
    print(n)
    return

for i in range(int(input())):
    n = int(input())
    s = input()
    algo(n, s)


for i in range(int(input())):
    n = int(input())
    s = input()
    rs = s[::-1]
    if s[0] == '1' or rs[0] == '1':
        print(n*2)
    elif '1' in s:
        a = s.index('1')
        b = rs.index('1')
        if a > b:
            print((n - b) * 2)
        else:
            print((n - a) * 2)
    else:
        print(n)


def algo(n, s):
    if s[0] == '1' or s[-1] == '1':
        print(n*2)
        return
    avg = round(n / 2 + .5)
    for i in range(0, avg):
        if s[i] == '1':
            print((n - i) * 2)
            return
    for i in range(n - 1, avg - 1, -1):
        if s[i] == '1':
            print((i + 1) * 2)
            return
    print(n)
    return

for i in range(int(input())):
    n = int(input())
    s = input()
    algo(n, s)


def algo(n, s):
    if s[0] == '1' or s[-1] == '1':
        print(n*2)
        return
    for i in range(n):
        if s[i] == '1':
            if i < n / 2:
                print((n - i) * 2)
                return
            else:
                print((i + 1) * 2)
                return
    print(n)
    return

for i in range(int(input())):
    n = int(input())
    s = input()
    algo(n, s)


for i in range(int(input())):
    n = int(input())
    s = input()
    rs = s[::-1]
    a = s.index('1')
    b = rs.index('1')
    if s[0] == '1' or rs[0] == '1':
        print(n*2)
    elif '1' in s:
        if a > b:
            print((n - b) * 2)
        else:
            print((n - a) * 2)
    else:
        print(n)


t = int(input())
for i in range(t):
    n, s = int(input()), input()
    ix1, ix2 = s.find('1'), s[::-1].find('1')

    if (ix1 == ix2 == -1):
        print(n)
        continue
    print(max(n - ix1, n-ix2 ) * 2)


t = int(input())
for i in range(t):
    n, s = int(input()), input()
    ix1, ix2 = s.find('1'), s[::-1].find('1')
    if (ix1 == ix2 == -1):
        print(n)
        continue
    print(max(n - ix1, ix2 - 0) * 2)


test = int(input())

while test>0:
    test-=1
    x = int(input())
    st = input()
    count = 0
    if st=='1':
        print(2)
    elif st=='0':
        print(1)
    elif st[0]=='1' or st[-1]=='1':
        print(2*x)
    elif '1' not in st:
        print(x)
    else:
        for i in range(x):
            if st[i]=='1':
                pos = i
        for i in range(x):
            if st[i]=='1':
                fs = i
                break
        t1,t2=0,0
        if pos<(x//2):
            pos = x-pos
            t1 = pos*2
        else:
            t1=(pos+1)*2

        if fs<(x//2):
            fs = x-fs
            t2 = fs*2
        else:
            t2=(fs+1)*2

        print(max(t1,t2))


test = int(input())

while test>0:
    test-=1
    x = int(input())
    st = input()
    count = 0
    if st=='1':
        print(2)
    elif st=='0':
        print(1)
    elif st[0]=='1' or st[-1]=='1':
        print(2*x)
    else:
        for i in range(x):
            if st[i]=='1':
                pos = i
        if pos<(x//2):
            pos = x-pos
            print(pos*2)
        else:
            print((pos+1)*2)


import math
test = int(input())

while test>0:
    test-=1
    x = int(input())
    st = input()
    count = 0
    if st=='1':
        print(2)
    elif st=='0':
        print(1)
    elif st[0]=='1' or st[-1]=='1':
        print(2*x)
    elif '1' not in st:
        print(x)
    else:
        for i in range(x):
            if st[i]=='1':
                pos = i
        if pos<(x//2):
            pos = x-pos
            print(pos*2)
        else:
            print((pos+1)*2)


import math
test = int(input())

while test>0:
    test-=1
    x = int(input())
    st = input()
    count = 0
    for i in range(x):
        if st[i]=='0':
            count+=1
        else:
            count+=2
    if (st[0]=='0' and st[1]=='1') or (st[-1]=='0' and st[-2]=='1') or (st[0]=='1' and st[1]=='0') or (st[-1]=='1' and st[-2]=='0'):
        count+=1
    print(count)


test = int(input())

while test>0:
    test-=1
    x = int(input())
    st = input()
    count = 0
    if st=='1':
        print(2)
    elif st=='0':
        print(1)
    else:
        for i in range(x):
            if st[i]=='0':
                count+=1
            else:
                count+=2
        if (st[0]=='0' and st[1]=='1') or (st[-1]=='0' and st[-2]=='1') or (st[0]=='1' and st[1]=='0') or (st[-1]=='1' and st[-2]=='0'):
            count+=1
        print(count)


test = int(input())

while test>0:
    test-=1
    x = int(input())
    st = input()
    count = 0
    if st=='1':
        print(2)
    elif st=='0':
        print(1)
    elif st[0]=='1' or st[-1]=='1':
        print(2*x)
    elif '1' not in st:
        print(x)
    else:
        for i in range(x):
            if st[i]=='1':
                pos = i
        if pos<(x//2):
            pos = x-pos
            print(pos*2)
        else:
            print((pos+1)*2)


for _ in range(int(input())):
    num_of_rooms = int(input())
    bit_map = input()
    last_ladder_position = 0
    first_ladder_position = 0

    if not '1' in bit_map:
        print(num_of_rooms)
        continue
    else:
        for i in range(num_of_rooms-1, -1, -1):
            if bit_map[i] == '1':
                last_ladder_position = i
                break
        for i in range(0,  num_of_rooms):
            if bit_map[i] == '1':
                first_ladder_position = i
                break
    ladder_position = max(num_of_rooms-1-first_ladder_position, last_ladder_position)
    if ladder_position < (num_of_rooms-1)//2:
        print(num_of_rooms+1)
    else:
        print((ladder_position+1)*2)


for _ in range(int(input())):
    num_of_rooms = int(input())
    bit_map = input()
    last_ladder_position = 0
    if not '1' in bit_map:
        print(num_of_rooms)
        continue
    else:
        for i in range(num_of_rooms-1, -1, -1):
            if bit_map[i] == '1':
                last_ladder_position = i
                break

    if last_ladder_position < (num_of_rooms-1)//2:
        print(num_of_rooms+1)
    else:
        print((last_ladder_position+1)*2)


t = int(input())
for i in range(t):
    n = int(input())
    s = input()
    l = n
    h = -1
    for i in range(n):
        if s[i]=="1":
            l = min(i,l)
            h = i
    if h == -1:
        print(n)
    else:
        print(max(h+1,n-l)*2)


t = int(input())
for i in range(t):
    n = int(input())
    s = input()
    l = n
    h = -1
    for i in range(n):
        if s[i]=="1":
            l = min(i,l)
            h = i
    if h == -1:
        print(n)
    else:
        print(max(l,h,n-h,n-l)*2)


t = int(input())
for i in range(t):
    n = int(input())
    s = input()
    l = n
    h = 0
    for i in range(n):
        if s[i]=="1":
            l = min(i,l)
            h = i
    if h == 0:
        print(n)
    else:
        print(max(l,h,n-h,n-l)*2)


for i in range(int(input())):
	n = int(input())
	k = input()
	if '1' in k:
		h = k.index('1')
		k = k.rindex('1')+1
		print(2*(n-min(h,n-1-h,n-k,k)))
	else:
		print(n)


for i in range(int(input())):
	n = int(input())
	k = input()
	if '1' in k:
		h = k.index('1')
		print(2*(n-min(h,n-1-h)))
	else:
		print(n)


for i in range(int(input())):
	n = int(input())
	k = input()
	if k[0] == '1' or k[-1] == '1':print(2*n)
	else:print(n+k.count('1'))


t=int(input())
for i in range(t):
    n=int(input())
    s=input()
    least_index1=-1
    least_index2=0
    for i in range(n):
        if(s[i]=='1'):
            least_index1=i
            break
    for i in range(-1,-n-1,-1):
        if(s[i]=='1'):
            least_index2=i
            break
    #print(least_index)
    ans1=(2*max(n-least_index1,least_index1+1)) if least_index1!=-1 else n
    ans2=(2*max(n+least_index2+1,-least_index2)) if least_index2!=0 else n
    print(max(ans1,ans2))


t=int(input())
for _ in range(t):
    n=int(input())
    s=input()
    if '1' not in s:
        print(n)
    else:
        for i in range(n):
            if (s[i]=='1'):
                temp=i
                break
        print(max(2*temp,2*(n-temp+1)))


t = int(input())
for i in range(t):
	b = int(input())
	s = input()
	if s.find('1') == -1:
		print(b)
		continue
	a = max(b-s.find('1'),s.rfind('1')+1)
	print(2*a)


R=lambda:int(input())
for i in[0]*R():b=R();s=input();n=s.find('1');print([2*max(b-n,s.rfind('1')+1),b][n<0])


R=lambda:int(input())
for i in range(R()):
	b = R()
	s = input()
	n = s.find('1')
	if n == -1:
		print(b)
	else:
		print(2*max(b-n,s.rfind('1')+1))


t = int(input())
for i in range(t):
	b = int(input())
	s = input()
	if s.find('1') == -1:
		print(b)
		continue
	a = max(b-s.find('1'),b-s.rfind('1'))
	print(2*a)


I,l=input,len
for i in'0'*int(I()):I();s=I();print(2*max(l(s.lstrip(i)),l(s.rstrip(i)))or l(s))


t= int(input())

for i in range(t):
     n = int(input())
     s = input()
     if s[0] =="1" or s[n-1] =="1" :
         print(2*n)
    # elif int(s) ==0 :
      #   print(n)

     else :
         print(n+s.count(1))


t= int(input())
for i in range(t):
    a , b,c,d,k = map(int,input().split())

    x = a//c
    if a%c==0 :
        x+=1
    y = b//d
    if b%d==0 :
        y+=1
    if k <(x+y):
        print(-1)
    else :
        print(x,y)


t= int(input())

for i in range(t):
     n = int(input())
     s = input()
     if s[0] =="1" or s[n-1] =="1" :
         print(2*n)
    # elif int(s) ==0 :
      #   print(n)

     else :
         print(n+s.count("1"))


t= int(input())

for i in range(t):
     n = int(input())
     s = input()
     if s[0] =="1" or s[n-1] =="1" :
         print(2*n)
     elif int(s) ==0 :
         print(n)

     else :
         print(n+1)


t=int(input())
for _ in range(t):
    n=int(input())
    s=input()
    p=s.find('1')
    q=s.rfind('1')
    if p==-1:
        print(n)
    else:
        print(max(n-p,q+1)*2)


t= int(input())

for i in range(t):
     n = int(input())
     s = input()
     if s[0] =="1" or s[n-1] =="1" :
         print(2*n)
     elif sum(int(s)) ==0 :
         print(n)

     else :
         print(n+1)


t= int(input())

for i in range(t):
     n = int(input())
     s = input()
     if s[0] =="1" or s[n-1] =="1" :
         print(2*n)
     else :
         print(n+1)


t=int(input())
for _ in range(t):
    n=int(input())
    s=input()
    p=s.find('1')
    q=s.rfind('1')
    if p==-1:
        print(n)
    else:
        print((n-p,q+1)*2)


for i in range(int(input())):
    w=int(input())
    e=input()
    if '1' in e:
        r=min(e.index('1'),e[::-1].index('1'))
        print(2*(w-r))
    else:
        print(w)


for i in range(int(input())):
    w=int(input())
    e=input()
    if '1' in e:
        r=max(e.index('1'),e[::-1].index('1'))
        print(2*(r))
    else:
        print(w)


t = int(input())
# t = 1
def roomCount(n, s):
    count = 0
    if s[0] == '1' or s[-1] == '1':
        print(2*n)
        return
    for i in range(len(s)):
        if s[i] == '1':
            count += 1
    print(count + n)
    return
for _ in range(t):
    n = int(input())
    s = input()
    roomCount(n, s)


t = int(input())
# t = 1
def roomCount(n, s):
    count = 0
    if s[0] == '1' or s[-1] == '1':
        print(2*n)
        return
    last = -1
    first = -1
    for i in range(len(s)):
        if s[i] == '1':
            if first == -1:
                first = i
            last = i
            count += 1
    print(max(count + n, 2 * (first + 1), 2 * (last + 1)))
    return
for _ in range(t):
    n = int(input())
    s = input()
    roomCount(n, s)


t = int(input())
# t = 1
def roomCount(n, s):
    count = 0
    if s[0] == '1' or s[-1] == '1':
        print(2*n)
        return
    last = -1
    first = -1
    for i in range(len(s)):
        if s[i] == '1':
            if first == -1:
                first = i
            last = i
            count += 1
    if first != -1:
        print(max(count + n, 2 * (n - first), 2 * (last + 1)))
    else:
        print(n)
    return
for _ in range(t):
    n = int(input())
    s = input()
    roomCount(n, s)


t = int(input())
# t = 1
def roomCount(n, s):
    count = 0
    if s[0] == '1' or s[-1] == '1':
        print(2*n)
        return
    last = -1
    first = -1
    for i in range(len(s)):
        if s[i] == '1':
            if first == -1:
                first = i
            last = i
            count += 1
    print(max(count + n, 2 * (n - first), 2 * (last + 1)))
    return
for _ in range(t):
    n = int(input())
    s = input()
    roomCount(n, s)


t = int(input())
results = []
for _ in range(t):
	n = int(input())
	rooms = str(input())
	if rooms[0] == '1' or rooms[-1] == '1':
		results.append(n*2)
	else:
		x = rooms.count('0')+rooms.count('1')*2
		b = 0
		c = 0
		if rooms.rfind('1')+1 > len(rooms)-rooms.rfind('1')+1:
			b = (rooms.rfind('1')+1)*2
		elif rooms.rfind('1') != -1:
			b = len(rooms)+1
		if rooms[::-1].rfind('1')+1 > len(rooms)-rooms[::-1].rfind('1')+1 and rooms[::-1].rfind('1') != -1:
			c = (rooms[::-1].rfind('1')+1)*2
		elif rooms[::-1].rfind('1') != -1:
			c = len(rooms)+1

		results.append(max(x, b, c))
for x in results:
	print(x)


t = int(input())
results = []
for _ in range(t):
	n = int(input())
	rooms = str(input())
	if rooms[0] == '1' or rooms[-1] == '1':
		results.append(n*2)
	else:
		x = rooms.count('0')+rooms.count('1')*2
		y = (rooms.rfind('1')+1)*2
		results.append(max(x, y))
for x in results:
	print(x)


t = int(input())
results = []
for _ in range(t):
	n = int(input())
	rooms = str(input())
	if rooms[0] == '1' or rooms[-1] == '1':
		results.append(n*2)
	else:
		results.append(rooms.count('0')+rooms.count('1')*2)
for x in results:
	print(x)


t=int(input())
for _ in range(t):
    n=int(input())
    l=[*input()]
    if l==['0']*n:
        print(n)
    else:
        k=l.copy()
        k.reverse()
        a=l.index('1')
        b=k.index('1')
        print(max(n-a,n-b)*2)


t=int(input())
for _ in range(t):
    n=int(input())
    l=[*input()]
    if l[0]=='1' or l[n-1]=='1':
        print(n*2)
    else:
        s=0
        for e in l:
            if e=='0':
                s+=1
            else:
                s+=2
        print(s)


x=0
for i in range(int(input())):
    n = int(input())
    b = list(map(int, input()))
    for j in range(n):
        if b[j]==1:
            x=j+1
    if b[0]==1 or b[n-1]==1:
        print(n*2)
    elif sum(b) != 0:
        print(x*2)
    else:
        print(n)


x=0
y=0
for i in range(int(input())):
    n = int(input())
    b = list(map(int, input()))
    for j in range(n):
        if b[j]==1:
            x=j+1
        if b[n-j-1]==1:
            y=j+1
    if b[0]==1 or b[n-1]==1:
        print(n*2)
    elif (x>=y)and(sum(b) != 0):
        print(x*2)
    elif (y>x)and(sum(b) != 0):
        print(y*2)
    else:
        print(n)


t = int(input())

for i in range(t):
    alaki = input()
    floor = input()
    anyStair = False
    for index in range(len(floor)):
        if floor[index] == '1':
            anyStair = True
            oneIndex = index
            break
    floor = floor[::-1]
    for index in range(len(floor)):
        if floor[index] == '1':
            anyStair = True
            if (index < oneIndex):
                oneIndex = index
    if (anyStair == False):
        print(len(floor))
        continue
    oneIndex = len(floor) - oneIndex - 1
    print((oneIndex + 1) * 2)


t = int(input())

for i in range(t):
    alaki = input()
    floor = input()
    anyStair = False
    for index in range(len(floor)):
        if floor[index] == '1':
            anyStair = True
            oneIndex = index
    if (anyStair == False):
        print(len(floor))
        continue
    if (len(floor) - oneIndex - 1 > oneIndex):
        oneIndex = len(floor) - oneIndex - 1
    print((oneIndex + 1) * 2)


t = int(input())

for i in range(t):
    alaki = input()
    floor = input()
    anyStair = False
    for index in range(len(floor)):
        if floor[index] == '1':
            anyStair = True
            oneIndex = index
            break
    if (anyStair == False):
        print(len(floor))
        continue
    if (len(floor) - oneIndex - 1 > oneIndex):
        oneIndex = len(floor) - oneIndex - 1
    print((oneIndex + 1) * 2)


def Recursion(s, graph, maximum, count):
	r = s[0]
	c = s[1]
	visited[r][c] = True
	count += 1
	#print('r '+str(r)+' c '+str(c))
	dx = [-1,0,1,0]
	dy = [0,1,0,-1]
	for i in range(4):
		x = r + dx[i]
		y = c + dy[i]
		if x > -1 and x < 2 and y > -1 and y < l:
			if graph[x][y] == 1 and visited[x][y] is False:
				maximum = Recursion([x, y], graph, maximum, count)
			elif i % 2 != 0 and graph[x][y] == 0 and visited[x][y] is False:
				maximum = Recursion([x, y], graph, maximum, count)
	visited[r][c] = False
	maximum = max(maximum, count)
	count -= 1
	return maximum

if __name__ == '__main__':
	case = int(input())
	for c in range(case):
		l = int(input())
		s = input()
		graph = [[] for _ in range(2)]
		visited = [[] for _ in range(2)]
		for i in range(l):
			graph[0].append(0 if s[i] == '0' else 1)
			graph[1].append(0 if s[i] == '0' else 1)
			visited[0].append(False)
			visited[1].append(False)
		maximum = 0
		count = 0
		maximum = max(Recursion([0,0], graph, maximum, count), maximum)
		maximum = max(Recursion([0,l - 1], graph, maximum, count), maximum)
		print(maximum)


def Recursion(s, graph, maximum, count):
	r = s[0]
	c = s[1]
	visited[r][c] = True
	count += 1
	#print('r '+str(r)+' c '+str(c))
	dx = [-1,0,1,0]
	dy = [0,1,0,-1]
	for i in range(4):
		x = r + dx[i]
		y = c + dy[i]
		if x > -1 and x < 2 and y > -1 and y < l:
			if graph[x][y] == 1 and visited[x][y] is False:
				maximum = Recursion([x, y], graph, maximum, count)
			elif i % 2 != 0 and graph[x][y] == 0 and visited[x][y] is False:
				maximum = Recursion([x, y], graph, maximum, count)
	visited[r][c] = False
	maximum = max(maximum, count)
	count -= 1
	return maximum

if __name__ == '__main__':
	case = int(input())
	for c in range(case):
		l = int(input())
		s = input()
		graph = [[] for _ in range(2)]
		visited = [[] for _ in range(2)]
		for i in range(l):
			graph[0].append(0 if s[i] == '0' else 1)
			graph[1].append(0 if s[i] == '0' else 1)
			visited[0].append(False)
			visited[1].append(False)
		maximum = 0
		count = 0
		maximum = max(Recursion([0,0], graph, maximum, count), maximum)
		#maximum = max(Recursion([0,l - 1], graph, maximum, count), maximum)
		print(maximum)


def Recursion(s, graph, maximum, count):
	r = s[0]
	c = s[1]
	visited[r][c] = True
	count += 1
	#print('r '+str(r)+' c '+str(c))
	dx = [-1,0,1,0]
	dy = [0,1,0,-1]
	for i in range(4):
		x = r + dx[i]
		y = c + dy[i]
		if x > -1 and x < 2 and y > -1 and y < l:
			if graph[x][y] == 1 and visited[x][y] is False:
				maximum = Recursion([x, y], graph, maximum, count)
			elif i % 2 != 0 and graph[x][y] == 0 and visited[x][y] is False:
				maximum = Recursion([x, y], graph, maximum, count)
	visited[r][c] = False
	maximum = max(maximum, count)
	count -= 1
	return maximum

if __name__ == '__main__':
	case = int(input())
	for c in range(case):
		l = int(input())
		s = input()
		graph = [[] for _ in range(2)]
		visited = [[] for _ in range(2)]
		left = 0
		right = 0
		for i in range(l):
			graph[0].append(0 if s[i] == '0' else 1)
			graph[1].append(0 if s[i] == '0' else 1)
			visited[0].append(False)
			visited[1].append(False)
			if i < l // 2 and s[i] == '0':
				left += 1
			else:
				right += 1
		maximum = 0
		count = 0
		if left > right:
			maximum = max(Recursion([0,0], graph, maximum, count), maximum)
		else:
			maximum = max(Recursion([0,l - 1], graph, maximum, count), maximum)
		print(maximum)


#def A(s, graph, end):

if __name__ == '__main__':
	case = int(input())
	for c in range(case):
		l = int(input())
		s = input()
		graph = [[] for _ in range(2)]
		visited = [[] for _ in range(2)]
		first = -1
		last = -1
		for i in range(l):
			graph[0].append(0 if s[i] == '0' else 1)
			graph[1].append(0 if s[i] == '0' else 1)
			visited[0].append(False)
			visited[1].append(False)
			if s[i] == '1' and first == -1:
				first = i
			if s[i] == '1' and i > last:
				last = i
		maximum = 0
		# start left
		count = 0
		for i in range(l):
			if i <= last:
				count += 1
			else:
				break
		#print(count)
		if first != -1:
			count += max(l - last - 1, last + 1)
		#print('count left= '+str(count))
		maximum = max(maximum, count)
		# start right
		count = 0
		for i in range(l-1,-1,-1):
			if i >= first:
				count += 1
			else:
				break
		if last != -1:
			count += max(first + 1, l - first)
		#print('count right= '+str(count))
		maximum = max(maximum, count)
		print(maximum)


#def A(s, graph, end):

if __name__ == '__main__':
	case = int(input())
	for c in range(case):
		l = int(input())
		s = input()
		graph = [[] for _ in range(2)]
		visited = [[] for _ in range(2)]
		first = -1
		last = -1
		for i in range(l):
			graph[0].append(0 if s[i] == '0' else 1)
			graph[1].append(0 if s[i] == '0' else 1)
			visited[0].append(False)
			visited[1].append(False)
			if s[i] == '1' and first == -1:
				first = i
			if s[i] == '1' and i > last:
				last = i
		maximum = 0
		# start left
		count = 0
		for i in range(l):
			if i <= last:
				count += 1
			else:
				break
		if first != -1:
			count += max(l - last, last)
		#print('count left= '+str(count))
		maximum = max(maximum, count)
		# start right
		count = 0
		for i in range(l-1,-1,-1):
			if i >= first:
				count += 1
			else:
				break
		if last != -1:
			count += max(first, l - first)
		#print('count right= '+str(count))
		maximum = max(maximum, count)
		print(maximum)


n=int(input())
from math import ceil
for i in range(n):
    x=int(input())
    l=input()
    o=l.count('1')
    if o==0:
        print(len(l))
        continue
    elif o==x:
        print(len(l)*2)
        continue
    if o==1:
        print((l.index('1')+1)*2)
    else:
        print(len(l)*o)


n=int(input())
from math import ceil
for i in range(n):
    x=int(input())
    l=input()
    o=l.count('1')
    if o==0:
        print(len(l))
        continue
    elif o==x:
        print(len(l)*2)
        continue
    else:
        print(len(l)*o)


for q in range(int(input())):
    n = int(input())
    bi = input()
    l = bi.find("1")

    if l == -1:
        print(n)
        continue
    r = n - 1 - bi.rfind("1")
    print(2*n - 2*min(l, r))


for q in range(int(input())):
    n = int(input())
    bi = input()
    l = bi.find("1")
    print(l)
    if l == -1:
        print(n)
        continue
    r = n - 1 - bi.rfind("1")
    print(2*n - 2*min(l, r))


def room(n,s):
    z = y = k = f = x= 0
    for i in range(n):
        if s[0] == "1" or s[-1] == "1":
            f = 2*n

        if s[i] == "1":
            z = (i+1) + (n-i)
            y = 2 * (n-i)
            x = 2 * (i+1)

        if s[i] == "0":
            k += 1
            i += 1

    return max(f,max(x,y,z),k)

t = int(input())
for i in range(t):
    n = int(input())
    s = input()
    ans = room(n,s)
    print(ans)


def room(n,s):
    z = y = k = f = x = 0
    for i in range(n):
        if s[0] == "1" or s[-1] == "1":
            f = 2*n

        if s[i] == "1":
            z = (i+1) + (n-i)
            y = 2 * (n-i)
            x = 2 * (i+1)

        if s[i] == "0":
            k += 1
            i += 1

    return max(f,max(x,y,z),k,n)

t = int(input())
for i in range(t):
    n = int(input())
    s = input()
    ans = room(n,s)
    print(ans)


def room(n,s):
    z = y = k = f = x = u = 0
    li = []
    for i in range(n):
        if s[0] == "1" or s[-1] == "1":
            f = 2*n
            li.append(f)
        if s[i] == "1":
            z = (i+1) + (n-i)
            y = 2 * (n-i)
            x = 2 * (i+1)
            u = max(x,y,z)
            li.append(u)

        if s[i] == "0":
            k += 1
            i += 1
            li.append(k)

    return max(li)

t = int(input())
for i in range(t):
    n = int(input())
    s = input()
    ans = room(n,s)
    print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	a = s.find("1")
	if(a == -1):
		print(n)
	elif(a == 0):
		print(n*2)
	elif(a == (n-1)):
		print(n*2)
	else:
		b = s.rfind("1")
		b = n-b
		print(a,b)
		if(a > b):
			val = (a+1)*2
		else:
			val = (b)*2
		print(val)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	a = s.find("1")
	if(a == -1):
		print(n)
	elif(a == 0):
		print(n*2)
	elif(a == (n-1)):
		print(n*2)
	else:
		b = s.rfind("1")
		z = n-b-1
		print(a,b)
		if(a < z):
			val = (n-a)*2
		else:
			val = (b+1)*2
		print(val)


#include<bits/stdc++.h>
using namespace std;
int main(){
	long long int n,proi=1,mi=1000001,min_i=n+1;
	cin>>n;
	long long int x[n],y[n],z[n];
	for(int i=0;i<n;i++){
		cin>>x[i];
		y[i]=-x[i]-1;
		if(mi>y[i]){
			mi=y[i];
			min_i=i;
		}
		if(abs(x[i])>abs(y[i]))
			z[i]=x[i];
		else
			z[i]=y[i];
	}
	for(int i=0;i<n;i++)
		proi=proi*z[i];
	if(proi>0)
		for(int i=0;i<n;i++)
			cout<<z[i]<<" ";
	else{
		z[min_i]=-z[min_i]-1;
		for(int i=0;i<n;i++)
			cout<<z[i]<<" ";
	}

}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,ma=-1000000,max_i=0,k=0;
	cin>>n;
	int x[n];
	for(int i=0;i<n;i++){
		cin>>x[i];
		if(x[i]>ma)
			ma=x[i];
			max_i=i;
	}
	for(int i=0;i<n;i++)
		if(x[i]>0){
			x[i]=-x[i]-1;
			k++;
		}
	if(k%2==0)
		for(int i=0;i<n;i++)
			cout<<x[i]<<" ";
	else{
		x[max_i]=ma;
		for(int i=0;i<n;i++)
			cout<<x[i]<<" ";
	}

}


#include<bits/stdc++.h>
using namespace std;
int main(){
	long long int n,proi=1,max=-1000000,max_i=-1,k=0;
	cin>>n;
	long long int x[n];
	for(int i=0;i<n;i++){
		cin>>x[i];
		if(x[i]>max)
			max=x[i];
			max_i=i;
	}
	for(int i=0;i<n;i++)
		if(x[i]>0){
			x[i]=-x[i]-1;
			k++;
		}
	if(k%2==0)
		for(int i=0;i<n;i++)
			cout<<x[i]<<" ";
	else{
		x[max_i]=max;
		for(int i=0;i<n;i++)
			cout<<x[i]<<" ";
	}

}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,ma=-1000000,max_i=0,k=0;
	cin>>n;
	int x[n];
	for(int i=0;i<n;i++){
		cin>>x[i];
		if(x[i]>max)
			ma=x[i];
			max_i=i;
	}
	for(int i=0;i<n;i++)
		if(x[i]>=0){
			x[i]=-x[i]-1;
			k++;
		}
	if(k%2==0)
		for(int i=0;i<n;i++)
			cout<<x[i]<<" ";
	else{
		x[max_i]=ma;
		for(int i=0;i<n;i++)
			cout<<x[i]<<" ";
	}

}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,proi=1,mi=1000001,min_i=n+1;
	cin>>n;
	int x[n],y[n],z[n];
	for(int i=0;i<n;i++){
		cin>>x[i];
		y[i]=-x[i]-1;
		if(mi>y[i]){
			mi=y[i];
			min_i=i;
		}
		if(abs(x[i])>abs(y[i]))
			z[i]=x[i];
		else
			z[i]=y[i];
	}
	for(int i=0;i<n;i++)
		proi=proi*z[i];
	if(proi>0)
		for(int i=0;i<n;i++)
			cout<<z[i]<<" ";
	else{
		z[min_i]=-z[min_i]-1;
		for(int i=0;i<n;i++)
			cout<<z[i]<<" ";
	}

}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,proi=1,mi=1000000,min_i=n;
	cin>>n;
	int x[n],y[n],z[n];
	for(int i=0;i<n;i++){
		cin>>x[i];
		y[i]=-x[i]-1;
		if(mi>y[i]){
			mi=y[i];
			min_i=i;
		}
		if(abs(x[i])>abs(y[i]))
			z[i]=x[i];
		else
			z[i]=y[i];
	}
	for(int i=0;i<n;i++)
		proi=proi*z[i];
	if(proi>0)
		for(int i=0;i<n;i++)
			cout<<z[i]<<" ";
	else{
		z[min_i]=-z[min_i]-1;
		for(int i=0;i<n;i++)
			cout<<z[i]<<" ";
	}

}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,proi=1,max=-1000000,max_i=-1,k=0;
	cin>>n;
	int x[n];
	for(int i=0;i<n;i++){
		cin>>x[i];
		if(x[i]>max)
			max=x[i];
			max_i=i;
	}
	for(int i=0;i<n;i++)
		if(x[i]>0){
			x[i]=-x[i]-1;
			k++;
		}
	if(k%2==0)
		for(int i=0;i<n;i++)
			cout<<x[i]<<" ";
	else{
		x[max_i]=max;
		for(int i=0;i<n;i++)
			cout<<x[i]<<" ";
	}

}


#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
typedef long long int ll;

typedef set<ll>::iterator iets ;
typedef vector<ll>::iterator ietv;
typedef vector<ll> V;
int main()
{
   // ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    int t=1;
	//cin>>t;
    while(t--)
    {
    	ll i,y,h,j,k,r=0,l=0,n=0,m=0,x=0,o=0,cnt1=INT_MAX,cnt2=0,cnt3=0,cnt4=0,min1=INT_MAX;
 	   	cin>>n;
 	   	ll a[n];
 	   	for(i=0;i<n;i++)
 	   	{
 	   	   cin>>a[i];
			   min1=min(min1,a[i]);
	    }
	    l=1;
	    if(n%2==0)
	    {
	    	for(i=0;i<n;i++)
	    	{
	    		if(a[i]>=0)
	    		cout<<-a[i]-1<<" ";
	    		else
	    		cout<<a[i]<<" ";
			}
		}
		else
		{
			for(i=0;i<n;i++)
			{
				if(a[i]>=0)
				a[i]=-a[i]-1;
				cnt1=min(cnt1,a[i]);
			}
			for(i=0;i<n;i++)
			{
				if(a[i]!=cnt1)
				cout<<a[i]<<" ";
				else
				{
					cout<<-a[i]-1<<" ";
					i++;
					break;
				}
			}
			for(;i<n;i++)
			cout<<a[i]<<" ";
		}

	}
}


#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
typedef long long int ll;

typedef set<ll>::iterator iets ;
typedef vector<ll>::iterator ietv;
typedef vector<ll> V;
int main()
{
   // ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    int t=1;
	//cin>>t;
    while(t--)
    {
    	ll i,y,h,j,k,r=0,l=0,n=0,m=0,x=0,o=0,cnt1=INT_MAX,cnt2=0,cnt3=0,cnt4=0,min1=INT_MIN;
 	   	cin>>n;
 	   	ll a[n];
 	   	for(i=0;i<n;i++)
 	   	{
 	   	   cin>>a[i];
			   min1=min(min1,a[i]);
	    }
	    l=1;
	    if(n%2==0)
	    {
	    	for(i=0;i<n;i++)
	    	{
	    		if(a[i]>=0)
	    		cout<<-a[i]-1<<" ";
	    		else
	    		cout<<a[i]<<" ";
			}
		}
		else
		{
			for(i=0;i<n;i++)
			{
				a[i]=-a[i]-1;
				cnt1=min(cnt1,a[i]);
			}
			for(i=0;i<n;i++)
			{
				if(a[i]!=cnt1)
				cout<<a[i]<<" ";
				else
				{
					cout<<-a[i]-1;
					i++;
					break;
				}
			}
			for(;i<n;i++)
			cout<<a[i]<<" ";
		}

	}
}


#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
typedef long long int ll;

typedef set<ll>::iterator iets ;
typedef vector<ll>::iterator ietv;
int main()
{
   // ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    int t=1;
//	cin>>t;
    while(t--)
    {
    	ll i,y,h,j,k,r=0,l=2,n,m,x=0,o=0,cnt1=INT_MAX,cnt2=INT_MIN,cnt3=0,cnt4=0;
    	cin>>n;
    	ll a[n];
    	vector<ll> b,c;

    	for(i=0;i<n;i++)
    	{
    		cin>>a[i];
    		if(n==1&&a[0]==0)
    	{
    		cout<<"0";
    		return 0;
		}
    		if(a[i]<0)
    		{
    			cnt2++;
			}
    		else
    		cnt3++;
		}
			for(i=0;i<n;i++)
			{
				if(a[i]>0)
				{
					a[i]=-1*a[i]-1;
				}
			}
		if(n%2==0)
		{
			for(i=0;i<n;i++)
			cout<<a[i]<<" ";
		}
		else
		{
			for(i=0;i<n;i++)
			{
				cnt2=max(cnt2,a[i]);
			}
			for(i=0;i<n;i++)
			{
				if(a[i]==cnt2)
				{
					a[i]=-1*a[i]-1;
					break;
				}
			}

			for(i=0;i<n;i++)
			cout<<a[i]<<" ";

		}
	}

}


#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
typedef long long int ll;

typedef set<ll>::iterator iets ;
typedef vector<ll>::iterator ietv;
int main()
{
   // ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    int t=1;
	//cin>>t;
    while(t--)
    {
    	ll i,y,h,j,k,r=0,l=0,n=0,m=0,x=0,o=0,cnt1=INT_MAX,cnt2=0,cnt3=0,cnt4=0;
 	   	cin>>n;
 	   	ll a[n];
 	   	for(i=0;i<n;i++)
 	   	{
 	   	   cin>>a[i];
	    }
	    l=1;
	    for(i=0;i<n;i++)
	    {
		  if(a[i]>=0)
		  a[i]=-1*a[i]-1;
		  l=l*a[i];
			}
		if(l>0)
		{
			for(i=0;i<n;i++)
			cout<<a[i]<<" ";
		}
		else
		{
			ll *it;
		//	cout<<"hey\n";
			it=min_element(a,a+n);
		//	cout<<*it<<endl;
			for(i=0;i<n;i++)
			{
				if(a[i]!=*it)
				{
					cout<<a[i]<<" ";
				}
				else
				{
					a[i]=-1*a[i]-1;
					cout<<a[i]<<" ";
					break;
				}
			}
			for(j=i+1;j<n;j++)
			cout<<a[j]<<" ";
		}

	}
}


#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
typedef long long int ll;

typedef set<ll>::iterator iets ;
typedef vector<ll>::iterator ietv;
typedef vector<ll> V;
int main()
{
   // ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    int t=1;
	//cin>>t;
    while(t--)
    {
    	ll i,y,h,j,k,r=0,l=0,n=0,m=0,x=0,o=0,cnt1=INT_MAX,cnt2=0,cnt3=0,cnt4=0,min1=INT_MIN;
 	   	cin>>n;
 	   	ll a[n];
 	   	for(i=0;i<n;i++)
 	   	{
 	   	   cin>>a[i];
			   min1=min(min1,a[i]);
	    }
	    l=1;
	    if(n%2==0)
	    {
	    	for(i=0;i<n;i++)
	    	{
	    		if(a[i]>=0)
	    		cout<<-a[i]-1<<" ";
	    		else
	    		cout<<a[i]<<" ";
			}
		}
		else
		{
			for(i=0;i<n;i++)
			{
				if(a[i]>=0)
				a[i]=-a[i]-1;
				cnt1=min(cnt1,a[i]);
			}
			for(i=0;i<n;i++)
			{
				if(a[i]!=cnt1)
				cout<<a[i]<<" ";
				else
				{
					cout<<-a[i]-1<<" ";
					i++;
					break;
				}
			}
			for(;i<n;i++)
			cout<<a[i]<<" ";
		}

	}
}


#include <iostream>
#include <bits/stdc++.h>
#include<vector>
using namespace std;

int main() {
	long long int n;
	long long int input;
	int flag=0;
	long long int max= INT_MAX;
	long long int ind;
	long long int no_positive=0;
	cin>>n;
	vector<long long int> list;
	for(long long int i=0;i<n;i++){
	    cin>>input;
	    list.push_back(input);
	    if(input>=0){no_positive++;}
	}

	for(long long int i=0;i<n;i++){
	    if(list[i]>=0){list[i]=-list[i]-1;}
	}
	for(long long int i=0;i<n;i++){
	    if(list[i]<max){max=list[i];ind=i;}
	}
	if(n%2!=0){list[ind]=-list[ind]-1;}

	for(long long int i=0;i<n;i++){
	    cout<<list[i]<<" ";
	}

	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include<vector>
using namespace std;

int main() {
	long long int n;
	long long int input;
	int flag=0;
	int max= INT_MIN;
	long long int no_positive=0;
	cin>>n;
	vector<long long int> list;
	for(int i=0;i<n;i++){
	    cin>>input;
	    list.push_back(input);
	    if(input>=0){no_positive++;}
	}
	for(int i=0;i<n;i++){
	    if(list[i]>max)max=list[i];
	}
	if((n-no_positive)%2==0){
	    if(no_positive%2==0){
	        for(int i=0;i<n;i++){
	            if(list[i]>=0)list[i]=-list[i]-1;
	        }
	    }
	    else{
	        for(int i=0;i<n;i++){
	            if(n>1){
	                if (list[i]==max && flag==0){flag=1;list[i]=-list[i]-1;}
	                else if (list[i]>=0  )list[i]=-list[i]-1;
	            }
	        }
	    }
	}
	else{
	    if(no_positive%2==0){
	        for(int i=0;i<n;i++){

	            if (list[i]==max && flag==0){flag=1;list[i]=-list[i]-1;}
	            else if (list[i]>=0  )list[i]=-list[i]-1;
	        }
	    }
	    else{
	        for(int i=0;i<n;i++){
	            if(list[i]>=0)list[i]=-list[i]-1;
	        }
	    }
	}
	for(int i=0;i<n;i++){
	    cout<<list[i]<<" ";
	}

	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include<vector>
using namespace std;

int main() {
	long long int n;
	long long int input;
	int flag=0;
	int max= INT_MIN;
	long long int no_positive=0;
	cin>>n;
	vector<long long int> list;
	for(int i=0;i<n;i++){
	    cin>>input;
	    list.push_back(input);
	    if(input>=0){no_positive++;}
	}
	for(int i=0;i<n;i++){
	    if(list[i]>max)max=list[i];
	}
	if((n-no_positive)%2==0){
	    if(no_positive%2==0){
	        for(int i=0;i<n;i++){
	            if(list[i]>=0)list[i]=-list[i]-1;
	        }
	    }
	    else{
	        for(int i=0;i<n;i++){

	            if (list[i]==max && flag==0){flag=1;list[i]=-list[i]-1;}
	            else if (list[i]>=0  )list[i]=-list[i]-1;

	        }
	    }
	}
	else{
	    if(no_positive%2==0){
	        for(int i=0;i<n;i++){

	            if (list[i]==max && flag==0){flag=1;list[i]=-list[i]-1;}
	            else if (list[i]>=0  )list[i]=-list[i]-1;
	        }
	    }
	    else{
	        for(int i=0;i<n;i++){
	            if(list[i]>=0)list[i]=-list[i]-1;
	        }
	    }
	}
	for(int i=0;i<n;i++){
	    cout<<list[i]<<" ";
	}

	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include<vector>
using namespace std;

int main() {
	long long int n;
	long long int input;
	int flag;
	long long int no_positive=0;
	cin>>n;
	vector<long long int> list;
	for(int i=0;i<n;i++){
	    cin>>input;
	    list.push_back(input);
	    if(input>=0){no_positive++;}
	}
	sort(list.begin(),list.end());
	if((n-no_positive)%2==0){
	    if(no_positive%2==0){
	        for(int i=0;i<n;i++){
	            if(list[i]>=0)list[i]=-list[i]-1;
	        }
	    }
	    else{
	        for(int i=0;i<n-1;i++){
	            if(list[i]>=0)list[i]=-list[i]-1;
	        }
	    }
	}
	else{
	    if(no_positive%2==0){
	        for(int i=0;i<n-1;i++){
	            if(list[i]>=0)list[i]=-list[i]-1;
	        }
	    }
	    else{
	        for(int i=0;i<n;i++){
	            if(list[i]>=0)list[i]=-list[i]-1;
	        }
	    }
	}
	for(int i=0;i<n;i++){
	    cout<<list[i]<<" ";
	}

	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include<vector>
using namespace std;

int main() {
	long long int n;
	long long int input;
	int flag=0;
	int max= INT_MIN;
	long long int no_positive=0;
	cin>>n;
	vector<long long int> list;
	for(int i=0;i<n;i++){
	    cin>>input;
	    list.push_back(input);
	    if(input>=0){no_positive++;}
	}
	for(int i=0;i<n;i++){
	    if(list[i]>max)max=list[i];
	}
	if((n-no_positive)%2==0){
	    if(no_positive%2==0){
	        for(int i=0;i<n;i++){
	            if(list[i]>=0)list[i]=-list[i]-1;
	        }
	    }
	    else{
	        for(int i=0;i<n;i++){
	            if(no_positive>1){
	                if (list[i]==max && flag==0){flag=1;list[i]=-list[i]-1;}
	                else if (list[i]>=0  )list[i]=-list[i]-1;
	            }
	        }
	    }
	}
	else{
	    if(no_positive%2==0){
	        for(int i=0;i<n;i++){

	            if (list[i]==max && flag==0){flag=1;list[i]=-list[i]-1;}
	            else if (list[i]>=0  )list[i]=-list[i]-1;
	        }
	    }
	    else{
	        for(int i=0;i<n;i++){
	            if(list[i]>=0)list[i]=-list[i]-1;
	        }
	    }
	}
	for(int i=0;i<n;i++){
	    cout<<list[i]<<" ";
	}

	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include<vector>
using namespace std;

int main() {
	long long int n;
	long long int input;
	int flag=0;
	long long int max= INT_MIN;
	long long int ind;
	long long int no_positive=0;
	cin>>n;
	vector<long long int> list;
	for(long long int i=0;i<n;i++){
	    cin>>input;
	    list.push_back(input);
	    if(input>=0){no_positive++;}
	}
	for(long long int i=0;i<n;i++){
	    if(list[i]>max){max=list[i];ind=i;}
	}
	for(long long int i=0;i<n;i++){
	    if(list[i]>=0){list[i]=-list[i]-1;}
	}
	if(n%2!=0){list[ind]=-list[ind]-1;}

	for(long long int i=0;i<n;i++){
	    cout<<list[i]<<" ";
	}

	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include<vector>
using namespace std;

int main() {
	long long int n;
	long long int input;
	int flag=0;
	int max= INT_MIN;
	int ind;
	long long int no_positive=0;
	cin>>n;
	vector<long long int> list;
	for(int i=0;i<n;i++){
	    cin>>input;
	    list.push_back(input);
	    if(input>=0){no_positive++;}
	}
	for(int i=0;i<n;i++){
	    if(list[i]>max){max=list[i];ind=i;}
	}
	for(int i=0;i<n;i++){
	    if(list[i]>=0){list[i]=-list[i]-1;}
	}
	if(n%2!=0){list[ind]=-list[ind]-1;}

	for(int i=0;i<n;i++){
	    cout<<list[i]<<" ";
	}

	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include<vector>
using namespace std;

int main() {
	long long int n;
	long long int input;
	int flag=0;
	int max= INT_MIN;
	long long int no_positive=0;
	cin>>n;
	vector<long long int> list;
	for(int i=0;i<n;i++){
	    cin>>input;
	    list.push_back(input);
	    if(input>=0){no_positive++;}
	}
	for(int i=0;i<n;i++){
	    if(list[i]>max)max=list[i];
	}
	if((n-no_positive)%2==0){
	    if(no_positive%2==0){
	        for(int i=0;i<n;i++){
	            if(list[i]>=0)list[i]=-list[i]-1;
	        }
	    }
	    else{
	        for(int i=0;i<n;i++){
	            if((list[i]>=0 && list[i]!=max) || (list[i]==max&& flag==1) )list[i]=-list[i]-1;
	            else if(list[i]==max && flag==0){flag=1;}
	        }
	    }
	}
	else{
	    if(no_positive%2==0){
	        for(int i=0;i<n;i++){

	            if(list[i]>=0 || (list[i]==max&& flag==1) )list[i]=-list[i]-1;
	            else if(list[i]==max && flag==0){flag=1;}
	        }
	    }
	    else{
	        for(int i=0;i<n;i++){
	            if(list[i]>=0)list[i]=-list[i]-1;
	        }
	    }
	}
	for(int i=0;i<n;i++){
	    cout<<list[i]<<" ";
	}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 100010;
int a[maxn];
int b[maxn];
int main(){
	int n;
	cin >> n;
	int ansf = 0;
	int ansz = 0;
	int minl = 1000001;
	int j = -1;
	for(int i = 0;i<n;i++){
		scanf("%d",&a[i]);
		if(a[i]<0){
			ansf++;
		}
		else {
			ansz++;
			if(a[i]<minl&&a[i] != 0){
				minl = a[i];
				j = i;
			}
		}
		b[i] = -a[i]-1;
	}
	if(ansf%2 == 0&&ansz%2 == 0||ansf%2 != 0&&ansz%2 != 0){
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>0){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
	else {
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>0&&i != j){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
}

/*
   f bubina, z faxiang-1
*/


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 100010;
int a[maxn];
int b[maxn] = {0};
int main(){
	int n;
	cin >> n;
	int ansf = 0;
	int ansz = 0;
	int minl = 1000001;
	int j = -1;
	int flag = 0;
	for(int i = 0;i<n;i++){
		scanf("%d",&a[i]);
		if(a[i]<0){
			ansf++;
			flag = 1;
		}
		else if(a[i]>0){
			ansz++;
			if(a[i]<minl){
				if(a[i] == 0) flag = 1;
				minl = a[i];
				j = i;
			}
			b[i] = -a[i]-1;
			flag = 1;
		}
		else if(a[i] == 0){
			if(n != 1) {
				a[i] = -1;
				ansf++;
			}
		}
	}
	if(flag == 0&&n%2 != 0){
		a[0] = 0;
    }
	if(ansf%2 == 0&&ansz%2 == 0||ansf%2 != 0&&ansz%2 != 0){
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>0){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
	else {
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>0&&i != j){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
}

/*
   f bubina, z faxiang-1
*/


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 100010;
int a[maxn];
int b[maxn];
int main(){
	int n;
	cin >> n;
	int ansf = 0;
	int ansz = 0;
	int minl = 1000001;
	int j = -1;
	for(int i = 0;i<n;i++){
		scanf("%d",&a[i]);
		if(a[i]<0){
			ansf++;
		}
		else {
			ansz++;
			if(a[i]<minl&&a[i] != 0){
				minl = a[i];
				j = i;
			}
		}
		b[i] = -a[i]-1;
	}
	if(ansf%2 == 0&&ansz%2 == 0||ansf%2 != 0&&ansz%2 != 0){
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>=0){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
	else {
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>=0&&i != j&&j != -1){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
}

/*
   f bubina, z faxiang-1
*/


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 100010;
int a[maxn];
int main(){
	int n;
	cin >> n;
    for(int i = 0;i<n;i++){
    	cin >> a[i];
    	if(a[i]>=0){
    		a[i] = -a[i]-1;
		}
	}
	if(n%2 == 1){
		int minl = -1;
		int j = -1;
		for(int i = 0;i<n;i++){
			if(a[i]<minl){
				minl = a[i];
				j = i;
			}
		}
		if(j == -1){
			a[0] = 0;
		}
		else {
			a[j] = -a[j]-1;
		}
	}
	for(int i = 0;i<n;i++){
		if(i) cout << " ";
		cout << a[i];
	}
	cout << endl;
    return 0;
}

/*
   f bubina, z faxiang-1
*/


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 100010;
int a[maxn];
int b[maxn];
int main(){
	int n;
	cin >> n;
	int ansf = 0;
	int ansz = 0;
	int minl = 1000001;
	int j = -1;
	for(int i = 0;i<n;i++){
		scanf("%d",&a[i]);
		if(a[i]<0){
			ansf++;
		}
		else {
			ansz++;
			if(a[i]<minl){
				minl = a[i];
				j = i;
			}
		}
		b[i] = -a[i]-1;
	}
	if(ansf%2 == 0&&ansz%2 == 0||ansf%2 != 0&&ansz%2 != 0){
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>0){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
	else {
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>0&&i != j){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
}

/*
   f bubina, z faxiang-1
*/


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 100010;
int a[maxn];
int b[maxn] = {0};
int main(){
	int n;
	cin >> n;
	int ansf = 0;
	int ansz = 0;
	int minl = 10000001;
	int j = -1;
	int flag = 0;
	for(int i = 0;i<n;i++){
		scanf("%d",&a[i]);
		if(a[i]<0){
			ansf++;
			flag = 1;
		}
		else if(a[i]>0){
			ansz++;
			if(a[i]<=minl){
				if(a[i] == 0) flag = 1;
				minl = a[i];
				j = i;
			}
			b[i] = -a[i]-1;
			flag = 1;
		}
		else if(a[i] == 0){
			if(n != 1) {
				a[i] = -1;
				ansf++;
			}
		}
	}
	if(flag == 0&&n%2 != 0){
		a[0] = 0;

    }
	if(ansf%2 == 0&&ansz%2 == 0||ansf%2 != 0&&ansz%2 != 0){
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>0){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
	else {
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>0&&i != j){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
}

/*
   f bubina, z faxiang-1
*/


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 10010;
int a[maxn];
int b[maxn];
int main(){
	int n;
	cin >> n;
	int ansf = 0;
	int ansz = 0;
	int minl = 1000001;
	int j = -1;
	for(int i = 0;i<n;i++){
		scanf("%d",&a[i]);
		if(a[i]<0){
			ansf++;
		}
		else {
			ansz++;
			if(a[i]<minl){
				minl = a[i];
				j = i;
			}
		}
		b[i] = -a[i]-1;
	}
	if(ansf%2 == 0&&ansz%2 == 0||ansf%2 != 0&&ansz%2 != 0){
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>0){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
	else {
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>0&&i != j){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
}

/*
   f bubina, z faxiang-1
*/


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 100010;
int a[maxn];
int b[maxn];
int main(){
	int n;
	cin >> n;
	int ansf = 0;
	int ansz = 0;
	int minl = 1000001;
	int j = -1;
	for(int i = 0;i<n;i++){
		scanf("%d",&a[i]);
		if(a[i]<0){
			ansf++;
		}
		else {
			ansz++;
			if(a[i]<minl&&a[i] != 0){
				minl = a[i];
				j = i;
			}
		}
		b[i] = -a[i]-1;
	}
	if(ansf%2 == 0&&ansz%2 == 0||ansf%2 != 0&&ansz%2 != 0){
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>0){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
	else {
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>=0&&i != j){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
}

/*
   f bubina, z faxiang-1
*/


#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10010;
int a[maxn];
int b[maxn];
int main(){
	int n;
	cin >> n;
	int ansf = 0;
	int ansz = 0;
	int minl = 1000001;
	int j = -1;
	for(int i = 0;i<n;i++){
		cin >> a[i];
		if(a[i]<0){
			ansf++;
		}
		else {
			ansz++;
			if(a[i]<minl){
				minl = a[i];
				j = i;
			}
		}
		b[i] = -a[i]-1;
	}
	if(ansf%2 == 0&&ansz%2 == 0||ansf%2 != 0&&ansz%2 != 0){
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>0){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
	else {
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>0&&i != j){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
}

/*
   f bubina, z faxiang-1
*/


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 100010;
int a[maxn];
int b[maxn];
int main(){
	int n;
	cin >> n;
	int ansf = 0;
	int ansz = 0;
	int minl = 1000001;
	int j = -1;
	int flag = 0;
	for(int i = 0;i<n;i++){
		scanf("%d",&a[i]);
		if(a[i]<0){
			ansf++;
		}
		else {
			ansz++;
			if(a[i]<minl&&a[i] != 0||a[i]<minl&&a[i] == 0&&flag == 0){
				if(a[i] == 0) flag = 1;
				minl = a[i];
				j = i;
			}
		}
		b[i] = -a[i]-1;
	}
	if(ansf%2 == 0&&ansz%2 == 0||ansf%2 != 0&&ansz%2 != 0){
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>=0){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
	else {
		for(int i = 0;i<n;i++){
			if(i) cout << " ";
			if(a[i]>=0&&i != j){
				cout << b[i];
			}
			else {
				cout << a[i];
			}
		}
		cout << endl;
		return 0;
	}
}

/*
   f bubina, z faxiang-1
*/


#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
#define pb push_back

int main()
{
    int n, a, f = 0;
    cin >> n;

    vector <int> v, v1;

    for(int i = 0; i < n; i++)
    {
        cin >> a;
        if(a >= 0)
            a = -a-1;
        v.pb(a);
    }
    if(v.size()%2 == 0)
    {
        for(int i = 0; i < v.size(); i++)
        {
            cout << v[i] << " ";
        }
    }
    else
    {
        sort(v.begin(), v.end());
        for(int i = 0; i < v.size(); i++)
        {
            if(v[0] == v[i] && f == 0)
            {
                v[i] = -v[i]-1;
                f=1;
            }
            cout << v[i] << " ";
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
#define pb push_back

int main()
{
    int n, a, flag=0;
    cin >> n;

    vector <int> v, v1;

    for(int i = 0; i < n; i++)
    {
        cin >> a;
        if(a >= 0)
            a = -a-1;
        v.pb(a);
    }
    if(v.size()%2 == 0)
    {
        for(int i = 0; i < v.size(); i++)
        {
            cout << v[i] << " ";
        }
    }
    else
    {
        sort(v.begin(), v.end());
        for(int i = 0; i < v.size(); i++)
        {
            if(i == 0)
            {
                v[i] = -v[i]-1;
            }
            cout << v[i] << " ";
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
#define pb push_back

int main()
{
    LL n, a, f = 0;
    cin >> n;

    vector <int> v, v1;

    for(int i = 0; i < n; i++)
    {
        cin >> a;
        if(a >= 0)
            a = -a-1;
        v.pb(a);
    }
    if(v.size()%2 == 0)
    {
        for(int i = 0; i < v.size(); i++)
        {
            cout << v[i] << " ";
        }
    }
    else
    {
        sort(v.begin(), v.end());
        for(int i = 0; i < v.size(); i++)
        {
            if(v[0] == v[i])
            {
                v[i] = -v[i]-1;
                f=1;
            }
            cout << v[i] << " ";
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
#define pb push_back

int main()
{
    int n, a, f = 0, mn = 1e8, idx = 0;
    cin >> n;

    vector <int> v, v1;

    for(int i = 0; i < n; i++)
    {
        cin >> a;
        if(a >= 0)
            a = -a-1;
        v.pb(a);
    }
    if(v.size()%2 == 0)
    {
        for(int i = 0; i < v.size(); i++)
        {
            cout << v[i] << " ";
        }
    }
    else
    {
        //sort(v.begin(), v.end());
        for(int i = 0; i < v.size(); i++)
        {
            if(v[i] < mn)
            {
                mn = v[i];
                idx = i;
            }
        }
        for(int i = 0; i < v.size(); i++)
        {
            if(i == idx)
            {
                v[i] = -v[i]-1;
            }
            cout << v[i] << " ";
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
#define pb push_back

int main()
{
    LL n, a, flag=0;
    cin >> n;

    vector <int> v, v1;

    for(int i = 0; i < n; i++)
    {
        cin >> a;
        if(a >= 0)
            a = -a-1;
        v.pb(a);
    }
    if(v.size()%2 == 0)
    {
        for(int i = 0; i < v.size(); i++)
        {
            cout << v[i] << " ";
        }
    }
    else
    {
        sort(v.begin(), v.end());
        for(int i = 0; i < v.size(); i++)
        {
            if(i == 0)
            {
                v[i] = -v[i]-1;
            }
            cout << v[i] << " ";
        }
    }

    return 0;
}


import java.util.*;
import java.io.*;
import java.math.*;

public class Main
{

    public static void process()throws IOException
    {
        int n=ni(),q=ni(),mx_idx=1;
        int arr[]=new int[n+1];
        for(int i=1;i<=n;i++) arr[i]=ni();
        for(int i=1;i<=n;i++)
            if(arr[i]>mx_idx)
                mx_idx=i;

        String ans[]=new String[n+1];

        ArrayList<Integer> li = new ArrayList<>();
        for(int i=mx_idx+1;i<=n;i++) li.add(arr[i]);

        int take=1;
        for(int i=1;i<mx_idx;i++){
            ans[i]=arr[take]+" "+arr[i+1];

            if(arr[take]<arr[i+1]){
                li.add(arr[take]);
                take=i+1;
            }
            else
                li.add(arr[i+1]);
        }

        // if(mx_idx==n)
        //     ans[mx_idx]=arr[mx_idx]+" "+li.get(0);
        // else
        //     ans[mx_idx]=arr[mx_idx]+" "+arr[mx_idx+1];

        //System.out.println(li);System.out.println(Arrays.toString(ans));

        while(q-- > 0){
            int x=ni();
            if(x<mx_idx)
                pn(ans[x]);
            else{
                x=(x-mx_idx)%(n-1);
                pn(arr[mx_idx]+" "+li.get(x));
            }
        }
    }

    static FastReader sc;
    static PrintWriter out;
    public static void main(String[]args)throws IOException
    {
        out = new PrintWriter(System.out);
        sc=new FastReader();

        long s = System.currentTimeMillis();
        int t=1;
        //t=ni();
        while(t-->0)
            process();

        out.flush();
        System.err.println(System.currentTimeMillis()-s+"ms");
    }

    static void pn(Object o){out.println(o);}
    static void p(Object o){out.print(o);}
    static int ni()throws IOException{return Integer.parseInt(sc.next());}
    static long nl()throws IOException{return Long.parseLong(sc.next());}
    static double nd()throws IOException{return Double.parseDouble(sc.next());}
    static String nln()throws IOException{return sc.nextLine();}
    static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}

    static long mod=(long)1e9+7l;

    static<T> void r_sort(T arr[],int n){
        Random r = new Random();
        for (int i = n-1; i > 0; i--){
            int j = r.nextInt(i+1);

            T temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        Arrays.sort(arr);
    }

/////////////////////////////////////////////////////////////////////////////////////////////////////////
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;

        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next(){
            while (st == null || !st.hasMoreElements()){
                try{ st = new StringTokenizer(br.readLine()); } catch (IOException  e){ e.printStackTrace(); }
            }
            return st.nextToken();
        }

        String nextLine(){
            String str = "";
            try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }
            return str;
        }
    }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
}


import java.util.*;
import java.io.*;
import java.math.*;

public class Main
{

    public static void process()throws IOException
    {
        int n=ni(),q=ni(),mx_idx=1;
        int arr[]=new int[n+1];
        for(int i=1;i<=n;i++) arr[i]=ni();
        for(int i=1;i<=n;i++)
            if(arr[i]>arr[mx_idx])
                mx_idx=i;

        String ans[]=new String[n+1];

        ArrayList<Integer> li = new ArrayList<>();
        for(int i=mx_idx+1;i<=n;i++) li.add(arr[i]);

        int take=1;
        for(int i=1;i<mx_idx;i++){
            ans[i]=arr[take]+" "+arr[i+1];

            if(arr[take]<arr[i+1]){
                li.add(arr[take]);
                take=i+1;
            }
            else
                li.add(arr[i+1]);
        }

        // if(mx_idx==n)
        //     ans[mx_idx]=arr[mx_idx]+" "+li.get(0);
        // else
        //     ans[mx_idx]=arr[mx_idx]+" "+arr[mx_idx+1];

        //System.out.println(li);System.out.println(Arrays.toString(ans));
        //System.out.println("max ele "+arr[mx_idx]);
        while(q-- > 0){
            long x=nl();
            if(x<mx_idx)
                pn(ans[(int)x]);
            else{
                x=(x-mx_idx)%(n-1);
                pn(arr[mx_idx]+" "+li.get((int)x));
            }
        }
    }

    static FastReader sc;
    static PrintWriter out;
    public static void main(String[]args)throws IOException
    {
        out = new PrintWriter(System.out);
        sc=new FastReader();

        long s = System.currentTimeMillis();
        int t=1;
        //t=ni();
        while(t-->0)
            process();

        out.flush();
        System.err.println(System.currentTimeMillis()-s+"ms");
    }

    static void pn(Object o){out.println(o);}
    static void p(Object o){out.print(o);}
    static int ni()throws IOException{return Integer.parseInt(sc.next());}
    static long nl()throws IOException{return Long.parseLong(sc.next());}
    static double nd()throws IOException{return Double.parseDouble(sc.next());}
    static String nln()throws IOException{return sc.nextLine();}
    static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}

    static long mod=(long)1e9+7l;

    static<T> void r_sort(T arr[],int n){
        Random r = new Random();
        for (int i = n-1; i > 0; i--){
            int j = r.nextInt(i+1);

            T temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        Arrays.sort(arr);
    }

/////////////////////////////////////////////////////////////////////////////////////////////////////////
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;

        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next(){
            while (st == null || !st.hasMoreElements()){
                try{ st = new StringTokenizer(br.readLine()); } catch (IOException  e){ e.printStackTrace(); }
            }
            return st.nextToken();
        }

        String nextLine(){
            String str = "";
            try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }
            return str;
        }
    }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
}


import java.util.*;
import java.io.*;
import java.math.*;

public class Main
{

    public static void process()throws IOException
    {
        int n=ni(),q=ni(),mx_idx=1;
        int arr[]=new int[n+1];
        for(int i=1;i<=n;i++) arr[i]=ni();
        for(int i=1;i<=n;i++)
            if(arr[i]>mx_idx)
                mx_idx=i;

        String ans[]=new String[n+1];

        ArrayList<Integer> li = new ArrayList<>();
        for(int i=mx_idx+1;i<=n;i++) li.add(arr[i]);

        int take=1;
        for(int i=1;i<mx_idx;i++){
            ans[i]=arr[take]+" "+arr[i+1];

            if(arr[take]<arr[i+1]){
                li.add(arr[take]);
                take=i+1;
            }
            else
                li.add(arr[i+1]);
        }

        // if(mx_idx==n)
        //     ans[mx_idx]=arr[mx_idx]+" "+li.get(0);
        // else
        //     ans[mx_idx]=arr[mx_idx]+" "+arr[mx_idx+1];

        //System.out.println(li);System.out.println(Arrays.toString(ans));

        while(q-- > 0){
            long x=nl();
            if(x<mx_idx)
                pn(ans[(int)x]);
            else{
                x=(x-mx_idx)%(n-1);
                pn(arr[mx_idx]+" "+li.get((int)x));
            }
        }
    }

    static FastReader sc;
    static PrintWriter out;
    public static void main(String[]args)throws IOException
    {
        out = new PrintWriter(System.out);
        sc=new FastReader();

        long s = System.currentTimeMillis();
        int t=1;
        //t=ni();
        while(t-->0)
            process();

        out.flush();
        System.err.println(System.currentTimeMillis()-s+"ms");
    }

    static void pn(Object o){out.println(o);}
    static void p(Object o){out.print(o);}
    static int ni()throws IOException{return Integer.parseInt(sc.next());}
    static long nl()throws IOException{return Long.parseLong(sc.next());}
    static double nd()throws IOException{return Double.parseDouble(sc.next());}
    static String nln()throws IOException{return sc.nextLine();}
    static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}

    static long mod=(long)1e9+7l;

    static<T> void r_sort(T arr[],int n){
        Random r = new Random();
        for (int i = n-1; i > 0; i--){
            int j = r.nextInt(i+1);

            T temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        Arrays.sort(arr);
    }

/////////////////////////////////////////////////////////////////////////////////////////////////////////
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;

        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next(){
            while (st == null || !st.hasMoreElements()){
                try{ st = new StringTokenizer(br.readLine()); } catch (IOException  e){ e.printStackTrace(); }
            }
            return st.nextToken();
        }

        String nextLine(){
            String str = "";
            try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }
            return str;
        }
    }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
}


// practice with kaiboy
import java.io.*;
import java.util.*;

public class CF1180C extends PrintWriter {
	CF1180C() { super(System.out); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		CF1180C o = new CF1180C(); o.main(); o.flush();
	}

	void main() {
		int n = sc.nextInt();
		int q = sc.nextInt();
		int[] qu = new int[n * 3];
		for (int i = 0; i < n; i++)
			qu[i] = sc.nextInt();
		int i_ = 0;
		for (int i = 1; i < n; i++)
			if (qu[i_] < qu[i])
				i_ = i;
		int[] aa = new int[n * 2];
		int[] bb = new int[n * 2];
		for (int i = 0; i < n * 2; i++) {
			int a = aa[i] = qu[i];
			int b = bb[i] = qu[i + 1];
			if (a < b) {
				int tmp = a; a = b; b = tmp;
			}
			qu[i + 1] = a;
			qu[i + n] = b;
		}
		while (q-- > 0) {
			int i = sc.nextInt() - 1;
			if (i >= i_)
				i = i_ + (i - i_) % (n - 1);
			println(aa[i] + " " + bb[i]);
		}
	}
}


// practice with kaiboy
import java.io.*;
import java.util.*;

public class CF1180C extends PrintWriter {
	CF1180C() { super(System.out); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		CF1180C o = new CF1180C(); o.main(); o.flush();
	}

	void main() {
		int n = sc.nextInt();
		int q = sc.nextInt();
		int[] qu = new int[n * 3];
		for (int i = 0; i < n; i++)
			qu[i] = sc.nextInt();
		int i_ = 0;
		for (int i = 1; i < n; i++)
			if (qu[i_] < qu[i])
				i_ = i;
		int[] aa = new int[n * 2];
		int[] bb = new int[n * 2];
		for (int i = 0; i < n * 2; i++) {
			int a = aa[i] = qu[i];
			int b = bb[i] = qu[i + 1];
			if (a < b) {
				int tmp = a; a = b; b = tmp;
			}
			qu[i + 1] = a;
			qu[i + n] = b;
		}
		while (q-- > 0) {
			long m = sc.nextLong() - 1;
			int i = (int) (m <= i_ ? m : i_ + (m - i_) % (n - 1));
			println(aa[i] + " " + bb[i]);
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Collections;
public class JavaApplication20 {
    public static void main(String[] args) {
        FastReader read = new FastReader();
        int n = read.nextInt();
        ArrayList<Integer> array = new ArrayList<Integer>();
        int q = read.nextInt();
        HashMap<Long, int[]> ans = new HashMap<Long, int[]>();
        for(int i = 0; i<n; i++){
            array.add(read.nextInt());
        }
        for(long j = 0; j<n - 1; j++){
            int arr[] = new int[2];
            arr[0] = array.get(0);
            arr[1] = array.get(1);
            ans.put(j+1, arr);
            if(array.get(0) > array.get(1)){
               array.add(array.get(1));
                array.remove(1);
            }
            else{
                array.add(array.get(0));
                array.remove(0);
            }
        }
        for(int i = 0; i<q; i++){
            long num = read.nextLong();
            if(num == n){
                System.out.println(array.get(0) + " " + array.get(1));
            }
            else if(num>n){
                long mod1 = num%(n-1);
                int mod = (int) mod1;
                if (mod == 0){
                    mod = n-1;
                }
                System.out.println(array.get(0) + " " + array.get(mod));
            }
            else{
                System.out.println(ans.get(num)[0] + " " + ans.get(num)[1]);
            }
            }
        }
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.util.*;
import java.io.*;

public class Mamo {
    static long mod=1000000007;
    static Reader in=new Reader();
    static List<Integer >G[];
    static long a[],p[],xp[],xv[];
    static StringBuilder Sd=new StringBuilder(),Sl=new StringBuilder();
    public static void main(String [] args) {
    	//Dir by MohammedElkady
    	int n=in.nextInt(),q=in.nextInt(),po=0,max=0;
    	ArrayList<Integer> a=new ArrayList();

    	for(int i=0;i<n;i++) {
    		a.add(in.nextInt());
    		if(a.get(i)>max) {max=a.get(i);po=i;}}
    	String S[]=new String [n];
    	for(int i=0;i<po;i++) {
    		S[i]=String.valueOf(a.get(0)+" "+a.get(1));
    		if(a.get(0)>a.get(1)) {
    			int x=a.remove(1);
                a.add(x);
    		}
    		else {	int x=a.remove(0);
            a.add(x);}
    	}
    	a.remove(0);
    	while(q-->0) {
    		long v=in.l();
    		if(v>po) {
    			out.append(max+" ");
    			v-=po;
    			v--;
    			v%=a.size();

    			out.append(a.get((int)(v%a.size()))+"\n");
    		}
    		else {out.append(S[(int)v-1]+"\n");}
    	}
    	out.close();
    }

    static long ans=0L;
    static boolean v[];
    static ArrayList<Integer>res;
    static  Queue <Integer> pop;
    static  Stack <Integer>rem;
    public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
    static long gcd(long g,long x){if(x<1)return g;else return gcd(x,g%x);}
    static class Reader
    {
        private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public Reader() { this(System.in); }public Reader(InputStream is) { mIs = is;}
        public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];}
        public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;}
        public String s(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();}
        public long l(){int c = read();while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }long res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read();}while(!isSpaceChar(c));return res * sgn;}
        public int nextInt(){int c = read() ;while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }int res = 0;do{if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read() ;}while(!isSpaceChar(c));return res * sgn;}
        public double d() throws IOException {return Double.parseDouble(s()) ;}
        public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; }
        public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; }
        public int[] arr(int n){int[] ret = new int[n];for (int i = 0; i < n; i++) {ret[i] = nextInt();}return ret;}
    }
}

class stree{
	public stree(int n,long a[]) {
		this.a=a;
		seg=new long[n*4];
		lazy=new long[n*4];
		build(1,0,n-1);
	}
	long seg[], a[],lazy[];
	    void check(int p,int s,int e) {
	        if(lazy[p]!=0) {
	            seg[p] += lazy[p];
	            if(s!=e) {
	                lazy[2*p] += lazy[p];
	                lazy[2*p+1] += lazy[p];
	            }
	            lazy[p] = 0;
	        }
	    }

	     void build(int p,int s,int e) {
	        check(p,s,e);

	        if(s==e) {
	            seg[p] = a[s];
	            return;
	        }

	        build(2*p,s,(s+e)/2);
	        build(2*p+1,(s+e)/2+1,e);

	        seg[p] = Math.max(seg[2*p], seg[2*p+1]);
	    }

	   void update(int p,int s,int e,int i,int v) {
	        check(p,s,e);

	        if(s==e) {
	            seg[p] = v;
	            return;
	        }

	        if(i<=(s+e)/2) update(2*p,s,(s+e)/2,i,v);
	        else update(2*p+1,(s+e)/2+1,e,i,v);

	        seg[p] = Math.max(seg[2*p],seg[2*p+1]);
	    }

	     void update(int p,int s,int e,int a,int b,int v) {
	        check(p,s,e);

	        if(s>=a && e<=b) {
	            seg[p] += v;

	            if(s!=e) {
	                lazy[2*p] += v;
	                lazy[2*p+1] += v;
	            }

	            return;
	        }

	        if(s>b || e<a)
	            return;

	        update(2*p,s,(s+e)/2,a,b,v);
	        update(2*p+1,(s+e)/2+1,e,a,b,v);

	        seg[p] = Math.max(seg[2*p],seg[2*p+1]);
	    }

    long get(int p,int s,int e,int a,int b) {
        if(s>=a && e<=b)
            return seg[p];

        if(s>b || e<a)
            return Long.MIN_VALUE;

        return Math.max(get(2*p,s,(s+e)/2,a,b), get(2*p+1,(s+e)/2+1,e,a,b));
    }
}
class node implements Comparable<node>{
int a, b;
node(int tt,int ll){
a=tt;b=ll;
}
    @Override
    public int compareTo(node o) {
    return b-o.b;
    }
}
class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
}

class Sorting{

 public static long[] bucketSort(long[] array, int bucketCount) {

 if (bucketCount <= 0) throw new IllegalArgumentException("Invalid bucket count");

 if (array.length <= 1) return array; //trivially sorted

 long high = array[0];

 long low = array[0];

 for (int i = 1; i < array.length; i++) { //find the range of input elements

 if (array[i] > high) high = array[i];

 if (array[i] < low) low = array[i];

 }

 double interval = ((double)(high - low + 1))/bucketCount; //range of one bucket

 ArrayList<Long> buckets[] = new ArrayList[bucketCount];

 for (int i = 0; i < bucketCount; i++) { //initialize buckets

 buckets[i] = new ArrayList();

 }

 for (int i = 0; i < array.length; i++) { //partition the input array

 buckets[(int)((array[i] - low)/interval)].add(array[i]);

 }

 int pointer = 0;

 for (int i = 0; i < buckets.length; i++) {

 Collections.sort(buckets[i]); //mergeSort

 for (int j = 0; j < buckets[i].size(); j++) { //merge the buckets

 array[pointer] = buckets[i].get(j);

 pointer++;

 }

}

return array;

}
}


#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int>   II;
typedef vector< II >      VII;
typedef vector<int>     VI;
typedef vector< VI > 	VVI;
typedef long long int 	LL;
typedef pair<LL,LL> 	PLL;
typedef unsigned long long int ULL;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end()
#define SET(a,b) memset(a,b,sizeof(a))
#define LET(x,a) __typeof(a) x(a)

#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
//Works for forward as well as backward iteration

#define gu getchar
#define pu putchar
#define si(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define sll(n) scanf("%lld",&n)
#define lldout(n) printf("%lld\n",n)

#define DRT() int t; si(t); while(t--)

#define PlUSWRAP(index,n) index = (index+1)%n		//index++; if(index>=n) index=0
#define MINUSWRAP(index,n) index = (index + n -1)%n 	//index--; if(index<0) index=n-1
#define ROUNDOFFINT(d) d = (int)((double)d + 0.5)	//Round off d to nearest integer

#define FLUSHN while(gu()!='\n')
#define FLUSHS while(gu()!=' ')

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define off ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
LL n,q,turn;
vector<LL> arr;
int main() {
	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
	#endif
	off;
	cin>>n>>q;
	arr.resize(n);
	LL maxind = -1;
	LL ma = 0;
	rep(i,0,n)
	{
		cin>>arr[i];
		if(ma<arr[i])
		{
			ma = arr[i];
			maxind = i;
		}
	}
	LL rem = maxind+1;
	// we can do it till the maximum comes to the front, after that it is just circular rotation
	deque<LL> d;
	rep(i,0,n)
	{
		d.PB(arr[i]);
	}
	vector<PLL> fronts;
	rep(i,0,maxind+1)
	{
		LL a = d.front();
		d.pop_front();
		LL b = d.front();
		d.pop_front();
		if(a>b)
		{
			d.push_front(a);
			d.push_back(b);
		}
		else
		{
			d.push_front(b);
			d.push_back(a);
		}
		fronts.PB(MP(a,b));
	}
	LL sz = fronts.size();
	rep(i,0,q)
	{
		cin>>turn;
		if(turn <= sz)
		{
			cout<<fronts[turn-1].F<<" "<<fronts[turn-1].S<<"\n";
		}
		else
		{
			LL left = turn - sz;
			LL se = left%(n-1);
			cout<<ma<<" "<<d.at(se+1)<<"\n";
		}
	}
  	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int>   II;
typedef vector< II >      VII;
typedef vector<int>     VI;
typedef vector< VI > 	VVI;
typedef long long int 	LL;
typedef pair<LL,LL> 	PLL;
typedef unsigned long long int ULL;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end()
#define SET(a,b) memset(a,b,sizeof(a))
#define LET(x,a) __typeof(a) x(a)

#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
//Works for forward as well as backward iteration

#define gu getchar
#define pu putchar
#define si(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define sll(n) scanf("%lld",&n)
#define lldout(n) printf("%lld\n",n)

#define DRT() int t; si(t); while(t--)

#define PlUSWRAP(index,n) index = (index+1)%n		//index++; if(index>=n) index=0
#define MINUSWRAP(index,n) index = (index + n -1)%n 	//index--; if(index<0) index=n-1
#define ROUNDOFFINT(d) d = (int)((double)d + 0.5)	//Round off d to nearest integer

#define FLUSHN while(gu()!='\n')
#define FLUSHS while(gu()!=' ')

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define off ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
LL n,q,turn;
vector<LL> arr;
int main() {
	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
	#endif
	off;
	cin>>n>>q;
	arr.resize(n);
	LL maxind = -1;
	LL ma = 0;
	rep(i,0,n)
	{
		cin>>arr[i];
		if(ma<arr[i])
		{
			ma = arr[i];
			maxind = i;
		}
	}
	LL rem = maxind+1;
	// we can do it till the maximum comes to the front, after that it is just circular rotation
	deque<LL> d;
	rep(i,0,n)
	{
		d.PB(arr[i]);
	}
	vector<PLL> fronts;
	rep(i,0,maxind)
	{
		LL a = d.front();
		d.pop_front();
		LL b = d.front();
		d.pop_front();
		if(a>b)
		{
			d.push_front(a);
			d.push_back(b);
		}
		else
		{
			d.push_front(b);
			d.push_back(a);
		}
		fronts.PB(MP(a,b));
	}
	LL sz = fronts.size();
	// cout<<"sz "<<sz<<"\n";
	rep(i,0,q)
	{
		cin>>turn;
		if(turn <= sz)
		{
			cout<<fronts[turn-1].F<<" "<<fronts[turn-1].S<<"\n";
		}
		else
		{
			LL left = turn - sz-1;
			// cout<<"left "<<left<<"\n";
			LL se = left%(n-1);
			cout<<ma<<" "<<d.at(se+1)<<"\n";
		}
	}
  	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int>   II;
typedef vector< II >      VII;
typedef vector<int>     VI;
typedef vector< VI > 	VVI;
typedef long long int 	LL;
typedef pair<LL,LL> 	PLL;
typedef unsigned long long int ULL;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end()
#define SET(a,b) memset(a,b,sizeof(a))
#define LET(x,a) __typeof(a) x(a)

#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
//Works for forward as well as backward iteration

#define gu getchar
#define pu putchar
#define si(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define sll(n) scanf("%lld",&n)
#define lldout(n) printf("%lld\n",n)

#define DRT() int t; si(t); while(t--)

#define PlUSWRAP(index,n) index = (index+1)%n		//index++; if(index>=n) index=0
#define MINUSWRAP(index,n) index = (index + n -1)%n 	//index--; if(index<0) index=n-1
#define ROUNDOFFINT(d) d = (int)((double)d + 0.5)	//Round off d to nearest integer

#define FLUSHN while(gu()!='\n')
#define FLUSHS while(gu()!=' ')

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define off ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
LL n,q,turn;
vector<LL> arr;
int main() {
	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
	#endif
	off;
	cin>>n>>q;
	arr.resize(n);
	LL maxind = -1;
	LL ma = 0;
	rep(i,0,n)
	{
		cin>>arr[i];
		if(ma<arr[i])
		{
			ma = arr[i];
			maxind = i;
		}
	}
	LL rem = maxind+1;
	// we can do it till the maximum comes to the front, after that it is just circular rotation
	deque<LL> d;
	rep(i,0,n)
	{
		d.PB(arr[i]);
	}
	vector<PLL> fronts;
	rep(i,0,maxind+1)
	{
		LL a = d.front();
		d.pop_front();
		LL b = d.front();
		d.pop_front();
		if(a>b)
		{
			d.push_front(a);
			d.push_back(b);
		}
		else
		{
			d.push_front(b);
			d.push_back(a);
		}
		fronts.PB(MP(a,b));
	}
	LL sz = fronts.size();
	rep(i,0,q)
	{
		cin>>turn;
		if(turn <= sz)
		{
			cout<<fronts[turn-1].F<<" "<<fronts[turn-1].S<<"\n";
		}
		else
		{
			LL left = turn - sz;
			LL se = left%(n-1);
			cout<<ma<<" "<<d.at(se)<<"\n";
		}
	}
  	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end()
#define SET(a,b) memset(a,b,sizeof(a))
#define off ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
typedef pair<int,int>   II;
typedef vector< II >      VII;
typedef vector<int>     VI;
typedef vector< VI > 	VVI;
typedef long long int 	LL;
typedef vector<LL> 		VL;
#define si(n) cin>>n
#define dout(n) cout<<n<<"\n"
#define DRT() int t; si(t); while(t--)
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
//# define M_PI           3.14159265358979323846264338327950288
// DSU
void IO(){
	#ifndef ONLINE_JUDGE
    freopen("../input.txt", "r", stdin);
    freopen("../output.txt", "w", stdout);
	#endif
	srand(chrono::steady_clock::now().time_since_epoch().count());
	off;
}

int main(){
	IO();
	LL n,q;
	cin>>n>>q;
	deque<int> d;
	VII ans;
	VL arr(n);
	rep(i,0,n){
		cin>>arr[i];
		d.push_back(arr[i]);
	}
	int ind = -1;
	int ma = -1;
	rep(i,0,n){
		if(arr[i] > ma){
			ma = arr[i];
			ind = i;
		}
	}
	for(int i=0;i<ind;i++){
		int a = d.front();
		d.pop_front();
		int b = d.front();
		d.pop_front();
		ans.PB(MP(a,b));
		if(a > b){
			d.push_front(a);
			d.push_back(b);
		}
		else{
			d.push_front(b);
			d.push_back(a);
		}
	}
	VL res;
	for(auto it : d){
		res.PB(it);
	}
	LL m;
	rep(i,0,q){
		cin>>m;
		if(m <= ind){
			cout<<ans[m-1].F<<" "<<ans[m-1].S<<"\n";
		}
		else{
			m -= ind;
			m = m % (n-1);
			if(m == 0)
				m = n-1;
			cout<<ma<<" "<<res[m]<<"\n";
		}
	}
	return 0;
}