#include <bits/stdc++.h>
using namespace std;
/// MACROS
#define int long long
#define itn int
#define str string
#define sz size
#define ll long long
#define ull unsigned long long
#define ld long double
#define ii pair<int, int>
#define fs first
#define sc second
#define vi vector<int>
#define vii vector<ii>
#define pb push_back
#define pf push_front
#define fl '\n'
#define el endl
#define eps 1e-12
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
/// FUNCTIONS
#define db(x) cerr << #x << ": " << (x) << '\n';
#define lg2(x) __lg(x)
#define pi acos(-1)
#define YN(x) cout << ((x) ? ("YES") : ("NO")) << fl;
#define yn(x) cout << ((x) ? ("Yes") : ("No")) << fl;
#define precision(x) cout.setf(ios::fixed);cout.precision(x);
#define FL cout<<fl;
#define EL cout<<el;
#define ESP cout<<" ";
void print(int &x){cout<<x;}
void print(ii x){cout<<x.fs<<" "<<x.sc;}
void print(const auto &v){for(auto it: v){print(it);cout<<" ";}cout<<fl;}
void printp(const auto &v){for(auto it: v){print((ii)it);cout<<fl;}}
vi read(int cnt){vi v(cnt);for(int i=0;i<cnt;i++)cin>>v[i];return v;}
vii readp(int cnt){vii v(cnt);for(int i=0;i<cnt;i++)cin>>v[i].fs>>v[i].sc;return v;}
/////////////////////////////////////////////////////////////////////////////////////
template <typename T>
T s_prev(const std::set<T>& s, const T& val, const T& defaultValue) {
auto it=s.lower_bound(val);
if (it==s.begin()) return defaultValue;
--it;
return *it;
}
template <typename T>
T s_next(const std::set<T>& s, const T& val, const T& defaultValue) {
auto it=s.upper_bound(val);
if (it==s.end()) return defaultValue;
return *it;
}
/////////////////////////////////////////////////////////////////////////////////////
itn n;
vi arr;
int spt[25][1000006];
void build(){
for(int i=0;i<n;i++){spt[0][i]=arr[i];}
for(int i=1;i<25;i++){
for(int j=0;j+((int)1<<i)-1<n;j++){
spt[i][j]=min(spt[i-1][j],spt[i-1][j+((int)1<<(i-1))]);
}
}
}
int qmin(int l,int r){
if(l>r)return 1e12;
int lg=lg2(r-l+1);
return min(spt[lg][l],spt[lg][r-((int)1<<lg)+1]);
}
int spt2[25][1000006];
void build2(){
for(int i=0;i<n;i++){spt2[0][i]=arr[i];}
for(int i=1;i<25;i++){
for(int j=0;j+((int)1<<i)-1<n;j++){
spt2[i][j]=max(spt2[i-1][j],spt2[i-1][j+((int)1<<(i-1))]);
}
}
}
int qmax(int l,int r){
if(l>r)return -1e12;
int lg=lg2(r-l+1);
return max(spt2[lg][l],spt2[lg][r-((int)1<<lg)+1]);
}
void solve(){
cin>>n;
arr=read(n);
build();
build2();
vii v2;
for(int i=0;i<n;i++){
v2.pb({arr[i],i});
}
sort(all(v2));
itn ans=qmax(0,n-1)-qmin(0,n-1);
reverse(all(v2));
int l=v2[0].sc;
int r=v2[0].sc;
for(int i=0;i<n;i++){
l=min(l,v2[i].sc);
r=max(r,v2[i].sc);
if(l==0 && r==n-1){
ans=min(ans,qmax(l,r)-qmin(l,r));
continue;
}
vi t;
int mir=qmin(l,r);
int mx=-1e12;
mx=max(mx,qmax(0,l-1));
mx=max(mx,qmax(r+1,n-1));
t.pb(mx);
int mi=1e12;
mi=min(mi,qmin(0,l-1));
mi=min(mi,qmin(r+1,n-1));
t.pb(mi);
t.pb(mx-(v2[0].fs-mir));
sort(all(t));
ans=min(ans,t.back()-t[0]);
}
reverse(all(v2));
int l=v2[0].sc;
int r=v2[0].sc;
for(int i=0;i<n;i++){
l=min(l,v2[i].sc);
r=max(r,v2[i].sc);
if(l==0 && r==n-1){
ans=min(ans,qmax(l,r)-qmin(l,r));
continue;
}
vi t;
int mir=qmax(l,r);
int mx=-1e12;
mx=max(mx,qmax(0,l-1));
mx=max(mx,qmax(r+1,n-1));
t.pb(mx);
int mi=1e12;
mi=min(mi,qmin(0,l-1));
mi=min(mi,qmin(r+1,n-1));
t.pb(mi);
t.pb(mi + (qmax(l,r)-qmin(l,r)));
sort(all(t));
ans=min(ans,t.back()-t[0]);
}
cout<<ans<<fl;
}
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int tc=1;
cin>>tc;
while(tc--){
solve();
}
return 0;
}