fun main(args:Array<String>?): Unit {
    val n = readLine()!!.trim().toInt()
    val materials = Array(n){
        val (a, x) = readLine()!!.trim().split(' ')
        Material(a, x.toInt())
    }.sortedBy(Material::name)
    val unionFind = UnionFind(n)
    val m = readLine()!!.trim().toInt()
    repeat(m){
        val (s, t) = readLine()!!.trim().split(' ')
        val sId = materials.binarySearchBy(s){it.name}
        val tId = materials.binarySearchBy(t){it.name}
        unionFind.merge(sId, tId)
    }
    val minCost = IntArray(n){Int.MAX_VALUE}
    for (i in 0 until unionFind.size){
        val root = unionFind.find(i)
        if (minCost[root] > materials[i].price) {
            minCost[root] = materials[i].price
        }
    }
    println((0 until unionFind.size).map{i -> minCost[unionFind.find(i)]}.sum())
}
class Material(val name: String, val price: Int)
class UnionFind(val size: Int){
    private val mArray: IntArray = IntArray(size){-1}
    fun find(a: Int): Int {
        return if (mArray[a] < 0) a
        else {
            mArray[a] = find(mArray[a])
            mArray[a]
        }
    }
    fun isSame(u: Int, v: Int): Boolean = find(u) == find(v)
    fun merge(u: Int, v: Int): Unit {
        val ur = find(u)
        val vr = find(v)
        if (ur != vr){
            if (mArray[ur] < mArray[vr]) {
                mArray[ur] += mArray[vr]
                mArray[vr] = ur
            }else {
                mArray[vr] += mArray[ur]
                mArray[ur] = vr
            }
        }
    }
}
