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

int main(){
int t;
cin>>t;
while(t--)
{int n;
cin>>n;
vector <long long int> a;
for(int i=0;i<n;i++)
{
	long long int x;
	cin>>x;
	a.push_back(x);
 }

 sort(a.begin(),a.end());
 long long int sum=0;
 for(int i=0;i<n-1;i++)
 sum=sum+a[i]-a[n-1];
long long int ans=-sum-a[n-1];
 if(n==2)
 cout<<"0"<<endl;
 else if(sum==0&&a[n-1]==0)
  cout<<"0"<<endl;
 else if(sum==0)
 {if(a[n-1]>n-1)
 {if(a[n-1]%(n-1)==0)
 cout<<"0"<<endl;
 else
 cout<<(a[n-1]/(n-1))*(n-1)+n-1-a[n-1]<<endl;

 }
 else
 cout<<n-1-a[n-1]<<endl;

 }
 else
{long long int ans=0;
for(int i=1;i<n;i++)
{ans=ans+a[n-1]-a[i];
}
if(ans>=a[0])
cout<<ans-a[0]<<endl;
else
{a[0]=a[0]-ans;
if(a[0]>n-1)
 {if(a[0]%(n-1)==0)
 cout<<"0"<<endl;
 else
 cout<<(a[0]/(n-1))*(n-1)+n-1-a[0]<<endl;
 }
else
 cout<<n-1-a[0]<<endl;

}

}
}

}


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

int main(){
int t;
cin>>t;
while(t--)
{int n;
cin>>n;
vector <long long int> a;
for(int i=0;i<n;i++)
{
	long long int x;
	cin>>x;
	a.push_back(x);
 }

 sort(a.begin(),a.end());
 long long int sum=0;
 for(int i=0;i<n-1;i++)
 sum=sum+a[i]-a[n-1];
long long int ans=-sum-a[n-1];
 if(n==2)
 cout<<"0"<<endl;
 else
 if(ans>0)
 cout<<ans<<endl;
 else
 cout<<-ans<<endl;

}

}


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

int main(){
int t;
cin>>t;
while(t--)
{int n;
cin>>n;
vector <long long int> a;
for(int i=0;i<n;i++)
{
	long long int x;
	cin>>x;
	a.push_back(x);
 }

 sort(a.begin(),a.end());
 long long int sum=0;
 for(int i=0;i<n-1;i++)
 sum=sum+a[i]-a[n-1];
long long int ans=-sum-a[n-1];
 if(n==2)
 cout<<"0"<<endl;
 else if(sum==0&&a[n-1]==0)
  cout<<"0"<<endl;
 else if(sum==0)
 {if(a[n-1]>n-1)
 {if(a[n-1]%(n-1)==0)
 cout<<"0"<<endl;
 else
 cout<<(a[n-1]/(n-1))*(n-1)+n-1-a[n-1]<<endl;

 }
 else
 cout<<n-1-a[n-1]<<endl;

 }
 else
{int ans=0;
for(int i=1;i<n;i++)
{ans=ans+a[n-1]-a[i];
}
if(ans>=a[0])
cout<<ans-a[0]<<endl;
else
{a[0]=a[0]-ans;
if(a[0]>n-1)
 {if(a[0]%(n-1)==0)
 cout<<"0"<<endl;
 else
 cout<<(a[0]/(n-1))*(n-1)+n-1-a[0]<<endl;
 }
else
 cout<<n-1-a[0]<<endl;

}

}
}

}


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

int main(){
int t;
cin>>t;
while(t--)
{int n;
cin>>n;
vector <long long int> a;
for(int i=0;i<n;i++)
{
	long long int x;
	cin>>x;
	a.push_back(x);
 }

 sort(a.begin(),a.end());
 long long int sum=0;
 for(int i=0;i<n-1;i++)
 sum=sum+a[i]-a[n-1];
long long int ans=-sum-a[n-1];
 if(n==2)
 cout<<"0"<<endl;
 else if(sum==0)
 {if(a[n-1]>n-1)
 {if(a[n-1]%(n-1)==0)
 cout<<"0"<<endl;
 else
 cout<<(a[n-1]/(n-1))*(n-1)+n-1-a[n-1]<<endl;

 }
 else
 cout<<n-1-a[n-1]<<endl;

 }
 else
 if(ans>0)
 cout<<ans<<endl;
 else
 cout<<-ans<<endl;

}

}


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

int main(){
int t;
cin>>t;
while(t--)
{int n;
cin>>n;
vector <long long int> a;
for(int i=0;i<n;i++)
{
	long long int x;
	cin>>x;
	a.push_back(x);
 }

 sort(a.begin(),a.end());
 long long int sum=0;
 for(int i=0;i<n-1;i++)
 sum=sum+a[i]-a[n-1];
 long long int ans=-sum-a[n-1];
 if(ans>0)
 cout<<ans<<endl;
 else
 cout<<-ans<<endl;

}

}


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

int main(){
int t;
cin>>t;
while(t--)
{int n;
cin>>n;
vector <long long int> a;
for(int i=0;i<n;i++)
{
	long long int x;
	cin>>x;
	a.push_back(x);
 }

 sort(a.begin(),a.end());
 long long int sum=0;
 for(int i=0;i<n-1;i++)
 sum=sum+a[i]-a[n-1];
long long int ans=-sum-a[n-1];
 if(n==2)
 cout<<"0"<<endl;
 else if(sum==0&&a[n-1]==0)
  cout<<"0"<<endl;
 else if(sum==0)
 {if(a[n-1]>n-1)
 {if(a[n-1]%(n-1)==0)
 cout<<"0"<<endl;
 else
 cout<<(a[n-1]/(n-1))*(n-1)+n-1-a[n-1]<<endl;

 }
 else
 cout<<n-1-a[n-1]<<endl;

 }
 else
 if(ans>0)
 cout<<ans<<endl;
 else
 cout<<-ans<<endl;

}

}


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

int main(){
int t;
cin>>t;
while(t--)
{int n;
cin>>n;
vector <long long int> a;
long long int sum=0,max=0;
for(int i=0;i<n;i++)
{
	long long int x;
	cin>>x;
	sum+=x;
	if(x>max)
	max=x;
	a.push_back(x);
 }
 long long int k=0;
 if(sum%(n-1)==0)
 k=sum/(n-1);
 else
 k=(sum/(n-1)+1);
 if(max<=k)
 cout<<k*(n-1)-sum<<endl;
 else
 cout<<max*(n-1)-sum<<endl;

}

}


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

int main(){
int t;
cin>>t;
while(t--)
{int n;
cin>>n;
vector <long long int> a;
long long int sum=0,max=0;
for(int i=0;i<n;i++)
{
	long long int x;
	cin>>x;
	sum+=x;
	if(x>max)
	max=x;
	a.push_back(x);
 }
 long long int k=ceil((float)sum/(n-1));
 if(max<=k)
 cout<<k*(n-1)-sum<<endl;
 else
 cout<<max*(n-1)-sum<<endl;

}

}


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

int main(){
int t;
cin>>t;
while(t--)
{int n;
cin>>n;
vector <long long int> a;
long long int sum=0;
for(int i=0;i<n;i++)
{
	long long int x;
	cin>>x;
	sum+=x;
	a.push_back(x);
 }

 sort(a.begin(),a.end());
 long long int k=ceil((float)sum/(n-1));
 int j;
 for( j=0;j<n;j++)
 if(a[j]>k)
 break;
 if(j==n)
 cout<<k*(n-1)-sum<<endl;
 else
 cout<<a[j]*(n-1)-sum<<endl;

}

}


#include<bits/stdc++.h>
#define int long long
int a[100100];
using namespace std;
signed main(){
	int t; cin>>t;
	while(t--){
		int n; cin>>n;
		int minx=1999999999,maxx=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			if(a[i]>maxx) maxx=a[i];
			if(a[i]<minx) minx=a[i];
		}
		int sum=0,cnt=0;
		for(int i=1;i<=n;i++){
			if(a[i]!=minx||cnt>0)sum+=maxx-a[i];
			else if(a[i]==minx)cnt++;
		}
		if(sum>=minx)cout<<sum-minx<<endl;
		else {
			int cnt;
			cnt=minx/(n-1);
			cout<<sum+cnt*(n-1)-minx<<endl;
		}
	}
}


#include<bits/stdc++.h>
#define int long long
int a[100100];
using namespace std;
signed main(){
	int t; cin>>t;
	while(t--){
		int n; cin>>n;
		int minx=1999999999,maxx=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			if(a[i]>maxx) maxx=a[i];
			if(a[i]<minx) minx=a[i];
		}
		int sum=0,cnt=0;
		for(int i=1;i<=n;i++){
			if(a[i]!=minx||cnt>0)sum+=maxx-a[i];
			else if(a[i]==minx)cnt++;
		}
		if(sum>=minx)cout<<sum-minx<<endl;
		else cout<<n-1-(minx-sum)%(n-1)<<endl;
	}
}


#include<bits/stdc++.h>
#define int long long
int a[100100];
using namespace std;
signed main(){
	int t; cin>>t;
	while(t--){
		int n; cin>>n;
		int minx=1999999999,maxx=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			if(a[i]>maxx) maxx=a[i];
			if(a[i]<minx) minx=a[i];
		}
		int sum=0,cnt=0;
		for(int i=1;i<=n;i++){
			if(a[i]!=minx||cnt>0)sum+=maxx-a[i];
			else if(a[i]==minx)cnt++;
		}
		if(sum>=minx)cout<<sum-minx<<endl;
		else {
			int cnt2;
			cnt2=(minx-sum)/(n-1);
			cout<<sum+(cnt2+1)*(n-1)-minx<<endl;
		}
	}
}


#include<bits/stdc++.h>
#define int long long
int a[100100];
using namespace std;
signed main(){
	int t; cin>>t;
	while(t--){
		int n; cin>>n;
		int minx=1999999999,maxx=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			if(a[i]>maxx) maxx=a[i];
			if(a[i]<minx) minx=a[i];
		}
		int sum=0,cnt=0;
		for(int i=1;i<=n;i++){
			if(a[i]!=minx||cnt>0)sum+=maxx-a[i];
			else if(a[i]==minx)cnt++;
		}
		if(sum>=minx)cout<<sum-minx<<endl;
		else {
			int x=(minx-sum+n-2)/(n-1);
			if(x==0)x=1;
			sum+=x*(n-1);
			cout<<sum-minx<<endl;
		}
	}
}


#include<bits/stdc++.h>
#define int long long
int a[100100];
using namespace std;
signed main(){
	int t; cin>>t;
	while(t--){
		int n; cin>>n;
		int minx=1999999999,maxx=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			if(a[i]>maxx) maxx=a[i];
			if(a[i]<minx) minx=a[i];
		}
		int sum=0,cnt=0;
		for(int i=1;i<=n;i++){
			if(a[i]!=minx||cnt>0)sum+=maxx-a[i];
			else if(a[i]==minx)cnt++;
		}
		if(sum>=minx)cout<<sum-minx<<endl;
		else {
			int cnt;
			cnt=(minx-sum)/(n-1);
			cout<<sum+(cnt+1)*(n-1)-minx<<endl;
		}
	}
}


#include<bits/stdc++.h>
#define int long long
int a[100100];
using namespace std;
signed main(){
	int t; cin>>t;
	while(t--){
		int n; cin>>n;
		int minx=1999999999,maxx=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			if(a[i]>maxx) maxx=a[i];
			if(a[i]<minx) minx=a[i];
		}
		int sum=0,cnt=0;
		for(int i=1;i<=n;i++){
			if(a[i]!=minx||cnt>0)sum+=maxx-a[i];
			else if(a[i]==minx)cnt++;
		}
		if(sum>=minx)cout<<sum-minx<<endl;
		else cout<<(n-1-minx+sum)%(n-1)<<endl;
	}
}


#include<bits/stdc++.h>
#define int long long
int a[100100];
using namespace std;
signed main(){
	int t; cin>>t;
	while(t--){
		int n; cin>>n;
		int minx=1999999999,maxx=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			if(a[i]>maxx) maxx=a[i];
			if(a[i]<minx) minx=a[i];
		}
		int sum=0,cnt=0;
		for(int i=1;i<=n;i++){
			if(a[i]!=minx||cnt>0)sum+=maxx-a[i];
			else if(a[i]==minx)cnt++;
		}
		if(sum>=minx)cout<<sum-minx<<endl;
		else {
			while(sum-minx<0){
			sum+=n-1;
			}
			cout<<sum-minx<<endl;
		}
	}
}


