class RedBlackNode(object):

    def __init__(self, key):
        self._key = key
        self._red = False
        self._left = None
        self._right = None
        self._p = None

class RedBlackTree(object):

    def __init__(self):
        self._nil = RedBlackNode(None)
        self._root = self._nil

    def insert_key(self, key):
        y = self.search(key, True)
        z = RedBlackNode(key)
        z._p = y
        if y == self._nil:
            self._root = z
        elif z._key < y._key:
            y._left = z
        else:
            y._right = z
        z._left = self._nil
        z._right = self._nil
        z._red = True
        self._insert_fixup(z)

    def _insert_fixup(self, z):
        while z._p._red:
            if z._p == z._p._p._left:
                y = z._p._p._right
                if y._red:
                    z._p._red = False
                    y._red = False
                    z._p._p._red = True
                    z = z._p._p
                else:
                    if z == z._p._right:
                        z = z._p
                        self._left_rotate(z)
                    z._p._red = False
                    z._p._p._red = True
                    self._right_rotate(z._p._p)
            else:
                y = z._p._p._left
                if y._red:
                    z._p._red = False
                    y._red = False
                    z._p._p._red = True
                    z = z._p._p
                else:
                    if z == z._p._left:
                        z = z._p
                        self._right_rotate(z)
                    z._p._red = False
                    z._p._p._red = True
                    self._left_rotate(z._p._p)
        self._root._red = False

    def _left_rotate(self, x):
        y = x._right
        x._right = y._left
        if y._left != self._nil:
            y._left._p = x
        y._p = x._p
        if x._p == self._nil:
            self._root = y
        elif x == x._p._left:
            x._p._left = y
        else:
            x._p._right = y
        y._left = x
        x._p = y

    def _right_rotate(self, y):
        x = y._left
        y._left = x._right
        if x._right != self._nil:
            x._right._p = y
        x._p = y._p
        if y._p == self._nil:
            self._root = x
        elif y == y._p._right:
            y._p._right = x
        else:
            y._p._left = x
        x._right = y
        y._p = x

    def search(self, key, s):
        x = self._root
        g = self._nil
        while x != self._nil:
            g = x
            if key < x._key:
                x = x._left
            else:
                x = x._right
        return(g)
    def search2(self, key):
        a = None
        b = None
        x = self._root
        while x != self._nil and key != x._key:
            if key < x._key:
                a = x
                x = x._left
            else:
                b = x
                x = x._right
        if key == x._key:
            return(None)
        else:
            return((a,b))

n = int(input())
s = [int(i) for i in input().split()]
t = RedBlackTree()
t.insert_key([s[0],0,0])

e = []
for i in s[1:]:
    o,u = t.search2([i,0,0])
    if u == None:
        e.append(o._key[0])
        if o._key[0] > i:
            o._key[1] = 1
        else:
            o._key[2] = 1
    elif o == None:
        e.append(u._key[0])
        if u._key[0] > i:
            u._key[1] = 1
        else:
            u._key[2] = 1
    else:
        if o._key[0] > i and u._key[0] > i:
            if o._key[1] == 0:
                o._key[1] = 1
                e.append(o._key[0])
            else:
                u._key[1] = 1
                e.append(u._key[0])

        elif o._key[0] < i and u._key[0] > i:
            if o._key[2] == 0:
                o._key[2] = 1
                e.append(o._key[0])
            else:
                u._key[1] = 1
                e.append(u._key[0])
        elif o._key[0] > i and u._key[0] < i:
            if o._key[1] == 0:
                o._key[1] = 1
                e.append(o._key[0])
            else:
                u._key[2] = 1
                e.append(u._key[0])
        elif o._key[0] < i and u._key[0] < i:
            if o._key[2] == 0:
                o._key[2] = 1
                e.append(o._key[0])
            else:
                u._key[2] = 1
                e.append(u._key[0])
    t.insert_key([i,0,0])
print(*e)


class RedBlackNode(object):

    def __init__(self, key):
        self._key = key
        self._red = False
        self._left = None
        self._right = None
        self._p = None

class RedBlackTree(object):

    def __init__(self):
        self._nil = RedBlackNode(None)
        self._root = self._nil

    def insert_key(self, key):
        x = self._root
        y = self.search(key, True)
        if y is not None:
            z = RedBlackNode(key)
            z._p = y
            if y == self._nil:
                self._root = z
            elif z._key < y._key:
                y._left = z
            else:
                y._right = z
            z._left = self._nil
            z._right = self._nil
            z._red = True
            self._insert_fixup(z)
        return z._p

    def _insert_fixup(self, z):
        while z._p._red:
            if z._p == z._p._p._left:
                y = z._p._p._right
                if y._red:
                    z._p._red = False
                    y._red = False
                    z._p._p._red = True
                    z = z._p._p
                else:
                    if z == z._p._right:
                        z = z._p
                        self._left_rotate(z)
                    z._p._red = False
                    z._p._p._red = True
                    self._right_rotate(z._p._p)
            else:
                y = z._p._p._left
                if y._red:
                    z._p._red = False
                    y._red = False
                    z._p._p._red = True
                    z = z._p._p
                else:
                    if z == z._p._left:
                        z = z._p
                        self._right_rotate(z)
                    z._p._red = False
                    z._p._p._red = True
                    self._left_rotate(z._p._p)
        self._root._red = False

    def _left_rotate(self, x):
        y = x._right
        x._right = y._left
        if y._left != self._nil:
            y._left._p = x
        y._p = x._p
        if x._p == self._nil:
            self._root = y
        elif x == x._p._left:
            x._p._left = y
        else:
            x._p._right = y
        y._left = x
        x._p = y

    def _right_rotate(self, y):
        x = y._left
        y._left = x._right
        if x._right != self._nil:
            x._right._p = y
        x._p = y._p
        if y._p == self._nil:
            self._root = x
        elif y == y._p._right:
            y._p._right = x
        else:
            y._p._left = x
        x._right = y
        y._p = x

    def search(self, key, s):
        x = self._root
        g = self._nil
        while x != self._nil and key != x._key:
            g = x
            if key < x._key:
                x = x._left
            else:
                x = x._right
        if key == x._key:
            return(None)
        else:
            return(g)
    def search2(self, key):
        a = None
        b = None
        x = self._root
        while x != self._nil and key != x._key:
            if key < x._key:
                a = x
                x = x._left
            else:
                b = x
                x = x._right
        if key == x._key:
            return(None)
        else:
            return((a,b))
    def search_left(self, v):
        while v._p!= None and v._p._left!=v:
            v = v._p
        return(v._p)
    def search_right(self, v):
        while v._p!= None and v._p._right!=v:
            v = v._p
        return(v._p)

n = int(input())
s = [int(i) for i in input().split()]
t = RedBlackTree()
t.insert_key([s[0],0,0])

e = []
for i in s[1:]:
    o,u = t.search2([i,0,0])
    if u == None:
        e.append(o._key[0])
        if o._key[0] > i:
            o._key[1] = 1
        else:
            o._key[2] = 1
    elif o == None:
        e.append(u._key[0])
        if u._key[0] > i:
            u._key[1] = 1
        else:
            u._key[2] = 1
    else:
        if o._key[0] > i and u._key[0] > i:
            if o._key[1] == 0:
                o._key[1] = 1
                e.append(o._key[0])
            else:
                u._key[1] = 1
                e.append(u._key[0])

        elif o._key[0] < s[i] and u._key[0] > i:
            if o._key[2] == 0:
                o._key[2] = 1
                e.append(o._key[0])
            else:
                u._key[1] = 1
                e.append(u._key[0])
        elif o._key[0] > s[i] and u._key[0] < i:
            if o._key[1] == 0:
                o._key[1] = 1
                e.append(o._key[0])
            else:
                u._key[2] = 1
                e.append(u._key[0])
        elif o._key[0] < s[i] and u._key[0] < i:
            if o._key[2] == 0:
                o._key[2] = 1
                e.append(o._key[0])
            else:
                u._key[2] = 1
                e.append(u._key[0])

    t.insert_key([i,0,0])
print(*e)


class TreeNode:
    def __init__(self, a):
        self.a = -1
        self.data = a
        self.left = -1
        self.right = -1

    def initNode(self, left, right):
        self.left = left
        self.right = right
    def set_data(self, data):
        self.data = data

class BinaryTree:
    def __init__(self, a):
        self.storage = [TreeNode(a)]
        self.nodeNum = 1
    def add_node(self, data):
        n = len(self.storage)
        node = TreeNode(data)
        self.storage.append(node)
        t = 0
        ch = 0
        i = 0
        while ch != -1:
            if data < self.storage[ch].data:
                t = ch
                i = 0
                ch = self.storage[ch].left
            else:
                t = ch
                i = 1
                ch = self.storage[ch].right
        if i:
            self.storage[t].right = n
        else:
            self.storage[t].left = n
        self.storage[n].a = t

n = int(input())
s = [int(i) for i in input().split()]
o = BinaryTree(s[0])
k = []
for i in s[1:]:
    o.add_node(i)
    k.append(s[o.storage[-1].a])
print(*k)


def BinSearch(li, x):
    i = 0
    j = len(li)-1
    m = int(j/2)
    while li[m] != x and i < j:
        if x > li[m]:
            i = m+1
        else:
            j = m-1
        m = int((i+j)/2)
    if x>li[i]:
        return i+1
    return i

n = int(input())
s = [int(i) for i in input().split()]
k = [[s[0],0,0]]
u = []
for i in s[1:]:
    p = [i,0,0]
    a = BinSearch(k, [i,0,0])
    if a == len(k):
        u.append(k[a-1][0])
        if k[a-1][1] == 1:
            k.insert(a, p)
        else:
            k[a-1][2] = 1
            k.insert(a, p)
    else:
        if a == 0:
            u.append(k[a][0])
            if k[a][2] == 1:
                k.insert(a+1, p)
            else:
                k[a][1] = 1
                k.insert(a, p)
        else:
            if k[a][1] == 0:
                u.append(k[a][0])
                if k[a][2] == 1:
                    k.insert(a+1, p)
                else:
                    k[a][1] = 1
                    k.insert(a, p)
            else:
                u.append(k[a-1][0])
                if k[a-1][1] == 1:
                    k.insert(a+1, p)
                else:
                    k[a-1][2] = 1
                    k.insert(a, p)
