import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip, std.datetime;

void main() {
    long[long] s;

    auto N = readln.chomp.to!int;
    foreach (_; 0..N) {
        auto t = readln.split.map!(to!long);
        auto a = t[0];
        auto x = t[1];
        if (a in s) s[a] = max(s[a], x);
        else s[a] = x;
    }

    auto M = readln.chomp.to!int;
    foreach (_; 0..M) {
        auto t = readln.split.map!(to!long);
        auto a = t[0];
        auto x = t[1];
        if (a in s) s[a] = max(s[a], x);
        else s[a] = x;
    }

    s.values.sum.writeln;
}
