//teja349
#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <iomanip>
//setbase - cout << setbase (16); cout << 100 << endl; Prints 64
//setfill - cout << setfill ('x') << setw (5); cout << 77 << endl; prints xxx77
//setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
//cout.precision(x) cout<<fixed<<val; // prints x digits after decimal in val
using namespace std;
#define f(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) f(i,0,n)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define vi vector< int >
#define vl vector< ll >
#define ss second
#define ff first
#define ll long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
#define inf (1000*1000*1000+5)
#define all(a) a.begin(),a.end()
#define tri pair<int,pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000*1000*1000+7)
#define pqueue priority_queue< int >
#define pdqueue priority_queue< int,vi ,greater< int > >
#define flush fflush(stdout)
#define primeDEN 727999983
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
void printvec(vector<T>& vec){
for(int i=0;i<vec.size();i++){
cout<<vec[i]<<" ";
}
cout<<endl;
}
int ans = 0;
vector<vi> vec(123456);
map<int,int> mapi;
int adder(int st,int en,int elem){
if(mapi.find(elem)==mapi.end())
return 0;
elem=mapi[elem];
int haha=upper_bound(all(vec[elem]),en)-lower_bound(all(vec[elem]),st);
ans+=haha;
return 0;
}
int remov(int st,int en,int elem){
if(mapi.find(elem)==mapi.end())
return 0;
elem=mapi[elem];
int haha=upper_bound(all(vec[elem]),en)-lower_bound(all(vec[elem]),st);
ans-=haha;
return 0;
}
int a[123456],b[123456];
int main(){
//std::ios::sync_with_stdio(false); cin.tie(NULL);
int t;
cin>>t;
// assert(t==5);
while(t--){
int n,q;
//cin>>n>>q;
scanf("%d",&n);
scanf("%d",&q);
//assert(n==100000);
//assert(q==n);
int i;
set<pii> seti;
rep(i,n){
//cin>>a[i];
scanf("%d",a+i);
//assert(1<=a[i] && a[i]<=1e9);
seti.insert(mp(i,a[i]));
}
seti.insert(mp(n,2));
mapi.clear();
int counter=0;
ans=0;
rep(i,n){
//cin>>b[i];
scanf("%d",b+i);
//assert(1<=b[i] && b[i]<=1e9);
if(b[i]==a[i])
ans++;
if(mapi.find(b[i])==mapi.end())
mapi[b[i]]=counter++;
vec[mapi[b[i]]].pb(i);
}
int l,r,c;
set<pii>::iterator it,it1;
int st,en;
pii papa;
rep(i,q){
//cin>>l>>r>>c;
scanf("%d",&l);
scanf("%d",&r);
scanf("%d",&c);
// int lol;
//cin>>lol;
l^=ans;
r^=ans;
c^=ans;
//assert(1<=l && l<=n);
//assert(l<=r && r<=n);
//assert(1<=c && c<=1e9);
l--;
r--;
it=seti.lower_bound(mp(l,0));
if(it==seti.end() || it->ff!=l){
it--;
papa=*it;
papa.ff=l;
seti.insert(papa);
}
it=seti.lower_bound(mp(r+1,0));
if(it==seti.end() || it->ff!=r+1){
it--;
papa=*it;
papa.ff=r+1;
seti.insert(papa);
}
while(1){
it=seti.lower_bound(mp(l,0));
if(it->ff==r+1)
break;
it1=it;
it1++;
st=it->ff;
en=it1->ff-1;
remov(st,en,it->ss);
seti.erase(it);
}
adder(l,r,c);
seti.insert(mp(l,c));
printf("%d\n",ans);
//cout<<ans<<endl;
// assert(ans==lol);
}
rep(i,counter){
vec[i].clear();
}
}
return 0;
}
Ly90ZWphMzQ5CiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8c2V0PgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8Y3N0ZGxpYj4KI2luY2x1ZGUgPGNsaW1pdHM+CiNpbmNsdWRlIDx1dGlsaXR5PgojaW5j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