#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<bitset>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define fuck(x) cout<<"["<<x<<"]"
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
const int MX = 4e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int Head[MX], erear;
struct Edge {
int v, nxt;
} E[MX * 2];
void edge_init() {
erear = 0;
memset(Head, -1, sizeof(Head));
}
void edge_add(int u, int v) {
E[erear].v = v;
E[erear].nxt = Head[u];
Head[u] = erear++;
}
int ans[MX];
int Max1[MX], Max2[MX], nam[MX];
struct Data {
int id, x;
bool operator<(const Data &P) const {
return x < P.x;
}
} A[MX];
int main() {
int n, m; //FIN;
while(~scanf("%d", &n)) {
edge_init();
memset(Max1, 0, sizeof(Max1));
memset(Max2, 0, sizeof(Max2));
memset(nam, 0, sizeof(nam));
for(int i = 1; i <= n; i++) {
A[i].id = i;
scanf("%d", &A[i].x);
}
sort(A + 1, A + 1 + n);
scanf("%d", &m);
for(int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
edge_add(u, v);
edge_add(v, u);
}
for(int i = 1; i <= n; i++) {
Data tp = A[i];
if(tp.x == nam[tp.id]) ans[tp.id] = Max2[tp.id] + 1;
else ans[tp.id] = Max1[tp.id] + 1;
for(int i = Head[tp.id]; ~i; i = E[i].nxt) {
int v = E[i].v;
if(tp.x == nam[v]) ans[tp.id] = max(ans[tp.id], Max2[v] + 1);
else ans[tp.id] = max(ans[tp.id], Max1[v] + 1);
}
for(int i = Head[tp.id]; ~i; i = E[i].nxt) {
int v = E[i].v;
if(ans[tp.id] > Max1[v]) {
Max2[v] = Max1[v];
Max1[v] = ans[tp.id];
nam[v] = tp.x;
} else if(ans[tp.id] > Max2[v]) {
Max2[v] = ans[tp.id];
}
}
}
LL res = 0;
for(int i = 1; i <= n; i++) {
res += ans[i];
}
printf("%lld\n", res);
}
return 0;
}