#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

class UnionFind {
	vector<int>parent;
	vector<int>rank;
public:
	UnionFind(int num) {
		num++;
		parent.resize(num);
		rank.resize(num);
		for (int i = 0; i < num; i++) {
			parent[i] = i;
			rank[i] = 0;
		}
	}
	int Find(int node) {
		if (parent[node] == node)return node;
		else return parent[node] = Find(parent[node]);
	}
	void Unite(int u, int v) {
		u = Find(u);
		v = Find(v);
		if (u == v)return;
		if (rank[u] < rank[v])parent[u] = v;
		else {
			parent[v] = u;
			if (rank[u] == rank[v])rank[u]++;
		}
	}
	bool Check_Same(int u, int v) {
		return Find(u) == Find(v);
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N;
	vector<int>v(N);
	for (auto &i : v)cin >> i;
	cin >> M;
	vector<int>ans(N, 1);
	vector<int>w(N);
	vector<int>y(N);
	set<pair<int, int>>st;
	vector<vector<pair<int, int>>>edge(N);
	UnionFind uf(N);
	for (int i = 0; i < M; i++) {
		cin >> L >> R;
		L--, R--;
		edge[L].push_back({ v[R],R });
		edge[R].push_back({ v[L],L });
		if (v[L] == v[R])uf.Unite(L, R);
	}
	vector<int>p(N);
	for (int i = 0; i < N; i++) {
		sort(edge[i].begin(), edge[i].end());
		for (int j = 1; j < edge[i].size(); j++) {
			if (edge[i][j].first == edge[i][j - 1].first) {
				uf.Unite(edge[i][j].second, edge[i][j - 1].second);
			}
		}
	}
	for (int i = 0; i < N; i++) {
		p[i] = uf.Find(i);
	}
	vector<vector<int>>nedge(N);
	vector<int>in(N);
	for (int i = 0; i < N; i++) {
		for (int j = 1; j < edge[i].size(); j++) {
			if (edge[i][j].first > edge[i][j - 1].first) {
				nedge[p[edge[i][j - 1].second]].push_back(p[edge[i][j].second]);
				in[p[edge[i][j].second]]++;
			}
		}
	}
	for (int i = 0; i < N; i++) {
		for (auto j : edge[i]) {
			if (v[i] < j.first){
				nedge[p[i]].push_back(p[j.second]);
				in[p[j.second]]++;
			}
		}
	}
	queue<int>Q;
	for (int i = 0; i < N; i++) {
		if (!in[i])Q.push(i);
	}
	while(!Q.empty()){
		int cn = Q.front();
		Q.pop();
		for (auto j : nedge[cn]) {
			in[j]--;
			if (!in[j])Q.push(j);
			ans[j] = max(ans[j], ans[cn] + 1);
		}
	}
	long long int ret = 0;
	for (int i = 0; i < N; i++) {
		ret += ans[p[i]];
	}
	cout << ret << endl;
}
