#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

int v, n;
map<int, int> lm, rm;
set<int> s;

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> v;
    s.insert(v);
    for(int i=1;i<n;i++) {
        cin >> v;
        auto it = lower_bound(s.begin(), s.end(), v);
        auto its = it;  its--;
        int dab;
        if(it == s.begin())
            lm[*it] = v, dab = *it;
        else if(it == s.end())
            rm[*its] = v, dab = *its;
        else {
            int l = *its, r = *it;
            if(rm.count(l) == 0)
                rm[l] = v, dab = l;
            else
                lm[r] = v, dab = r;
        }
        s.insert(v);
        cout << dab << ' ';
    }
    cout << endl;
}


#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

int v, n;
map<int, int> lm, rm;
set<int> s;

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> v;
    s.insert(v);
    for(int i=1;i<n;i++) {
        cin >> v;
        auto it = lower_bound(s.begin(), s.end(), v), its = it;
        its--;
        int dab;
        if(it == s.begin())
            lm[*it] = v, dab = *it;
        else if(it == s.end())
            rm[*its] = v, dab = *its;
        else {
            int l = *its, r = *it;
            if(rm[l] == 0)
                rm[l] = v, dab = l;
            else
                lm[r] = v, dab = r;
        }
        s.insert(v);
        cout << dab << ' ';
    }
    cout << endl;
}


#include <cstdio>
#include <vector>
using namespace std;

struct node {
    int left, right;
    node() :left(-1), right(-1) {}
};

int n, v[111111], head;
node tree[111111];
int parent[111111];

void append(int here, int val) {
    if(here < val) {
        if(tree[here].right == -1) {
            tree[here].right = val;
            parent[val] = here;
            return;
        }
        append(tree[here].right, val);
    }
    if(here > val) {
        if(tree[here].left == -1) {
            tree[here].left = val;
            parent[val] = here;
            return ;
        }
        append(tree[here].left, val);
    }
}

int main () {
    scanf("%d",&n);
    for(int i=0;i<n;i++) {
        scanf("%d",v+i);
    }
    head = v[0];
    for(int i=1;i<n;i++) {
        append(head,v[i]);
    }
    for(int i=1;i<n;i++) {
        printf("%d ",parent[v[i]]);
    }
    printf("\n");
}


#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

int v, n;
map<int, int> lm, rm;
set<int> s;

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> v;
    s.insert(v);
    for(int i=1;i<n;i++) {
        cin >> v;
        auto it = lower_bound(s.begin(), s.end(), v);
        // auto its = it;  its--;
        int dab;
        if(it != s.end() && lm.count(*it) == 0) {
            lm[*it] = v;
            dab = *it;
        }
        else {
            it--;
            rm[*it] = v;
            dab = *it;
        }
        s.insert(v);
        cout << dab << ' ';
    }
    cout << endl;
}


#include <bits/stdc++.h>

using namespace std;

int N;
int a[100001];

int parent[100001];

map<int, int> ckr, ckl, parl, parr;

