#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
class disjointset {
public:
vector<ll> parent, size;
disjointset(ll n) {
parent.resize(n + 1);
size.resize(n + 1);
for (ll i = 1; i <= n; i++) {
parent[i] = i;
size[i] = 1;
}
}
ll findparent(ll node) {
if (node == parent[node]) {
return node;
}
return parent[node] = findparent(parent[node]);
}
void join(ll u, ll v) {
ll up = findparent(u), vp = findparent(v);
if (up == vp) return;
if (size[up] <= size[vp]) {
parent[up] = vp;
size[vp] += size[up];
} else {
parent[vp] = up;
size[up] += size[vp];
}
}
};
void solve() {
ll n, q,cnt=0;cin >> n >> q;
vector<ll> v(n + 1);
for (ll i = 1; i <= n; i++) {
cin >> v[i];
}
disjointset dsu(n);
for (ll x,y,i = 0; i < q; i++) {
cin >> x >> y;
dsu.join(x, y);
}
unordered_map<ll, vector<ll>> adj;
for (ll i = 1; i <= n; i++) {
adj[dsu.findparent(i)].push_back(i);
}
for (auto &u : adj) {
vector<ll> pos = u.second;
vector<ll> p;
for (int i=0;i<pos.size();i++) {
p.push_back(v[pos[i]]);
}
sort(pos.begin(), pos.end());
sort(p.begin(), p.end());
for (int i = 0; i < pos.size(); i++) {
if (p[i] == pos[i]) {
cnt++;
}
}
}
cout << cnt << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ll t=1;cin >> t;
while (t--) {
solve();
}
return 0;
}