#include<bits/stdc++.h>
#define int long long
int a[100100];
using namespace std;
signed main(){
	int t; cin>>t;
	while(t--){
		int n; cin>>n;
		int minx=1999999999,maxx=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			if(a[i]>maxx) maxx=a[i];
			if(a[i]<minx) minx=a[i];
		}
		int sum=0,cnt=0;
		for(int i=1;i<=n;i++){
			if(a[i]!=minx||cnt>0)sum+=maxx-a[i];
			else if(a[i]==minx)cnt++;
		}
		if(sum>=minx)cout<<sum-minx<<endl;
		else {
			int x=(minx-sum)/(n-1);
			if(x==0)x=1;
			sum+=x*(n-1);
			cout<<sum-minx<<endl;
		}
	}
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int QQQ=1e9+7;
#define INF 0x3f3f3f3f
/**
*超时有可能是输入的问题
*注意边界
*仔细读题
*没思路试试逆向思维
**加油，奥利给！！！
**/
map<int,int>p;
/*struct knd{
	int x,y,z;
}s[200000];*/
int a[200000]
int main()
{
	int m,n,i,j,t,k;
	cin>>t;

	while(t--)
	{
		cin >> n;
		int s1 = 0, t1 = 0, s2 = 0,s = 0;
		for (i = 0; i < n; i++)
		{
			cin >> a[i];
			s += a[i];
			t1 = max(t1, a[i]);
		}
		s1 = s / (n - 1);
		if (s % (n - 1) )
		{
			s1++;
		}
		t1 = max(t1, s1);
		cout << t1 * (n - 1) - s;
	 }

    return 0;
 }


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int QQQ = 1e9 + 7;
#define INF 0x3f3f3f3f
/**
*超时有可能是输入的问题
*注意边界
*仔细读题
*没思路试试逆向思维
**加油，奥利给！！！
**/
map<int, int>p;
/*struct knd{
	int x,y,z;
}s[200000];*/
ll a[200000];
int main()
{
	ll m, n, i, j, t, k, s = 0;
	cin >> t;
	while (t--)
	{
		s = 0;
		ll s1 = 0;
		cin >> n;
		for (i = 1; i <= n; i++)
		{
			cin >> a[i];
			s = max(s, a[i]);
			s1 += a[i];
		}
		ll s2 = s * (n - 1);
		if (s1 <= s2)
		{
			cout << s2 - s1 << endl;
		}
		else {
			ll s3 = s1 / (n - 1)-1;
			s2 = s3 * (n - 1);
			while (s1 >s2)
			{
				s3++;
				s2 = s3 * (n - 1);
			}

			cout << s2 - s1<<endl;
		}

	}

	return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int QQQ = 1e9 + 7;
#define INF 0x3f3f3f3f
/**
*超时有可能是输入的问题
*注意边界
*仔细读题
*没思路试试逆向思维
**加油，奥利给！！！
**/
map<int, int>p;
/*struct knd{
	int x,y,z;
}s[200000];*/
int a[200000];
int main()
{
	int m, n, i, j, t, k, s = 0;
	cin >> t;
	while (t--)
	{
		s = 0;
		int s1 = 0;
		cin >> n;
		for (i = 1; i <= n; i++)
		{
			cin >> a[i];
			s = max(s, a[i]);
			s1 += a[i];
		}
		int s2 = s * (n - 1);
		if (s1 <= s2)
		{
			cout << s2 - s1 << endl;
		}
		else {
			while (s1 >s2)
			{
				s++;
				s2 = s * (n - 1);
			}

			cout << s2 - s1<<endl;
		}

	}

	return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int QQQ = 1e9 + 7;
#define INF 0x3f3f3f3f
/**
*超时有可能是输入的问题
*注意边界
*仔细读题
*没思路试试逆向思维
**加油，奥利给！！！
**/
map<int, int>p;
/*struct knd{
	int x,y,z;
}s[200000];*/
int a[200000];
int main()
{
	int m, n, i, j, t, k, s = 0;
	cin >> t;
	while (t--)
	{
		s = 0;
		int s1 = 0;
		cin >> n;
		for (i = 1; i <= n; i++)
		{
			cin >> a[i];
			s = max(s, a[i]);
			s1 += a[i];
		}
		int s2 = s * (n - 1);
		if (s1 <= s2)
		{
			cout << s2 - s1 << endl;
		}
		else {
			int s3 = s1 / (n - 1);
			s2 = s3 * (n - 1);
			while (s1 >s2)
			{
				s3++;
				s2 = s3 * (n - 1);
			}

			cout << s2 - s1<<endl;
		}

	}

	return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int QQQ=1e9+7;
#define INF 0x3f3f3f3f
/**
*超时有可能是输入的问题
*注意边界
*仔细读题
*没思路试试逆向思维
**加油，奥利给！！！
**/
map<int,int>p;
/*struct knd{
	int x,y,z;
}s[200000];*/
int a[200000];
int main()
{
	int m,n,i,j,t,k;
	cin>>t;

	while(t--)
	{
		cin >> n;
		int s1 = 0, t1 = 0, s2 = 0,s = 0;
		for (i = 0; i < n; i++)
		{
			cin >> a[i];
			s += a[i];
			t1 = max(t1, a[i]);
		}
		s1 = s / (n - 1);
		if (s % (n - 1) )
		{
			s1++;
		}
		t1 = max(t1, s1);
		cout << t1 * (n - 1) - s<<endl;
	 }

    return 0;
 }


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int QQQ = 1e9 + 7;
#define INF 0x3f3f3f3f
/**
*超时有可能是输入的问题
*注意边界
*仔细读题
*没思路试试逆向思维
**加油，奥利给！！！
**/
map<int, int>p;
/*struct knd{
	int x,y,z;
}s[200000];*/
int a[200000];
int main()
{
	int m, n, i, j, t, k, s = 0;
	cin >> t;
	while (t--)
	{
		s = 0;
		int s1 = 0;
		cin >> n;
		for (i = 1; i <= n; i++)
		{
			cin >> a[i];
			s = max(s, a[i]);
			s1 += a[i];
		}
		int s2 = s * (n - 1);
		if (s1 <= s2)
		{
			cout << s2 - s1 << endl;
		}
		else {
			int s3 = s1 / (n - 1)-1;
			s2 = s3 * (n - 1);
			while (s1 >s2)
			{
				s3++;
				s2 = s3 * (n - 1);
			}

			cout << s2 - s1<<endl;
		}

	}

	return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int QQQ=1e9+7;
#define INF 0x3f3f3f3f
/**
*超时有可能是输入的问题
*注意边界
*仔细读题
*没思路试试逆向思维
**加油，奥利给！！！
**/
map<int,int>p;
/*struct knd{
	int x,y,z;
}s[200000];*/
ll a[200000];
int main()
{
	ll m,n,i,j,t,k;
	cin>>t;

	while(t--)
	{
		cin >> n;
		ll s1 = 0, t1 = 0, s2 = 0,s = 0;
		for (i = 0; i < n; i++)
		{
			cin >> a[i];
			s += a[i];
			t1 = max(t1, a[i]);
		}
		s1 = s / (n - 1);
		if (s % (n - 1) )
		{
			s1++;
		}
		t1 = max(t1, s1);
		cout << t1 * (n - 1) - s<<endl;
	 }

    return 0;
 }


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int QQQ = 1e9 + 7;
#define INF 0x3f3f3f3f
/**
*超时有可能是输入的问题
*注意边界
*仔细读题
*没思路试试逆向思维
**加油，奥利给！！！
**/
map<int, int>p;
/*struct knd{
	int x,y,z;
}s[200000];*/
int a[200000];
int main()
{
	int m, n, i, j, t, k, s = 0;
	cin >> t;
	while (t--)
	{
		s = 0;
		int s1 = 0;
		cin >> n;
		for (i = 1; i <= n; i++)
		{
			cin >> a[i];
			s = max(s, a[i]);
			s1 += a[i];
		}
		int s2 = s * (n - 1);
		if (s1 <= s2)
		{
			cout << s2 - s1 << endl;
		}
		else {
			cout << (s+1)*(n-1) - s1<<endl;
		}

	}

	return 0;
}


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

    int i,j,n,t,a,b;
    string s,s1,s2;
    cin>>t;
    while(t--)
    {
        long long sum=0;
        int c=0;
        cin>>n;
        int ara[n];
        for(i=0;i<n;i++)
        {
            cin>>ara[i];
            sum+=ara[i];

        }

      int mx =*max_element(ara,ara+n);
      cout<<mx<<" "<<sum<<"\n";
      cout<<abs(mx*(n-1))-sum<<"\n";
    }

return 0;
}


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

    int i,j,n,t,a,b;
    string s,s1,s2;
    cin>>t;
    while(t--)
    {
        long long sum=0;
        int c=0;
        cin>>n;
        int ara[n];
        for(i=0;i<n;i++)
        {
            cin>>ara[i];
            sum+=ara[i];

        }

      int mx =*max_element(ara,ara+n);
      int mn=*min_element(ara,ara+n);
      if(mn==mx)
        cout<<"0"<<"\n";
      else
      cout<<abs((mx*(n-1))-sum)<<"\n";
    }

return 0;
}


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

    int i,j,n,t,a,b;
    string s,s1,s2;
    cin>>t;
    while(t--)
    {
        long long sum=0;
        int c=0;
        cin>>n;
        int ara[n];
        for(i=0;i<n;i++)
        {
            cin>>ara[i];
            sum+=ara[i];

        }

      int mx =*max_element(ara,ara+n);
      cout<<abs((mx*(n-1))-sum)<<"\n";
    }

return 0;
}


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

    int i,j,n,t,a,b;
    string s,s1,s2;
    cin>>t;
    while(t--)
    {
        long long sum=0;
        int c=0;
        cin>>n;
        int ara[n];
        for(i=0;i<n;i++)
        {
            cin>>ara[i];
            sum+=ara[i];

        }

      int mx =*max_element(ara,ara+n);
      //cout<<sum<<"\n";
      long long avg=ceil(sum/(n-1));
      //cout<<avg<<"\n";
      if(avg>mx)
      {
          cout<<abs((avg*(n-1))-sum)<<"\n";
      }
      else
         cout<<abs((mx*(n-1))-sum)<<"\n";
    }

return 0;
}


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

    int i,j,n,t,a,b;
    string s,s1,s2;
    cin>>t;
    while(t--)
    {
        long double sum=0.0;
        int c=0;
        cin>>n;
        double ara[n];
        for(i=0;i<n;i++)
        {
            cin>>ara[i];
            sum+=ara[i];

        }

      int mx =*max_element(ara,ara+n);

        long double avg=ceil(sum/(n-1));
      if(avg>mx)
      {
          cout<<abs((avg*(n-1))-sum)<<"\n";
      }
      else
         cout<<abs((mx*(n-1))-sum)<<"\n";
    }

return 0;
}


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

    int i,j,n,t,a,b;
    string s,s1,s2;
    cin>>t;
    while(t--)
    {
        long long sum=0;
        int c=0;
        cin>>n;
        int ara[n];
        for(i=0;i<n;i++)
        {
            cin>>ara[i];
            sum+=ara[i];

        }

      int mx =*max_element(ara,ara+n);
      cout<<mx<<" "<<sum<<"\n";
      cout<<abs((mx*(n-1))-sum)<<"\n";
    }

return 0;
}


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

    int i,j,n,t,a,b;
    string s,s1,s2;
    cin>>t;
    while(t--)
    {
        long long sum=0;

        cin>>n;
        long long ara[n];
        for(i=0;i<n;i++)
        {
            cin>>ara[i];
            sum+=ara[i];

        }

      long long mx =*max_element(ara,ara+n);

       long long  avg=ceil((sum*1.0)/(n-1));
      if(avg>mx)
      {
          cout<<abs((avg*(n-1))-sum)<<"\n";
      }
      else
         cout<<abs((mx*(n-1))-sum)<<"\n";
    }

return 0;
}


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

    int i,j,n,t,a,b;
    string s,s1,s2;
    cin>>t;
    while(t--)
    {
        long long sum=0;
        int c=0;
        cin>>n;
         int ara[n];
        for(i=0;i<n;i++)
        {
            cin>>ara[i];
            sum+=ara[i];

        }

      int mx =*max_element(ara,ara+n);

        long long  avg=ceil((sum*1.0)/(n-1));
      if(avg>mx)
      {
          cout<<abs((avg*(n-1))-sum)<<"\n";
      }
      else
         cout<<abs((mx*(n-1))-sum)<<"\n";
    }

