#include <bits/stdc++.h>
using namespace std;
void Print() { cout << endl; }
template <typename T1, typename... T>
void Print(const T1 &t1, const T &...t) {
  cout << t1 << " ";
  Print(t...);
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
long long gcd(long long a, long long b) {
  if (b) {
    long long r = a % b;
    a = b, b = r;
  }
  return a;
}
tuple<long long, long long, long long> extend_gcd(long long a, long long b) {
  if (b == 0)
    return make_tuple(a, 1, 0);
  else {
    long long g, nx, ny;
    std::tie(g, nx, ny) = extend_gcd(b, a % b);
    return make_tuple(g, ny, nx - (a / b) * ny);
  }
}
long long fastpow(long long a, long long e, long long mod) {
  if (e == 0) return 1;
  if (e == 1) return a % mod;
  long long t = fastpow(a, e / 2, mod);
  t = t * t % mod;
  if (e & 1)
    return t * a % mod;
  else
    return t % mod;
}
const double PI = 3.14159265358979323846;
const int MOD = (int)1e9 + 7;
const int N = 1000005;
int p[N], ans[N];
bool vis[N];
vector<int> edges[N];
priority_queue<int> vals;
priority_queue<int, std::vector<int>, std::greater<int> > idx;
void dfs(int v, int r) {
  vis[v] = true;
  idx.push(v);
  vals.push(p[v]);
  for (auto to : edges[v]) {
    if (!vis[to]) dfs(to, r);
  }
  if (v == r) {
    while (!idx.empty()) {
      int tidx = idx.top();
      idx.pop();
      int tval = vals.top();
      vals.pop();
      ans[tidx] = tval;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> p[i];
  }
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    if (a == b) continue;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) dfs(i, i);
  }
  for (int i = 1; i <= n; ++i) cout << ans[i] << ' ';
  cout << endl;
  return 0;
}
