← Home
package main

import (
	"bufio"
	"fmt"
	"os"
)

type FastScanner struct {
	r *bufio.Reader
}

func NewFastScanner() *FastScanner {
	return &FastScanner{r: bufio.NewReaderSize(os.Stdin, 1<<20)}
}

func (fs *FastScanner) NextInt64() int64 {
	var sign int64 = 1
	var val int64
	c, _ := fs.r.ReadByte()
	for (c < '0' || c > '9') && c != '-' {
		c, _ = fs.r.ReadByte()
	}
	if c == '-' {
		sign = -1
		c, _ = fs.r.ReadByte()
	}
	for c >= '0' && c <= '9' {
		val = val*10 + int64(c-'0')
		c, _ = fs.r.ReadByte()
	}
	fs.r.UnreadByte()
	return val * sign
}

func main() {
	in := NewFastScanner()
	out := bufio.NewWriterSize(os.Stdout, 1<<20)
	defer out.Flush()

	n := int(in.NextInt64())
	mp := make(map[int64]int64, n)
	var total int64

	for i := 0; i < n; i++ {
		a := in.NextInt64()
		x := in.NextInt64()
		mp[a] = x
		total += x
	}

	m := int(in.NextInt64())
	for i := 0; i < m; i++ {
		b := in.NextInt64()
		y := in.NextInt64()
		if x, ok := mp[b]; ok {
			if y > x {
				total += y - x
			}
		} else {
			total += y
		}
	}

	fmt.Fprint(out, total)
}