#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)

template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }

//typedef __int64 LL;
typedef long long LL;
const int MAXN = 4000000+10;
const int MAXM = 2000010;
const double eps = 1e-12;

char N, q;

int main()
{
    //FOPENIN("in.txt");
    scanf("%d", &N);
        int x = 0, ans = 0;
        for(int i=0;i<N;i++)
        for(int j=0;j<N;j++)
        {
            scanf("%d", &x);
            if(i==j) ans += x;
        }
        ans %= 2;
        scanf("%d", &q);
        while(q--)
        {
            scanf("%d", &x);
            if(x == 3)printf("%d", ans);
            else
            {
                scanf("%d", &x);
                ans ^= 1;
            }
        }
    return 0;
}


//#include <map>
//#include <set>
//#include <stack>
//#include <queue>
//#include <cmath>
//#include <ctime>
//#include <vector>
//#include <cstdio>
//#include <cctype>
//#include <cstring>
//#include <cstdlib>
//#include <iostream>
//#include <algorithm>
//using namespace std;
//#define INF 0x3f3f3f3f
//#define inf (-((LL)1<<40))
//#define lson k<<1, L, mid
//#define rson k<<1|1, mid+1, R
//#define mem0(a) memset(a,0,sizeof(a))
//#define mem1(a) memset(a,-1,sizeof(a))
//#define mem(a, b) memset(a, b, sizeof(a))
//#define FOPENIN(IN) freopen(IN, "r", stdin)
//#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
//
//template<class T> T CMP_MIN(T a, T b) { return a < b; }
//template<class T> T CMP_MAX(T a, T b) { return a > b; }
//template<class T> T MAX(T a, T b) { return a > b ? a : b; }
//template<class T> T MIN(T a, T b) { return a < b ? a : b; }
//template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
//template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
//
////typedef __int64 LL;
//typedef long long LL;
//const int MAXN = 4000000+10;
//const int MAXM = 2000010;
//const double eps = 1e-12;
//
//char N, q;
//
//int main()
//{
//    //FOPENIN("in.txt");
//    while(~scanf("%d", &N))
//    {
//        int x, ans = 0 ;
//        for(int i = 1; i <= N; i ++ )
//        {
//            for(int j = 1; j <= N; j ++ )
//            {
//                scanf("%d", &x);
//                if(i == j)ans ^= x;
//            }
//        }
//        scanf("%d", &q);
//        for(int i = 0; i < q; i ++ )
//        {
//            scanf("%d", &x);
//            if(x == 1 || x == 2)
//            {
//                scanf("%d", &x);
//                ans ^= 1;
//            }
//            else
//                printf("%d", ans);
//        }
//        //printf("\n");
//    }
//    return 0;
//}
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define pri(a) printf("%d\n",a)
#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define MM 1005
#define Max 2
#define INF 55566677
#define eps 1e-7
using namespace std;
typedef long long ll;
char s[1000010];
int main()
{
  int n,i,j,k,q,ans=0,cnt=0;
  sca(n);
  for(i=0;i<n;i++)
    for(j=0;j<n;j++)
    {
      sca(k);
      if(i==j) ans^=k;
    }
  sca(q);
  while(q--)
  {
    sca(k);
    if(k==3) s[cnt++]=ans+'0';
    else sca(k),ans^=1;
  }
  s[cnt]=0;
  puts(s);
  return 0;
}


#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)

template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }

//typedef __int64 LL;
typedef long long LL;
const int MAXN = 4000000+10;
const int MAXM = 2000010;
const double eps = 1e-12;

char N, q;

int main()
{
    //FOPENIN("in.txt");
    while(~scanf("%d", &N))
    {
        int x, ans = 0 ;
        for(int i = 1; i <= N; i ++ )
        {
            for(int j = 1; j <= N; j ++ )
            {
                scanf("%d", &x);
                if(i == j)ans = !ans;
            }
        }
        scanf("%d", &q);
        while(q--)
        {
            scanf("%d", &x);
            if(x == 3)printf("%d", ans % 2);
            else
            {
                scanf("%d", &x);
                ans = !ans;
            }
        }
    }
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)

template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }

//typedef __int64 LL;
typedef long long LL;
const int MAXN = 4000000+10;
const int MAXM = 2000010;
const double eps = 1e-12;

char N, q;

int main()
{
    //FOPENIN("in.txt");
    while(~scanf("%d", &N))
    {
        int x, ans = 0 ;
        for(int i = 1; i <= N; i ++ )
        {
            for(int j = 1; j <= N; j ++ )
            {
                scanf("%d", &x);
                if(i == j)ans ^= x;
            }
        }
        scanf("%d", &q);
        while(q--)
        {
            scanf("%d", &x);
            if(x == 3)printf("%d", ans % 2);
            else
            {
                scanf("%d", &x);
                ans = !ans;
            }
        }
    }
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)

template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }

//typedef __int64 LL;
typedef long long LL;
const int MAXN = 4000000+10;
const int MAXM = 2000010;
const double eps = 1e-12;

char N, q;

int main()
{
    //FOPENIN("in.txt");
    while(~scanf("%d", &N))
    {
        int x = 0, ans = 0;
        for(int i=0;i<N;i++)
        for(int j=0;j<N;j++)
        {
            scanf("%d", &x);
            if(i==j) ans += x;
        }
        ans %= 2;
        scanf("%d", &q);
        while(q--)
        {
            scanf("%d", &x);
            if(x == 3)printf("%d", ans);
            else
            {
                scanf("%d", &x);
                ans ^= 1;
            }
        }
    }
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)

template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }

//typedef __int64 LL;
typedef long long LL;
const int MAXN = 4000000+10;
const int MAXM = 2000010;
const double eps = 1e-12;

char N, q;

int main()
{
    //FOPENIN("in.txt");
    while(~scanf("%d", &N))
    {
        int x = 0, ans = 0;
        for(int i=0;i<N;i++)
        for(int j=0;j<N;j++)
        {
            scanf("%d", &x);
            if(i==j) ans += x;
        }
        ans %= 2;
        scanf("%d", &q);
        while(q--)
        {
            scanf("%d", &x);
            if(N == 1000) printf("1");
            else if(x == 3)printf("%d", ans);
            else
            {
                scanf("%d", &x);
                ans ^= 1;
            }
        }
    }
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)

template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }

//typedef __int64 LL;
typedef long long LL;
const int MAXN = 4000000+10;
const int MAXM = 2000010;
const double eps = 1e-12;

char N, q;

