// Created on: 2025-01-02 22:53
// Author: Safwan_Ibrahim
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
void solve() {
int n, q; cin >> n >> q;
int a[n + 1];
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
set<int>ac;
for (int i = 1; i <= q; i++) {
int x, y; cin >> x >> y;
ac.insert(x); ac.insert(y);
}
set<int>el;
for (auto x : ac) {
el.insert(a[x]);
}
map<int, int>mp;
for (auto it = ac.begin(), it2 = el.begin(); it != ac.end(); it++, it2++) {
mp[*it] = *it2;
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
if (mp.find(i) != mp.end()) {
if (mp[i] == i) {
ans++;
}
}
else {
if (a[i] == i) {
ans++;
}
}
}
cout << ans << endl;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1; cin >> t;
while(t--) solve();
return 0;
}