#include <bits/stdc++.h>
using namespace std;
long long Q, N, T;
long long freq[100001];
void solve() {
  memset(freq, 0, sizeof(freq));
  cin >> N;
  long long temp;
  long long sum = 0;
  long long b1, c1;
  for (long long i = 0; i < N; i++) {
    cin >> temp;
    sum += temp;
    freq[temp]++;
  }
  cin >> Q;
  long long op[Q];
  for (long long i = 0; i < Q; i++) {
    cin >> b1 >> c1;
    sum = sum - (b1 * freq[b1]) + (c1 * freq[b1]);
    freq[c1] += freq[b1];
    freq[b1] = 0;
    op[i] = sum;
  }
  for (long long i = 0; i < Q; i++) cout << op[i] << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> T;
  while (T--) solve();
  return 0;
}