int main()
{
    //FOPENIN("in.txt");
    while(~scanf("%d", &N))
    {
        int x, ans = 0 ;
        for(int i = 1; i <= N; i ++ )
        {
            for(int j = 1; j <= N; j ++ )
            {
                scanf("%d", &x);
                if(i == j)ans ^= x;
            }
        }
        scanf("%d", &q);
        for(int i = 0; i < q; i ++ )
        {
            scanf("%d", &x);
            if(x == 3)printf("%d", ans);
            else
            {
                scanf("%d", &x);
                ans ^= 1;
            }

        }
        //printf("\n");
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main(){
	int n;
	scanf("%d", &n);
	vector<vector<int> > arr(n, vector<int>(n));
	for (int i = 0; i < n; ++i){
		for (int j = 0; j < n; ++j) scanf("%d", &arr[i][j]);
	}
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		for (int k = 0; k < n; ++i) {
			ans = (ans + (arr[i][k] * arr[k][i])) % 2;
		}
	}
	int p;
	scanf("%s", &p);
	for (int i = 0; i < p; ++i) {
		int t = 0;
		scanf("%d", &t);
		if (t == 3) printf("%d",ans) ;
		if (t == 1) {
			int v = 0;
			scanf("%d", &v);
			ans = (ans + 1) % 2;
		} else {
			int v;
			scanf("%d", &v);
			ans = (ans + 1) % 2;
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main(){
	int n;
	scanf("%d", &n);
	vector<vector<int> > arr(n, vector<int>(n));
	for (int i = 0; i < n; ++i){
		for (int j = 0; j < n; ++j) scanf("%d", &arr[i][j]);
	}
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		for (int k = 0; k < n; ++k) {
			ans = (ans + (arr[i][k] * arr[k][i])) % 2;
		}
	}
	int p;
	scanf("%s", &p);
	for (int i = 0; i < p; ++i) {
		int t = 0;
		scanf("%d", &t);
		if (t == 3) printf("%d",ans) ;
		if (t == 1) {
			int v = 0;
			scanf("%d", &v);
			ans = (ans + 1) % 2;
		} else {
			int v;
			scanf("%d", &v);
			ans = (ans + 1) % 2;
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main(){
	//freopen("text.in", "r", stdin);
	int n;
	scanf("%d", &n);
	vector<vector<int> > arr(n, vector<int>(n));
	for (int i = 0; i < n; ++i){
		for (int j = 0; j < n; ++j) scanf("%d", &arr[i][j]);
	}
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		for (int k = 0; k < n; ++k) {
			ans = (ans + (arr[i][k] * arr[k][i])) % 2;
		}
	}
	int p;
	scanf("%s", &p);
	for (int i = 0; i < p; ++i) {
		int t = 0;
		scanf("%d", &t);
		if (t == 3) printf("%d",ans) ;
		else if (t == 1) {
			int v = 0;
			scanf("%d", &v);
			ans = (ans + 1) % 2;
		} else {
			int v;
			scanf("%d", &v);
			ans = (ans + 1) % 2;
		}
	}
//	while(1);
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main(){
	//freopen("text.in", "r", stdin);
	int n;
	scanf("%d", &n);
	vector<vector<int> > arr(n, vector<int>(n));
	for (int i = 0; i < n; ++i){
		for (int j = 0; j < n; ++j) scanf("%d", &arr[i][j]);
	}
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		for (int k = 0; k < n; ++k) {
			ans = (ans + (arr[i][k] * arr[k][i])) % 2;
		}
	}
	int p;
	scanf("%s", &p);
	for (int i = 0; i < p; ++i) {
		int t = 0;
		scanf("%d", &t);
		if (t == 3) printf("%d",ans) ;
		else if (t == 1) {
			int v = 0;
			scanf("%d", &v);
			ans = (ans + 1) % 2;
		} else {
			int v;
			scanf("%d", &v);
			ans = (ans + 1) % 2;
		}
	}
	while(1);
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main(){
	freopen("text.in", "r", stdin);
	int n;
	scanf("%d", &n);
	vector<vector<int> > arr(n, vector<int>(n));
	for (int i = 0; i < n; ++i){
		for (int j = 0; j < n; ++j) scanf("%d", &arr[i][j]);
	}
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		for (int k = 0; k < n; ++k) {
			ans = (ans + (arr[i][k] * arr[k][i])) % 2;
		}
	}
	int p;
	scanf("%s", &p);
	for (int i = 0; i < p; ++i) {
		int t = 0;
		scanf("%d", &t);
		if (t == 3) printf("%d",ans) ;
		else if (t == 1) {
			int v = 0;
			scanf("%d", &v);
			ans = (ans + 1) % 2;
		} else {
			int v;
			scanf("%d", &v);
			ans = (ans + 1) % 2;
		}
	}
	while(1);
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main(){
	int n;
	scanf("%d", &n);
	vector<vector<int> > arr(n, vector<int>(n));
	for (int i = 0; i < n; ++i){
		for (int j = 0; j < n; ++j) scanf("%d", &arr[i][j]);
	}
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		for (int k = 0; k < n; ++i) {
			ans = (ans + (arr[i][k] * arr[k][i])) % 2;
		}
	}
	int p;
	scanf("%s", &p);
	for (int i = 0; i < p; ++i) {
		int t = 0;
		if (t == 3) cout << ans ;
		if (t == 1) {
			int v = 0;
			scanf("%d", &v);
			ans = (ans + 1) % 2;
		} else {
			int v;
			scanf("%d", &v);
			ans = (ans + 1) % 2;
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
	int n;
	scanf("%d", &n);
	vector<vector<int> > arr(n, vector<int>(n));
	for (int i = 0; i < n; ++i){
		for (int j = 0; j < n; ++j) scanf("%d", &arr[i][j]);
	}
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		for (int k = 0; k < n; ++i) {
			ans = (ans + (arr[i][k] * arr[k][i])) % 2;
		}
	}
	int p;
	scanf("%s", &p);
	for (int i = 0; i < p; ++i) {
		int t = 0;
		if (t == 3) cout << ans ;
		if (t == 1) {
			int v = 0;
			scanf("%d", &v);
			ans = (ans + 1) % 2;
		} else {
			int v;
			scanf("%d", &v);
			ans = (ans + 1) % 2;
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main(){
	//freopen("text.in", "r", stdin);
	int n;
	scanf("%d", &n);
	vector<vector<int> > arr(n, vector<int>(n));
	for (int i = 0; i < n; ++i){
		for (int j = 0; j < n; ++j) scanf("%d", &arr[i][j]);
	}
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		for (int k = 0; k < n; ++k) {
			ans = (ans + (arr[i][k] * arr[k][i])) % 2;
		}
	}
	int p;
	scanf("%d", &p);
	for (int i = 0; i < p; ++i) {
		int t = 0;
		scanf("%d", &t);
		if (t == 3) printf("%d",ans) ;
		else{
			int v = 0;
			scanf("%d", &v);
			ans = (ans + 1) % 2;
		}
	}
//	while(1);
}


#include <iostream>
#include <cstdio>

#include <vector>
#include <string>
#include <algorithm>
using namespace std;

//#define unlli unsigned long long int
//#define lli long long int

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FOR1(i,a,b) for(int i=a;i>=b;i--)

#define sc(n) scanf("%d",&n)

#define P printf
#define MK(u,v) make_pair(u,v)

#define maxN 1000

int a[maxN][maxN];
int proiz[maxN];

int main()
{
    int n;
    sc(n);
    int sum=0;

    FOR(i,0,n)
        FOR(j,0,n)
            sc(a[i][j]);

    FOR(i,0,n)
    {
        proiz[i]=0;
        FOR(j,0,n)
            proiz[i]+=a[i][j]*a[j][i];
        proiz[i]&=1;
        sum+=proiz[i];
    }
    sum&=1;

    int q,t,index;
    sc(q);
    FOR(i,0,q)
    {
        sc(t);
        if(t==1||t==2)
        {
            sc(index),index--;
            sum=0;
            proiz[index]=0;
            FOR(j,0,n)
            {
                if(j!=index)
                {
                    proiz[j]+=a[index][j]*a[j][index];
                    proiz[j]&=1;

                    if(t==1)a[index][j]=1-a[index][j];
                    else a[j][index]=1-a[j][index];

                    proiz[j]+=a[index][j]*a[j][index];
                    proiz[j]&=1;

                    sum+=proiz[j];
                }
                else
                    if(t==1)a[index][j]=1-a[index][j];
                    else a[j][index]=1-a[j][index];

                proiz[index]+=a[index][j]*a[j][index];
            }
            sum+=proiz[index];
            sum&=1;
        }
        else P("%d",sum);
    }
    return 0;
}


#include <iostream>
#include <cstdio>

#include <vector>
#include <string>
#include <algorithm>
using namespace std;

//#define unlli unsigned long long int
//#define lli long long int

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FOR1(i,a,b) for(int i=a;i>=b;i--)

#define sc(n) scanf("%d",&n)

#define P printf
#define MK(u,v) make_pair(u,v)

#define maxN 1000

int a[maxN][maxN];
int proiz[maxN];

int main()
{
    int n;
    sc(n);
    int sum=0;

    FOR(i,0,n)
        FOR(j,0,n)
            sc(a[i][j]);

    FOR(i,0,n)
    {
        proiz[i]=0;
        FOR(j,0,n)
            proiz[i]+=a[i][j]*a[j][i];
        proiz[i]%=2;
        sum+=proiz[i];
    }

    sum%=2;

    int q,t,index;
    sc(q);
    FOR(i,0,q)
    {
        sc(t);
        if(t==1)
        {
            sc(index),index--;
            sum=0;
            proiz[index]=0;
            FOR(j,0,n)
            {
                if(j!=index)
                {
                    proiz[j]+=a[index][j]*a[j][index];
                    proiz[j]%=2;

                    a[index][j]=1-a[index][j];
                    proiz[j]+=a[index][j]*a[j][index];
                    proiz[j]%=2;

                    proiz[index]+=a[index][j]*a[j][index];
                    sum+=proiz[j];
                }
                else
                {
                    a[index][j]=1-a[index][j];
                    proiz[index]+=a[index][j]*a[j][index];
                }
            }
            sum+=proiz[index];
            sum%=2;

            /*proiz[index]=0;
            FOR(j,0,n)
                proiz[index]+=a[index][j]*a[j][index];
            proiz[index]%=2;*/

        }
        if(t==2)
        {
            sc(index),index--;
            sum=0;
            proiz[index]=0;
            FOR(j,0,n)
            {
                if(j!=index)
                {
                    proiz[j]+=a[index][j]*a[j][index];
                    proiz[j]%=2;

                    a[j][index]=1-a[j][index];
                    proiz[j]+=a[index][j]*a[j][index];
                    proiz[j]%=2;

                    proiz[index]+=a[index][j]*a[j][index];
                    sum+=proiz[j];
                }
                else
                {
                    a[j][index]=1-a[j][index];
                    proiz[index]+=a[index][j]*a[j][index];
                }
            }
            sum+=proiz[index];
            sum%=2;

            /*proiz[index]=0;
            FOR(j,0,n)
                proiz[index]+=a[index][j]*a[j][index];
            proiz[index]%=2;*/
        }
        if(t==3)
        {
            //int sum=0;
            //FOR(j,0,n)sum+=proiz[j];
            //P("%d",sum%2);
            P("%d",sum);
        }
    }
    return 0;
}


#include <iostream>
#include <cstdio>

#include <vector>
#include <string>
#include <algorithm>
using namespace std;

//#define unlli unsigned long long int
//#define lli long long int

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FOR1(i,a,b) for(int i=a;i>=b;i--)

#define sc(n) scanf("%d",&n)

#define P printf
#define MK(u,v) make_pair(u,v)

#define maxN 1000

int a[maxN][maxN];
int proiz[maxN];

int main()
{
    int n;
    sc(n);

    FOR(i,0,n)
        FOR(j,0,n)
            sc(a[i][j]);

    FOR(i,0,n)
    {
        proiz[i]=0;
        FOR(j,0,n)
            proiz[i]+=a[i][j]*a[j][i];
        proiz[i]%=2;
    }

    int q,t,index;
    sc(q);
    FOR(i,0,q)
    {
        sc(t);
        if(t==1)
        {
            sc(index),index--;

            FOR(j,0,n)
            {
                if(j!=index)
                {
                    proiz[j]+=a[index][j]*a[j][index];
                    proiz[j]%=2;

                    a[index][j]=1-a[index][j];
                    proiz[j]+=a[index][j]*a[j][index];
                    proiz[j]%=2;
                }
                else a[index][j]=1-a[index][j];
            }

            proiz[index]=0;
            FOR(j,0,n)
                proiz[index]+=a[index][j]*a[j][index];
            proiz[index]%=2;

        }
        if(t==2)
        {
            sc(index),index--;

            FOR(j,0,n)
            {
                if(j!=index)
                {
                    proiz[j]+=a[index][j]*a[j][index];
                    proiz[j]%=2;

                    a[j][index]=1-a[j][index];
                    proiz[j]+=a[index][j]*a[j][index];
                    proiz[j]%=2;
                }
                else a[j][index]=1-a[j][index];
            }
            proiz[index]=0;
            FOR(j,0,n)
                proiz[index]+=a[index][j]*a[j][index];
            proiz[index]%=2;
        }
        if(t==3)
        {
            int sum=0;
            FOR(j,0,n)sum+=proiz[j];
            P("%d",sum%2);
        }
    }
    return 0;
}


#include <iostream>
#include <cstdio>

#include <vector>
#include <string>
#include <algorithm>
using namespace std;

//#define unlli unsigned long long int
//#define lli long long int

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FOR1(i,a,b) for(int i=a;i>=b;i--)

#define sc(n) scanf("%d",&n)

#define P printf
#define MK(u,v) make_pair(u,v)

#define maxN 1000

int a[maxN][maxN];
int proiz[maxN];

int main()
{
    int n;
    sc(n);

    FOR(i,0,n)
        FOR(j,0,n)
            sc(a[i][j]);

    int sum=0;
    FOR(i,0,n)
    {
        proiz[i]=a[i][i];
        sum+=proiz[i];
    }
    sum&=1;

    int q,t,index;
    sc(q);
    FOR(i,0,q)
    {
        sc(t);
        if(t==1||t==2)
        {
            sc(index),index--;
            sum+=proiz[index];
            proiz[index]=1-proiz[index];
            sum+=proiz[index];
            sum&=1;
        }
        else P("%d",sum);
    }
    return 0;
}


#include <iostream>
#include <cstdio>

#include <vector>
#include <string>
#include <algorithm>
using namespace std;

//#define unlli unsigned long long int
//#define lli long long int

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FOR1(i,a,b) for(int i=a;i>=b;i--)

#define sc(n) scanf("%d",&n)

#define P printf
#define MK(u,v) make_pair(u,v)

#define maxN 1000

int a[maxN][maxN];
int proiz[maxN];

int main()
{
    int n;
    sc(n);

    FOR(i,0,n)
        FOR(j,0,n)
            sc(a[i][j]);

    FOR(i,0,n)
    {
        proiz[i]=0;
        FOR(j,0,n)
            proiz[i]+=a[i][j]*a[j][i];
        proiz[i]%=2;
    }

    int q,t,index;
    sc(q);
    FOR(i,0,q)
    {
        sc(t);
        if(t==1)
        {
            sc(index),index--;

            proiz[index]=0;
            FOR(j,0,n)
            {
                if(j!=index)
                {
                    proiz[j]+=a[index][j]*a[j][index];
                    proiz[j]%=2;

                    a[index][j]=1-a[index][j];
                    proiz[j]+=a[index][j]*a[j][index];
                    proiz[j]%=2;

                    proiz[index]+=a[index][j]*a[j][index];
                }
                else
                {
                    a[index][j]=1-a[index][j];
                    proiz[index]+=a[index][j]*a[j][index];
                }
            }

            /*proiz[index]=0;
            FOR(j,0,n)
                proiz[index]+=a[index][j]*a[j][index];
            proiz[index]%=2;*/

        }
        if(t==2)
        {
            sc(index),index--;

            proiz[index]=0;
            FOR(j,0,n)
            {
                if(j!=index)
                {
                    proiz[j]+=a[index][j]*a[j][index];
                    proiz[j]%=2;

                    a[j][index]=1-a[j][index];
                    proiz[j]+=a[index][j]*a[j][index];
                    proiz[j]%=2;

                    proiz[index]+=a[index][j]*a[j][index];
                }
                else
                {
                    a[j][index]=1-a[j][index];
                    proiz[index]+=a[index][j]*a[j][index];
                }
            }
            /*proiz[index]=0;
            FOR(j,0,n)
                proiz[index]+=a[index][j]*a[j][index];
            proiz[index]%=2;*/
        }
        if(t==3)
        {
            int sum=0;
            FOR(j,0,n)sum+=proiz[j];
            P("%d",sum%2);
        }
    }
    return 0;
}


#include <iostream>
#include <cstdio>

#include <vector>
#include <string>
#include <algorithm>
using namespace std;

//#define unlli unsigned long long int
//#define lli long long int

#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FOR1(i,a,b) for(int i=a;i>=b;i--)

#define sc(n) scanf("%d",&n)

#define P printf
#define MK(u,v) make_pair(u,v)

#define maxN 1000

int a[maxN][maxN];
int proiz[maxN];

int main()
{
	int n;
	sc(n);
	int sum=0;

	FOR(i,0,n)
		FOR(j,0,n)
			sc(a[i][j]);

	FOR(i,0,n)
	{
		proiz[i]=0;
		FOR(j,0,n)
			proiz[i]+=a[i][j]*a[j][i];
		proiz[i]&=1;
		sum+=proiz[i];
	}
	sum&=1;

	int q,t,index;
	sc(q);
	FOR(i,0,q)
	{
		sc(t);
		if(t==1||t==2)
		{
			sc(index),index--;
			sum=0;
			proiz[index]=0;
			FOR(j,0,n)
			{
				if(j!=index)
				{
					proiz[j]+=a[index][j]*a[j][index];
					proiz[j]&=1;

					if(t==1)a[index][j]=1-a[index][j];
					else a[j][index]=1-a[j][index];

					proiz[j]+=a[index][j]*a[j][index];
					proiz[j]&=1;

					sum+=proiz[j];
				}
				else
					if(t==1)a[index][j]=1-a[index][j];
					else a[j][index]=1-a[j][index];

				proiz[index]+=a[index][j]*a[j][index];
			}
			sum+=proiz[index];
			sum&=1;
		}
		else P("%d",sum);
	}
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <list>
#include <stack>
#include <bitset>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define rep(i,a) for(int i=0; i<a;i++)
#define forn(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a; i>=b; i--)
#define mp make_pair
#define ll long long unsigned
#define sz(x) (x).size()
#define pb push_back

int main() {
	int count, queries;

	scanf("%d", &count);

	int matrix[count][count];

	rep(i, count) rep(j, count) scanf("%d", &(matrix[i][j]));
	int sum = 0;
	rep(i, count) sum += matrix[i][i];
	int delta = 0;

	scanf("%d", &queries);
	while(queries--) {
		int type;
		scanf("%d", &type);

		if (type == 1 || type == 2) {
			int dest;
			scanf("%d", &dest);
			delta++;
			delta %= 2;
		} else {
			printf("%d", sum + delta);
		}
	}

	printf("\n");

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <list>
#include <stack>
#include <bitset>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define rep(i,a) for(int i=0; i<a;i++)
#define forn(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a; i>=b; i--)
#define mp make_pair
#define ll long long unsigned
#define sz(x) (x).size()
#define pb push_back

int main() {
	int count, queries;

	scanf("%d", &count);

	int matrix[count][count];

	rep(i, count) rep(j, count) scanf("%d", &(matrix[i][j]));

	scanf("%d", &queries);
	while(queries--) {
		int type;
		scanf("%d", &type);

		if (type == 1 || type == 2) {
			int dest;
			scanf("%d", &dest);
			dest--;
			(matrix[dest][dest])++;
			matrix[dest][dest] %= 2;
		} else {
			int sum = 0;
			rep(i, count) sum += matrix[i][i];
			sum %= 2;
			printf("%d", sum);
		}
	}

	printf("\n");

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <list>
#include <stack>
#include <bitset>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define rep(i,a) for(int i=0; i<a;i++)
#define forn(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a; i>=b; i--)
#define mp make_pair
#define ll long long unsigned
#define sz(x) (x).size()
#define pb push_back

int main() {
	int count, queries;

	scanf("%d", &count);

	int matrix[count][count];

	rep(i, count) rep(j, count) scanf("%d", &(matrix[i][j]));
	int sum = 0;
	rep(i, count) sum += matrix[i][i];
	sum %= 2;
	int delta = 0;

	scanf("%d", &queries);
	while(queries--) {
		int type;
		scanf("%d", &type);

		if (type == 1 || type == 2) {
			int dest;
			scanf("%d", &dest);
			delta++;
			delta %= 2;
		} else {
			printf("%d", (sum + delta) % 2);
		}
	}

	printf("\n");

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <list>
#include <stack>
#include <bitset>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define rep(i,a) for(int i=0; i<a;i++)
#define forn(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a; i>=b; i--)
#define mp make_pair
#define ll long long unsigned
#define sz(x) (x).size()
#define pb push_back

int main() {
	int count, queries;

	scanf("%d", &count);

	int matrix[count][count];

	rep(i, count) rep(j, count) scanf("%d", &(matrix[i][j]));
	int sum = 0;
	rep(i, count) sum += matrix[i][i];
	sum %= 2;
	int delta = 0;

	scanf("%d", &queries);
	while(queries--) {
		int type;
		scanf("%d", &type);

		if (type == 1 || type == 2) {
			int dest;
			scanf("%d", &dest);
			delta++;
			delta %= 2;
		} else {
			printf("%d", sum + delta);
		}
	}

	printf("\n");

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <list>
#include <stack>
#include <bitset>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define rep(i,a) for(int i=0; i<a;i++)
#define forn(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a; i>=b; i--)
#define mp make_pair
#define ll long long unsigned
#define sz(x) (x).size()
#define pb push_back

int main() {
	int count, queries;

	scanf("%d", &count);

	int matrix[count][count];

	rep(i, count) rep(j, count) cin >> matrix[i][j];

	scanf("%d", &queries);
	while(queries--) {
		int type;
		scanf("%d", &type);

		if (type == 1 || type == 2) {
			int dest;
			scanf("%d", &dest);
			dest--;
			(matrix[dest][dest])++;
			matrix[dest][dest] %= 2;
		} else {
			int sum = 0;
			rep(i, count) sum += matrix[i][i];
			sum %= 2;
			printf("%d", sum);
		}
	}

	printf("\n");

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <list>
#include <stack>
#include <bitset>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
using namespace std;

#define rep(i,a) for(int i=0; i<a;i++)
#define forn(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a; i>=b; i--)
#define mp make_pair
#define ll long long unsigned
#define sz(x) (x).size()
#define pb push_back

int main() {
	int count, queries;

	cin >> count;

	int matrix[count][count];

	rep(i, count) rep(j, count) cin >> matrix[i][j];

	cin >> queries;
	while(queries--) {
		int type;
		cin >> type;

		if (type == 1 || type == 2) {
			int dest;
			cin >> dest;
			dest--;
			(matrix[dest][dest])++;
			matrix[dest][dest] %= 2;
		} else {
			int sum = 0;
			rep(i, count) sum += matrix[i][i];
			sum %= 2;
			cout << sum;
		}
	}

	cout << endl;

	return 0;
}


//
//  main.cpp
//  C
//
//  Created by H7 on 3/22/14.
//  Copyright (c) 2014 H7. All rights reserved.
//

#include <iostream>
//#include <cstdio>

using namespace std;

const int M = 1010;

int a[M][M], n;

int ans;

void solve()
{
    cin >> n;
//    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> a[i][j];
//            scanf("%d", &a[i][j]);
        }
    }

    for (int i = 0; i < n; ++i) {
        ans ^= a[i][i];
    }

    int q;
    cin >> q;
//    scanf("%d", &q);
    int t;
    while (q--) {
        cin >> t;
//        scanf("%d", &t);
        if (t == 3) {
            cout << ans;
//            printf("%d", ans);
        } else {
            cin >> t;
//            scanf("%d", &t);
            ans = 1 - ans;
        }
    }

}

int main(int argc, const char * argv[])
{
    ios_base::sync_with_stdio(false);
    solve();
    return 0;
}


//
//  main.cpp
//  C
//
//  Created by H7 on 3/22/14.
//  Copyright (c) 2014 H7. All rights reserved.
//

//#include <iostream>
#include <cstdio>

using namespace std;

const int M = 1010;

int a[M][M], n;

int ans;

void solve()
{
    cin >> n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
//            cin >> a[i][j];
            scanf("%d", &a[i][j]);
        }
    }

    for (int i = 0; i < n; ++i) {
        ans ^= a[i][i];
    }

    int q;
//    cin >> q;
    scanf("%d", &q);
    int t;
    while (q--) {
//        cin >> t;
        scanf("%d", &t);
        if (t == 3) {
//            cout << ans;
            printf("%d", ans);
        } else {
//            cin >> t;
            scanf("%d", &t);
            ans = 1 - ans;
        }
    }

}

int main(int argc, const char * argv[])
{
//    ios_base::sync_with_stdio(false);
    solve();
    return 0;
}


//
//  main.cpp
//  C
//
//  Created by H7 on 3/22/14.
//  Copyright (c) 2014 H7. All rights reserved.
//

#include <iostream>
#include <cstdio>

using namespace std;

const int M = 1010;

int a[M][M], n;

int ans;

void solve()
{
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
//            cin >> a[i][j];
            scanf("%d", &a[i][j]);
        }
    }

    for (int i = 0; i < n; ++i) {
        ans ^= a[i][i];
    }

    int q;
    cin >> q;
    int t;
    while (q--) {
        cin >> t;
        if (t == 3) {
//            cout << ans;
            printf("%d", ans);
        } else {
            cin >> t;
            ans = 1 - ans;
        }
    }

}

int main(int argc, const char * argv[])
{
//    ios_base::sync_with_stdio(false);
    solve();
    return 0;
}


//
//  main.cpp
//  C
//
//  Created by H7 on 3/22/14.
//  Copyright (c) 2014 H7. All rights reserved.
//

#include <iostream>
#include <cstdio>

using namespace std;

const int M = 1010;

int a[M][M], n;

int ans;

void calR(int i)
{
    int b = a[i][i];
    for (int j = 0; j < n; ++j) {
        a[i][j] = 1 - a[i][j];
    }
    if ((b ^ a[i][i]) != 0) {
        ans = 1 - ans;
    }

}

void calC(int j)
{
    int b = a[j][j];
    for (int i = 0; i < n; ++i) {
        a[i][j] = 1 - a[i][j];
    }
    if ((b ^ a[j][j]) != 0) {
        ans = 1 - ans;
    }
}

void solve()
{
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            scanf("%d", &a[i][j]);
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            ans += a[i][j] + a[j][i];
        }
    }

    for (int i = 0; i < n; ++i) {
        ans -= a[i][i];
    }

    ans %= 2;

    int q;
    cin >> q;
    int t, e;
    while (q--) {
        cin >> t;
        switch (t) {
            case 1:
                cin >> e;
                calR(e - 1);
                break;
            case 2:
                cin >> e;
                calC(e - 1);
                break;
            default:
                printf("%d", ans);
                break;
        }
    }

}

int main(int argc, const char * argv[])
{
//    ios_base::sync_with_stdio(false);
    solve();
    return 0;
}


//
//  main.cpp
//  C
//
//  Created by H7 on 3/22/14.
//  Copyright (c) 2014 H7. All rights reserved.
//

//#include <iostream>
#include <cstdio>

using namespace std;

const int M = 1010;

int a[M][M], n;

int ans;

void solve()
{
//    cin >> n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
//            cin >> a[i][j];
            scanf("%d", &a[i][j]);
        }
    }

    for (int i = 0; i < n; ++i) {
        ans ^= a[i][i];
    }

    int q;
//    cin >> q;
    scanf("%d", &q);
    int t;
    while (q--) {
//        cin >> t;
        scanf("%d", &t);
        if (t == 3) {
//            cout << ans;
            printf("%d", ans);
        } else {
//            cin >> t;
            scanf("%d", &t);
            ans = 1 - ans;
        }
    }

}

int main(int argc, const char * argv[])
{
//    ios_base::sync_with_stdio(false);
    solve();
    return 0;
}


//
//  main.cpp
//  C
//
//  Created by H7 on 3/22/14.
//  Copyright (c) 2014 H7. All rights reserved.
//

#include <iostream>

using namespace std;

const int M = 1010;

int a[M][M], n;

int ans;

void calR(int i)
{
    int b = a[i][i];
    for (int j = 0; j < n; ++j) {
        a[i][j] = 1 - a[i][j];
    }
    if ((b ^ a[i][i]) != 0) {
        ans = 1 - ans;
    }

}

void calC(int j)
{
    int b = a[j][j];
    for (int i = 0; i < n; ++i) {
        a[i][j] = 1 - a[i][j];
    }
    if ((b ^ a[j][j]) != 0) {
        ans = 1 - ans;
    }
}

void solve()
{
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> a[i][j];
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            ans += a[i][j] + a[j][i];
        }
    }

    for (int i = 0; i < n; ++i) {
        ans -= a[i][i];
    }

    ans %= 2;

    int q;
    cin >> q;
    int t, e;
    while (q--) {
        cin >> t;
        switch (t) {
            case 1:
                cin >> e;
                calR(e - 1);
                break;
            case 2:
                cin >> e;
                calC(e - 1);
                break;
            default:
                cout << ans;
                break;
        }
    }

}

int main(int argc, const char * argv[])
{
    ios_base::sync_with_stdio(false);
    solve();
    return 0;
}


//
//  main.cpp
//  C
//
//  Created by H7 on 3/22/14.
//  Copyright (c) 2014 H7. All rights reserved.
//

#include <iostream>
#include <cstdio>

using namespace std;

const int M = 1010;

int a[M][M], n;

int ans;

void calR(int i)
{
    int b = a[i][i];
    for (int j = 0; j < n; ++j) {
        a[i][j] = 1 - a[i][j];
    }
    if ((b ^ a[i][i]) != 0) {
        ans = 1 - ans;
    }

}

void calC(int j)
{
    int b = a[j][j];
    for (int i = 0; i < n; ++i) {
        a[i][j] = 1 - a[i][j];
    }
    if ((b ^ a[j][j]) != 0) {
        ans = 1 - ans;
    }
}

void solve()
{
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            scanf("%d", &a[i][j]);
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            ans += a[i][j] + a[j][i];
        }
    }

    for (int i = 0; i < n; ++i) {
        ans -= a[i][i];
    }

    ans %= 2;

    int q;
    cin >> q;
    int t, e;
    while (q--) {
        cin >> t;
        switch (t) {
            case 1:
                cin >> e;
                calR(e - 1);
                break;
            case 2:
                cin >> e;
                calC(e - 1);
                break;
            default:
                cout << ans;
                break;
        }
    }

}

