#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
namespace SOLVE {
void main() {
  int n;
  cin >> n;
  vector<int> A(n);
  deque<int> dq;
  for (long long i = 0; i < n; i++) {
    cin >> A[i];
    if (dq.size() == 0) {
      dq.push_front(A[i]);
    } else if (dq.size() == 1) {
      if (A[i] >= dq.size()) {
        dq.push_front(A[i]);
      } else {
        dq.push_back(A[i]);
      }
    } else if (A[i] >= dq.front()) {
      dq.push_front(A[i]);
      dq.pop_back();
    } else if (A[i] >= dq.back()) {
      dq.pop_back();
      dq.push_back(A[i]);
    }
  }
  for (long long i = 0; i < n; i++) {
    if (dq.size() == 1) {
      cout << dq.front() << endl;
    } else {
      if (A[i] == dq.front()) {
        cout << dq.back() << endl;
      } else {
        cout << dq.front() << endl;
      }
    }
  }
}
}  // namespace SOLVE
signed main() {
  int t;
  t = 1;
  while (t--) {
    SOLVE::main();
  }
  return 0;
}
