#include <bits/stdc++.h>
using LL = long long;
using namespace std;
constexpr int N = 3E5 + 5 , M = 6E8;
int cnt , rt[N] , ls[M + 5] , rs[M + 5] , sum[M + 5];
void pushup(int u) {
sum[u] = sum[ls[u]] + sum[rs[u]];
}
void update(int &u,int l,int r,int pos,int v) {
if (u == 0) u = ++cnt;
if (l == r) {
sum[u] += v;
return;
}
int md = (l + r) >> 1;
if (pos <= md) update(ls[u] , l , md , pos , v);
else update(rs[u] , md + 1 , r , pos , v);
pushup(u);
}
int query(int u,int l,int r,int ql,int qr) {
if (u == 0) return 0;
if (l == ql && r == qr) {
return sum[u];
}
int md = (l + r) >> 1;
if (qr <= md) return query(ls[u] , l , md , ql , qr);
else if (ql > md) return query(rs[u] , md + 1 , r , ql , qr);
else {
return query(ls[u] , l , md , ql , md) + query(rs[u] , md + 1 , r , md + 1 , qr);
}
}
template<typename T>
struct Fenwick{
int n;
std::vector<T> tr;
Fenwick(int n) : n(n), tr(n + 1, 0){}
int lowbit(int x){
return x & -x;
}
void modify(int x, T c){
for(int i = x; i <= n; i += lowbit(i)) tr[i] += c;
}
void modify(int l, int r, T c){
modify(l, c);
if (r + 1 <= n) modify(r + 1, -c);
}
T query(int x){
T res = T();
for(int i = x; i; i -= lowbit(i)) res += tr[i];
return res;
}
T query(int l, int r){
return query(r) - query(l - 1);
}
int find_first(T sum){
int ans = 0; T val = 0;
for(int i = std::__lg(n); i >= 0; i--){
if ((ans | (1 << i)) <= n && val + tr[ans | (1 << i)] < sum){
ans |= 1 << i;
val += tr[ans];
}
}
return ans + 1;
}
int find_last(T sum){
int ans = 0; T val = 0;
for(int i = std::__lg(n); i >= 0; i--){
if ((ans | (1 << i)) <= n && val + tr[ans | (1 << i)] <= sum){
ans |= 1 << i;
val += tr[ans];
}
}
return ans;
}
};
using BIT = Fenwick<LL>;
void clear() {
for (int i = 1 ; i <= cnt ; ++i) {
rt[i] = ls[i] = rs[i] = sum[i] = 0;
}
cnt = 0;
}
void solve() {
clear();
int n,q;
cin >> n >> q;
vector<int> c(n + 1) , v(n + 1);
for (int i = 1 ; i <= n ; ++i) {
cin >> c[i];
}
for (int i = 1 ; i <= n ; ++i) {
cin >> v[i];
}
BIT tr(n);
for (int i = 1 ; i <= n ; ++i) {
tr.modify(i , v[i]);
}
for (int i = 1 ; i <= n ; ++i) {
update(rt[c[i]] , 1 , n , i , 1);
}
for (int i = 1 ; i <= q ; ++i) {
int opt;
cin >> opt;
if (opt == 1) {
int p,x;
cin >> p >> x;
if (c[p] == x) continue;
update(rt[c[p]] , 1 , n , p , -1);
c[p] = x;
update(rt[c[p]] , 1 , n , p , 1);
} else if (opt == 2) {
int p,x;
cin >> p >> x;
if (v[p] == x) continue;
tr.modify(p , -v[p]);
v[p] = x;
tr.modify(p , v[p]);
} else {
int x,k;
cin >> x >> k;
vector<int> s(k);
for (int j = 0 ; j < k ; ++j) {
cin >> s[j];
}
auto check = [&] (int l,int r) {
int sum = 0;
for (int j = 0 ; j < k ; ++j) {
sum += query(rt[s[j]] , 1 , n , l , r);
}
return (sum == r - l + 1);
};
int L , R , l = 1 , r = x;
while (l < r) {
int md = (l + r) >> 1;
if (check(md , x)) r = md;
else l = md + 1;
}
L = r;
l = x , r = n;
while (l < r) {
int md = (l + r + 1) >> 1;
if (check(x , md)) l = md;
else r = md - 1;
}
R = l;
cout << tr.query(R) - tr.query(L - 1) << "\n";
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}