def partition(A, p, r):

    x = A[r - 1]

    i = p - 1

    for j in range(p, r-1):

        if A[j] <= x:

            i += 1

            temp = A[i]

            A[i] = A[j]

            A[j] = temp

    temp = A[i+1]

    A[i+1] = A[r-1]

    A[r-1] = temp

    return i









if __name__ == '__main__':

    # ??????????????\???

    # A = [13, 19, 9, 5, 12, 8, 7, 4, 21, 2, 6, 11]

    num_of_data = int(eval(input()))

    A = [int(x) for x in input().split(' ')]



    # ???????????????

    p = partition(A, 0, len(A))



    # ???????????¨???

    left = A[:p+1]

    partition = A[p+1]

    right = A[p+2:]

    print(('{0} [{1}] {2}'.format(' '.join(map(str, left)), partition, ' '.join(map(str, right)))))
def partition(A, p, r):

    x = A[r]

    i = p - 1

    for j in range(p, r):

        if A[j] <= x:

            i += 1

            temp = A[i]

            A[i] = A[j]

            A[j] = temp

    temp = A[i+1]

    A[i+1] = A[r]

    A[r] = temp

    return i









if __name__ == '__main__':

    # ??????????????\???

    # A = [13, 19, 9, 5, 12, 8, 7, 4, 21, 2, 6, 11]

    num_of_data = int(eval(input()))

    A = [int(x) for x in input().split(' ')]



    # ???????????????

    p = partition(A, 0, len(A)-1)



    # ???????????¨???

    left = A[:p+1]

    partition = A[p+1]

    right = A[p+2:]

    print(('{0} [{1}] {2}'.format(' '.join(map(str, left)), partition, ' '.join(map(str, right)))))
def partition(A, p, r):
    x = A[r - 1]
    i = p - 1
    for j in range(p, r - 1):
        if A[j] <= x:
            i += 1
            temp = A[i]
            A[i] = A[j]
            A[j] = temp
    temp = A[i + 1]
    A[i + 1] = A[r - 1]
    A[r - 1] = temp
    return i


if __name__ == "__main__":
    # ??????????????\???
    # A = [13, 19, 9, 5, 12, 8, 7, 4, 21, 2, 6, 11]
    num_of_data = int(eval(input()))
    A = [int(x) for x in input().split(" ")]
    # ???????????????
    p = partition(A, 0, len(A))
    # ???????????¨???
    left = A[: p + 1]
    partition = A[p + 1]
    right = A[p + 2 :]
    print(
        (
            "{0} [{1}] {2}".format(
                " ".join(map(str, left)), partition, " ".join(map(str, right))
            )
        )
    )

def partition(A, p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            temp = A[i]
            A[i] = A[j]
            A[j] = temp
    temp = A[i + 1]
    A[i + 1] = A[r]
    A[r] = temp
    return i


if __name__ == "__main__":
    # ??????????????\???
    # A = [13, 19, 9, 5, 12, 8, 7, 4, 21, 2, 6, 11]
    num_of_data = int(eval(input()))
    A = [int(x) for x in input().split(" ")]
    # ???????????????
    p = partition(A, 0, len(A) - 1)
    # ???????????¨???
    left = A[: p + 1]
    partition = A[p + 1]
    right = A[p + 2 :]
    print(
        (
            "{0} [{1}] {2}".format(
                " ".join(map(str, left)), partition, " ".join(map(str, right))
            )
        )
    )

-    x = A[r - 1] +    x = A[r] -    for j in range(p, r - 1): +    for j in range(p, r): -    A[i + 1] = A[r - 1] -    A[r - 1] = temp +    A[i + 1] = A[r] +    A[r] = temp -    p = partition(A, 0, len(A)) +    p = partition(A, 0, len(A) - 1)