import std.conv, std.stdio;
import std.algorithm, std.array, std.range, std.string;
void main()
{
    readln;
    auto a = Solver(readln.chomp.split.to!(int[]));
    immutable q = readln.chomp.to!size_t;
    foreach (_; 0..q)
    {
        auto buf = readln.chomp.split.to!(int[]);
        a.query(buf[0], buf[1]).writeln;
    }
}

private:

struct Solver
{
    this (int[] _a)
    {
        this.a = new long[10001];
        foreach (e; _a)
        {
            this.a[e] += 1;
            this.s += e;
        }
    }
    long query(in int b, in int c)
    {
        a[c] += a[b];
        s += (c - b) * a[b];
        a[b] = 0;
        return s;
    }
    invariant
    {
        assert (s == _s);
    }
    long[] a;
    long s;
    private long _s() @property const
    {
        long ret;
        foreach (i, e; a)
            ret += i * e;
        return ret;
    }
}
