#include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector <int>> vvi;
typedef vector<vector <ll>> vvll;
bool cmp(int a,int b)
{return a>b;
}
int main()
{
	int t;
	cin>>t;
	int ans,answer;
	vi v;
	int n,arr[11]={};
	for(int i=0;i<t;i++)
	{
		cin>>n;
		int j=1,ch=0;
		arr[n]++;
		v.clear();
		for(int j=0;j<11;j++)
		v.push_back(arr[j]);
		sort(v.begin(),v.end(),cmp);
		//for(auto u:v)
		//cout<<u<<' ';
		//cout<<'\n';
	    if(v[0]==1)
	    answer=i+1;
	    else
	    if(v[j]-ans==1)
	    { ans=v[0];
	    while(v[j])
	    {
			if(v[j]!=ans-1)
			{
				ch=1;
				break;
			}
			j++;
		}
	    if(ch==1)
		continue;
		else
		answer=i+1;
	  }
	  else if(v[j]==ans)
	  {

		  ans=v[0];
	    while(v[j])
	    {
			if(v[j]!=ans)
			{
				ch=1;
				break;
			}
			j++;
		}

	    if(ch==1 && v[j]==1 && v[j+1]==0)
		answer=i+1;
		else
		continue;

	  }

}
cout<<answer<<'\n';
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector <int>> vvi;
typedef vector<vector <ll>> vvll;
bool cmp(int a,int b)
{return a>b;
}
int main()
{
	int t;
	cin>>t;
	int ans,answer;
	vi v;
	int n,arr[11]={};
	for(int i=0;i<t;i++)
	{
		cin>>n;
		int j=1,ch=0;
		arr[n]++;
		v.clear();
		for(int j=0;j<11;j++)
		v.push_back(arr[j]);
		sort(v.begin(),v.end(),cmp);
		/*for(auto u:v)
		cout<<u<<' ';
		cout<<'\n';
		*/
		ans=v[0];
	    if(v[0]==1)
	    answer=i+1;
	    else
	    if(v[j]!=ans)
	    { ans=v[0];
	    while(v[j])
	    {
			if(v[j]!=ans-1)
			{
				ch=1;
				break;
			}
			j++;
		}
	    if(ch==1)
		continue;
		else
		answer=i+1;
	  }
	  else
	  {

		  ans=v[0];
	    while(v[j])
	    {
			if(v[j]!=ans)
			{
				ch=1;
				break;
			}
			j++;
		}

	    if(ch==1 && v[j]==1 && v[j+1]==0)
		answer=i+1;
		else
		{
		continue;
	}

	  }

}
cout<<answer;
cout<<'\n';
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector <int>> vvi;
typedef vector<vector <ll>> vvll;
bool cmp(int a,int b)
{return a>b;
}
int main()
{
	int t;
	cin>>t;
	int ans,answer;
	vi v;
	int n,arr[11]={};
	for(int i=0;i<t;i++)
	{
		cin>>n;
		int j=1,ch=0;
		arr[n]++;
		v.clear();
		for(int j=0;j<11;j++)
		v.push_back(arr[j]);
		sort(v.begin(),v.end(),cmp);
		for(auto u:v)
		cout<<u<<' ';
		cout<<'\n';
	    if(v[0]==1)
	    answer=i+1;
	    else
	    if(v[j]-ans==1)
	    { ans=v[0];
	    while(v[j])
	    {
			if(v[j]!=ans-1)
			{
				ch=1;
				break;
			}
			j++;
		}
	    if(ch==1)
		continue;
		else
		answer=i+1;
	  }
	  else if(v[j]==ans)
	  {

		  ans=v[0];
	    while(v[j])
	    {
			if(v[j]!=ans)
			{
				ch=1;
				break;
			}
			j++;
		}

	    if(ch==1 && v[j]==1 && v[j+1]==0)
		answer=i+1;
		else
		continue;

	  }

}
cout<<answer;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector <int>> vvi;
typedef vector<vector <ll>> vvll;
bool cmp(int a,int b)
{return a>b;
}
int main()
{
	int t;
	cin>>t;
	int ans,answer;
	vi v;
	int n,arr[11]={};
	for(int i=0;i<t;i++)
	{
		cin>>n;
		int j=1,ch=0;
		arr[n]++;
		v.clear();
		for(int j=0;j<11;j++)
		v.push_back(arr[j]);
		sort(v.begin(),v.end(),cmp);
		//for(auto u:v)
		//cout<<u<<' ';
		//cout<<'\n';
	    if(v[0]==1)
	    answer=i+1;
	    else
	    if(v[j]-ans==1)
	    { ans=v[0];
	    while(v[j])
	    {
			if(v[j]!=ans-1)
			{
				ch=1;
				break;
			}
			j++;
		}
	    if(ch==1)
		continue;
		else
		answer=i+1;
	  }
	  else if(v[j]==ans)
	  {

		  ans=v[0];
	    while(v[j])
	    {
			if(v[j]!=ans)
			{
				ch=1;
				break;
			}
			j++;
		}

	    if(ch==1 && v[j]==1 && v[j+1]==0)
		answer=i+1;
		else
		continue;

	  }

}
cout<<answer;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector <int>> vvi;
typedef vector<vector <ll>> vvll;
bool cmp(int a,int b)
{return a>b;
}
int main()
{
	int t;
	cin>>t;
	int ans,answer;
	vi v;
	int n,arr[11]={};
	for(int i=0;i<t;i++)
	{
		cin>>n;
		int j=1,ch=0;
		arr[n]++;
		v.clear();
		for(int j=0;j<11;j++)
		v.push_back(arr[j]);
		sort(v.begin(),v.end(),cmp);
		//for(auto u:v)
		//cout<<u<<' ';
		//cout<<'\n';
	    if(v[0]==1)
	    answer=i+1;
	    else
	    if(v[j]!=ans)
	    { ans=v[0];
	    while(v[j])
	    {
			if(v[j]!=ans-1)
			{
				ch=1;
				break;
			}
			j++;
		}
	    if(ch==1)
		continue;
		else
		answer=i+1;
	  }
	  else
	  {

		  ans=v[0];
	    while(v[j])
	    {
			if(v[j]!=ans)
			{
				ch=1;
				break;
			}
			j++;
		}

	    if(ch==1 && v[j]==1 && v[j+1]==0)
		answer=i+1;
		else
		continue;

	  }

}
cout<<answer;
cout<<'\n';
return 0;
}


#include <bits/stdc++.h>
using namespace std;

//TEMPLATE
#define pb              push_back
#define mp              make_pair
#define ll              long long
#define ld              long double
//#define pi              3.1415926536
#define pi              acos(-1)
//#define mod             1000000007
#define newline         printf("\n")
#define printminusone   printf("-1\n")
#define scl1(n)         scanf("%lld", &n)
#define scl2(a, b)      scanf("%lld %lld", &a, &b)
#define printl(n)       printf("%lld\n", n)
#define ssl1(str)       scanf(" %[^\n]", str)
#define scd1(n)         scanf("%lf", &n)
#define scd2(a, b)      scanf("%lf %lf", &a, &b)
#define printd(n)       printf("%lf\n", n)
#define printld(n)      printf("%Lf\n", n)
#define printcase       printf("Case %lld: ", i)
#define reversed(s)     reverse(s.begin(), s.end())
#define asorted(s)      sort(s.begin(), s.end())
#define dsorted(s)      sort(s.begin(), s.end(), cmp)
#define for0(i, n)      for (i = 0; i < n; i++)
#define for1(i, n)      for (i = 1; i <= n; i++)
#define fori(itt, stt)  for(itt = stt.begin(); itt != stt.end(); itt++)
#define pii             pair <ll, ll>
#define piii            pair <ll, pii>
//#define SIZE            100005
//#define INF             1000000005
#define F               first
#define S               second
#define FR              ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ms(arr, x)      memset(arr, x, sizeof arr)
#define bitcount(n)     __builtin_popcountll(n)

const ll INF = 1e18;
const ll SIZE = 1e5+5;
const ll mod = 1e9+7;

ll a[SIZE];
ll b[SIZE];
ll cc[SIZE];

int main() {
    ll t = 0, n, x, y, z, i, j, k, g, p, q, ans = 0, sum = 0, c = 0;
    scl1(n);

    for1(i, n) scl1(a[i]);

    set <ll> s;

    for1(i, n) {
        s.insert(a[i]);

        cc[b[a[i]]]--;

        b[a[i]]++;
        cc[b[a[i]]]++;

        t = s.size();
        if(t == 1) {
            ans = i;
            continue;
        }

        t = s.size();

        if(t == i) ans = i;

//        c = i / t;
        c = (i-1) / (t-1);

        if(((c*(t-1)) != i-1)) continue;

        if(cc[1] == 1) {
//            c = (i-1) / (t-1);

            if(cc[c] == t-1) ans = i;
        }
        if(cc[c] == t-1 && cc[c+1] == 1) ans = i;
        if(cc[c+1] == t-1 && cc[c] == 1) ans = i;
    }

    printl(ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

//TEMPLATE
#define pb              push_back
#define mp              make_pair
#define ll              long long
#define ld              long double
//#define pi              3.1415926536
#define pi              acos(-1)
//#define mod             1000000007
#define newline         printf("\n")
#define printminusone   printf("-1\n")
#define scl1(n)         scanf("%lld", &n)
#define scl2(a, b)      scanf("%lld %lld", &a, &b)
#define printl(n)       printf("%lld\n", n)
#define ssl1(str)       scanf(" %[^\n]", str)
#define scd1(n)         scanf("%lf", &n)
#define scd2(a, b)      scanf("%lf %lf", &a, &b)
#define printd(n)       printf("%lf\n", n)
#define printld(n)      printf("%Lf\n", n)
#define printcase       printf("Case %lld: ", i)
#define reversed(s)     reverse(s.begin(), s.end())
#define asorted(s)      sort(s.begin(), s.end())
#define dsorted(s)      sort(s.begin(), s.end(), cmp)
#define for0(i, n)      for (i = 0; i < n; i++)
#define for1(i, n)      for (i = 1; i <= n; i++)
#define fori(itt, stt)  for(itt = stt.begin(); itt != stt.end(); itt++)
#define pii             pair <ll, ll>
#define piii            pair <ll, pii>
//#define SIZE            100005
//#define INF             1000000005
#define F               first
#define S               second
#define FR              ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ms(arr, x)      memset(arr, x, sizeof arr)
#define bitcount(n)     __builtin_popcountll(n)

const ll INF = 1e18;
const ll SIZE = 1e5+5;
const ll mod = 1e9+7;

ll a[SIZE];
ll b[SIZE];
ll cc[SIZE];

int main() {
    ll t = 0, n, x, y, z, i, j, k, g, p, q, ans = 0, sum = 0, c = 0;
    scl1(n);

    for1(i, n) scl1(a[i]);

    set <ll> s;

    for1(i, n) {
        s.insert(a[i]);

        cc[b[a[i]]]--;

        b[a[i]]++;
        cc[b[a[i]]]++;

        t = s.size();
        if(t == 1) {
            ans = i;
            continue;
        }

        t = s.size();

        if(t == i) ans = i;

        c = i / t;

        if(cc[1] == 1) {
            c = (i-1) / (t-1);
            if(cc[c] == t-1 && ((c*(i-1)) == t-1) ) ans = i;
        }
        if(cc[c] == t-1 && cc[c+1] == 1) ans = i;
        if(cc[c+1] == t-1 && cc[c] == 1) ans = i;
    }

    printl(ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

//TEMPLATE
#define pb              push_back
#define mp              make_pair
#define ll              long long
#define ld              long double
//#define pi              3.1415926536
#define pi              acos(-1)
//#define mod             1000000007
#define newline         printf("\n")
#define printminusone   printf("-1\n")
#define scl1(n)         scanf("%lld", &n)
#define scl2(a, b)      scanf("%lld %lld", &a, &b)
#define printl(n)       printf("%lld\n", n)
#define ssl1(str)       scanf(" %[^\n]", str)
#define scd1(n)         scanf("%lf", &n)
#define scd2(a, b)      scanf("%lf %lf", &a, &b)
#define printd(n)       printf("%lf\n", n)
#define printld(n)      printf("%Lf\n", n)
#define printcase       printf("Case %lld: ", i)
#define reversed(s)     reverse(s.begin(), s.end())
#define asorted(s)      sort(s.begin(), s.end())
#define dsorted(s)      sort(s.begin(), s.end(), cmp)
#define for0(i, n)      for (i = 0; i < n; i++)
#define for1(i, n)      for (i = 1; i <= n; i++)
#define fori(itt, stt)  for(itt = stt.begin(); itt != stt.end(); itt++)
#define pii             pair <ll, ll>
#define piii            pair <ll, pii>
//#define SIZE            100005
//#define INF             1000000005
#define F               first
#define S               second
#define FR              ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ms(arr, x)      memset(arr, x, sizeof arr)
#define bitcount(n)     __builtin_popcountll(n)

const ll INF = 1e18;
const ll SIZE = 1e5+5;
const ll mod = 1e9+7;

ll a[SIZE];
ll b[SIZE];
ll cc[SIZE];

int main() {
    ll t = 0, n, x, y, z, i, j, k, g, p, q, ans = 0, sum = 0, c = 0;
    scl1(n);

    for1(i, n) scl1(a[i]);

    set <ll> s;

    for1(i, n) {
        s.insert(a[i]);

        cc[b[a[i]]]--;

        b[a[i]]++;
        cc[b[a[i]]]++;

        t = s.size();
        if(t == 1) {
            ans = i;
            continue;
        }

        t = s.size();

        if(t == i) ans = i;

        c = i / t;

        if(cc[1] == 1) {
            c = (i-1) / (t-1);
            if(cc[c] == t-1) ans = i;
        }
        if(cc[c] == t-1 && cc[c+1] == 1) ans = i;
        if(cc[c+1] == t-1 && cc[c] == 1) ans = i;
    }

    printl(ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

//TEMPLATE
#define pb              push_back
#define mp              make_pair
#define ll              long long
#define ld              long double
//#define pi              3.1415926536
#define pi              acos(-1)
//#define mod             1000000007
#define newline         printf("\n")
#define printminusone   printf("-1\n")
#define scl1(n)         scanf("%lld", &n)
#define scl2(a, b)      scanf("%lld %lld", &a, &b)
#define printl(n)       printf("%lld\n", n)
#define ssl1(str)       scanf(" %[^\n]", str)
#define scd1(n)         scanf("%lf", &n)
#define scd2(a, b)      scanf("%lf %lf", &a, &b)
#define printd(n)       printf("%lf\n", n)
#define printld(n)      printf("%Lf\n", n)
#define printcase       printf("Case %lld: ", i)
#define reversed(s)     reverse(s.begin(), s.end())
#define asorted(s)      sort(s.begin(), s.end())
#define dsorted(s)      sort(s.begin(), s.end(), cmp)
#define for0(i, n)      for (i = 0; i < n; i++)
#define for1(i, n)      for (i = 1; i <= n; i++)
#define fori(itt, stt)  for(itt = stt.begin(); itt != stt.end(); itt++)
#define pii             pair <ll, ll>
#define piii            pair <ll, pii>
//#define SIZE            100005
//#define INF             1000000005
#define F               first
#define S               second
#define FR              ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ms(arr, x)      memset(arr, x, sizeof arr)
#define bitcount(n)     __builtin_popcountll(n)

const ll INF = 1e18;
const ll SIZE = 1e5+5;
const ll mod = 1e9+7;

ll a[SIZE];
ll b[SIZE];
ll cc[SIZE];

int main() {
    ll t = 0, n, x, y, z, i, j, k, g, p, q, ans = 0, sum = 0, c = 0;
    scl1(n);

    for1(i, n) scl1(a[i]);

    set <ll> s;

    for1(i, n) {
        s.insert(a[i]);

        cc[b[a[i]]]--;

        b[a[i]]++;
        cc[b[a[i]]]++;

        t = s.size();
        if(t == 1) {
            ans = i;
            continue;
        }

        t = s.size();

        if(t == i) ans = i;

        if(cc[1] == 1) {
            c = (i-1) / (t-1);
            if(cc[c] == (t-1) && (i-1)%(t-1) == 0) ans = i;

            continue;
        }

//        if(i%t == 0) continue;

        c = i / t;

        if(cc[c-1] == t-1 && cc[c] == 1) ans = i;
        if(cc[c-1] == 1 && cc[c] == t-1) ans = i;
        if(cc[c] == t-1 && cc[c+1] == 1) ans = i;
        if(cc[c+1] == t-1 && cc[c] == 1) ans = i;
    }

    printl(ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

//TEMPLATE
#define pb              push_back
#define mp              make_pair
#define ll              long long
#define ld              long double
//#define pi              3.1415926536
#define pi              acos(-1)
//#define mod             1000000007
#define newline         printf("\n")
#define printminusone   printf("-1\n")
#define scl1(n)         scanf("%lld", &n)
#define scl2(a, b)      scanf("%lld %lld", &a, &b)
#define printl(n)       printf("%lld\n", n)
#define ssl1(str)       scanf(" %[^\n]", str)
#define scd1(n)         scanf("%lf", &n)
#define scd2(a, b)      scanf("%lf %lf", &a, &b)
#define printd(n)       printf("%lf\n", n)
#define printld(n)      printf("%Lf\n", n)
#define printcase       printf("Case %lld: ", i)
#define reversed(s)     reverse(s.begin(), s.end())
#define asorted(s)      sort(s.begin(), s.end())
#define dsorted(s)      sort(s.begin(), s.end(), cmp)
#define for0(i, n)      for (i = 0; i < n; i++)
#define for1(i, n)      for (i = 1; i <= n; i++)
#define fori(itt, stt)  for(itt = stt.begin(); itt != stt.end(); itt++)
#define pii             pair <ll, ll>
#define piii            pair <ll, pii>
//#define SIZE            100005
//#define INF             1000000005
#define F               first
#define S               second
#define FR              ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ms(arr, x)      memset(arr, x, sizeof arr)
#define bitcount(n)     __builtin_popcountll(n)

const ll INF = 1e18;
const ll SIZE = 1e5+5;
const ll mod = 1e9+7;

ll a[SIZE];
ll b[SIZE];
ll cc[SIZE];

int main() {
    ll t = 0, n, x, y, z, i, j, k, g, p, q, ans = 0, sum = 0, c = 0;
    scl1(n);

    for1(i, n) scl1(a[i]);

    set <ll> s;

    for1(i, n) {
        s.insert(a[i]);

        cc[b[a[i]]]--;

        b[a[i]]++;
        cc[b[a[i]]]++;

        t = s.size();
        if(t == 1) {
            ans = i;
            continue;
        }

        t = s.size();

        if(t == i) ans = i;

        if(cc[1] == 1) {
            c = (i-1) / (t-1);
            if(cc[c] == (t-1) && (i-1)%(t-1) == 0) ans = i;

            continue;
        }

        if(i%t == 0) continue;

        c = i / t;

        if(cc[c] == t-1 && cc[c+1] == 1) ans = i;
        if(cc[c+1] == t-1 && cc[c] == 1) ans = i;
    }

    printl(ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

//TEMPLATE
#define pb              push_back
#define mp              make_pair
#define ll              long long
#define ld              long double
//#define pi              3.1415926536
#define pi              acos(-1)
//#define mod             1000000007
#define newline         printf("\n")
#define printminusone   printf("-1\n")
#define scl1(n)         scanf("%lld", &n)
#define scl2(a, b)      scanf("%lld %lld", &a, &b)
#define printl(n)       printf("%lld\n", n)
#define ssl1(str)       scanf(" %[^\n]", str)
#define scd1(n)         scanf("%lf", &n)
#define scd2(a, b)      scanf("%lf %lf", &a, &b)
#define printd(n)       printf("%lf\n", n)
#define printld(n)      printf("%Lf\n", n)
#define printcase       printf("Case %lld: ", i)
#define reversed(s)     reverse(s.begin(), s.end())
#define asorted(s)      sort(s.begin(), s.end())
#define dsorted(s)      sort(s.begin(), s.end(), cmp)
#define for0(i, n)      for (i = 0; i < n; i++)
#define for1(i, n)      for (i = 1; i <= n; i++)
#define fori(itt, stt)  for(itt = stt.begin(); itt != stt.end(); itt++)
#define pii             pair <ll, ll>
#define piii            pair <ll, pii>
//#define SIZE            100005
//#define INF             1000000005
#define F               first
#define S               second
#define FR              ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ms(arr, x)      memset(arr, x, sizeof arr)
#define bitcount(n)     __builtin_popcountll(n)

const ll INF = 1e18;
const ll SIZE = 1e5+5;
const ll mod = 1e9+7;

ll a[SIZE];
ll b[SIZE];
ll cc[SIZE];

int main() {
    ll t = 0, n, x, y, z, i, j, k, g, p, q, ans = 0, sum = 0, c = 0, occ = 0;
    scl1(n);

    for1(i, n) scl1(a[i]);

    set <ll> s;

    for1(i, n) {
        s.insert(a[i]);

        cc[b[a[i]]]--;

        b[a[i]]++;
        cc[b[a[i]]]++;

        occ = max(occ, b[a[i]]);

        t = s.size();
        if(t == 1) {
            ans = i;
            continue;
        }

        t = s.size();

        if(t == i) ans = i;

        else if(cc[1] == 1) {
            if(cc[occ]*occ == (i-1)) ans = i;
        }

        else if(cc[occ] == 1 && (cc[occ-1]*(occ-1) == (i-occ))) ans = i;
    }

    printl(ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

//TEMPLATE
#define pb              push_back
#define mp              make_pair
#define ll              long long
#define ld              long double
//#define pi              3.1415926536
#define pi              acos(-1)
//#define mod             1000000007
#define newline         printf("\n")
#define printminusone   printf("-1\n")
#define scl1(n)         scanf("%lld", &n)
#define scl2(a, b)      scanf("%lld %lld", &a, &b)
#define printl(n)       printf("%lld\n", n)
#define ssl1(str)       scanf(" %[^\n]", str)
#define scd1(n)         scanf("%lf", &n)
#define scd2(a, b)      scanf("%lf %lf", &a, &b)
#define printd(n)       printf("%lf\n", n)
#define printld(n)      printf("%Lf\n", n)
#define printcase       printf("Case %lld: ", i)
#define reversed(s)     reverse(s.begin(), s.end())
#define asorted(s)      sort(s.begin(), s.end())
#define dsorted(s)      sort(s.begin(), s.end(), cmp)
#define for0(i, n)      for (i = 0; i < n; i++)
#define for1(i, n)      for (i = 1; i <= n; i++)
#define fori(itt, stt)  for(itt = stt.begin(); itt != stt.end(); itt++)
#define pii             pair <ll, ll>
#define piii            pair <ll, pii>
//#define SIZE            100005
//#define INF             1000000005
#define F               first
#define S               second
#define FR              ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ms(arr, x)      memset(arr, x, sizeof arr)
#define bitcount(n)     __builtin_popcountll(n)

const ll INF = 1e18;
const ll SIZE = 1e5+5;
const ll mod = 1e9+7;

ll a[SIZE];
ll b[SIZE];
ll cc[SIZE];

int main() {
    ll t = 0, n, x, y, z, i, j, k, g, p, q, ans = 0, sum = 0, c = 0, occ = 0;
    scl1(n);

    for1(i, n) scl1(a[i]);

    set <ll> s;

    for1(i, n) {
        s.insert(a[i]);

        cc[b[a[i]]]--;

        b[a[i]]++;
        cc[b[a[i]]]++;

        occ = max(occ, b[a[i]]);

        t = s.size();
        if(t == 1) {
            ans = i;
            continue;
        }

        t = s.size();

        if(t == i) ans = i;

        else if(cc[1] == 1) {
            if(cc[occ]*occ == (i-1)) ans = i;
        }

        else if(cc[occ] == 1 && (cc[occ-1]*(occ-1) == (i-1))) ans = i;
    }

    printl(ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

//TEMPLATE
#define pb              push_back
#define mp              make_pair
#define ll              long long
#define ld              long double
//#define pi              3.1415926536
#define pi              acos(-1)
//#define mod             1000000007
#define newline         printf("\n")
#define printminusone   printf("-1\n")
#define scl1(n)         scanf("%lld", &n)
#define scl2(a, b)      scanf("%lld %lld", &a, &b)
#define printl(n)       printf("%lld\n", n)
#define ssl1(str)       scanf(" %[^\n]", str)
#define scd1(n)         scanf("%lf", &n)
#define scd2(a, b)      scanf("%lf %lf", &a, &b)
#define printd(n)       printf("%lf\n", n)
#define printld(n)      printf("%Lf\n", n)
#define printcase       printf("Case %lld: ", i)
#define reversed(s)     reverse(s.begin(), s.end())
#define asorted(s)      sort(s.begin(), s.end())
#define dsorted(s)      sort(s.begin(), s.end(), cmp)
#define for0(i, n)      for (i = 0; i < n; i++)
#define for1(i, n)      for (i = 1; i <= n; i++)
#define fori(itt, stt)  for(itt = stt.begin(); itt != stt.end(); itt++)
#define pii             pair <ll, ll>
#define piii            pair <ll, pii>
//#define SIZE            100005
//#define INF             1000000005
#define F               first
#define S               second
#define FR              ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ms(arr, x)      memset(arr, x, sizeof arr)
#define bitcount(n)     __builtin_popcountll(n)

const ll INF = 1e18;
const ll SIZE = 1e5+5;
const ll mod = 1e9+7;

ll a[SIZE];
ll b[SIZE];
ll cc[SIZE];

int main() {
    ll t = 0, n, x, y, z, i, j, k, g, p, q, ans = 0, sum = 0, c = 0;
    scl1(n);

    for1(i, n) scl1(a[i]);

    set <ll> s;

    for1(i, n) {
        s.insert(a[i]);

        cc[b[a[i]]]--;

        b[a[i]]++;
        cc[b[a[i]]]++;

        t = s.size();
        if(t == 1) {
            ans = i;
            continue;
        }

        t = s.size();

        if(t == i) ans = i;

        c = i / t;

        if(cc[1] == 1) {
            c = (i-1) / (t-1);

            if(cc[c] == t-1 && ((c*(t-1)) == i-1) ) ans = i;
        }
        if(cc[c] == t-1 && cc[c+1] == 1) ans = i;
        if(cc[c+1] == t-1 && cc[c] == 1) ans = i;
    }

    printl(ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  int n;
  cin>>n;
  if(n==1)
  {
      cout<<1;
      return 0;
  }
  else if(n==2)
  {
      cout<<2;
      return 0;
  }

  //int M=0;
  int a[11]={0};
  int ans=2;
  for(int i=1;i<=n;i++)
  {
      int x;
      cin>>x;
      a[x]++;
      map<int,int>s;
      for(int j=1;j<11;j++)
      {
          if(a[j])
          s[a[j]]++;
          if(s.size()>2) break;
      }
      auto it=s.begin();
      if(s.size()==2)
      {
          auto itr=it++;
          if(it->second==1)
          {
              if(it->first==1 || it->first-itr->first==1)
              ans=i;
          }
          else if(itr->second==1)
          {
              if(itr->first==1 || itr->first-it->first==1)
              ans=i;
          }

      }

      else if(s.size()==1)
      {
          it=s.begin();
          if(it->first==1)
          ans=i;
      }
      //cout<<ans;

  }
  cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  int n;
  cin>>n;
  if(n==1)
  {
      cout<<1;
      return 0;
  }
  else if(n==2)
  {
      cout<<2;
      return 0;
  }

  //int M=0;
  int a[11]={0};
  int ans=2;
  map<int,int>s;
  for(int i=1;i<=n;i++)
  {
      int x;
      cin>>x;

      if(a[x])
      s[a[x]]--;
      auto it=s.begin();
      if(s[a[x]]==0)
      {
          it=s.find(a[x]);
          s.erase(it);
      }
      a[x]++;
      s[a[x]]++;
      it=s.begin();

      if(s.size()==2)
      {
          auto itr=it++;
          //cout<<it->first<<" "<<it->second<<" "<<itr->first<<" "<<itr->second<<endl;
          if(it->second==1)
          {
              if(it->first==1 || it->first-itr->first==1)
              {ans=i;}
          }
          else if(itr->second==1)
          {
              if(itr->first==1 || itr->first-it->first==1)
              {ans=i;}
          }

      }

      else if(s.size()==1)
      {
          it=s.begin();

          ans=i;

      }

     cout<<ans;

  }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  int n;
  cin>>n;
  if(n==1)
  {
      cout<<1;
      return 0;
  }
  else if(n==2)
  {
      cout<<2;
      return 0;
  }

  //int M=0;
  int a[11]={0};
  int ans=2;
  map<int,int>s;
  for(int i=1;i<=n;i++)
  {
      int x;
      cin>>x;

      if(a[x])
      s[a[x]]--;
      auto it=s.begin();
      if(s[a[x]]==0)
      {
          it=s.find(a[x]);
          s.erase(it);
      }
      a[x]++;
      s[a[x]]++;
      it=s.begin();

      if(s.size()==2)
      {
          auto itr=it++;
          //cout<<it->first<<" "<<it->second<<" "<<itr->first<<" "<<itr->second<<endl;
          if(it->second==1)
          {
              if(it->first==1 || it->first-itr->first==1)
              {ans=i;}
          }
          else if(itr->second==1)
          {
              if(itr->first==1 || itr->first-it->first==1)
              {ans=i;}
          }

      }

      else if(s.size()==1)
      {
          it=s.begin();

          ans=i;

      }

  }
 cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  int n;
  cin>>n;
  if(n==1)
  {
      cout<<1;
      return 0;
  }
  else if(n==2)
  {
      cout<<2;
      return 0;
  }

  //int M=0;
  int a[11]={0};
  int ans=2;
  map<int,int>s;
  for(int i=1;i<=n;i++)
  {
      int x;
      cin>>x;

      if(a[x])
      s[a[x]]--;
      auto it=s.begin();
      if(s[a[x]]==0)
      {
          it=s.find(a[x]);
          s.erase(it);
      }
      a[x]++;
      s[a[x]]++;
      it=s.begin();

      if(s.size()==2)
      {
          auto itr=it++;
          //cout<<it->first<<" "<<it->second<<" "<<itr->first<<" "<<itr->second<<endl;
          if(it->second==1)
          {
              if(it->first==1 || it->first-itr->first==1)
              {ans=i;}
          }
          else if(itr->second==1)
          {
              if(itr->first==1 || itr->first-it->first==1)
              {ans=i;}
          }

      }

      else if(s.size()==1)
      {
          it=s.begin();

          ans=i;

      }

  }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  int n;
  cin>>n;
  if(n==1)
  {
      cout<<1;
      return 0;
  }
  else if(n==2)
  {
      cout<<2;
      return 0;
  }

  //int M=0;
  int a[11]={0};
  int ans=2;
  map<int,int>s;
  for(int i=1;i<=n;i++)
  {
      int x;
      cin>>x;

      if(a[x])
      s[a[x]]--;
      auto it=s.begin();
      if(s[a[x]]==0)
      {
          it=s.find(a[x]);
          s.erase(it);
      }
      a[x]++;
      s[a[x]]++;
      it=s.begin();

      if(s.size()==2)
      {
          auto itr=it++;
          //cout<<it->first<<" "<<it->second<<" "<<itr->first<<" "<<itr->second<<endl;
          if(it->second==1)
          {
              if(it->first==1 || it->first-itr->first==1)
              {ans=i;}
          }
          else if(itr->second==1)
          {
              if(itr->first==1 || itr->first-it->first==1)
              {ans=i;}
          }

      }

      else if(s.size()==1)
      {
          it=s.begin();

          ans=i;

      }

     cout<<ans;

  }
  cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  int n;
  cin>>n;
  if(n==1)
  {
      cout<<1;
      return 0;
  }
  else if(n==2)
  {
      cout<<2;
      return 0;
  }

  //int M=0;
  int a[11]={0};
  int ans=2;
  map<int,int>s;
  for(int i=1;i<=n;i++)
  {
      int x;
      cin>>x;

      if(a[x])
      s[a[x]]--;
      auto it=s.begin();
      if(s[a[x]]==0)
      {
          it=s.find(a[x]);
          s.erase(it);
      }
      a[x]++;
      s[a[x]]++;
      it=s.begin();

      if(s.size()==2)
      {
          auto itr=it++;
          //cout<<it->first<<" "<<it->second<<" "<<itr->first<<" "<<itr->second<<endl;
          if(it->second==1)
          {
              if(it->first==1 || it->first-itr->first==1)
              {ans=i;}
          }
          else if(itr->second==1)
          {
              if(itr->first==1 || itr->first-it->first==1)
              {ans=i;}
          }

      }

      else if(s.size()==1)
      {
          it=s.begin();
          if(it->first==1 || (it->second==1))
          ans=i;

      }

  }
 cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  int n;
  cin>>n;
  if(n==1)
  {
      cout<<1;
      return 0;
  }
  else if(n==2)
  {
      cout<<2;
      return 0;
  }

  //int M=0;
  int a[11]={0};
  int ans=2;
  for(int i=1;i<=n;i++)
  {
      int x;
      cin>>x;
      a[x]++;
      map<int,int>s;
      for(int j=1;j<11;j++)
      {
          if(a[j])
          s[a[j]]++;
          if(s.size()>2) break;
      }
      auto it=s.begin();
      if(s.size()==2)
      {
          auto itr=it++;
          if(it->second==1)
          {
              if(it->first==1 || it->first-itr->first==1)
              ans=i;
          }
          else if(itr->second==1)
          {
              if(itr->first==1 || itr->first-it->first==1)
              ans=i;
          }

      }

      else if(s.size()==1)
      {
          ans=i;
      }
      //cout<<ans;

  }
  cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int a[20] ;

int main(){

    int n ;
    cin >> n ;
    int ans = 0 ;
    for (int i = 0; i < n; i++){

        int tmp ;
        cin >> tmp ;
        a[tmp]++ ;
        set<int> lol ;
        int flag = 0 ;
        for (int x = 0; x < 11; x++){

            if (a[x]!=0)
                lol.insert(a[x]) ;
            if (a[x]==1){
                flag++;
            }

        }
        if ((*lol.begin()==1) && (lol.size()==1)){ ans = i+1; continue ; }
        if (lol.size() == 2){
            int a = -1 ;
            int b = -1 ;
            for (auto wow:lol){
                if (a == -1){ a = wow ;}
                else        { b = wow ; }
            }
            if ((b-a==1) || flag == 1){ ans = i+1 ; }

        }

    }
    cout << ans ;

}


#include <bits/stdc++.h>
using namespace std;

int a[20] ;
int bl[1e5+10] ;

int main(){

    int n ;
    cin >> n ;
    int ans = 0 ;
    for (int i = 0; i < n; i++){

        fill(bl,bl+20,0) ;
        int tmp ;
        cin >> tmp ;
        a[tmp]++ ;
        set<int> lol ;
        int flag = 0 ;
        for (int x = 0; x < 11; x++){

            if (a[x]!=0){
                lol.insert(a[x]) ;
                bl[a[x]]++ ;
            }

        }
        if ((*lol.begin()==1) && (lol.size()==1)){ ans = i+1; continue ; }
        if (lol.size() == 2){
            int a = -1 ;
            int b = -1 ;
            for (auto wow:lol){
                if (a == -1){ a = wow ;}
                else        { b = wow ; }
            }
            if ((b-a==1 && (bl[b]==1)) || (bl[1]==1)){ ans = i+1 ; }

        }

    }
    cout << ans ;

}


#include <bits/stdc++.h>
using namespace std;

int a[20] ;
int bl[20] ;

int main(){

    int n ;
    cin >> n ;
    int ans = 0 ;
    for (int i = 0; i < n; i++){

        fill(bl,bl+20,0) ;
        int tmp ;
        cin >> tmp ;
        a[tmp]++ ;
        set<int> lol ;
        int flag = 0 ;
        for (int x = 0; x < 11; x++){

            if (a[x]!=0){
                lol.insert(a[x]) ;
                bl[a[x]]++ ;
            }

        }
        if ((*lol.begin()==1) && (lol.size()==1)){ ans = i+1; continue ; }
        if (lol.size() == 2){
            int a = -1 ;
            int b = -1 ;
            for (auto wow:lol){
                if (a == -1){ a = wow ;}
                else        { b = wow ; }
            }
            if ((b-a==1 && (bl[b]==1)) || (bl[1]==1)){ ans = i+1 ; }

        }

    }
    cout << ans ;

}


#include <bits/stdc++.h>
using namespace std;

int a[100010] ;
int b[100010] ;

int main(){

    int n ;
    cin >> n ;
    int ans = 0 ;
    int lol = 0 ;
    for (int i = 0; i < n; i++){

        int tmp ;
        cin >> tmp ;
        a[tmp]++ ;
        b[a[tmp]]++ ;
        b[a[tmp]-1]-- ;
        lol = max(lol, a[tmp]) ;
        if( b[1] == i+1 ){ ans = i+1 ; continue ;}
        if( b[i+1] == 1 ){ ans = i+1 ; continue ;}
        if( b[1] == 1 && b[lol]*lol == i ){ ans = i+1 ; continue ;}
        if( b[lol]== 1 && b[lol-1]*(lol-1) == i-lol+1){ ans = i+1 ; continue ;}

    }
    cout << ans ;

}


#include <bits/stdc++.h>
using namespace std;

int a[20] ;
int bl[100010] ;

int main(){

    int n ;
    cin >> n ;
    int ans = 0 ;
    for (int i = 0; i < n; i++){

        fill(bl,bl+20,0) ;
        int tmp ;
        cin >> tmp ;
        a[tmp]++ ;
        set<int> lol ;
        int flag = 0 ;
        for (int x = 0; x < 11; x++){

            if (a[x]!=0){
                lol.insert(a[x]) ;
                bl[a[x]]++ ;
            }

        }
        if ((*lol.begin()==1) && (lol.size()==1)){ ans = i+1; continue ; }
        if (lol.size() == 2){
            int a = -1 ;
            int b = -1 ;
            for (auto wow:lol){
                if (a == -1){ a = wow ;}
                else        { b = wow ; }
            }
            if ((b-a==1 && (bl[b]==1)) || (bl[1]==1)){ ans = i+1 ; }

        }

    }
    cout << ans ;

}


#include <bits/stdc++.h>
using namespace std;

int a[20] ;
int bl[100010] ;

int main(){

    int n ;
    cin >> n ;
    int ans = 0 ;
    for (int i = 0; i < n; i++){

        fill(bl,bl+20,0) ;
        int tmp ;
        cin >> tmp ;
        a[tmp]++ ;
        set<int> lol ;
        int flag = 0 ;
        int hoho = 0 ;
        for (int x = 0; x < 11; x++){

            if (a[x]!=0){
                lol.insert(a[x]) ;
                bl[a[x]]++ ;
                hoho++ ;
            }

        }
        if (hoho == 1){ ans = i+1; continue ; }
        if ((*lol.begin()==1) && (lol.size()==1)){ ans = i+1; continue ; }
        if (lol.size() == 2){
            int a = -1 ;
            int b = -1 ;
            for (auto wow:lol){
                if (a == -1){ a = wow ;}
                else        { b = wow ; }
            }
            if ((b-a==1 && (bl[b]==1)) || (bl[1]==1)){ ans = i+1 ; }

        }

    }
    cout << ans ;

}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
#define ll long long int
//#define d double
#define F first
#define S second
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix std::fixed<<setprecision(10)
ll M=1e9+7;
#define f(n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define vi vector<int>
#define pb push_back
#define si set<int>
#define cl(n,m) (ceil(double(n)/double(m)))
#define T() int t; cin>>t; while(t--)
#define pi pair<ll,ll>
ll MOD=998244353;
#define endl "\n"
const double PI = 3.141592653589793238460;
bitset<N>bs;
bitset<512>bb[512];
//vector<vector<int>>arr;
ll power(ll a,ll b)
{
    ll res=1;
    if(a==0)return 0;
    if(a==1)return 1;
    for(;b>0;b>>=1)
    {
        if(b&1){res=(res*a);
        if(res>M)res%=M;}
        a=(a*a);
        if(a>M)a=a%M;
    }
    return res;
}

int main()
{
  int n;
  cin>>n;
  int arr[n];
  f(n)cin>>arr[i];

  int cnt[10+1]{0};
int ans=0;
map<int,int>s;
  for(int i=0;i<n;i++)
  {
      int z=cnt[arr[i]];
      if(s.count(z)){
      s[z]--;
      if(s[z]==0)s.erase(z);
      }
      cnt[arr[i]]++;
      z=cnt[arr[i]];
      s[z]++;
      if(s.size()==2)
      {
         // cout<<i<<" ";
          auto it=s.begin();
          auto itr=s.rbegin();
          int x=it->F,y=itr->F;
          if(abs(x-y)==1)ans=i+1;
          else if(z==1)ans=i+1;
      }
      else if(s.size()==1)
      {
          int x=s.begin()->F;
          if(x==1)
          {
              ans=i+1;
          }
      }
  }
  int i=0;
  for(i=1;i<n;i++)if(arr[i]!=arr[i-1])break;
  if(i==n)ans=n;
cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
#define ll long long int
//#define d double
#define F first
#define S second
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix std::fixed<<setprecision(10)
ll M=1e9+7;
#define f(n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define vi vector<int>
#define pb push_back
#define si set<int>
#define cl(n,m) (ceil(double(n)/double(m)))
#define T() int t; cin>>t; while(t--)
#define pi pair<ll,ll>
ll MOD=998244353;
#define endl "\n"
const double PI = 3.141592653589793238460;
bitset<N>bs;
bitset<512>bb[512];
//vector<vector<int>>arr;
ll power(ll a,ll b)
{
    ll res=1;
    if(a==0)return 0;
    if(a==1)return 1;
    for(;b>0;b>>=1)
    {
        if(b&1){res=(res*a);
        if(res>M)res%=M;}
        a=(a*a);
        if(a>M)a=a%M;
    }
    return res;
}

int main()
{
  int n;
  cin>>n;
  int arr[n];
  f(n)cin>>arr[i];

  int cnt[10+1]{0};
int ans=0;
  for(int i=0;i<n;i++)
  {
      cnt[arr[i]]++;
      int sum=0;
      set<int>s;
      int total=0;
      int tt=0;
      for(int j=1;j<=10;j++)
      {
       if(cnt[j]>0){s.insert(cnt[j]);  total=cnt[j];tt++;}
      }
      if(s.size()==2)
      {
          auto it=s.begin();
          auto itr=it;
          itr++;
          if(abs(*it-*itr)==1)
          ans=max(ans,i+1);
          else
          {
              int x=cnt[arr[i]];
              if(x==1)
              ans=max(ans,i+1);
          }
      }
      else if(s.size()==1 && total==1)
      {
          ans=max(ans,i+1);
      }
      else if(tt==1)ans=max(ans,i+1);
  }
cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
#define ll long long int
//#define d double
#define F first
#define S second
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix std::fixed<<setprecision(10)
ll M=1e9+7;
#define f(n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define vi vector<int>
#define pb push_back
#define si set<int>
#define cl(n,m) (ceil(double(n)/double(m)))
#define T() int t; cin>>t; while(t--)
#define pi pair<ll,ll>
ll MOD=998244353;
#define endl "\n"
const double PI = 3.141592653589793238460;
bitset<N>bs;
bitset<512>bb[512];
//vector<vector<int>>arr;
ll power(ll a,ll b)
{
    ll res=1;
    if(a==0)return 0;
    if(a==1)return 1;
    for(;b>0;b>>=1)
    {
        if(b&1){res=(res*a);
        if(res>M)res%=M;}
        a=(a*a);
        if(a>M)a=a%M;
    }
    return res;
}

int main()
{
  int n;
  cin>>n;
  int arr[n];
  f(n)cin>>arr[i];

  int cnt[10+1]{0};
int ans=0;
map<int,int>s;
  for(int i=0;i<n;i++)
  {
      int z=cnt[arr[i]];
      if(s.count(z)){
      s[z]--;
      if(s[z]==0)s.erase(z);
      }
      cnt[arr[i]]++;
      z=cnt[arr[i]];
      s[z]++;
      if(s.size()==2)
      {
         // cout<<i<<" ";
          auto it=s.begin();
          auto itr=s.rbegin();
          int x=it->F,y=itr->F;
          int xx=it->S,yy=itr->S;
          if(abs(x-y)==1 && (xx==1||yy==1))ans=i+1;
          else if(z==1 && (xx==1||yy==1))ans=i+1;
      }
      else if(s.size()==1)
      {
          int x=s.begin()->F;
          if(x==1)
          {
              ans=i+1;
          }
      }
  }
  int i=0;
  for(i=1;i<n;i++)if(arr[i]!=arr[i-1])break;
  if(i==n)ans=n;
cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
#define ll long long int
//#define d double
#define F first
#define S second
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix std::fixed<<setprecision(10)
ll M=1e9+7;
#define f(n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define vi vector<int>
#define pb push_back
#define si set<int>
#define cl(n,m) (ceil(double(n)/double(m)))
#define T() int t; cin>>t; while(t--)
#define pi pair<ll,ll>
ll MOD=998244353;
#define endl "\n"
const double PI = 3.141592653589793238460;
bitset<N>bs;
bitset<512>bb[512];
//vector<vector<int>>arr;
ll power(ll a,ll b)
{
    ll res=1;
    if(a==0)return 0;
    if(a==1)return 1;
    for(;b>0;b>>=1)
    {
        if(b&1){res=(res*a);
        if(res>M)res%=M;}
        a=(a*a);
        if(a>M)a=a%M;
    }
    return res;
}

int main()
{
  int n;
  cin>>n;
  int arr[n];
  f(n)cin>>arr[i];

  int cnt[10+1]{0};
int ans=0;
set<int>s;
  for(int i=0;i<n;i++)
  {
      s.erase(cnt[arr[i]]);
      cnt[arr[i]]++;
      int z=cnt[arr[i]];
      s.insert(cnt[arr[i]]);
      if(s.size()==2)
      {
          auto it=s.begin();
          auto itr=s.rbegin();
          int x=*it,y=*itr;
//if(i==6)cout<<x<<" "<<y<<endl;
          if(abs(x-y)==1)ans=i+1;
          else if(z==1)ans=i+1;
      }
      else if(s.size()==1)
      {
          int x=*s.begin();
          if(x==1)
          {
              ans=i+1;
          }
      }
  }
  int i=0;
  for(i=1;i<n;i++)if(arr[i]!=arr[i-1])break;
  if(i==n)ans=n;
cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
#define ll long long int
//#define d double
#define F first
#define S second
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix std::fixed<<setprecision(10)
ll M=1e9+7;
#define f(n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define vi vector<int>
#define pb push_back
#define si set<int>
#define cl(n,m) (ceil(double(n)/double(m)))
#define T() int t; cin>>t; while(t--)
#define pi pair<ll,ll>
ll MOD=998244353;
#define endl "\n"
const double PI = 3.141592653589793238460;
bitset<N>bs;
bitset<512>bb[512];
//vector<vector<int>>arr;
ll power(ll a,ll b)
{
    ll res=1;
    if(a==0)return 0;
    if(a==1)return 1;
    for(;b>0;b>>=1)
    {
        if(b&1){res=(res*a);
        if(res>M)res%=M;}
        a=(a*a);
        if(a>M)a=a%M;
    }
    return res;
}

int main()
{
  int n;
  cin>>n;
  int arr[n];
  f(n)cin>>arr[i];

  int cnt[10+1]{0};
int ans=0;
  for(int i=0;i<n;i++)
  {
      cnt[arr[i]]++;
      int sum=0;
      set<int>s;
      int total=0;
      for(int j=1;j<=10;j++)
      {
       if(cnt[j]>0){s.insert(cnt[j]);  total=cnt[j];}
      }
      if(s.size()==2)
      {
          ans=max(ans,i+1);
      }
      else if(s.size()==1 && total==1)
      {
          ans=max(ans,i+1);
      }
  }
cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
#define ll long long int
//#define d double
#define F first
#define S second
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix std::fixed<<setprecision(10)
ll M=1e9+7;
#define f(n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define vi vector<int>
#define pb push_back
#define si set<int>
#define cl(n,m) (ceil(double(n)/double(m)))
#define T() int t; cin>>t; while(t--)
#define pi pair<ll,ll>
ll MOD=998244353;
#define endl "\n"
const double PI = 3.141592653589793238460;
bitset<N>bs;
bitset<512>bb[512];
//vector<vector<int>>arr;
ll power(ll a,ll b)
{
    ll res=1;
    if(a==0)return 0;
    if(a==1)return 1;
    for(;b>0;b>>=1)
    {
        if(b&1){res=(res*a);
        if(res>M)res%=M;}
        a=(a*a);
        if(a>M)a=a%M;
    }
    return res;
}

int main()
{
  int n;
  cin>>n;
  int arr[n];
  f(n)cin>>arr[i];

  int cnt[10+1]{0};
int ans=0;
map<int,int>s;
  for(int i=0;i<n;i++)
  {
      int z=cnt[arr[i]];
      if(s.count(z)){
      s[z]--;
      if(s[z]==0)s.erase(z);
      }
      cnt[arr[i]]++;
      z=cnt[arr[i]];
      s[z]++;
      if(s.size()==2)
      {
         // cout<<i<<" ";
          auto it=s.begin();
          auto itr=s.rbegin();
          int x=it->F,y=itr->F;
          int xx=it->S,yy=itr->S;
          if(x<y){swap(x,y);
          swap(xx,yy);}

          if(x-y==1 && xx==1)ans=i+1;
          else if(y==1 && yy==1)ans=i+1;
      }
      else if(s.size()==1)
      {
          int x=s.begin()->F;
          if(x==1)
          {
              ans=i+1;
          }
      }
  }
  int i=0;
  for(i=1;i<n;i++)if(arr[i]!=arr[i-1])break;
  if(i==n)ans=n;
cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
#define ll long long int
//#define d double
#define F first
#define S second
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix std::fixed<<setprecision(10)
ll M=1e9+7;
#define f(n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define vi vector<int>
#define pb push_back
#define si set<int>
#define cl(n,m) (ceil(double(n)/double(m)))
#define T() int t; cin>>t; while(t--)
#define pi pair<ll,ll>
ll MOD=998244353;
#define endl "\n"
const double PI = 3.141592653589793238460;
bitset<N>bs;
bitset<512>bb[512];
//vector<vector<int>>arr;
ll power(ll a,ll b)
{
    ll res=1;
    if(a==0)return 0;
    if(a==1)return 1;
    for(;b>0;b>>=1)
    {
        if(b&1){res=(res*a);
        if(res>M)res%=M;}
        a=(a*a);
        if(a>M)a=a%M;
    }
    return res;
}

int main()
{
  int n;
  cin>>n;
  int arr[n];
  f(n)cin>>arr[i];

  int cnt[10+1]{0};
int ans=0;
  for(int i=0;i<n;i++)
  {
      cnt[arr[i]]++;
      int sum=0;
      set<int>s;
      int total=0;
      for(int j=1;j<=10;j++)
      {
       if(cnt[j]>0){s.insert(cnt[j]);  total=cnt[j];}
      }
      if(s.size()==2)
      {
          auto it=s.begin();
          auto itr=it;
          itr++;
          if(abs(*it-*itr)==1)
          ans=max(ans,i+1);
      }
      else if(s.size()==1 && total==1)
      {
          ans=max(ans,i+1);
      }
  }
cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
#define ll long long int
//#define d double
#define F first
#define S second
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix std::fixed<<setprecision(10)
ll M=1e9+7;
#define f(n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define vi vector<int>
#define pb push_back
#define si set<int>
#define cl(n,m) (ceil(double(n)/double(m)))
#define T() int t; cin>>t; while(t--)
#define pi pair<ll,ll>
ll MOD=998244353;
#define endl "\n"
const double PI = 3.141592653589793238460;
bitset<N>bs;
bitset<512>bb[512];
//vector<vector<int>>arr;
ll power(ll a,ll b)
{
    ll res=1;
    if(a==0)return 0;
    if(a==1)return 1;
    for(;b>0;b>>=1)
    {
        if(b&1){res=(res*a);
        if(res>M)res%=M;}
        a=(a*a);
        if(a>M)a=a%M;
    }
    return res;
}

int main()
{
  int n;
  cin>>n;
  int arr[n];
  f(n)cin>>arr[i];

  int cnt[10+1]{0};
int ans=0;
  for(int i=0;i<n;i++)
  {
      cnt[arr[i]]++;
      int sum=0;
      set<int>s;
      int total=0;
      for(int j=1;j<=10;j++)
      {
       if(cnt[j]>0){s.insert(cnt[j]);  total=cnt[j];}
      }
      if(s.size()==2)
      {
          auto it=s.begin();
          auto itr=it;
          itr++;
          if(abs(*it-*itr)==1)
          ans=max(ans,i+1);
          else
          {
              int x=cnt[arr[i]];
              if(x==1)
              ans=max(ans,i+1);
          }
      }
      else if(s.size()==1 && total==1)
      {
          ans=max(ans,i+1);
      }
  }
cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
#define ll long long int
//#define d double
#define F first
#define S second
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix std::fixed<<setprecision(10)
ll M=1e9+7;
#define f(n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define vi vector<int>
#define pb push_back
#define si set<int>
#define cl(n,m) (ceil(double(n)/double(m)))
#define T() int t; cin>>t; while(t--)
#define pi pair<ll,ll>
ll MOD=998244353;
#define endl "\n"
const double PI = 3.141592653589793238460;
bitset<N>bs;
bitset<512>bb[512];
//vector<vector<int>>arr;
ll power(ll a,ll b)
{
    ll res=1;
    if(a==0)return 0;
    if(a==1)return 1;
    for(;b>0;b>>=1)
    {
        if(b&1){res=(res*a);
        if(res>M)res%=M;}
        a=(a*a);
        if(a>M)a=a%M;
    }
    return res;
}

int main()
{
  int n;
  cin>>n;
  int arr[n];
  f(n)cin>>arr[i];

  int cnt[10+1]{0};
int ans=0;
  for(int i=0;i<n;i++)
  {
      cnt[arr[i]]++;
      int sum=0;
      set<int>s;
      int total=0;
      for(int j=1;j<=10;j++)
      {
       if(cnt[j]>0){s.insert(cnt[j]);  total=cnt[j];}
      }
      if(s.size()==2)
      {
          auto it=s.begin();
          auto itr=it;
          itr++;
          if(abs(*it-*itr)==1)
          ans=max(ans,i+1);
          else
          {
              int x=cnt[arr[i]];
              if(x==1)
              ans=max(ans,i+1);
              else if(*it-1==*itr||*itr-1==*it)ans=max(ans,i+1);
          }
      }
      else if(s.size()==1 && total==1)
      {
          ans=max(ans,i+1);
      }
  }
cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
#define ll long long int
//#define d double
#define F first
#define S second
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix std::fixed<<setprecision(10)
ll M=1e9+7;
#define f(n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define vi vector<int>
#define pb push_back
#define si set<int>
#define cl(n,m) (ceil(double(n)/double(m)))
#define T() int t; cin>>t; while(t--)
#define pi pair<ll,ll>
ll MOD=998244353;
#define endl "\n"
const double PI = 3.141592653589793238460;
bitset<N>bs;
bitset<512>bb[512];
//vector<vector<int>>arr;
ll power(ll a,ll b)
{
    ll res=1;
    if(a==0)return 0;
    if(a==1)return 1;
    for(;b>0;b>>=1)
    {
        if(b&1){res=(res*a);
        if(res>M)res%=M;}
        a=(a*a);
        if(a>M)a=a%M;
    }
    return res;
}

int main()
{
  int n;
  cin>>n;
  int arr[n];
  f(n)cin>>arr[i];

  int cnt[10+1]{0};
int ans=0;
map<int,int>s;
  for(int i=0;i<n;i++)
  {
      int z=cnt[arr[i]];
      if(s.count(z)){
      s[z]--;
      if(s[z]==0)s.erase(z);
      }
      cnt[arr[i]]++;
      z=cnt[arr[i]];
      s[z]++;
      if(s.size()==2)
      {
         // cout<<i<<" ";
          auto it=s.begin();
          auto itr=s.rbegin();
          int x=it->F,y=itr->F;
          int xx=it->S,yy=itr->S;
          if(x<y){swap(x,y);
          swap(xx,yy);}

          if(x-y==1 && xx==1)ans=i+1;
          else if(z==1 && yy==1)ans=i+1;
      }
      else if(s.size()==1)
      {
          int x=s.begin()->F;
          if(x==1)
          {
              ans=i+1;
          }
      }
  }
  int i=0;
  for(i=1;i<n;i++)if(arr[i]!=arr[i-1])break;
  if(i==n)ans=n;
cout<<ans;
}


#include<iostream>
#include<string>
#include<deque>
#include<algorithm>
#define ll long long
#define pb push_back
using namespace std;
ll a,b,c,d,n,m,k,x,y,z,cnt,ans,tt;
string s;
deque<ll>v,q,t;
int main()
{
    cin>>tt;
    while(tt--){
        cin>>n;v.clear();c=0;
        for(ll i=0;i<n;i++){
            cin>>a;c+=a;
        }if(c<=0){
            cout<<(-1)*c+1<<endl;;
        }else
            cout<<0<<endl;
    }
}


#include<iostream>
#include<string>
#include<deque>
#include<algorithm>
#define ll long long
#define pb push_back
using namespace std;
ll a,b,c,d,n,m,k,x,y,z,cnt,ans,tt;
string s;
deque<ll>v,q,t;
int main()
{
    cin>>tt;
    while(tt--){
        cin>>n;v.clear();cnt=0;c=0;
        for(ll i=0;i<n;i++){
            cin>>a;if(a==0)cnt++;
            else
                c+=a;
        }if(c<=0 and cnt!=0){
            cout<<-1*c+cnt<<"\n";
        }else if(c<=0 and cnt==0){
            cout<<-1*c+1<<"\n";
        }else
            cout<<"0\n";
    }
}


#include <bits/stdc++.h>
#define ll long long int
#define FOR(i, a, n) for(int i = a; i < n; i++)
#define _FOR(i, a, n) for(int i = a; i <= n; i++)
#define FOOR(i, a, n) for(int i = a; i < n - 1; i++)
#define _FOOR(i, a, n) for(int i = a; i <= n - 1; i++)
#define FOOOR(i, a, n) for(int i = n; i > a; i--)
#define endl "\n"
#define vi vector<int>
#define se set<char>
#define pb push_back
#define R return 0
#define _R return
#define B break
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout,tie(0)
using namespace std;

	ll n;

int main(){

	//freopen("input.txt", "w", stdout);
	//freopen("input.txt", "r", stdin);
	cin >> n;
	while(n--){
		int t;
		cin >> t;
		int sum = 0, dem = 0;
		int  a;
		FOR(i, 0, t){
			cin >> a;
			if(a == 0) dem++;
			sum += a;
		}
		if(sum == 0 && dem != 0){
			cout << dem << endl;
		}
		else{
			if(sum != 0 && dem == 0){
				cout << "0" << endl;
			}
			else{
				if(sum != 0 && dem != 0){
					cout << dem << endl;
				}
			}
		}
	}

return 0;

}


#include <bits/stdc++.h>
#define ll long long int
#define FOR(i, a, n) for(int i = a; i < n; i++)
#define _FOR(i, a, n) for(int i = a; i <= n; i++)
#define FOOR(i, a, n) for(int i = a; i < n - 1; i++)
#define _FOOR(i, a, n) for(int i = a; i <= n - 1; i++)
#define FOOOR(i, a, n) for(int i = n; i > a; i--)
#define endl "\n"
#define vi vector<int>
#define se set<char>
#define pb push_back
#define R return 0
#define _R return
#define B break
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout,tie(0)
using namespace std;

	ll n;

int main(){

	//freopen("input.txt", "w", stdout);
	//freopen("input.txt", "r", stdin);
	cin >> n;
	while(n--){
		int t;
		cin >> t;
		int sum = 0, dem = 0;
		int  a;
		FOR(i, 0, t){
			cin >> a;
			if(a == 0) dem++;
			sum += a;
		}
		if(sum == 0 && dem == 0) cout << "1" << endl;
		else if(dem == 0 && sum != 0) cout << "0" << endl;
		else if(sum == 0 && dem != 0) cout << dem << endl;
		else  if(sum != 0 && dem != 0){
			if(sum + dem == 0){
				cout << dem + 1 << endl;
			}
			else cout << dem << endl;
		}
	}

return 0;

}


#include <bits/stdc++.h>
#define ll long long int
#define FOR(i, a, n) for(int i = a; i < n; i++)
#define _FOR(i, a, n) for(int i = a; i <= n; i++)
#define FOOR(i, a, n) for(int i = a; i < n - 1; i++)
#define _FOOR(i, a, n) for(int i = a; i <= n - 1; i++)
#define FOOOR(i, a, n) for(int i = n; i > a; i--)
#define endl "\n"
#define vi vector<int>
#define se set<char>
#define pb push_back
#define R return 0
#define _R return
#define B break
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout,tie(0)
using namespace std;

	ll n;

int main(){

	//freopen("input.txt", "w", stdout);
	//freopen("input.txt", "r", stdin);
	cin >> n;
	while(n--){
		int t;
		cin >> t;
		int sum = 0, dem = 0;
		int  a;
		FOR(i, 0, t){
			cin >> a;
			if(a == 0) dem++;
			sum += a;
		}
		if(sum == 0 && dem != 0){
			cout << dem << endl;
		}
		else{
			if(sum != 0 && dem == 0){
				cout << "0" << endl;
			}
			else{
				cout << dem + 1 << endl;
			}
		}
	}

return 0;

}


#include <bits/stdc++.h>

using namespace std;
int main()
{ int t, n, sum=0, pr=1, c=0;
   cin >> t;
   for (int i=0; i<t; i++){
    cin >> n;
    int a[n];
    for (int i=0; i<n; i++){
        cin >> a[i];
        if (a[i]==0)
            c++;
        sum=sum+a[i];
        pr=pr*a[i];
    }
   // cout << c << " " << sum << endl;
    if (c!=0 && sum!=-(c))
        cout << c << "\n";
    else if (sum!=0 && pr!=0)
        cout << 0 << "\n";
    else if (pr!=0 && sum==0)
        cout << 1 << "\n";
    else if (sum=c)
        cout << (c+1);
    sum=0;
    pr=1;
    c=0;
   }
}


#include <iostream>

using namespace std;

int main()
{ int t, n, sum=0, pr=1, c=0;
   cin >> t;
   for (int i=0; i<t; i++){
    cin >> n;
    int a[n];
    for (int i=0; i<n; i++){
        cin >> a[i];
        sum=sum+a[i];
        if (a[i]==0)
            c++;
    }
    if (sum!=0 && c==0)
        cout << 0 << "\n";
    else if (sum==0 && c==0)
        cout << 1 << "\n";
    else if (c!=0 && sum!=-(c))
        cout << c << "\n";
    else if (c!=0 && sum==-(c))
        cout << c+1 << "\n";
    sum=0;
    pr=1;
    c=0;
   }
}


#include <iostream>

using namespace std;

int main()
{ int t, n, sum=0, pr=1, c=0;
   cin >> t;
   for (int i=0; i<t; i++){
    cin >> n;
    int a[n];
    for (int i=0; i<n; i++){
        cin >> a[i];
        sum=sum+a[i];
        pr=pr*a[i];
        if (a[i]==0)
            c++;
    }
    if (sum!=0 && pr!=0)
        cout << 0 << "\n";
    else if (sum==0 && pr!=0)
        cout << 1 << "\n";
    else if (c!=0 && sum!=-(c))
        cout << c << "\n";
    else if (c!=0 && sum==-(c))
        cout << c+1 << "\n";
    sum=0;
    pr=1;
    c=0;
   }
}


#include <bits/stdc++.h>
#define ll long long int

using namespace std;

int main() {
    ll t;
    cin >> t;
    while(t--) {
        ll n;
        cin >> n;
        ll s = 0, z = 0, x;
        for(int i = 0;i < n;i++) {
            cin >> x;
            s += x;
            if(x == 0) z++;
        }
        if(s == 0 && z == 0) {
            cout << 1 << endl;
        } else if(s == 0 && z != 0) {
            cout << z << endl;
        } else if(s != 0 && z != 0) {
            cout << z + 1 << endl;
        } else {
            cout << 0 << endl;
        }
    }
}


#include <bits/stdc++.h>
#define ll long long int

using namespace std;

int main() {
    ll t;
    cin >> t;
    while(t--) {
        ll n;
        cin >> n;
        ll s = 0, z = 0, x;
        for(int i = 0;i < n;i++) {
            cin >> x;
            s += x;
            if(x == 0) z++;
        }
        if(z != 0) {
            if(s == -z) cout << z + 1 << endl;
            else cout << z << endl;
        } else if(s == 0) cout << 1 << endl;
        else cout << 0 << endl;
    }
}


#include <bits/stdc++.h>
#define ll long long int

using namespace std;

int main() {
    ll t;
    cin >> t;
    while(t--) {
        ll n;
        cin >> n;
        ll s = 0, z = 0, x;
        for(int i = 0;i < n;i++) {
            cin >> x;
            s += x;
            if(x == 0) z++;
        }
        if(s == 0 && z == 0) {
            cout << 1 << endl;
        } else if(s == 0 && z != 0) {
            cout << z << endl;
        } else if(s != 0 && z != 0) {
            if(s == -1) cout << z + 1 << endl;
            else cout << z << endl;
        } else {
            cout << 0 << endl;
        }
    }
}


#include <cstdio>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{

	int t;
	scanf("%d",&t);
	for (int i = 1 ; i <= t ; i ++)
	{
		int n;
		scanf("%d",&n);
		int zero = 0;
		int sum = 0;
		for (int j = 1 ; j <= n ; j ++)
		{
			int a;
			scanf("%d",&a);
			sum += a;
			zero += (a == 0);
		}
		printf("%d",zero + (-sum == zero));
	}

}


#include <cstdio>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{

	int t;
	scanf("%d",&t);
	for (int i = 1 ; i <= t ; i ++)
	{
		int n;
		scanf("%d",&n);
		int zero = 0;
		int sum = 0;
		for (int j = 1 ; j <= n ; j ++)
		{
			int a;
			scanf("%d",&a);
			sum += a;
			zero += (a == 0);
		}
		printf("%d\n",zero + (sum == zero));
	}

}


#include <cstdio>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{

	int t;
	scanf("%d",&t);
	for (int i = 1 ; i <= t ; i ++)
	{
		int n;
		scanf("%d",&n);
		int zero = 0;
		int sum = 0;
		for (int j = 1 ; j <= n ; j ++)
		{
			int a;
			scanf("%d",&a);
			sum += a;
			zero += (a == 0);
		}
		printf("%d\n",zero + (-sum == zero));
	}

}


#include<bits/stdc++.h>
using namespace std;

int main()
{

   int T; cin>>T;
   while(T--)
   {
           int p=1,s=0,z=0;
           int n; cin>>n;  int mi=9999999,ma=-9999999;
           int a[n];
           for(int i=0;i<n;i++){cin>>a[i]; s+=a[i]; p*=a[i];  if(!a[i])z++;
             }

          if(s && p)cout<<0<<endl;
          else if(z)cout<<z<<endl;
          else  cout<<1<<endl;

   }

        return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{

   int T; cin>>T;
   while(T--)
   {
           int n; cin>>n;  int mi=9999999,ma=-9999999;
           int a[n]; for(int i=0;i<n;i++){cin>>a[i]; mi=min(mi,a[i]);ma=max(ma,a[i]); }

          if((mi<0 && ma<0) || (mi>0 && ma>0))cout<<0<<endl;
          else if(mi==0 && ma==0)cout<<n<<endl;
          else { if(mi==-1 && ma==1)cout<<2<<endl; else cout<<1<<endl;   }

   }

        return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{

   int T; cin>>T;
   while(T--)
   {
           int p=1,s=0,z=0;
           int n; cin>>n;
           int a[n];
           for(int i=0;i<n;i++){cin>>a[i]; s+=a[i]; p*=a[i]; if(!a[i])z++;
         }

          if(s && p)cout<<0<<endl;
          else { if(p)cout<<1<<endl; else if(s==-z)cout<<z+1<<endl; else cout<<z<<endl;  }

   }

        return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{

   int T; cin>>T;
   while(T--)
   {
           int p=1,s=0;
           int n; cin>>n;  int mi=9999999,ma=-9999999;
           int a[n];
           for(int i=0;i<n;i++){cin>>a[i]; s+=a[i]; p*=a[i];
           mi=min(mi,a[i]);ma=max(ma,a[i]); }

          if(s && p)cout<<0<<endl;
          else if(mi==0 && ma==0)cout<<n<<endl;
          else { if(mi==-1 && ma==1)cout<<2<<endl; else cout<<1<<endl;   }

   }

        return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{

   int T; cin>>T;
   while(T--)
   {
           int p=1,s=0;
           int n; cin>>n;  int mi=9999999,ma=-9999999;
           int a[n];
           for(int i=0;i<n;i++){cin>>a[i]; s+=a[i]; p*=a[i];
           mi=min(mi,a[i]);ma=max(ma,a[i]); }

          if(s && p)cout<<0<<endl;
          else { if(p)cout<<1<<endl; else if(s==-z)cout<<z+1<<endl; else cout<<z<<endl;  }

   }

        return 0;
}


#include<bits/stdc++.h>
#define pb push_back
typedef long long int l1;
using namespace std;
#define input l1 t;cin>>t;while(t--)
int main(){
    input{
        l1 n;
        cin>>n;
        vector<l1>a;
        l1 b;
        l1 s=0;
        l1 p=1;
        for(l1 i=0;i<n;i++){
            cin>>b;
            a.push_back(b);
            p=p*b;
            s+=b;
        }
        l1 ans=0;
        l1 c=count(a.begin(),a.end(),0);
        if((c==0)&&(s==0)){
            ans=1;
        }
        else if((c!=0)&&(s==0)){
            ans =c;
        }
        else{
            if((c+s)==0){
                ans=c+1;
            }
            else{
                ans=c;
            }
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
#define pb push_back
typedef long long int l1;
using namespace std;
#define input l1 t;cin>>t;while(t--)
int main(){
    input{
        l1 n;
        cin>>n;
        vector<l1>a;
        l1 b;
        l1 s=0;
        l1 p=1;
        for(l1 i=0;i<n;i++){
            cin>>b;
            a.push_back(b);
            p=p*b;
            s+=b;
        }
        l1 ans=0;
        l1 c=count(a.begin(),a.end(),0);
        if((c==0)&&(s==0)){
            ans=0;
        }
        else if((c!=0)&&(s==0)){
            ans =c;
        }
        else{
            if((c+s)==0){
                ans=c+1;
            }
            else{
                ans=c;
            }
        }
        cout<<ans<<endl;
    }
}


 #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n;

        int cnts=0;
        int cntp=0;
        int ans=0;
        int sum;
        int t;
        scanf("%d",&t);
        while(t--)
        {

            scanf("%d",&n);
            int ara[n];
            for(int i=0;i<n;i++)
            {
                scanf("%d",&ara[i]);
            }
              for(int i=0;i<n;i++)
            {
                if(ara[i]==0)
                {
                    cntp++;
                    ara[i]=ara[i]+1;
                }
            }

            for(int i=0;i<n;i++)
            {
                sum+=ara[i];
            }

            if(sum==0)
            {
                cnts++;
            }
            ans=cnts+cntp;
            printf("%d\n",ans);

         cnts=0;
         cntp=0;
        ans=0;
        sum=0;

        }
        return 0;
    }


    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n;
        int ara[n];
        int cnts=0;
        int cntp=0;
        int ans=0;
        int sum;
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            for(int i=0;i<n;i++)
            {
                scanf("%d",&ara[i]);
            }
              for(int i=0;i<n;i++)
            {
                if(ara[i]==0)
                {
                    cntp++;
                    ara[i]=ara[i]+1;
                }
            }

            for(int i=0;i<n;i++)
            {
                sum+=ara[i];
            }

            if(sum==0)
            {
                cnts++;
            }
            ans=cnts+cntp;
            printf("%d\n",ans);

         cnts=0;
         cntp=0;
        ans=0;
        sum=0;

        }
        return 0;
    }


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    int ara[n];
    int cnts=0;
    int cntp=0;
    int ans=0;
    int sum;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&ara[i]);
        }
          for(int i=0;i<n;i++)
        {
            if(ara[i]==0)
            {
                cntp++;
                ara[i]=ara[i]+1;
            }
        }

        for(int i=0;i<n;i++)
        {
            sum+=ara[i];
        }

        if(sum==0)
        {
            cnts++;
        }
        ans=cnts+cntp;
        printf("%d\n",ans);

    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
long long t,n,a[107],sum = 0,proiz = 1,k = 0;
int main()
{
    cin >> t;
    for(int i = 1; i<=t; i++)
    {
        sum = 0;
        proiz = 1;
        k = 0;
        cin >> n;
        for(int j = 1; j<=n; j++)
        {
            cin >> a[j];
            sum +=a[j];
            if(a[j] == 0)
            {
                a[j]++;
                k++;
                sum++;
            }
        }
         if(sum == 0){
                a[1]++;
                k++;
                }
        cout << k << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
long long t,n,a[107],sum = 0,proiz = 1,k = 0;
int main()
{
    cin >> t;
    for(int i = 1;i<=t;i++){
        cin >> n;
        for(int i = 1;i<=n;i++){
            cin >> a[i];
            sum +=a[i];
            proiz = proiz * a[i];
        }
            while(sum == 0 || proiz == 0)
            k++;
            a[i]++;
    }
    cout << k << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
long long t,n,a[107],sum = 0,proiz = 1,k = 0;
int main()
{
    cin >> t;
    for(int i = 1; i<=t; i++)
    {
        sum = 0;
        proiz = 1;
        k = 0;
        cin >> n;
        for(int j = 1; j<=n; j++)
        {
            cin >> a[i];
            sum +=a[i];
            if(a[i] == 0)
            {
                a[i]++;
                k++;
                sum++;
            }
        }
         if(sum == 0){
                a[1]++;
                k++;
                }
        cout << k << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
long long t,n,a[107],sum = 0,proiz = 1,k = 0;
int main()
{
    cin >> t;
    for(int i = 1;i<=t;i++){
        cin >> n;
        for(int i = 1;i<=n;i++){
            cin >> a[i];
            sum +=a[i];
            proiz *= a[i];
        }
            while(sum == 0 && proiz == 0){
            k++;
            a[1]++;
            }

    cout << k << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin >>t;
	while(t--)
	{
		int n,sum=0,mul=1,ans=0;
		vector<int>arr,zero;
		cin >>n;
		for(int i=0;i<n;i++)
		{
			int a;
			cin >>a;
			arr.push_back(a);
			sum=sum+a;
			if(a==0)
			{
				zero.push_back(i);
			}
		}
		//cout <<"here z is"<<zero.size()<<" z "<<endl;
		if(zero.size()>0)
		{
			//cout <<zero.size()<<" z "<<endl;
			if(sum==0)
			{
				ans=zero.size();
				sum=sum+zero.size();
				//cout <<"here1";
				cout<<ans<<endl;
			}
			else if(sum>0)
			{
				ans=zero.size();
				sum=sum+zero.size();
				//cout <<"here2";
				cout<<ans<<endl;
			}
			else if(sum<0)
			{
				ans=zero.size();
				sum=sum-zero.size();
				//cout <<"here3";
				cout<<ans<<endl;
			}
		}
		if(sum==0)
		{
			ans=1;
			//cout <<"here4";
			cout<<ans<<endl;
		}
		if(sum!=0 && zero.size()==0)
		{
			//cout <<"here5";
			cout <<0<<endl;
		}

	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin >>t;
	while(t--)
	{
		int n,sum=0,mul=1,ans=0;
		vector<int>arr,zero;
		cin >>n;
		for(int i=0;i<n;i++)
		{
			int a;
			cin >>a;
			arr.push_back(a);
			sum=sum+a;
			if(a==0)
			{
				zero.push_back(i);
			}
		}
		if(zero.size()>0)
		{
			if(sum==0)
			{
				ans=zero.size();
				sum=sum+zero.size();
				cout<<ans<<endl;
			}
			if(sum>0)
			{
				ans=zero.size();
				sum=sum+zero.size();
				cout<<ans<<endl;
			}
			if(sum<0)
			{
				ans=zero.size();
				sum=sum-zero.size();
				cout<<ans<<endl;
			}
		}
		if(sum==0)
		{
			ans=1;
			cout<<ans<<endl;
		}

	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin >>t;
	while(t--)
	{
		int n,sum=0,mul=1,ans=0;
		vector<int>arr,zero;
		cin >>n;
		for(int i=0;i<n;i++)
		{
			int a;
			cin >>a;
			arr.push_back(a);
			sum=sum+a;
			if(a==0)
			{
				zero.push_back(i);
			}
		}
		//cout <<"here z is"<<zero.size()<<" z "<<endl;
		if(zero.size()>0)
		{
			ans=zero.size();
			sum=sum+zero.size();
			if(sum==0)
			{
				ans=ans+1;
				//cout <<"here2";
				cout<<ans<<endl;

			}
			else
			{
				cout <<ans<<endl;
			}
		}
		else if(sum==0)
		{
			ans=ans+1;
			//cout <<"here4";
			cout<<ans<<endl;
		}
		if(sum!=0 && zero.size()==0)
		{
			//cout <<"here5";
			cout <<0<<endl;
		}

	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin >>t;
	while(t--)
	{
		int n,sum=0,mul=1,ans=0;
		vector<int>arr,zero;
		cin >>n;
		for(int i=0;i<n;i++)
		{
			int a;
			cin >>a;
			arr.push_back(a);
			sum=sum+a;
			if(a==0)
			{
				zero.push_back(i);
			}
		}
		//cout <<"here z is"<<zero.size()<<" z "<<endl;
		if(zero.size()>0)
		{
			ans=zero.size()+1;
			sum=sum+zero.size();
		}
		if(sum==0)
		{
			ans=ans+1;
			//cout <<"here4";
			cout<<ans<<endl;
		}
		if(sum!=0 && zero.size()==0)
		{
			//cout <<"here5";
			cout <<0<<endl;
		}

	}
	return 0;
}


/*its_Rashid*/
#include <bits/stdc++.h>
using namespace std ;

typedef double du ;
typedef string st ;
typedef long long ll ;
typedef long double ld ;
typedef long long int lli ;

#define vi vector <lli>
#define loop(num) for (lli i = 0 ; i < num ; i ++ )
#define loop2(num) for (lli j = 0 ; j < num ; j ++ )
#define loopcin(num , ar ) for (lli i = 0 ; i < num ; i ++ ) {cin >> ar [i] ;}
#define loopcout(num , ar) for (lli i = 0 ; i < num ; i ++ ) {cout << ar [i] << ' '; }
#define looppu(num , ar) for (lli i = 0 ; i < num ;  i++) {lli x ; cin >> x ; ar . push_back (x) ;}
#define loopin(num , ar) for (lli i = 0 ; i < num ;  i++) {lli x ; cin >> x ; ar . insert (x) ;}

#define forr(num1 , num2) for (lli i = num1 ; i < num2 ; i ++ )
#define forrcin(num1 , num2 , ar ) for (lli i = num1 ; i < num2 ; i ++ ) {cin >> ar [i] ;}
#define forrcout(num1 , num2 , ar) for (lli i = num1 ; i < num2 ; i ++ ) {cout << ar [i] << ' '; }

#define D2forrcin(num1_1 , num1_2 , num2_1 , num2_2 , A ) for (lli i = num1_1 ; i < num1_2 ; i ++){ for (lli j = num2_1 ; j < num2_2 ; j++){cin >> A[i][j] ;}}
#define D2forrcout(num1_1 , num1_2 , num2_1 , num2_2 , A ) for (lli i = num1_1 ; i < num1_2 ; i ++){ for (lli j = num2_1 ; j < num2_2 ; j++){cout << A[i][j] << ' ' ;}cout << endl ;}

#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl

#define F first
#define S second
#define pb push_back
#define pf push_front
#define pob pop_back ()
#define pof pop_front ()

#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

signed main ()
{
    fastio
    ll n ; cin >> n ;
    while (n--) {
        ll s , sum = 0 , res = 0 ; cin >> s ;
        bool oks = false , oke = false ;
        ll a[s] ;
        loop(s) {
            cin >> a[i] ;
            sum += a[i] ;
            if (a[i] == 0) oke = true ;
            if (sum == 0) oks = true ;
        }

        if (oks && !oke) res = 1 ;
        else if (!oks && !oke) res = 0 ;
        else {
            ll cnt = 0 , sum = 0 ;
            loop(s) {
                if (a[i] == 0){
                    a[i] ++ ;
                     cnt ++ ;
                }
                sum += a[i] ;
            }
            res = (sum == 0 ? cnt + 1 : cnt) ;
        }
        cout << res << endl ;
    }
    return 0 ;
}

/*___________________________________________________________________functions___________________________________________________________________________*/


/*its_Rashid*/
#include <bits/stdc++.h>
using namespace std ;

typedef double du ;
typedef string st ;
typedef long long ll ;
typedef long double ld ;
typedef long long int lli ;

#define vi vector <lli>
#define loop(num) for (lli i = 0 ; i < num ; i ++ )
#define loop2(num) for (lli j = 0 ; j < num ; j ++ )
#define loopcin(num , ar ) for (lli i = 0 ; i < num ; i ++ ) {cin >> ar [i] ;}
#define loopcout(num , ar) for (lli i = 0 ; i < num ; i ++ ) {cout << ar [i] << ' '; }
#define looppu(num , ar) for (lli i = 0 ; i < num ;  i++) {lli x ; cin >> x ; ar . push_back (x) ;}
#define loopin(num , ar) for (lli i = 0 ; i < num ;  i++) {lli x ; cin >> x ; ar . insert (x) ;}

#define forr(num1 , num2) for (lli i = num1 ; i < num2 ; i ++ )
#define forrcin(num1 , num2 , ar ) for (lli i = num1 ; i < num2 ; i ++ ) {cin >> ar [i] ;}
#define forrcout(num1 , num2 , ar) for (lli i = num1 ; i < num2 ; i ++ ) {cout << ar [i] << ' '; }

#define D2forrcin(num1_1 , num1_2 , num2_1 , num2_2 , A ) for (lli i = num1_1 ; i < num1_2 ; i ++){ for (lli j = num2_1 ; j < num2_2 ; j++){cin >> A[i][j] ;}}
#define D2forrcout(num1_1 , num1_2 , num2_1 , num2_2 , A ) for (lli i = num1_1 ; i < num1_2 ; i ++){ for (lli j = num2_1 ; j < num2_2 ; j++){cout << A[i][j] << ' ' ;}cout << endl ;}

#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl

#define F first
#define S second
#define pb push_back
#define pf push_front
#define pob pop_back ()
#define pof pop_front ()

#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

signed main ()
{
    fastio
    ll n ; cin >> n ;
    //ll res[n] = {0} ;
    loop2(n) {
        ll s , sum = 0 ,res = 0 ; cin >> s ;
        bool oks = false , oke = false ;
        ll a[s] ;
        loop(s) {
            cin >> a[i] ;
            sum += a[i] ;
            if (a[i] == 0) oke = true ;
        }
        if (sum == 0) oks = true ;

        if (oks && !oke) res = 1 ;
        else if (!oks && !oke) res = 0 ;
        else {
            ll cnt = 0 , sum = 0 ;
            loop(s) {
                if (a[i] == 0){
                    a[i] ++ ;
                     cnt ++ ;
                }
                sum += a[i] ;
            }
            res = (sum == 0 ? cnt + 1 : cnt) ;
        }
        cout << res << endl ;
    }
    //loop(n) {cout << res[i] << endl ;}
    return 0 ;
}

/*___________________________________________________________________functions___________________________________________________________________________*/


/*its_Rashid*/
#include <bits/stdc++.h>
using namespace std ;

typedef double du ;
typedef string st ;
typedef long long ll ;
typedef long double ld ;
typedef long long int lli ;

#define vi vector <lli>
#define loop(num) for (lli i = 0 ; i < num ; i ++ )
#define loop2(num) for (lli j = 0 ; j < num ; j ++ )
#define loopcin(num , ar ) for (lli i = 0 ; i < num ; i ++ ) {cin >> ar [i] ;}
#define loopcout(num , ar) for (lli i = 0 ; i < num ; i ++ ) {cout << ar [i] << ' '; }
#define looppu(num , ar) for (lli i = 0 ; i < num ;  i++) {lli x ; cin >> x ; ar . push_back (x) ;}
#define loopin(num , ar) for (lli i = 0 ; i < num ;  i++) {lli x ; cin >> x ; ar . insert (x) ;}

#define forr(num1 , num2) for (lli i = num1 ; i < num2 ; i ++ )
#define forrcin(num1 , num2 , ar ) for (lli i = num1 ; i < num2 ; i ++ ) {cin >> ar [i] ;}
#define forrcout(num1 , num2 , ar) for (lli i = num1 ; i < num2 ; i ++ ) {cout << ar [i] << ' '; }

#define D2forrcin(num1_1 , num1_2 , num2_1 , num2_2 , A ) for (lli i = num1_1 ; i < num1_2 ; i ++){ for (lli j = num2_1 ; j < num2_2 ; j++){cin >> A[i][j] ;}}
#define D2forrcout(num1_1 , num1_2 , num2_1 , num2_2 , A ) for (lli i = num1_1 ; i < num1_2 ; i ++){ for (lli j = num2_1 ; j < num2_2 ; j++){cout << A[i][j] << ' ' ;}cout << endl ;}

#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl

#define F first
#define S second
#define pb push_back
#define pf push_front
#define pob pop_back ()
#define pof pop_front ()

#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

signed main ()
{
    fastio
    ll n ; cin >> n ;
    ll res[n] = {0} ;
    loop2(n) {
        ll s , sum = 0 ; cin >> s ;
        bool oks = false , oke = false ;
        ll a[s] ;
        loop(s) {
            cin >> a[i] ;
            sum += a[i] ;
            if (a[i] == 0) oke = true ;
        }
        if (sum == 0) oks = true ;

        if (oks && !oke) res[j] = 1 ;
        else if (!oks && !oke) res[j] = 0 ;
        else {
            ll cnt = 0 , sum = 0 ;
            loop(s) {
                if (a[i] == 0){
                    a[i] ++ ;
                     cnt ++ ;
                }
                sum += a[i] ;
            }
            res[j] = (sum == 0 ? cnt + 1 : cnt) ;
        }
    }
    loop(n) {cout << res[i] << endl ;}
    return 0 ;
}

/*___________________________________________________________________functions___________________________________________________________________________*/


#include <cstdio>
#include <cstring>
using namespace std;
const int N = 105;
int t, n, p;
int main() {
	scanf("%d", &t);
	while (t--) {
		int ans = 0, sum = 0, cnt0 = 0;
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) {
			scanf("%d", &p);
			if (p == 0) cnt0++;
			sum += p;
		}
		ans += cnt0;
		sum += cnt0;
		if (sum != 0) printf("%d\n", ans);
		else printf("%d\n", ans - sum + 1);
	}
	return 0;
}


#include <iostream>

using namespace std;
int main ()
{
    int testcase;
    cin>>testcase ;
    for(int i=0;i<testcase;i++)
    {
        int n ;
        cin>>n;
        int arr[n],value=0,sum=0,a=0;
        for(int j=0;j<n;j++)
            cin>>arr[j];
        while(sum == 0 ){
            for(int j=0;j<n;j++){
                sum=sum+arr[j];
                if(arr[j]==0){
                    value++;
                    arr[j]=1;
                    a=1;
                    break ;
                }
            }
            if(sum==0 && a == 0){
                for(int k=0;k<n;k++){
                    if(arr[k]>0){
                      arr[k]=arr[k]+1;
                      value++;
                      break ;
                    }
                }

            }
            if(a){
                a=0;
                sum=0;
            }
        }
        cout<<value<<"\n";
    }
}


#include <iostream>
#include <stdio.h>
using namespace std;
int main ()
{
    int testcase;
    cin>>testcase ;

    for(int i=0;i<testcase;i++)
    {
        int n ;
        cin>>n;
        int arr[n],value=0,sum=0,a=0;
        for(int j=0;j<n;j++)
            cin>>arr[j];
        while(sum == 0 ){
            for(int j=0;j<n;j++){
                sum=sum+arr[j];
                if(arr[j]==0){
                    value++;
                    arr[j]=1;
                    a=1;
                    break ;
                }
            }
            if(sum==0 && a == 0){
                arr[0]=arr[0]+1;
                value++;
            }
            if(a){
                a=0;
                sum=0;
            }
        }
        cout<<value;
    }
}


#include <iostream>
#include <stdio.h>
using namespace std;
int main ()
{
    int testcase;
    cin>>testcase ;

    for(int i=0;i<testcase;i++)
    {
        int n ;
        cin>>n;
        int arr[n],value=0,sum=0,a=0;
        for(int j=0;j<n;j++)
            cin>>arr[j];
        while(sum == 0 ){
            for(int j=0;j<n;j++){
                sum=sum+arr[j];
                if(arr[j]==0){
                    value++;
                    arr[j]=1;
                    a=1;
                    break ;
                }
            }
            if(sum==0 && a == 0){
                arr[0]=arr[0]+1;
                value++;
            }
            if(a){
                a=0;
                sum=0;
            }
        }
        cout<<value<<"\n";
    }
}


#include <iostream>

using namespace std;
int main ()
{
    int testcase;
    cin>>testcase ;
    for(int i=0;i<testcase;i++)
    {
        int n ;
        cin>>n;
        int arr[n],value=0,sum=0,a=0;
        for(int j=0;j<n;j++)
            cin>>arr[j];
        while(sum == 0 ){
            for(int j=0;j<n;j++){
                sum=sum+arr[j];
                if(arr[j]==0){
                    value++;
                    arr[j]=1;
                    a=1;
                    break ;
                }
            }
            if(sum==0 && a == 0){
                arr[0]=arr[0]+1;
                value++;
            }
            if(a){
                a=0;
                sum=0;
            }
        }
        cout<<value<<"\n";
    }
}


#include <iostream>
using namespace std;
int main() {
    int t,n,a,sum,pr,k;
    cin >>t;
    int arr[t];
    for (int i = 0; i < t; i++){
        cin >>n; k=0; sum=0; pr=1;
        for (int j = 0; j < n; j++){
           cin >>a;
           if (a==0) {a+=1; k++;}
           sum+=a; pr*=a;}
           if (sum!=0)
              arr[i]=k;
           else arr[i]=k+1;}
    for (int i = 0; i < t; i++)
        cout<<arr[i]<<endl;

}


#include <bits/stdc++.h>
using namespace std;
int main() {
    int t,n,a,sum,pr,k;
    cin >>t;
    int arr[t]; arr[t]={0}; sum=0; pr=1; k=0;
    for (int i = 0; i < t; i++){
        cin >>n; k=0;
        for (int j = 0; j < n; j++){
           cin >>a;
           if (a==0) {a=1; k++;}
           sum+=a; pr*=a;}
           if (sum==0) {k++;}
           arr[i]=k;}
    for (int i = 0; i < t; i++)
        cout<<arr[i]<<endl;

}


#include <iostream>
using namespace std;
int main() {
    int t,n,a,sum,pr,k;
    cin >>t;
    int arr[t]; sum=0; pr=1; k=0;
    for (int i = 0; i < t; i++){
        cin >>n; k=0;
        for (int j = 0; j < n; j++){
           cin >>a;
           if (a==0) {a=1; k++;}
           sum+=a; pr*=a;}
           if (sum!=0)
              arr[i]=k;
           else arr[i]=k+1;}
    for (int i = 0; i < t; i++)
        cout<<arr[i]<<endl;

}


#include <bits/stdc++.h>
using namespace std;
int main() {
    int t,n,a,sum,pr,k;
    cin >>t;
    int arr[t]; sum=0; pr=1; k=0;
    for (int i = 0; i < t; i++){
        cin >>n; k=0;
        for (int j = 0; j < n; j++){
           cin >>a;
           if (a==0) {a=1; k++;}
           sum+=a; pr*=a;}
           if (sum==0) {k++;}
           arr[i]=k;}
    for (int i = 0; i < t; i++)
        cout<<arr[i]<<endl;

}


    #include<bits/stdc++.h>

    using namespace std;

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

        int n;
        cin >> n;
        int sum = 0;
        int zeros = 0;
         while(n--){
            int ln = 0;
            cin >> ln;
            int x;
            zeros = 0;
            sum = 0;

            for(int i = 0 ; i < ln; i++){
                cin >> x;
                sum += x;
                if(x == 0){
                   ++zeros;
                }
            }

            if(sum+zeros < 0){
                while(sum+zeros < 1) ++zeros;
            }
            cout << zeros << endl;
        }

        return 0;
    }


#include<bits/stdc++.h>

using namespace std;

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

    int n;
    cin >> n;
    int v[n];
    int sum = 0;
    int zeros = 0;
    int ind = 0;
    while(n--){
        int ln = 0;
        cin >> ln;
        int x;
        zeros = 0;
        sum = 0;
        for(int i = 0 ; i < ln; i++){
            cin >> x;
            sum += x;
            if(x == 0){
                zeros++;
            }
        }
        if(sum <=0){
            while(sum+zeros < 1) zeros++ , sum++;
        }
        v[ind++] = zeros;
    }
        for(int i = 0 ; i < 4; i++){
        cout << v[i] << endl;
    }

    return 0;
}


    #include<bits/stdc++.h>

    using namespace std;

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

		int n;
        cin >> n;

        while(n--){
            int ln = 0;
            cin >> ln;
            int sum = 0;
			int z = 0;
            for(int i = 0 ; i < ln; i++){
                int x;
                cin >> x;
                sum += x;
                if(x == 0){
                  z +=1;
                }
            }
            if(sum+z < 1){
				int cnt = 0;
				while(sum+z <= 0){ ++cnt; sum++;}
				cout << cnt+z << endl;
            }else{
				cout << z << endl;
			}
        }

        return 0;
    }


    #include<bits/stdc++.h>

    using namespace std;

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

		int n;
        cin >> n;

        while(n--){
            int ln = 0;
            cin >> ln;
            int sum = 0;
			int z = 0;

            for(int i = 0 ; i < ln; i++){
                int x;
                cin >> x;
                sum += x;
                if(x == 0){
                  z +=1;
                }
            }

            if(z == 0){
				if(sum == 0){
					cout << 1 << endl;
				}else{
					cout << 0 << endl;
				}
			}else{
				sum = sum + z;
				if(sum == 0){
					cout << z + 1 << endl;
				}else{
					cout << z << endl;
				}
			}

        }

        return 0;
    }


    #include<bits/stdc++.h>

    using namespace std;

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

        int n;
        cin >> n;
        int sum = 0;
        int zeros = 0;
         while(n--){
            int ln = 0;
            cin >> ln;
            int x;
            zeros = 0;
            sum = 0;

            for(int i = 0 ; i < ln; i++){
                cin >> x;
                sum += x;
                if(x == 0){
                   ++zeros;
                }
            }

            if(sum+zeros <=0){
                while(sum+zeros < 1) ++zeros;
            }
            cout << zeros << endl;
        }

        return 0;
    }


    #include<bits/stdc++.h>

    using namespace std;

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

        int n;
        cin >> n;
        int sum = 0;
        int zeros = 0;
         while(n--){
            int ln = 0;
            cin >> ln;
            zeros = 0;
            sum = 0;
            for(int i = 0 ; i < ln; i++){
                int x;
                cin >> x;
                sum += x;
                if(x == 0){
                    ++zeros;
                }
            }

            if(sum+zeros <=0){
                while(sum+zeros <= 0) zeros++;
            }
            cout << zeros << endl;
        }

        return 0;
    }


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t, i;
    cin >> t;
    while (t--)
    {
        long long x=0, c = 0, y = 0,ans;
        int n;

        cin >> n;
        int a[n + 2];
        for (i = 0; i < n; i++)
        {
            cin >> a[i];
            if (a[i] > 0)
            {
               x=x+a[i];
            }else if(a[i]<0)
            {
            y=y+a[i];
            }else if(a[i]==0)

            {
            x++;
            c++;
            }

        }
        ans=x+y;
     if(ans>0)
     {
        cout<<c<<endl;
     }else
     {
        cout<<c+abs(x+y)+1<<endl;
     }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t, i;
    cin >> t;
    while (t--)
    {
        long long x=0, c = 0, y = 0,ans,g;
        int n;

        cin >> n;

        for (i = 0; i < n; i++)
        {
            cin >> g;
            if (g > 0)
            {
               x=x+g;
            }else if(g<0)
            {
            y=y+g;
            }else if(g==0)

            {
            x++;
            c++;
            }

        }
        ans=x+y;
     if(ans!=0)
     {
        cout<<c<<endl;
     }else
     {
        cout<<c+abs(x+y)+1<<endl;
     }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,i;
    cin>>t;
    while(t--)
    {
        long long s=0,c=0,s1=0;
        int n;

        cin>>n;
        int a[n+2];
        for(i=0;i<n;i++)
        {
            cin>>a[i];
            s1=s1+a[i];
            if(a[i]==0)
            {
             a[i]=1;
             c++;
            }
            s=s+a[i];

        }
        if(s1>0)
        {
            cout<<"0"<<endl;
        }else if(s>0)
        {
            cout<<c<<endl;
        }else
        {
        cout<<abs(1-s)+c<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t, i;
    cin >> t;
    while (t--)
    {
        long long x=0, c = 0, y = 0,ans,g;
        int n;

        cin >> n;

        for (i = 0; i < n; i++)
        {
            cin >> g;
            if (g > 0)
            {
               x=x+g;
            }else if(g<0)
            {
            y=y+g;
            }else if(g==0)

            {
            x++;
            c++;
            }

        }
        ans=x+y;
     if(ans>0)
     {
        cout<<c<<endl;
     }else
     {
        cout<<c+abs(x+y)+1<<endl;
     }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, i, ans;
        cin >> n;
        int x, m;
        m = n / 2;
        n = n * 2;
        int a[n + 4];
        for (i = 1; i <= n; i++)
        {
            cin >> a[i];
        }
        sort(a + 1, a + n + 1);
        x = n / 2;
            ans = a[x+1]-a[x];

        cout << ans << endl;
    }
    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;

int main()
{   long N,i,sum,num,n,x;
    scanf("%ld",&N);
    while(N--){
        sum=num=0;
        scanf("%ld",&n);
        for(i=1;i<=n;i++){
            scanf("%ld",&x);
            sum+=x;
            if(!x)
                num++;
        }
        //printf("%ld\n",num+((!sum)?1:0));
        if(!num){
            if(!sum)
                printf("1\n");
            else
                printf("0\n");
        }else
            printf("%ld\n",num);
    }
    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;

int main()
{   long N,i,sum,num,n,x;
    scanf("%ld",&N);
    while(N--){
        sum=num=0;
        scanf("%ld",&n);
        for(i=1;i<=n;i++){
            scanf("%ld",&x);
            sum+=x;
            if(!x)
                num++;
        }
        //printf("%ld\n",num+((!sum)?1:0));
        if(!num){
            if(!sum)
                printf("1\n");
            else
                printf("0\n");
        }else
            printf("%ld\n",num+((num+sum==0)?1:0));
    }
    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;

int main()
{   long N,i,sum,num,n,x;
    scanf("%ld",&N);
    while(N--){
        sum=num=0;
        scanf("%ld",&n);
        for(i=1;i<=n;i++){
            scanf("%ld",&x);
            sum+=x;
            if(!x)
                num++;
        }
        printf("%ld\n",num+((num+sum==0)?1:0));
    }
    return 0;
}


#include <bits/stdc++.h>
#define M_pi 3.1415926535
using namespace std;
typedef pair<int,int> ii;
typedef vector<ii> vii;
int n,t,s,res,k;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    scanf("%d",&t);
    while(t--){
        res=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&k);
            if(k==0) {res++; s++;}
            else s+=k;
        }
        if (s==0) res++;
        printf("%d\n",res);
    }
    return 0;
}


#include <bits/stdc++.h>
#define M_pi 3.1415926535
using namespace std;
int n,t,s,res,k;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    scanf("%d",&t);
    while(t--){
        res=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&k);
            if(k==0) {res++; s++;}
            else s+=k;
        }
        if (s==0) res++;
        printf("%d\n",res);
    }
    return 0;
}


#include <bits/stdc++.h>
#define M_pi 3.1415926535
using namespace std;
typedef pair<int,int> ii;
typedef vector<ii> vii;
int n,t,s,res,k;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    scanf("%d",&t);
    while(t--){
        res=s=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&k);
            if(k==0) {res++; s++;}
            else s+=k;
        }
        if (s==0) res++;
        printf("%d\n",res);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define str string
#define all(x) (x).begin(),(x).end()
#define rep(x,b,c) for(int x=b;x<c;x++)
#define  pb(x) push_back(x)
vector<int>v;
int n;
int ans=0;
int main(){
	cin>>n;
	rep(i,0,n){
		int num=0;
		int k;
		cin>>k;
		rep(j,0,k){
			int a;
			cin>>a;
			if(a==0){
				v.push_back(1);
				ans++;
				num++;
			}
			else{
				v.push_back(a);
				num+=a;
			}
		}
		if(num<=0) ans+=1-num;
		v.clear();
		cout<<ans<<endl;
		ans=0;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define str string
#define all(x) (x).begin(),(x).end()
#define rep(x,b,c) for(int x=b;x<c;x++)
#define  pb(x) push_back(x)
vector<int>v;
int n;
int ans=0;
int main(){
	cin>>n;
	rep(i,0,n){
		int num=0;
		int k;
		cin>>k;
		rep(j,0,k){
			int a;
			cin>>a;
			if(a==0){
				v.push_back(1);
				ans++;
			}
			else v.push_back(a);
			num+=v[v.size()-1];
		}
		if(num<=0) ans+=1-num;
		v.clear();
		cout<<ans<<endl;
		ans=0;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define str string
#define all(x) (x).begin(),(x).end()
#define rep(x,b,c) for(int x=b;x<c;x++)
#define  pb(x) push_back(x)
vector<int>v;
int n;
int ans=0;
int main(){
	cin>>n;
	rep(i,0,n){
		int num=0;
		int k;
		cin>>k;
		rep(j,0,k){
			int a;
			cin>>a;
			if(a==0){
				v.push_back(1);
				ans++;
				num++;
			}
			else{
				v.push_back(a);
				num+=a;
			}
		}
		if(num==0) ans+=1;
		v.clear();
		cout<<ans<<endl;
		ans=0;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define str string
#define all(x) (x).begin(),(x).end()
#define rep(x,b,c) for(int x=b;x<c;x++)
#define  pb(x) push_back(x)
vector<int>v;
int n;
int ans=0;
int main(){
	cin>>n;
	rep(i,0,n){
		int num=0;
		int k;
		cin>>k;
		rep(j,0,k){
			int a;
			cin>>a;
			if(a==0){
				v.push_back(1);
				ans++;
			}
			else v.push_back(a);
			num+=v[v.size()-1];
		}
		if(num<=0) ans+=1-num;
		v.clear();
		cout<<ans<<endl;

ans=0;
	}
	return 0;
}


#include <iostream>
#include<bits/stdc++.h>
#define var long long int
using namespace std;

int main() {
    int T, N;
    cin >> T;
    while (T --) {
        cin >> N;
        int Counter = 0, Sum = 0;
        for (int i = 0, Number; i < N; i ++) {
            cin >> Number;
            if (!Number)
                Counter ++;
            Sum += Number;
        }
        cout << "ANS ";
        if (Counter)
            if (!(Sum + Counter))
                cout << Counter + 1 << endl;
            else
                cout << Counter << endl;
        else if (!Sum)
            cout << 1 << endl;
        else
            cout << 0 << endl;
    }
}


#include <iostream>
#include<bits/stdc++.h>
#define var long long int
using namespace std;

int main() {
    int T, N;
    cin >> T;
    while (T --) {
        cin >> N;
        int Counter = 0, Sum = 0;
        for (int i = 0, Number; i < N; i ++) {
            cin >> Number;
            if (!Number)
                Counter ++;
            Sum += Number;
        }
        if (Counter)
            cout << Counter << endl;
        else if (!Sum)
            cout << 1 << endl;
        else
            cout << 0 << endl;
    }
}


#include <iostream>
#include<bits/stdc++.h>
#define var long long int
using namespace std;

int main() {
    int T, N;
    cin >> T;
    while (T --) {
        cin >> N;
        int Counter = 0, Sum = 0;
        for (int i = 0, Number; i < N; i ++) {
            cin >> Number;
            if (!Number)
                Counter ++;
            Sum += Number;
        }
        //cout << "ANS ";
        if (Counter)
            if (!(Sum + Counter))
                cout << Counter + 1 << endl;
            else
                cout << Counter << endl;
        else if (!Sum)
            cout << 1 << endl;
        else
            cout << 0 << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int t,n;
int a[105];
int main(){
	cin>>t;
	while(t--){
		cin>>n;
		int nol = 0,sum = 0,mini = 2e9;
		for(int i = 0;i<n;i++){
			cin>>a[i];
			if(!a[i]) nol++;
			sum+=a[i];
			mini = min(mini,a[i]);
		}
		int ans = 0;
		if(sum== 0) ans = -sum+1;
		if(nol && sum==0) ans+=max(0,nol-(-sum+1));
		else if(nol) {
			ans+=nol;
			if(sum+nol == 0) ans++;
		}
		cout<<ans<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int t,n;
int a[105];
int main(){
	cin>>t;
	while(t--){
		cin>>n;
		int nol = 0,sum = 0,mini = 2e9;
		for(int i = 0;i<n;i++){
			cin>>a[i];
			if(!a[i]) nol++;
			sum+=a[i];
			mini = min(mini,a[i]);
		}
		int ans = 0;
		if(sum <=0) ans = -sum+1;
		cout<<ans<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int t,n;
int a[105];
int main(){
	cin>>t;
	while(t--){
		cin>>n;
		int nol = 0,sum = 0,mini = 2e9;
		for(int i = 0;i<n;i++){
			cin>>a[i];
			if(!a[i]) nol++;
			sum+=a[i];
			mini = min(mini,a[i]);
		}
		int ans = 0;
		if(sum <=0) ans = -sum+1;
		if(nol) ans+=max(0,nol-(-sum+1));
		cout<<ans<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int t,n;
int a[105];
int main(){
	cin>>t;
	while(t--){
		cin>>n;
		int nol = 0,sum = 0,mini = 2e9;
		for(int i = 0;i<n;i++){
			cin>>a[i];
			if(!a[i]) nol++;
			sum+=a[i];
			mini = min(mini,a[i]);
		}
		int ans = 0;
		if(sum <=0) ans = -sum+1;
		if(nol && sum<=0) ans+=max(0,nol-(-sum+1));
		else if(nol) ans+=nol;
		cout<<ans<<"\n";
	}
	return 0;
}