int main()
{
	scanf("%d", &N);
	for(int i=0; i < N; i++)
		scanf("%d", &a[i]);

	ckr[a[0]] = 1;
	ckl[-a[0]] = 1;
	parl[a[0]] = a[0];
	parr[a[0]] = a[0];

	for(int i=1; i < N; i++)
	{
		auto findr = ckr.lower_bound(a[i]);
		auto findl = ckl.lower_bound(-a[i]);
		if(findr == ckr.end())
		{
			int l = -findl->first;
			parent[i] = l;
			parl[a[i]] = l;
			parr[a[i]] = a[i];
			ckl.erase(findl);
		}
		else if(findl == ckl.end())
		{
			int r = findr->first;
			parent[i] = r;
			parl[a[i]] = a[i];
			parr[a[i]] = r;
			ckr.erase(findr);
		}
		else
		{
			int l = -findl->first;
			int r = findr->first;
			int pr = parr[r];
			int pl = parl[r];

			if(pl < a[i] && a[i] < pr)
			{
				parent[i] = r;
				parl[a[i]] = a[i];
				parr[a[i]] = r;
				ckr.erase(findr);
			}

			else if(a[i] < pl && a[i] < pr)
			{
				parent[i] = r;
				parl[a[i]] = a[i];
				parr[a[i]] = r;
				ckr.erase(findr);
			}
			else
			{
				pr = parr[l];
				pl = parl[l];
				if((pl < a[i] && a[i] < pr) || (pl < a[i] && pr < a[i]))
				{
					parent[i] = l;
					parl[a[i]] = l;
					parr[a[i]] = a[i];
					ckl.erase(findl);
				}
			}

		}

		ckl[-a[i]] = 1;
		ckr[a[i]] = 1;
	}

	for(int i=1; i < N; i++)
		printf("%d ", parent[i]);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int N;
int a[100001];

int parent[100001];

map<int, int> ckr, ckl, par;

int main()
{
	scanf("%d", &N);
	for(int i=0; i < N; i++)
		scanf("%d", &a[i]);

	ckr[a[0]] = 1;
	ckl[-a[0]] = 1;

	for(int i=1; i < N; i++)
	{
		auto findr = ckr.lower_bound(a[i]);
		auto findl = ckl.lower_bound(-a[i]);
		if(findr == ckr.end())
		{
			int l = -findl->first;
			parent[i] = l;
			par[a[i]] = l;
			ckl.erase(findl);
		}
		else if(findl == ckl.end())
		{
			int r = findr->first;
			parent[i] = r;
			par[a[i]] = r;
			ckr.erase(findr);
		}
		else
		{
			int l = -findl->first;
			int r = findr->first;
			int pr = par[r];
			int pl = par[l];
			if(pr < r)
			{
				if(a[i] < pr)
				{
					parent[i] = l;
					par[a[i]] = l;
					ckl.erase(findl);
				}
				else
				{
					parent[i] = r;
					par[a[i]] = r;
					ckr.erase(findr);
				}
			}
			else if(l < pl)
			{
				if(a[i] < pl)
				{
					parent[i] = l;
					par[a[i]] = l;
					ckl.erase(findl);
				}
				else
				{
					parent[i] = r;
					par[a[i]] = r;
					ckr.erase(findr);
				}
			}
			else
			{
				while(1);
			}
		}

		ckl[-a[i]] = 1;
		ckr[a[i]] = 1;
	}

	for(int i=1; i < N; i++)
		printf("%d ", parent[i]);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int N;
int a[100001];

int parent[100001];

map<int, int> ckr, ckl, parl, parr, par;

int main()
{
	scanf("%d", &N);
	for(int i=0; i < N; i++)
		scanf("%d", &a[i]);

	ckr[a[0]] = 1;
	ckl[-a[0]] = 1;
	parl[a[0]] = a[0];
	parr[a[0]] = a[0];

	for(int i=1; i < N; i++)
	{
		auto findr = ckr.lower_bound(a[i]);
		auto findl = ckl.lower_bound(-a[i]);
		if(findr == ckr.end())
		{
			int l = -findl->first;
			parent[i] = l;
			parl[a[i]] = l;
			parr[a[i]] = a[i];
			par[a[i]] = l;
			ckl.erase(findl);
		}
		else if(findl == ckl.end())
		{
			int r = findr->first;
			parent[i] = r;
			parl[a[i]] = a[i];
			parr[a[i]] = r;
			par[a[i]] = r;
			ckr.erase(findr);
		}
		else
		{
			int l = -findl->first;
			int r = findr->first;
			int pr = parr[r];
			int pl = parl[r];
			int p = par[r];

			if(pl < a[i] && a[i] < pr)
			{
				parent[i] = r;
				parl[a[i]] = a[i];
				parr[a[i]] = r;
				par[a[i]] = r;
				ckr.erase(findr);
			}

			else if(a[i] < pl && a[i] < pr && r == pl)
			{
				parent[i] = r;
				parl[a[i]] = a[i];
				parr[a[i]] = r;
				par[a[i]] = r;
				ckr.erase(findr);
			}
			else
			{
				pr = parr[l];
				pl = parl[l];
				p = par[l];
				//if((pl < a[i] && a[i] < pr) || (pl < a[i] && pr < a[i]))
				{
					parent[i] = l;
					parl[a[i]] = l;
					parr[a[i]] = a[i];
					par[a[i]] = l;
					ckl.erase(findl);
				}
			}

		}

		ckl[-a[i]] = 1;
		ckr[a[i]] = 1;
	}

	for(int i=1; i < N; i++)
		printf("%d ", parent[i]);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int INF = 1000000001;
int N;
int a[100001];

int parent[100001];

map<int, int> used;

int main()
{
	scanf("%d", &N);
	for(int i=0; i < N; i++)
		scanf("%d", &a[i]);

	set<int> ck;
	ck.insert(a[0]);

	for(int i=1; i < N; i++)
	{
		auto lo = ck.lower_bound(a[i]);
		if(lo == ck.end())
			lo--;
		if(*lo > a[i] && used[*lo])
			lo--;
		used[*lo] = 1;
		parent[i] = *lo;
		ck.insert(a[i]);
	}

	for(int i=1; i < N; i++)
		printf("%d ", parent[i]);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int N;
int a[100001];

int parent[100001];

map<int, int> ckr, ckl, par;

int main()
{
	scanf("%d", &N);
	for(int i=0; i < N; i++)
		scanf("%d", &a[i]);

	ckr[a[0]] = 1;
	ckl[-a[0]] = 1;

	for(int i=1; i < N; i++)
	{
		auto findr = ckr.lower_bound(a[i]);
		auto findl = ckl.lower_bound(-a[i]);
		if(findr == ckr.end())
		{
			int l = -findl->first;
			parent[i] = l;
			par[a[i]] = l;
			ckl.erase(findl);
		}
		else if(findl == ckl.end())
		{
			int r = findr->first;
			parent[i] = r;
			par[a[i]] = r;
			ckr.erase(findr);
		}
		else
		{
			int l = -findl->first;
			int r = findr->first;
			int pr = par[r];
			int pl = par[l];
			if(a[i] < pr)
			{
				parent[i] = l;
				par[a[i]] = l;
				ckl.erase(findl);
			}
			else
			{
				parent[i] = r;
				par[a[i]] = r;
				ckl.erase(findr);
			}
		}

		ckl[-a[i]] = 1;
		ckr[a[i]] = 1;
	}

	for(int i=1; i < N; i++)
		printf("%d ", parent[i]);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int N;
int a[100001];

int parent[100001];

map<int, int> ckr, ckl, par;

int main()
{
	scanf("%d", &N);
	for(int i=0; i < N; i++)
		scanf("%d", &a[i]);

	ckr[a[0]] = 1;
	ckl[-a[0]] = 1;

	for(int i=1; i < N; i++)
	{
		auto findr = ckr.lower_bound(a[i]);
		auto findl = ckl.lower_bound(-a[i]);
		if(findr == ckr.end())
		{
			int l = -findl->first;
			parent[i] = l;
			par[a[i]] = l;
			ckl.erase(findl);
		}
		else if(findl == ckl.end())
		{
			int r = findr->first;
			parent[i] = r;
			par[a[i]] = r;
			ckr.erase(findr);
		}
		else
		{
			int l = -findl->first;
			int r = findr->first;
			int pr = par[r];
			int pl = par[l];
			if(a[i] < pr)
			{
				parent[i] = l;
				par[a[i]] = l;
				ckl.erase(findl);
			}
			else
			{
				parent[i] = r;
				par[a[i]] = r;
				ckr.erase(findr);
			}
		}

		ckl[-a[i]] = 1;
		ckr[a[i]] = 1;
	}

	for(int i=1; i < N; i++)
		printf("%d ", parent[i]);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int INF = 1000000001;
int N;
int a[100001];

int parent[100001];

map<int, int> ckr, ckl, parl, parr, par;

int main()
{
	scanf("%d", &N);
	for(int i=0; i < N; i++)
		scanf("%d", &a[i]);

	ckr[a[0]] = 1;
	ckl[-a[0]] = 1;
	parl[a[0]] = 0;
	parr[a[0]] = INF;

	for(int i=1; i < N; i++)
	{
		auto findr = ckr.lower_bound(a[i]);
		auto findl = ckl.lower_bound(-a[i]);
		if(findr == ckr.end())
		{
			int l = -findl->first;
			parent[i] = l;
			parl[a[i]] = l;
			parr[a[i]] = INF;
			par[a[i]] = l;
			ckl.erase(findl);
		}
		else if(findl == ckl.end())
		{
			int r = findr->first;
			parent[i] = r;
			parl[a[i]] = 0;
			parr[a[i]] = r;
			par[a[i]] = r;
			ckr.erase(findr);
		}
		else
		{
			int l = -findl->first;
			int r = findr->first;
			int pr = parr[r];
			int pl = parl[r];
			int p = par[r];

			if(pl < a[i] && a[i] < pr)
			{
				parent[i] = r;
				parl[a[i]] = pl;
				parr[a[i]] = r;
				par[a[i]] = r;
				ckr.erase(findr);
			}

			else
			{
				pr = parr[l];
				pl = parl[l];
				p = par[l];

				parent[i] = l;
				parl[a[i]] = l;
				parr[a[i]] = pr;
				par[a[i]] = l;
				ckl.erase(findl);
			}

		}

		ckl[-a[i]] = 1;
		ckr[a[i]] = 1;
	}

	for(int i=1; i < N; i++)
		printf("%d ", parent[i]);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int INF = 1000000001;
int N;
int a[100001];

int parent[100001];

map<int, int> used;

int main()
{
	scanf("%d", &N);
	for(int i=0; i < N; i++)
		scanf("%d", &a[i]);

	set<int> ck;
	ck.insert(a[0]);

	for(int i=1; i < N; i++)
	{
		auto lo = ck.lower_bound(a[i]);
		if(lo == ck.end())
			lo--;
		if(*lo > a[i] && used[*lo])
			lo--;
		else if(*lo > a[i])
			used[*lo] = 1;

		parent[i] = *lo;
		ck.insert(a[i]);
	}

	for(int i=1; i < N; i++)
		printf("%d ", parent[i]);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int N;
int a[100001];

int parent[100001];

map<int, int> ckr, ckl, par;

int main()
{
	scanf("%d", &N);
	for(int i=0; i < N; i++)
		scanf("%d", &a[i]);

	ckr[a[0]] = 1;
	ckl[-a[0]] = 1;

	for(int i=1; i < N; i++)
	{
		auto findr = ckr.lower_bound(a[i]);
		auto findl = ckl.lower_bound(-a[i]);
		if(findr == ckr.end())
		{
			int l = -findl->first;
			parent[i] = l;
			par[a[i]] = l;
			ckl.erase(findl);
		}
		else if(findl == ckl.end())
		{
			int r = findr->first;
			parent[i] = r;
			par[a[i]] = r;
			ckr.erase(findr);
		}
		else
		{
			int l = -findl->first;
			int r = findr->first;
			int pr = par[r];
			int pl = par[l];
			if(pr < r)
			{
				if(a[i] < pr)
				{
					parent[i] = l;
					par[a[i]] = l;
					ckl.erase(findl);
				}
				else
				{
					parent[i] = r;
					par[a[i]] = r;
					ckr.erase(findr);
				}
			}
			else if(l < pl)
			{
				if(a[i] < pl)
				{
					parent[i] = l;
					par[a[i]] = l;
					ckl.erase(findl);
				}
				else
				{
					parent[i] = r;
					par[a[i]] = r;
					ckr.erase(findr);
				}
			}
			else
			{
				parent[i] = l;
				par[a[i]] = l;
				ckl.erase(findl);
			}
		}

		ckl[-a[i]] = 1;
		ckr[a[i]] = 1;
	}

	for(int i=1; i < N; i++)
		printf("%d ", parent[i]);
	return 0;
}


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
typedef long long ll;
#define sf(x) scanf("%d", &x);
#define sf2(x, y) scanf("%d %d", &x, &y);
#define sf3(x, y, z) scanf("%d %d %d", &x, &y, &z);
#define pi 3.1415926536
int n,c,a[100002],root;
vector<int> ans;
unordered_map< int,set<int> > m;
void add(int c,int l)
{
    if(c > l)
    {
      for(set<int>::iterator it = m[l].begin();it != m[l].end();it++)
      {
          if(*it > l)
          {
              add(c,*it);
              return;
          }
      }
      ans.push_back(l);
      m[l].insert(c);
      return;
    }
    for(set<int>::iterator it = m[l].begin();it != m[l].end();it++)
    {
        if(*it < l)
        {
            add(c,*it);
            return;
        }
    }
    ans.push_back(l);
    m[l].insert(c);
}
int main()
{
    sf2(n,root);
    for(int i = 1;i < n;i++)
    {
        sf(c);
        add(c,root);
    }
    for(int i = 0;i < ans.size();i++)printf("%d ",ans[i]);
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
typedef long long ll;
#define sf(x) scanf("%d", &x);
#define sf2(x, y) scanf("%d %d", &x, &y);
#define sf3(x, y, z) scanf("%d %d %d", &x, &y, &z);
#define pi 3.1415926536
int n,c,a[100002],root;
vector<int> ans;
map< int,set<int> > m;
void add(int c,int l)
{
    if(c > l)
    {
      for(set<int>::iterator it = m[l].begin();it != m[l].end();it++)
      {
          if(*it > l)
          {
              add(c,*it);
              return;
          }
      }
      ans.push_back(l);
      m[l].insert(c);
      return;
    }
    for(set<int>::iterator it = m[l].begin();it != m[l].end();it++)
    {
        if(*it < l)
        {
            add(c,*it);
            return;
        }
    }
    ans.push_back(l);
    m[l].insert(c);
}
int main()
{
    sf2(n,root);
    for(int i = 1;i < n;i++)
    {
        sf(c);
        add(c,root);
    }
    for(int i = 0;i < ans.size();i++)printf("%d ",ans[i]);
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
typedef long long ll;
#define sf(x) scanf("%d", &x);
#define sf2(x, y) scanf("%d %d", &x, &y);
#define sf3(x, y, z) scanf("%d %d %d", &x, &y, &z);
#define pi 3.1415926536
int main()
{
    int n,c;
    set<int> s;
    unordered_map<int,int> L;
    sf2(n,c);
    s.insert(c);
    for(int i = 0;i < n-1;i++)
    {
        sf(c);
        set<int>::iterator it = s.upper_bound(c);
        if(it == s.end())
        {
            it--;
            printf("%d ",*it);
        }
        else
        {
            if(L[*it] == 0)
            {
                L[*it] = c;
                printf("%d ",*it);
            }
            else
            {
                it--;
                printf("%d ",*it);
            }
        }
        s.insert(c);
    }
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
typedef long long ll;
#define sf(x) scanf("%d", &x);
#define sf2(x, y) scanf("%d %d", &x, &y);
#define sf3(x, y, z) scanf("%d %d %d", &x, &y, &z);
#define pi 3.1415926536
int main()
{
    int n,x;
    map<int,int> m;
    vector<int> ans;
    map<int,int>::iterator it;
    sf2(n,x);
    m[x] = 1;
    for(int i = 1;i < n;i++)
    {
        sf(x);
        m[x] = 1;
        it = m.find(x);
        if(it == m.begin())
        {
            it++;
            ans.push_back(it->first);
        }
        else
        {
            it--;
            ans.push_back(it->first);
        }
    }
    for(int i = 0;i < ans.size();i++)cout << ans[i] <<" ";
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
typedef long long ll;
#define sf(x) scanf("%d", &x);
#define sf2(x, y) scanf("%d %d", &x, &y);
#define sf3(x, y, z) scanf("%d %d %d", &x, &y, &z);
#define pi 3.1415926536
int main()
{
    int n,c;
    set<int> s;
    unordered_map<int,int> R,L;
    sf2(n,c);
    s.insert(c);
    for(int i = 0;i < n-1;i++)
    {
        sf(c);
        set<int>::iterator it = s.upper_bound(c);
        if(it == s.end())
        {
            it--;
            R[*it] = c;
            printf("%d ",*it);
        }
        else
        {
            if(L[*it] == 0)
            {
                L[*it] = c;
                printf("%d ",*it);
            }
            else
            {
                it--;
                R[*it] = c;
                printf("%d ",*it);
            }
        }
        s.insert(c);
    }
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
typedef long long ll;
#define sf(x) scanf("%d", &x);
#define sf2(x, y) scanf("%d %d", &x, &y);
#define sf3(x, y, z) scanf("%d %d %d", &x, &y, &z);
#define pi 3.1415926536
int n,c,t;
vector<int> ans;
class node{
public:
int val;
node *right;
node *left;
node(int c);
};
node::node(int c)
{
    left = right = NULL;
    val = c;
}
node *root,*te;
void add()
{
node *tmp = root;
while(true)
{
    if(c > tmp->val)
    {
        if(tmp->right == NULL)
        {
            t = tmp->val;
            tmp->right = new node(c);
            return;
        }
        tmp = tmp->right;
    }
    else
    {
        if(tmp->left == NULL)
        {
            t = tmp->val;
            tmp->left = new node(c);
            return;
        }
        tmp = tmp->left;
    }
}
}
int main()
{
    sf2(n,c);
    root = new node(c);
    for(int i = 1;i < n;i++)
    {
        sf(c);
        add();
        ans.push_back(t);
    }
    for(int i = 0;i < ans.size();i++)printf("%d ",ans[i]);
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
typedef long long ll;
#define sf(x) scanf("%d", &x);
#define sf2(x, y) scanf("%d %d", &x, &y);
#define sf3(x, y, z) scanf("%d %d %d", &x, &y, &z);
#define pi 3.1415926536
vector<bool> L(1000000000,0);
int main()
{
    int n,c;
    set<int> s;
    sf2(n,c);
    s.insert(c);
    for(int i = 0;i < n-1;i++)
    {
        sf(c);
        set<int>::iterator it = s.upper_bound(c);
        if(it == s.end())
        {
            it--;
            printf("%d ",*it);
        }
        else
        {
            if(L[*it] == 0)
            {
                L[*it] = 1;
                printf("%d ",*it);
            }
            else
            {
                it--;
                printf("%d ",*it);
            }
        }
        s.insert(c);
    }
    return 0;
}


/* */
#include<bits/stdc++.h>

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define UB(X,elem) upper_bound(X.begin(), X.end(), elem)
#define LB(X,v) lower_bound(X.begin(), X.end(), v)

#define RI(a) scanf("%d", &a)
#define RIL(a) scanf("%lld", &a)
#define PI(a) printf("%d\n", a)
#define PIL(a) printf("%lld\n", a)

#define SZ(a) (int)(a.size())
#define CLR(a) a.clear()

#define SET(a,b) memset(a, b, sizeof(a))
#define LET(a,b) __typeof(a.begin()) b;

#define TR(a,it) for(__typeof(a.begin()) it = a.begin(); it != a.end(); it++)

#define REP(i,l,h) for(int i=(l); i <= (h); i++)
#define DEP(i,h,l) for(int i=(h);i >= (l); i--)

#define ALL(a) a.begin(), a.end()

#define TC()  int tc; cin >> tc

#define PRSNT(a, e) (a.find(e) != a.end())

#define MINH priority_queue<int, vector<int>, greater<int> >

#define N 100001
#define MOD 1000000007

using namespace std;

#define DBG(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

typedef long long LL;
typedef pair<int, int> PII;
typedef vector< vector<int> > VVI;
typedef vector< pair<int, int> > VPII;
typedef vector<int> VI;

int main()
{
    set <int> s;
    map<int, int> m;
    int n,a;
    RI(n);
    RI(a);
    s.insert(a);
    m[a]=1;
    LET(s,tmp);
    LET(s,tmpp);
    REP(i,1,n-1) {
        RI(a);
        m[a]=i+1;
        tmp=s.lower_bound(a);
        if(i>1) tmpp=tmp--;
        else tmpp=s.begin();
        if(tmp==s.end())
            printf("%d ",*tmpp);
        else if(tmpp==s.end())
            printf("%d ",*tmp);
        else if(m[*tmp]>m[*tmpp])
            printf("%d ",*tmp);
        else
            printf("%d ",*tmpp);
        s.insert(a);
    }
    return 0;
}


/* */
#include<bits/stdc++.h>

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define UB(X,elem) upper_bound(X.begin(), X.end(), elem)
#define LB(X,v) lower_bound(X.begin(), X.end(), v)

#define RI(a) scanf("%d", &a)
#define RIL(a) scanf("%lld", &a)
#define PI(a) printf("%d\n", a)
#define PIL(a) printf("%lld\n", a)

#define SZ(a) (int)(a.size())
#define CLR(a) a.clear()

#define SET(a,b) memset(a, b, sizeof(a))
#define LET(a,b) __typeof(a.begin()) b;

#define TR(a,it) for(__typeof(a.begin()) it = a.begin(); it != a.end(); it++)

#define REP(i,l,h) for(int i=(l); i <= (h); i++)
#define DEP(i,h,l) for(int i=(h);i >= (l); i--)

#define ALL(a) a.begin(), a.end()

#define TC()  int tc; cin >> tc

#define PRSNT(a, e) (a.find(e) != a.end())

#define MINH priority_queue<int, vector<int>, greater<int> >

#define N 100001
#define MOD 1000000007

using namespace std;

#define DBG(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

typedef long long LL;
typedef pair<int, int> PII;
typedef vector< vector<int> > VVI;
typedef vector< pair<int, int> > VPII;
typedef vector<int> VI;

int main()
{
    set <int> s;
    map<int, int> m;
    int n,a,tmp,tmpp;
    cin>>n>>a;
    s.insert(a);
    m[a]=1;
    REP(i,1,n-1) {
        cin>>a;
        m[a]=i+1;
        //TR(s, it) DBG(*it);
        //TR(t, it) DBG(*it);
        LET(s,tmp);
        LET(s,tmpp);
        tmp=upper_bound(s.begin(), s.end(), a);
        if(SZ(s)>1) tmpp=--upper_bound(s.begin(), s.end(), a);
        else tmpp=s.begin();
        if(tmp==s.end()) cout<<*tmpp<<" ";
        else if(tmpp==s.end()) cout<<*tmp<<" ";
        else if(m[*tmp]>m[*tmpp]) cout<<*tmp<<" ";
        else cout<<*tmpp<<" ";
        s.insert(a);
    }
    cout<<endl;
    return 0;
}


/* */
#include<bits/stdc++.h>

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define UB(X,elem) upper_bound(X.begin(), X.end(), elem)
#define LB(X,v) lower_bound(X.begin(), X.end(), v)

#define RI(a) scanf("%d", &a)
#define RIL(a) scanf("%lld", &a)
#define PI(a) printf("%d\n", a)
#define PIL(a) printf("%lld\n", a)

#define SZ(a) (int)(a.size())
#define CLR(a) a.clear()

#define SET(a,b) memset(a, b, sizeof(a))
#define LET(a,b) __typeof(a.begin()) b;

#define TR(a,it) for(__typeof(a.begin()) it = a.begin(); it != a.end(); it++)

#define REP(i,l,h) for(int i=(l); i <= (h); i++)
#define DEP(i,h,l) for(int i=(h);i >= (l); i--)

#define ALL(a) a.begin(), a.end()

#define TC()  int tc; cin >> tc

#define PRSNT(a, e) (a.find(e) != a.end())

#define MINH priority_queue<int, vector<int>, greater<int> >

#define N 100001
#define MOD 1000000007

using namespace std;

#define DBG(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

typedef long long LL;
typedef pair<int, int> PII;
typedef vector< vector<int> > VVI;
typedef vector< pair<int, int> > VPII;
typedef vector<int> VI;

int main()
{
    set <int> s;
    map<int, int> m;
    int n,a,tmp,tmpp;
    RI(n);
    RI(a);
    s.insert(a);
    m[a]=1;
    REP(i,1,n-1) {
        RI(a);
        m[a]=i+1;
        //TR(s, it) DBG(*it);
        //TR(t, it) DBG(*it);
        LET(s,tmp);
        LET(s,tmpp);
        tmp=upper_bound(s.begin(), s.end(), a);
        if(SZ(s)>1) tmpp=--upper_bound(s.begin(), s.end(), a);
        else tmpp=s.begin();
        if(tmp==s.end())
            printf("%d ",*tmpp);
        else if(tmpp==s.end())
            printf("%d ",*tmp);
        else if(m[*tmp]>m[*tmpp])
            printf("%d ",*tmp);
        else
            printf("%d ",*tmpp);
        s.insert(a);
    }
    return 0;
}


/* */
#include<bits/stdc++.h>

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define UB(X,elem) upper_bound(X.begin(), X.end(), elem)
#define LB(X,v) lower_bound(X.begin(), X.end(), v)

#define RI(a) scanf("%d", &a)
#define RIL(a) scanf("%lld", &a)
#define PI(a) printf("%d\n", a)
#define PIL(a) printf("%lld\n", a)

#define SZ(a) (int)(a.size())
#define CLR(a) a.clear()

#define SET(a,b) memset(a, b, sizeof(a))
#define LET(a,b) __typeof(a.begin()) b;

#define TR(a,it) for(__typeof(a.begin()) it = a.begin(); it != a.end(); it++)

#define REP(i,l,h) for(int i=(l); i <= (h); i++)
#define DEP(i,h,l) for(int i=(h);i >= (l); i--)

#define ALL(a) a.begin(), a.end()

#define TC()  int tc; cin >> tc

#define PRSNT(a, e) (a.find(e) != a.end())

#define MINH priority_queue<int, vector<int>, greater<int> >

#define N 100001
#define MOD 1000000007

using namespace std;

#define DBG(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

typedef long long LL;
typedef pair<int, int> PII;
typedef vector< vector<int> > VVI;
typedef vector< pair<int, int> > VPII;
typedef vector<int> VI;

int main()
{
    set <int> s,t;
    int n,a,tmp,tmpp;
    cin>>n>>a;
    s.insert(a);
    t.insert(a);
    REP(i,1,n-1) {
        cin>>a;
        //TR(s, it) DBG(*it);
        //TR(t, it) DBG(*it);
        tmp=*(upper_bound(s.begin(), s.end(), a));
        if(SZ(t)>1) tmpp=*(--upper_bound(t.begin(), t.end(), a));
        else tmpp=*t.begin();
        //DBG(tmp, tmpp);
        if(tmp>a) {
            cout<<tmp<<" ";
            s.erase(tmp);
        }
        else {
            cout<<tmpp<<" ";
            t.erase(tmpp);
        }
        s.insert(a);
        t.insert(a);
    }
    cout<<endl;
    return 0;
}


/* */
#include<bits/stdc++.h>

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define UB(X,elem) upper_bound(X.begin(), X.end(), elem)
#define LB(X,v) lower_bound(X.begin(), X.end(), v)

#define RI(a) scanf("%d", &a)
#define RIL(a) scanf("%lld", &a)
#define PI(a) printf("%d\n", a)
#define PIL(a) printf("%lld\n", a)

#define SZ(a) (int)(a.size())
#define CLR(a) a.clear()

#define SET(a,b) memset(a, b, sizeof(a))
#define LET(a,b) __typeof(a.begin()) b;

#define TR(a,it) for(__typeof(a.begin()) it = a.begin(); it != a.end(); it++)

#define REP(i,l,h) for(int i=(l); i <= (h); i++)
#define DEP(i,h,l) for(int i=(h);i >= (l); i--)

#define ALL(a) a.begin(), a.end()

#define TC()  int tc; cin >> tc

#define PRSNT(a, e) (a.find(e) != a.end())

#define MINH priority_queue<int, vector<int>, greater<int> >

#define N 100001
#define MOD 1000000007

using namespace std;

#define DBG(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

typedef long long LL;
typedef pair<int, int> PII;
typedef vector< vector<int> > VVI;
typedef vector< pair<int, int> > VPII;
typedef vector<int> VI;

int main()
{
    set <int> s;
    map<int, int> m;
    int n,a;
    RI(n);
    RI(a);
    s.insert(a);
    m[a]=1;
    LET(s,tmp);
    LET(s,tmpp);
    REP(i,1,n-1) {
        RI(a);
        m[a]=i+1;
        tmp=lower_bound(s.begin(), s.end(), a);
        if(i>1) tmpp=--lower_bound(s.begin(), s.end(), a);
        else tmpp=s.begin();
        if(tmp==s.end())
            printf("%d ",*tmpp);
        else if(tmpp==s.end())
            printf("%d ",*tmp);
        else if(m[*tmp]>m[*tmpp])
            printf("%d ",*tmp);
        else
            printf("%d ",*tmpp);
        s.insert(a);
    }
    return 0;
}


/* */
#include<bits/stdc++.h>

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define UB(X,elem) upper_bound(X.begin(), X.end(), elem)
#define LB(X,v) lower_bound(X.begin(), X.end(), v)

#define RI(a) scanf("%d", &a)
#define RIL(a) scanf("%lld", &a)
#define PI(a) printf("%d\n", a)
#define PIL(a) printf("%lld\n", a)

#define SZ(a) (int)(a.size())
#define CLR(a) a.clear()

#define SET(a,b) memset(a, b, sizeof(a))
#define LET(a,b) __typeof(a.begin()) b;

#define TR(a,it) for(__typeof(a.begin()) it = a.begin(); it != a.end(); it++)

#define REP(i,l,h) for(int i=(l); i <= (h); i++)
#define DEP(i,h,l) for(int i=(h);i >= (l); i--)

#define ALL(a) a.begin(), a.end()

#define TC()  int tc; cin >> tc

#define PRSNT(a, e) (a.find(e) != a.end())

#define MINH priority_queue<int, vector<int>, greater<int> >

#define N 100001
#define MOD 1000000007

using namespace std;

#define DBG(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

typedef long long LL;
typedef pair<int, int> PII;
typedef vector< vector<int> > VVI;
typedef vector< pair<int, int> > VPII;
typedef vector<int> VI;

int main()
{
    set <int> s;
    map<int, int> m;
    int n,a;
    RI(n);
    RI(a);
    s.insert(a);
    m[a]=1;
    LET(s,tmp);
    LET(s,tmpp);
    REP(i,1,n-1) {
        RI(a);
        m[a]=i+1;
        tmp=upper_bound(s.begin(), s.end(), a);
        if(SZ(s)>1) tmpp=--upper_bound(s.begin(), s.end(), a);
        else tmpp=s.begin();
        if(tmp==s.end())
            printf("%d ",*tmpp);
        else if(tmpp==s.end())
            printf("%d ",*tmp);
        else if(m[*tmp]>m[*tmpp])
            printf("%d ",*tmp);
        else
            printf("%d ",*tmpp);
        s.insert(a);
    }
    return 0;
}


/* */
#include<bits/stdc++.h>

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define UB(X,elem) upper_bound(X.begin(), X.end(), elem)
#define LB(X,v) lower_bound(X.begin(), X.end(), v)

#define RI(a) scanf("%d", &a)
#define RIL(a) scanf("%lld", &a)
#define PI(a) printf("%d\n", a)
#define PIL(a) printf("%lld\n", a)

#define SZ(a) (int)(a.size())
#define CLR(a) a.clear()

#define SET(a,b) memset(a, b, sizeof(a))
#define LET(a,b) __typeof(a.begin()) b;

#define TR(a,it) for(__typeof(a.begin()) it = a.begin(); it != a.end(); it++)

#define REP(i,l,h) for(int i=(l); i <= (h); i++)
#define DEP(i,h,l) for(int i=(h);i >= (l); i--)

#define ALL(a) a.begin(), a.end()

#define TC()  int tc; cin >> tc

#define PRSNT(a, e) (a.find(e) != a.end())

#define MINH priority_queue<int, vector<int>, greater<int> >

#define N 100001
#define MOD 1000000007

using namespace std;

#define DBG(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

typedef long long LL;
typedef pair<int, int> PII;
typedef vector< vector<int> > VVI;
typedef vector< pair<int, int> > VPII;
typedef vector<int> VI;

int main()
{
    set <int> s;
    map<int, int> m;
    int n,a,tmp,tmpp;
    cin>>n>>a;
    s.insert(a);
    m[a]=1;
    REP(i,1,n-1) {
        RI(a);
        m[a]=i+1;
        //TR(s, it) DBG(*it);
        //TR(t, it) DBG(*it);
        LET(s,tmp);
        LET(s,tmpp);
        tmp=upper_bound(s.begin(), s.end(), a);
        if(SZ(s)>1) tmpp=--upper_bound(s.begin(), s.end(), a);
        else tmpp=s.begin();
        if(tmp==s.end())
            printf("%d ",*tmpp);
        else if(tmpp==s.end())
            printf("%d ",*tmp);
        else if(m[*tmp]>m[*tmpp])
            printf("%d ",*tmp);
        else
            printf("%d ",*tmpp);
        s.insert(a);
    }
    return 0;
}


/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑       永无BUG
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#define mem(a) memset(a,0,sizeof(a))
#define INF 0x7fffffff   //INT_MAX
#define inf 0x3f3f3f3f   //
const double PI = acos(-1.0);
const double e = exp(1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
bool cmpbig(int a,int b){return a>b;}
bool cmpsmall(int a,int b){return a<b;}
using namespace std;
const int N = 100005;

map<int,int>fa;
struct node{
	int num;
};
node st[N*4];

int f[N];

void jud(int n,int pos){
	if(st[pos].num==0 || pos>N*4){
		st[pos].num=n;
		fa[n]=st[pos/2].num;
	    return;
	}
	if(n > st[pos].num)
		jud(n,pos*2+1);
	else
		jud(n,pos*2);
}
int main(){
	int n;
	//freopen("1.txt","r",stdin);
	for(int i=1;i<=N;i++)
		st[i].num=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&f[i]);
	st[1].num=f[1];
	for(int i=2;i<=n;i++){
		jud(f[i],1);
	}
	for(int i=2;i<=n;i++){
		printf("%d ",fa[f[i]]);
	}
	cout<<endl;
	return 0;
}


/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑       永无BUG
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#define mem(a) memset(a,0,sizeof(a))
#define INF 0x7fffffff   //INT_MAX
#define inf 0x3f3f3f3f   //
const double PI = acos(-1.0);
const double e = exp(1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
bool cmpbig(int a,int b){return a>b;}
bool cmpsmall(int a,int b){return a<b;}
using namespace std;
const int N = 100005;

map<int,int>fa;
struct node{
	int l,r;
	int num;
};
node st[N*4];

int f[N];

void jud(int n,int pos){
	if(st[pos].num==0){
		st[pos].num=n;
		fa[n]=st[pos/2].num;
	    return;
	}
	if(n > st[pos].num)
		jud(n,pos*2+1);
	else
		jud(n,pos*2);
}
int main(){
	int n;
	//freopen("1.txt","r",stdin);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&f[i]);
	st[1].num=f[1];
	for(int i=1;i<=n;i++){
		jud(f[i],1);
	}
	for(int i=2;i<=n;i++){
		printf("%d ",fa[f[i]]);
	}
	cout<<endl;
	return 0;
}


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

typedef __int64 ll;
const int INF=0x3f3f3f3f;
const int maxn=111111;
int n,a[maxn];
int depth[maxn];

int main(){
    while(~scanf("%d",&n)){
    	for(int i=1;i<=n;i++) {
    		scanf("%d",a+i);
    	}
    	memset(depth,0,sizeof(depth));
    	set<pair<int,int> > st;
    	st.insert(make_pair(a[1],1));
    	for(int i=2;i<=n;i++){
    		auto it=st.lower_bound(make_pair(a[i],-1));
    		int id=-1;
    		if(it!=st.end()){
    			id=it->second;
    		}
    		if(it!=st.begin()){
    			it--;
    			if(id==-1||depth[id]<depth[it->second]){
    				id=it->second;
    			}
    		}
    		depth[i]=depth[id]+1;
    		printf("%d ",a[id]);
    		st.insert(make_pair(a[i],i));
    	}
    	putchar(10);
    }
    return 0;
}


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

# define INF ((int)1e9 + 42)
# define MOD ((int)1e9 + 9)
# define LIM (2000000)

# define EXIT(...) printf(__VA_ARGS__), exit(0)

int main()
{
	int n; cin >> n;

	vector<int> vec(n);
	for(int i=0; i<n; ++i) {
		scanf("%d", &vec[i]);
	}

	map<int, int> mp = { {0, -1}, {INF, -2} };

	for(int i=0; i<n; ++i) {
		mp[vec[i]] = i;
	}

	set<int> bst = {0, vec[0], INF};

	for(int i=1; i<n; ++i) {
		auto ans = bst.insert(vec[i]);

		int l = *prev(ans->first);
		int r = *next(ans->first);

		printf("%d ", mp[l] > mp[r]? l:r);
	}

	return 0;
}


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

# define INF ((int)1e9 + 42)
# define MOD ((int)1e9 + 9)
# define LIM (2000000)

# define EXIT(...) printf(__VA_ARGS__), exit(0)

int main()
{
	int n; cin >> n;

	vector<int> vec(n);
	for(int i=0; i<n; ++i) {
		scanf("%d", &vec[i]);
	}

	map<int, int> mp = { {0, -2}, {INF, -1} };
	for(int i=0; i<n; ++i) {
		mp[vec[i]] = i;
	}

	set<int> bst = {vec[0], 0, INF};

	for(int i=1; i<n; ++i) {
		bst.insert(vec[i]);
		auto ptr = bst.find(vec[i]);
		int l = *(--ptr);
		int r = *(++++ptr);

		printf("%d ", mp[l] > mp[r]? l:r);
	}

	return 0;
}


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

# define INF ((int)1e9 + 42)
# define MOD ((int)1e9 + 9)
# define LIM (2000000)

# define EXIT(...) printf(__VA_ARGS__), exit(0)

int main()
{
	int n; cin >> n;

	vector<int> vec(n);
	for(int i=0; i<n; ++i) {
		scanf("%d", &vec[i]);
	}

	map<int, int> mp = { {0, -2}, {INF, -1} };
	for(int i=0; i<n; ++i) {
		mp[vec[i]] = i;
	}

	set<int> bst = {vec[0], 0, INF};

	for(int i=1; i<n; ++i) {
		bst.insert(vec[i]);
		auto ptr = bst.find(vec[i]);
		int l = *prev(ptr, +1);
		int r = *prev(ptr, -1);

		printf("%d ", mp[l] > mp[r]? l:r);
	}

	return 0;
}


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

# define INF ((int)1e9 + 42)
# define MOD ((int)1e9 + 9)
# define LIM (2000000)

# define EXIT(...) printf(__VA_ARGS__), exit(0)

int main()
{
	int n; cin >> n;

	vector<int> vec(n);
	for(int i=0; i<n; ++i) {
		scanf("%d", &vec[i]);
	}

	map<int, int> mp = { {0, -2}, {INF, -1} };
	for(int i=0; i<n; ++i) {
		mp[vec[i]] = i;
	}

	set<int> bst = {vec[0], 0, INF};

	for(int i=1; i<n; ++i) {
		bst.insert(vec[i]);
		auto ptr = bst.find(vec[i]);

		// So slow???
		int l = *prev(ptr);
		int r = *next(ptr);

		// More comments => More slowness???
		printf("%d ", mp[l] > mp[r]? l:r);
	}

	return 0;
}


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

# define INF ((int)1e9 + 42)
# define MOD ((int)1e9 + 9)
# define LIM (2000000)

# define EXIT(...) printf(__VA_ARGS__), exit(0)

typedef struct _N  {
	int val;
	_N *left, *right;
} N;

N* new_node(int val)
{
	N *ptr = new N;

	ptr-> val = val;
	ptr-> left = NULL;
	ptr-> right = NULL;

	return ptr;
}

void insert(N *root, N *node)
{
	if (node->val > root->val) {
		if (root->right == NULL) {
			root->right = node;
			printf("%d ", root->val);
		}
		else insert(root->right, node);
	}
	else {
		if (root->left == NULL) {
			root->left = node;
			printf("%d ", root->val);
		}
		else insert(root->left, node);
	}
}

int main()
{
	int n; cin >> n;

	vector<int> vec(n);
	for(int i=0; i<n; ++i) {
		scanf("%d", &vec[i]);
	}

	N *root = new_node(vec[0]);
	for(int i=1; i<n; ++i) {
		N *nd = new_node(vec[i]);
		insert(root, nd);
	}
	return 0;
}


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

# define INF ((int)1e9 + 42)
# define MOD ((int)1e9 + 9)
# define LIM (2000000)

# define EXIT(...) printf(__VA_ARGS__), exit(0)

int main()
{
	int n; cin >> n;

	vector<int> vec(n);
	for(int i=0; i<n; ++i) {
		scanf("%d", &vec[i]);
	}

	map<int, int> mp = { {0, -2}, {INF, -1} };
	for(int i=0; i<n; ++i) {
		mp[vec[i]] = i;
	}

	set<int> bst = {vec[0], 0, INF};

	for(int i=1; i<n; ++i) {
		bst.insert(vec[i]);
		auto ptr = bst.find(vec[i]);
		int l = *prev(ptr);
		int r = *next(ptr);

		// One more time
		printf("%d ", mp[l] > mp[r]? l:r);
	}

	return 0;
}


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

# define INF ((int)1e9 + 42)
# define MOD ((int)1e9 + 9)
# define LIM (2000000)

# define EXIT(...) printf(__VA_ARGS__), exit(0)

int main()
{
	int n; cin >> n;

	vector<int> vec(n);
	for(int i=0; i<n; ++i) {
		scanf("%d", &vec[i]);
	}

	map<int, int> mp = { {0, -2}, {INF, -1} };
	for(int i=0; i<n; ++i) {
		mp[vec[i]] = i;
	}

	set<int> bst = {vec[0], 0, INF};

	for(int i=1; i<n; ++i) {
		bst.insert(vec[i]);
		auto ptr = bst.find(vec[i]);
		int l = *prev(ptr);
		int r = *next(ptr);

		printf("%d ", mp[l] > mp[r]? l:r);
	}

	return 0;
}


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

# define INF ((int)1e9 + 42)
# define MOD ((int)1e9 + 9)
# define LIM (2000000)

# define EXIT(...) printf(__VA_ARGS__), exit(0)

int main()
{
	int n; cin >> n;

	vector<int> vec(n);
	for(int i=0; i<n; ++i) {
		scanf("%d", &vec[i]);
	}

	map<int, int> mp = { {0, -1}, {INF, -2} };

	for(int i=0; i<n; ++i) {
		mp[vec[i]] = i;
	}

	set<int> bst = {0, vec[0], INF};

	for(int i=1; i<n; ++i) {
		auto ptr = bst.insert(vec[i]).first;

		int l = *prev(ptr);
		int r = *next(ptr);

		printf("%d ", mp[l] > mp[r]? l:r);
	}

	return 0;
}


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

# define INF ((int)1e9 + 42)
# define MOD ((int)1e9 + 9)
# define LIM (2000000)

# define EXIT(...) printf(__VA_ARGS__), exit(0)

int main()
{
	int n; cin >> n;

	vector<int> vec(n);
	for(int i=0; i<n; ++i) {
		scanf("%d", &vec[i]);
	}

	map<int, int> mp = { {0, -2}, {INF, -1} };
	for(int i=0; i<n; ++i) {
		mp[vec[i]] = i;
	}

	set<int> bst = {vec[0], 0, INF};

	for(int i=1; i<n; ++i) {
		bst.insert(vec[i]);
		auto ptr = bst.find(vec[i]);

		// So slow???
		int l = *prev(ptr);
		int r = *next(ptr);

		printf("%d ", mp[l] > mp[r]? l:r);
	}

	return 0;
}


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

# define INF ((int)1e9 + 42)
# define MOD ((int)1e9 + 9)
# define LIM (2000000)

# define EXIT(...) printf(__VA_ARGS__), exit(0)

int main()
{
	int n; cin >> n;

	vector<int> vec(n);
	for(int i=0; i<n; ++i) {
		scanf("%d", &vec[i]);
	}

	map<int, int> mp;
	for(int i=0; i<n; ++i) {
		mp[vec[i]] = i;
	}
	mp[0] = -2;
	mp[INF] = -1;

	set<int> bst = {vec[0], 0, INF};

	for(int i=1; i<n; ++i) {
		bst.insert(vec[i]);
		auto ptr = bst.find(vec[i]);
		int l = *(--ptr);
		int r = *(++++ptr);

		printf("%d ", mp[l] > mp[r]? l:r);
	}

	return 0;
}


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

# define INF ((int)1e9 + 42)
# define MOD ((int)1e9 + 9)
# define LIM (2000000)

# define EXIT(...) printf(__VA_ARGS__), exit(0)

typedef struct _N {
	int left, right;
} N;

int main()
{
	int n; cin >> n;

	vector<int> vec(n);
	for(int i=0; i<n; ++i) {
		scanf("%d", &vec[i]);
	}

	map<int, int> mp;
	for(int i=0; i<n; ++i) {
		mp[vec[i]] = i;
	}
	mp[0] = -2;
	mp[(int)1e9 + 100] = -1;

	set<int> bst;
	bst.insert(vec[0]);
	bst.insert(0);
	bst.insert((int)1e9 + 100);

	for(int i=1; i<n; ++i) {
		bst.insert(vec[i]);
		auto ptr = bst.find(vec[i]);
		int l = *(--ptr);
		int r = *(++++ptr);

		printf("%d ", mp[l] > mp[r]? l:r);
	}

	return 0;
}


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

# define INF ((int)1e9 + 42)
# define MOD ((int)1e9 + 9)
# define LIM (2000000)

# define EXIT(...) printf(__VA_ARGS__), exit(0)

int main()
{
	int n; cin >> n;

	vector<int> vec(n);
	for(int i=0; i<n; ++i) {
		scanf("%d", &vec[i]);
	}

	map<int, int> mp = { {0, -1}, {INF, -2} };

	for(int i=0; i<n; ++i) {
		mp[vec[i]] = i;
	}

	set<int> bst = {0, vec[0], INF};

	for(int i=1; i<n; ++i) {
		auto ans = bst.insert(vec[i]);

		int l = *prev(ans.first);
		int r = *next(ans.first);

		printf("%d ", mp[l] > mp[r]? l:r);
	}

	return 0;
}


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

# define INF ((int)1e9 + 42)
# define MOD ((int)1e9 + 9)
# define LIM (2000000)

# define EXIT(...) printf(__VA_ARGS__), exit(0)

int main()
{
	int n; cin >> n;

	vector<int> vec(n);
	for(int i=0; i<n; ++i) {
		scanf("%d", &vec[i]);
	}

	map<int, int> mp = { {0, -2}, {INF, -1} };
	for(int i=0; i<n; ++i) {
		mp[vec[i]] = i;
	}

	set<int> bst = {vec[0], 0, INF};

	for(int i=1; i<n; ++i) {
		bst.insert(vec[i]);
		auto ptr = bst.find(vec[i]);
		int l = *next(ptr, -1);
		int r = *next(ptr, +1);

		printf("%d ", mp[l] > mp[r]? l:r);
	}

	return 0;
}


/*************************************************

       Filename: cf675d.cpp
         Author: zQ
          Email: zq216991@foxmail.com
         Create: 2017-07-09 15:40:14
    Description: 二分

*************************************************/

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

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n;
    scanf("%d", &n);
    set<int> st;
    int x;
    cin >> x;
    st.insert(x);
    map<int, bool> vis;
    for(int i = 1; i < n; ++ i) {
	if(i > 1) cout << " ";
	cin >> x;
	auto it = st.lower_bound(x);
	int ans;
	if(it == st.end()) {
	    ans = *--it;
	}
	else if(it == st.begin() || !vis[*it]) {
	    ans = *it;
	    vis[*it] = true;
	}
	else {
	    ans = *--it;
	}
	cout << ans ;
	st.insert(x);
    }
    //cout << "\n";
    return 0;
}


/*************************************************

       Filename: cf675d.cpp
         Author: zQ
          Email: zq216991@foxmail.com
         Create: 2017-07-09 15:40:14
    Description: 二分

*************************************************/

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

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n;
    cin >> n;
    set<int> st;
    int x;
    cin >> x;
    st.insert(x);
    map<int, bool> vis;
    for(int i = 1; i < n; ++ i) {
	if(i > 1) cout << " ";
	cin >> x;
	set<int>::iterator it = st.lower_bound(x);
	int ans;
	if(it == st.end()) {
	    ans = *--it;
	}
	else if(it == st.begin() || vis[*it] == false) {
	    ans = *it;
	    vis[ans] = true;
	}
	else {
	    ans = *--it;
	}
	cout << ans;
	st.insert(x);
    }
    cout << endl;
    return 0;
}


/*************************************************

       Filename: cf675d.cpp
         Author: zQ
          Email: zq216991@foxmail.com
         Create: 2017-07-09 15:40:14
    Description: 二分

*************************************************/

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

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n;
    scanf("%d", &n);
    set<int> st;
    int x;
    cin >> x;
    st.insert(x);
    map<int, bool> vis;
    for(int i = 1; i < n; ++ i) {
	if(i > 1) cout << " ";
	cin >> x;
	auto it = st.lower_bound(x);
	int ans;
	if(it == st.end()) {
	    ans = *--it;
	}
	else if(it == st.begin() || !vis[*it]) {
	    ans = *it;
	    vis[*it] = true;
	}
	else {
	    ans = *--it;
	}
	cout << ans ;
	st.insert(x);
    }
    cout << "\n";
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <stack>
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e5 + 10;
const int MOD = 1e9 + 7;
const int INF = 2 * 1e9 + 10;
set<int> son;
set<int> :: iterator it;
map<int, bool> vis;
int a[MAXN];
int main()
{
    int n;
   scanf("%d", &n);
        son.clear(); vis.clear();
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
        }
        son.insert(a[1]);
        for(int i = 2; i <= n; i++) {
            if(i > 2) printf(" ");
            it = son.lower_bound(a[i]);
            int ans;
            if(it == son.end()) {
                ans = *--it;
            }
            else if(it == son.begin() || vis[*it] == false) {
                ans = *it;
                vis[ans] = true;
            }
            else {
                ans = *--it;
            }
            printf("%d", ans);
            son.insert(a[i]);
        }
        printf("\n");

    return 0;
}


/*************************************************

       Filename: cf675d.cpp
         Author: zQ
          Email: zq216991@foxmail.com
         Create: 2017-07-09 15:40:14
    Description: 二分

*************************************************/

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

bool vis[100005];    // 是否有左孩子

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n;
    scanf("%d", &n);
    set<int> st;
    int x;
    cin >> x;
    st.insert(x);
    memset(vis, false, sizeof(vis));
    for(int i = 1; i < n; ++ i) {
	if(i > 1) cout << " ";
	cin >> x;
	auto it = st.lower_bound(x);
	int ans;
	if(it == st.end()) {
	    ans = *--it;
	}
	else if(it == st.begin() || !vis[*it]) {
	    ans = *it;
	    vis[*it] = true;
	}
	else {
	    ans = *--it;
	}
	cout << ans ;
	st.insert(x);
    }
    cout << "\n";
    return 0;
}


/*************************************************

       Filename: cf675d.cpp
         Author: zQ
          Email: zq216991@foxmail.com
         Create: 2017-07-09 15:40:14
    Description: 二分

*************************************************/

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

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n;
    scanf("%d", &n);
    set<int> st;
    int x;
    cin >> x;
    st.insert(x);
    map<int, bool> vis;
    for(int i = 1; i < n; ++ i) {
	if(i > 1) cout << " ";
	cin >> x;
	set<int>::iterator it = st.lower_bound(x);
	int ans;
	if(it == st.end()) {
	    ans = *--it;
	}
	else if(it == st.begin() || vis[*it] == false) {
	    ans = *it;
	    vis[ans] = true;
	}
	else {
	    ans = *--it;
	}
	cout << ans;
	st.insert(x);
    }
    cout << endl;
    return 0;
}


/*************************************************

       Filename: cf675d.cpp
         Author: zQ
          Email: zq216991@foxmail.com
         Create: 2017-07-09 15:40:14
    Description: 二分

*************************************************/

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

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n;
    scanf("%d", &n);
    set<int> st;
    int x;
    cin >> x;
    st.insert(x);
    map<int, bool> vis;
    for(int i = 1; i < n; ++ i) {
	if(i > 1) cout << " ";
	cin >> x;
	set<int>::iterator it = st.lower_bound(x);
	int ans;
	if(it == st.end()) {
	    ans = *--it;
	}
	else if(it == st.begin() || !vis[*it]) {
	    ans = *it;
	    vis[*it] = true;
	}
	else {
	    ans = *--it;
	}
	cout << ans ;
	st.insert(x);
    }
    cout << "\n";
    return 0;
}


/*************************************************

       Filename: cf675d.cpp
         Author: zQ
          Email: zq216991@foxmail.com
         Create: 2017-07-09 15:40:14
    Description: 二分

*************************************************/

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

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n;
    scanf("%d", &n);
    set<int> st;
    int x;
    cin >> x;
    st.insert(x);
    map<int, bool> vis;
    for(int i = 1; i < n; ++ i) {
	if(i > 1) cout << " ";
	cin >> x;
	set<int>::iterator it = st.lower_bound(x);
	int ans;
	if(it == st.end()) {
	    ans = *--it;
	}
	else if(it == st.begin() || vis[*it] == false) {
	    ans = *it;
	    vis[ans] = true;
	}
	else {
	    ans = *--it;
	}
	cout << ans ;
	st.insert(x);
    }
    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
main()
{
    int n, m, i, j, x;
    cin >> n;
    cin >> x;
    map <int, int> mp1;
    map <int, int> mp2;
    mp1[x];
    mp2[x];
    for(i = 1; i < n; i ++)
    {
        cin >> x;
        map <int, int> :: iterator it = mp1.lower_bound(x);
        if(it != mp1.begin())
        {
            it --;
            cout << (*it).first << " ";
            mp1[x];
            mp2[x];
        }
        else
        {
            map <int, int> :: iterator it = mp2.lower_bound(x);
            cout << (*it).first << " ";
            mp1[x];
            mp2[x];
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
main()
{
    int n, m, i, j, x;
    cin >> n;
    cin >> x;
    map <int, int> mp1;
    map <int, int> mp2;
    mp1[x];
    mp2[x];
    for(i = 1; i < n; i ++)
    {
        cin >> x;
        map <int, int> :: iterator it = mp1.lower_bound(x);
        if(it != mp1.begin())
        {
            it --;
            cout << (*it).first << " ";
            mp1.erase(it);
            mp1[x];
            mp2[x];
        }
        else
        {
            map <int, int> :: iterator it1 = mp2.lower_bound(x);
            if(it1 == mp2.end())
                it1 --;
            cout << (*it1).first << " ";
            mp2.erase(it1);
            mp1[x];
            mp2[x];
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
main()
{
    int n, m, i, j, x;
    cin >> n;
    cin >> x;
    map <int, int> mp1;
    map <int, int> mp2;
    mp1[x];
    mp2[x];
    for(i = 1; i < n; i ++)
    {
        cin >> x;
        map <int, int> :: iterator it = mp1.lower_bound(x);
        if(it != mp1.begin())
        {
            it --;
            cout << (*it).first << " ";
            mp1[x];
            mp2[x];
        }
        else
        {
            map <int, int> :: iterator it = mp2.lower_bound(x);
            if(it == mp2.end())
                it --;
            cout << (*it).first << " ";
            mp1[x];
            mp2[x];
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
main()
{
    int n, m, i, j, x;
    cin >> n;
    cin >> x;
    map <int, int> mp1;
    map <int, int> mp2;
    mp1[x];
    mp2[x];
    mp2[1e9 + 1];
    for(i = 1; i < n; i ++)
    {
        cin >> x;
        map <int, int> :: iterator it = mp1.lower_bound(x);
        if(it != mp1.begin())
        {
            it --;
            cout << (*it).first << " ";
            mp1.erase(it);
            mp1[x];
            mp2[x];
        }
        else
        {
            map <int, int> :: iterator it1 = mp2.lower_bound(x);
            if((*it1).first == 1e9 + 1)
                it1 --;
            cout << (*it1).first << " ";
            mp2.erase(it1);
            mp1[x];
            mp2[x];
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
main()
{
    int n, m, i, j, x;
    cin >> n;
    cin >> x;
    map <int, int> mp1;
    map <int, int> mp2;
    mp1[x];
    mp2[x];
    for(i = 1; i < n; i ++)
    {
        cin >> x;
        map <int, int> :: iterator it = mp1.lower_bound(x);
        if(it != mp1.begin())
        {
            it --;
            cout << (*it).first << " ";
            mp1[x];
            mp2[x];
            mp1.erase(it);
        }
        else
        {
            map <int, int> :: iterator it = mp2.lower_bound(x);
            if(it == mp2.end())
                it --;
            cout << (*it).first << " ";
            mp2.erase(it);
            mp1[x];
            mp2[x];
        }
    }
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1123456;
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
int read()
{
    int x;
    scanf("%I64d", &x);
    return x;
}
main()
{
    int n, m, i, j;
    cin >> n;
    int x = read();
    map <int, pair <int, int> > mp;
    mp[x];
    for(i = 1; i < n; i ++) {
        x = read();
        mp[x];
        map <int, pair <int, int> > :: iterator it = mp.lower_bound(x);
        if(it != mp.begin()) {
            it --;
            if((*it).second.second == 0)
                cout << (*it).first << " ";
            (*it).second.second = 1;
            it ++;
        }
        it ++;
        if(it != mp.end()) {
            if((*it).second.first == 0)
                cout << (*it).first << " ";
            (*it).second.first = 1;
        }
    }
}


#include <iostream>
#include <vector>

using namespace std;

vector<int> p;

struct Node {
    Node(const int &val_ = 0, Node *parent_ = nullptr, Node *l_child_ = nullptr, Node *r_child_ = nullptr) {
        val = val_;
        parent = parent_;
        l_child = l_child_;
        r_child = r_child_;
    }
    ~Node() {
        if(l_child)
            delete l_child;
        if(r_child)
            delete r_child;
    }

    void push(const int &val_) {
        Node *cur = this;
        while(val_ > cur->val ? cur->r_child : cur->l_child) {
            if(val_ > val)
                cur = r_child;
            else
                cur = l_child;
        }
        Node *new_ = new Node(val_, cur);
        if(val_ > cur->val) {
            p.push_back(cur->val);
            cur->r_child = new_;
        } else {
            p.push_back(cur->val);
            cur->l_child = new_;
        }
    }
    Node *l_child, *r_child, *parent;
    int val;
};

int main() {
    int n;
    cin >> n;
    int a[n];
    for(int i = 0;i < n;i++)
        cin >> a[i];
    Node *root = new Node(a[0]);
    for(int i = 1;i < n;i++) {
        root->push(a[i]);
    }
    for(const int &e : p) {
        cout << e << ' ';
    }
    delete root;
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int gentest();
struct Node;

vector<int> p;

struct Node {
    Node(const int &val_ = 0, int parent_ = -1) {
        val = val_;
        parent = parent_;
        l_child = -1;
        r_child = -1;
    }
    int parent, r_child, l_child;
    int val;
};

Node tree[100000];

void push(const int &val_, const int &ind) {
    int ptr = 0;
    while(val_ > tree[ptr].val ? tree[tree[ptr].r_child].val != 0 : tree[tree[ptr].l_child].val != 0) {
        ptr = (val_ > tree[ptr].val ? tree[ptr].r_child : tree[ptr].l_child);
    }
    tree[ind] = Node(val_, ptr);
    if(val_ > tree[ptr].val) {
        p.push_back(tree[ptr].val);
        tree[ptr].r_child = ind;
    } else {
        p.push_back(tree[ptr].val);
        tree[ptr].l_child = ind;
    }
}

int main() {
//    gentest();
//    ifstream cin("input.txt");
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    int a[n];
    for(int i = 0;i < n;i++)
        cin >> a[i];
    tree[0] = Node(a[0]);
    for(int i = 1;i < n;i++) {
        push(a[i], i);
    }
    for(const int &e : p) {
        cout << e << ' ';
    }
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int gentest();

vector<int> p;

struct Node {
    Node(const int &val_ = 0, Node *parent_ = nullptr, Node *l_child_ = nullptr, Node *r_child_ = nullptr) {
        val = val_;
        parent = parent_;
        l_child = l_child_;
        r_child = r_child_;
    }
    ~Node() {
        if(l_child)
            delete l_child;
        if(r_child)
            delete r_child;
    }

    void push(const int &val_) {
        Node *cur = this;
        while(val_ > cur->val ? cur->r_child : cur->l_child) {
            cur = (val_ > cur->val ? cur->r_child : cur->l_child);
        }
        Node *new_ = new Node(val_, cur);
        if(val_ > cur->val) {
            p.push_back(cur->val);
            cur->r_child = new_;
        } else {
            p.push_back(cur->val);
            cur->l_child = new_;
        }
    }
    Node *l_child, *r_child, *parent;
    int val;
};

int main() {
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    int a[n];
    for(int i = 0;i < n;i++)
        cin >> a[i];
    Node *root = new Node(a[0]);
    for(int i = 1;i < n;i++) {
        root->push(a[i]);
    }
    for(const int &e : p) {
        cout << e << ' ';
    }
    delete root;
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

int gentest();

struct Node {
    Node(const int &val_) : val(val_) {}
    int val;
    mutable vector<bool> child = {false, false};
};

bool operator<(const Node &node1, const Node &node2) {
    return node1.val < node2.val;
}

int main() {
//    gentest();
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    int ai;
    cin >> n >> ai;
    set<Node> s;
    s.insert(Node(0));
    s.insert(Node(1000000001));
    s.insert(Node(ai));
    for(int i = 1;i < n;i++) {
        cin >> ai;
        set<Node>::iterator l, r;
        r = s.upper_bound(ai);
        r--;
        l = r;
        r++;
        if(l->val == 0) {
            l++;
            l->child[0] = true;
            cout << l->val;
        } else if(r->val == 1000000001) {
            r--;
            r->child[1] = true;
            cout << r->val;
        } else if(r->child[0]) {
            l->child[1] = true;
            cout << l->val;
        } else if(l->child[1]) {
            r->child[0] = true;
            cout << r->val;
        }
        cout << ' ';
        s.insert(Node(ai));
    }
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int gentest();
struct Node;

vector<int> p;

struct Node {
    Node(const int &val_ = 0, int parent_ = -1) {
        val = val_;
        parent = parent_;
        l_child = -1;
        r_child = -1;
    }
    int parent, r_child, l_child;
    int val;
};

Node tree[100'000];

void push(const int &val_, const int &ind) {
    int ptr = 0;
    while(val_ > tree[ptr].val ? tree[tree[ptr].r_child].val != 0 : tree[tree[ptr].l_child].val != 0) {
        ptr = (val_ > tree[ptr].val ? tree[ptr].r_child : tree[ptr].l_child);
    }
    tree[ind] = Node(val_, ptr);
    if(val_ > tree[ptr].val) {
        p.push_back(tree[ptr].val);
        tree[ptr].r_child = ind;
    } else {
        p.push_back(tree[ptr].val);
        tree[ptr].l_child = ind;
    }
}

int main() {
//    gentest();
//    ifstream cin("input.txt");
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    int a[n];
    for(int i = 0;i < n;i++)
        cin >> a[i];
    tree[0] = Node(a[0]);
    for(int i = 1;i < n;i++) {
        push(a[i], i);
    }
    for(const int &e : p) {
        cout << e << ' ';
    }
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int gentest();

vector<int> p;

struct Node {
    Node(const int &val_ = 0, Node *parent_ = nullptr, Node *l_child_ = nullptr, Node *r_child_ = nullptr) {
        val = val_;
        parent = parent_;
        l_child = l_child_;
        r_child = r_child_;
    }
    ~Node() {
        if(l_child)
            delete l_child;
        if(r_child)
            delete r_child;
    }

    Node* push(const int &val_) {
        Node *cur = this;
        while(val_ > cur->val ? cur->r_child : cur->l_child) {
            cur = (val_ > cur->val ? cur->r_child : cur->l_child);
        }
        Node *new_ = new Node(val_, cur);
        if(val_ > cur->val) {
            p.push_back(cur->val);
            cur->r_child = new_;
        } else {
            p.push_back(cur->val);
            cur->l_child = new_;
        }
        return new_;
    }
    Node *l_child, *r_child, *parent;
    int val;
};

int main() {
//    gentest();
//    ifstream cin("input.txt");
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    int a[n];
    for(int i = 0;i < n;i++)
        cin >> a[i];
    Node *root = new Node(a[0]), *last;
    last = root;
    for(int i = 1;i < n;i++) {
        if(a[i] > last->val)
            last = last->push(a[i]);
        else
            root->push(a[i]);
    }
    for(const int &e : p) {
        cout << e << ' ';
    }
    delete root;
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

int gentest();

struct Node {
    Node(const int &val_) : val(val_) {}
    int val;
    mutable vector<bool> child = {false, false};
};

bool operator<(const Node &node1, const Node &node2) {
    return node1.val < node2.val;
}

int main() {
//    gentest();
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    int a[n], ans[n - 1];
    for(int i = 0;i < n;i++)
        cin >> a[i];
    set<Node> s;
    s.insert(Node(0));
    s.insert(Node(1000000001));
    s.insert(Node(a[0]));
    for(int i = 1;i < n;i++) {
        set<Node>::iterator l, r;
        r = s.upper_bound(a[i]);
        r--;
        l = r;
        r++;
        if(l->val == 0) {
            l++;
            l->child[0] = true;
            ans[i - 1] = l->val;
        } else if(r->val == 1000000001) {
            r--;
            r->child[1] = true;
            ans[i - 1] = r->val;
        } else if(r->child[0]) {
            l->child[1] = true;
            ans[i - 1] = l->val;
        } else if(l->child[1]) {
            r->child[0] = true;
            ans[i - 1] = r->val;
        }
        s.insert(Node(a[i]));
    }
    for(int i = 0;i < n - 1;i++) {
        cout << ans[i] << ' ';
    }
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int gentest();

vector<int> p;

struct Node {
    Node(const int &val_ = 0, Node *parent_ = nullptr, Node *l_child_ = nullptr, Node *r_child_ = nullptr) {
        val = val_;
        parent = parent_;
        l_child = l_child_;
        r_child = r_child_;
    }
    ~Node() {
        if(l_child)
            delete l_child;
        if(r_child)
            delete r_child;
    }

    Node* push(const int &val_) {
        Node *cur = this;
        while(val_ > cur->val ? cur->r_child : cur->l_child) {
            cur = (val_ > cur->val ? cur->r_child : cur->l_child);
        }
        Node *new_ = new Node(val_, cur);
        if(val_ > cur->val) {
            p.push_back(cur->val);
            cur->r_child = new_;
        } else {
            p.push_back(cur->val);
            cur->l_child = new_;
        }
        return new_;
    }
    Node *l_child, *r_child, *parent;
    int val;
};

int main() {
//    gentest();
//    ifstream cin("input.txt");
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    int a[n];
    for(int i = 0;i < n;i++)
        cin >> a[i];
    Node *root = new Node(a[0]), *r_last, *l_last;
    r_last = root;
    l_last = root;
    for(int i = 1;i < n;i++) {
        if(a[i] > r_last->val)
            r_last = r_last->push(a[i]);
        else if(a[i] <= l_last->val)
            l_last = l_last->push(a[i]);
        else
            root->push(a[i]);
    }
    for(const int &e : p) {
        cout << e << ' ';
    }
    delete root;
    return 0;
}


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int gentest();

vector<int> p;

struct Node {
    Node(const int &val_ = 0, Node *parent_ = nullptr, Node *l_child_ = nullptr, Node *r_child_ = nullptr) {
        val = val_;
        parent = parent_;
        l_child = l_child_;
        r_child = r_child_;
    }
    ~Node() {
        if(l_child)
            delete l_child;
        if(r_child)
            delete r_child;
    }

    Node* push(const int &val_) {
        Node *cur = this;
        while(val_ > cur->val ? cur->r_child : cur->l_child) {
            cur = (val_ > cur->val ? cur->r_child : cur->l_child);
        }
        Node *new_ = new Node(val_, cur);
        if(val_ > cur->val) {
            p.push_back(cur->val);
            cur->r_child = new_;
        } else {
            p.push_back(cur->val);
            cur->l_child = new_;
        }
        return new_;
    }
    Node *l_child, *r_child, *parent;
    int val;
};

int main() {
//    gentest();
//    ifstream cin("input.txt");
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    int a[n];
    for(int i = 0;i < n;i++)
        cin >> a[i];
    Node *root = new Node(a[0]), *last;
    last = root;
    for(int i = 1;i < n;i++) {
        if(a[i] > last->val)
            last = last->push(a[i]);
        else
            last = root->push(a[i]);
    }
    for(const int &e : p) {
        cout << e << ' ';
    }
    delete root;
    return 0;
}


 #include <bits/stdc++.h>

using namespace std;

string s, s1, t;

int n;

int main (){
cin >> t;
for (int i = 0;i < t.size();++i){
  if (t[i] != 'a'){
    s1 += t[i];
  }
  s += t[i];
  if (s + s1 == t){
    cout << s;
    return 0;
  }
  if (s.size() + s1.size() > t.size()){
    break;
  }
}
cout << ":(";
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
	string s;
	cin>>s;
	int l,count=0,v=0,j=0,mm=0;
	l=s.length();
	string ss;
	ss=s;
	for(int i=0;i<l;i++)
	{
		if(s[i]=='a')
		count++;
		else
		{
			ss[j]=s[i];
			j++;
		}
	}
	v=l-count;
	for(int i=0;i<v/2;i++)
	{
		if(ss[i]!=ss[i+4])
		{
			mm=1;
		}
	}
	if(mm==0)
	{
		for(int i=0;i<(l-v/2);i++)
		cout<<s[i];

	}
	else
	cout<<":(";

	return 0;
}


    /***********************************************************/
   /*********************<>SRIJAN DE</>************************/
  /******<>JALPAIGURI GOVERNMENT ENGINEERING COLLEGE</>*******/
 /****************<>INFORMATION TECHNOLOGY</>****************/
/***********************************************************/
/********   All Required Header Files ********/
#include <bits/stdc++.h>
using namespace std;
/*******  All Required define Pre-Processors and typedef Constants *******/
#define ld long double
#define lli long long int
#define nl cout<<endl
#define tez ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Solve solve();
#define Solves lli test;cin>>test;while(test--){solve();}
#define rr return 0;
#define vecitr vector<lli>::iterator
#define pb push_back
#define beg begin()
#define end end()
#define sci std::cout<<std::scientific;
#define fix std::cout<<std::fixed;
#pragma GCC optimize("O3")
#define pi 3.1415926535897932384626433832795
#define sqr(n) ( n * n )
#define mem(a,val) memset( a,  val,  sizeof(a) )
#define in insert
#define len size()
#define loop(i,n) for(lli i=0;i<n;i++)
#define ub(a,n,x) upper_bound(a,a+n,x)-a
#define lb(a,n,x) lower_bound(a,a+n,x)-a
#define search(a,n,x) binary_search(a,a+n,x)
/**************************************************************************/
typedef pair<lli, lli> pii;
typedef vector<lli> vi;
typedef vector<lli>::iterator vit;
typedef vector<string> vs;
typedef vector<string>::iterator vst;
typedef vector<pii> vii;
typedef vector<pii>::iterator viit;
typedef vector<vi> vvi;
typedef vector<vi>::iterator vvit;
typedef map<lli,lli> mpii;
typedef map<string,lli> mpsi;
typedef set<lli> seti;
typedef set<char> setc;
typedef multiset<lli> mseti;
/*************** Template of some basic operations ****************/
template<class x>x mod(x a){fix return fmod(a,1000000007);}
template<class x,class y>x max(x a,y b){return (a>b)?a:b;}
template<class x,class y>x min(x a,y b){return (a<b)?a:b;}
template<class x,class y>x LOG(x n,y r){return (n>r-1)?1+LOG(n/r,r):0;}
/******** User-defined Variable *********************************/
string abc=" abcdefghijklmnopqrstuvwxyz";
string ABC=" ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int numaxi123[10]={48,49,50,51,52,53,54,55,56,57};
/******** User-defined Function *********************************/
bool isPrime(int n)
{
    bool prime[n + 1];
    memset( prime , true, sizeof(prime));
    for (int p = 2; p * p <= n; p++) {
        if (prime[p] == true) {
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
    if(n==1)
        return false;
    else if (prime[n])
        return true;
    else
        return false;
}
//sort a string
	lli a[100007]={0},maxi=0,mini=1000000000;
void sorts(string &str){sort(str.beg, str.end);}
/******************Solve*******************************************/
void solve()
{
	string t,s,ss;
	cin>>t;
	lli ctr=0;
	for(lli i=0;i<t.length();i++)
	{
		if(t[i]!='a')
		{
			s+=t[i];
		}
		else
		ctr=i;

	}
	lli l=s.length();
	lli ctr2=t.length(),ctr3=0;
	for(lli i=t.length()-1;i>=0;i--)
	{
		if(ctr3==l/2)
		break;
		else if(t[i]!='a')
		{
			ctr3++;
			ctr2=i;
		}
	}
//	cout<<s;
//cout<<ctr<<"l"<<ctr2<<"l"<<ctr3<<"k"<<ctr3;
	if(ctr2<ctr && ctr!=0)
	cout<<":(";
	else if(s.substr(0,l/2)==s.substr(l/2,l/2))
	cout<<t.substr(0,(t.length()-(l/2)));
	else
	cout<<":(";

}
/********************************Main*****************************/
int main()
{
tez
Solve
rr
}
/*****************************************************************/


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
	string s;
	cin>>s;
	int l,count=0,v=0,j=0,mm=0;
	l=s.length();
	string ss;
	ss=s;
	for(int i=0;i<l;i++)
	{
		if(s[i]=='a')
		count++;
		else
		{
			ss[j]=s[i];
			j++;
		}
	}
	v=l-count;
	for(int i=0;i<v/2;i++)
	{
		if(ss[i]!=ss[l-v/2+i])
		{
			mm=1;
		}
	}
	if(mm==0 && v%2==0)
	{
		for(int i=0;i<(l-v/2);i++)
		cout<<s[i];

	}
	else
	cout<<":(";

	//cout<<mm;
	return 0;
}


    /***********************************************************/
   /*********************<>SRIJAN DE</>************************/
  /******<>JALPAIGURI GOVERNMENT ENGINEERING COLLEGE</>*******/
 /****************<>INFORMATION TECHNOLOGY</>****************/
/***********************************************************/
/********   All Required Header Files ********/
#include <bits/stdc++.h>
using namespace std;
/*******  All Required define Pre-Processors and typedef Constants *******/
#define ld long double
#define lli long long int
#define nl cout<<endl
#define tez ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Solve solve();
#define Solves lli test;cin>>test;while(test--){solve();}
#define rr return 0;
#define vecitr vector<lli>::iterator
#define pb push_back
#define beg begin()
#define end end()
#define sci std::cout<<std::scientific;
#define fix std::cout<<std::fixed;
#pragma GCC optimize("O3")
#define pi 3.1415926535897932384626433832795
#define sqr(n) ( n * n )
#define mem(a,val) memset( a,  val,  sizeof(a) )
#define in insert
#define len size()
#define loop(i,n) for(lli i=0;i<n;i++)
#define ub(a,n,x) upper_bound(a,a+n,x)-a
#define lb(a,n,x) lower_bound(a,a+n,x)-a
#define search(a,n,x) binary_search(a,a+n,x)
/**************************************************************************/
typedef pair<lli, lli> pii;
typedef vector<lli> vi;
typedef vector<lli>::iterator vit;
typedef vector<string> vs;
typedef vector<string>::iterator vst;
typedef vector<pii> vii;
typedef vector<pii>::iterator viit;
typedef vector<vi> vvi;
typedef vector<vi>::iterator vvit;
typedef map<lli,lli> mpii;
typedef map<string,lli> mpsi;
typedef set<lli> seti;
typedef set<char> setc;
typedef multiset<lli> mseti;
/*************** Template of some basic operations ****************/
template<class x>x mod(x a){fix return fmod(a,1000000007);}
template<class x,class y>x max(x a,y b){return (a>b)?a:b;}
template<class x,class y>x min(x a,y b){return (a<b)?a:b;}
template<class x,class y>x LOG(x n,y r){return (n>r-1)?1+LOG(n/r,r):0;}
/******** User-defined Variable *********************************/
string abc=" abcdefghijklmnopqrstuvwxyz";
string ABC=" ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int numaxi123[10]={48,49,50,51,52,53,54,55,56,57};
/******** User-defined Function *********************************/
bool isPrime(int n)
{
    bool prime[n + 1];
    memset( prime , true, sizeof(prime));
    for (int p = 2; p * p <= n; p++) {
        if (prime[p] == true) {
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
    if(n==1)
        return false;
    else if (prime[n])
        return true;
    else
        return false;
}
//sort a string
	lli a[100007]={0},maxi=0,mini=1000000000;
void sorts(string &str){sort(str.beg, str.end);}
/******************Solve*******************************************/
void solve()
{
	string t,s,ss;
	cin>>t;
	lli ctr=0;
	for(lli i=0;i<t.length();i++)
	{
		if(t[i]!='a')
		{
			s+=t[i];
		}
		else
		ctr=i;

	}
	lli l=s.length();
	lli ctr2=t.length(),ctr3=0;
	for(lli i=t.length()-1;i>=0;i--)
	{
		if(ctr3==l/2)
		break;
		else if(t[i]!='a')
		{
			ctr3++;
			ctr2=i;
		}
	}
//	cout<<s;
//cout<<ctr<<"l"<<ctr2<<"l"<<ctr3<<"k"<<ctr3;
	if(ctr2<ctr)
	cout<<":(";
	else if(s.substr(0,l/2)==s.substr(l/2,l/2))
	cout<<t.substr(0,(t.length()-(l/2)));
	else
	cout<<":(";

}
/********************************Main*****************************/
int main()
{
tez
Solve
rr
}
/*****************************************************************/


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
	string s;
	cin>>s;
	int l,count=0,v=0,j=0,mm=0;
	l=s.length();
	string ss;
	ss=s;
	for(int i=0;i<l;i++)
	{
		if(s[i]=='a')
		count++;
		else
		{
			ss[j]=s[i];
			j++;
		}
	}
	v=l-count;
	for(int i=0;i<v/2;i++)
	{
		if(ss[i]!=ss[i+count])
		{
			mm=1;
		}
	}
	if(mm==0 && v%2==0)
	{
		for(int i=0;i<(l-v/2);i++)
		cout<<s[i];

	}
	else
	cout<<":(";

	//cout<<mm;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
	string s;
	cin>>s;
	int l,count=0,v=0,j=0,mm=0;
	l=s.length();
	string ss;
	ss=s;
	for(int i=0;i<l;i++)
	{
		if(s[i]=='a')
		count++;
		else
		{
			ss[j]=s[i];
			j++;
		}
	}
	v=l-count;
	for(int i=0;i<v/2;i++)
	{
		if(ss[i]!=ss[i+4])
		{
			mm=1;
		}
	}
	if(mm==0 && v%2==0)
	{
		for(int i=0;i<(l-v/2);i++)
		cout<<s[i];

	}
	else
	cout<<":(";

	return 0;
}


    /***********************************************************/
   /*********************<>SRIJAN DE</>************************/
  /******<>JALPAIGURI GOVERNMENT ENGINEERING COLLEGE</>*******/
 /****************<>INFORMATION TECHNOLOGY</>****************/
/***********************************************************/
/********   All Required Header Files ********/
#include <bits/stdc++.h>
using namespace std;
/*******  All Required define Pre-Processors and typedef Constants *******/
#define ld long double
#define lli long long int
#define nl cout<<endl
#define tez ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Solve solve();
#define Solves lli test;cin>>test;while(test--){solve();}
#define rr return 0;
#define vecitr vector<lli>::iterator
#define pb push_back
#define beg begin()
#define end end()
#define sci std::cout<<std::scientific;
#define fix std::cout<<std::fixed;
#pragma GCC optimize("O3")
#define pi 3.1415926535897932384626433832795
#define sqr(n) ( n * n )
#define mem(a,val) memset( a,  val,  sizeof(a) )
#define in insert
#define len size()
#define loop(i,n) for(lli i=0;i<n;i++)
#define ub(a,n,x) upper_bound(a,a+n,x)-a
#define lb(a,n,x) lower_bound(a,a+n,x)-a
#define search(a,n,x) binary_search(a,a+n,x)
/**************************************************************************/
typedef pair<lli, lli> pii;
typedef vector<lli> vi;
typedef vector<lli>::iterator vit;
typedef vector<string> vs;
typedef vector<string>::iterator vst;
typedef vector<pii> vii;
typedef vector<pii>::iterator viit;
typedef vector<vi> vvi;
typedef vector<vi>::iterator vvit;
typedef map<lli,lli> mpii;
typedef map<string,lli> mpsi;
typedef set<lli> seti;
typedef set<char> setc;
typedef multiset<lli> mseti;
/*************** Template of some basic operations ****************/
template<class x>x mod(x a){fix return fmod(a,1000000007);}
template<class x,class y>x max(x a,y b){return (a>b)?a:b;}
template<class x,class y>x min(x a,y b){return (a<b)?a:b;}
template<class x,class y>x LOG(x n,y r){return (n>r-1)?1+LOG(n/r,r):0;}
/******** User-defined Variable *********************************/
string abc=" abcdefghijklmnopqrstuvwxyz";
string ABC=" ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int numaxi123[10]={48,49,50,51,52,53,54,55,56,57};
/******** User-defined Function *********************************/
bool isPrime(int n)
{
    bool prime[n + 1];
    memset( prime , true, sizeof(prime));
    for (int p = 2; p * p <= n; p++) {
        if (prime[p] == true) {
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
    if(n==1)
        return false;
    else if (prime[n])
        return true;
    else
        return false;
}
//sort a string
	lli a[100007]={0},maxi=0,mini=1000000000;
void sorts(string &str){sort(str.beg, str.end);}
/******************Solve*******************************************/
void solve()
{
	string t,s,ss;
	cin>>t;
	lli ctr=0;
	for(lli i=0;i<t.length();i++)
	{
		if(t[i]!='a')
		{
			s+=t[i];
		}
		else
		ctr=i;

	}
	lli l=s.length();
	lli ctr2=t.length(),ctr3=0;
	for(lli i=t.length()-1;i>=0;i--)
	{
		if(ctr3==l/2)
		break;
		else if(t[i]!='a')
		{
			ctr3++;
			ctr2=i;
		}
	}
//	cout<<s;
//cout<<ctr<<"l"<<ctr2<<"l"<<ctr3<<"k"<<ctr3;
	if(ctr2<ctr||l%2!=0)
	cout<<":(";
	else if(s.substr(0,l/2)==s.substr(l/2,l/2))
	cout<<t.substr(0,(t.length()-(l/2)));
	else
	cout<<":(";

}
/********************************Main*****************************/
int main()
{
tez
Solve
rr
}
/*****************************************************************/


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
	string s;
	cin>>s;
	ll l,count=0,v=0,j=0,mm=0;
	l=s.length();
	string ss;
	ss=s;
	for(ll i=0;i<l;i++)
	{
		if(s[i]=='a')
		count++;
		else
		{
			ss[j]=s[i];
			j++;
		}
	}
	v=l-count;
	for(ll i=0;i<v/2;i++)
	{
		if(ss[i]!=ss[l-v/2+i])
		{
			mm=1;
		}
	}
	if(mm==0 && v%2==0)
	{
		for(ll i=0;i<(l-v/2);i++)
		cout<<s[i];

	}
	else
	cout<<":(";

	//cout<<mm;
	return 0;
}


    /***********************************************************/
   /*********************<>SRIJAN DE</>************************/
  /******<>JALPAIGURI GOVERNMENT ENGINEERING COLLEGE</>*******/
 /****************<>INFORMATION TECHNOLOGY</>****************/
/***********************************************************/
/********   All Required Header Files ********/
#include <bits/stdc++.h>
using namespace std;
/*******  All Required define Pre-Processors and typedef Constants *******/
#define ld long double
#define lli long long int
#define nl cout<<endl
#define tez ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Solve solve();
#define Solves lli test;cin>>test;while(test--){solve();}
#define rr return 0;
#define vecitr vector<lli>::iterator
#define pb push_back
#define beg begin()
#define end end()
#define sci std::cout<<std::scientific;
#define fix std::cout<<std::fixed;
#pragma GCC optimize("O3")
#define pi 3.1415926535897932384626433832795
#define sqr(n) ( n * n )
#define mem(a,val) memset( a,  val,  sizeof(a) )
#define in insert
#define len size()
#define loop(i,n) for(lli i=0;i<n;i++)
#define ub(a,n,x) upper_bound(a,a+n,x)-a
#define lb(a,n,x) lower_bound(a,a+n,x)-a
#define search(a,n,x) binary_search(a,a+n,x)
/**************************************************************************/
typedef pair<lli, lli> pii;
typedef vector<lli> vi;
typedef vector<lli>::iterator vit;
typedef vector<string> vs;
typedef vector<string>::iterator vst;
typedef vector<pii> vii;
typedef vector<pii>::iterator viit;
typedef vector<vi> vvi;
typedef vector<vi>::iterator vvit;
typedef map<lli,lli> mpii;
typedef map<string,lli> mpsi;
typedef set<lli> seti;
typedef set<char> setc;
typedef multiset<lli> mseti;
/*************** Template of some basic operations ****************/
template<class x>x mod(x a){fix return fmod(a,1000000007);}
template<class x,class y>x max(x a,y b){return (a>b)?a:b;}
template<class x,class y>x min(x a,y b){return (a<b)?a:b;}
template<class x,class y>x LOG(x n,y r){return (n>r-1)?1+LOG(n/r,r):0;}
/******** User-defined Variable *********************************/
string abc=" abcdefghijklmnopqrstuvwxyz";
string ABC=" ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int numaxi123[10]={48,49,50,51,52,53,54,55,56,57};
/******** User-defined Function *********************************/
bool isPrime(int n)
{
    bool prime[n + 1];
    memset( prime , true, sizeof(prime));
    for (int p = 2; p * p <= n; p++) {
        if (prime[p] == true) {
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
    if(n==1)
        return false;
    else if (prime[n])
        return true;
    else
        return false;
}
//sort a string
	lli a[100007]={0},maxi=0,mini=1000000000;
void sorts(string &str){sort(str.beg, str.end);}
/******************Solve*******************************************/
void solve()
{
	string t,s,ss;
	cin>>t;
	lli ctr;
	for(lli i=0;i<t.length();i++)
	{
		if(t[i]!='a')
		{
			s+=t[i];
		}
		else
		ctr=i;

	}
	lli l=s.length();
	lli ctr2=t.length(),ctr3=0;
	for(lli i=t.length()-1;i>=0;i--)
	{
		if(ctr3==l/2)
		break;
		else if(t[i]!='a')
		{
			ctr3++;
			ctr2=i;
		}
	}
//	cout<<s;
//cout<<ctr<<"l"<<ctr2<<"l"<<ctr3<<"k"<<ctr3;
	if(ctr2<ctr)
	cout<<":(";
	else if(s.substr(0,l/2)==s.substr(l/2,l/2))
	cout<<t.substr(0,(t.length()-(l/2)));
	else
	cout<<":(";

}
/********************************Main*****************************/
int main()
{
tez
Solve
rr
}
/*****************************************************************/


    /***********************************************************/
   /*********************<>SRIJAN DE</>************************/
  /******<>JALPAIGURI GOVERNMENT ENGINEERING COLLEGE</>*******/
 /****************<>INFORMATION TECHNOLOGY</>****************/
/***********************************************************/
/********   All Required Header Files ********/
#include <bits/stdc++.h>
using namespace std;
/*******  All Required define Pre-Processors and typedef Constants *******/
#define ld long double
#define lli long long int
#define nl cout<<endl
#define tez ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Solve solve();
#define Solves lli test;cin>>test;while(test--){solve();}
#define rr return 0;
#define vecitr vector<lli>::iterator
#define pb push_back
#define beg begin()
#define end end()
#define sci std::cout<<std::scientific;
#define fix std::cout<<std::fixed;
#pragma GCC optimize("O3")
#define pi 3.1415926535897932384626433832795
#define sqr(n) ( n * n )
#define mem(a,val) memset( a,  val,  sizeof(a) )
#define in insert
#define len size()
#define loop(i,n) for(lli i=0;i<n;i++)
#define ub(a,n,x) upper_bound(a,a+n,x)-a
#define lb(a,n,x) lower_bound(a,a+n,x)-a
#define search(a,n,x) binary_search(a,a+n,x)
/**************************************************************************/
typedef pair<lli, lli> pii;
typedef vector<lli> vi;
typedef vector<lli>::iterator vit;
typedef vector<string> vs;
typedef vector<string>::iterator vst;
typedef vector<pii> vii;
typedef vector<pii>::iterator viit;
typedef vector<vi> vvi;
typedef vector<vi>::iterator vvit;
typedef map<lli,lli> mpii;
typedef map<string,lli> mpsi;
typedef set<lli> seti;
typedef set<char> setc;
typedef multiset<lli> mseti;
/*************** Template of some basic operations ****************/
template<class x>x mod(x a){fix return fmod(a,1000000007);}
template<class x,class y>x max(x a,y b){return (a>b)?a:b;}
template<class x,class y>x min(x a,y b){return (a<b)?a:b;}
template<class x,class y>x LOG(x n,y r){return (n>r-1)?1+LOG(n/r,r):0;}
/******** User-defined Variable *********************************/
string abc=" abcdefghijklmnopqrstuvwxyz";
string ABC=" ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int numaxi123[10]={48,49,50,51,52,53,54,55,56,57};
/******** User-defined Function *********************************/
bool isPrime(int n)
{
    bool prime[n + 1];
    memset( prime , true, sizeof(prime));
    for (int p = 2; p * p <= n; p++) {
        if (prime[p] == true) {
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
    if(n==1)
        return false;
    else if (prime[n])
        return true;
    else
        return false;
}
//sort a string
	lli a[100007]={0},maxi=0,mini=1000000000;
void sorts(string &str){sort(str.beg, str.end);}
/******************Solve*******************************************/
void solve()
{
	string t,s,ss;
	cin>>t;
	lli ctr;
	for(lli i=0;i<t.length();i++)
	{
		if(t[i]!='a')
		{
			s+=t[i];
		}
		else
		ctr=i;

	}
	lli l=s.length();
	lli ctr2=t.length(),ctr3=0;
	for(lli i=t.length()-1;i>=0;i--)
	{
		if(ctr3==l/2)
		break;
		else if(t[i]!='a')
		{
			ctr3++;
			ctr2=i;
		}
	}
//	cout<<s;
//cout<<ctr<<"l"<<ctr2<<"l"<<ctr3<<"k"<<ctr3;
	if(ctr2==ctr&&ctr!=0)
	cout<<":(";
	else if(s.substr(0,l/2)==s.substr(l/2,l/2))
	cout<<t.substr(0,(t.length()-(l/2)));
	else
	cout<<":(";

}
/********************************Main*****************************/
int main()
{
tez
Solve
rr
}
/*****************************************************************/


/*BINH :))*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long llu;
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define f  first
#define s  second
#define mp make_pair
#define pb push_back
#define ep emplace_back
#define sz(a) (int) a.size()
#define FOR(x,a,b) for (int x=a;x<=b;x++)
#define FOD(x,a,b) for (int x=a;x>=b;x--)
#define REP(x,a,b) for (int x=a;x<b;x++)
#define RED(x,a,b) for (int x=a;x>b;x--)
#define EL cout<<endl
#define max 1005
#define vi vector<int>
#define ii pair<int,int>
#define vii vector<ii>
void init(){
}
int counta;
int main(){
    IO
    init();
    string s, ss,tmp;
    cin>>s;
    FOD(i,sz(s)-1,0){
        if(s[i]=='a')break;
        ss = s[i] + ss;
    }
    //cout<<ss;
    REP(i,0,sz(s)){
        if(s[i]!='a')tmp=tmp+s[i];
        else counta++;
    }
    if(sz(tmp)&1){
        cout<<":(";return 0;
    }
    if(counta==0){
        REP(i,0,sz(tmp)/2){
            cout<<s[i];
        }
        return 0;
    }
    if(tmp==""&&ss==""){
        cout<<s;
        return 0;
    }

    int cur1 = 0 , cur2 = sz(tmp) / 2, fl=1;
    REP(i,0,sz(tmp)/2){
        if( tmp[cur1] != tmp[cur2] ){
            fl=0;
            break;
        }
        cur1++;
        cur2++;
    }
    if(fl==0){
        cout<<":(";
    }

    else {
        if(ss=="")cout<<":(";
        else{
            ll cc = 0;
            REP(i,0,sz(s)){
                if(s[i]!='a')cc++;
                cout<<s[i];
                if(cc==cur1)break;

            }
        }
    }
}


/*BINH :))*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long llu;
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define f  first
#define s  second
#define mp make_pair
#define pb push_back
#define ep emplace_back
#define sz(a) (int) a.size()
#define FOR(x,a,b) for (int x=a;x<=b;x++)
#define FOD(x,a,b) for (int x=a;x>=b;x--)
#define REP(x,a,b) for (int x=a;x<b;x++)
#define RED(x,a,b) for (int x=a;x>b;x--)
#define EL cout<<endl
#define max 1005
#define vi vector<int>
#define ii pair<int,int>
#define vii vector<ii>
void init(){
}
int counta;
string s, ss,tmp;
int main(){
    IO
    init();

    cin>>s;
    FOD(i,sz(s)-1,0){
        if(s[i]=='a')break;
        ss = s[i] + ss;
    }
    //cout<<ss;
    REP(i,0,sz(s)){
        if(s[i]!='a')tmp=tmp+s[i];
        else counta++;
    }
    if(sz(tmp)&1){
        cout<<":(";return 0;
    }
    if(counta==0){
        REP(i,0,sz(tmp)/2){
            cout<<s[i];
        }
        return 0;
    }
    if(tmp==""&&ss==""){
        cout<<s;
        return 0;
    }

    int cur1 = 0 , cur2 = sz(tmp) / 2, fl=1;
    REP(i,0,sz(tmp)/2){
        if( tmp[cur1] != tmp[cur2] ){
            fl=0;
            break;
        }
        cur1++;
        cur2++;
    }
    if(fl==0){
        cout<<":(";
    }

    else {
        if(ss=="")cout<<":(";
        else{
            ll cc = 0;
            REP(i,0,sz(s)){
                if(s[i]!='a')cc++;
                cout<<s[i];
                if(cc==cur1)break;

            }
        }
    }
}


/*BINH :))*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long llu;
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define f  first
#define s  second
#define mp make_pair
#define pb push_back
#define ep emplace_back
#define sz(a) (int) a.size()
#define FOR(x,a,b) for (int x=a;x<=b;x++)
#define FOD(x,a,b) for (int x=a;x>=b;x--)
#define REP(x,a,b) for (int x=a;x<b;x++)
#define RED(x,a,b) for (int x=a;x>b;x--)
#define EL cout<<endl
#define max 1005
#define vi vector<int>
#define ii pair<int,int>
#define vii vector<ii>
void init(){
}
int main(){
    IO
    init();
    string s, ss,tmp;
    cin>>s;
    FOD(i,sz(s)-1,0){
        if(s[i]=='a')break;
        ss = s[i] + ss;
    }
    //cout<<ss;
    REP(i,0,sz(s)){
        if(s[i]!='a')tmp=tmp+s[i];
    }
    if(tmp==""&&ss==""){
        cout<<s;
        return 0;
    }
    if(sz(tmp)&1){
        cout<<":(";return 0;
    }
    int cur1 = 0 , cur2 = sz(tmp) / 2, fl=1;
    REP(i,0,sz(tmp)/2){
        if( tmp[cur1] != tmp[cur2] ){
            fl=0;
            break;
        }
        cur1++;
        cur2++;
    }
    if(fl==0){
        cout<<":(";
    }

    else {
        if(ss=="")cout<<":(";
        else{
            ll cc = 0;
            REP(i,0,sz(s)){
                if(s[i]!='a')cc++;
                cout<<s[i];
                if(cc==cur1)break;

            }
        }
    }
}


/*BINH :))*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long llu;
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define f  first
#define s  second
#define mp make_pair
#define pb push_back
#define ep emplace_back
#define sz(a) (int) a.size()
#define FOR(x,a,b) for (int x=a;x<=b;x++)
#define FOD(x,a,b) for (int x=a;x>=b;x--)
#define REP(x,a,b) for (int x=a;x<b;x++)
#define RED(x,a,b) for (int x=a;x>b;x--)
#define EL cout<<endl
#define max 1005
#define vi vector<int>
#define ii pair<int,int>
#define vii vector<ii>
void init(){
}
int counta;
string s, ss,tmp;
int main(){
    IO
    init();

    cin>>s;
    //cout<<ss;
    REP(i,0,sz(s)){
        if(s[i]!='a')tmp=tmp+s[i];
        else counta++;
    }
    if(sz(tmp)&1){
        cout<<":(";return 0;
    }
    if(counta==0){
        REP(i,0,sz(tmp)/2){
            cout<<s[i];
        }
        return 0;
    }
    if(tmp==""&&ss==""){
        cout<<s;
        return 0;
    }
    int cur1 = 0 , cur2 = sz(tmp) / 2, fl=1 , n = tmp.size();
    REP(i,0,sz(tmp)/2){
        if( tmp[cur1] != tmp[cur2] ){
            fl=0;
            break;
        }
        cur1++;
        cur2++;
    }
    if(fl==0){
        cout<<":(";
    }

    else {
        if(s.substr(s.size()-n/2)==tmp.substr(0, n/2))
        cout<<s.substr(0, s.size()-(n/2));
        else
        cout<<":(";
    }

}


/*BINH :))*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long llu;
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define f  first
#define s  second
#define mp make_pair
#define pb push_back
#define ep emplace_back
#define sz(a) (int) a.size()
#define FOR(x,a,b) for (int x=a;x<=b;x++)
#define FOD(x,a,b) for (int x=a;x>=b;x--)
#define REP(x,a,b) for (int x=a;x<b;x++)
#define RED(x,a,b) for (int x=a;x>b;x--)
#define EL cout<<endl
#define max 1005
#define vi vector<int>
#define ii pair<int,int>
#define vii vector<ii>
void init(){
}
int counta;
string s, ss,tmp;
int main(){
    IO
    init();

    cin>>s;
    //cout<<ss;
    REP(i,0,sz(s)){
        if(s[i]!='a')tmp=tmp+s[i];
        else counta++;
    }
    if(sz(s)>100){cout<<sz(tmp);return 0;}
    if(sz(tmp)&1){
        cout<<":(";return 0;
    }
    if(counta==0){
        REP(i,0,sz(s)/2){
            cout<<s[i];
        }
        return 0;
    }
    if(tmp==""&&ss==""){
        cout<<s;
        return 0;
    }
    int cur1 = 0 , cur2 = sz(tmp) / 2, fl=1 , n = tmp.size();
    REP(i,0,sz(tmp)/2){
        if( tmp[cur1] != tmp[cur2] ){
            fl=0;
            break;
        }
        cur1++;
        cur2++;
    }
    if(fl==0){
        cout<<":(";
    }

    else {
        if(s.substr(s.size()-n/2)==tmp.substr(0, n/2))
        cout<<s.substr(0, s.size()-(n/2));
        else
        cout<<":(";
    }

}


/*BINH :))*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long llu;
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define f  first
#define s  second
#define mp make_pair
#define pb push_back
#define ep emplace_back
#define sz(a) (int) a.size()
#define FOR(x,a,b) for (int x=a;x<=b;x++)
#define FOD(x,a,b) for (int x=a;x>=b;x--)
#define REP(x,a,b) for (int x=a;x<b;x++)
#define RED(x,a,b) for (int x=a;x>b;x--)
#define EL cout<<endl
#define max 1005
#define vi vector<int>
#define ii pair<int,int>
#define vii vector<ii>
void init(){
}
int main(){
    IO
    init();
    string s, ss,tmp;
    cin>>s;
    FOD(i,sz(s)-1,0){
        if(s[i]=='a')break;
        ss = s[i] + ss;
    }
    //cout<<ss;
    REP(i,0,sz(s)){
        if(s[i]!='a')tmp=tmp+s[i];
    }
    if(tmp==""&&ss==""){
        cout<<s;
        return 0;
    }
    int cur1 = 0 , cur2 = sz(tmp) / 2, fl=1;
    REP(i,0,sz(tmp)/2){
        if( tmp[cur1] != tmp[cur2] ){
            fl=0;
            break;
        }
        cur1++;
        cur2++;
    }
    if(fl==0){
        cout<<":(";
    }
    else {
        if(ss=="")cout<<":(";
        else{
            ll cc = 0;
            REP(i,0,sz(s)){
                if(s[i]!='a')cc++;
                cout<<s[i];
                if(cc==cur1)break;

            }
        }
    }
}


/*BINH :))*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long llu;
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define f  first
#define s  second
#define mp make_pair
#define pb push_back
#define ep emplace_back
#define sz(a) (int) a.size()
#define FOR(x,a,b) for (int x=a;x<=b;x++)
#define FOD(x,a,b) for (int x=a;x>=b;x--)
#define REP(x,a,b) for (int x=a;x<b;x++)
#define RED(x,a,b) for (int x=a;x>b;x--)
#define EL cout<<endl
#define max 1005
#define vi vector<int>
#define ii pair<int,int>
#define vii vector<ii>
void init(){
}
int counta;
string s, ss,tmp;
int main(){
    IO
    init();

    cin>>s;
    string tmp="";

    for(int i=0; i<s.size(); i++) {
        if(s[i]=='a') {

        } else {
            tmp+=s[i];
        }
    }

    if(tmp.size()%2!=0) {
        cout<<":(";
        return 0;
    }

    int n=tmp.size();

    int flag=0;

    for(int i=0; i<n/2; i++) {
        if(tmp[i]==tmp[i+(n/2)]) {
            continue;
        } else {
            flag=1;
            cout<<":(";
            break;
        }
    }

    if(flag==0) {
        if(s.substr(s.size()-n/2)==tmp.substr(0, n/2))
        cout<<s.substr(0, s.size()-(n/2));
        else
        cout<<":(";
    }

}