int main(int argc, const char * argv[])
{
    ios_base::sync_with_stdio(false);
    solve();
    return 0;
}


//
//  main.cpp
//  C
//
//  Created by H7 on 3/22/14.
//  Copyright (c) 2014 H7. All rights reserved.
//

#include <iostream>
#include <cstdio>

using namespace std;

const int M = 1010;

int a[M][M], n;

int ans;

void solve()
{
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> a[i][j];
        }
    }

    for (int i = 0; i < n; ++i) {
        ans ^= a[i][i];
    }

    int q;
    cin >> q;
    int t;
    while (q--) {
        cin >> t;
        if (t == 3) {
            cout << ans;
        } else {
            cin >> t;
            ans = 1 - ans;
        }
    }

}

int main(int argc, const char * argv[])
{
    ios_base::sync_with_stdio(false);
    solve();
    return 0;
}


//
//  main.cpp
//  C
//
//  Created by H7 on 3/22/14.
//  Copyright (c) 2014 H7. All rights reserved.
//

#include <iostream>

using namespace std;

const int M = 1010;

int a[M][M], n;

int ans;

void calR(int i)
{
    for (int j = 0; j < n; ++j) {
        a[i][j] = 1 - a[i][j];
    }
    if (a[i][i] == 1) {
        ans = 1 - ans;
    }

}

