#include <bits/stdc++.h>
using namespace std;
template <typename T, size_t size>
ostream &operator<<(ostream &os, const array<T, size> &arr) {
  os << '{';
  string sep;
  for (const auto &x : arr) os << sep << x, sep = ", ";
  return os << '}';
}
template <class T>
ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V) os << v << " ";
  return os << "]";
}
template <class T>
ostream &operator<<(ostream &os, set<T> S) {
  os << "{ ";
  for (auto s : S) os << s << " ";
  return os << "}";
}
template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R>
ostream &operator<<(ostream &os, map<L, R> M) {
  os << "{ ";
  for (auto m : M) os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}
const int MAX = 1000010;
const int MOD2 = 998244353;
const int MOD1 = 1000000007;
const int INF = 0x3f;
const long long int LLINF = 0x3f3f3f3f3f3f3f3fll;
const long double EPS = 1e-7;
struct UF {
  vector<int> e;
  UF(int n) : e(n, -1) {}
  bool same_set(int a, int b) { return find(a) == find(b); }
  int size(int x) { return -e[find(x)]; }
  int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
  bool join(int a, int b) {
    a = find(a);
    b = find(b);
    if (a == b) return false;
    if (e[a] > e[b]) swap(a, b);
    e[a] += e[b];
    e[b] = a;
    return true;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed << setprecision(25);
  cerr << fixed << setprecision(10);
  int n, m;
  cin >> n >> m;
  vector<int> p(n + 1);
  for (int i = 1; i <= n; i++) cin >> p[i];
  UF uf(n + 1);
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    uf.join(x, y);
  }
  map<int, set<int>> setnum_pos;
  map<int, set<int>> setnum_val;
  for (int i = 1; i <= n; i++) {
    setnum_pos[uf.find(i)].insert(-i);
    setnum_val[uf.find(i)].insert(p[i]);
  }
  vector<int> ans(n + 1);
  for (auto [setnum, pos_set] : setnum_pos) {
    for (auto idx : pos_set) {
      ans[-idx] = *setnum_val[setnum].begin();
      setnum_val[setnum].erase(ans[-idx]);
    }
  }
  for (int i = 1; i <= n; i++) cout << ans[i] << " ";
  cout << "\n";
  return 0;
}
