-module('Main').
-export([main/1]).

main(_) ->
    {ok, [N]} = io:fread("", "~d"),
    L = read_n_data(N),
    {ok, [Q]} = io:fread("", "~d"),
    LL = read_2n_data(Q),
    lists:foldl(fun({E1, E2}, Acc) ->
                        NewAcc = lists:map(fun(X) ->
                                                case X =:= E1 of
                                                    true ->
                                                        E2;
                                                    false ->
                                                        X
                                                end
                                        end,
                                        Acc),
                        Ans = lists:sum(NewAcc),
                        io:format("~p~n", [Ans]),
                        NewAcc
                end,
                L,
                LL),
    halt().


%% N個データ読み込み
read_n_data(N) -> read_n_data(N, []).
read_n_data(0, Acc) -> Acc;
read_n_data(N, Acc) -> 
    {ok, [NewElement]} = io:fread("", "~d"),
    read_n_data(N - 1, [NewElement|Acc]).

%% ２個づつN個データ読み込み
read_2n_data(N) -> read_n_data(N, []).
read_2n_data(0, Acc) -> Acc;
read_2n_data(N, Acc) -> 
    {ok, [E1, E2]} = io:fread("", "~d ~d"),
    read_n_data(N - 1, [{E1, E2}|Acc]).