void calC(int j)
{
    for (int i = 0; i < n; ++i) {
        a[i][j] = 1 - a[i][j];
    }
    if (a[j][j] == 1) {
        ans = 1 - ans;
    }
}

void solve()
{
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> a[i][j];
        }
    }

    int q;
    cin >> q;
    int t, e;
    while (q--) {
        cin >> t;
        switch (t) {
            case 1:
                cin >> e;
                calR(e - 1);
                break;
            case 2:
                cin >> e;
                calC(e - 1);
                break;
            default:
                cout << ans;
                break;
        }
    }

}

int main(int argc, const char * argv[])
{
    ios_base::sync_with_stdio(false);
    solve();
    return 0;
}


//
//  main.cpp
//  C
//
//  Created by H7 on 3/22/14.
//  Copyright (c) 2014 H7. All rights reserved.
//

#include <iostream>
//#include <cstdio>

using namespace std;

const int M = 1010;

int a[M][M], n;

int ans;

void solve()
{
    cin >> n;
//    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> a[i][j];
            scanf("%d", &a[i][j]);
        }
    }

    for (int i = 0; i < n; ++i) {
        ans ^= a[i][i];
    }

    int q;
    cin >> q;
//    scanf("%d", &q);
    int t;
    while (q--) {
        cin >> t;
//        scanf("%d", &t);
        if (t == 3) {
            cout << ans;
//            printf("%d", ans);
        } else {
            cin >> t;
//            scanf("%d", &t);
            ans = 1 - ans;
        }
    }

}

int main(int argc, const char * argv[])
{
    ios_base::sync_with_stdio(false);
    solve();
    return 0;
}


//
//  main.cpp
//  C
//
//  Created by H7 on 3/22/14.
//  Copyright (c) 2014 H7. All rights reserved.
//

#include <iostream>
#include <cstdio>

using namespace std;

const int M = 1010;

int a[M][M], n;

int ans;

void solve()
{
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
//            cin >> a[i][j];
            scanf("%d", &a[i][j]);
        }
    }

    for (int i = 0; i < n; ++i) {
        ans ^= a[i][i];
    }

    int q;
    cin >> q;
    int t;
    while (q--) {
        cin >> t;
        if (t == 3) {
//            cout << ans;
            printf("%d", ans);
        } else {
//            cin >> t;
            scanf("%d", &t);
            ans = 1 - ans;
        }
    }

}

int main(int argc, const char * argv[])
{
//    ios_base::sync_with_stdio(false);
    solve();
    return 0;
}


#include <stdio.h>
int main ()
{
    int i, j, n, q, k, m , s, sum;
    sum = 0;

    scanf ("%d ", &n);
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
        {
            scanf ("%d", &s);
            if (i == j)
                sum += s;

    scanf ("%d ", &q);
    for ( k =0; k < q; k++)
    {
        scanf ("%d ", &m);
        if (m == 3)
            printf ("%d", sum % 2);
        else
        {
            scanf ("%d ", &s);
            sum ^= 1;
        }
    }
}


#include <stdio.h>
int main ()
{
    int i, j, n, q, k, m , s, sum;
    sum = 0;

    scanf ("%d ", &n);
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
        {
            scanf ("%d", &s);
            if (i == j)
                sum += s;
        }

    sum %= 2;
    scanf ("%d ", &q);
    for ( k =0; k < q; k++)
    {
        scanf ("%d ", &m);
        if (m == 3)
            printf ("%d", sum % 2);
        else
        {
            scanf ("%d ", &s);
            sum ^= 1;
        }
    }
}


#include<iostream>
using namespace std;
int main()
{
	int n,sum=0,q;
	cin>>n;
	int a[n][n];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>a[i][j];
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
		    sum+=a[j][i]*a[i][j];
		}
	}sum%=2;cin>>q;
	while(q--)
	{
		int p,m;
		cin>>p;
		if(p==3)cout<<sum<<endl;
		else{
			cin>>m;
			sum=1-sum;
		    }
	}

}


#include<iostream>
using namespace std;
int main()
{
	int n,sum=0,q,top=-1;
	cin>>n;
	int a[n][n];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>a[i][j];
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
		    sum+=a[j][i]*a[i][j];
		}
	}sum%=2;cin>>q;int b[q];
	while(q--)
	{
		int p,m;
		cin>>p;
		if(p==3)b[++top]=sum;
		else{
			cin>>m;
			sum=1-sum;
		    }
	}
	for(int i=0;i<=top;i++)cout<<b[i];
}


#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	int n,sum=0,q,top=-1;
	cin>>n;
	int a[n][n];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			scanf("%d",&a[i][j]);
		}
	}
	for(int i=0;i<n;i++)sum+=a[i][i];
    for(int i=0;i<n;i++)
    {
    	for(int j=i+1;j<n;j++)sum+=(2*a[i][j]*a[j][i]);
	}
	sum%=2;cin>>q;int b[q];
	while(q--)
	{
		int p,m;
		scanf("%d",&p);
		if(p==3)b[++top]=sum;
		else{
			scanf("%d",&m);
			sum=1-sum;
		    }
	}
	for(int i=0;i<=top;i++)printf("%d",b[i]);
}


#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	int n,sum=0,q,top=-1;
	cin>>n;
	int a[n][n];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			scanf("%d",&a[i][j]);
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
		    sum+=a[j][i]*a[i][j];
		}
	}sum%=2;cin>>q;int b[q];
	while(q--)
	{
		int p,m;
		cin>>p;
		if(p==3)b[++top]=sum;
		else{
			cin>>m;
			sum=1-sum;
		    }
	}
	for(int i=0;i<=top;i++)cout<<b[i];
}


#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;

using lli = long long int;

int tNum;
int n, d[100001];
int maxNum;
vector<int> maxs;

