object Main {
  def main(args: Array[String]): Unit = {
    val number_of_piece = io.StdIn.readInt
    val numbers         = io.StdIn.readLine.split( " " ).map{_.toInt}
    val operation_num   = io.StdIn.readInt

    var operations: Vector[ ( Int, Int ) ] = Vector.empty
    var nums_map: Map[ Int, Int ]          = Map.empty

    ( 0 until operation_num ).foreach { i =>
      val operation = io.StdIn.readLine.split( " " ).map{_.toInt}
      operations = operations :+ ( operation( 0 ), operation( 1 ) )
    }

    numbers.foreach { i =>
      if( nums_map.contains( i ) ) nums_map += i -> ( nums_map( i ) + 1 )
      else                         nums_map += i -> 1
    }

    var sum = -1.toLong

    operations.foreach { i =>
      var changing_num_of_piece = 0
      if( nums_map.contains( i._1 ) ) {
        changing_num_of_piece = nums_map( i._1 )
        if( nums_map.contains( i._2 ) ) nums_map += i._2 -> ( nums_map( i._1 ) + nums_map( i._2 ) )
        else                            nums_map += i._2 -> nums_map( i._1 )
        nums_map += i._1 -> 0
      }

      if( sum < 0 ) sum = calcSumOfNumsMap( nums_map )
      else          sum += ( ( i._2 - i._1 )*changing_num_of_piece ).toLong
      println( sum )
    }
  }

  def calcSumOfNumsMap( nums_map: Map[ Int, Int ] ): Long = {
    var sum = 0.toLong

    nums_map.foreach{ i =>
      sum += ( i._1 * i._2 ).toLong
    }

    return sum
  }
}