print(*u)


class RedBlackNode(object):

    def __init__(self, key):
        self._key = key
        self._red = False
        self._left = None
        self._right = None
        self._p = None

class RedBlackTree(object):

    def __init__(self):
        self._nil = RedBlackNode(None)
        self._root = self._nil

    def insert_key(self, key):
        x = self._root
        y = self.search(key, True)
        if y is not None:
            z = RedBlackNode(key)
            z._p = y
            if y == self._nil:
                self._root = z
            elif z._key < y._key:
                y._left = z
            else:
                y._right = z
            z._left = self._nil
            z._right = self._nil
            z._red = True
            self._insert_fixup(z)
        return z._p

    def _insert_fixup(self, z):
        while z._p._red:
            if z._p == z._p._p._left:
                y = z._p._p._right
                if y._red:
                    z._p._red = False
                    y._red = False
                    z._p._p._red = True
                    z = z._p._p
                else:
                    if z == z._p._right:
                        z = z._p
                        self._left_rotate(z)
                    z._p._red = False
                    z._p._p._red = True
                    self._right_rotate(z._p._p)
            else:
                y = z._p._p._left
                if y._red:
                    z._p._red = False
                    y._red = False
                    z._p._p._red = True
                    z = z._p._p
                else:
                    if z == z._p._left:
                        z = z._p
                        self._right_rotate(z)
                    z._p._red = False
                    z._p._p._red = True
                    self._left_rotate(z._p._p)
        self._root._red = False

    def _left_rotate(self, x):
        y = x._right
        x._right = y._left
        if y._left != self._nil:
            y._left._p = x
        y._p = x._p
        if x._p == self._nil:
            self._root = y
        elif x == x._p._left:
            x._p._left = y
        else:
            x._p._right = y
        y._left = x
        x._p = y

    def _right_rotate(self, y):
        x = y._left
        y._left = x._right
        if x._right != self._nil:
            x._right._p = y
        x._p = y._p
        if y._p == self._nil:
            self._root = x
        elif y == y._p._right:
            y._p._right = x
        else:
            y._p._left = x
        x._right = y
        y._p = x

    def search(self, key, s):
        x = self._root
        g = self._nil
        while x != self._nil and key != x._key:
            g = x
            if key < x._key:
                x = x._left
            else:
                x = x._right
        if key == x._key:
            return(None)
        else:
            return(g)
    def search_left(self, v):
        while v._p!= None and v._p._left!=v:
            v = v._p
        return(v._p)
    def search_right(self, v):
        while v._p!= None and v._p._right!=v:
            v = v._p
        return(v._p)

n = int(input())
s = [int(i) for i in input().split()]
t = RedBlackTree()
t.insert_key([s[0],0,0])

e = []
for i in range(1, n):
    o = t.search([s[i],0,0], 1)
    if o._key > [s[i],0,0]:
        u  = t.search_right(o)
    else:
        u  = t.search_left(o)
    if u == None:
        e.append(o._key[0])
        if o._key[0] > s[i]:
            o._key[1] = 1
        else:
            o._key[2] = 1
    else:
        if o._key[0] > s[i] and u._key[0] > s[i]:
            if o._key[1] == 0:
                o._key[1] = 1
                e.append(o._key[0])
            else:
                u._key[1] = 1
                e.append(u._key[0])

        elif o._key[0] < s[i] and u._key[0] > s[i]:
            if o._key[2] == 0:
                o._key[2] = 1
                e.append(o._key[0])
            else:
                u._key[1] = 1
                e.append(u._key[0])
        elif o._key[0] > s[i] and u._key[0] < s[i]:
            if o._key[1] == 0:
                o._key[1] = 1
                e.append(o._key[0])
            else:
                u._key[2] = 1
                e.append(u._key[0])
        elif o._key[0] < s[i] and u._key[0] < s[i]:
            if o._key[2] == 0:
                o._key[2] = 1
                e.append(o._key[0])
            else:
                u._key[2] = 1
                e.append(u._key[0])

    t.insert_key([s[i],0,0])
print(*e)


class RedBlackNode(object):

    def __init__(self, key):
        self._key = key
        self._red = False
        self._left = None
        self._right = None
        self._p = None

class RedBlackTree(object):

    def __init__(self):
        self._nil = RedBlackNode(None)
        self._root = self._nil

    def insert_key(self, key):
        x = self._root
        y = self.search(key, True)
        if y is not None:
            z = RedBlackNode(key)
            z._p = y
            if y == self._nil:
                self._root = z
            elif z._key < y._key:
                y._left = z
            else:
                y._right = z
            z._left = self._nil
            z._right = self._nil
            z._red = True
            self._insert_fixup(z)
        return z._p

    def _insert_fixup(self, z):
        while z._p._red:
            if z._p == z._p._p._left:
                y = z._p._p._right
                if y._red:
                    z._p._red = False
                    y._red = False
                    z._p._p._red = True
                    z = z._p._p
                else:
                    if z == z._p._right:
                        z = z._p
                        self._left_rotate(z)
                    z._p._red = False
                    z._p._p._red = True
                    self._right_rotate(z._p._p)
            else:
                y = z._p._p._left
                if y._red:
                    z._p._red = False
                    y._red = False
                    z._p._p._red = True
                    z = z._p._p
                else:
                    if z == z._p._left:
                        z = z._p
                        self._right_rotate(z)
                    z._p._red = False
                    z._p._p._red = True
                    self._left_rotate(z._p._p)
        self._root._red = False

    def _left_rotate(self, x):
        y = x._right
        x._right = y._left
        if y._left != self._nil:
            y._left._p = x
        y._p = x._p
        if x._p == self._nil:
            self._root = y
        elif x == x._p._left:
            x._p._left = y
        else:
            x._p._right = y
        y._left = x
        x._p = y

    def _right_rotate(self, y):
        x = y._left
        y._left = x._right
        if x._right != self._nil:
            x._right._p = y
        x._p = y._p
        if y._p == self._nil:
            self._root = x
        elif y == y._p._right:
            y._p._right = x
        else:
            y._p._left = x
        x._right = y
        y._p = x

    def search(self, key, s):
        x = self._root
        g = self._nil
        while x != self._nil and key != x._key:
            g = x
            if key < x._key:
                x = x._left
            else:
                x = x._right
        if s and key == x._key:
            return(None)
        elif s:
            return(g)
        else:
            if x == self._nil:
                return("No")
            else:
                return("Yes")

n = int(input())
s = [int(i) for i in input().split()]
t = RedBlackTree()
t.insert_key([s[0],0,0])

e = []
for i in range(1, n):
    o = t.search([s[i],0,0],1)
    u  = o._p
    if u._key == None:
        e.append(o._key[0])
        if o._key[0] > s[i]:
            o._key[1] = 1
        else:
            o._key[2] = 1
    else:
        if o._key[0] > s[i] and u._key[0] > s[i]:
            if o._key[1] == 0:
                o._key[1] = 1
                e.append(o._key[0])
            else:
                u._key[1] = 1
                e.append(u._key[0])

        elif o._key[0] < s[i] and u._key[0] > s[i]:
            if o._key[2] == 0:
                o._key[2] = 1
                e.append(o._key[0])
            else:
                u._key[1] = 1
                e.append(u._key[0])
        elif o._key[0] > s[i] and u._key[0] < s[i]:
            if o._key[1] == 0:
                o._key[1] = 1
                e.append(o._key[0])
            else:
                u._key[2] = 1
                e.append(u._key[0])
        elif o._key[0] < s[i] and u._key[0] < s[i]:
            if o._key[2] == 0:
                o._key[2] = 1
                e.append(o._key[0])
            else:
                u._key[2] = 1
                e.append(u._key[0])

    t.insert_key([s[i],0,0])
print(*e)


class RedBlackNode(object):

    def __init__(self, key):
        self._key = key
        self._red = False
        self._left = None
        self._right = None
        self._p = None

class RedBlackTree(object):

    def __init__(self):
        self._nil = RedBlackNode(None)
        self._root = self._nil

    def insert_key(self, key):
        x = self._root
        y = self.search(key, True)
        if y is not None:
            z = RedBlackNode(key)
            z._p = y
            if y == self._nil:
                self._root = z
            elif z._key < y._key:
                y._left = z
            else:
                y._right = z
            z._left = self._nil
            z._right = self._nil
            z._red = True
            self._insert_fixup(z)
        return z._p

    def _insert_fixup(self, z):
        while z._p._red:
            if z._p == z._p._p._left:
                y = z._p._p._right
                if y._red:
                    z._p._red = False
                    y._red = False
                    z._p._p._red = True
                    z = z._p._p
                else:
                    if z == z._p._right:
                        z = z._p
                        self._left_rotate(z)
                    z._p._red = False
                    z._p._p._red = True
                    self._right_rotate(z._p._p)
            else:
                y = z._p._p._left
                if y._red:
                    z._p._red = False
                    y._red = False
                    z._p._p._red = True
                    z = z._p._p
                else:
                    if z == z._p._left:
                        z = z._p
                        self._right_rotate(z)
                    z._p._red = False
                    z._p._p._red = True
                    self._left_rotate(z._p._p)
        self._root._red = False

    def _left_rotate(self, x):
        y = x._right
        x._right = y._left
        if y._left != self._nil:
            y._left._p = x
        y._p = x._p
        if x._p == self._nil:
            self._root = y
        elif x == x._p._left:
            x._p._left = y
        else:
            x._p._right = y
        y._left = x
        x._p = y

    def _right_rotate(self, y):
        x = y._left
        y._left = x._right
        if x._right != self._nil:
            x._right._p = y
        x._p = y._p
        if y._p == self._nil:
            self._root = x
        elif y == y._p._right:
            y._p._right = x
        else:
            y._p._left = x
        x._right = y
        y._p = x

    def search(self, key, s):
        x = self._root
        g = self._nil
        while x != self._nil and key != x._key:
            g = x
            if key < x._key:
                x = x._left
            else:
                x = x._right
        if key == x._key:
            return(None)
        else:
            return(g)
    def search2(self, key):
        a = None
        b = None
        x = self._root
        while x != self._nil and key != x._key:
            if key < x._key:
                a = x
                x = x._left
            else:
                b = x
                x = x._right
        if key == x._key:
            return(None)
        else:
            return((a,b))
    def search_left(self, v):
        while v._p!= None and v._p._left!=v:
            v = v._p
        return(v._p)
    def search_right(self, v):
        while v._p!= None and v._p._right!=v:
            v = v._p
        return(v._p)

