#include using namespace std; #define REP(i,a,b) for(i=a;i struct heapEx { int *hp, *place, size; T *val; void malloc(int N){hp=(int*)std::malloc(N*sizeof(int));place=(int*)std::malloc(N*sizeof(int));val=(T*)std::malloc(N*sizeof(T));} void free(){free(hp);free(place);free(val);} void* malloc(int N, void *workMemory){hp=(int*)workMemory;workMemory=(void*)(hp+N);place=(int*)workMemory;workMemory=(void*)(place+N);val=(T*)workMemory;workMemory=(void*)(val+N);return workMemory;} void init(int N){int i;size=0;rep(i,N)place[i]=-1;} void up(int n){int m;while(n){m=(n-1)/2;if(val[hp[m]]<=val[hp[n]])break;swap(hp[m],hp[n]);swap(place[hp[m]],place[hp[n]]);n=m;}} void down(int n){int m;for(;;){m=2*n+1;if(m>=size)break;if(m+1val[hp[m+1]])m++;if(val[hp[m]]>=val[hp[n]])break;swap(hp[m],hp[n]);swap(place[hp[m]],place[hp[n]]);n=m;}} void change(int n, T v){T f=val[n];val[n]=v;if(place[n]==-1){place[n] = size;hp[size++] = n;up(place[n]);}else{if(f < v) down(place[n]); else if(f > v) up(place[n]);}} int pop(void){int res=hp[0];place[res]=-1;size--;if(size)hp[0]=hp[size],place[hp[0]]=0,down(0);return res;} }; #define INF 1000000000 int T, N, M, C[400]; vector ls[400]; int st[400]; int main(){ int i, res; heapEx heap; // binary heap heap.malloc(400); scanf("%d",&T); while(T--){ scanf("%d%d",&N,&M); rep(i,M) scanf("%d",C+i), C[i]--; rep(i,400) ls[i].clear(), st[i] = 0; rep(i,M) ls[C[i]].push_back(i); rep(i,400) ls[i].push_back(INF); heap.init(400); // heap is empty now res = 0; rep(i,M){ st[C[i]]++; if(heap.place[C[i]] >= 0){ // if C[i] is in heap heap.change(C[i], -ls[C[i]][st[C[i]]]); // then change value continue; } res++; if(heap.size == N) heap.pop(); // if heap has N elements, then delete the top one heap.change(C[i], -ls[C[i]][st[C[i]]]); // insert the element C[i] into heap } printf("%d\n", res); } return 0; }