#include <bits/stdc++.h>
using namespace std;
//template
#pragma region
const int MOD = 1e9 + 7;
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
template<class T> using ordset = tree<
T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;
struct mi {
int v;
explicit operator int() const { return v; }
mi() { v = 0; }
mi(long long _v) : v(_v % MOD) { v += (v < 0) * MOD; }
};
mi &operator+=(mi &a, mi b) {
if ((a.v += b.v) >= MOD) a.v -= MOD;
return a;
}
mi &operator-=(mi &a, mi b) {
if ((a.v -= b.v) < 0) a.v += MOD;
return a;
}
mi operator+(mi a, mi b) { return a += b; }
mi operator-(mi a, mi b) { return a -= b; }
mi operator*(mi a, mi b) { return mi((long long)a.v * b.v); }
mi &operator*=(mi &a, mi b) { return a = a * b; }
mi mypow(mi a, long long p) {
assert(p >= 0);
return p == 0 ? 1 : mypow(a * a, p / 2) * (p & 1 ? a : 1);
}
mi inv(mi a) {
assert(a.v != 0);
return mypow(a, MOD - 2);
}
mi operator/(mi a, mi b) { return a * inv(b); }
#define all(x) x.begin(), x.end()
#define lef(x) (x << 1)
#define rig(x) (lef(x) | 1)
#define ft first
#define sd second
#define mt make_tuple
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define dbg(x) cout << #x << " = " << x << '\n';
#define rep(i, begin, end) for (__typeof(begin) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<int, int> pii;
typedef pair<ld, ld> pdb;
// typedef __int128_t lll;
template<typename T, typename T1>
ostream & operator<<(ostream &os, pair<T, T1> p){
os << "(" << p.ft << "," << p.sd << ")";
return os;
}
template<typename T, typename T1>
istream & operator>>(istream &is, pair<T, T1>& p){
is >> p.ft >> p.sd;
return is;
}
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#pragma endregion
const int N = 1e5 + 30;
ll n, m, k;
ll x[N];
string s;
vector<int> adj[N];
set<int> st;
char vis[N];
void dfs(int u){
vis[u] = 1;
st.insert(u);
for(auto v: adj[u]){
if(!vis[v]) dfs(v);
}
}
void test() {
cin >> n >> m;
rep(i,1,n + 1) cin >> x[i], adj[i].clear(), vis[i] = 0;
rep(i,0,m){
int a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
ll ans = 0;
set<int> pt;
rep(i,1,n+1){
if(!vis[i]){
st.clear();
pt.clear();
dfs(i);
for(auto j: st) pt.insert(x[j]);
for(auto j: st){
if(pt.find(j) != pt.end()) ans++;
}
}
}
cout << ans << '\n';
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int te = 1;
cin >> te;
while(te--) test();
return 0;
}