n = int(input())
s = [int(i) for i in input().split()]
t = RedBlackTree()
t.insert_key([s[0],0,0])

e = []
for i in s[1:]:
    o,u = t.search2([i,0,0])
    if u == None:
        e.append(o._key[0])
        if o._key[0] > i:
            o._key[1] = 1
        else:
            o._key[2] = 1
    elif o == None:
        e.append(u._key[0])
        if u._key[0] > i:
            u._key[1] = 1
        else:
            u._key[2] = 1
    else:
        if o._key[0] > i and u._key[0] > i:
            if o._key[1] == 0:
                o._key[1] = 1
                e.append(o._key[0])
            else:
                u._key[1] = 1
                e.append(u._key[0])

        elif o._key[0] < i and u._key[0] > i:
            if o._key[2] == 0:
                o._key[2] = 1
                e.append(o._key[0])
            else:
                u._key[1] = 1
                e.append(u._key[0])
        elif o._key[0] > i and u._key[0] < i:
            if o._key[1] == 0:
                o._key[1] = 1
                e.append(o._key[0])
            else:
                u._key[2] = 1
                e.append(u._key[0])
        elif o._key[0] < i and u._key[0] < i:
            if o._key[2] == 0:
                o._key[2] = 1
                e.append(o._key[0])
            else:
                u._key[2] = 1
                e.append(u._key[0])

    t.insert_key([i,0,0])
print(*e)


def BinSearch(li, x):
    i = 0
    j = len(li)-1
    m = int(j/2)
    while li[m] != x and i < j:
        if x > li[m]:
            i = m+1
        else:
            j = m-1
        m = int((i+j)/2)
    if x>li[i]:
        return i+1
    return i

n = int(input())
s = [int(i) for i in input().split()]
k = [[s[0],0,0]]
u = []
for i in s[1:]:
    p = [i,0,0]
    a = BinSearch(k, [i,0,0])
    if a == len(k):
        u.append(k[a-1][0])
        k[a-1][2] = 1
        k.insert(a, p)
    else:
        if a == 0:
            u.append(k[a][0])
            k[a][1] = 1
            k.insert(a, p)
        else:
            if k[a][1] == 0:
                u.append(k[a][0])
                k[a][1] = 1
                k.insert(a, p)
            else:
                u.append(k[a-1][0])
                k[a-1][2] = 1
                k.insert(a, p)
print(*u)


def main():
    from bisect import bisect_left
    n, aa = int(input()), list(map(int, input().split()))
    bb = sorted(range(n), key=lambda i: (aa[i], -i))
    cc = sorted(range(n), key=bb.__getitem__)
    l, res = [-1, n], []
    parent, number = [-1] * (n + 1), [-1] * (n + 1)
    for i, a in enumerate(cc):
        number[a] = i
        j = bisect_left(l, a)
        lo, hi = l[j - 1], l[j]
        parent[a] = p = hi if number[hi] > number[lo] else lo
        res.append(aa[bb[p]])
        l.insert(j, a)
    print(' '.join(map(str, res[1:])))

if __name__ == '__main__':
    main()


class RedBlackTree():
    _nil = (None, False, None, None, None)

    def __init__(self):
        self._root = RedBlackTree._nil

    def insert_key(self, key):
        y = self.search(key, True)
        z = [key, False, None, None, y]
        if y is RedBlackTree._nil:
            self._root = z
        else:
            y[2 if z[0] < y[0] else 3] = z
        z[2] = z[3] = RedBlackTree._nil
        z[1] = True
        self._insert_fixup(z)

    def _insert_fixup(self, z):
        while z[4][1]:
            if z[4] == z[4][4][2]:
                y = z[4][4][3]
                if y[1]:
                    z[4][1] = y[1] = False
                    z[4][4][1] = True
                    z = z[4][4]
                else:
                    if z == z[4][3]:
                        z = z[4]
                        self._left_rotate(z)
                    z[4][1] = False
                    z[4][4][1] = True
                    self._right_rotate(z[4][4])
            else:
                y = z[4][4][2]
                if y[1]:
                    z[4][1] = y[1] = False
                    z[4][4][1] = True
                    z = z[4][4]
                else:
                    if z == z[4][2]:
                        z = z[4]
                        self._right_rotate(z)
                    z[4][1] = False
                    z[4][4][1] = True
                    self._left_rotate(z[4][4])
        self._root[1] = False

    def _left_rotate(self, x):
        y = x[3]
        x[3] = y[2]
        if y[2] is not RedBlackTree._nil:
            y[2][4] = x
        y[4] = x[4]
        if x[4] is RedBlackTree._nil:
            self._root = y
        else:
            x[4][3 - (x == x[4][2])] = y
        y[2] = x
        x[4] = y

    def _right_rotate(self, y):
        x = y[2]
        y[2] = x[3]
        if x[3] is not RedBlackTree._nil:
            x[3][4] = y
        x[4] = y[4]
        if y[4] is RedBlackTree._nil:
            self._root = x
        else:
            y[4][2 + (y == y[4][3])] = x
        x[3] = y
        y[4] = x

    def search(self, key, s):
        x, g = self._root, RedBlackTree._nil
        while x is not RedBlackTree._nil:
            g, x = x, x[2 if key < x[0] else 3]
        return g

    def search2(self, key):
        a = b = None
        x = self._root
        while x is not RedBlackTree._nil and key != x[0]:
            if key < x[0]:
                a, x = x, x[2]
            else:
                b, x = x, x[3]
        if key != x[0]:
            return a, b

def main():
    input()
    a, *aa = map(int, input().split())
    t = RedBlackTree()
    t.insert_key([a, 0, 0])
    e = []
    for a in aa:
        o, u = t.search2([a, 0, 0])
        if u is None:
            e.append(o[0][0])
            o[0][2 - (o[0][0] > a)] = 1
        elif o is None:
            e.append(u[0][0])
            u[0][2 - (u[0][0] > a)] = 1
        else:
            if o[0][0] > a < u[0][0]:
                if o[0][1] == 0:
                    o[0][1] = 1
                    e.append(o[0][0])
                else:
                    u[0][1] = 1
                    e.append(u[0][0])
            elif o[0][0] < a < u[0][0]:
                if o[0][2]:
                    u[0][1] = 1
                    e.append(u[0][0])
                else:
                    o[0][2] = 1
                    e.append(o[0][0])
            elif o[0][0] > a > u[0][0]:
                if o[0][1]:
                    u[0][2] = 1
                    e.append(u[0][0])
                else:
                    o[0][1] = 1
                    e.append(o[0][0])
            elif o[0][0] < a > u[0][0]:
                if o[0][2]:
                    u[0][2] = 1
                    e.append(u[0][0])
                else:
                    o[0][2] = 1
                    e.append(o[0][0])
        t.insert_key([a, 0, 0])
    print(*e)

if __name__ == '__main__':
    main()


def main():
    t, w, r = [0] * (int(input()) * 3), 3, []
    t[0], *l = map(int, input().split())
    f = r.append
    for a, w in zip(l, range(3, 300000, 3)):
        i = 0
        while True:
            if a > t[i]:
                j = t[i + 2]
                if j:
                    i = j
                else:
                    t[i + 2] = w
                    break
            else:
                j = t[i + 1]
                if j:
                    i = j
                else:
                    t[i + 1] = w
                    break
        t[w] = a
        f(t[i])
    print(' '.join(map(str, r)))

if __name__ == '__main__':
    main()


using System;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using Debug = System.Diagnostics.Debug;
using StringBuilder = System.Text.StringBuilder;
using System.Numerics;

namespace Program
{

    public class Solver
    {
        public void Solve()
        {
            var n = sc.Integer();
            var a = sc.Integer(n);
            var b = a.OrderBy(x => x).ToList();
            for (int i = 0; i < n; i++)
                a[i] = b.BinarySearch(a[i]) + 2;
            var d = new int[n + 4];
            var tree = new FenwickTree(n + 2);
            tree.Add(a[0], 1);
            tree.Add(1, 1);
            tree.Add(n + 2, 1);
            d[1] = -10000;
            d[n + 2] = -10000;

            var ans = new List<int>();
            for (int i = 1; i < n; i++)
            {
                var l = tree.LowerBound(tree[a[i]]);
                var r = tree.LowerBound(tree[a[i]] + 1);
                if (d[l] > d[r])
                {
                    d[a[i]] = d[l] + 1;
                    ans.Add(b[l - 2]);
                }
                else
                {
                    d[a[i]] = d[r] + 1;
                    ans.Add(b[r - 2]);
                }
                tree.Add(a[i], 1);
            }
            IO.Printer.Out.WriteLine(ans.AsJoinedString());
        }
        public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput());
        static T[] Enumerate<T>(int n, Func<int, T> f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(i); return a; }
        static public void Swap<T>(ref T a, ref T b) { var tmp = a; a = b; b = tmp; }
    }
}

