#include "bits/stdc++.h" using namespace std; //HEAD_OF_CONFIG_ static const int MOD=1000000007; //1000000000000000003LL static const double eps=1e-8; //TAIL_OF_CONFIG_ //[HEAD_OF_JKI'S_HEADER_ //TYPEDEF typedef long long lld; typedef unsigned long long u64; typedef pair pii; //COMPARE template inline T MIN(const T x, const T y){ return (x inline T MAX(const T x, const T y){ return (x>y)?x:y; } template inline void UPDMIN(T &x, const T y){ if(x>y)x=y; } template inline void UPDMAX(T &x, const T y){ if(x inline int SIZE(const T &x){ return (int)x.size(); } template inline int LENGTH(const T &x){ return (int)x.length(); } template inline pair MP(const T1 &x, const T2 &y){ return make_pair(x, y); } //BIT inline int BINT(const int x){ return 1< inline T LOWBIT(const T x){ return (x^(x-1))&x; } template inline int BITCOUNT(const T x){ return (!x)?x:(1+BITCOUNT(x&(x-1))); } //CONST VALUE const double PI=acos(-1.0); const double EPS=1e-5; //CALCULATE template inline T SQR(const T x){ return x*x; } template inline T1 POW(const T1 x, const T2 y){ if(!y)return 1;else if((y&1)==0){ return SQR(POW(x, y>>1)); }else return POW(x, y^1)*x; } //NUMBERIC template inline T GCD(const T x, const T y){ if(x<0)return GCD(-x, y); if(y<0)return GCD(x, -y); return (!y)?x:GCD(y, x%y); } template inline T LCM(const T x, const T y){ if(x<0)return LCM(-x, y); if(y<0)return LCM(x, -y); return x*(y/GCD(x, y)); } template inline T EEA(const T a, const T b, T &x, T &y){ /* a*x+b*y == GCD(a, b) == EEA(a, b, x, y) */ if(a<0){ T d=EEA(-a, b, x, y); x=-x; return d; } if(b<0){ T d=EEA(a, -b, x, y); y=-y; return d; } if(!b){ x=1; y=0; return a; }else{ T d=EEA(b, a%b, x, y); T t=x; x=y; y=t-(a/b)*y; return d; } } template inline vector > FACTORIZE(T x){ vector > ret; if(x<0)x=-x; for (T i=2;x>1;){ if(x%i==0){ int count=0; for(;x%i==0;x/=i)count++; ret.push_back(MP(i, count)); } i++;if(i>x/i)i=x; } return ret; } template inline int ISPRIME(const T x){ if(x<=1)return 0; for(T i=2; SQR(i)<=x; i++)if(x%i==0)return 0; return 1; } template inline T EULARFUNCTION(T x){ vector > f=FACTORIZE(x); for(typename vector >::iterator it=f.begin(); it!=f.end(); it++){ x=x/it->first*(it->first-1); } return x; } template inline T INVERSEE(const T a, const T b=MOD){ T x, y; EEA(a, b, x, y); return x?x:1; } int *PRIMELIST(const int til, int *length=NULL){ int *foo=(int*)malloc(sizeof(int)*(til+1)); int len=0; memset(foo, 0, sizeof(int)*(til+1)); for(int i=2; i<=til; i++){ if(!foo[i])foo[len++]=i; for(int j=0; j inline T MOD_STD(const T x, const T m=MOD){ return (x%m+m)%m; } template inline void MOD_STD(T *x, const T m=MOD){ *x=(*x%m+m)%m; } template inline T MOD_ADD(const T x, const T y, const T m=MOD){ return (x+y)%m; } template inline void MOD_ADD(T *x, const T y, const T m=MOD){ *x=(*x+y)%m; } template inline T MOD_MUL(const T x, const T y, const T m=MOD){ return (T)((1LL*x*y)%m); } template inline void MOD_MUL(T *x, const T y, const T m=MOD){ *x=(T)((1LL*(*x)*y)%m); } template inline T1 MOD_POW(const T1 x, const T2 y, const T1 m=MOD){ if(y==0)return 1%m;else if((y&1)==0){ T1 z=MOD_POW(x, y>>1, m); return MOD_MUL(z, z, m); }else return MOD_MUL(MOD_POW(x, y^1, m), x, m); } inline lld MODL_MUL(lld x, lld y, const lld m=MOD){ MOD_STD(&x, m); MOD_STD(&y, m); if(x0){ if(y&1){ MOD_ADD(&z, x, m); } MOD_ADD(&x, x, m); y>>=1; } return z; } inline lld MODL_POW(const lld x, const lld y, const lld m=MOD){ if(y==0LL)return 1LL%m;else if((y&1)==0LL){ lld z=MODL_POW(x, y>>1, m); return MODL_MUL(z, z, m); }else return MODL_MUL(MODL_POW(x, y^1, m), x, m); } //General template class cycleq_t{ private: size_t cnt, cap; size_t lef, rig; T *que; public: cycleq_t(const size_t size){ this->cap=size; this->que=(T*)malloc(sizeof(T)*size); this->clear(); } ~cycleq_t(){ free(this->que); } inline void clear(){ this->lef=this->rig=0; this->cnt=0; } inline void put(const T &obj){ assert(this->cntcap); this->que[this->rig++]=obj; if(this->rig==this->cap) this->rig=0; this->cnt++; } inline void put_back(const T &obj){ this->put(obj); } inline void put_front(const T &obj){ assert(this->cntcap); this->lef--; if(!~this->lef) this->lef=this->cap-1; this->que[this->lef]=obj; this->cnt++; } inline T pop(){ assert(this->cnt>0); T res=this->que[this->lef++]; if(this->lef==this->cap) this->lef=0; this->cnt--; return res; } inline T pop_front(){ return this->pop(); } inline T pop_back(){ assert(this->cnt>0); this->rig--; if(!~this->rig) this->rig=this->cap-1; this->cnt--; return this->que[this->rig]; } inline T get(int64_t offset=0){ if(offset>=0){ assert(offsetcnt); offset+=this->lef; if(offset>this->cap) offset-=this->cap; return this->que[offset]; }else{ assert(offset>=-this->cnt); offset+=this->rig; if(offset<0) offset+=this->cap; return this->que[offset]; } } }; //MATRIX template class MATX{ private: unsigned long hig, wid; T *data; void __init(){ this->data=(T*)malloc(sizeof(T)*this->hig*this->wid); memset(this->data, 0, sizeof(T)*this->hig*this->wid); } public: MATX(){ this->hig=this->wid=1; __init(); } MATX(const unsigned long _len){ this->hig=this->wid=_len; __init(); } MATX(const unsigned long _hig, const unsigned long _wid){ this->hig=_hig; this->wid=_wid; __init(); } MATX(const MATX &rhs){ this->hig=rhs.hig; this->wid=rhs.wid; this->data=(T*)malloc(sizeof(T)*this->hig*this->wid); for(unsigned long x=0; xhig; x++) for(unsigned long y=0; ywid; y++) this->data[x*this->wid+y]=rhs.at(x, y); } ~MATX(){ free(this->data); } T & operator()(const unsigned long x, const unsigned long y){ if(x>=this->hig || y>=this->wid)return (*(T*)NULL); return this->data[x*this->wid+y]; } T * operator[](const unsigned long x){ if(x>=this->hig)return (T*)NULL; return this->data+(x*this->wid); } MATX & operator=(const MATX &rhs){ if(this->hig!=rhs.hig || this->wid!=rhs.wid){ free(this->data); this->hig=rhs.hig; this->wid=rhs.wid; this->data=(T*)malloc(sizeof(T)*this->hig*this->wid); } for(unsigned long x=0; xhig; x++) for(unsigned long y=0; ywid; y++) this->data[x*this->wid+y]=rhs.at(x, y); return *this; } const MATX operator+(const MATX &opn) const{ MATX ret(*this); for(unsigned long x=0; xhig, opn.wid); const unsigned long len=MIN(this->wid, opn.hig); for(unsigned long x=0; xat(x, z)*opn.at(z, y); return ret; } const MATX mul(const MATX &opn) const{ return *this*opn; } template const MATX mul(const MATX &opn, const T2 m) const{ MATX ret(this->hig, opn.wid); const unsigned long len=MIN(this->wid, opn.wid); for(unsigned long x=0; xat(x, z), opn.at(z, y), m), m); return ret; } MATX & operator +=(const MATX &rhs){ *this=*this+rhs; return *this; } MATX & operator -=(const MATX &rhs){ *this=*this-rhs; return *this; } MATX & operator *=(const MATX &rhs){ *this=*this*rhs; return *this; } const MATX pow(const unsigned long p) const{ MATX buff(*this), ret(this->hig, this->wid); ret.set_one(); if(p>0)for(unsigned long i=1;;i<<=1){ if(p&i)ret*=buff; buff*=buff; if(i>(p>>1))break; } return ret; } template const MATX pow(const unsigned long p, const T2 m) const{ MATX buff(*this), ret(this->hig, this->wid); ret.set_one(); if(p>0)for(unsigned long i=1;;i<<=1){ if(p&i)ret=ret.mul(buff, m); buff=buff.mul(buff, m); if(i>(p>>1))break; } return ret; } const T at(const unsigned long x, const unsigned long y) const{ if(x>=this->hig || y>=this->wid)return 0; return this->data[x*wid+y]; } void show() const{ for(unsigned long x=0; xhig; x++){ for(unsigned long y=0; ywid; y++) cout<at(x, y)<<" "; cout<hig; x++) for(unsigned long y=0; ywid; y++) this->data[x*this->wid+y]=(x==y)?1:0; } }; //Complex template class complex_t{ public: T r, i;//real part & imaginary part; x+yi complex_t(T x=0.0, T y=0.0){ this->r=x; this->i=y; } complex_t operator + (const complex_t &opn) const { return complex_t(this->r+opn.r, this->i+opn.i); } complex_t operator - (const complex_t &opn) const { return complex_t(this->r-opn.r, this->i-opn.i); } complex_t operator * (const complex_t &opn) const { return complex_t(this->r*opn.r-this->i*opn.i, this->r*opn.i+this->i*opn.r); } }; template void fast_fourier_trans(complex_t f[], const int len, const int is_dft){ for(int i=1, j=len>>1; i>1; while(j>=k){ j-=k; k>>=1; } if(j wn(cos(is_dft?(-2*PI/h):(2*PI/h)), sin(is_dft?(-2*PI/h):(2*PI/h))); for(int i=0; i wm(1.0, 0.0); for(int j=i; j>1); j++){ complex_t u = f[j]; complex_t t = wm*f[j+(h>>1)]; f[j] = u+t; f[j+(h>>1)] = u-t; wm = wm*wn; } } } if(!is_dft){ for(int i=0; i>=1;s++; } for(int i=0; i<12 && this->prime_table[i]prime_table[i], p, s, n))return 0; } return 1; } }; const int MILLERRABIN::prime_table[12] = { 2, 3, 5, 7, 11, 13 ,17, 19, 23, 29, 31, 37 }; //Computational Geometry template inline int fsign(const T x){ if(x>-eps && x class point_t{ public: T x, y; point_t (){ this->x=0.0; this->y=0.0; } point_t (const T _x, const T _y){ this->x=_x; this->y=_y; } point_t operator - (const point_t &rhs) const{ return point_t(this->x-rhs.x, this->y-rhs.y); } T operator ^ (const point_t &rhs) const{ return this->x*rhs.y - this->y*rhs.x; } T operator * (const point_t &rhs) const{ return this->x*rhs.x + this->y*rhs.y; } bool operator < (const point_t &rhs) const{ if(fsign(this->y-rhs.y)!=0) return fsign(this->y-rhs.y)<0; return fsign(this->x-rhs.x)<0; } T cross(const point_t &p, const point_t &q) const{ return (p-*this)^(q-*this); } void rotate(const double radian){ T x0=x, y0=y; T sinr=sin(radian); T cosr=cos(radian); x=x0*cosr-y0*sinr; y=x0*sinr+y0*cosr; } void rotate(const point_t &p, const double radian){ T x0=x-p.x, y0=y-p.y; T sinr=sin(radian); T cosr=cos(radian); x=x0*cosr-y0*sinr+p.x; y=x0*sinr+y0*cosr+p.y; } T dist2(const point_t &lhs, const point_t &rhs) const{ return (lhs-rhs)*(lhs-rhs); } T dist2(const point_t &rhs) const{ return (*this-rhs)*(*this-rhs); } T dist(const point_t &lhs, const point_t &rhs) const{ return sqrt((lhs-rhs)*(lhs-rhs)); } T dist(const point_t &rhs) const{ return sqrt((*this-rhs)*(*this-rhs)); } }; template class segment_t{ public: point_t p, q; segment_t (){ this->p.x=this->p.y=0.0; this->q.x=this->q.y=0.0; } template segment_t (const point_t &_p, const point_t &_q){ this->p.x=_p.x; this->p.y=_p.y; this->q.x=_q.x; this->q.y=_q.y; } segment_t (const T px, const T py, const T qx, const T qy){ this->p.x=px; this->p.y=py; this->q.x=qx; this->q.y=qy; } T length() const{ return this->p.dist(this->q); } T length2() const{ return this->p.dist2(this->q); } int contain(const point_t &pnt, const int ignore_endpoint=0) const{ if(ignore_endpoint){ return fsign((this->p-pnt)^(this->q-pnt))==0 && fsign((pnt.x-this->p.x)*(pnt.x-this->q.x)) <0 && fsign((pnt.y-this->p.y)*(pnt.y-this->q.y)) <0; }else{ return fsign((this->p-pnt)^(this->q-pnt))==0 && fsign((pnt.x-this->p.x)*(pnt.x-this->q.x)) <=0 && fsign((pnt.y-this->p.y)*(pnt.y-this->q.y)) <=0; } } int intersection(const segment_t &sa, const segment_t &sb, const int ignore_endpoint=0) const{ if(!ignore_endpoint){ if(sa.contain(sb.p) || sa.contain(sb.q) || sb.contain(sa.p) || sb.contain(sa.q)) return 1; } return fsign(sa.p.cross(sa.q, sb.p))*fsign(sa.p.cross(sa.q, sb.q))<0 && fsign(sb.p.cross(sb.q, sa.p))*fsign(sb.p.cross(sb.q, sa.q))<0; } int intersection(const segment_t &rhs, const int ignore_endpoint=0) const{ return this->intersection(*this, rhs, ignore_endpoint); } }; #ifndef __APPLE__ template static int compare_pas(const void *x, const void *y, void *z){ #else template static int compare_pas(void *z, const void *x, const void *y){ #endif const point_t *p1 = (point_t*)x; const point_t *p2 = (point_t*)y; const point_t *p0 = (point_t*)z; int sgn = fsign(((*p1)-(*p0))^((*p2)-(*p0))); if(sgn!=0)return -sgn; return fsign(p0->dist2(*p1)-p0->dist2(*p2)); } template void polar_angle_sort(point_t *pnts, const int n){ int p=0; for(int i=1; i), compare_pas, pnts); #else qsort_r(pnts+1, n-1, sizeof(point_t), pnts, compare_pas); #endif } template void graham(point_t *pnts, const int n, int *idx, int &m){ polar_angle_sort(pnts, n); m=0; if(n<3)return; idx[m++]=0; idx[m++]=1; for(int i=2; i1 && fsign(pnts[idx[m-2]].cross(pnts[idx[m-1]], pnts[i]))<=0)m--; idx[m++]=i; } } //Graph template class maxflow_c{ private: size_t capv, cape; size_t cntv, cnte; struct mf_edge_t; struct mf_vertex_t{ mf_edge_t *head, *hold; size_t idx, hig; } *vertex, **vhead, **vtail, **vque; struct mf_edge_t{ mf_vertex_t *src, *dst; mf_edge_t *nxt, *rsv; T cap, cur; } *edge_pool, *edge_tail, **esup; struct dncnode_t{ mf_vertex_t *vtx; T precap, reflux; } *dinic_stack_pool; size_t *dcnt; size_t higlev(size_t src, size_t trm, const int reverse=0){ if(reverse) swap(src, trm); if(this->vque==NULL) this->vque=(mf_vertex_t**)malloc(sizeof(mf_vertex_t*)*this->capv); size_t lef=0, rig=0; for(mf_vertex_t **vtxp=this->vhead; vtxp!=this->vtail; vtxp++) (*vtxp)->hig=numeric_limits::max(); this->vertex[src].hig=0; this->vque[rig++]=&this->vertex[src]; this->vertex[trm].hig=numeric_limits::max(); for(; lefvque[lef]; for(mf_edge_t *edge=vtx->head; edge!=NULL; edge=edge->nxt){ if(!reverse){ if(edge->curcap && edge->dst->hig>vtx->hig+1){ edge->dst->hig=vtx->hig+1; this->vque[rig++]=edge->dst; } }else{ if(edge->rsv->currsv->cap && edge->dst->hig>vtx->hig+1){ edge->dst->hig=vtx->hig+1; this->vque[rig++]=edge->dst; } } } } this->vque[rig++]=NULL; return this->vertex[trm].hig; } T isap_augment(const size_t src, const size_t trm){ T inc=numeric_limits::max(); for(size_t i=trm; i!=src; i=this->esup[i]->src->idx){ UPDMIN(inc, this->esup[i]->cap-this->esup[i]->cur); } for(size_t i=trm; i!=src; i=this->esup[i]->src->idx){ this->esup[i]->cur+=inc; this->esup[i]->rsv->cur-=inc; } return inc; } size_t isap_retreat(size_t &i, const size_t src){ size_t best=numeric_limits::max(); for(mf_edge_t *edge=this->vertex[i].head; edge!=NULL; edge=edge->nxt){ if(edge->curcap){ UPDMIN(best, edge->dst->hig+1); } } size_t rep=--this->dcnt[this->vertex[i].hig]; this->vertex[i].hig=best; if(bestcntv){ this->dcnt[best]++; } if(i!=src) i=this->esup[i]->src->idx; return rep; } public: maxflow_c(const size_t numv, const size_t nume){ this->capv=numv; this->cape=nume; this->vertex=(mf_vertex_t*)malloc(sizeof(mf_vertex_t)*numv); for(size_t i=0; ivertex[i].idx=i; } this->vhead=(mf_vertex_t**)malloc(sizeof(mf_vertex_t*)*numv); this->vtail=this->vhead; this->vque=NULL; this->edge_pool=(mf_edge_t*)malloc(sizeof(mf_edge_t)*(nume<<1)); this->dinic_stack_pool=NULL; this->esup=NULL; this->dcnt=NULL; this->reset(); }; ~maxflow_c(){ if(this->dcnt!=NULL) free(this->dcnt); if(this->esup!=NULL) free(this->esup); if(this->dinic_stack_pool!=NULL) free(this->dinic_stack_pool); free(this->edge_pool); if(this->vque!=NULL) free(this->vque); free(this->vhead); free(this->vertex); } void reset(){ for(size_t i=0; icapv; i++) this->vertex[i].head=NULL; this->vtail=this->vhead; this->edge_tail=this->edge_pool; this->cntv=0; this->cnte=0; } void add_edge(const size_t u, const size_t v, const T c, const T r=0){ assert(ucapv && vcapv); assert(this->cntecape); this->edge_tail->nxt=this->vertex[u].head; this->edge_tail->rsv=this->edge_tail+1; this->edge_tail->src=&this->vertex[u]; this->edge_tail->dst=&this->vertex[v]; this->edge_tail->cap=c; this->edge_tail->cur=0; if(this->vertex[u].head==NULL){ *this->vtail++=&this->vertex[u]; this->cntv++; } this->vertex[u].head=this->edge_tail++; this->edge_tail->nxt=this->vertex[v].head; this->edge_tail->rsv=this->edge_tail-1; this->edge_tail->src=&this->vertex[v]; this->edge_tail->dst=&this->vertex[u]; this->edge_tail->cap=r; this->edge_tail->cur=0; if(this->vertex[v].head==NULL){ *this->vtail++=&this->vertex[v]; this->cntv++; } this->vertex[v].head=this->edge_tail++; this->cnte++; } T dinic(const size_t src, const size_t trm){ assert(srccapv && trmcapv && src!=trm); T foo=0; if(this->dinic_stack_pool==NULL) this->dinic_stack_pool=(dncnode_t*)malloc(sizeof(dncnode_t)*this->capv); while(higlev(src, trm)::max()){ for(size_t i=0; this->vque[i]!=NULL; i++){ this->vque[i]->hold=this->vque[i]->head; } dncnode_t *top=this->dinic_stack_pool; top->vtx=&this->vertex[src]; top->precap=numeric_limits::max(); top->reflux=0; while(this->vertex[src].hold!=NULL){ if(top->vtx->idx!=trm && top->vtx->hold!=NULL && top->vtx->hold->curvtx->hold->cap && top->vtx->hold->src->hig+1==top->vtx->hold->dst->hig){ dncnode_t *prv=top++; top->vtx=prv->vtx->hold->dst; top->precap=MIN(prv->precap-prv->reflux, prv->vtx->hold->cap-prv->vtx->hold->cur); top->reflux=0; }else if(top->vtx->idx==trm){ T inc=top->precap; foo+=inc; while(top->precap==top->reflux+inc){ inc+=top->reflux; top--; top->vtx->hold->cur+=inc; top->vtx->hold->rsv->cur-=inc; } top->reflux+=inc; }else{ if(top->vtx->hold==NULL){ T ref=top->reflux; top--; top->vtx->hold->cur+=ref; top->vtx->hold->rsv->cur-=ref; top->reflux+=ref; } top->vtx->hold=top->vtx->hold->nxt; } } } return foo; } T isap(const size_t src, const size_t trm){ assert(srccapv && trmcapv && src!=trm); T foo=0; if(higlev(src, trm, 1)::max()){ if(this->dcnt==NULL)this->dcnt=(size_t*)malloc(sizeof(size_t)*this->capv); if(this->esup==NULL)this->esup=(mf_edge_t**)malloc(sizeof(mf_edge_t*)*this->capv); for(size_t i=0; icntv; i++)this->dcnt[i]=0; for(size_t i=0; this->vque[i]!=NULL; i++){ this->vque[i]->hold=this->vque[i]->head; this->dcnt[this->vque[i]->hig]++; } size_t i=src; while(this->vertex[src].higcntv){ mf_edge_t *edge=this->vertex[i].hold; while(edge!=NULL && !(edge->curcap && edge->src->hig==edge->dst->hig+1)){ edge=edge->nxt; } if(edge!=NULL){ this->vertex[i].hold=edge; this->esup[edge->dst->idx]=edge; i=edge->dst->idx; if(i==trm){ foo+=isap_augment(src, trm); i=src; } }else{ this->vertex[i].hold=this->vertex[i].head; if(isap_retreat(i, src)==0) break; } } } return foo; } }; //]TAIL_OF_JKI'S_HEADER int main(){ int T;scanf("%d", &T); while(T--){ int n;scanf("%d", &n); int foo=0; for(int i=0; i