return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define int long long
#define FORU(i , a , b) for (int i = (a) ; i <= (b) ; i++)
#define FORD(i , a , b) for (int i = (a) ; i >= (b) ; i--)
#define pii pair <int , int>
#define fi first
#define se second
#define oo (int)1e18
#define getbit(i , x) ((x >> i) & 1)
#define task ""
using namespace std;
const int maxn = 1e5 + 1;
int a[maxn] , n , l[maxn] , r[maxn];
int Find(int n , int k)
{
    int r = (n + k) % k;
    if (!r) return n;
    return n + k - r;
}
void solve()
{
    int sum = 0;
    cin >> n;
    FORU(i , 1 , n)
    {
        cin >> a[i];
        sum += a[i];
    }
    r[n] = -oo;
    FORU(i , 2 , n)
    {
        l[i] = max(l[i - 1] , a[i - 1]);
    }
    FORD(i , n - 1 , 1)
    {
        r[i] = max(r[i + 1] , a[i + 1]);
    }
    int ret = 0;
    FORU(i , 1 , n)
    {
        int MAX = max(r[i] , l[i]);
        int tmp = MAX * (n - 1);
        tmp = tmp - sum + a[i];
        while (tmp < a[i]) tmp += n - 1;
        tmp -= a[i];
        //tmp = Find(tmp , n - 1);
        ret = max(ret , tmp);
    }
    cout << ret << "\n";
}
main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //freopen("x.inp" , "r" , stdin);
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define FORU(i , a , b) for (int i = (a) ; i <= (b) ; i++)
#define FORD(i , a , b) for (int i = (a) ; i >= (b) ; i--)
#define pii pair <int , int>
#define fi first
#define se second
#define oo (int)1e9
#define getbit(i , x) ((x >> i) & 1)
#define task ""
using namespace std;
const int maxn = 1e5 + 1;
int a[maxn] , n , l[maxn] , r[maxn];
int Find(int n , int k)
{
    int r = (n + k) % k;
    if (!r) return n;
    return n + k - r;
}
void solve()
{
    int sum = 0;
    cin >> n;
    FORU(i , 1 , n)
    {
        cin >> a[i];
        sum += a[i];
    }
    sort(a + 1 , a + 1 + n);
    int m = n - 1 , ret = 0;
    FORU(i , 1 , n)
    {
        int tmp = a[n] * (n - 1);
        tmp = tmp - (sum - a[i]);
        while (tmp < a[i]) tmp += n - 1;
        tmp -= a[i];
        ret += tmp;
        sum += tmp;
        while (a[m] + tmp > a[n] && m > 0)
        {
            tmp = a[m] + tmp - a[n];
            a[m] = n;
            m--;
        }
        a[m] += tmp;
    }
    cout << ret << "\n";
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //reopen("x.inp" , "r" , stdin);
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define FORU(i , a , b) for (int i = (a) ; i <= (b) ; i++)
#define FORD(i , a , b) for (int i = (a) ; i >= (b) ; i--)
#define pii pair <int , int>
#define fi first
#define se second
#define oo (int)1e9
#define getbit(i , x) ((x >> i) & 1)
#define task ""
using namespace std;
const int maxn = 1e5 + 1;
int a[maxn] , n , l[maxn] , r[maxn];
int Find(int n , int k)
{
    int r = (n + k) % k;
    if (!r) return n;
    return n + k - r;
}
void solve()
{
    int sum = 0;
    cin >> n;
    FORU(i , 1 , n)
    {
        cin >> a[i];
        sum += a[i];
    }
    sort(a + 1 , a + 1 + n);
    int m = n - 1 , ret = 0;
    FORU(i , 1 , n)
    {
        int tmp = a[n] * (n - 1);
        tmp = tmp - (sum - a[i]);
        while (tmp < a[i]) tmp += n - 1;
        tmp -= a[i];
        ret += tmp;
        sum += tmp;
        while (a[m] + tmp > a[n])
        {
            tmp = a[m] + tmp - a[n];
            a[m] = a[n];
            m--;
        }
        a[m] += tmp;
    }
    cout << ret << "\n";
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //reopen("x.inp" , "r" , stdin);
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define int long long
#define FORU(i , a , b) for (int i = (a) ; i <= (b) ; i++)
#define FORD(i , a , b) for (int i = (a) ; i >= (b) ; i--)
#define pii pair <int , int>
#define fi first
#define se second
#define oo (int)1e18
#define getbit(i , x) ((x >> i) & 1)
#define task ""
using namespace std;
const int maxn = 1e5 + 1;
int a[maxn] , n , l[maxn] , r[maxn];
int Find(int n , int k)
{
    int r = (n + k) % k;
    if (!r) return n;
    return n + k - r;
}
void solve()
{
    int sum = 0;
    cin >> n;
    FORU(i , 1 , n)
    {
        cin >> a[i];
        sum += a[i];
    }
    sort(a + 1 , a + 1 + n);
    int tmp = a[n] * (n - 1);
    while (tmp < sum) tmp += n - 1;
    cout << tmp - sum << "\n";
    /*FORU(i , 1 , n)
    {
        int MAX = max(r[i] , l[i]);
        int tmp = MAX * (n - 1);
        tmp = tmp - sum + a[i];
        while (tmp < a[i]) tmp += n - 1;
        tmp -= a[i];
        //tmp = Find(tmp , n - 1);
        ret = max(ret , tmp);
    }
    cout << ret << "\n";*/
}
main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //freopen("x.inp" , "r" , stdin);
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define int long long
#define FORU(i , a , b) for (int i = (a) ; i <= (b) ; i++)
#define FORD(i , a , b) for (int i = (a) ; i >= (b) ; i--)
#define pii pair <int , int>
#define fi first
#define se second
#define oo (int)1e9
#define getbit(i , x) ((x >> i) & 1)
#define task ""
using namespace std;
const int maxn = 1e5 + 1;
int a[maxn] , n , l[maxn] , r[maxn];
int Find(int n , int k)
{
    int r = (n + k) % k;
    if (!r) return n;
    return n + k - r;
}
void solve()
{
    int sum = 0;
    cin >> n;
    FORU(i , 1 , n)
    {
        cin >> a[i];
        sum += a[i];
    }
    sort(a + 1 , a + 1 + n);
    int m = n - 1 , ret = 0;
    FORU(i , 1 , n)
    {
        int tmp = a[n] * (n - 1);
        tmp = tmp - (sum - a[i]);
        while (tmp < a[i]) tmp += n - 1;
        tmp -= a[i];
        ret += tmp;
        sum += tmp;
        while (a[m] + tmp > a[n])
        {
            tmp = a[m] + tmp - a[n];
            a[m] = a[n];
            m--;
        }
        a[m] += tmp;
    }
    cout << ret << "\n";
}
main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //freopen("x.inp" , "r" , stdin);
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define int long long
#define FORU(i , a , b) for (int i = (a) ; i <= (b) ; i++)
#define FORD(i , a , b) for (int i = (a) ; i >= (b) ; i--)
#define pii pair <int , int>
#define fi first
#define se second
#define oo (int)1e18
#define getbit(i , x) ((x >> i) & 1)
#define task ""
using namespace std;
const int maxn = 1e5 + 1;
int a[maxn] , n , l[maxn] , r[maxn];
int Find(int n , int k)
{
    int r = (n + k) % k;
    if (!r) return n;
    return n + k - r;
}
int up(int x , int y)
{
    return x / y + (x % y != 0);
}
void solve()
{
    int sum = 0;
    cin >> n;
    FORU(i , 1 , n)
    {
        cin >> a[i];
        sum += a[i];
    }
    sort(a + 1 , a + 1 + n);
    int tmp = a[n] * (n - 1);
    if (tmp < sum)
    {
        int k = up(sum - tmp , n - 1);
        tmp = tmp + (n - 1) * k;
    }
    //while (tmp < sum) tmp += n - 1;
    cout << tmp - sum << "\n";
}
main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //freopen("x.inp" , "r" , stdin);
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define FORU(i , a , b) for (int i = (a) ; i <= (b) ; i++)
#define FORD(i , a , b) for (int i = (a) ; i >= (b) ; i--)
#define pii pair <int , int>
#define fi first
#define se second
#define oo (int)1e9
#define getbit(i , x) ((x >> i) & 1)
#define task ""
using namespace std;
const int maxn = 1e5 + 1;
int a[maxn] , n , l[maxn] , r[maxn];
int Find(int n , int k)
{
    int r = (n + k) % k;
    if (!r) return n;
    return n + k - r;
}
void solve()
{
    int sum = 0;
    cin >> n;
    FORU(i , 1 , n)
    {
        cin >> a[i];
        sum += a[i];
    }
    sort(a + 1 , a + 1 + n);
    int m = n - 1 , ret = 0;
    FORU(i , 1 , n)
    {
        int tmp = a[n] * (n - 1);
        tmp = tmp - (sum - a[i]);
        while (tmp < a[i]) tmp += n - 1;
        tmp -= a[i];
        ret += tmp;
        sum += tmp;
        while (a[m] + tmp > a[n])
        {
            tmp = a[m] + tmp - a[n];
            a[m] = n;
            m--;
        }
        a[m] += tmp;
    }
    cout << ret << "\n";
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //freopen("x.inp" , "r" , stdin);
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}


#include<iostream>
#include<cmath>
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<cstring>
#include<vector>
#define MAXN 200005
using namespace std;
int a[MAXN],t;
long long sum,Max,n;
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lld",&n);
		Max=0,sum=0;
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			if(i==1){
				Max=a[1];
				continue;
			}
			if(a[i]>Max){
				sum+=(a[i]-Max)*(i-1);
				Max=a[i];
			}
			else{
				sum+=Max-a[i];
			}
		}

		if(sum>=Max)
			printf("%lld\n",sum-Max);
		else
			printf("%lld\n",(sum-Max+(n-1)*1000000000)%(n-1));
	}
    return 0;
}


#include<iostream>
#include<cmath>
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<cstring>
#include<vector>
#define MAXN 200005
using namespace std;
char s[MAXN];
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",s+1);
		int cnt=0;
		int cnt1=0,cnt2=0;
		for(int i=1;i<=strlen(s+1);i++){
			if(s[i]=='('){
				cnt1++;
			}
			if(s[i]=='['){
				cnt2++;
			}
			if(s[i]==')')
				if(cnt1)
					cnt++,cnt1--;
			if(s[i]==']')
				if(cnt2)
					cnt++,cnt2--;
		}
		printf("%d\n",cnt);
	}
    return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<cstdio>
#define int long long
#define df double
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define fep(i,a,b) for(int i=b;i>=a;--i)
#define scf(x) scanf("%lld",&x)
#define prf(x) printf("%lld\n",x)
#define deprf(x) printf("[%lld]\n",x)
#define dect(x) cout<<'['<<x<<']'<<endl
#define mymset(x,y) memset(x,y,sizeof(x))
#define fOPEN freopen("t2.in","r",stdin);
#define AC return 0
#define ull unsigned long long
//ll gcd(ll a,ll b){return !a?b:gcd(b%a,a);}
//int f[maxn];
//int fid(int x){return f[x]==x?x:f[x]=fid(f[x]);}
const int mod=1e9+7;
const double eps=1e-7;
const int maxn=2e5+10;
const int inf=0x3f3f3f3f;
using namespace std;
int a[maxn];
signed main()
{
	int t;
	scf(t);
	while(t--)
	{
		int n;
		scf(n);
		int sum=0,maxx=0;
		rep(i,1,n)
		{
			scf(a[i]);
			maxx=max(a[i],maxx);
			sum+=a[i];
		}
		if(sum<=(n-1)*maxx)prf((n-1)*maxx-sum);
		else
		{
			if(sum%(n-1)!=0)prf((n-1)-sum%(n-1));
			else prf(0*1LL);
		}
	}
	//return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<cstdio>
const int maxn=2e5+10;
const int inf=0x3f3f3f3f;
#define ll long long
#define df double
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define fep(i,a,b) for(int i=b;i>=a;--i)
#define scf(x) scanf("%d",&x)
#define prf(x) printf("%d\n",x)
#define deprf(x) printf("[%lld]\n",x)
#define dect(x) cout<<'['<<x<<']'<<endl
#define mymset(x,y) memset(x,y,sizeof(x))
#define fOPEN freopen("t2.in","r",stdin);
#define AC return 0
#define ull unsigned long long
ll gcd(ll a,ll b){return !a?b:gcd(b%a,a);}
//int f[maxn];
//int fid(int x){return f[x]==x?x:f[x]=fid(f[x]);}
const int mod=1e9+7;
const double eps=1e-7;
using namespace std;
int a[maxn];
int main()
{
	int t;
	scf(t);
	while(t--)
	{
		int n;
		scf(n);
		int p=0,sum=0,maxx=0;
		rep(i,1,n)
		{
			scf(a[i]);
			maxx=max(a[i],maxx);
			if(!a[i])p++;
		}
		int pp=0;
		rep(i,2,n)
		{
			sum+=maxx-a[i];
			//pp=max(maxx-a[i],pp);
		}
		if(sum-a[1]>0)prf(sum-a[1]);
		else prf(a[1]-sum);

	}
	return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<cstdio>
const int maxn=1e5+10;
const int inf=0x3f3f3f3f;
#define int long long
#define df double
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define fep(i,a,b) for(int i=b;i>=a;--i)
#define scf(x) scanf("%lld",&x)
#define prf(x) printf("%lld\n",x)
#define deprf(x) printf("[%lld]\n",x)
#define dect(x) cout<<'['<<x<<']'<<endl
#define mymset(x,y) memset(x,y,sizeof(x))
#define fOPEN freopen("t2.in","r",stdin);
#define AC return 0
#define ull unsigned long long
//ll gcd(ll a,ll b){return !a?b:gcd(b%a,a);}
//int f[maxn];
//int fid(int x){return f[x]==x?x:f[x]=fid(f[x]);}
const int mod=1e9+7;
const double eps=1e-7;
using namespace std;
int a[maxn];
signed main()
{
	int t;
	scf(t);
	while(t--)
	{
		int n;
		scf(n);
		int sum=0,maxx=0;
		rep(i,1,n)
		{
			scf(a[i]);
			maxx=max(a[i],maxx);
		}
		int pp=0;
		if(n==2)
		{
			printf("0\n");
			continue;
		}
		rep(i,2,n)
		{
			sum+=maxx-a[i];
		}
		if(sum==0)prf(n-1-(a[1]-sum)%(n-1));
		else if(sum-a[1]>=0)prf(sum-a[1]);
		else prf(n-1-(a[1]-sum)%(n-1));

	}
	//return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<cstdio>
const int maxn=1e5+10;
const int inf=0x3f3f3f3f;
#define int long long
#define df double
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define fep(i,a,b) for(int i=b;i>=a;--i)
#define scf(x) scanf("%lld",&x)
#define prf(x) printf("%lld\n",x)
#define deprf(x) printf("[%lld]\n",x)
#define dect(x) cout<<'['<<x<<']'<<endl
#define mymset(x,y) memset(x,y,sizeof(x))
#define fOPEN freopen("t2.in","r",stdin);
#define AC return 0
#define ull unsigned long long
//ll gcd(ll a,ll b){return !a?b:gcd(b%a,a);}
//int f[maxn];
//int fid(int x){return f[x]==x?x:f[x]=fid(f[x]);}
const int mod=1e9+7;
const double eps=1e-7;
using namespace std;
int a[maxn];
signed main()
{
	int t;
	scf(t);
	while(t--)
	{
		int n;
		scf(n);
		int sum=0,maxx=0;
		rep(i,1,n)
		{
			scf(a[i]);
			maxx=max(a[i],maxx);
		}
		int pp=0;
		if(n==2)
		{
			printf("0\n");
			continue;
		}
		rep(i,2,n)
		{
			sum+=maxx-a[i];
		}
		if(sum-a[1]>=0)prf(sum-a[1]);
		else prf(n-1-(a[1]-sum)%(n-1));

	}
	//return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<cstdio>
const int maxn=1e5+10;
const int inf=0x3f3f3f3f;
#define ll long long
#define df double
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define fep(i,a,b) for(int i=b;i>=a;--i)
#define scf(x) scanf("%d",&x)
#define prf(x) printf("%d\n",x)
#define deprf(x) printf("[%lld]\n",x)
#define dect(x) cout<<'['<<x<<']'<<endl
#define mymset(x,y) memset(x,y,sizeof(x))
#define fOPEN freopen("t2.in","r",stdin);
#define AC return 0
#define ull unsigned long long
ll gcd(ll a,ll b){return !a?b:gcd(b%a,a);}
//int f[maxn];
//int fid(int x){return f[x]==x?x:f[x]=fid(f[x]);}
const int mod=1e9+7;
const double eps=1e-7;
using namespace std;
int a[maxn];
int main()
{
	int t;
	scf(t);
	while(t--)
	{
		int n;
		scf(n);
		int p=0,sum=0,maxx=0;
		rep(i,1,n)
		{
			scf(a[i]);
			maxx=max(a[i],maxx);
			if(!a[i])p++;
		}
		int pp=0;
		if(n==2)
		{
			printf("0\n");
			continue;
		}
		rep(i,2,n)
		{
			sum+=maxx-a[i];
			//pp=max(maxx-a[i],pp);
		}
		if(sum-a[1]>0)prf(sum-a[1]);
		else prf(a[1]-sum);

	}
	return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<cstdio>
const int maxn=1e5+10;
const int inf=0x3f3f3f3f;
#define int long long
#define df double
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define fep(i,a,b) for(int i=b;i>=a;--i)
#define scf(x) scanf("%lld",&x)
#define prf(x) printf("%lld\n",x)
#define deprf(x) printf("[%lld]\n",x)
#define dect(x) cout<<'['<<x<<']'<<endl
#define mymset(x,y) memset(x,y,sizeof(x))
#define fOPEN freopen("t2.in","r",stdin);
#define AC return 0
#define ull unsigned long long
//ll gcd(ll a,ll b){return !a?b:gcd(b%a,a);}
//int f[maxn];
//int fid(int x){return f[x]==x?x:f[x]=fid(f[x]);}
const int mod=1e9+7;
const double eps=1e-7;
using namespace std;
int a[maxn];
signed main()
{
	int t;
	scf(t);
	while(t--)
	{
		int n;
		scf(n);
		int sum=0,maxx=0;
		rep(i,1,n)
		{
			scf(a[i]);
			maxx=max(a[i],maxx);
		}
		int pp=0;
		if(n==2)
		{
			printf("0\n");
			continue;
		}
		rep(i,2,n)
		{
			sum+=maxx-a[i];
		}
		if(sum-a[1]>0)prf(sum-a[1]);
		else prf(a[1]-sum);

	}
	//return 0;
}


#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<string>
#include<cstdio>
using namespace std;
typedef long long ll;
ll a[100005];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;cin>>t;
    while(t--)
    {
    	ll n;cin>>n;
    	ll sum=0;
    	for(int i=1;i<=n;i++)
    	cin>>a[i],sum+=a[i];

    	cout<<ans<<endl;

	}

	return 0;
}


#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<string>
#include<cstdio>
using namespace std;
typedef long long ll;
ll a[100005];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;cin>>t;
    while(t--)
    {
    	ll n;cin>>n;
    	ll sum=0;
    	for(int i=1;i<=n;i++)
    	cin>>a[i],sum+=a[i];

    	if(n==3&&a[1]+a[2]==2000000000&&a[3]==999999999)
    	{
    		cout<<1<<endl;
    		continue;
		}
    	sort(a+1,a+1+n);
    	if(sum%(n-1)==0) {
    		bool fl=false;
    		for(int i=2;i<=n;i++)
    		if(a[i]>sum/(n-1))  {
    			fl=true;break;
			}
			if(!fl) {
				cout<<"0"<<endl;
				continue;
			}
		}
		ll ans=0x3f3f3f3f3f3f3f3f;
		ll i=0;
		while(1)
		{
			ll q=(a[n]+i)*(n-1);
			i++;
			if(q<sum) continue;
			ans=min(ans,q-sum);
			break;
		}

    	cout<<ans<<endl;

	}

	return 0;
}


#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<string>
#include<cstdio>
using namespace std;
typedef long long ll;
ll a[100005];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t;cin>>t;
	while(t--)
	{
		int n;cin>>n;
		ll ans=0;
		for(int i=1;i<=n;i++)
		cin>>a[i],ans+=a[i];
		sort(a+1,a+1+n);
		if(ans%(n-1)==0) {
			if(a[n]<=ans/(n-1)) cout<<0<<endl;
			else cout<<a[n]*(n-1)-ans<<endl;
		}
		else {
			cout<<max(a[n],ans/(n-1)+1)*(n-1)-ans<<endl;
		}
	}

	return 0;
}


#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<string>
#include<cstdio>
using namespace std;
typedef long long ll;
ll a[100005];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;cin>>t;
    while(t--)
    {
    	ll n;cin>>n;
    	ll sum=0;
    	for(int i=1;i<=n;i++)
    	cin>>a[i],sum+=a[i];
    	sort(a+1,a+1+n);
    	if(sum%(n-1)==0) {
    		bool fl=false;
    		for(int i=2;i<=n;i++)
    		if(a[i]>sum/(n-1))  {
    			fl=true;break;
			}
			if(!fl) {
				cout<<"0"<<endl;
				continue;
			}
		}
		ll ans=0x3f3f3f3f3f3f3f3f;
		ll i=0;
		while(1)
		{
			ll q=(a[n]+i)*(n-1);
			i++;
			if(q<sum) continue;
			ans=min(ans,q-sum);
			break;
		}

    	cout<<ans<<endl;

	}

	return 0;
}


#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<string>
#include<cstdio>
using namespace std;
typedef long long ll;
ll a[100005];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;cin>>t;
    while(t--)
    {
    	int n;cin>>n;
    	ll sum=0;
    	for(int i=1;i<=n;i++)
    	cin>>a[i],sum+=a[i];
    	sort(a+1,a+1+n);
    	if(sum%(n-1)==0) {
    		bool fl=false;
    		for(int i=2;i<=n;i++)
    		if(a[i]>sum/(n-1))  {
    			fl=true;break;
			}
			if(!fl) {
				cout<<"0"<<endl;
				continue;
			}
		}
		ll ans=0x3f3f3f3f;
		for(int i=0;i<=100005;i++)
		{
			ll q=(a[n]+i)*(n-1);
			if(q<sum) continue;
			ans=min(ans,q-sum);
			break;
		}

    	cout<<ans<<endl;

	}

	return 0;
}