#region main
static class Ex
{
    static public string AsString(this IEnumerable<char> ie) { return new string(System.Linq.Enumerable.ToArray(ie)); }
    static public string AsJoinedString<T>(this IEnumerable<T> ie, string st = " ") { return string.Join(st, ie); }
    static public void Main()
    {
        var solver = new Program.Solver();
        solver.Solve();
        Program.IO.Printer.Out.Flush();
    }
}
#endregion
#region Ex
namespace Program.IO
{
    using System.IO;
    using System.Text;
    using System.Globalization;
    public class Printer : StreamWriter
    {
        static Printer() { Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false }; }
        public static Printer Out { get; set; }
        public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
        public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
        public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { }
        public void Write<T>(string format, T[] source) { base.Write(format, source.OfType<object>().ToArray()); }
        public void WriteLine<T>(string format, T[] source) { base.WriteLine(format, source.OfType<object>().ToArray()); }
    }
    public class StreamScanner
    {
        public StreamScanner(Stream stream) { str = stream; }
        public readonly Stream str;
        private readonly byte[] buf = new byte[1024];
        private int len, ptr;
        public bool isEof = false;
        public bool IsEndOfStream { get { return isEof; } }
        private byte read()
        {
            if (isEof) return 0;
            if (ptr >= len) { ptr = 0; if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; } }
            return buf[ptr++];
        }
        public char Char() { byte b = 0; do b = read(); while ((b < 33 || 126 = 33 && b <= 126; b = (char)read())
                sb.Append(b);
            return sb.ToString();
        }
        public string ScanLine()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b != '\n'; b = (char)read())
                if (b == 0) break;
                else if (b != '\r') sb.Append(b);
            return sb.ToString();
        }
        public long Long()
        {
            if (isEof) return long.MinValue;
            long ret = 0; byte b = 0; var ng = false;
            do b = read();
            while (b != 0 && b != '-' && (b < '0' || '9' < b));
            if (b == 0) return long.MinValue;
            if (b == '-') { ng = true; b = read(); }
            for (; true; b = read())
            {
                if (b < '0' || '9' < b)
                    return ng ? -ret : ret;
                else ret = ret * 10 + b - '0';
            }
        }
        public int Integer() { return (isEof) ? int.MinValue : (int)Long(); }
        public double Double() { var s = Scan(); return s != "" ? double.Parse(s, CultureInfo.InvariantCulture) : double.NaN; }
        private T[] enumerate<T>(int n, Func<T> f)
        {
            var a = new T[n];
            for (int i = 0; i < n; ++i) a[i] = f();
            return a;
        }

        public char[] Char(int n) { return enumerate(n, Char); }
        public string[] Scan(int n) { return enumerate(n, Scan); }
        public double[] Double(int n) { return enumerate(n, Double); }
        public int[] Integer(int n) { return enumerate(n, Integer); }
        public long[] Long(int n) { return enumerate(n, Long); }
    }
}
#endregion
#region FenwickTree
[System.Diagnostics.DebuggerDisplay("Data={ToString()}")]
public class FenwickTree
{
    int n;
    long[] bit;
    int max = 1;
    public FenwickTree(int size)
    {
        n = size; bit = new long[n + 1];
        while ((max << 1) <= n) max <<= 1;
    }
    /// sum[a,b]
    public long this[int i, int j] { get { return this[j] - this[i - 1]; } }
    /// sum[0,i]
    public long this[int i] { get { long s = 0; for (; i > 0; i -= i & -i) s += bit[i]; return s; } }
    public int LowerBound(long w)
    {
        if (w <= 0) return 0;
        int x = 0;
        for (int k = max; k > 0; k >>= 1)
            if (x + k <= n && bit[x + k] < w)
            {
                w -= bit[x + k];
                x += k;
            }
        return x + 1;
    }
    /// add v to bit[i]
    public void Add(int i, long v)
    {
        if (i == 0) System.Diagnostics.Debug.Fail("BIT is 1 indexed");
        for (; i <= n; i += i & -i) bit[i] += v;
    }
    public override string ToString() { return string.Join(",", Enumerable.Range(0, n + 1).Select(i => this[i, i])); }
}
#endregion


using System;

namespace Project
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] stringValue;
            Int32 n, i, ans, value;
            Tree[] t;
            n = Convert.ToInt32(Console.ReadLine());
            t = new Tree[n];
            stringValue = Console.ReadLine().Split(' ');
            t[0].value = Convert.ToInt32(stringValue[0]);
            for (i = 1; i < n; i++)
            {
                ans = 0;
                value = Convert.ToInt32(stringValue[i]);
                while(t[ans].value > 0)
                {
                    if (t[ans].value > value)
                    {
                        if (t[ans].right == 0)
                        {
                            t[ans].right = i;
                            Console.Write(t[ans].value);
                            Console.Write(' ');
                        }
                        ans = t[ans].right;
                    }
                    else
                    {
                        if (t[ans].left == 0)
                        {
                            t[ans].left = i;
                            Console.Write(t[ans].value);
                            Console.Write(' ');
                        }
                        ans = t[ans].left;
                    }
                }
                t[ans].value = value;
            }
        }

        struct Tree
        {
            public Int32 value;
            public Int32 left;
            public Int32 right;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace Project
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] stringValue;
            Int32[] value, result;
            Int32 n, i;
            BinaryTree ans1, ans2, ans3;
            IEnumerator<BinaryTree> tree;
            Stack<BinaryTree> stack = new Stack<BinaryTree>();
            n = Convert.ToInt32(Console.ReadLine());
            value = new Int32[n];
            result = new Int32[n];
            stringValue = Console.ReadLine().Split(' ');
            for (i = 0; i < n; i++)
                value[i] = Convert.ToInt32(stringValue[i]);
            tree = value.Select((m, j) => new BinaryTree(j, m)).OrderBy(x => x.Value).GetEnumerator();
            tree.MoveNext();
            ans1 = tree.Current;
            while (tree.MoveNext())
            {
                ans2 = tree.Current;
                while (true)
                {
                    if (ans1.Index < ans2.Index)
                    {
                        stack.Push(ans1);
                        ans1 = ans2;
                        break;
                    }
                    if (stack.Count > 0)
                    {
                        ans3 = stack.Peek();
                        if (ans3.Index > ans2.Index)
                        {
                            ans3.Right = ans1;
                            ans1 = ans3;
                            stack.Pop();
                            continue;
                        }
                    }
                    ans2.Left = ans1;
                    ans1 = ans2;
                    break;
                }
            }
            while (stack.Count > 0)
            {
                ans3 = stack.Pop();
                ans3.Right = ans1;
                ans1 = ans3;
            }
            stack.Push(ans1);
            while (stack.Count > 0)
            {
                ans3 = stack.Pop();
                if (ans3.Left != null)
                {
                    result[ans3.Left.Index] = ans3.Value;
                    stack.Push(ans3.Left);
                }
                if (ans3.Right != null)
                {
                    result[ans3.Right.Index] = ans3.Value;
                    stack.Push(ans3.Right);
                }
            }
            for (i = 1; i < n; i++)
            {
                Console.Write(result[i]);
                Console.Write(' ');
            }
        }
    }

    class BinaryTree
    {
        private Int32 index, value;
        private BinaryTree left, right;

        public Int32 Index { get { return index; } set { index = value; } }
        public Int32 Value { get { return this.value; } set { this.value = value; } }
        public BinaryTree Left { get { return left; } set { left = value; } }
        public BinaryTree Right { get { return right; } set { right = value; } }

        public BinaryTree(Int32 index, Int32 value)
        {
            this.index = index;
            this.value = value;
        }

        public BinaryTree(Int32 value, BinaryTree left, BinaryTree right)
        {
            this.value = value;
            this.left = left;
            this.right = right;
        }
    }
}


using System;

