#include #pragma comment(linker, "/STACK:102400000,102400000") #define LL long long #define pii pair #define xx first #define yy second using namespace std; const int N = 110000, M = 1001000; int t[N], head[M], sz[M], tot, a[N], v[M], p[M]; //int fa[N]; int f (int x) { return x & -x; } void add (int x, int c) { for (int i = x; i < N; i += f (i)) t[i] += c; } struct color { int nx, p; color () {} color (int nx, int p) : nx (nx), p (p) {} } c[N * 50]; void addc (int x, int y) { // cout << x << ' ' << y << endl; c[tot] = color (head[x], y); head[x] = tot++; } int qry (int x) { int r = 0; for (int i = x; i; i -= f (i)) r += t[i]; return r; } //int myfind (int x) { // if (fa[x] == 0) return x; // return fa[x] = myfind (fa[x]); //} int main () { // freopen ("in.txt", "r", stdin); int T; cin >> T; while (T--) { int n, q; cin >> n >> q; memset (t, 0, sizeof t); memset (head, -1, sizeof head); memset (sz, 0, sizeof sz); // memset (fa, 0, sizeof fa); memset (v, 0, sizeof v); for (int i = 1; i < M; i++) p[i] = i; tot = 0; for (int i = 1; i <= n; i++){ scanf ("%d", &a[i]); v[a[i]] = 1; addc (a[i], i); sz[a[i]]++; if (i > 1 && a[i] != a[i - 1]) { add (i - 1, 1); } } int t, x, y; while (q--) { scanf ("%d%d%d", &t, &x, &y); if (t == 1) { if (x == y) continue; if (v[x] == 0) continue; v[x] = 0; v[y] = 1; int tx = p[x], ty = p[y]; // cout << x << ' ' << y << endl; // if (tx == ty) continue; if (sz[tx] > sz[ty]) { swap (p[x], p[y]); swap (tx, ty); // swap (x, y); } // cout << x << ' ' << y << ' ' << tx << ' ' << ty << endl; // if (sz[tx] == 0) continue; // int i = c[head[ty]]; for (int i = head[tx]; ~i; i = c[i].nx) { int p = c[i].p; if (p > 1 && a[p - 1] != a[p]) add (p - 1, -1); if (p < n && a[p] != a[p + 1]) add (p, -1); addc (ty, p); } // p[x] = p[y]; // fa[x] = y; sz[ty] += sz[tx]; sz[tx] = 0; for (int i = head[tx]; ~i; i = c[i].nx) { int p = c[i].p; a[p] = ty; } for (int i = head[tx]; ~i; i = c[i].nx) { int p = c[i].p; // a[p] = ty; if (p > 1 && a[p - 1] != a[p]) add (p - 1, 1); if (p < n && a[p] != a[p + 1]) add (p, 1); // addc (ty, c[i].p); } head[tx] = -1; // for (int i = 1; i <= n; i++) cout << a[i] << ' '; cout << endl; } else { printf ("%d\n", qry (y - 1) - qry (x - 1) + 1); } } } }