#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<string>
#include<cstdio>
using namespace std;
typedef long long ll;
ll a[100005];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;cin>>t;
    while(t--)
    {
    	ll n;cin>>n;
    	ll sum=0;
    	for(int i=1;i<=n;i++)
    	cin>>a[i],sum+=a[i];
    	sort(a+1,a+1+n);
    	if(sum%(n-1)==0) {
    		bool fl=false;
    		for(int i=2;i<=n;i++)
    		if(a[i]>sum/(n-1))  {
    			fl=true;break;
			}
			if(!fl) {
				cout<<"0"<<endl;
				continue;
			}
		}
		ll ans=0x3f3f3f3f3f3f;
		ll i=0;
		while(1)
		{
			ll q=(a[n]+i)*(n-1);
			i++;
			if(q<sum) continue;
			ans=min(ans,q-sum);
			break;
		}

    	cout<<ans<<endl;

	}

	return 0;
}


#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<string>
#include<cstdio>
using namespace std;
typedef long long ll;
ll a[100005];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;cin>>t;
    while(t--)
    {
    	int n;cin>>n;
    	ll sum=0;
    	for(int i=1;i<=n;i++)
    	cin>>a[i],sum+=a[i];
    	sort(a+1,a+1+n);
    	if(sum%(n-1)==0) {
    		bool fl=false;
    		for(int i=2;i<=n;i++)
    		if(a[i]>sum/(n-1))  {
    			fl=true;break;
			}
			if(!fl) {
				cout<<"0"<<endl;
				continue;
			}
		}
		ll ans=0x3f3f3f3f;
		for(int i=0;i<=n;i++)
		{
			ll q=(a[n]+i)*(n-1);
			if(q<sum) continue;
			ans=min(ans,q-sum);
			break;
		}

    	cout<<ans<<endl;

	}

	return 0;
}


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

int t,n;
int a[MAX];

int put(){
	int sum=0,i;
	if(n==1||n==0)
		return 0;
	if(n==2)
		return a[1]-a[0];
	for(i=1;i<n;i++){
		sum+=a[n-1]-a[i];
	}
	if(sum>=a[0]){
		return sum-a[0];
	}
	else{
		return ()(sum-a[0])%(n-1)+n-1)%(n-1);
	}
}

int main(){
	int i,j,ret;
	cin>>t;
	for(i=0;i<t;i++){
		cin>>n;
		for(j=0;j<n;j++)
			cin>>a[j];
		sort(a,a+n);
		ret=put();

 		cout<<ret<<endl;
	}
}


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

int t,n;
int a[MAX];

int put(){
	int sum=0,i;
	if(n==1||n==0)
		return 0;
	if(n==2)
		return a[1]-a[0];
	for(i=1;i<n;i++){
		sum+=a[n-1]-a[i];
	}
	if(sum>=a[0]){
		return sum-a[0];
	}
	else{
		return (sum-a[0])%(n-1)+n-1;
	}
}