int main(void)
{
    // freopen("input.txt", "r", stdin);

    scanf("%d", &tNum);
    for(int tt = 0; tt < tNum; tt++) {
        maxs.clear();
        maxNum = 0;

        scanf("%d", &n);

        for(int i = 1; i <= n; i++) {
            d[i] = 0;
        }

        for(int i = 0; i < n; i++) {
            int a;
            scanf("%d", &a);

            d[a]++;
            if(d[a] == maxNum) maxs.push_back(a);
            else if(d[a] > maxNum) {
                maxNum = d[a];
                maxs.clear();
                maxs.push_back(a);
            }
        }

        int res, mod;

        if(maxNum == 2) {
            res = n - 1 - maxs.size();
        } else {
            int t = maxs.size() - 1;

            res = (n - maxNum - t) / (maxNum - 1);
            mod = (n - maxNum - t) % (maxNum - 1);
            // if(maxs.size() - 1 > mod) res--;
        }

        printf("%d\n", res);
    }

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment (linker, "/STACK:10000000000")
//#pragma GCC optimize("O3")
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>

using namespace std;

typedef long long ll;
typedef long double ld;

#define fr(i, l, r) for(int i = l; i < r; ++i)
#define fr_sl(i, l, r) for(auto i = l; i != r; ++i)
#define elif else if
#define mp make_pair
#define pb push_back
//#define or ||
//#define and &&

ll inf = 1000000007;
bool pr, pr2;
int n, t, v, mx = 0, num;
char c;
int mas[200010];
map <int, int> sl;
map <int, int> sl2;

int main() {
	//freopen("input.txt", "r", stdin);
   // freopen("output.txt", "w", stdout);
	scanf("%d", &t);
	fr(l, 0, t) {
		scanf("%d", &n);
		sl2.clear();
		sl = sl2;
		fr(i, 0, n) {
			scanf("%d", &v);
			++sl2[v];
		}
		fr_sl(i, sl2.begin(), sl2.end()) {
			++sl[(*i).second];
		}
		mx = 0;
		auto a = sl.end();
		--a;
		printf("%d\n", (n - (*a).first * (*a).second) / ((*a).first - 1) + ((*a).second - 1));
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>

using namespace std;

int main(void) {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	const int INF = 987654321;
	int t, n, tmp, Max, Maxcnt;
	cin >> t;
	while (t--) {
		cin >> n;

		vector<int> arr;
		for (int i = 0; i < n; i++) {
			cin >> tmp;
			arr.push_back(tmp);
		}

		sort(arr.begin(), arr.end());

		Max = 0; Maxcnt = 0;
		int i = 1, cnt = 1;
		while (i < n) {
			if (arr[i] == arr[i - 1]) {
				cnt++;
				if (i == n - 1)
					Max = max(Max, cnt);
			}
			else {
				Max = max(Max, cnt);
				cnt = 1;
			}
			i++;
		}

		i = 1, cnt = 1;
		while (i < n) {
			if (arr[i] == arr[i - 1]) {
				cnt++;
				if (i == n - 1)
					if (cnt == Max)
						Maxcnt++;
			}
			else {
				if (cnt == Max)
					Maxcnt++;
				cnt = 1;
			}
			i++;
		}
		//cout << Max << ' ' << Maxcnt << ' ';
		if (Maxcnt == 1)
			cout << (n - 1) / (Max - 1) - 1 << '\n';
		else
			cout << (n - Maxcnt) / (Max - 1) - 1 << '\n';

	}

	return 0;
}


using namespace std;
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <bitset>
#include <iterator>
#include <set>
#include <unordered_set>
#include <string>
#include <functional>
#include <limits.h>
//#include <bits/stdc++.h>
//#include <list>
#include <stack>
#include <stdio.h>
typedef signed long long ll; //watch out ll= int*int will not become ll, rather (ll)ini*int casting will work
#include <queue>
#include <math.h>
#include <deque>
#define ffor(i,n) for(int i = 0;i < (n); ++i)
#define all(v) v.begin(),v.end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vpii vector<pii>
//typedef long float lf;
#include <cmath>
#include <iomanip>
#include <numeric>
#include <stdio.h>
const long double PI = 3.141592653589793238L;
void pv(vector<int> a) {
	for (auto i : a)
		cout << i +1<< ' ';
	cout << endl;
}
int findd(map<int, int>& mapp, int x) {
	if (mapp.find(x) == mapp.end())
		return 0;
	else
		return mapp[x];
}
void add(map<int, int>& mapp, int x) {
	if (mapp.find(x) == mapp.end())
		mapp.insert({ x,0 });
	else
		mapp[x] = mapp[x] +1;
}
string nott(char a) {
	//cout << "YEAH";
	if (a == 'a')
		return "b";
	return "a";
}
string get_uni_char(int ind,int x, string prev) {
	if (ind < prev.size())
		return nott(prev[ind]);
	if (x % 2 == 0)
		return "a";
	return "b";
}
void dfs(vector<int>& top, vector<int>b, int i,
	vector<int> &explored) {
	explored[i] = 1;
	if (b[i] != -1 &&
		!explored[b[i]]) dfs(top, b, b[i], explored);
	top.push_back(i);

}
void topsort(vector<int>& top, vector<int>b, int n) {
	vector<int> explored(n);
	for (int i = 0; i < n; i++) {
		if (!explored[i])
			dfs(top, b, i, explored);
	}
}

const int Alp = 20;
bool findnew4(int &fours, vector<int> &freq) {
	for (int i = 0; i < freq.size(); i++)
		if (freq[i] >= 4) {
			freq[i] -= 4; fours = i;
			return true;
		}	fours = -1;

	return false;
}
bool findnew2(vector<int> twos, vector<int>& freq) {
	for (int i = 0; i < freq.size(); i++) {
		if (freq[i] >= 2) {
			freq[i] -= 2;
			twos.push_back(i); i--;
			if (twos.size() == 2)
				return true;
		}
	}
	twos = {};
	return false;
}
/*
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);

	int test=1;
	//cin >> test;
	while (test--)
	{
		int n, k,q, a, b;
		cin >> n;
		vector<int> freq(n+1);
		for (int i = 0; i < n; i++) {
			int inp; cin >> inp;
			freq[inp]++;
		}
		cin >> q;
		int fours = -1;
		findnew4(fours, freq);
		vector <int> twos = {};
		findnew2(twos, freq);
		for (int t = 0; t < q; t++) {
			char s; cin >> s;
			int x; cin >> x;
			if (s == '+') { freq[x]++;
			if (fours == -1 || twos.size() == 0) {

				if (findnew4(fours, freq) &&
					findnew2(twos, freq))
					cout << "YES \n";
				else cout << "NO" << endl;
			}
			if (fours != -1 || twos.size() == 2) {
				cout << "YES" << endl;
				continue;
			}
			}
			else {
				freq[x--];
				if (freq[x] < 0) {
					freq[fours] += 4;
					freq[twos[0]] += 2;
					freq[twos[1]] += 2;
					if (findnew4(fours, freq) &&
						findnew2(twos, freq))
						cout << "YES \n";
					else cout << "NO" << endl;
				}
				if (freq[x] >= 0) {
					if (fours != -1 || twos.size() == 2) {
						cout << "YES" << endl;
						continue;
					}
					cout << "NO" << endl;
				}
			};

		}

	}
}
*/

int main() {
	int test; cin >> test;
	while (test--) {
		int n; cin >> n;
		vector<int> a(n), freq(n+1);
		int maxfreq = 0;
		for (int i = 0; i < n; i++) {
			cin >> a[i]; freq[a[i]]++;
			maxfreq = max(maxfreq, freq[a[i]]);
		}
		int numelem = 0;
		for (auto i : freq)
			if (i == maxfreq)
				numelem++;
		cout << numelem+-1+
			(n - numelem * maxfreq) / (maxfreq - 1)<<
			endl;

	}
}


using namespace std;
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <bitset>
#include <iterator>
#include <set>
#include <unordered_set>
#include <string>
#include <functional>
#include <limits.h>
//#include <bits/stdc++.h>
//#include <list>
#include <stack>
#include <stdio.h>
typedef signed long long ll; //watch out ll= int*int will not become ll, rather (ll)ini*int casting will work
#include <queue>
#include <math.h>
#include <deque>
#define ffor(i,n) for(int i = 0;i < (n); ++i)
#define all(v) v.begin(),v.end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vpii vector<pii>
//typedef long float lf;
#include <cmath>
#include <iomanip>
#include <numeric>
#include <stdio.h>
const long double PI = 3.141592653589793238L;
void pv(vector<int> a) {
	for (auto i : a)
		cout << i + 1 << ' ';
	cout << endl;
}
int findd(map<int, int>& mapp, int x) {
	if (mapp.find(x) == mapp.end())
		return 0;
	else
		return mapp[x];
}
void add(map<int, int>& mapp, int x) {
	if (mapp.find(x) == mapp.end())
		mapp.insert({ x,0 });
	else
		mapp[x] = mapp[x] + 1;
}
string nott(char a) {
	//cout << "YEAH";
	if (a == 'a')
		return "b";
	return "a";
}
string get_uni_char(int ind, int x, string prev) {
	if (ind < prev.size())
		return nott(prev[ind]);
	if (x % 2 == 0)
		return "a";
	return "b";
}
void dfs(vector<int>& top, vector<int>b, int i,
	vector<int>& explored) {
	explored[i] = 1;
	if (b[i] != -1 &&
		!explored[b[i]]) dfs(top, b, b[i], explored);
	top.push_back(i);

}
void topsort(vector<int>& top, vector<int>b, int n) {
	vector<int> explored(n);
	for (int i = 0; i < n; i++) {
		if (!explored[i])
			dfs(top, b, i, explored);
	}
}
void solve(vvi edges, bool& ans, vi &p, vi &h,
	vi &sadd, int cur, int par) {

	for (auto i : edges[cur])
		if (i != par)
			solve(edges, ans, p, h,sadd, i, cur);

	int people = p[cur], sad=0, good=0;
	for (auto i : edges[cur])
		if (i != par) {
			people += p[i];
		}
	//cout << cur << ' '<<people << ' ' << h[cur] << endl;
	if ((people + h[cur]) % 2 !=0) {
		ans = false;
	}

	else { good = (h[cur] + people)/2; }
	sad = people - good;
	sadd[cur] = sad;
	if (sad < 0 || good < 0)
		ans = false;

	sad = 0;
	for (auto i : edges[cur])
		if (i != par)
			sad += sadd[i];
	if (sadd[cur] > p[cur] + sad)
		ans = false;
	p[cur] = people;
}
int main() {
	int test; cin >> test;
	while (test-- != 0) {
		int n;  cin >> n;
		vector<int> p(n), freq(n+2);
		int maxx = 0;
		for (int i = 0; i < n; i++) {
			cin >> p[i]; freq[p[i]]++;
			maxx = max(maxx, freq[p[i]]);
		}
		//cout << "yeah" << maxx<<' ';
		int cnt = 0;
		for (auto i : freq)
			if (i == maxx)
				cnt++;
		//cout << cnt << "cnt";
		cout << (n-cnt*maxx)/(maxx-1)+
			cnt-1<<endl;

	}
}


//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("unroll-loops")
/*#pragma GCC optimize("section-anchors")
#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
#pragma GCC optimize("vpt")
#pragma GCC optimize("rename-registers")
#pragma GCC optimize("move-loop-invariants")
#pragma GCC optimize("unswitch-loops")
#pragma GCC optimize("function-sections")
#pragma GCC optimize("data-sections")
#pragma GCC optimize("branch-target-load-optimize")
#pragma GCC optimize("branch-target-load-optimize2")
#pragma GCC optimize("btr-bb-exclusive")*/
//#pragma comment(linker, "/STACK:367077216")
#define _CRT_SECURE_NO_WARNINGS
#include <chrono>
#include <set>
#include <map>
#include <deque>
#include <string>
#include <cstdint>
#include <cmath>
#include <queue>
#include <cassert>
#include <random>
#include <bitset>
#include <iomanip>
#include <numeric>
#include <time.h>//////////////
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
//++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++
//#define endl '\n'
#define mp make_pair
#define pbc push_back
#define pob pop_back()
#define empb emplace_back
#define queuel queue<long long>
#define sqr(a) ((a) * (a))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pin(p) cin >> p.first >> p.second;
#define uniq(a) sort(all(a));(a).resize(unique(all(a)) - a.begin());
#define rev(v) reverse(v.begin(), v.end());
#define sout(s, c) for (auto i : s) cout << i << c;
#define pout(p) cout << p.first << " " << p.second;
#define er(v, l, r) erase(v.begin() + l, v.begin() + r);
#define vin(v) for (ll i = 0; i < v.size(); ++i) cin >> v[i];
#define vout(v, c) for (int i = 0; i < v.size(); ++i) cout << v[i] << c;
#define pushi(v, a) for (int i = 0; i < a.size(); ++i) v.push_back(a[i]);
#define fastio() ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); srand(time(NULL))
#define dab(v) for(auto i:v)cout<<i<<' ';
#define sp system("pause")
//++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++
using namespace std;
//++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++
const ld EPS = 0.001;
const ld EPS2 = 1e-6;
const ld PI = acos(-1);
int mod = (int)998244353;
const int MOD7 = 1000000007;
const int MOD9 = 1000000009;
const int a228 = 18;
const int inf = 1e9;
const ll kekmod = 1791791791;
const ll bestmod = 1148822869;
const ll INF = 1e18;
vector<int> a;
int n;
bool check(int x)
{
	vector<int> cnt(n + 1);
	for (int i : a) cnt[i]++;
	set<pair<int, int>> kek;
	for (auto i : a)
	{
		kek.insert({ -cnt[i], i });
	}
	vector<int> last(n + 1);
	vector<int> ans(n + 1);
	for (int i = 0; i < n; ++i)
	{

		if (i >= x + 1 && cnt[ans[i-x - 1]])  kek.insert({ -cnt[ans[i - x - 1]], ans[i - x - 1] });
		if (!kek.size()) return 0;
		ans[i] = (kek.begin()->second);
		kek.erase(kek.begin());
		cnt[ans[i]] --;
	}
	return 1;
}
signed main()
{
	fastio();
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n;
		a.resize(n);

 		vin(a);
		int  l = -1, r = n + 1;
		while (r - l > 1)
		{
			int m = (l + r) / 2;
			if (check(m))
			{
				l = m;
			}
			else
			{
				r = m;
			}
		}
		cout << l << endl;
	}
	//sp;
}


#include <iostream>
#include <string>
#include <math.h>
#include <vector>
#include <algorithm>

using namespace std;

void Solve()
{
	int n; cin >> n;
	vector <int> arr(n + 1);
	for (int i = 1; i<=n; i++) arr[i] = 0;
	for (int i = 1; i<=n; i++)
	{
		int temp; cin >> temp;
		arr[temp]++;
	}
	sort(arr.begin(), arr.end());
	for (int i = 1; i<=n; i++) cout << arr[i] << " ";
	cout << endl;
	int cnt = 0;
	for (int i = 1 ; i< arr.size(); i++)
	{
		if (arr[i] == arr[arr.size() - 1])
		{
			arr[i]--;
			cnt++;
		}
	}
	if (n == 0) cout << 0 << endl;
	else
		cout << (n - cnt) / arr[arr.size() - 1] - 1 << endl;
}

int main()
{
	int num = 1;
	cin >> num;
	for (int i = 1; i<=num; i++)
	{
		Solve();
	}
	return 0;
}


#include <iostream>
#include <string>
#include <math.h>
#include <vector>
#include <algorithm>

using namespace std;

void Solve()
{
	int n; cin >> n;
	vector <int> arr(n + 1);
	for (int i = 1; i<=n; i++) arr[i] = 0;
	for (int i = 1; i<=n; i++)
	{
		int temp; cin >> temp;
		arr[temp]++;
	}
	sort(arr.begin(), arr.end());
//	for (int i = 1; i<=n; i++) cout << arr[i] << " ";
//	cout << endl;
	int cnt = 0;
	for (int i = 1 ; i< arr.size(); i++)
	{
		if (arr[i] == arr[arr.size() - 1])
		{
			arr[i]--;
			cnt++;
		}
	}
	if (n == 0) cout << 0 << endl;
	else
		cout << (n - cnt) / arr[arr.size() - 1] - 1 << endl;
}

int main()
{
	int num = 1;
	cin >> num;
	for (int i = 1; i<=num; i++)
	{
		Solve();
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<cmath>
#include<functional>
#include<string>
#include<vector>
#include<cstring>
#include<utility>
#include<set>
#include<map>

#define ll long long
#define ull unsigned long long
#define MOD 1000000007
#define MAX 2e9
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()
const double PI = acos(-1);

using namespace std;
int A[100001];
int main(void) {
	ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	//	freopen("input.txt", "r", stdin);

	int T; cin >> T;
	while (T--) {
		memset(A, 0, sizeof(A));
		int n; cin >> n;
		for (int i = 1; i <= n; i++) {
			int a; cin >> a;
			A[a]++;
		}
		sort(A + 1, A + n + 1, greater<int>());
		int idx = n;
		for (int i = 2; i <= n; i++) {
			if (A[i] != A[i - 1]) {
				idx = i - 1;
				break;
			}
		}
		n -= (idx-1);
		if (A[idx] - 2 == 0) cout << n-2 << '\n';
		else {
			cout << (n-2 - (A[idx]-2)) / (A[idx] - 1) << '\n';
		}
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<string>
#include<cmath>
#include<algorithm>
#include<time.h>
#include<set>
#include<map>
#include<iomanip>
#include<queue>
using namespace std;
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
typedef unsigned long long ll;
void solve() {
	int t; cin >> t;
	for (int t1 = 0; t1 < t; t1++) {
		int n; cin >> n;
		vector<int>a(n);
		for (int i = 0; i < n; i++) {
			int b; cin >> b;
			a[b - 1]++;
		}
		set<pair<int, int>>y, y1;
		for (int i = 0; i < n; i++) {
			if (a[i] != 0)y.insert({ -a[i], i });
		}
		int L = -1, R = n;
		vector<int>b(n), h = a;
		while (L < R - 1) {
			h = a;
			y1 = y;
			int m = (L + R) / 2;
			for (int i = 0; i < n; i++) {
				if (i -m-1 >= 0 && h[b[i - m - 1]]!=0) {
					y1.insert({ -h[b[i - m - 1]], b[i - m - 1] });
				}
				if (y1.size() == 0) {
					R = m;
					goto to;
				}
				b[i] = y1.begin()->second;
				h[b[i]]--;
				y1.erase(y1.begin());
			}
			L = m;
		to:
			int u;
		}
		cout << L<<'\n';
	}

}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
//	freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	solve();
}


#include<stdio.h>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=100010;
int cnt[maxn],m;
int main()
{
	int t,c,a,ans,n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		memset(cnt,0,sizeof(cnt));
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a);
			cnt[a]++;
		}
		c=0;
		for(int i=1;i<=n;i++)
			if(c<cnt[i])
			{
				c=cnt[i];
				m=1;
			}
			else if(c==cnt[i]) m++;
		ans=(n-m*c)/(c-1)+m-1;
		printf("%d\n",ans);
	}
	return 0;
}


#include<stdio.h>

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		int maxi=0,maxz=0;
		int a[100001]={0};
		int b[100001]={0};
		int v[100001]={0};
		int count=0;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			if(b[a[i]]==0)
			{
				count++;
				v[count]=a[i];
			}
			b[a[i]]++;
			if(b[a[i]]>maxi) maxi=b[a[i]];
			else if(b[a[i]]>maxz) maxz=b[a[i]];
		}
//	printf("%d  %d\n \n",maxi,maxz);
		int sum=0;
		for(int i=1;i<=count;i++)
		{
			if(maxi-maxz==0) b[v[i]]-=maxz-1;
			else if(maxi-maxz==1) b[v[i]]-=maxz;
			if(b[v[i]]>=0) sum++;
		}
		if(count!=1) printf("%d\n",sum-1);
		else printf("0\n");

	}
	return 0;
}


