#include<bits/stdc++.h>
using namespace std;
int n;
long long int a[100005];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{ scanf("%d",&n);
long long int sum=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
sum+=a[i];
}
if(n==2)
{ printf("%lld\n",min(a[1],a[2]));
}
else
{
set<long long int> s;
for(int i=1;i<=n;i++)
{ long long int A,D;
if(i==1)
{ A=a[i+1];
D=a[i+2]-a[i+1];
sum-=a[i];
if((a[n]==(A+D*(n-2)))&&(sum==(((n-1)*(A+a[n]))/2)))
{ s.insert(a[i]);
}
sum+=a[i];
}
else if(i==n)
{ A=a[1];
D=a[i-1]-a[i-2];
sum-=a[i];
if((a[i-1]==(A+D*(n-2)))&&(sum==(((n-1)*(2*A+(n-2)*D))/2)))
{
s.insert(a[i]);
}
sum+=a[i];
}
else
{ A=a[1];
D=a[i+1]-a[i-1];
sum-=a[i];
if((a[n]==(A+D*(n-2)))&&(sum==(((n-1)*(2*A+(n-2)*D))/2)))
{ s.insert(a[i]);
}
sum+=a[i];
}
}
if(s.empty())
{ printf("-1\n");
}
else
{ set<long long int>:: iterator it;
it=s.begin();
printf("%lld\n",*it);
}
}
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IG47CmxvbmcgbG9uZyBpbnQgYVsxMDAwMDVdOwogCmludCBtYWluKCkKeyAgICAKICAgIGludCB0OwogICAgc2NhbmYoIiVkIiwmdCk7CiAgICB3aGlsZSh0LS0pCiAgICB7ICAgc2NhbmYoIiVkIiwmbik7CiAgICAgICAgbG9uZyBsb25nIGludCBzdW09MDsKICAgICAgICBmb3IoaW50IGk9MTtpPD1uO2krKykKICAgICAgIHsKCQkgc2NhbmYoIiVsbGQiLCZhW2ldKTsKCQkgc3VtKz1hW2ldOwoJICAgIH0KCQkgCiAgICAgICAgCiAgICAgICAgaWYobj09MikKICAgICAgICB7ICAgcHJpbnRmKCIlbGxkXG4i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