#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
struct DSU {
vector<int> par, rank, sz;
int c;
DSU(int n) : par(n + 1), rank(n + 1, 0), sz(n + 1, 1), c(n) {
for (int i = 1; i <= n; i++) par[i] = i;
}
int find(int v) {
if (par[v] == v) return v;
else return par[v] = find(par[v]);
}
bool same(int a, int b) {
return find(a) == find(b);
}
int get_size(int v) {
return sz[find(v)];
}
int count() {
return c;
}
void merge(int a, int b) {
a = find(a), b = find(b);
if (a == b) return;
c--;
if (rank[a] > rank[b]) swap(a, b);
par[a] = b;
sz[b] += sz[a];
if (rank[a] == rank[b]) rank[b]++;
}
};
int main(){
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--){
int n, q, s = 0;
cin >> n >> q;
vector<int> a(n+1),vis(n+1);
for (int i = 1; i <= n; i++){
cin >> a[i];
if (i == a[i]) vis[i] = 1;
}
DSU dsu(n + 1);
while (q--){
int x, y;
cin >> x >> y;
if (vis[x]||vis[y]) continue;
if (a[x]==y||a[y]==x){
vis[x]=vis[y]=1;
}
}
int mx = 0;
for (int i = 1; i <= n; i++){
s += vis[i];
}
cout << s<<endl;
}
}