#include<stdio.h>

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		int maxi=0,maxz=0;
		int a[100001]={0};
		int b[100001]={0};
		int v[100001]={0};
		int count=0;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			if(b[a[i]]==0)
			{
				count++;
				v[count]=a[i];
			}
			b[a[i]]++;
			if(b[a[i]]>maxi) maxi=b[a[i]];
			else if(b[a[i]]>maxz) maxz=b[a[i]];
		}
//	printf("%d  %d\n \n",maxi,maxz);
		int sum=0;
		for(int i=1;i<=count;i++)
		{
			if(maxi-maxz<=1) b[v[i]]-=maxz;
			if(b[v[i]]>=0) sum++;
		}
		if(count!=1&&maxi-maxz==0) printf("%d\n",sum);
		else if(count!=1&&maxi-maxz==1) printf("%d\n",sum-1);
		else printf("0\n");

	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pil pair<int, ll>
#define pll pair<ll, ll>

int arr[100001];

int main()
{
	int t, n, temp, a, b, k;
	scanf("%d", &t);
	for (; t > 0; t--)
	{
		scanf("%d", &n);
		memset(arr, 0, sizeof(arr));
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &temp);
			arr[temp]++;
		}
		temp = 0;
		a = b = 0;
		for (int i = 1; i <= n; i++)
			if (arr[i] > a)
			{
				a = arr[i];
				b = 1;
			}
			else if (arr[i] == a)
				b++;
		k = n - (a * b);
		printf("%d\n", b - 1 + (k / (a - 1)));
	}
	return 0;
}


#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int a[100005];
int cmp(int x, int y)
{
	return x > y;
}
void rlmn()
{
	int n;
	scanf("%d", &n);
	int tmp;
	memset(a, 0, sizeof(a));
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &tmp);
		a[tmp]++;
	}
	sort(a, a + n + 1, cmp);
	tmp = a[0];
	int num = 0;
	for (int i = 0; i < n; i++)
	{
		if (tmp == a[i])
		{
			num++;
		}
		else
			break;
	}
int ans = (n - num) / (tmp - 1) - 1;
	printf("%d\n", ans);
}
int main()
{
	int T;
	scanf("%d", &T);
	while (T--)rlmn();
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cstring>
#include <string>
#include <queue>

using namespace std;

typedef int intor;
#define int long long
#define pii pair<long long, long long>
#define vi vector<long long>

#define NMAX (100*1000+10)
#define INF (1000*1000*1000*10)

int a[NMAX];

void solve()
{
	int n;
	cin >> n;
	memset(a, 0, sizeof(a[0]) * n + 2);
	for (int i = 0; i < n; i++)
	{
		int val;
		cin >> val;
		a[val]++;
	}
	sort(a, a + n + 1, [](int f, int s) {return s < f; });

	int equal = 1;
	int k = 1;
	while (a[k] == a[0])
	{
		equal++;
		k++;
	}

	int zan = (n - 1) / (a[0] - 1);
	if (equal == 1)
	{
		cout << zan - 1 << endl;
		return;
	}
	int ost = n - zan * (a[0] - 1) - 1;
	if (ost >= equal - 1)
	{
		cout << zan - 1 << endl;
		return;
	}
	else
	{
		while (ost < equal - 1)
		{
			zan--;
			ost = n - zan * (a[0] - 1) - 1;
		}
		cout << zan - 1 << endl;
		return;
	}

	return;
}

intor main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++)
	{
		solve();
	}
	return 0;
}


#include <iostream>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <assert.h>
#include <map>
#include <set>
#include <bitset>
#include <iomanip>
#include <stack>
// #include <unordered_map>
#include <cmath>
using namespace std;
// #pragma comment(linker, "/STACK:1024000000,1024000000")
// #define INF 0x7f7f7f7f  //2139062143
#define INF 0x3f3f3f3f //1061109567
#define INF2 2147483647
#define llINF 9223372036854775807
#define pi 3.14159265358979323846264338327950254
#define pb push_back
#define debug cout << "debug\n";
#define STDIN                      \
    freopen("in.txt", "r", stdin); \
    freopen("out.txt", "w", stdout);
#define IOS                      \
    ios::sync_with_stdio(false); \
    cin.tie(NULL);
#define scai(x) scanf("%d", &x)
#define sca2i(x, y) scanf("%d %d", &x, &y)
#define scaf(x) scanf("%lf", &x)
#define sca2f(x, y) scanf("%lf %lf", &x, &y)
#define For(m, n) for (int i = m; i < n; i++)
#define PII pair<int, int>
#define PLL pair<long, long>
#define ft first
#define sd second
#define pb push_back
// linux系统可用
// typedef __int128_t ll;
typedef long long ll;
// int size = 256 << 20; // 256MB/
// char *p = (char*)malloc(size) + size;
// __asm__("movl %0, %%esp\n" :: "r"(p) );
#define ST(x, y, z) sort(x + y, x + z);
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
#define rep(i, a, b) for (register int i = a; i <= b; i++)
#define rrep(i, a, b) for (register int i = a; i >= b; i--)
#define PIII pair<int, PII>
#define re read()
#define case  \
    int T;    \
    cin >> T; \
    rep(_, 1, T)
// #define LL long long
#define int ll
inline int read()
{
    char tempt = getchar();
    int x = 0, f = 0;
    while (tempt < '0' || tempt > '9')
        f |= tempt == '-', tempt = getchar();
    while (tempt >= '0' && tempt <= '9')
        x = x * 10 + tempt - '0', tempt = getchar();
    return f ? -x : x;
}
void write(register int x)
{
    if (x < 0)
    {
        putchar('-');
        x = -x;
    }
    if (x < 10)
        putchar(x + '0');
    else
    {
        write(x / 10);
        putchar(x % 10 + '0');
    }
}
inline void print(const register int x, const register char c = '\n')
{
    write(x);
    putchar(c);
}
inline int qmi(int m, int k, int p)
{
    int res = 1 % p, t = m;
    while (k)
    {
        if (k & 1)
            res = res * t % p;
        t = t * t % p;
        k >>= 1;
    }
    return res;
}

inline int qmi(int m, int k)
{
    int res = 1, t = m;
    while (k)
    {
        if (k & 1)
            res = res * t;
        t = t * t;
        k >>= 1;
    }
    return res;
}
inline bool ou(int x)
{
    return x % 2 == 0;
}

const int N = 1e5 + 10;
int a[N];
map<int,int> ma;
int32_t main()
{
    // STDIN;
    case{
        int n;
        cin >> n;
        int nmax = 0;
        ma.clear();
        rep(i,1, n) a[i] = re, ma[a[i]]++;

        int ncount = 0;
        for (auto &i : ma) nmax = max(nmax, i.second);
        for (auto &i :ma) if (i.second == nmax) ncount++;
        // cout << ncount << endl;
        int yu = n - ncount*nmax;
        // cout << yu << endl;

        if (yu == 0) cout << 0 << endl;
        else{
            cout << (yu)/(nmax-1) + ncount-1 << endl;
        }
    }
}


#include <iostream>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <assert.h>
#include <map>
#include <set>
#include <bitset>
#include <iomanip>
#include <stack>
// #include <unordered_map>
#include <cmath>
using namespace std;
// #pragma comment(linker, "/STACK:1024000000,1024000000")
// #define INF 0x7f7f7f7f  //2139062143
#define INF 0x3f3f3f3f //1061109567
#define INF2 2147483647
#define llINF 9223372036854775807
#define pi 3.14159265358979323846264338327950254
#define pb push_back
#define debug cout << "debug\n";
#define STDIN                      \
    freopen("in.txt", "r", stdin); \
    freopen("out.txt", "w", stdout);
#define IOS                      \
    ios::sync_with_stdio(false); \
    cin.tie(NULL);
#define scai(x) scanf("%d", &x)
#define sca2i(x, y) scanf("%d %d", &x, &y)
#define scaf(x) scanf("%lf", &x)
#define sca2f(x, y) scanf("%lf %lf", &x, &y)
#define For(m, n) for (int i = m; i < n; i++)
#define PII pair<int, int>
#define PLL pair<long, long>
#define ft first
#define sd second
#define pb push_back
// linux系统可用
// typedef __int128_t ll;
typedef long long ll;
// int size = 256 << 20; // 256MB/
// char *p = (char*)malloc(size) + size;
// __asm__("movl %0, %%esp\n" :: "r"(p) );
#define ST(x, y, z) sort(x + y, x + z);
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
#define rep(i, a, b) for (register int i = a; i <= b; i++)
#define rrep(i, a, b) for (register int i = a; i >= b; i--)
#define PIII pair<int, PII>
#define re read()
#define case  \
    int T;    \
    cin >> T; \
    rep(_, 1, T)
