#include <bits/stdc++.h>
using namespace std;
const unsigned long long int INF = 1001001001;
const double PI = 3.141592653;
const unsigned long long int sosu = 1000000007;
int z(int a, int b) {
  if (a > b)
    return a - b;
  else
    return b - a;
}
const int MX = 100005;
int main() {
  unsigned long long int n;
  cin >> n;
  unsigned long long int q;
  cin >> q;
  vector<unsigned long long int> vec(MX);
  for (unsigned long long int i = 0; i < n; i++) {
    int a;
    cin >> a;
    vec[a]++;
  }
  unsigned long long int tot = 0;
  for (unsigned long long int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    tot -= b * vec[b];
    tot -= c * vec[c];
    vec[c] += vec[b];
    vec[b] = 0;
    tot += b * vec[b];
    tot += c * vec[c];
    cout << tot << endl;
  }
  return 0;
}