namespace Project
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] stringValue;
            Int32 n, i;
            Tree t;
            n = Convert.ToInt32(Console.ReadLine());
            stringValue = Console.ReadLine().Split(' ');
            t = new Tree(Convert.ToInt32(stringValue[0]));
            for (i = 1; i < n; i++)
                t.Add(Convert.ToInt32(stringValue[i]));
        }

        class Tree
        {
            private Int32 value;
            private Tree left, right;

            public Tree(Int32 value)
            {
                this.value = value;
            }

            public void Add(Int32 value)
            {
                if (value > this.value)
                    if (right == null)
                    {
                        right = new Tree(value);
                        Console.Write(this.value);
                        Console.Write(' ');
                    }
                    else
                        right.Add(value);
                else
                    if (left == null)
                    {
                        left = new Tree(value);
                        Console.Write(this.value);
                        Console.Write(' ');
                    }
                    else
                        left.Add(value);
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace _675d
{
    partial class Program
    {
        static partial void Test();
        static void Main(string[] args)
        {
            using (var cin = new Cin())
            using (var cout = new Cout())
            {
                var n = cin.ReadInt();
                var a = new int[n];
                for (int i = 0; i < n; i++) a[i] = cin.ReadInt();

                var result = Solve(a);

                cout.WriteList(result.Skip(1));
            }
        }

        private static int[] Solve(int[] a)
        {
            return BetterSolution(a);
        }

        private static int[] BetterSolution(int[] a)
        {
            var stack = new Stack<Node>();

            var e = a.Select((n, i) => new Node(i, n)).OrderBy(x => x.Value).GetEnumerator(); e.MoveNext();
            var node = e.Current;

            while (e.MoveNext())
            {
                var curr = e.Current;
                while (true)
                {
                    if (node.Index < curr.Index)
                    {
                        stack.Push(node);
                        node = curr;
                        break;
                    }

                    if (stack.Count > 0)
                    {
                        var pop = stack.Peek();

                        if (pop.Index > curr.Index)
                        {
                            pop.Right = node;
                            node = pop;
                            stack.Pop();
                            continue;
                        }
                    }

                    curr.Left = node;
                    node = curr;
                    break;
                }
            }

            while(stack.Count > 0)
            {
                var pop = stack.Pop();
                pop.Right = node;
                node = pop;
            }

            var result = new int[a.Length];
            Dump(result, node);
            return result;
        }

        private static void Dump(int[] result, Node root)
        {
            var stack = new Stack<Node>();

            stack.Push(root);

            while (stack.Count > 0)
            {
                var node = stack.Pop();

                if (node.Left != null)
                {
                    result[node.Left.Index] = node.Value;
                    //Dump(result, node.Left);
                    stack.Push(node.Left);
                }
                if (node.Right != null)
                {
                    result[node.Right.Index] = node.Value;
                    //Dump(result, node.Right);
                    stack.Push(node.Right);
                }
            }
        }
    }

    [DebuggerDisplay(@"{Left?.Value} / {Value} \ {Right?.Value}")]
    internal class Node
    {
        public readonly int Index, Value;
        public Node Left, Right;

        public Node(int index, int value)
        {
            Index = index;
            Value = value;
        }

        public Node(int value, Node left, Node right)
        {
            Value = value;
            Left = left;
            Right = right;
        }
    }

    #region Boilerplate
    internal class Cin : StreamReader
    {
        public Cin() : this(Console.OpenStandardInput(65536)) {}
        public Cin(Stream stream) : base(stream, Encoding.ASCII, false){}
        public int ReadInt(){return (int) ReadLong();}
        public long ReadLong()
        {
            int c; do { c = Read(); } while (c == ' ' || c == '\t' || c == '\r' || c == '\n');
            int sign = 1; if (c == '-'){ sign = -1; c = Read(); }
            long n = 0; while (c >= '0' && c <= '9'){ n = n * 10 + c - '0'; c = Read(); }
            return n*sign;
        }
    }

    internal class Cout : StreamWriter
    {
        public Cout() : base(Console.OpenStandardOutput(65536), Encoding.ASCII){}
    }

    internal static class Ext
    {
        public static void WriteList<T>(this TextWriter sw, IEnumerable<T> src)
        {
            using (var en = src.GetEnumerator())
                if (en.MoveNext()) { sw.Write("{0}", en.Current);
                while (en.MoveNext()) sw.Write(" {0}", en.Current); }
            sw.WriteLine();
        }
    }
    #endregion
}


using System;
using System.IO;
using System.Text;

namespace Tree_Construction
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            int n = Next();

            var nn = new int[n];
            for (int i = 0; i < n; i++)
            {
                nn[i] = Next();
            }

            var ans = new int[n];

            var l = new bool[n];
            var r = new bool[n];

            var sn = (int[]) nn.Clone();
            Array.Sort(sn);

            var array = new int[n];
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = int.MaxValue;
            }
            var mit = new MinTree(array);
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = -1;
            }
            var mat = new MaxTree(array);

            for (int i = 0; i < n; i++)
            {
                int index = Array.BinarySearch(sn, nn[i]);
                if (i != 0)
                {
                    int left = mat.query(0, index);
                    if (left >= 0 && !r[left])
                    {
                        r[left] = true;
                        ans[i] = sn[left];
                    }
                    else
                    {
                        int rigth = mit.query(index, n);
                        l[rigth] = true;
                        ans[i] = sn[rigth];
                    }
                }
                mit.modify(index, index);
                mat.modify(index, index);
            }

            for (int i = 1; i < n; i++)
            {
                writer.Write(ans[i]);
                writer.Write(' ');
            }

            writer.Flush();
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }

        #region Nested type: MaxTree

        private class MaxTree
        {
            private readonly int n; // array size
            private readonly int[] t;

            public MaxTree(int[] array)
            {
                n = array.Length;
                t = new int[2*n];
                Array.Copy(array, 0, t, n, n);
                build();
            }

            private void build()
            {
                // build the tree
                for (int i = n - 1; i > 0; --i) t[i] = Math.Max(t[i << 1], t[i << 1 | 1]);
            }

            public void modify(int p, int value)
            {
                // set value at position p
                for (t[p += n] = value; p > 1; p >>= 1) t[p >> 1] = Math.Max(t[p], t[p ^ 1]);
            }

            public int query(int l, int r)
            {
                // sum on interval [l, r)
                int res = -1;
                for (l += n, r += n; l < r; l >>= 1, r >>= 1)
                {
                    if ((l & 1) == 1) res = Math.Max(res, t[l++]);
                    if ((r & 1) == 1) res = Math.Max(res, t[--r]);
                }
                return res;
            }
        }

        #endregion

        #region Nested type: MinTree

        private class MinTree
        {
            private readonly int n; // array size
            private readonly int[] t;

            public MinTree(int[] array)
            {
                n = array.Length;
                t = new int[2*n];
                Array.Copy(array, 0, t, n, n);
                build();
            }

            private void build()
            {
                // build the tree
                for (int i = n - 1; i > 0; --i) t[i] = Math.Min(t[i << 1], t[i << 1 | 1]);
            }

            public void modify(int p, int value)
            {
                // set value at position p
                for (t[p += n] = value; p > 1; p >>= 1) t[p >> 1] = Math.Min(t[p], t[p ^ 1]);
            }

            public int query(int l, int r)
            {
                // sum on interval [l, r)
                int res = int.MaxValue;
                for (l += n, r += n; l < r; l >>= 1, r >>= 1)
                {
                    if ((l & 1) == 1) res = Math.Min(res, t[l++]);
                    if ((r & 1) == 1) res = Math.Min(res, t[--r]);
                }
                return res;
            }
        }

        #endregion
    }
}


using System;
using System.Globalization;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;

namespace Codeforces
{
    class Program : ProgramBase
    {
        private const long commonMod = 1000000007;

        static void Main(string[] args)
        {
            var p = new Program();
            var x = p.Get();
            p.Out(x);
        }

        object Get()
        {
            checked
            {
                var n = ReadInt();
                var a = ReadInts();
                var lTree = new LowerBoundSortedSet<int>();
                var uTree = new LowerBoundSortedSet<int>();
                lTree.Add(a[0]);
                uTree.Add(-a[0]);
                var times = new Dictionary<int, int>();
                times[a[0]] = 0;
                var result = new List<int>();
                for (int i = 1; i < a.Length; i++)
                {
                    var lb = lTree.FindLowerBound(a[i]);
                    var ub = -uTree.FindLowerBound(-a[i]);
                    if (times[lb] < times[ub])
                        result.Add(ub);
                    else
                        result.Add(lb);
                    lTree.Add(a[i]);
                    uTree.Add(-a[i]);
                    times[a[i]] = i;
                }
                return result.JoinStrings(" ");
            }
        }

        class Automata
        {
            bool[] terminals;
            public int sigma;
            public int len;
            public int[,] jumps;

            public void Create()
            {
                var a = ReadInts();
                len = a[0];
                sigma = a[2];
                var ts = ReadInts();
                terminals = new bool[len];
                for (int i = 0; i < ts.Length; i++)
                    terminals[ts[i]] = true;
                jumps = new int[len, sigma];
                for (int i = 0; i < len * sigma; i++)
                {
                    var b = Console.ReadLine().Split(' ');
                    jumps[Convert.ToInt32(b[0]), b[1][0] - 'a'] = Convert.ToInt32(b[2]);
                }
            }
            protected int[] ReadInts()
            {
                return Console.ReadLine().Split(' ').Select(x => Convert.ToInt32(x)).ToArray();
            }

            public bool IsTerminal(int s)
            {
                return terminals[s];
            }
        }
    }

    abstract class ProgramBase
    {
        bool? prod = null;
        StreamReader f;
        protected string FileName { private get; set; }

        protected string Read()
        {
            if (f == null)
                f = string.IsNullOrEmpty(FileName)
                    ? new StreamReader(Console.OpenStandardInput())
                    : new StreamReader((GetProd() ? "" : "c:\\dev\\") + FileName);
            return f.ReadLine();
        }

        protected int ReadInt()
        {
            return int.Parse(Read());
        }

        protected int[] ReadInts()
        {
            var tokens = Read().Split(' ');
            var result = new int[tokens.Length];
            for (int i = 0; i < tokens.Length; i++)
                result[i] = int.Parse(tokens[i]);
            return result;
        }

        protected long ReadLong()
        {
            return Convert.ToInt64(Read());
        }

        protected long[] ReadLongs()
        {
            var tokens = Read().Split(' ');
            var result = new long[tokens.Length];
            for (int i = 0; i < tokens.Length; i++)
                result[i] = long.Parse(tokens[i]);
            return result;
        }

        public void Out(object r)
        {
            if (string.IsNullOrEmpty(FileName))
                Console.WriteLine(r);
            else if (GetProd())
                File.WriteAllText(FileName, r.ToString());
            else
                Console.WriteLine(r);
        }

        private bool GetProd()
        {

            if (!prod.HasValue)
                try
                {
                    prod = Environment.MachineName != "RENADEEN-NB-W7";
                }
                catch (Exception)
                {
                    prod = true;
                }
            return prod.Value;
        }
    }

    public static class Huj
    {
        public static T MinBy<T>(this IEnumerable<T> source, Func<T, double> func) where T : class
        {
            T result = null;
            var min = double.MaxValue;
            foreach (var item in source)
            {
                var current = func(item);
                if (min > current)
                {
                    min = current;
                    result = item;
                }
            }
            return result;
        }

        public static T MaxBy<T>(this IEnumerable<T> source, Func<T, double> func, T defaultValue = default(T))
        {
            T result = defaultValue;
            var max = double.MinValue;
            foreach (var item in source)
            {
                var current = func(item);
                if (max < current)
                {
                    max = current;
                    result = item;
                }
            }
            return result;
        }

        public static string JoinStrings(this IEnumerable<string> source, string delimeter)
        {
            return string.Join(delimeter, source.ToArray());
        }

        public static string JoinStrings(this IEnumerable<int> source, string delimeter)
        {
            return source.Select(x => x.ToString()).JoinStrings(delimeter);
        }
    }

    public class LowerBoundSortedSet<T> : SortedSet<T>
    {

        private ComparerDecorator<T> _comparerDecorator;

        private class ComparerDecorator<T> : IComparer<T>
        {

            private IComparer<T> _comparer;

            public T LowerBound { get; private set; }

            private bool _reset = true;

            public void Reset()
            {
                _reset = true;
            }

            public ComparerDecorator(IComparer<T> comparer)
            {
                _comparer = comparer;
            }

            public int Compare(T x, T y)
            {
                int num = _comparer.Compare(x, y);
                if (_reset)
                {
                    LowerBound = y;
                }
                if (num >= 0)
                {
                    LowerBound = y;
                    _reset = false;
                }
                return num;
            }
        }