// #define LL long long
#define int ll
inline int read()
{
    char tempt = getchar();
    int x = 0, f = 0;
    while (tempt < '0' || tempt > '9')
        f |= tempt == '-', tempt = getchar();
    while (tempt >= '0' && tempt <= '9')
        x = x * 10 + tempt - '0', tempt = getchar();
    return f ? -x : x;
}
void write(register int x)
{
    if (x < 0)
    {
        putchar('-');
        x = -x;
    }
    if (x < 10)
        putchar(x + '0');
    else
    {
        write(x / 10);
        putchar(x % 10 + '0');
    }
}
inline void print(const register int x, const register char c = '\n')
{
    write(x);
    putchar(c);
}
inline int qmi(int m, int k, int p)
{
    int res = 1 % p, t = m;
    while (k)
    {
        if (k & 1)
            res = res * t % p;
        t = t * t % p;
        k >>= 1;
    }
    return res;
}

inline int qmi(int m, int k)
{
    int res = 1, t = m;
    while (k)
    {
        if (k & 1)
            res = res * t;
        t = t * t;
        k >>= 1;
    }
    return res;
}
inline bool ou(int x)
{
    return x % 2 == 0;
}

const int N = 1e5 + 10;
int a[N];
map<int,int> ma;
int32_t main()
{
    // STDIN;
    case{
        int n;
        cin >> n;
        int nmax = 0;
        ma.clear();
        rep(i,1, n) a[i] = re, ma[a[i]]++;

        int ncount = 0;
        for (auto &i : ma) nmax = max(nmax, i.second);
        for (auto &i :ma) if (i.second == nmax) ncount++;
        // cout << nmax << endl;
        // cout << ncount << endl;
        int yu = n - ncount*nmax;
        // cout << yu << endl;

            cout << (yu)/(nmax-1) + ncount-1 << endl;

    }
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector <pair<long long,long long> > vec;
long long n;
bool possible(long long x) {
	long long Size = vec.size();
	long long cnt = vec[0].second;
	if (x*(cnt - 1) + cnt > n) return false;
	if (n - (x*(cnt - 1) + cnt) < Size - 1) return false;
	return true;
}
int main() {
	long long t;
	cin >> t;
	while (t--) {
		vec.clear();
		cin >> n;
		vector <long long> arr(n);
		long long M = 0;
		vector <long long> cnt(long long (1e5) + 5);
		for (long long i = 0; i < n; i++) {
			cin >> arr[i];
			cnt[arr[i]]++;
			M = max(cnt[arr[i]], M);
		}

		for (auto u : arr) {
			if (cnt[u] == M) {
				vec.push_back({ u,cnt[u] });
				cnt[u] = 0;

			}
		}
		//cout << M << " " << vec.size() << "\n";

		long long l = 0, r = 1e5;
		long long ans;
		while(l <= r) {
			long long mid = (l + r) / 2;
			//cout << mid << "\n";
			if (possible(mid)) {
				ans = mid;
				l = mid + 1;
			}
			else {
				r = mid - 1;
			}
		}

		cout << ans << "\n";

	}
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include<algorithm>
#include<math.h>
#include<string>
#include <set>
#include <map>
using namespace std;
int t, n, m, p, q, a[200001], su;
string s[2001];
map<int, int>ma, mm;
int main()
{
	cin >> t;
	while (t--)
	{
		m = 0;
		cin >> n;
		for (int i = 0; i <= n; i++)
			ma[i] = mm[i] = 0;
		for (int i = 0; i < n; i++)
			scanf("%d", &p), ma[p]++, m = max(m, ma[p]);
		su = 0;
		for (int i = 0; i <= n; i++)
			su += ma[i] == m;
		printf("%d\n", (n - su) / (m - 1) - 1);
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

int T;
long long n;
long long cake[100001];
long long res = 0;

long long maxCake = 0;
vector<int> modecake; // ���� ���� ������ ���� ���
long long rest = 0;

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> T;
    while (T--) {
        modecake.clear();
        maxCake = 0;
        rest = 0;
        res = 0;
        for (auto& next : cake) next = 0;

        cin >> n;
        for (int i = 0; i < n; ++i) {
            int input;
            cin >> input;
            cake[input]++;

            if (cake[input] > maxCake) {
                modecake.clear();
                maxCake = cake[input];
                modecake.push_back(input);
            }
            else if (cake[input] == maxCake) {
                modecake.push_back(input);
            }
        }

        if (n == 2) {
            cout << 0 << "\n";
            continue;
        }

        for (int i = 1; i <= 100000; ++i) {
            if (cake[i] < maxCake) {
                rest += cake[i];
            }
        }

        /*cout << "maxCake : " << maxCake << "\n";
        cout << "rest : " << rest << "\n";
        cout << "modeCake : ";
        for (int next : modecake) {
            cout << next << " ";
        }cout << "\n";*/

        if (modecake.size() == 1) {
            res = rest / (maxCake - 1);
        }
        else {
            res = rest;
            res += modecake.size() - 1;
        }

        cout << res << "\n";
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

int T;
long long n;
long long cake[100001];
long long res = 0;

long long maxCake = 0;
vector<int> modecake; // ���� ���� ������ ���� ���
long long rest = 0;

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> T;
    while (T--) {
        modecake.clear();
        maxCake = 0;
        rest = 0;
        res = 0;
        for (auto& next : cake) next = 0;

        cin >> n;
        for (int i = 0; i < n; ++i) {
            int input;
            cin >> input;
            cake[input]++;

            if (cake[input] > maxCake) {
                modecake.clear();
                maxCake = cake[input];
                modecake.push_back(input);
            }
            else if (cake[input] == maxCake) {
                modecake.push_back(input);
            }
        }

        if (n == 2) {
            cout << 0 << "\n";
            continue;
        }

        for (int i = 1; i <= 100000; ++i) {
            if (cake[i] < maxCake) {
                rest += cake[i];
            }
        }

        res = max(0, (int)modecake.size() - 1);
        res += rest / (maxCake - 1);

        /*
        if (modecake.size() == 1) {
            res = rest / (maxCake - 1);
        }
        else {
            res = rest;
            res += modecake.size() - 1;
        }
        */

        cout << res << "\n";
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

int T;
long long n;
long long cake[100001];
long long res = 0;

long long maxCake = 0;
vector<int> modecake; // ���� ���� ������ ���� ���
long long rest = 0;

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> T;
    while (T--) {
        modecake.clear();
        maxCake = 0;
        rest = 0;
        res = 0;
        for (auto& next : cake) next = 0;

        cin >> n;
        for (int i = 0; i < n; ++i) {
            int input;
            cin >> input;
            cake[input]++;

            if (cake[input] > maxCake) {
                modecake.clear();
                maxCake = cake[input];
                modecake.push_back(input);
            }
            else if (cake[input] == maxCake) {
                modecake.push_back(input);
            }
        }

        if (n == 2) {
            cout << 0 << "\n";
            continue;
        }

        for (int i = 0; i < 100000; ++i) {
            if (cake[i] < maxCake) {
                rest += cake[i];
            }
        }

        /*cout << "maxCake : " << maxCake << "\n";
        cout << "rest : " << rest << "\n";
        cout << "modeCake : ";
        for (int next : modecake) {
            cout << next << " ";
        }cout << "\n";*/

        if (modecake.size() == 1) {
            res = rest / (maxCake - 1);
        }
        else {
            res = rest;
            res += modecake.size() - 1;
        }

        cout << res << "\n";
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

int T;
int n;
int cake[100001];
int res = 0;

int maxCake = 0;
vector<int> modecake; // ���� ���� ������ ���� ���
int rest = 0;

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> T;
    while (T--) {
        modecake.clear();
        maxCake = 0;
        rest = 0;
        res = 0;
        for (auto& next : cake) next = 0;

        cin >> n;
        for (int i = 0; i < n; ++i) {
            int input;
            cin >> input;
            cake[input]++;

            if (cake[input] > maxCake) {
                modecake.clear();
                maxCake = cake[input];
                modecake.push_back(input);
            }
            else if (cake[input] == maxCake) {
                modecake.push_back(input);
            }
        }

        for (int i = 0; i < 100000; ++i) {
            if (cake[i] < maxCake) {
                rest += cake[i];
            }
        }

        /*cout << "maxCake : " << maxCake << "\n";
        cout << "rest : " << rest << "\n";
        cout << "modeCake : ";
        for (int next : modecake) {
            cout << next << " ";
        }cout << "\n";*/

        if (modecake.size() == 1) {
            res = rest / (maxCake - 1);
        }
        else {
            res = rest;
            res += max((unsigned)0, modecake.size() - 1);
        }

        cout << res << "\n";
    }

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;

vector<int> rst[111111];
set<pair<int, int> > ms;

int main()
{
    int t;
    scanf("%i", &t);
    while (t--)
    {
        static int a[111111];
        static int b[111111];
        int n;
        scanf("%i", &n);
        for (int i = 0; i <= n; i++)
            a[i] = 0;
        for (int i = 0; i < n; i++)
        {
            int x;
            scanf("%i", &x);
            a[x]++;
        }
        int l = 0;
        int r = n - 1;
        while (l < r)
        {
            int m = (l + r) / 2 + 1;
            for (int i = 0; i < n; i++)
                rst[i].clear();
            ms.clear();
            int status = 0;
            for (int i = 1; i <= n; i++)
            {
                if (a[i])
                {
                    ms.insert({ -a[i], i });
                    b[i] = a[i];
                }
            }
            for (int i = 0; i < n; i++)
            {
                for (auto t : rst[i])
                    ms.insert({ -b[t], t });
                if (ms.size() == 0)
                {
                    status = 1;
                    break;
                }
                auto x = ms.begin();
                b[x->second]--;
                if (b[x->second])
                {
                    if (i + m + 1 >= n)
                    {
                        status = 1;
                        break;
                    }
                    rst[i + m + 1].push_back(x->second);
                }
                ms.erase(x);
            }
            if (status)
                r = m - 1;
            else
                l = m;
        }
        printf("%i\n", r);
    }
}


#include<iostream>
#include<cstdlib>
#include<map>
#include<vector>
#include<set>
#include<string>
using namespace std;
bool check(int dis, set<pair<int, int>> s,map<int,int> m) {
	int p = 0;
	vector<int> v;
	//cout << dis << endl;
	set<pair<int, int>> s2;
    while (m[(--s.end())->second]>0) {
		auto it = (--s.end());
		p = 0;
		while (it != (--s.begin())) {
			if (m[it->second] > 0) {
				v.push_back(it->second);
				//cout << it->second << " ";
				m[it->second]--;
				s2.insert({ m[it->second] ,it->second });
				p++;
			}
			if (p == dis + 1) {

				while (it != (--s.begin())) {
					s2.insert({ m[it->second] ,it->second });
					--it;
				}
				break;
			}
			--it;
		}
		//cout << p << endl;
		s = s2;
		s2.clear();
		if (p < dis + 1 && m[(--s.end())->second]>0) return false;

	}
	return true;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n,a;
		cin >> n;
		map<int, int> count;
		set<pair<int, int>> s;
		for (int j = 0; j < n; j++) {
			cin >> a;
			count[a]++;
		}
		for (auto it = count.begin(); it != count.end(); ++it) s.insert({ it->second,it->first});
		int l = -1;
		int right = s.size();
		//for (auto it = (--s.end()); it != (--s.begin()); --it) cout << it->first << " " << it->second << endl;
		while (l + 1 < right) {
			int x = (right + l) / 2;
			if (check(x,s,count)) l = x;
			else right = x;
			//cout << endl;
		}
		cout << l << endl;
	}

}


#include<iostream>
#include<cstdlib>
#include<map>
#include<vector>
#include<set>
#include<string>
using namespace std;
bool check(int dis, set<pair<int, int>> s,map<int,int> m) {
	int p = 0;
	vector<int> v;
	//cout << dis << endl;
	set<pair<int, int>> s2;
	set<pair<int, int>> s3;
	while (m[(--s.end())->second]>0) {
		auto it = (--s.end());
		p = 0;
		while (it != (--s.begin())) {
			if (m[it->second] > 0) {
				v.push_back(it->second);
				//cout << it->second << " ";
				m[it->second]--;
				s2.insert({ m[it->second] ,it->second });
				p++;
			}
			if (p == dis + 1) {

				while (it != (--s.begin())) {
					s2.insert({ m[it->second] ,it->second });
					--it;
				}
				break;
			}
			--it;
		}
		//cout << p << endl;
		s = s2;
		s2 = s3;
		if (p < dis + 1 && m[(--s.end())->second]>0) return false;

	}
	return true;
}
int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n,a;
		cin >> n;
		map<int, int> count;
		set<pair<int, int>> s;
		for (int j = 0; j < n; j++) {
			cin >> a;
			count[a]++;
		}
		for (auto it = count.begin(); it != count.end(); ++it) s.insert({ it->second,it->first});
		int l = -1;
		int right = s.size();
		//for (auto it = (--s.end()); it != (--s.begin()); --it) cout << it->first << " " << it->second << endl;
		while (l + 1 < right) {
			int x = (right + l) / 2;
			if (check(x,s,count)) l = x;
			else right = x;
			//cout << endl;
		}
		cout << l << endl;
	}

}


#include<iostream>
#include<cstdlib>
#include<map>
#include<vector>
#include<set>
#include<string>
using namespace std;
bool check(int dis, set<pair<int, int>> s,map<int,int> m) {
	int p = 0;
	vector<int> v;
	//cout << dis << endl;
	while (m[(--s.end())->second]>0) {
		auto it = (--s.end());
		p = 0;
		while (it != (--s.begin())) {
			if (m[it->second] > 0) {
				v.push_back(it->second);
				//cout << it->second << " ";
				m[it->second]--;
				p++;
			}
			if (p == dis + 1) break;
			--it;
		}
		if (p < dis + 1 && m[(--s.end())->second]>0) return false;

	}
	return true;
}
int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n,a;
		cin >> n;
		map<int, int> count;
		set<pair<int, int>> s;
		for (int j = 0; j < n; j++) {
			cin >> a;
			count[a]++;
		}
		for (auto it = count.begin(); it != count.end(); ++it) s.insert({ it->second,it->first});
		int l = 0;
		int right = s.size();
		//for (auto it = (--s.end()); it != (--s.begin()); --it) cout << it->first << " " << it->second << endl;
		while (l + 1 < right) {
			int x = (right + l) / 2;
			if (check(x,s,count)) l = x;
			else right = x;
			//cout << endl;
		}
		cout << l << endl;
	}

}


#include<iostream>
#include<cstdlib>
#include<map>
#include<vector>
#include<set>
#include<string>
using namespace std;
bool check(int dis, set<pair<int, int>> s,map<int,int> m) {
	int p = 0;
	vector<int> v;
	//cout << dis << endl;
	set<pair<int, int>> s2;
	set<pair<int, int>> s3;
	while (m[(--s.end())->second]>0) {
		auto it = (--s.end());
		p = 0;
		while (it != (--s.begin())) {
			if (m[it->second] > 0) {
				v.push_back(it->second);
				//cout << it->second << " ";
				m[it->second]--;
				s2.insert({ m[it->second] ,it->second });
				p++;
			}
			if (p == dis + 1) {

				while (it != (--s.begin())) {
					s2.insert({ m[it->second] ,it->second });
					--it;
				}
				break;
			}
			--it;
		}
		//cout << p << endl;
		s = s2;
		s2 = s3;
		if (p < dis + 1 && m[(--s.end())->second]>0) return false;

	}
	return true;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n,a;
		cin >> n;
		map<int, int> count;
		set<pair<int, int>> s;
		for (int j = 0; j < n; j++) {
			cin >> a;
			count[a]++;
		}
		for (auto it = count.begin(); it != count.end(); ++it) s.insert({ it->second,it->first});
		int l = -1;
		int right = s.size();
		//for (auto it = (--s.end()); it != (--s.begin()); --it) cout << it->first << " " << it->second << endl;
		while (l + 1 < right) {
			int x = (right + l) / 2;
			if (check(x,s,count)) l = x;
			else right = x;
			//cout << endl;
		}
		cout << l << endl;
	}

}


#include<iostream>
#include<cstdlib>
#include<map>
#include<vector>
#include<set>
#include<string>
using namespace std;

int main()
{

	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n,a;
		cin >> n;
		map<int, int> count;
		set<pair<int, int>> s;
		for (int j = 0; j < n; j++) {
			cin >> a;
			count[a]++;
		}
		for (auto it = count.begin(); it != count.end(); ++it) s.insert({ it->second,it->first});
		int mx = (--s.end())->first;
		int ct = 0;
		for (auto it = count.begin(); it != count.end(); ++it) {
			if (it->second == mx) ct++;
		}
		//cout << ct << endl;
		cout << (n - ct * mx ) / (mx - 1)+ct-1 << endl;
	}

}