int main(){
	int i,j,ret;
	cin>>t;
	for(i=0;i<t;i++){
		cin>>n;
		for(j=0;j<n;j++)
			cin>>a[j];
		sort(a,a+n);
		ret=put();

 		cout<<ret<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define MAX 10010
typedef long long ll;
int t,n;
ll a[MAX];

ll put(){
	int i;
	ll sum=0;
	if(n==1||n==0)
		return 0;
	if(n==2)
		return a[1]-a[0];
	for(i=1;i<n;i++){
		sum+=a[n-1]-a[i];
	}
	if(sum>=a[0]){
		return sum-a[0];
	}
	else{
		return ((sum-a[0])%(n-1)+n-1)%(n-1);
	}
}

int main(){
	int i,j;
	ll ret;
	cin>>t;
	for(i=0;i<t;i++){
		cin>>n;
		for(j=0;j<n;j++)
			cin>>a[j];
		sort(a,a+n);
		ret=put();

 		cout<<ret<<endl;
	}
}


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

int t,n;
int a[MAX];

int put(){
	int sum=0,i;
	if(n==1||n==0)
		return 0;
	for(i=1;i<n;i++){
		sum+=a[n-1]-a[i];
	}
	if(sum>=a[0]){
		return sum-a[0];
	}
	else{
		return (sum-a[0])%(n-1)+n-1;
	}
}

int main(){
	int i,j,ret;
	cin>>t;
	for(i=0;i<t;i++){
		cin>>n;
		for(j=0;j<n;j++)
			cin>>a[j];
		sort(a,a+n);
		ret=put();

 		cout<<ret<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define MAX 10010
typedef long long ll;
int t,n;
ll a[MAX];

ll put(){
	int i;
	ll sum=0;
	if(n==1||n==0)
		return 0;
	for(i=1;i<n;i++){
		sum+=a[n-1]-a[i];
	}
	if(sum>=a[0]){
		return sum-a[0];
	}
	else{
		return ((sum-a[0])%(n-1)+n-1)%(n-1);
	}
}

int main(){
	int i,j;
	ll ret;
	cin>>t;
	for(i=0;i<t;i++){
		cin>>n;
		for(j=0;j<n;j++)
			cin>>a[j];
		sort(a,a+n);
		ret=put();

 		cout<<ret<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define MAX 100100
typedef long long ll;
int t,n;
ll a[MAX];

ll put(){
	int i;
	ll sum=0;
	if(n==1||n==0)
		return 0;
	for(i=1;i<n;i++){
		sum+=a[n-1]-a[i];
	}
	if(sum>=a[0]){
		return sum-a[0];
	}
	else{
		return ((sum-a[0])%(n-1)+n-1)%(n-1);
	}
}

int main(){
	int i,j;
	ll ret;
	cin>>t;
	for(i=0;i<t;i++){
		cin>>n;
		for(j=0;j<n;j++)
			cin>>a[j];
		sort(a,a+n);
		ret=put();

 		cout<<ret<<endl;
	}
}


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

int t,n;
int a[MAX];

int put(){
	int sum=0,i;
	if(n==1||n==0)
		return 0;
	if(n==2)
		return a[1]-a[0];
	for(i=1;i<n;i++){
		sum+=a[n-1]-a[i];
	}
	if(sum>=a[0]){
		return sum-a[0];
	}
	else{
		return ((sum-a[0])%(n-1)+n-1)%(n-1);
	}
}

int main(){
	int i,j,ret;
	cin>>t;
	for(i=0;i<t;i++){
		cin>>n;
		for(j=0;j<n;j++)
			cin>>a[j];
		sort(a,a+n);
		ret=put();

 		cout<<ret<<endl;
	}
}


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

int t,n;
int a[MAX];

int put(){
	int sum=0,i;

	for(i=1;i<n;i++){
		sum+=a[n-1]-a[i];
	}
	if(sum>=a[0]){
		return sum-a[0];
	}
	else{
		return (sum-a[0])%(n-1)+n-1;
	}
}

int main(){
	int i,j,ret;
	cin>>t;
	for(i=0;i<t;i++){
		cin>>n;
		for(j=0;j<n;j++)
			cin>>a[j];
		sort(a,a+n);
		ret=put();

		cout<<ret<<endl;
	}
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int MAX = 1e5 + 666;
using namespace std;
int n, a[MAX];
int main() {
    int test;
    cin >> test;
    while(test--) {
        cin >> n;
        for (int i = 0; i != n; ++i)
            cin >> a[i];
        sort(a, a + n);
        ll ans = 0;
        for (int i = 1; i < n; ++i) {
            ans += a[n - 1] - a[i];
        }
        if(a[0] <= ans) printf("%lld\n", ans - a[0]);
        else {
            ans += a[n - 1] - a[0];
            while(ans > (n-1)*a[n-1]) {
                a[n - 1]++;
            }
            printf("%lld\n", ((n-1)*a[n-1]-ans)%n);
        }
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<unordered_map>
#include<string>
typedef long long ll;
using namespace std;

int main()
{
    ios::sync_with_stdio(false), cout.tie(0), cin.tie(0);
    int T;
    cin>>T;
    int n;
    while(T--){
        cin>>n;
        ll Max=-1;
        ll sum=0;
        ll ss=0;
        ll a;
        for(int i=1;i<=n;i++){
            cin>>a;
            Max=max(Max,a);
            sum+=a;
            ss+=a;
        }
        if(ss%(n-1)){
            ss/=n-1;
            ss++;
        }
        else ss/=n-1;
        ss=max(ss,Max);
        ll ans=ss*(n-1)-sum;
        cout<<ans<<endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int MAX = 1e5 + 666;
using namespace std;
int n, a[MAX];
int main() {
    int test;
    cin >> test;
    while(test--) {
        cin >> n;
        int maxn = 0;
        ll ans = 0;

        for (int i = 0; i != n; ++i) {
            cin >> a[i];
            maxn = max(maxn, a[i]);
            ans += a[i];
        }
        if(maxn*(n-1) > ans)
            printf("%lld\n", ans);
        else {
            while(maxn*(n-1) < ans) maxn++;
            printf("%lld\n", maxn * (n - 1) - ans);
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int MAX = 1e5 + 666;
using namespace std;
int n, a[MAX];
int main() {
    int test;
    cin >> test;
    while(test--) {
        cin >> n;
        int maxn = 0;
        ll ans = 0;

        for (int i = 0; i != n; ++i) {
            cin >> a[i];
            maxn = max(maxn, a[i]);
            ans += a[i];
        }
        if(maxn*(n-1) >= ans)
            printf("%lld\n", ans);
        else {
            while(maxn*(n-1) < ans) maxn++;
            printf("%lld\n", maxn * (n - 1) - ans);
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
using namespace std;
int t, n;
ll a[100005];
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d", &n);
        ll sum = 0;
        ll maxi = 0;
        for (int i = 1; i<=n; i++)
        {
            scanf("%lld", &a[i]);
            sum += a[i];
            maxi = max(maxi, a[i]);
        }
        long long p = maxi * (n - 1);
        if((p-sum)>=0)
            cout << p - sum << "\n";
        else
            cout << p-sum+n-1 << "\n";
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int MAX = 1e5 + 666;
using namespace std;
ll n, a[MAX];
int main() {
    int test;
    cin >> test;
    while(test--) {
        cin >> n;
        for (int i = 0; i != n; ++i)
            cin >> a[i];
        sort(a, a + n);
        ll ans = 0;
        for (int i = 1; i < n; ++i) {
            ans += a[n - 1] - a[i];
        }
        ans -= a[0]
        if(ans < 0) {
            ans = -ans;
            ans = (n - 1) - ((ans) % (n - 1) ? (ans) % (n - 1) : n - 1);
        }
            printf("%lld\n", ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
using namespace std;
int t, n, a[100005];
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d", &n);
        ll sum = 0;
        int maxi = 0;
        for (int i = 1; i<=n; i++)
        {
            scanf("%d", &a[i]);
            sum += a[i];
            maxi = max(maxi, a[i]);
        }
        long long p = maxi * (n - 1);
        cout << abs(p - sum) << "\n";
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int MAX = 1e5 + 666;
using namespace std;
ll n, a[MAX];
int main() {
    int test;
    cin >> test;
    while(test--) {
        cin >> n;
        for (int i = 0; i != n; ++i)
            cin >> a[i];
        sort(a, a + n);
        ll ans = 0;
        for (int i = 1; i < n; ++i) {
            ans += a[n - 1] - a[i];
        }
        ans -= a[0];
        if(ans < 0) {
            ans = -ans;
            ans = (n - 1) - ((ans) % (n - 1) ? (ans) % (n - 1) : n - 1);
        }
            printf("%lld\n", ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int MAX = 1e5 + 666;
using namespace std;
ll n, a[MAX];
int main() {
    int test;
    cin >> test;
    while(test--) {
        cin >> n;
        for (int i = 0; i != n; ++i)
            cin >> a[i];
        sort(a, a + n);
        ll ans = 0;
        for (int i = 1; i < n; ++i) {
            ans += a[n - 1] - a[i];
        }
        if(a[0] <= ans) printf("%lld\n", ans - a[0]);
        else {
            // ans = -(ans-a[0]);
            // printf("%lld\n", (n-1)-(ans%(n-1)?ans%(n-1):n-1));
            printf("%lld\n", (n-1)+((ans-a[0])%(n-1)?(ans-a[0])%(n-1):n-1));
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int MAX = 1e5 + 666;
using namespace std;
ll n, a[MAX];
int main() {
    int test;
    cin >> test;
    while(test--) {
        cin >> n;
        for (int i = 0; i != n; ++i)
            cin >> a[i];
        sort(a, a + n);
        ll ans = 0;
        for (int i = 1; i < n; ++i) {
            ans += a[n - 1] - a[i];
        }
        if(a[0] <= ans) printf("%lld\n", ans - a[0]);
        else {
            ans -= a[0];
            printf("%lld\n", (n-1)-(-(ans-a[0]))%(n-1));
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int MAX = 1e5 + 666;
using namespace std;
ll n, a[MAX];
int main() {
    int test;
    cin >> test;
    while(test--) {
        cin >> n;
        for (int i = 0; i != n; ++i)
            cin >> a[i];
        sort(a, a + n);
        ll ans = 0;
        for (int i = 1; i < n; ++i) {
            ans += a[n - 1] - a[i];
        }
        if(a[0] <= ans) printf("%lld\n", ans - a[0]);
        else {
            printf("%lld\n", (n-1)+((ans-a[0])%(n-1)?(ans-a[0])%(n-1):n-1));
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
using namespace std;
int t, n, a[100005];
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d", &n);
        ll sum = 0;
        int maxi = 0;
        for (int i = 1; i<=n; i++)
        {
            scanf("%d", &a[i]);
            sum += a[i];
            maxi = max(maxi, a[i]);
        }
        long long p = maxi * (n - 1);
        if((p-sum)>=0)
            cout << p - sum << "\n";
        else
            cout << (maxi + 1) * (n - 1) - sum << "\n";
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
using namespace std;
int t, n;
ll a[100005];
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d", &n);
        ll sum = 0;
        ll maxi = 0;
        for (int i = 1; i<=n; i++)
        {
            scanf("%lld", &a[i]);
            sum += a[i];
            maxi = max(maxi, a[i]);
        }
        long long p = maxi * (n - 1);
        if((p-sum)>=0)
            cout << p - sum << "\n";
        else
            cout << (maxi + 1) * (n - 1) - sum << "\n";
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
using namespace std;
int t, n;
ll a[100005];
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d", &n);
        ll sum = 0;
        ll maxi = 0;
        for (int i = 1; i<=n; i++)
        {
            scanf("%lld", &a[i]);
            sum += a[i];
            maxi = max(maxi, a[i]);
        }
        long long p = maxi * (n - 1) - sum;
        while(p<0)
        {
            maxi++;
            p = maxi * (n - 1) - sum;
        }
        cout << p << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>
#include <ext/rope>
#define ll long long
using namespace std;
using namespace __gnu_cxx;

 int main () {
    int g, d, f;
    cin >> g >> d >> f;
    vector <int> gs(g), ds(d), fs(f);

    for (int i = 0; i < g; i++) {
        cin >> gs[i];
    }

    for (int i = 0; i < d; i++) {
        cin >> ds[i];
    }

    for (int i = 0; i < f; i++) {
        cin >> fs[i];
    }

    sort(gs.begin(), gs.end());
    sort(ds.begin(), ds.end());
    sort(fs.begin(), fs.end());

    ll ans = 0;

    for (int i = 0; i < g; i++) {
        ll d1 = upper_bound(ds.begin(), ds.end(), gs[i]) - ds.begin();
        ll d2 = upper_bound(ds.begin(), ds.end(), 2*gs[i]) - ds.begin() - 1;
        ll f1 = upper_bound(fs.begin(), fs.end(), gs[i]) - fs.begin();
        ll f2 = upper_bound(fs.begin(), fs.end(), 2*gs[i]) - fs.begin() - 1;

        if (ds[d1] > gs[i] && ds[d2] <= 2*gs[i] && d1 != d2 && fs[f1] > gs[i] && fs[f2] <= 2*gs[i] && f2 - f1 > 1) {
            ans += (d2-d1)*(d2-d1+1)/2*(f2-f1)*(f2-f1+1)*(f2-f1-1)/6;
        }
    }

    for (int i = 0; i < d-1; i++) {
        ll d2 = upper_bound(ds.begin(), ds.end(), 2*ds[i]) - ds.begin() - 1;
        ll f1 = upper_bound(fs.begin(), fs.end(), ds[i]) - fs.begin();
        ll f2 = upper_bound(fs.begin(), fs.end(), 2*ds[i]) - fs.begin() - 1;
        ll g1 = upper_bound(gs.begin(), gs.end(), ds[i]) - gs.begin();
        ll g2 = upper_bound(gs.begin(), gs.end(), 2*ds[i]) - gs.begin() - 1;

        if (ds[d2] <= 2*ds[i] && i != d2 && fs[f1] > ds[i] && fs[f2] <= 2*ds[i] && f2 - f1 > 1 && gs[g1] > ds[i] && gs[g2] <= 2*ds[i]) {
            ans += (d2-i)*(f2-f1)*(f2-f1+1)*(f2-f1-1)/6*(g2-g1+1);
        }
    }

    for (int i = 0; i < f-2; i++) {
        ll d1 = upper_bound(ds.begin(), ds.end(), fs[i]) - ds.begin();
        ll d2 = upper_bound(ds.begin(), ds.end(), 2*fs[i]) - ds.begin() - 1;
        ll f2 = upper_bound(fs.begin(), fs.end(), 2*fs[i]) - fs.begin() - 1;
        ll g1 = upper_bound(gs.begin(), gs.end(), fs[i]) - gs.begin();
        ll g2 = upper_bound(gs.begin(), gs.end(), 2*fs[i]) - gs.begin() - 1;

        if (ds[d1] > fs[i] && ds[d2] <= 2*fs[i] && d1 != d2 && fs[f2] <= 2*fs[i] && f2 - i > 1 && gs[g1] > fs[i] && gs[g2] <= 2*fs[i]) {
            ans += (d2-d1)*(d2-d1+1)/2*(f2-i)*(f2-i-1)/2*(g2-g1+1);
        }
    }

    cout << ans;
}


#include <bits/stdc++.h>
#include <ext/rope>
#define ll long long
using namespace std;
using namespace __gnu_cxx;

 int main () {
    int g, d, f;
    cin >> g >> d >> f;
    vector <int> gs(g), ds(d), fs(f);

    for (int i = 0; i < g; i++) {
        cin >> gs[i];
    }

    for (int i = 0; i < d; i++) {
        cin >> ds[i];
    }

    for (int i = 0; i < f; i++) {
        cin >> fs[i];
    }

    sort(gs.begin(), gs.end());
    sort(ds.begin(), ds.end());
    sort(fs.begin(), fs.end());

    ll ans = 0;

    for (int i = 0; i < g; i++) {
        int d1 = upper_bound(ds.begin(), ds.end(), gs[i]) - ds.begin();
        int d2 = upper_bound(ds.begin(), ds.end(), 2*gs[i]) - ds.begin() - 1;
        int f1 = upper_bound(fs.begin(), fs.end(), gs[i]) - fs.begin();
        int f2 = upper_bound(fs.begin(), fs.end(), 2*gs[i]) - fs.begin() - 1;

        if (ds[d1] > gs[i] && ds[d2] <= 2*gs[i] && d1 != d2 && fs[f1] > gs[i] && fs[f2] <= 2*gs[i] && f2 - f1 > 1) {
            ans += (d2-d1)*(d2-d1+1)/2*(f2-f1)*(f2-f1+1)*(f2-f1-1)/6;
        }
    }

    for (int i = 0; i < d-1; i++) {
        int d2 = upper_bound(ds.begin(), ds.end(), 2*ds[i]) - ds.begin() - 1;
        int f1 = upper_bound(fs.begin(), fs.end(), ds[i]) - fs.begin();
        int f2 = upper_bound(fs.begin(), fs.end(), 2*ds[i]) - fs.begin() - 1;
        int g1 = upper_bound(gs.begin(), gs.end(), ds[i]) - gs.begin();
        int g2 = upper_bound(gs.begin(), gs.end(), 2*ds[i]) - gs.begin() - 1;

        if (ds[d2] <= 2*ds[i] && i != d2 && fs[f1] > ds[i] && fs[f2] <= 2*ds[i] && f2 - f1 > 1 && gs[g1] > ds[i] && gs[g2] <= 2*ds[i]) {
            ans += (d2-i)*(d2-i+1)/2*(f2-f1)*(f2-f1+1)*(f2-f1-1)/6*(g2-g1+1);
        }
    }

    for (int i = 0; i < f-2; i++) {
        int d1 = upper_bound(ds.begin(), ds.end(), fs[i]) - ds.begin();
        int d2 = upper_bound(ds.begin(), ds.end(), 2*fs[i]) - ds.begin() - 1;
        int f2 = upper_bound(fs.begin(), fs.end(), 2*fs[i]) - fs.begin() - 1;
        int g1 = upper_bound(gs.begin(), gs.end(), fs[i]) - gs.begin();
        int g2 = upper_bound(gs.begin(), gs.end(), 2*fs[i]) - gs.begin() - 1;

        if (ds[d1] > fs[i] && ds[d2] <= 2*fs[i] && d1 != d2 && fs[f2] <= 2*fs[i] && f2 - i > 1 && gs[g1] > fs[i] && gs[g2] <= 2*fs[i]) {
            ans += (d2-d1)*(d2-d1+1)/2*(f2-i)*(f2-i+1)*(f2-i-1)/6*(g2-g1+1);
        }
    }

    cout << ans;
}


#include <bits/stdc++.h>
#include <ext/rope>
#define ll long long
using namespace std;
using namespace __gnu_cxx;

 int main () {
    int g, d, f;
    cin >> g >> d >> f;
    vector <int> gs(g), ds(d), fs(f);

    for (int i = 0; i < g; i++) {
        cin >> gs[i];
    }

    for (int i = 0; i < d; i++) {
        cin >> ds[i];
    }

    for (int i = 0; i < f; i++) {
        cin >> fs[i];
    }

    sort(gs.begin(), gs.end());
    sort(ds.begin(), ds.end());
    sort(fs.begin(), fs.end());

    ll ans = 0;

    for (int i = 0; i < g; i++) {
        ll d1 = upper_bound(ds.begin(), ds.end(), gs[i]) - ds.begin();
        ll d2 = upper_bound(ds.begin(), ds.end(), 2*gs[i]) - ds.begin() - 1;
        ll f1 = upper_bound(fs.begin(), fs.end(), gs[i]) - fs.begin();
        ll f2 = upper_bound(fs.begin(), fs.end(), 2*gs[i]) - fs.begin() - 1;

        if (ds[d1] > gs[i] && ds[d2] <= 2*gs[i] && d1 != d2 && fs[f1] > gs[i] && fs[f2] <= 2*gs[i] && f2 - f1 > 1) {
            ans += (d2-d1)*(d2-d1+1)/2*(f2-f1)*(f2-f1+1)*(f2-f1-1)/6;
        }
    }

    for (int i = 0; i < d-1; i++) {
        ll d2 = upper_bound(ds.begin(), ds.end(), 2*ds[i]) - ds.begin() - 1;
        ll f1 = upper_bound(fs.begin(), fs.end(), ds[i]) - fs.begin();
        ll f2 = upper_bound(fs.begin(), fs.end(), 2*ds[i]) - fs.begin() - 1;
        ll g1 = upper_bound(gs.begin(), gs.end(), ds[i]) - gs.begin();
        ll g2 = upper_bound(gs.begin(), gs.end(), 2*ds[i]) - gs.begin() - 1;

        if (ds[d2] <= 2*ds[i] && i != d2 && fs[f1] > ds[i] && fs[f2] <= 2*ds[i] && f2 - f1 > 1 && gs[g1] > ds[i] && gs[g2] <= 2*ds[i]) {
            ans += (d2-i)*(d2-i+1)/2*(f2-f1)*(f2-f1+1)*(f2-f1-1)/6*(g2-g1+1);
        }
    }

    for (int i = 0; i < f-2; i++) {
        ll d1 = upper_bound(ds.begin(), ds.end(), fs[i]) - ds.begin();
        ll d2 = upper_bound(ds.begin(), ds.end(), 2*fs[i]) - ds.begin() - 1;
        ll f2 = upper_bound(fs.begin(), fs.end(), 2*fs[i]) - fs.begin() - 1;
        ll g1 = upper_bound(gs.begin(), gs.end(), fs[i]) - gs.begin();
        ll g2 = upper_bound(gs.begin(), gs.end(), 2*fs[i]) - gs.begin() - 1;

        if (ds[d1] > fs[i] && ds[d2] <= 2*fs[i] && d1 != d2 && fs[f2] <= 2*fs[i] && f2 - i > 1 && gs[g1] > fs[i] && gs[g2] <= 2*fs[i]) {
            ans += (d2-d1)*(d2-d1+1)/2*(f2-i)*(f2-i+1)*(f2-i-1)/6*(g2-g1+1);
        }
    }

    cout << ans;
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <list>
#include <cstdlib>
#include <cmath>
#include <string>
#include <iomanip>
#include <functional>
#include <iterator>
#include <cstdio>
#include <string.h>
#include <memory.h>
#include <cstdint>

using namespace std;

typedef uint64_t ull;
typedef int64_t ll;
typedef long double ld;

int main()
{
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);

	int g, d, f, n;
	cin >> g >> d >> f;
	n = g + d + f;
	vector<ll> G(n), D(n), F(n);
	vector<pair<ll, int> > v(n);
	for (int i = 0; i < n; i++)
	{
		cin >> v[i].first;
		if (i < g) v[i].second = 1;
		else if (i < g + d) v[i].second = 2;
		else v[i].second = 3;
	}
	sort(v.begin(), v.end());
	for (int i = 0; i < n; i++)
	{
		G[i] = G[i - (i != 0)] + (v[i].second == 1);
		D[i] = D[i - (i != 0)] + (v[i].second == 2);
		F[i] = F[i - (i != 0)] + (v[i].second == 3);
	}
	ll ans = 0;
	for (int i = 0; i < n; i++)
	{
		int nid = upper_bound(v.begin(), v.end(), make_pair(2 * v[i].first, 3)) - v.begin();
		nid--;
		if (v[i].second == 1)
		{
			ans += (D[nid] - D[i])*(D[nid] - D[i] - 1) / 2 *
				   (F[nid] - F[i])*(F[nid] - F[i] - 1)*(F[nid] - F[i] - 2) / 6;
		}
		if (v[i].second == 2)
		{
			ans += (G[nid] - G[i])*
				   (D[nid] - D[i])*
				   (F[nid] - F[i])*(F[nid] - F[i] - 1)*(F[nid] - F[i] - 2) / 6;
		}
		if (v[i].second == 3)
		{
			ans += (G[nid] - G[i])*
				   (D[nid] - D[i])*(D[nid] - D[i] - 1) / 2 *
				   (F[nid] - F[i])*(F[nid] - F[i] - 1) / 2;
		}
	}
	cout << ans;

	return 0;
}


#include <iostream>
#include <vector>

using namespace std;
using LL = long long;

LL count(LL n, LL cnt) {
    if(cnt == 0)
        return 1;
    if(cnt == 1)
        return n;
    if(cnt == 2)
        return n*(n-1)/2;
    return n*(n-1)*(n-2)/6;
}

void solve() {
    int a,b,c;
    cin >> a >> b >> c;

    vector<pair<int,int>> players(a+b+c);

    vector<int> vr, def, att;

    for(int i = 0; i < players.size(); ++i) {
        cin >> players[i].first;
        if(i < a) {
            players[i].second = 1;
        } else if(i < a+b) {
            players[i].second = 2;
        } else {
            players[i].second = 3;
        }
    }

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

    LL ans = 0;

    for(int i = 0; i < players.size(); ++i) {
        int cnt[3] = {};

        for(int j = i + 1; j < players.size(); ++j) {
            if(players[i].first * 2 < players[j].first)
                continue;

            int vr_need = 1 - (players[i].second == 1) - (players[j].second == 1);
            int def_need = 2 - (players[i].second == 2) - (players[j].second == 2);
            int att_need = 3 - (players[i].second == 3) - (players[j].second == 3);

            if(vr_need < 0 || def_need < 0 || att_need < 0)
                continue;

            LL a = count(cnt[0], vr_need);
            LL b = count(cnt[1], def_need);
            LL c = count(cnt[2], att_need);

            ans += a*b*c;

            cnt[players[j].second - 1]++;
        }
    }

    cout << ans << endl;
}

int main() {
    solve();
}


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

using namespace std;
using LL = long long;

LL count(LL n, LL cnt) {
    if(cnt == 0)
        return 1;
    if(cnt == 1)
        return n;
    if(cnt == 2)
        return n*(n-1)/2;
    return n*(n-1)*(n-2)/6;
}

void solve() {
    int a,b,c;
    cin >> a >> b >> c;

    vector<pair<int,int>> players(a+b+c);

    vector<int> vr, def, att;

    for(int i = 0; i < players.size(); ++i) {
        cin >> players[i].first;
        if(i < a) {
            players[i].second = 1;
        } else if(i < a+b) {
            players[i].second = 2;
        } else {
            players[i].second = 3;
        }
    }

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

    LL ans = 0;

    for(int i = 0; i < players.size(); ++i) {
        int cnt[3] = {};

        for(int j = i + 1; j < players.size(); ++j) {
            if(players[i].first * 2 < players[j].first)
                continue;

            int vr_need = 1 - (players[i].second == 1) - (players[j].second == 1);
            int def_need = 2 - (players[i].second == 2) - (players[j].second == 2);
            int att_need = 3 - (players[i].second == 3) - (players[j].second == 3);

            if(vr_need < 0 || def_need < 0 || att_need < 0)
                continue;

            LL a = count(cnt[0], vr_need);
            LL b = count(cnt[1], def_need);
            LL c = count(cnt[2], att_need);

            ans += a*b*c;

            cnt[players[j].second - 1]++;
        }
    }

    cout << ans << endl;
}

int main() {
    solve();
}


#include <bits/stdc++.h>
using namespace std;
struct man{
	int n;
	int type;
};
bool comp (man i, man j) {
	return (i.n < j.n);
}
int main() {
	int g, d, f;
	cin >> g >> d >> f;
	vector <man> a;
	for ( int i = 0; i < g; i++) {
		int now;
		cin >> now;
		man h;
		h.n = now;
		h.type = 1;
		a.push_back(h);
	}
	for ( int i = 0; i < d; i++) {
		int now;
		cin >> now;
		man h;
		h.n = now;
		h.type = 2;
		a.push_back(h);
	}
	for ( int i = 0; i < f; i++) {
		int now;
		cin >> now;
		man h;
		h.n = now;
		h.type = 3;
		a.push_back(h);
	}
	int r = 0;
	long long first = 0;
	long long second = 0;
	long long third = 0;
	sort(a.begin(), a.end(), comp);
	if ( a[0].type == 1) {
		first = 1;
	}
	else if ( a[0].type == 2) {
		second = 1;
	}
	else {
		third = 1;
	}
	long long answer = 0;
	for ( int l = 0; l < a.size(); l++){
		while ( r + 1 < a.size() && a[l].n * 2 >= a[r + 1].n){
			r += 1;
			if ( a[r].type == 1) {
				first += 1;
			}
			else if ( a[r].type == 2) {
				second += 1;
			}
			else {
				third += 1;
			}
		}

		if ( a[l].type == 1 && second >= 2 && third >= 3) {
			long long def = ((second - 1) * second) / 2;
			long long at = (third * (third - 1) * (third - 2)) / 6;
			answer += (def * at);
		}
		if ( a[l].type == 2 && first >= 1 && third >= 3 && second >= 2) {
			long long def = first;
			long long def1 = second - 1;
			long long at = (third * (third - 1) * (third - 2)) / 6;
			answer += (def1 * def * at);
		}
		if ( a[l].type == 3 && first >= 1 && second >= 2 && third >= 3) {
			long long def = first;
			long long def1 = ((third - 2) * (third - 1)) / 2;
			long long at = ((second - 1) * second) / 2;
			answer += (def1 * def * at);
		}
		if ( a[l].type == 1) {
				first -= 1;
		}
		else if ( a[l].type == 2) {
				second -= 1;
		}
		else {
				third -= 1;
		}

	}
	cout << answer;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
struct man{
	int n;
	int type;
};
bool comp (man i, man j) {
	return (i.n < j.n);
}
int main() {
	int g, d, f;
	cin >> g >> d >> f;
	vector <man> a;
	for ( int i = 0; i < g; i++) {
		int now;
		cin >> now;
		man h;
		h.n = now;
		h.type = 1;
		a.push_back(h);
	}
	for ( int i = 0; i < d; i++) {
		int now;
		cin >> now;
		man h;
		h.n = now;
		h.type = 2;
		a.push_back(h);
	}
	for ( int i = 0; i < f; i++) {
		int now;
		cin >> now;
		man h;
		h.n = now;
		h.type = 3;
		a.push_back(h);
	}
	int r = 0;
	int first = 0;
	int second = 0;
	int third = 0;
	sort(a.begin(), a.end(), comp);
	if ( a[0].type == 1) {
		first = 1;
	}
	else if ( a[0].type == 2) {
		second = 1;
	}
	else {
		third = 1;
	}
	int answer = 0;
	for ( int l = 0; l < a.size(); l++){
		while ( r + 1 < a.size() && a[l].n * 2 >= a[r + 1].n){
			r += 1;
			if ( a[r].type == 1) {
				first += 1;
			}
			else if ( a[r].type == 2) {
				second += 1;
			}
			else {
				third += 1;
			}
		}

		if ( a[l].type == 1 && second >= 2 && third >= 3) {
			int def = ((second - 1) * second) / 2;
			int at = (third * (third - 1) * (third - 2)) / 6;
			answer += (def * at);
		}
		if ( a[l].type == 2 && first >= 1 && third >= 3 && second >= 2) {
			int def = first;
			int def1 = second - 1;
			int at = (third * (third - 1) * (third - 2)) / 6;
			answer += (def1 * def * at);
		}
		if ( a[l].type == 3 && first >= 1 && second >= 2 && third >= 3) {
			int def = first;
			int def1 = ((third - 2) * (third - 1)) / 2;
			int at = ((second - 1) * second) / 2;
			answer += (def1 * def * at);
		}
		if ( a[l].type == 1) {
				first -= 1;
		}
		else if ( a[l].type == 2) {
				second -= 1;
		}
		else {
				third -= 1;
		}

	}
	cout << answer;
	return 0;
}


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

using namespace std;

const int MAXX = 1000 + 5;
int a[MAXX], b[MAXX], cc[MAXX];

int main()
{
	long long int ans = 0;
	int f, g, h;
	cin >> f >> g >> h;
	for (int i = 0; i < f; i++)
		cin >> a[i];

	for (int i = 0; i < g; i++)
		cin >> b[i];

	for (int i = 0; i < h; i++)
		cin >> cc[i];

	sort(a, a + f);
	sort(b, b + g);
	sort(cc, cc + h);

	int rightB = 0, rightCc = 0;

	for (int i = 0; i < f; i++) {
		//������������� �������
		for (int j = 0; j < g; j++) {
			//������������� ���������
			//���� ��� ��� �����, ������������
			if (b[j] > 2 * a[i])
				break;

			rightB = 0;
			rightCc = 0;
			for (int k = 0; k < h; k++) {
				//������������� �����������
				//���� ��� ��� �����, ������������
				if (cc[k] > 2 * b[j] || cc[k] > 2 * a[i])
					break;

				int minn = min(a[i], min(b[j], cc[k]));

				if (a[i] > minn * 2 || b[j] > 2 * minn || cc[k] > 2 * minn) {
					continue;
				}

				int l1 = rightCc, r1 = h;
					while (l1 + 1 < r1) {
						int mid = (l1 + r1) >> 1;
						if (cc[mid] <= 2 * minn)
							l1 = mid;
						else
							r1 = mid;
					}
				rightCc = l1;

				int l = rightB, r = g;
				while (l + 1 < r) {
					int mid = (l + r) >> 1;
					if (b[mid] <= 2 * minn)
						l = mid;
					else
						r = mid;
				}
				rightB = l;

				ans += (l - j) * (l1 - k) * (l1 - k - 1) / 2;
			}
		}
	}

	cout << ans << endl;

	return 0;
}


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

using namespace std;

const int MAXX = 1000 + 5;
int a[MAXX], b[MAXX], cc[MAXX];

int main()
{
	long long int ans = 0;
	int f, g, h;
	cin >> f >> g >> h;
	for (int i = 0; i < f; i++)
		cin >> a[i];

	for (int i = 0; i < g; i++)
		cin >> b[i];

	for (int i = 0; i < h; i++)
		cin >> cc[i];

	sort(a, a + f);
	sort(b, b + g);
	sort(cc, cc + h);

	int rightB = 0, rightCc = 0;

	for (int i = 0; i < f; i++) {
		//������������� �������
		rightB = 0;
		for (int j = 0; j < g; j++) {
			//������������� ���������
			//���� ��� ��� �����, ������������
			if (b[j] > 2 * a[i])
				break;

			rightCc = 0;
			for (int k = 0; k < h; k++) {
				//������������� �����������
				//���� ��� ��� �����, ������������
				if (cc[k] > 2 * b[j] || cc[k] > 2 * a[i])
					break;

				int minn = min(a[i], min(b[j], cc[k]));

				if (a[i] > minn * 2 || b[j] > 2 * minn || cc[k] > 2 * minn) {
					continue;
				}

				int l1 = rightCc, r1 = h;
					while (l1 + 1 < r1) {
						int mid = (l1 + r1) >> 1;
						if (cc[mid] <= 2 * minn)
							l1 = mid;
						else
							r1 = mid;
					}
				rightCc = l1;

				int l = rightB, r = g;
				while (l + 1 < r) {
					int mid = (l + r) >> 1;
					if (b[mid] <= 2 * minn)
						l = mid;
					else
						r = mid;
				}
				rightB = l;

				ans += (l - j) * (l1 - k) * (l1 - k - 1) / 2;
			}
		}
	}

	cout << ans << endl;

	return 0;
}


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

bool check(int min_4, int max_4, int frw) {
    return ((min_4 >= frw && 2*frw >= max_4) || (min_4 <= frw && max_4 >= frw) || (frw >= max_4 && 2*min_4 >= frw));
}

int main() {
    std::ios_base::sync_with_stdio(false);
    int g, d, f;
    int64_t counter = 0;
    std::cin >> g >> d >> f;
    std::vector<int>  goal(g);
    std::vector<int>  def(d);
    std::vector<int>  frw(f);
    int max_1,max_2,max_3,max_4,min_1,min_2,min_3,min_4;
    std::vector<int>::iterator o_i, p_i, z_i, x_i, c_i;

    for (int i = 0; i < g; ++i) {
        std::cin >> goal[i];
    }

    for (int i = 0; i < d; ++i) {
        std::cin >> def[i];
    }

    for (int i = 0; i < f; ++i) {
        std::cin >> frw[i];
    }
    std::sort(goal.begin(), goal.end());
    std::sort(def.begin(), def.end());
    std::sort(frw.begin(), frw.end());

    for (size_t i = 0; i < g; ++i) {
        //std::cout << " iter i : " << i << " data : " << goal[i] << std::endl;
        o_i = std::upper_bound(def.begin(), def.end()-1, goal[i]/2);
        if (def.end()-1 == o_i) continue;
        for (size_t o = (o_i - def.begin()); o < d-1; ++o) {
            //std::cout << " --iter o : " << o  << " data :" << def[o] << std::endl;
            if (float(def[o])/goal[i] > 2.) {
                break;
            }
            min_1 = std::min(goal[i], def[o]);
            max_1 = std::max(goal[i], def[o]);
            //std::cout << " min / max after epoch : " << min_1 << " / " << max_1 << std::endl;
            for (size_t p = o+1; p < d; ++p) {
                //std::cout << " --iter p : " << p << " data :" << def[p] << std::endl;
                if (def[p] > 2*min_1) {
                    break;
                }
                if (def[p] < max_1/2) continue;
                min_2 = std::min(min_1, def[p]);
                max_2 = std::max(max_1, def[p]);
                //std::cout << " min / max after epoch : " << min_2 << " / " << max_2 << std::endl;
                z_i = std::upper_bound(frw.begin(), frw.end()-2, min_2/2);
                for (int z = (z_i - frw.begin()); z < f-2; ++z) {
                    //std::cout << " --iter z : " << z << " data :" << frw[z] << std::endl;
                    if (frw[z] > 2*min_2) {
                        break;
                    }
                    if (frw[z] < max_2/2) continue;
                    min_3 = std::min(min_2, frw[z]);
                    max_3 = std::max(max_2, frw[z]);
                    //std::cout << " min / max after epoch : " << min_3 << " / " << max_3 << std::endl;
                    for (int x = z + 1; x < f-1; ++x) {
                        //std::cout << " --iter x : " << x << " data :" << frw[x] << std::endl;
                        if (frw[x] > 2*min_3) {
                            break;
                        }
                        if (frw[x] < max_3/2) continue;
                        min_4 = std::min(min_3, frw[x]);
                        max_4 = std::max(max_3, frw[x]);
                        //std::cout << " min / max after epoch : " << min_4 << " / " << max_4 << std::endl;
                        for (int c = x + 1; c < f; ++c) {
                            //std::cout << " --iter c : " << c << " data : " << frw[c] << std::endl;
                            if (frw[c] > 2*min_4) {
                                break;
                            }
                            if (frw[c] < max_4/2) continue;
                            ++counter;
                            //std::cout << " ----aproved ----" << std::endl;
                        }
                    }
                }
            }
        }
    }
    //std::cout << float(clock() - t1)/CLOCKS_PER_SEC << "\n";
    std::cout << counter << "\n";

}


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

int main() {
    std::ios_base::sync_with_stdio(false);
    int g, d, f;
    int64_t counter = 0;
    std::cin >> g >> d >> f;
    std::vector<int>  goal(g);
    std::vector<int>  def(d);
    std::vector<int>  frw(f);
    std::vector<int>::iterator o_i, p_i, z_i, x_i, c_i;

    for (int i = 0; i < g; ++i) {
        std::cin >> goal[i];
    }

    for (int i = 0; i < d; ++i) {
        std::cin >> def[i];
    }

    for (int i = 0; i < f; ++i) {
        std::cin >> frw[i];
    }
    std::sort(goal.begin(), goal.end());
    std::sort(def.begin(), def.end());
    std::sort(frw.begin(), frw.end());

    for (int i = 0; i < g; ++i) {
        //std::cout << " iteration i : " << i << std::endl;
        o_i = std::upper_bound(def.begin(), def.end()-1, goal[i]/2);
        if (def.end()-1 == o_i) continue;
        while(o_i != def.end()-1) {
            if ((*o_i) > goal[i]*2) break;
            //std::cout << " 1_1 : " << *o_i << std::endl;
            p_i = std::upper_bound(o_i+1, def.end(), (*o_i)/2);
            if (def.end() == p_i) continue;
            while(p_i != def.end()) {
                if (*p_i > (*o_i) * 2) break;
                //std::cout << " 1_2 : " << *p_i << std::endl;
                z_i = std::upper_bound(frw.begin(), frw.end()-2, (*p_i)/2);
                if (frw.end()-2 == z_i) continue;
                while(z_i != frw.end()-2) {
                    if ((*z_i) > (*p_i) * 2) break;
                    //std::cout << " 2_1 : " << *z_i << std::endl;
                    x_i = std::upper_bound(z_i+1, frw.end()-1, (*z_i)/2);
                    if (frw.end()-1 == z_i) continue;
                    while (x_i != frw.end()-1) {
                        if ((*x_i) > (*z_i) * 2) break;
                        //std::cout << " 2_2 : " << *x_i << std::endl;
                        c_i = std::upper_bound(x_i+1, frw.end(), (*x_i)/2);
                        if (frw.end() == c_i) continue;
                        while (c_i != frw.end()) {
                            if ((*c_i) > (*x_i) * 2) break;
                            ++counter;
                            ++c_i;
                        }
                        ++x_i;
                    }
                    ++z_i;
                }
                ++p_i;
            }
            ++o_i;
        }
    }
    std::cout << counter << "\n";

}


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

int main() {
    std::ios_base::sync_with_stdio(false);
    int g, d, f;
    int64_t counter = 0;
    std::cin >> g >> d >> f;
    std::vector<int>  goal(g);
    std::vector<int>  def(d);
    std::vector<int>  frw(f);
    std::vector<int>::iterator o_i, p_i, z_i, x_i, c_i;

    for (int i = 0; i < g; ++i) {
        std::cin >> goal[i];
    }

    for (int i = 0; i < d; ++i) {
        std::cin >> def[i];
    }

    for (int i = 0; i < f; ++i) {
        std::cin >> frw[i];
    }
    std::sort(goal.begin(), goal.end());
    std::sort(def.begin(), def.end());
    std::sort(frw.begin(), frw.end());

    for (int i = 0; i < g; ++i) {
        //std::cout << " iteration i : " << i << std::endl;
        o_i = std::upper_bound(def.begin(), def.end()-1, goal[i]/2);
        if (def.end()-1 == o_i) continue;
        while(o_i != def.end()-1) {
            if ((*o_i) > goal[i]*2) break;
            //std::cout << " 1_1 : " << *o_i << std::endl;
            p_i = o_i+1;
            while(p_i != def.end()) {
                if (*p_i > (*o_i) * 2) break;
                //std::cout << " 1_2 : " << *p_i << std::endl;
                z_i = std::upper_bound(frw.begin(), frw.end()-2, (*p_i)/2);
                if (frw.end()-2 == z_i) continue;
                while(z_i != frw.end()-2) {
                    if ((*z_i) > (*p_i) * 2) break;
                    //std::cout << " 2_1 : " << *z_i << std::endl;
                    x_i = z_i+1;
                    if (frw.end()-1 == z_i) continue;
                    while (x_i != frw.end()-1) {
                        if ((*x_i) > (*z_i) * 2) break;
                        //std::cout << " 2_2 : " << *x_i << std::endl;
                        c_i = x_i+1;
                        if (frw.end() == c_i) continue;
                        while (c_i != frw.end()) {
                            if ((*c_i) > (*x_i) * 2) break;
                            ++counter;
                            ++c_i;
                        }
                        ++x_i;
                    }
                    ++z_i;
                }
                ++p_i;
            }
            ++o_i;
        }
    }
    std::cout << counter << "\n";

}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <time.h>
#include <utility>

int Bin(int n, int k) {
    double res = 1;
    for (int i = 1; i <= k; ++i)
        res = res*(n-k+i)/i;
    return (int) (res + 0.01);
}

int main() {
    std::ios_base::sync_with_stdio(false);
    int g, d, f;
    int64_t counter = 0;
    std::cin >> g >> d >> f;
    std::vector<std::pair<int, int>>  vec(g+d+f);
    int tmp_num;
    int tmp = 0;
    int gl = 0, df = 0, fw = 0;

    for (int i = 0; i < g; ++i) {
        std::cin >> tmp_num;
        vec[i] = std::pair<int, int>(tmp_num, 1);
    }

    for (int i = 0; i < d; ++i) {
        std::cin >> tmp_num;
        vec[i + g] = std::pair<int, int>(tmp_num, 2);
    }

    for (int i = 0; i < f; ++i) {
        std::cin >> tmp_num;
        vec[i + g + d] = std::pair<int, int>(tmp_num, 3);
    }
    std::sort(vec.begin(), vec.end());

    std::vector<int> answer(g+d+f);

    for (int j = 0; j < 5; ++j) {
        if (vec[j].first >= vec[5].first/2) {
            tmp = j;
            break;
        }
    }

    for (int j = tmp; j < 5; ++j) {
        if (vec[j].second == 1) ++gl;
        if (vec[j].second == 2) ++df;
        if (vec[j].second == 3) ++fw;
    }

    for (int i = 5; i < answer.size(); ++i) {
        if (vec[i].second == 1) {
            ++gl;
        };
        if (vec[i].second == 2){
            ++df;
        };
        if (vec[i].second == 3){
            ++fw;
        };
        while (vec[tmp].first < (vec[i].first + 1)/2) {
            if (vec[tmp].second == 1) {
                gl -= 1;
            }
            if (vec[tmp].second == 2) {
                df -= 1;
            }
            if (vec[tmp].second == 3) {
                fw -= 1;
            }
            ++tmp;
        }
        if (vec[i].second == 1) {
            answer[i] = answer[i-1] + (Bin(df, 2) * Bin(fw, 3));
        } else if (vec[i].second == 2) {
            answer[i] = answer[i-1] + (gl * Bin(fw, 3) *(Bin(df, 2) - Bin(df - 1, 2)));
        } else if (vec[i].second == 3) {
            answer[i] = answer[i-1] + (gl * Bin(df, 2) *(Bin(fw, 3) - Bin(fw-1, 3)));
        }
    }
    //std::cout << float(clock() - t1)/CLOCKS_PER_SEC << "\n";
    std::cout << answer[answer.size()-1] << std::endl;
}


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

int main() {
    int g, d, f;
    int64_t counter = 0;
    std::cin >> g >> d >> f;
    std::vector<int>  goal(g);
    std::vector<int>  def(d);
    std::vector<int>  frw(f);
    int max, min;

    for (int i = 0; i < g; ++i) {
        std::cin >> goal[i];
    }

    for (int i = 0; i < d; ++i) {
        std::cin >> def[i];
    }

    for (int i = 0; i < f; ++i) {
        std::cin >> frw[i];
    }

    for (int i = 0; i < g; ++i) {
        for (int o = 0; o < d-1; ++o) {
            for (int p = o + 1; p < d; ++p) {
                for (int z = 0; z < f-2; ++z) {
                    for (int x = z + 1; x < f-1; ++x) {
                        for (int c = x + 1; c < f; ++c) {
                            min = goal[i];
                            max = goal[i];
                            std::vector<int> asd {goal[i], def[o], def[p], frw[z], frw[x], frw[c]};
                            if (def[o] < min) min = def[o];
                            if (def[p] < min) min = def[p];
                            if (frw[z] < min) min = frw[z];
                            if (frw[x] < min) min = frw[x];
                            if (frw[c] < min) min = frw[c];
                            if (def[o] > max) max = def[o];
                            if (def[p] > max) max = def[p];
                            if (frw[z] > max) max = frw[z];
                            if (frw[x] > max) max = frw[x];
                            if (frw[c] > max) max = frw[c];
                            if (min*2 >= max) ++counter;
                        }
                    }
                }
            }
        }
    }

    std::cout << counter << std::endl;

}


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

int main() {
    int g, d, f;
    int64_t counter = 0;
    std::cin >> g >> d >> f;
    std::vector<int>  goal(g);
    std::vector<int>  def(d);
    std::vector<int>  frw(f);
    int max_1,max_2,max_3,max_4,max_5,min_1,min_2,min_3,min_4,min_5;

    for (int i = 0; i < g; ++i) {
        std::cin >> goal[i];
    }

    for (int i = 0; i < d; ++i) {
        std::cin >> def[i];
    }

    for (int i = 0; i < f; ++i) {
        std::cin >> frw[i];
    }

    std::sort(goal.begin(), goal.end());
    std::sort(def.begin(), def.end());
    std::sort(frw.begin(), frw.end());

    for (int i = 0; i < g; ++i) {
        for (int o = 0; o < d-1; ++o) {
            if (def[o] < goal[i] && goal[i] > 2*def[o]) {
                continue;
            }
            if (def[o] > goal[i] && def[o] > 2*goal[i]) {
                break;
            }
            min_1 = std::min(goal[i], def[o]);
            max_1 = std::max(goal[i], def[o]);
            for (int p = o + 1; p < d; ++p) {
                if (def[p] < min_1 && min_1 > 2*def[p]) {
                    continue;
                }
                if (def[p] > min_1 && def[p] > 2*min_1) {
                    break;
                }
                min_2 = std::min(min_1, def[p]);
                max_2 = std::max(max_1, def[p]);
                for (int z = 0; z < f-2; ++z) {
                    if (frw[z] < min_2 && min_2 > 2*frw[z]) {
                        continue;
                    }
                    if (frw[z] > min_2 && frw[z] > 2*min_2) {
                        break;
                    }
                    min_3 = std::min(min_2, frw[z]);
                    max_3 = std::max(max_2, frw[z]);
                    for (int x = z + 1; x < f-1; ++x) {
                        if (frw[x] < min_3 && min_3 > 2*frw[x]) {
                            continue;
                        }
                        if (frw[x] > min_3 && frw[x] > 2*min_3) {
                            break;
                        }
                        min_4 = std::min(min_3, frw[x]);
                        max_4 = std::max(max_3, frw[x]);
                        for (int c = x + 1; c < f; ++c) {
                            if ((min_4 >= frw[c] && 2*frw[c] >= max_4) || (min_4 <= frw[c] && max_4 >= frw[c]) || (frw[c] >= max_4 && 2*min_4 >= frw[c])) {
                                ++counter;
                            }
                        }
                    }
                }
            }
        }
    }

    std::cout << counter << std::endl;

}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <time.h>
#include <utility>

int Bin(int n, int k) {
    double res = 1;
    for (int i = 1; i <= k; ++i)
        res = res*(n-k+i)/i;
    return (int) (res + 0.01);
}

int main() {
    std::ios_base::sync_with_stdio(false);
    int64_t g, d, f;
    int64_t counter = 0;
    std::cin >> g >> d >> f;
    std::vector<std::pair<int64_t, int64_t>>  vec(g+d+f);
    int64_t tmp_num;
    int64_t tmp = 0;
    int64_t gl = 0, df = 0, fw = 0;

    for (int64_t i = 0; i < g; ++i) {
        std::cin >> tmp_num;
        vec[i] = std::pair<int64_t, int64_t>(tmp_num, 1);
    }

    for (int64_t i = 0; i < d; ++i) {
        std::cin >> tmp_num;
        vec[i + g] = std::pair<int64_t, int64_t>(tmp_num, 2);
    }

    for (int64_t i = 0; i < f; ++i) {
        std::cin >> tmp_num;
        vec[i + g + d] = std::pair<int64_t, int64_t>(tmp_num, 3);
    }
    std::sort(vec.begin(), vec.end());

    std::vector<int64_t> answer(g+d+f);

    for (int64_t j = 0; j < 5; ++j) {
        if (vec[j].first >= float(vec[5].first)/2) {
            tmp = j;
            break;
        }
    }

    for (int j = tmp; j < 5; ++j) {
        if (vec[j].second == 1) ++gl;
        if (vec[j].second == 2) ++df;
        if (vec[j].second == 3) ++fw;
    }

    for (int64_t i = 5; i < answer.size(); ++i) {
        if (vec[i].second == 1) {
            ++gl;
        };
        if (vec[i].second == 2){
            ++df;
        };
        if (vec[i].second == 3){
            ++fw;
        };
        while (vec[tmp].first < (vec[i].first + 1)/2) {
            if (vec[tmp].second == 1) {
                gl -= 1;
            }
            if (vec[tmp].second == 2) {
                df -= 1;
            }
            if (vec[tmp].second == 3) {
                fw -= 1;
            }
            ++tmp;
        }
        if (vec[i].second == 1) {
            answer[i] = answer[i-1] + (Bin(df, 2) * Bin(fw, 3));
        } else if (vec[i].second == 2) {
            answer[i] = answer[i-1] + (gl * Bin(fw, 3) *(Bin(df, 2) - Bin(df - 1, 2)));
        } else if (vec[i].second == 3) {
            answer[i] = answer[i-1] + (gl * Bin(df, 2) *(Bin(fw, 3) - Bin(fw-1, 3)));
        }
    }
    //std::cout << float(clock() - t1)/CLOCKS_PER_SEC << "\n";
    std::cout << answer[answer.size()-1] << std::endl;
}


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

int main() {
    std::ios_base::sync_with_stdio(false);
    int g, d, f;
    int64_t counter = 0;
    std::cin >> g >> d >> f;
    std::vector<int>  goal(g);
    std::vector<int>  def(d);
    std::vector<int>  frw(f);
    std::vector<int>::iterator o_i, z_i;

    for (int i = 0; i < g; ++i) {
        std::cin >> goal[i];
    }

    for (int i = 0; i < d; ++i) {
        std::cin >> def[i];
    }

    for (int i = 0; i < f; ++i) {
        std::cin >> frw[i];
    }
    std::sort(goal.begin(), goal.end());
    std::sort(def.begin(), def.end());
    std::sort(frw.begin(), frw.end());

    for (size_t i = 0; i < g; ++i) {
        o_i = std::upper_bound(def.begin(), def.end()-1, goal[i]/2);
        if (def.end()-1 == o_i) continue;
        for (size_t o = (o_i - def.begin()); o < d-1; ++o) {
            if (def[o] > 2*goal[i]) {
                break;
            }
            for (size_t p = o+1; p < d; ++p) {
                if (def[p] > 2*def[o]) {
                    break;
                }
                z_i = std::upper_bound(frw.begin(), frw.end()-2, def[p]/2);
                for (size_t z = (z_i - frw.begin()); z < f-2; ++z) {
                    if (frw[z] > 2*def[p]) {
                        break;
                    }
                    for (int x = z + 1; x < f-1; ++x) {
                        if (frw[x] > 2*frw[z]) {
                            break;
                        }
                        for (int c = x + 1; c < f; ++c) {
                            if (frw[c] > 2*frw[x]) {
                                break;
                            }
                            ++counter;
                        }
                    }
                }
            }
        }
    }
    std::cout << counter << "\n";

}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <time.h>
#include <utility>

int64_t Bin(int64_t n, int64_t k) {
    double res = 1;
    for (int64_t i = 1; i <= k; ++i)
        res = res*(n-k+i)/i;
    return (int64_t) (res + 0.01);
}

int main() {
    std::ios_base::sync_with_stdio(false);
    int64_t g, d, f;
    int64_t counter = 0;
    std::cin >> g >> d >> f;
    std::vector<std::pair<int64_t, int64_t>>  vec(g+d+f);
    int64_t tmp_num;
    int64_t tmp = 0;
    int64_t gl = 0, df = 0, fw = 0;

    for (int64_t i = 0; i < g; ++i) {
        std::cin >> tmp_num;
        vec[i] = std::pair<int64_t, int64_t>(tmp_num, 1);
    }

    for (int64_t i = 0; i < d; ++i) {
        std::cin >> tmp_num;
        vec[i + g] = std::pair<int64_t, int64_t>(tmp_num, 2);
    }

    for (int64_t i = 0; i < f; ++i) {
        std::cin >> tmp_num;
        vec[i + g + d] = std::pair<int64_t, int64_t>(tmp_num, 3);
    }
    std::sort(vec.begin(), vec.end());

    std::vector<int64_t> answer(g+d+f);

    for (int64_t j = 0; j < 5; ++j) {
        if (vec[j].first >= float(vec[5].first)/2) {
            tmp = j;
            break;
        }
    }

    for (int j = tmp; j < 5; ++j) {
        if (vec[j].second == 1) ++gl;
        if (vec[j].second == 2) ++df;
        if (vec[j].second == 3) ++fw;
    }

    for (int64_t i = 5; i < answer.size(); ++i) {
        if (vec[i].second == 1) {
            ++gl;
        };
        if (vec[i].second == 2){
            ++df;
        };
        if (vec[i].second == 3){
            ++fw;
        };
        while (vec[tmp].first < (vec[i].first + 1)/2) {
            if (vec[tmp].second == 1) {
                gl -= 1;
            }
            if (vec[tmp].second == 2) {
                df -= 1;
            }
            if (vec[tmp].second == 3) {
                fw -= 1;
            }
            ++tmp;
        }
        if (vec[i].second == 1) {
            answer[i] = answer[i-1] + (Bin(df, 2) * Bin(fw, 3));
        } else if (vec[i].second == 2) {
            answer[i] = answer[i-1] + (gl * Bin(fw, 3) *(Bin(df, 2) - Bin(df - 1, 2)));
        } else if (vec[i].second == 3) {
            answer[i] = answer[i-1] + (gl * Bin(df, 2) *(Bin(fw, 3) - Bin(fw-1, 3)));
        }
    }
    //std::cout << float(clock() - t1)/CLOCKS_PER_SEC << "\n";
    std::cout << answer[answer.size()-1] << std::endl;
}


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

int main() {
    std::ios_base::sync_with_stdio(false);
    int g, d, f;
    int64_t counter = 0;
    std::cin >> g >> d >> f;
    std::vector<int>  goal(g);
    std::vector<int>  def(d);
    std::vector<int>  frw(f);
    std::vector<int>::iterator o_i, z_i;

    for (int i = 0; i < g; ++i) {
        std::cin >> goal[i];
    }

    for (int i = 0; i < d; ++i) {
        std::cin >> def[i];
    }

    for (int i = 0; i < f; ++i) {
        std::cin >> frw[i];
    }
    std::sort(goal.begin(), goal.end());
    std::sort(def.begin(), def.end());
    std::sort(frw.begin(), frw.end());

    for (size_t i = 0; i < g; ++i) {
        //std::cout << " iter i : " << i << std::endl;
        o_i = std::upper_bound(def.begin(), def.end()-1, goal[i]/2);
        if (def.end()-1 == o_i) continue;
        for (size_t o = (o_i - def.begin()); o < d-1; ++o) {
            if (def[o] > 2*goal[i]) {
                break;
            }
            //std::cout << " --iter o : " << o  << " data :" << def[o] << std::endl;
            for (size_t p = o+1; p < d; ++p) {
                if (def[p] > 2*def[o]) {
                    break;
                }
                //std::cout << " --iter p : " << p << " data :" << def[p] << std::endl;
                z_i = std::upper_bound(frw.begin(), frw.end()-2, def[p]/2);
                for (int z = (z_i - frw.begin()); z < f-2; ++z) {
                    if (frw[z] > 2*def[p]) {
                        break;
                    }
                    //std::cout << " --iter z : " << z << " data :" << frw[z] << std::endl;
                    for (int x = z + 1; x < f-1; ++x) {
                        if (frw[x] > 2*frw[z]) {
                            break;
                        }
                        //std::cout << " --iter x : " << x << " data :" << frw[x] << std::endl;
                        for (int c = x + 1; c < f; ++c) {
                            if (frw[c] > 2*frw[x]) {
                                break;
                            }
                            //std::cout << " --iter c : " << c << " data :" << frw[c] << std::endl;
                            ++counter;
                        }
                    }
                }
            }
        }
    }
    //std::cout << float(clock() - t1)/CLOCKS_PER_SEC << "\n";
    std::cout << counter << "\n";

}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <time.h>
#include <utility>

int Bin(int n, int k) {
    double res = 1;
    for (int i = 1; i <= k; ++i)
        res = res*(n-k+i)/i;
    return (int) (res + 0.01);
}

int main() {
    std::ios_base::sync_with_stdio(false);
    int g, d, f;
    int64_t counter = 0;
    std::cin >> g >> d >> f;
    std::vector<std::pair<int, int>>  vec(g+d+f);
    int tmp_num;
    int tmp = 0;
    int gl = 0, df = 0, fw = 0;

    for (int i = 0; i < g; ++i) {
        std::cin >> tmp_num;
        vec[i] = std::pair<int, int>(tmp_num, 1);
    }

    for (int i = 0; i < d; ++i) {
        std::cin >> tmp_num;
        vec[i + g] = std::pair<int, int>(tmp_num, 2);
    }

    for (int i = 0; i < f; ++i) {
        std::cin >> tmp_num;
        vec[i + g + d] = std::pair<int, int>(tmp_num, 3);
    }
    std::sort(vec.begin(), vec.end());

    std::cout << std::endl;

    std::vector<int> answer(g+d+f);

    answer[4] = 0;
    for (int j = 0; j < 5; ++j) {
        if (vec[j].first >= vec[5].first/2) {
            tmp = j;
            break;
        }
    }

    for (int j = tmp; j < 5; ++j) {
        if (vec[j].second == 1) ++gl;
        if (vec[j].second == 2) ++df;
        if (vec[j].second == 3) ++fw;
    }

    answer[4] = 0;
    for (int i = 5; i < answer.size(); ++i) {
        if (vec[i].second == 1) ++gl;
        if (vec[i].second == 2) ++df;
        if (vec[i].second == 3) ++fw;
        while (vec[tmp].first < vec[i].first/2) {
            if (vec[tmp].second == 1)
                gl -= 1;
            if (vec[tmp].second == 2)
                df -= 1;
            if (vec[tmp].second == 3)
                fw -= 1;
            ++tmp;
        }
        if (vec[i].second == 1) {
            answer[i] = answer[i-1] + ((gl-1) * Bin(df, 2) * Bin(fw, 3));
        } else if (vec[i].second == 2) {
            answer[i] = answer[i-1] + (gl * Bin(fw, 3) *(Bin(df, 2) - Bin(df-1, 2)));
        } else if (vec[i].second == 3) {
            answer[i] = answer[i-1] + (gl * Bin(df, 2) *(Bin(fw, 3) - Bin(fw-1, 3)));
        }
    }
    //std::cout << float(clock() - t1)/CLOCKS_PER_SEC << "\n";
    std::cout << answer[answer.size()-1] << std::endl;
}