require 'set'

class UnionFind
	def initialize
		@obj_to_node = {}
	end

	class Node
		def initialize(obj)
			@obj = obj
			@parent = self
			@size = 1
			@rank = 0
		end

		def root
			return self if @parent == self
			@parent = @parent.root
		end

		attr_accessor :parent, :size, :rank
	end

	def union(x, y)
		u = (@obj_to_node[x] ||= Node.new(x)).root
		v = (@obj_to_node[y] ||= Node.new(y)).root
		return if u == v
		if u.rank > v.rank
			v.parent = u
			u.size += v.size
		elsif v.rank > u.rank
			u.parent = v
			v.size += u.size
		else
			u.parent = v
			v.rank += 1
			v.size += u.size
		end
	end

    def groups
        @obj_to_node.keys.group_by {|x| @obj_to_node[x].root}.values
    end
end

n = gets.to_i
contribution = [0] + gets.split.map(&:to_i)
uf = UnionFind.new

g = {}
g_inv = {}
(1..n).map {|i| g[i] = Set.new; g_inv[i] = Set.new}

m = gets.to_i
m.times do
    u, v = gets.split.map(&:to_i)
    if contribution[u] < contribution[v]
        g[v] << u
        g_inv[u] << v
    elsif contribution[u] > contribution[v]
        g[u] << v
        g_inv[v] << u
    else
        uf.union(u, v)
    end
end

(1..n).each do |c|
    (g[c] + g_inv[c]).sort_by{|v| contribution[v]}.each_cons(2) do |u, v|
        if contribution[u] == contribution[v]
            uf.union(u, v)
        else
            g[v] << u
            g_inv[u] << v
        end
    end
end

scc_size = Hash.new(1)

uf.groups.each do |scc|
    next if scc.size == 1
    rep = scc[0]
    scc_size[rep] = scc.size

    scc[1..-1].each do |u|
        g[rep] += g[u]
        g_inv[rep] += g_inv[u]

        g_inv[u].each do |v|
            g[v].delete(u)
            g[v] << rep
        end

        g[u].each do |v|
            g_inv[v].delete(u)
            g_inv[v] << rep
        end

        g.delete(u)
        g_inv.delete(u)
    end
end

salary = {}
out_deg = {}
queue = []
g.each do |u, s|
    queue << u if s.empty?
    out_deg[u] = s.size
end

until queue.empty?
    u = queue.shift
    salary[u] = (g[u].map {|v| salary[v]}.max || 0) + 1
    g_inv[u].each do |v|
        out_deg[v] -= 1
        queue << v if out_deg[v] == 0
    end
end

p g.keys.map {|v| salary[v] * scc_size[v]}.inject(:+)