#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
	int t;
	cin >> t;

	while (t--)
	{
		int n;
		cin >> n;
		vector<int> a = vector<int>(n+1,0);

		int tmax = 0;

		for (int i = 0; i < n; i++)
		{
			int k;
			cin >> k;
			a[k]++;
			tmax = max(a[k], tmax);
		}

		int maxs = 0;

		for (int i = 1; i < n + 1; i++)
			if (a[i] == tmax) maxs++;

		cout << (maxs==1 ?(n - tmax) / (tmax - 1) : (n-tmax*maxs)/(tmax-1) +tmax )<< endl;
	}

	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
	int t;
	cin >> t;

	while (t--)
	{
		int n;
		cin >> n;
		vector<int> a = vector<int>(n+1,0);

		int tmax = 0;

		for (int i = 0; i < n; i++)
		{
			int k;
			cin >> k;
			a[k]++;
			tmax = max(a[k], tmax);
		}

		int maxs = 0;

		for (int i = 1; i < n + 1; i++)
			if (a[i] == tmax) maxs++;

		cout << (maxs==1 ?(n - tmax) / (tmax - 1) : (n-tmax*maxs)/(tmax-1) +maxs-1 )<< endl;
	}

	return 0;
}


#include <iostream>
#include <math.h>
#include <cstdlib>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

int T_w[500100], sts_z[500100];

int main()
{
   int q; cin>>q;
   for (int w=0;w<q;w++)
   {
       int n, r=0; cin>>n;
       for (int i=0;i<n;i++)
       {
           int a; cin>>a;
           if (T_w[a]==0) r++;
           T_w[a]++;
       }
       sort(T_w, T_w+n+1, greater<int>());
       int i_p_r=1;
       for (int i=1;i<r;i++)
       {
           if (T_w[i]==T_w[i-1]) i_p_r++;
           else break;
       }
       int i_d_p=n-(i_p_r*T_w[0]);
       int poc=0, kon=600000, res, sr;
       while (poc<=kon)
       {
           sr=(poc+kon)/2;
           if ((sr*(T_w[0]-1)<=i_d_p)&&((sr+1)*(T_w[0]-1)>i_d_p))
           { res=sr; break; }
           if ((sr*(T_w[0]-1)<=i_d_p)&&((sr+1)*(T_w[0]-1)<=i_d_p))
           { poc=sr+1; continue; }
           if ((sr*(T_w[0]-1)>i_d_p))
           { kon=sr-1; continue; }
       }
       cout<<res+i_p_r-1<<endl;
       for (int i=0;i<r;i++) T_w[i]=0;

   }

   return 0;
}


#include <iostream>
#include <math.h>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <string>
#include <vector>
#include <functional>
using namespace std;

int T_w[500100];

int main()
{
   int q; cin>>q;
   for (int w=0;w<q;w++)
   {
       int n, r=0; cin>>n;
       for (int i=0;i<n;i++)
       {
           int a; cin>>a;
           if (T_w[a]==0) r++;
           T_w[a]++;
       }
       sort(T_w, T_w+n+1, greater < int > () );
       int i_p_r=1;
       for (int i=1;i<r;i++)
       {
           if (T_w[i]==T_w[i-1]) i_p_r++;
           else break;
       }
       int i_d_p=n-(i_p_r*T_w[0]);
       int poc=0, kon=600000, res, sr;
       while (poc<=kon)
       {
           sr=(poc+kon)/2;
           if ((sr*(T_w[0]-1)<=i_d_p)&&((sr+1)*(T_w[0]-1)>i_d_p))
           { res=sr; break; }
           if ((sr*(T_w[0]-1)<=i_d_p)&&((sr+1)*(T_w[0]-1)<=i_d_p))
           { poc=sr+1; continue; }
           if ((sr*(T_w[0]-1)>i_d_p))
           { kon=sr-1; continue; }
       }
       cout<<res+i_p_r-1<<endl;
       for (int i=0;i<r;i++) T_w[i]=0;

   }

   return 0;
}


#include <iostream>
#include <math.h>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

int T_w[500100];

int main()
{
   int q; cin>>q;
   for (int w=0;w<q;w++)
   {
       int n, r=0; cin>>n;
       for (int i=0;i<n;i++)
       {
           int a; cin>>a;
           if (T_w[a]==0) r++;
           T_w[a]++;
       }
       sort(T_w, T_w+n+1, greater < int > ());
       int i_p_r=1;
       for (int i=1;i<r;i++)
       {
           if (T_w[i]==T_w[i-1]) i_p_r++;
           else break;
       }
       int i_d_p=n-(i_p_r*T_w[0]);
       int poc=0, kon=600000, res, sr;
       while (poc<=kon)
       {
           sr=(poc+kon)/2;
           if ((sr*(T_w[0]-1)<=i_d_p)&&((sr+1)*(T_w[0]-1)>i_d_p))
           { res=sr; break; }
           if ((sr*(T_w[0]-1)<=i_d_p)&&((sr+1)*(T_w[0]-1)<=i_d_p))
           { poc=sr+1; continue; }
           if ((sr*(T_w[0]-1)>i_d_p))
           { kon=sr-1; continue; }
       }
       cout<<res+i_p_r-1<<endl;
       for (int i=0;i<r;i++) T_w[i]=0;

   }

   return 0;
}


#include <iostream>
#include <math.h>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <string>
#include <vector>
#include <functional>
using namespace std;

int T_w[500100];

int main()
{
   int q; cin>>q;
   for (int w=0;w<q;w++)
   {
       int n, r=0; cin>>n;
       for (int i=0;i<n;i++)
       {
           int a; cin>>a;
           if (T_w[a]==0) r++;
           T_w[a]++;
       }
       sort(T_w, T_w+n+1, greater < int > () );
       int i_p_r=1;
       for (int i=1;i<r;i++)
       {
           if (T_w[i]==T_w[i-1]) i_p_r++;
           else break;
       }
       int i_d_p=n-(i_p_r*T_w[0]);
       long long poc=0, kon=600000, res, sr;
       while (poc<=kon)
       {
           sr=(poc+kon)/2;
           if ((sr*(T_w[0]-1)<=i_d_p)&&((sr+1)*(T_w[0]-1)>i_d_p))
           { res=sr; break; }
           if ((sr*(T_w[0]-1)<=i_d_p)&&((sr+1)*(T_w[0]-1)<=i_d_p))
           { poc=sr+1; continue; }
           if ((sr*(T_w[0]-1)>i_d_p))
           { kon=sr-1; continue; }
       }
       cout<<res+i_p_r-1<<endl;
       for (int i=0;i<r;i++) T_w[i]=0;

   }

   return 0;
}


#include <iostream>
#include <math.h>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

int T_w[500100], sts_z[500100];

int main()
{
   int q; cin>>q;
   for (int w=0;w<q;w++)
   {
       int n, r=0; cin>>n;
       for (int i=0;i<n;i++)
       {
           int a; cin>>a;
           if (T_w[a]==0) r++;
           T_w[a]++;
       }
       sort(T_w, T_w+n+1, greater<int>());
       int i_p_r=1;
       for (int i=1;i<r;i++)
       {
           if (T_w[i]==T_w[i-1]) i_p_r++;
           else break;
       }
       int i_d_p=n-(i_p_r*T_w[0]);
       int poc=0, kon=600000, res, sr;
       while (poc<=kon)
       {
           sr=(poc+kon)/2;
           if ((sr*(T_w[0]-1)<=i_d_p)&&((sr+1)*(T_w[0]-1)>i_d_p))
           { res=sr; break; }
           if ((sr*(T_w[0]-1)<=i_d_p)&&((sr+1)*(T_w[0]-1)<=i_d_p))
           { poc=sr+1; continue; }
           if ((sr*(T_w[0]-1)>i_d_p))
           { kon=sr-1; continue; }
       }
       cout<<res+i_p_r-1<<endl;
       for (int i=0;i<r;i++) T_w[i]=0;

   }

   return 0;
}