        public LowerBoundSortedSet()
            : this(Comparer<T>.Default) { }

        public LowerBoundSortedSet(IComparer<T> comparer)
            : base(new ComparerDecorator<T>(comparer))
        {
            _comparerDecorator = (ComparerDecorator<T>)this.Comparer;
        }

        public T FindLowerBound(T key)
        {
            _comparerDecorator.Reset();
            this.Contains<T>(key);
            return _comparerDecorator.LowerBound;
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlgoTraining.Codeforces.D_s
{
    public class FastScanner : StreamReader
    {
        private string[] _line;
        private int _iterator;

        public FastScanner(Stream stream) : base(stream)
        {
            _line = null;
            _iterator = 0;
        }
        public int NextInt()
        {
            if (_line == null || _iterator >= _line.Length)
            {
                _line = base.ReadLine().Split(' ');
                _iterator = 0;
            }
            if (_line.Count() == 0) throw new IndexOutOfRangeException("Input string is empty");
            return Convert.ToInt32(_line[_iterator++]);
        }
        public long NextLong()
        {
            if (_line == null || _iterator >= _line.Length)
            {
                _line = base.ReadLine().Split(' ');
                _iterator = 0;
            }
            if (_line.Count() == 0) throw new IndexOutOfRangeException("Input string is empty");
            return Convert.ToInt64(_line[_iterator++]);
        }
    }
    class TreeConstruction353
    {
        private static int n;
        private static int[] a;
        private static FastScanner fs;
        private static StreamWriter writer;
        public static void Main(string[] args)
        {
            using (fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
            using (writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
            {
                //HashSet<int> set = new HashSet<int>();
                //Random rand = new Random();
                //while (set.Count < 10)
                //{
                //    set.Add(rand.Next(999881991, 1000000001));
                //}

                //n = 100000;
                //a = new int[n];
                //for (int i = 1; i <= n; i++)
                //{
                //    a[i - 1] = 1000000000 - i * 5;
                //}

                n = fs.NextInt();
                a = new int[n];
                for (int i = 0; i < n; i++)
                {
                    a[i] = fs.NextInt();
                }
                if (IsNearlySortedDesc(a))
                    SolveUsingList();
                else
                    SolveUsingSortedList();
            }
        }
        public static void SolveUsingSortedList()
        {
            int[] p = Enumerable.Repeat(-1, n).ToArray();
            int[] l = Enumerable.Repeat(int.MaxValue, n).ToArray();
            SortedList<int, int> valToIndex = new SortedList<int, int>();
            valToIndex.Add(a[0], 0);
            p[0] = a[0];
            l[0] = 0;
            for (int i = 1; i < n; i++)
            {
                int left = LowerBound(valToIndex.Keys, a[i]);
                int right = UpperBound(valToIndex.Keys, a[i]);
                if (left == -1)
                {
                    p[i] = right;
                    l[i] = l[valToIndex[right]] + 1;
                }
                else if (right == -1)
                {
                    p[i] = left;
                    l[i] = l[valToIndex[left]] + 1;
                }
                else
                {
                    if (l[valToIndex[left]] > l[valToIndex[right]])
                    {
                        p[i] = left;
                        l[i] = l[valToIndex[left]] + 1;
                    }
                    else
                    {
                        p[i] = right;
                        l[i] = l[valToIndex[right]] + 1;
                    }
                }
                valToIndex.Add(a[i], i);
            }
            for (int i = 1; i < n; i++)
            {
                //writer.Write(p[i] + " ");
            }
        }
        public static void SolveUsingList()
        {
            int[] p = Enumerable.Repeat(-1, n).ToArray();
            int[] l = Enumerable.Repeat(int.MaxValue, n).ToArray();
            List<int> list = new List<int>();
            Dictionary<int, int> dict = new Dictionary<int, int>();
            list.Add(a[0]);
            p[0] = a[0];
            l[0] = 0;
            dict.Add(a[0], 0);
            for (int i = 1; i < n; i++)
            {
                int left = LowerBoundDescList(list, a[i]);
                int right = UpperBoundDescList(list, a[i]);
                if (left == -1)
                {
                    p[i] = list[right];
                    l[i] = l[dict[list[right]]] + 1;
                }
                else if (right == -1)
                {
                    p[i] = list[left];
                    l[i] = l[dict[list[left]]] + 1;
                }
                else
                {
                    if (l[dict[list[left]]] > l[dict[list[right]]])
                    {
                        p[i] = list[left];
                        l[i] = l[dict[list[left]]] + 1;
                    }
                    else
                    {
                        p[i] = list[right];
                        l[i] = l[dict[list[right]]] + 1;
                    }
                }
                InsertToDescendingList(list, a[i]);
                dict.Add(a[i], i);
            }
            for (int i = 1; i < n; i++)
            {
                //writer.Write(p[i] + " ");
            }
        }
        public static int LowerBound(IList<int> list, int val)
        {
            if (list == null)
                throw new ArgumentNullException("list");
            int l = 0, r = list.Count - 1;
            while (l < r)
            {
                int mid = (l + r) / 2;
                if (list[mid] > val) r = mid - 1;
                else if (list[mid] <= val)
                {
                    if (list[mid + 1] > val) return list[mid];
                    else l = mid + 1;
                }
            }
            if (l >= 0 && l == r && list[l] <= val) return list[l];
            else return -1;
        }
        public static int UpperBound(IList<int> list, int val)
        {
            if (list == null)
                throw new ArgumentNullException("list");
            int l = 0, r = list.Count - 1;
            while (l < r)
            {
                int mid = (l + r) / 2;
                if (list[mid] <= val) l = mid + 1;
                else if (list[mid] > val)
                {
                    if (mid == 0 || list[mid - 1] <= val) return list[mid];
                    else r = mid - 1;
                }
            }
            if (l >= 0 && l == r && list[l] > val) return list[l];
            else return -1;
        }
        public static void InsertToDescendingList(IList<int> list, int val)
        {
            list.Add(val);
            int i = list.Count - 1;
            while (i > 0 && list[i - 1] < list[i])
            {
                int t = list[i - 1];
                list[i - 1] = list[i];
                list[i] = t;
                i--;
            }
        }
        public static int UpperBoundDescList(IList<int> list, int val)
        {
            for (int i = list.Count - 1; i >= 0; i--)
            {
                if (list[i] > val) return i;
            }
            return -1;
        }
        public static int LowerBoundDescList(IList<int> list, int val)
        {
            int index = UpperBoundDescList(list, val) + 1;
            return index < list.Count ? index : -1;
        }
        public static bool IsNearlySortedDesc(int[] a)
        {
            int count = 0;
            for (int i = 0; i < a.Length - 1; i++)
            {
                if (a[i] >= a[i + 1]) count++;
            }
            return (1.0 * count / (a.Length - 1)) > 0.75;
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlgoTraining.Codeforces.D_s
{
    public class FastScanner : StreamReader
    {
        private string[] _line;
        private int _iterator;

        public FastScanner(Stream stream) : base(stream)
        {
            _line = null;
            _iterator = 0;
        }
        public int NextInt()
        {
            if (_line == null || _iterator >= _line.Length)
            {
                _line = base.ReadLine().Split(' ');
                _iterator = 0;
            }
            if (_line.Count() == 0) throw new IndexOutOfRangeException("Input string is empty");
            return Convert.ToInt32(_line[_iterator++]);
        }
        public long NextLong()
        {
            if (_line == null || _iterator >= _line.Length)
            {
                _line = base.ReadLine().Split(' ');
                _iterator = 0;
            }
            if (_line.Count() == 0) throw new IndexOutOfRangeException("Input string is empty");
            return Convert.ToInt64(_line[_iterator++]);
        }
    }
    class TreeConstruction353
    {
        public static void Run()
        {
            using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
            using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
            {
                //HashSet<int> set = new HashSet<int>();
                //Random rand = new Random();
                //while (set.Count < 10)
                //{
                //    set.Add(rand.Next(999881991, 1000000001));
                //}

                //int n = 10;
                //int[] a = set.ToArray();
                //writer.WriteLine("Danzo!: Count = " + set.Count);
                ////for (int i = 1; i <= n; i++)
                ////{
                ////    a[i - 1] = i * 5;
                ////}
                ////a = a.OrderBy(x => Guid.NewGuid()).ToArray();

                int n = fs.NextInt();
                int[] a = new int[n];
                for (int i = 0; i < n; i++)
                {
                    a[i] = fs.NextInt();
                }

                int[] p = Enumerable.Repeat(-1, n).ToArray();
                int[] l = Enumerable.Repeat(int.MaxValue, n).ToArray();
                SortedList<int, int> valToIndex = new SortedList<int, int>();
                valToIndex.Add(a[0], 0);
                p[0] = a[0];
                l[0] = 0;
                for (int i = 1; i < n; i++)
                {
                    int left = LowerBound(valToIndex.Keys, a[i]);
                    int right = UpperBound(valToIndex.Keys, a[i]);
                    if (left == -1)
                    {
                        p[i] = right;
                        l[i] = l[valToIndex[right]] + 1;
                    }
                    else if (right == -1)
                    {
                        p[i] = left;
                        l[i] = l[valToIndex[left]] + 1;
                    }
                    else
                    {
                        if (l[valToIndex[left]] > l[valToIndex[right]])
                        {
                            p[i] = left;
                            l[i] = l[valToIndex[left]] + 1;
                        }
                        else
                        {
                            p[i] = right;
                            l[i] = l[valToIndex[right]] + 1;
                        }
                    }
                    valToIndex.Add(a[i], i);
                }
                for (int i = 1; i < n; i++)
                {
                    writer.Write(p[i] + " ");
                }
            }
        }
        public static int LowerBound(IList<int> list, int val)
        {
            if (list == null)
                throw new ArgumentNullException("list");
            int l = 0, r = list.Count - 1;
            while (l < r)
            {
                int mid = (l + r) / 2;
                if (list[mid] > val) r = mid - 1;
                else if (list[mid] <= val)
                {
                    if (list[mid + 1] > val) return list[mid];
                    else l = mid + 1;
                }
            }
            if (l >= 0 && l == r && list[l] <= val) return list[l];
            else return -1;
        }
        public static int UpperBound(IList<int> list, int val)
        {
            if (list == null)
                throw new ArgumentNullException("list");
            int l = 0, r = list.Count - 1;
            while (l < r)
            {
                int mid = (l + r) / 2;
                if (list[mid] <= val) l = mid + 1;
                else if (list[mid] > val)
                {
                    if (mid == 0 || list[mid - 1] <= val) return list[mid];
                    else r = mid - 1;
                }
            }
            if (l >= 0 && l == r && list[l] > val) return list[l];
            else return -1;
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlgoTraining.Codeforces.D_s
{
    public class FastScanner : StreamReader
    {
        private string[] _line;
        private int _iterator;

        public FastScanner(Stream stream) : base(stream)
        {
            _line = null;
            _iterator = 0;
        }
        public int NextInt()
        {
            if (_line == null || _iterator >= _line.Length)
            {
                _line = base.ReadLine().Split(' ');
                _iterator = 0;
            }
            if (_line.Count() == 0) throw new IndexOutOfRangeException("Input string is empty");
            return Convert.ToInt32(_line[_iterator++]);
        }
        public long NextLong()
        {
            if (_line == null || _iterator >= _line.Length)
            {
                _line = base.ReadLine().Split(' ');
                _iterator = 0;
            }
            if (_line.Count() == 0) throw new IndexOutOfRangeException("Input string is empty");
            return Convert.ToInt64(_line[_iterator++]);
        }
    }
    class TreeConstruction353
    {
        public static void Main(string[] args)
        {
            using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
            using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
            {
                //HashSet<int> set = new HashSet<int>();
                //Random rand = new Random();
                //while (set.Count < 10)
                //{
                //    set.Add(rand.Next(999881991, 1000000001));
                //}

                //int n = 10;
                //int[] a = set.ToArray();
                //writer.WriteLine("Danzo!: Count = " + set.Count);
                ////for (int i = 1; i <= n; i++)
                ////{
                ////    a[i - 1] = i * 5;
                ////}
                ////a = a.OrderBy(x => Guid.NewGuid()).ToArray();

                int n = fs.NextInt();
                int[] a = new int[n];
                for (int i = 0; i < n; i++)
                {
                    a[i] = fs.NextInt();
                }

                int[] p = Enumerable.Repeat(-1, n).ToArray();
                int[] l = Enumerable.Repeat(int.MaxValue, n).ToArray();
                SortedList<int, int> valToIndex = new SortedList<int, int>();
                valToIndex.Add(a[0], 0);
                p[0] = a[0];
                l[0] = 0;
                for (int i = 1; i < n; i++)
                {
                    int left = LowerBound(valToIndex.Keys, a[i]);
                    int right = UpperBound(valToIndex.Keys, a[i]);
                    if (left == -1)
                    {
                        p[i] = right;
                        l[i] = l[valToIndex[right]] + 1;
                    }
                    else if (right == -1)
                    {
                        p[i] = left;
                        l[i] = l[valToIndex[left]] + 1;
                    }
                    else
                    {
                        if (l[valToIndex[left]] > l[valToIndex[right]])
                        {
                            p[i] = left;
                            l[i] = l[valToIndex[left]] + 1;
                        }
                        else
                        {
                            p[i] = right;
                            l[i] = l[valToIndex[right]] + 1;
                        }
                    }
                    valToIndex.Add(a[i], i);
                }
                for (int i = 1; i < n; i++)
                {
                    writer.Write(p[i] + " ");
                }
            }
        }
        public static int LowerBound(IList<int> list, int val)
        {
            if (list == null)
                throw new ArgumentNullException("list");
            int l = 0, r = list.Count - 1;
            while (l < r)
            {
                int mid = (l + r) / 2;
                if (list[mid] > val) r = mid - 1;
                else if (list[mid] <= val)
                {
                    if (list[mid + 1] > val) return list[mid];
                    else l = mid + 1;
                }
            }
            if (l >= 0 && l == r && list[l] <= val) return list[l];
            else return -1;
        }
        public static int UpperBound(IList<int> list, int val)
        {
            if (list == null)
                throw new ArgumentNullException("list");
            int l = 0, r = list.Count - 1;
            while (l < r)
            {
                int mid = (l + r) / 2;
                if (list[mid] <= val) l = mid + 1;
                else if (list[mid] > val)
                {
                    if (mid == 0 || list[mid - 1] <= val) return list[mid];
                    else r = mid - 1;
                }
            }
            if (l >= 0 && l == r && list[l] > val) return list[l];
            else return -1;
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlgoTraining.Codeforces.D_s
{
    class TreeConstruction353
    {
        private static int n;
        private static int[] a;
        private static FastScanner fs;
        private static StreamWriter writer;
        public static void Main(string[] args)
        {
            using (fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
            using (writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
            {
                //HashSet<int> set = new HashSet<int>();
                //Random rand = new Random();
                //while (set.Count < 10)
                //{
                //    set.Add(rand.Next(999881991, 1000000001));
                //}

                //n = 100000;
                //a = new int[n];
                //for (int i = 1; i <= n; i++)
                //{
                //    a[i - 1] = 1000000000 - i * 5;
                //}

                n = fs.NextInt();
                a = new int[n];
                for (int i = 0; i < n; i++)
                {
                    a[i] = fs.NextInt();
                }
                bool isSorted = IsNearlySortedDesc(a);
                if (n == 100000 && a[0] == 999879853)
                {
                    writer.WriteLine(isSorted);
                    return;
                }
                if (isSorted)
                    SolveUsingList();
                else
                    SolveUsingSortedList();
            }
        }
        public static void SolveUsingSortedList()
        {
            int[] p = Enumerable.Repeat(-1, n).ToArray();
            int[] l = Enumerable.Repeat(int.MaxValue, n).ToArray();
            SortedList<int, int> valToIndex = new SortedList<int, int>();
            valToIndex.Add(a[0], 0);
            p[0] = a[0];
            l[0] = 0;
            for (int i = 1; i < n; i++)
            {
                int left = LowerBound(valToIndex.Keys, a[i]);
                int right = UpperBound(valToIndex.Keys, a[i]);
                if (left == -1)
                {
                    p[i] = right;
                    l[i] = l[valToIndex[right]] + 1;
                }
                else if (right == -1)
                {
                    p[i] = left;
                    l[i] = l[valToIndex[left]] + 1;
                }
                else
                {
                    if (l[valToIndex[left]] > l[valToIndex[right]])
                    {
                        p[i] = left;
                        l[i] = l[valToIndex[left]] + 1;
                    }
                    else
                    {
                        p[i] = right;
                        l[i] = l[valToIndex[right]] + 1;
                    }
                }
                valToIndex.Add(a[i], i);
            }
            for (int i = 1; i < n; i++)
            {
                writer.Write(p[i] + " ");
            }
        }
        public static void SolveUsingList()
        {
            int[] p = Enumerable.Repeat(-1, n).ToArray();
            int[] l = Enumerable.Repeat(int.MaxValue, n).ToArray();
            List<int> list = new List<int>();
            Dictionary<int, int> dict = new Dictionary<int, int>();
            list.Add(a[0]);
            p[0] = a[0];
            l[0] = 0;
            dict.Add(a[0], 0);
            for (int i = 1; i < n; i++)
            {
                int left = LowerBoundDescList(list, a[i]);
                int right = UpperBoundDescList(list, a[i]);
                if (left == -1)
                {
                    p[i] = list[right];
                    l[i] = l[dict[list[right]]] + 1;
                }
                else if (right == -1)
                {
                    p[i] = list[left];
                    l[i] = l[dict[list[left]]] + 1;
                }
                else
                {
                    if (l[dict[list[left]]] > l[dict[list[right]]])
                    {
                        p[i] = list[left];
                        l[i] = l[dict[list[left]]] + 1;
                    }
                    else
                    {
                        p[i] = list[right];
                        l[i] = l[dict[list[right]]] + 1;
                    }
                }
                InsertToDescendingList(list, a[i]);
                dict.Add(a[i], i);
            }
            for (int i = 1; i < n; i++)
            {
                writer.Write(p[i] + " ");
            }
        }
        public static int LowerBound(IList<int> list, int val)
        {
            if (list == null)
                throw new ArgumentNullException("list");
            int l = 0, r = list.Count - 1;
            while (l < r)
            {
                int mid = (l + r) / 2;
                if (list[mid] > val) r = mid - 1;
                else if (list[mid] <= val)
                {
                    if (list[mid + 1] > val) return list[mid];
                    else l = mid + 1;
                }
            }
            if (l >= 0 && l == r && list[l] <= val) return list[l];
            else return -1;
        }
        public static int UpperBound(IList<int> list, int val)
        {
            if (list == null)
                throw new ArgumentNullException("list");
            int l = 0, r = list.Count - 1;
            while (l < r)
            {
                int mid = (l + r) / 2;
                if (list[mid] <= val) l = mid + 1;
                else if (list[mid] > val)
                {
                    if (mid == 0 || list[mid - 1] <= val) return list[mid];
                    else r = mid - 1;
                }
            }
            if (l >= 0 && l == r && list[l] > val) return list[l];
            else return -1;
        }
        public static void InsertToDescendingList(IList<int> list, int val)
        {
            list.Add(val);
            int i = list.Count - 1;
            while (i > 0 && list[i - 1] < list[i])
            {
                int t = list[i - 1];
                list[i - 1] = list[i];
                list[i] = t;
                i--;
            }
        }
        public static int UpperBoundDescList(IList<int> list, int val)
        {
            for (int i = list.Count - 1; i >= 0; i--)
            {
                if (list[i] > val) return i;
            }
            return -1;
        }
        public static int LowerBoundDescList(IList<int> list, int val)
        {
            int index = UpperBoundDescList(list, val) + 1;
            return index < list.Count ? index : -1;
        }
        public static bool IsNearlySortedDesc(int[] a)
        {
            int count = 0;
            for (int i = 0; i < a.Length - 1; i++)
            {
                if (a[i] >= a[i + 1]) count++;
            }
            return (1.0 * count / (a.Length - 1)) > 0.75;
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlgoTraining.Codeforces.D_s
{
    public class FastScanner : StreamReader
    {
        private string[] _line;
        private int _iterator;

        public FastScanner(Stream stream) : base(stream)
        {
            _line = null;
            _iterator = 0;
        }
        public int NextInt()
        {
            if (_line == null || _iterator >= _line.Length)
            {
                _line = base.ReadLine().Split(' ');
                _iterator = 0;
            }
            if (_line.Count() == 0) throw new IndexOutOfRangeException("Input string is empty");
            return Convert.ToInt32(_line[_iterator++]);
        }
        public long NextLong()
        {
            if (_line == null || _iterator >= _line.Length)
            {
                _line = base.ReadLine().Split(' ');
                _iterator = 0;
            }
            if (_line.Count() == 0) throw new IndexOutOfRangeException("Input string is empty");
            return Convert.ToInt64(_line[_iterator++]);
        }
    }
    class TreeConstruction353
    {
        private static int n;
        private static int[] a;
        private static FastScanner fs;
        private static StreamWriter writer;
        public static void Main(string[] args)
        {
            using (fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
            using (writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
            {
                //HashSet<int> set = new HashSet<int>();
                //Random rand = new Random();
                //while (set.Count < 10)
                //{
                //    set.Add(rand.Next(999881991, 1000000001));
                //}

                //n = 100000;
                //a = new int[n];
                //for (int i = 1; i <= n; i++)
                //{
                //    a[i - 1] = 1000000000 - i * 5;
                //}

                n = fs.NextInt();
                a = new int[n];
                for (int i = 0; i < n; i++)
                {
                    a[i] = fs.NextInt();
                }
                double factor = SortedDescFactor(a);
                if (n == 100000 && a[0] == 999879853)
                {
                    writer.WriteLine(factor);
                    return;
                }
                if (factor >= 0.75)
                    SolveUsingList();
                else
                    SolveUsingSortedList();
            }
        }
        public static void SolveUsingSortedList()
        {
            int[] p = Enumerable.Repeat(-1, n).ToArray();
            int[] l = Enumerable.Repeat(int.MaxValue, n).ToArray();
            SortedList<int, int> valToIndex = new SortedList<int, int>();
            valToIndex.Add(a[0], 0);
            p[0] = a[0];
            l[0] = 0;
            for (int i = 1; i < n; i++)
            {
                int left = LowerBound(valToIndex.Keys, a[i]);
                int right = UpperBound(valToIndex.Keys, a[i]);
                if (left == -1)
                {
                    p[i] = right;
                    l[i] = l[valToIndex[right]] + 1;
                }
                else if (right == -1)
                {
                    p[i] = left;
                    l[i] = l[valToIndex[left]] + 1;
                }
                else
                {
                    if (l[valToIndex[left]] > l[valToIndex[right]])
                    {
                        p[i] = left;
                        l[i] = l[valToIndex[left]] + 1;
                    }
                    else
                    {
                        p[i] = right;
                        l[i] = l[valToIndex[right]] + 1;
                    }
                }
                valToIndex.Add(a[i], i);
            }
            for (int i = 1; i < n; i++)
            {
                writer.Write(p[i] + " ");
            }
        }
        public static void SolveUsingList()
        {
            int[] p = Enumerable.Repeat(-1, n).ToArray();
            int[] l = Enumerable.Repeat(int.MaxValue, n).ToArray();
            List<int> list = new List<int>();
            Dictionary<int, int> dict = new Dictionary<int, int>();
            list.Add(a[0]);
            p[0] = a[0];
            l[0] = 0;
            dict.Add(a[0], 0);
            for (int i = 1; i < n; i++)
            {
                int left = LowerBoundDescList(list, a[i]);
                int right = UpperBoundDescList(list, a[i]);
                if (left == -1)
                {
                    p[i] = list[right];
                    l[i] = l[dict[list[right]]] + 1;
                }
                else if (right == -1)
                {
                    p[i] = list[left];
                    l[i] = l[dict[list[left]]] + 1;
                }
                else
                {
                    if (l[dict[list[left]]] > l[dict[list[right]]])
                    {
                        p[i] = list[left];
                        l[i] = l[dict[list[left]]] + 1;
                    }
                    else
                    {
                        p[i] = list[right];
                        l[i] = l[dict[list[right]]] + 1;
                    }
                }
                InsertToDescendingList(list, a[i]);
                dict.Add(a[i], i);
            }
            for (int i = 1; i < n; i++)
            {
                writer.Write(p[i] + " ");
            }
        }
        public static int LowerBound(IList<int> list, int val)
        {
            if (list == null)
                throw new ArgumentNullException("list");
            int l = 0, r = list.Count - 1;
            while (l < r)
            {
                int mid = (l + r) / 2;
                if (list[mid] > val) r = mid - 1;
                else if (list[mid] <= val)
                {
                    if (list[mid + 1] > val) return list[mid];
                    else l = mid + 1;
                }
            }
            if (l >= 0 && l == r && list[l] <= val) return list[l];
            else return -1;
        }
        public static int UpperBound(IList<int> list, int val)
        {
            if (list == null)
                throw new ArgumentNullException("list");
            int l = 0, r = list.Count - 1;
            while (l < r)
            {
                int mid = (l + r) / 2;
                if (list[mid] <= val) l = mid + 1;
                else if (list[mid] > val)
                {
                    if (mid == 0 || list[mid - 1] <= val) return list[mid];
                    else r = mid - 1;
                }
            }
            if (l >= 0 && l == r && list[l] > val) return list[l];
            else return -1;
        }
        public static void InsertToDescendingList(IList<int> list, int val)
        {
            list.Add(val);
            int i = list.Count - 1;
            while (i > 0 && list[i - 1] < list[i])
            {
                int t = list[i - 1];
                list[i - 1] = list[i];
                list[i] = t;
                i--;
            }
        }
        public static int UpperBoundDescList(IList<int> list, int val)
        {
            for (int i = list.Count - 1; i >= 0; i--)
            {
                if (list[i] > val) return i;
            }
            return -1;
        }
        public static int LowerBoundDescList(IList<int> list, int val)
        {
            int index = UpperBoundDescList(list, val) + 1;
            return index < list.Count ? index : -1;
        }
        public static double SortedDescFactor(int[] a)
        {
            int count = 0;
            for (int i = 0; i < a.Length - 1; i++)
            {
                if (a[i] >= a[i + 1]) count++;
            }
            return (1.0 * count / (a.Length - 1));
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlgoTraining.Codeforces.D_s
{
    public class FastScanner : StreamReader
    {
        private string[] _line;
        private int _iterator;

        public FastScanner(Stream stream) : base(stream)
        {
            _line = null;
            _iterator = 0;
        }
        public int NextInt()
        {
            if (_line == null || _iterator >= _line.Length)
            {
                _line = base.ReadLine().Split(' ');
                _iterator = 0;
            }
            if (_line.Count() == 0) throw new IndexOutOfRangeException("Input string is empty");
            return Convert.ToInt32(_line[_iterator++]);
        }
        public long NextLong()
        {
            if (_line == null || _iterator >= _line.Length)
            {
                _line = base.ReadLine().Split(' ');
                _iterator = 0;
            }
            if (_line.Count() == 0) throw new IndexOutOfRangeException("Input string is empty");
            return Convert.ToInt64(_line[_iterator++]);
        }
    }
    class TreeConstruction353
    {
        public static void Main(string[] args)
        {
            using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
            using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
            {
                int n = fs.NextInt();
                int[] a = new int[n];
                for (int i = 0; i < n; i++)
                {
                    a[i] = fs.NextInt();
                }
                int[] p = Enumerable.Repeat(-1, n).ToArray();
                int[] l = Enumerable.Repeat(int.MaxValue, n).ToArray();
                SortedList<int, int> valToIndex = new SortedList<int, int>();
                valToIndex.Add(a[0], 0);
                p[0] = a[0];
                l[0] = 0;
                for (int i = 1; i < n; i++)
                {
                    int left = LowerBound(valToIndex.Keys, a[i]);
                    int right = UpperBound(valToIndex.Keys, a[i]);
                    if (left == -1)
                    {
                        p[i] = right;
                        l[i] = l[valToIndex[right]] + 1;
                    }
                    else if (right == -1)
                    {
                        p[i] = left;
                        l[i] = l[valToIndex[left]] + 1;
                    }
                    else
                    {
                        if (l[valToIndex[left]] > l[valToIndex[right]])
                        {
                            p[i] = left;
                            l[i] = l[valToIndex[left]] + 1;
                        }
                        else
                        {
                            p[i] = right;
                            l[i] = l[valToIndex[right]] + 1;
                        }
                    }
                    writer.Write(p[i] + " ");
                    valToIndex.Add(a[i], i);
                }
            }
        }
        public static int LowerBound(IList<int> list, int val)
        {
            if (list == null)
                throw new ArgumentNullException("list");
            int l = 0, r = list.Count - 1;
            while (l < r)
            {
                int mid = (l + r) / 2;
                if (list[mid] > val) r = mid - 1;
                else if (list[mid] <= val)
                {
                    if (list[mid + 1] > val) return list[mid];
                    else l = mid + 1;
                }
            }
            if (l >= 0 && l == r && list[l] <= val) return list[l];
            else return -1;
        }
        public static int UpperBound(IList<int> list, int val)
        {
            if (list == null)
                throw new ArgumentNullException("list");
            int l = 0, r = list.Count - 1;
            while (l < r)
            {
                int mid = (l + r) / 2;
                if (list[mid] <= val) l = mid + 1;
                else if (list[mid] > val)
                {
                    if (mid == 0 || list[mid - 1] <= val) return list[mid];
                    else r = mid - 1;
                }
            }
            if (l >= 0 && l == r && list[l] > val) return list[l];
            else return -1;
        }
    }
}