//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

#define prec setprecision

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T m, T n) {while(n) {T t=m%n; m=n;n=t;};return m;}
template<typename T> T exgcd(T a, T b, T& sa, T& ta) {T q, r, sb=0, tb=1, sc, tc; sa=1,ta=0; if(b) do q = a/b, r = a-q*b, a = b, b = r, sc = sa-q*sb, sa = sb, sb = sc, tc = ta-q*tb, ta = tb, tb = tc; while(b); return a; }
template<typename T> T mul_inv(T a, T b) { T t1 = a, t2 = b, t3; T v1 = 1, v2 = 0, v3; T x; while (t2 != 1) x = t1/t2, t3 = t1 - x*t2, v3 = v1 - x*v2, t1 = t2, t2 = t3, v1 = v2, v2 = v3; return (v2 + b) % b; }
template<typename T> T powmod(T a, T b, T MOD) {if (b < 0) return 0; T rv = 1; while (b) (b % 2) && (rv = (rv*a) % MOD), a = a*a%MOD, b /= 2; return rv;}
template<ll n> struct BitSize{enum{Size=BitSize<n/2>::Size+1};};
template<> struct BitSize<0>{enum{Size=1};};
template<> struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define BITMAX(n) (BitSize<n>::Size - 1)

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
	while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif

const int maxt = 1e6 + 7;
int t;
int a[maxt];
int b[maxt];
int cnt[maxt];
int mx = 0;

bool valid(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  if(m*n != t) return false;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      if(z>mx) return false;
      b[z]++;
    }
  }
  for(int i=1;i<=mx;i++) if(cnt[i]!=b[i]) return false;
  cout << m << " " << n << endl << x << " " << y << endl;
  return true;
}

int main(int argc, char *argv[]) {
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  cin >> t;
  memset(cnt, 0, sizeof(cnt));
  for(int i=1;i<=t;i++) cin >> a[i], cnt[a[i]]++, mx = max(mx, a[i]);

  int ds[] = {0,0,0,0};
  int left = 4;
  int nd = 1;
  cnt[0] = 0;
  while(cnt[nd] - cnt[nd-1] == 4) {
    nd++;
  }
  int missing = left - (cnt[nd] - cnt[nd-1]);
  if(missing<=0) exit(-1);
  ds[0] = nd - 1;
  if(missing == 1) {
    while(cnt[nd] - cnt[nd-1] >= 2) {
      nd++;
    }
    ds[1] = nd - 1;
  } else {
    ds[1] = nd - 1;
  }

  while(cnt[nd] - cnt[nd-1] >= 0) {
      nd++;
  }
  ds[2] = nd - 1;

  debug(ds[0], ds[1], ds[2]);

  bool found = false;

  for(int i1=0;i1<3;i1++) {
    for(int i2=0;i2<3;i2++) {
      if(i1==i2) continue;
      int w = (ds[i1] + ds[i2] + 1);
      debug(i1,i2,ds[i1],ds[i2],w);
      if(t%w==0) {
        int h = t/w;
        int i3 = 6-i1-i2;
        if(valid(ds[i1], ds[i3], ds[i2], h - ds[i3])) {
          return 0;
        }
      }
    }
  }

  if(!found) cout << -1 << endl; //@sub

  return 0;
}


//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

#define prec setprecision

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T m, T n) {while(n) {T t=m%n; m=n;n=t;};return m;}
template<typename T> T exgcd(T a, T b, T& sa, T& ta) {T q, r, sb=0, tb=1, sc, tc; sa=1,ta=0; if(b) do q = a/b, r = a-q*b, a = b, b = r, sc = sa-q*sb, sa = sb, sb = sc, tc = ta-q*tb, ta = tb, tb = tc; while(b); return a; }
template<typename T> T mul_inv(T a, T b) { T t1 = a, t2 = b, t3; T v1 = 1, v2 = 0, v3; T x; while (t2 != 1) x = t1/t2, t3 = t1 - x*t2, v3 = v1 - x*v2, t1 = t2, t2 = t3, v1 = v2, v2 = v3; return (v2 + b) % b; }
template<typename T> T powmod(T a, T b, T MOD) {if (b < 0) return 0; T rv = 1; while (b) (b % 2) && (rv = (rv*a) % MOD), a = a*a%MOD, b /= 2; return rv;}
template<ll n> struct BitSize{enum{Size=BitSize<n/2>::Size+1};};
template<> struct BitSize<0>{enum{Size=1};};
template<> struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define BITMAX(n) (BitSize<n>::Size - 1)

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
	while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif

const int maxt = 1e6 + 7;
int t;
int a[maxt];
int b[maxt];
int cnt[maxt];
int mx = 0;

bool valid(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  if(m*n != t) return false;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      if(z>mx) return false;
      b[z]++;
    }
  }
  for(int i=1;i<=mx;i++) if(cnt[i]!=b[i]) return false;
  cout << m << " " << n << endl << x << " " << y << endl;
  return true;
}

void xtest(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      b[z]++;
    }
  }
  debug(b[1], b[2], b[3], b[4], b[5]);
}

int main(int argc, char *argv[]) {
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  xtest(10,10,1,2);
  xtest(10,10,2,3);
  xtest(10,10,3,4);
  cin >> t;
  memset(cnt, 0, sizeof(cnt));
  for(int i=1;i<=t;i++) cin >> a[i], cnt[a[i]]++, mx = max(mx, a[i]);

  int ds[] = {0,0,0,0};
  int left = 4;
  int nd = 1;
  cnt[0] = 0;
  while(cnt[nd] - cnt[nd-1] == 4) {
    nd++;
  }
  int missing = left - (cnt[nd] - cnt[nd-1]);
  if(missing<=0) exit(-1);
  ds[0] = nd - 1;
  if(missing == 1) {
    while(cnt[nd] - cnt[nd-1] == 3) {
      nd++;
    }
    ds[1] = nd - 1;
  } else {
    ds[1] = nd - 1;
  }

  if(t==1000000) printf("%d %d\n", ds[0], ds[1]);

  debug(ds[0], ds[1]);

  bool found = false;

  for(int i=0;i<=mx;i++) {
    if(min(i, mx-i) >= ds[1]) {
      if(valid(i, mx-i, ds[0], ds[1])) {
        found = true;
        break;
      }
    }
  }

  if(!found) {
    int h = ds[0] + ds[1] + 1;
    if(t%h==0) {
      int w = t / h - 1;
      for(int i=0, ie = min(w,mx);i<=ie;i++) {
        if(valid(i, ds[1], w-i, ds[0])) {
          found = true;
          break;
        }
      }
    }
  }

  if(!found) cout << -1 << endl; //@sub

  return 0;
}


//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

#define prec setprecision

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T m, T n) {while(n) {T t=m%n; m=n;n=t;};return m;}
template<typename T> T exgcd(T a, T b, T& sa, T& ta) {T q, r, sb=0, tb=1, sc, tc; sa=1,ta=0; if(b) do q = a/b, r = a-q*b, a = b, b = r, sc = sa-q*sb, sa = sb, sb = sc, tc = ta-q*tb, ta = tb, tb = tc; while(b); return a; }
template<typename T> T mul_inv(T a, T b) { T t1 = a, t2 = b, t3; T v1 = 1, v2 = 0, v3; T x; while (t2 != 1) x = t1/t2, t3 = t1 - x*t2, v3 = v1 - x*v2, t1 = t2, t2 = t3, v1 = v2, v2 = v3; return (v2 + b) % b; }
template<typename T> T powmod(T a, T b, T MOD) {if (b < 0) return 0; T rv = 1; while (b) (b % 2) && (rv = (rv*a) % MOD), a = a*a%MOD, b /= 2; return rv;}
template<ll n> struct BitSize{enum{Size=BitSize<n/2>::Size+1};};
template<> struct BitSize<0>{enum{Size=1};};
template<> struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define BITMAX(n) (BitSize<n>::Size - 1)

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
	while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif

const int maxt = 1e6 + 7;
int t;
int a[maxt];
int b[maxt];
int cnt[maxt];
int mx = 0;

bool valid(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  if(m*n != t) return false;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      if(z>mx) return false;
      b[z]++;
    }
  }
  for(int i=1;i<=mx;i++) if(cnt[i]!=b[i]) return false;
  cout << m << " " << n << endl << x << " " << y << endl;
  return true;
}

int main(int argc, char *argv[]) {
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  cin >> t;
  memset(cnt, 0, sizeof(cnt));
  for(int i=1;i<=t;i++) cin >> a[i], cnt[a[i]]++, mx = max(mx, a[i]);

  int ds[] = {0,0,0,0};
  int left = 4;
  int nd = 1;
  cnt[0] = 0;
  while(cnt[nd] - cnt[nd-1] == 4) {
    nd++;
  }
  int missing = left - (cnt[nd] - cnt[nd-1]);
  if(missing<=0) exit(-1);
  ds[0] = nd - 1;
  if(missing == 1) {
    while(cnt[nd] - cnt[nd-1] >= 2) {
      nd++;
    }
    ds[1] = nd - 1;
  } else {
    ds[1] = nd - 1;
  }

  while(cnt[nd] - cnt[nd-1] >= 0) {
      nd++;
  }
  ds[2] = nd - 1;

  debug(ds[0], ds[1], ds[2]);

  bool found = false;

  for(int i1=0;i1<3;i1++) {
    for(int i2=0;i2<3;i2++) {
      if(i1==i2) continue;
      int w = (ds[i1] + ds[i2] + 1);
      if(t%w==0) {
        int h = t/w;
        int i3 = 6-i1-i2;
        if(valid(ds[i1], ds[i3], ds[i2], h - ds[i3])) {
          return 0;
        }
      }
    }
  }

  if(!found) cout << -1 << endl; //@sub

  return 0;
}


//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

#define prec setprecision

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T m, T n) {while(n) {T t=m%n; m=n;n=t;};return m;}
template<typename T> T exgcd(T a, T b, T& sa, T& ta) {T q, r, sb=0, tb=1, sc, tc; sa=1,ta=0; if(b) do q = a/b, r = a-q*b, a = b, b = r, sc = sa-q*sb, sa = sb, sb = sc, tc = ta-q*tb, ta = tb, tb = tc; while(b); return a; }
template<typename T> T mul_inv(T a, T b) { T t1 = a, t2 = b, t3; T v1 = 1, v2 = 0, v3; T x; while (t2 != 1) x = t1/t2, t3 = t1 - x*t2, v3 = v1 - x*v2, t1 = t2, t2 = t3, v1 = v2, v2 = v3; return (v2 + b) % b; }
template<typename T> T powmod(T a, T b, T MOD) {if (b < 0) return 0; T rv = 1; while (b) (b % 2) && (rv = (rv*a) % MOD), a = a*a%MOD, b /= 2; return rv;}
template<ll n> struct BitSize{enum{Size=BitSize<n/2>::Size+1};};
template<> struct BitSize<0>{enum{Size=1};};
template<> struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define BITMAX(n) (BitSize<n>::Size - 1)

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
	while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif

const int maxt = 1e6 + 7;
int t;
int a[maxt];
int b[maxt];
int cnt[maxt];
int mx = 0;

bool valid(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  if(m*n != t) return false;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      if(z>mx) return false;
      b[z]++;
    }
  }
  for(int i=1;i<=mx;i++) if(cnt[i]!=b[i]) return false;
  cout << m << " " << n << endl << x << " " << y << endl;
  return true;
}

void xtest(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      b[z]++;
    }
  }
  debug(b[1], b[2], b[3], b[4], b[5]);
}

int main(int argc, char *argv[]) {
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  xtest(10,10,1,2);
  xtest(10,10,2,3);
  xtest(10,10,3,4);
  cin >> t;
  memset(cnt, 0, sizeof(cnt));
  for(int i=1;i<=t;i++) cin >> a[i], cnt[a[i]]++, mx = max(mx, a[i]);

  int ds[] = {0,0,0,0};
  int left = 4;
  int nd = 1;
  cnt[0] = 0;
  while(cnt[nd] - cnt[nd-1] == 4) {
    nd++;
  }
  int missing = left - (cnt[nd] - cnt[nd-1]);
  if(missing<=0) exit(-1);
  ds[0] = nd - 1;
  if(missing == 1) {
    while(cnt[nd] - cnt[nd-1] >= 2) {
      nd++;
    }
    ds[1] = nd - 1;
  } else {
    ds[1] = nd - 1;
  }

  if(t==1000000) {
    printf("%d %d %d\n", ds[0], ds[1], missing);
    for(int i=1;i<=30;i++) printf("[%d] %d ", i, cnt[i]);
    printf("\n");
  }

  debug(ds[0], ds[1]);

  bool found = false;

  for(int i=0;i<=mx;i++) {
    if(min(i, mx-i) >= ds[1]) {
      if(valid(i, mx-i, ds[0], ds[1])) {
        found = true;
        break;
      }
    }
  }

  if(!found) {
    int h = ds[0] + ds[1] + 1;
    if(t%h==0) {
      int w = t / h - 1;
      for(int i=0, ie = min(w,mx);i<=ie;i++) {
        if(valid(i, ds[1], w-i, ds[0])) {
          found = true;
          break;
        }
      }
    }
  }

  if(!found) cout << -1 << endl; //@sub

  return 0;
}


//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

#define prec setprecision

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T m, T n) {while(n) {T t=m%n; m=n;n=t;};return m;}
template<typename T> T exgcd(T a, T b, T& sa, T& ta) {T q, r, sb=0, tb=1, sc, tc; sa=1,ta=0; if(b) do q = a/b, r = a-q*b, a = b, b = r, sc = sa-q*sb, sa = sb, sb = sc, tc = ta-q*tb, ta = tb, tb = tc; while(b); return a; }
template<typename T> T mul_inv(T a, T b) { T t1 = a, t2 = b, t3; T v1 = 1, v2 = 0, v3; T x; while (t2 != 1) x = t1/t2, t3 = t1 - x*t2, v3 = v1 - x*v2, t1 = t2, t2 = t3, v1 = v2, v2 = v3; return (v2 + b) % b; }
template<typename T> T powmod(T a, T b, T MOD) {if (b < 0) return 0; T rv = 1; while (b) (b % 2) && (rv = (rv*a) % MOD), a = a*a%MOD, b /= 2; return rv;}
template<ll n> struct BitSize{enum{Size=BitSize<n/2>::Size+1};};
template<> struct BitSize<0>{enum{Size=1};};
template<> struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define BITMAX(n) (BitSize<n>::Size - 1)

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
	while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif

const int maxt = 1e6 + 7;
int t;
int a[maxt];
int b[maxt];
int cnt[maxt];
int mx = 0;

bool valid(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  if(m*n != t) return false;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      if(z>mx) return false;
      b[z]++;
    }
  }
  for(int i=1;i<=mx;i++) if(cnt[i]!=b[i]) return false;
  cout << m << " " << n << endl << x << " " << y << endl;
  return true;
}

void xtest(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      b[z]++;
    }
  }
  debug(b[1], b[2], b[3], b[4], b[5]);
}

int main(int argc, char *argv[]) {
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  xtest(10,10,1,2);
  xtest(10,10,2,3);
  xtest(10,10,3,4);
  cin >> t;
  memset(cnt, 0, sizeof(cnt));
  for(int i=1;i<=t;i++) cin >> a[i], cnt[a[i]]++, mx = max(mx, a[i]);

  int ds[] = {0,0,0,0};
  int left = 4;
  int nd = 1;
  cnt[0] = 0;
  while(cnt[nd] - cnt[nd-1] == left) {
    nd++;
  }
  int missing = left - (cnt[nd] - cnt[nd-1]);
  if(missing<=0) exit(-1);
  ds[0] = nd - 1;
  if(missing == 1) {
    while(cnt[nd] - cnt[nd-1] >= 1) {
      nd++;
    }
    ds[1] = nd - 1;
  } else {
    ds[1] = nd - 1;
  }

  debug(ds[0], ds[1]);

  bool found = false;

  for(int i=0;i<=mx;i++) {
    if(min(i, mx-i) >= ds[1]) {
      if(valid(i, mx-i, ds[0], ds[1])) {
        found = true;
        break;
      }
    }
  }

  if(!found) {
    int h = ds[0] + ds[1] + 1;
    if(t%h==0) {
      int w = t / h - 1;
      for(int i=0, ie = min(w,mx);i<=ie;i++) {
        if(valid(i, ds[1], w-i, ds[0])) {
          found = true;
          break;
        }
      }
    }
  }

  if(!found) cout << -1 << endl; //@sub

  return 0;
}


//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

#define prec setprecision

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T m, T n) {while(n) {T t=m%n; m=n;n=t;};return m;}
template<typename T> T exgcd(T a, T b, T& sa, T& ta) {T q, r, sb=0, tb=1, sc, tc; sa=1,ta=0; if(b) do q = a/b, r = a-q*b, a = b, b = r, sc = sa-q*sb, sa = sb, sb = sc, tc = ta-q*tb, ta = tb, tb = tc; while(b); return a; }
template<typename T> T mul_inv(T a, T b) { T t1 = a, t2 = b, t3; T v1 = 1, v2 = 0, v3; T x; while (t2 != 1) x = t1/t2, t3 = t1 - x*t2, v3 = v1 - x*v2, t1 = t2, t2 = t3, v1 = v2, v2 = v3; return (v2 + b) % b; }
template<typename T> T powmod(T a, T b, T MOD) {if (b < 0) return 0; T rv = 1; while (b) (b % 2) && (rv = (rv*a) % MOD), a = a*a%MOD, b /= 2; return rv;}
template<ll n> struct BitSize{enum{Size=BitSize<n/2>::Size+1};};
template<> struct BitSize<0>{enum{Size=1};};
template<> struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define BITMAX(n) (BitSize<n>::Size - 1)

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
	while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif

const int maxt = 1e6 + 7;
int t;
int a[maxt];
int b[maxt];
int cnt[maxt];
int mx = 0;

bool valid(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  if(m*n != t) return false;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      if(z>mx) return false;
      b[z]++;
    }
  }
  for(int i=1;i<=mx;i++) if(cnt[i]!=b[i]) return false;
  cout << m << " " << n << endl << x << " " << y << endl;
  return true;
}

void xtest(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      b[z]++;
    }
  }
  debug(b[1], b[2], b[3], b[4], b[5]);
}

int main(int argc, char *argv[]) {
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  xtest(10,10,1,2);
  xtest(10,10,2,3);
  xtest(10,10,3,4);
  cin >> t;
  memset(cnt, 0, sizeof(cnt));
  for(int i=1;i<=t;i++) cin >> a[i], cnt[a[i]]++, mx = max(mx, a[i]);

  int ds[] = {0,0,0,0};
  int left = 4;
  int nd = 1;
  cnt[0] = 0;
  while(cnt[nd] - cnt[nd-1] == 4) {
    nd++;
  }
  int missing = left - (cnt[nd] - cnt[nd-1]);
  if(missing<=0) exit(-1);
  ds[0] = nd - 1;
  if(missing == 1) {
    while(cnt[nd] - cnt[nd-1] >= 2) {
      nd++;
    }
    ds[1] = nd - 1;
  } else {
    ds[1] = nd - 1;
  }

  // if(t==1000000) {
  //   printf("%d %d %d\n", ds[0], ds[1], missing);
  //   for(int i=1;i<=30;i++) printf("[%d] %d ", i, cnt[i]);
  //   printf("\n");
  // }

  debug(ds[0], ds[1]);

  bool found = false;

  for(int i=0;i<=mx;i++) {
    if(min(i, mx-i) >= ds[1]) {
      if(valid(i, mx-i, ds[0], ds[1])) {
        found = true;
        break;
      }
    }
  }

  if(!found) {
    int h = ds[0] + ds[1] + 1;
    if(t%h==0) {
      int w = t / h - 1;
      for(int i=0, ie = min(w,mx);i<=ie;i++) {
        if(valid(i, ds[1], w-i, ds[0])) {
          found = true;
          break;
        }
      }
    }
  }

  if(!found) cout << -1 << endl; //@sub

  return 0;
}


//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

#define prec setprecision

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T m, T n) {while(n) {T t=m%n; m=n;n=t;};return m;}
template<typename T> T exgcd(T a, T b, T& sa, T& ta) {T q, r, sb=0, tb=1, sc, tc; sa=1,ta=0; if(b) do q = a/b, r = a-q*b, a = b, b = r, sc = sa-q*sb, sa = sb, sb = sc, tc = ta-q*tb, ta = tb, tb = tc; while(b); return a; }
template<typename T> T mul_inv(T a, T b) { T t1 = a, t2 = b, t3; T v1 = 1, v2 = 0, v3; T x; while (t2 != 1) x = t1/t2, t3 = t1 - x*t2, v3 = v1 - x*v2, t1 = t2, t2 = t3, v1 = v2, v2 = v3; return (v2 + b) % b; }
template<typename T> T powmod(T a, T b, T MOD) {if (b < 0) return 0; T rv = 1; while (b) (b % 2) && (rv = (rv*a) % MOD), a = a*a%MOD, b /= 2; return rv;}
template<ll n> struct BitSize{enum{Size=BitSize<n/2>::Size+1};};
template<> struct BitSize<0>{enum{Size=1};};
template<> struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define BITMAX(n) (BitSize<n>::Size - 1)

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
	while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif

const int maxt = 1e6 + 7;
int t;
int a[maxt];
int b[maxt];
int cnt[maxt];
int mx = 0;

bool valid(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  if(m*n != t) return false;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      if(z>mx) return false;
      b[z]++;
    }
  }
  for(int i=1;i<=mx;i++) if(cnt[i]!=b[i]) return false;
  cout << m << " " << n << endl << x << " " << y << endl;
  return true;
}

void xtest(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      b[z]++;
    }
  }
  debug(b[1], b[2], b[3], b[4], b[5]);
}

int main(int argc, char *argv[]) {
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  xtest(10,10,1,2);
  xtest(10,10,2,3);
  xtest(10,10,3,4);
  cin >> t;
  memset(cnt, 0, sizeof(cnt));
  for(int i=1;i<=t;i++) cin >> a[i], cnt[a[i]]++, mx = max(mx, a[i]);

  int ds[] = {0,0,0,0};
  int left = 4;
  int nd = 1;
  cnt[0] = 0;
  while(cnt[nd] - cnt[nd-1] == left) {
    nd++;
  }
  int missing = left - (cnt[nd] - cnt[nd-1]);
  ds[0] = nd - 1;
  if(missing == 1) {
    while(cnt[nd] - cnt[nd-1] >= 1) {
      nd++;
    }
    ds[1] = nd;
  } else {
    ds[1] = nd - 1;
  }

  debug(ds[0], ds[1]);

  bool found = false;

  for(int i=0;i<=mx;i++) {
    if(min(i, mx-i) >= ds[1]) {
      if(valid(i, mx-i, ds[0], ds[1])) {
        found = true;
        break;
      }
    }
  }

  if(!found) {
    int h = ds[0] + ds[1] + 1;
    if(t%h==0) {
      int w = t / h - 1;
      for(int i=0, ie = min(w,mx);i<=ie;i++) {
        if(valid(i, ds[1], w-i, ds[0])) {
          found = true;
          break;
        }
      }
    }
  }

  if(!found) cout << -1 << endl; //@sub

  return 0;
}


//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

#define prec setprecision

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T m, T n) {while(n) {T t=m%n; m=n;n=t;};return m;}
template<typename T> T exgcd(T a, T b, T& sa, T& ta) {T q, r, sb=0, tb=1, sc, tc; sa=1,ta=0; if(b) do q = a/b, r = a-q*b, a = b, b = r, sc = sa-q*sb, sa = sb, sb = sc, tc = ta-q*tb, ta = tb, tb = tc; while(b); return a; }
template<typename T> T mul_inv(T a, T b) { T t1 = a, t2 = b, t3; T v1 = 1, v2 = 0, v3; T x; while (t2 != 1) x = t1/t2, t3 = t1 - x*t2, v3 = v1 - x*v2, t1 = t2, t2 = t3, v1 = v2, v2 = v3; return (v2 + b) % b; }
template<typename T> T powmod(T a, T b, T MOD) {if (b < 0) return 0; T rv = 1; while (b) (b % 2) && (rv = (rv*a) % MOD), a = a*a%MOD, b /= 2; return rv;}
template<ll n> struct BitSize{enum{Size=BitSize<n/2>::Size+1};};
template<> struct BitSize<0>{enum{Size=1};};
template<> struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define BITMAX(n) (BitSize<n>::Size - 1)

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
	while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif

const int maxt = 1e6 + 7;
int t;
int a[maxt];
int b[maxt];
int cnt[maxt];
int mx = 0;

bool valid(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  if(m*n != t) return false;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      if(z>mx) return false;
      b[z]++;
    }
  }
  for(int i=1;i<=mx;i++) if(cnt[i]!=b[i]) return false;
  cout << m << " " << n << endl << x << " " << y << endl;
  return true;
}

int main(int argc, char *argv[]) {
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  cin >> t;
  memset(cnt, 0, sizeof(cnt));
  for(int i=1;i<=t;i++) cin >> a[i], cnt[a[i]]++, mx = max(mx, a[i]);

  int ds[] = {0,0,0,0};
  int left = 4;
  int nd = 1;
  cnt[0] = 0;
  while(cnt[nd] - cnt[nd-1] == 4) {
    nd++;
  }
  int missing = left - (cnt[nd] - cnt[nd-1]);
  if(missing<=0) exit(-1);
  ds[0] = nd - 1;
  if(missing == 1) {
    while(cnt[nd] - cnt[nd-1] >= 2) {
      nd++;
    }
    ds[1] = nd - 1;
  } else {
    ds[1] = nd - 1;
  }

  while(cnt[nd] - cnt[nd-1] >= 0) {
      nd++;
  }
  ds[2] = nd - 1;

  debug(ds[0], ds[1], ds[2]);

  if(t==1000000) {
    printf("%d %d %d\n", ds[0], ds[1], ds[2]);
  }

  bool found = false;

  for(int i1=0;i1<3;i1++) {
    for(int i2=0;i2<3;i2++) {
      if(i1==i2) continue;
      int w = (ds[i1] + ds[i2] + 1);
      debug(i1,i2,ds[i1],ds[i2],w);
      if((t%w)==0) {
        int h = t/w;
        int i3 = 3-i1-i2;
        debug(i1,i2,i3,ds[i1],ds[i2],w,h);
        if(valid(ds[i1], ds[i3], ds[i2], h - 1 - ds[i3])) {
          return 0;
        }
      }
    }
  }

  if(!found) cout << -1 << endl; //@sub

  return 0;
}


//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

#define prec setprecision

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T m, T n) {while(n) {T t=m%n; m=n;n=t;};return m;}
template<typename T> T exgcd(T a, T b, T& sa, T& ta) {T q, r, sb=0, tb=1, sc, tc; sa=1,ta=0; if(b) do q = a/b, r = a-q*b, a = b, b = r, sc = sa-q*sb, sa = sb, sb = sc, tc = ta-q*tb, ta = tb, tb = tc; while(b); return a; }
template<typename T> T mul_inv(T a, T b) { T t1 = a, t2 = b, t3; T v1 = 1, v2 = 0, v3; T x; while (t2 != 1) x = t1/t2, t3 = t1 - x*t2, v3 = v1 - x*v2, t1 = t2, t2 = t3, v1 = v2, v2 = v3; return (v2 + b) % b; }
template<typename T> T powmod(T a, T b, T MOD) {if (b < 0) return 0; T rv = 1; while (b) (b % 2) && (rv = (rv*a) % MOD), a = a*a%MOD, b /= 2; return rv;}
template<ll n> struct BitSize{enum{Size=BitSize<n/2>::Size+1};};
template<> struct BitSize<0>{enum{Size=1};};
template<> struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define BITMAX(n) (BitSize<n>::Size - 1)

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
	while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif

const int maxt = 1e6 + 7;
int t;
int a[maxt];
int b[maxt];
int cnt[maxt];
int mx = 0;

bool valid(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  if(m*n != t) return false;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      if(z>mx) return false;
      b[z]++;
    }
  }
  for(int i=1;i<=mx;i++) if(cnt[i]!=b[i]) return false;
  cout << m << " " << n << endl << x << " " << y << endl;
  return true;
}

int main(int argc, char *argv[]) {
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  cin >> t;
  memset(cnt, 0, sizeof(cnt));
  for(int i=1;i<=t;i++) cin >> a[i], cnt[a[i]]++, mx = max(mx, a[i]);

  int ds[] = {0,0,0,0};
  int left = 4;
  int nd = 1;
  cnt[0] = 0;
  while(cnt[nd] - cnt[nd-1] == 4) {
    nd++;
  }
  int missing = left - (cnt[nd] - cnt[nd-1]);
  if(missing<=0) exit(-1);
  ds[0] = nd - 1;
  if(missing == 1) {
    while(cnt[nd] - cnt[nd-1] >= 2) {
      nd++;
    }
    ds[1] = nd - 1;
  } else {
    ds[1] = nd - 1;
  }

  while(cnt[nd] - cnt[nd-1] >= 0) {
      nd++;
  }
  ds[2] = nd - 1;

  debug(ds[0], ds[1], ds[2]);

  bool found = false;

  for(int i1=0;i1<3;i1++) {
    for(int i2=0;i2<3;i2++) {
      if(i1==i2) continue;
      int w = (ds[i1] + ds[i2] + 1);
      debug(i1,i2,w);
      if(t%w==0) {
        int h = t/w;
        int i3 = 6-i1-i2;
        if(valid(ds[i1], ds[i3], ds[i2], h - ds[i3])) {
          return 0;
        }
      }
    }
  }

  if(!found) cout << -1 << endl; //@sub

  return 0;
}


//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

#define prec setprecision

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T m, T n) {while(n) {T t=m%n; m=n;n=t;};return m;}
template<typename T> T exgcd(T a, T b, T& sa, T& ta) {T q, r, sb=0, tb=1, sc, tc; sa=1,ta=0; if(b) do q = a/b, r = a-q*b, a = b, b = r, sc = sa-q*sb, sa = sb, sb = sc, tc = ta-q*tb, ta = tb, tb = tc; while(b); return a; }
template<typename T> T mul_inv(T a, T b) { T t1 = a, t2 = b, t3; T v1 = 1, v2 = 0, v3; T x; while (t2 != 1) x = t1/t2, t3 = t1 - x*t2, v3 = v1 - x*v2, t1 = t2, t2 = t3, v1 = v2, v2 = v3; return (v2 + b) % b; }
template<typename T> T powmod(T a, T b, T MOD) {if (b < 0) return 0; T rv = 1; while (b) (b % 2) && (rv = (rv*a) % MOD), a = a*a%MOD, b /= 2; return rv;}
template<ll n> struct BitSize{enum{Size=BitSize<n/2>::Size+1};};
template<> struct BitSize<0>{enum{Size=1};};
template<> struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define BITMAX(n) (BitSize<n>::Size - 1)

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
	while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif

const int maxt = 1e6 + 7;
int t;
int a[maxt];
int b[maxt];
int cnt[maxt];
int mx = 0;

bool valid(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      if(z>mx) return false;
      b[z]++;
    }
  }
  for(int i=1;i<=mx;i++) if(cnt[i]!=b[i]) return false;
  cout << m << " " << n << endl << x << " " << y << endl;
  return true;
}

int main(int argc, char *argv[]) {
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  cin >> t;
  memset(cnt, 0, sizeof(cnt));
  for(int i=1;i<=t;i++) cin >> a[i], cnt[a[i]]++, mx = max(mx, a[i]);

  int ds[] = {0,0,0,0};
  int last = 0, left = 4;
  int nd = 1;
  for(int i=0;i<2;) {
    while(cnt[nd] == last + left) {
      nd++;
      last += left;
    }
    int missing = last + left - cnt[nd];
    debug(i, missing, nd);
    for(int j=0;j<missing && i < 2;j++)
      ds[i++] = nd-1;
  }

  debug(ds[0], ds[1]);

  bool found = false;

  for(int i=0;i<=mx;i++) {
    if(min(i, mx-i) >= ds[1]) {
      if(valid(i, mx-i, ds[0], ds[1])) {
        found = true;
        break;
      }
    }
  }

  if(!found) {
    int r = ds[1] + 1;
    while(cnt[r+1] == cnt[r]) r++;
    debug(r);
    for(int i=0;i<2;i++) {
      if(valid(mx-ds[i], ds[i], r, ds[1-i])) {
          found = true;
          break;
      }
    }
  }

  if(!found) cout << -1 << endl; //@sub

  return 0;
}


//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

#define prec setprecision

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T m, T n) {while(n) {T t=m%n; m=n;n=t;};return m;}
template<typename T> T exgcd(T a, T b, T& sa, T& ta) {T q, r, sb=0, tb=1, sc, tc; sa=1,ta=0; if(b) do q = a/b, r = a-q*b, a = b, b = r, sc = sa-q*sb, sa = sb, sb = sc, tc = ta-q*tb, ta = tb, tb = tc; while(b); return a; }
template<typename T> T mul_inv(T a, T b) { T t1 = a, t2 = b, t3; T v1 = 1, v2 = 0, v3; T x; while (t2 != 1) x = t1/t2, t3 = t1 - x*t2, v3 = v1 - x*v2, t1 = t2, t2 = t3, v1 = v2, v2 = v3; return (v2 + b) % b; }
template<typename T> T powmod(T a, T b, T MOD) {if (b < 0) return 0; T rv = 1; while (b) (b % 2) && (rv = (rv*a) % MOD), a = a*a%MOD, b /= 2; return rv;}
template<ll n> struct BitSize{enum{Size=BitSize<n/2>::Size+1};};
template<> struct BitSize<0>{enum{Size=1};};
template<> struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define BITMAX(n) (BitSize<n>::Size - 1)

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
	while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif

const int maxt = 1e6 + 7;
int t;
int a[maxt];
int b[maxt];
int cnt[maxt];
int mx = 0;

bool valid(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  if(m*n != t) return false;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      if(z>mx) return false;
      b[z]++;
    }
  }
  for(int i=1;i<=mx;i++) if(cnt[i]!=b[i]) return false;
  cout << m << " " << n << endl << x << " " << y << endl;
  return true;
}

int main(int argc, char *argv[]) {
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  cin >> t;
  memset(cnt, 0, sizeof(cnt));
  for(int i=1;i<=t;i++) cin >> a[i], cnt[a[i]]++, mx = max(mx, a[i]);

  int ds[] = {0,0,0,0};
  int left = 4;
  int nd = 1;
  cnt[0] = 0;
  while(cnt[nd] - cnt[nd-1] == 4) {
    nd++;
  }
  int missing = left - (cnt[nd] - cnt[nd-1]);
  if(missing<=0) exit(-1);
  ds[0] = nd - 1;
  if(missing == 1) {
    while(cnt[nd] - cnt[nd-1] >= 2) {
      nd++;
    }
    ds[1] = nd - 1;
  } else {
    ds[1] = nd - 1;
  }

  while(cnt[nd] - cnt[nd-1] >= 0) {
      nd++;
  }
  ds[2] = nd - 1;

  debug(ds[0], ds[1], ds[2]);

  // if(t==1000000) {
  //   printf("%d %d %d\n", ds[0], ds[1], ds[2]);
  // }

  bool found = false;

  for(int i1=0;i1<3;i1++) {
    for(int i2=0;i2<3;i2++) {
      if(i1==i2) continue;
      int w = (ds[i1] + ds[i2] + 1);
      debug(i1,i2,ds[i1],ds[i2],w);
      if((t%w)==0) {
        int h = t/w;
        int i3 = 3-i1-i2;
        debug(i1,i2,i3,ds[i1],ds[i2],w,h);
        if(valid(ds[i1], ds[i3], ds[i2], h - 1 - ds[i3])) {
          return 0;
        }
      }
    }
  }

  ds[2]--;
  for(int i1=0;i1<3;i1++) {
    for(int i2=0;i2<3;i2++) {
      if(i1==i2) continue;
      int w = (ds[i1] + ds[i2] + 1);
      debug(i1,i2,ds[i1],ds[i2],w);
      if((t%w)==0) {
        int h = t/w;
        int i3 = 3-i1-i2;
        debug(i1,i2,i3,ds[i1],ds[i2],w,h);
        if(valid(ds[i1], ds[i3], ds[i2], h - 1 - ds[i3])) {
          return 0;
        }
      }
    }
  }

  if(!found) cout << -1 << endl; //@sub

  return 0;
}


//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

#define prec setprecision

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T m, T n) {while(n) {T t=m%n; m=n;n=t;};return m;}
template<typename T> T exgcd(T a, T b, T& sa, T& ta) {T q, r, sb=0, tb=1, sc, tc; sa=1,ta=0; if(b) do q = a/b, r = a-q*b, a = b, b = r, sc = sa-q*sb, sa = sb, sb = sc, tc = ta-q*tb, ta = tb, tb = tc; while(b); return a; }
template<typename T> T mul_inv(T a, T b) { T t1 = a, t2 = b, t3; T v1 = 1, v2 = 0, v3; T x; while (t2 != 1) x = t1/t2, t3 = t1 - x*t2, v3 = v1 - x*v2, t1 = t2, t2 = t3, v1 = v2, v2 = v3; return (v2 + b) % b; }
template<typename T> T powmod(T a, T b, T MOD) {if (b < 0) return 0; T rv = 1; while (b) (b % 2) && (rv = (rv*a) % MOD), a = a*a%MOD, b /= 2; return rv;}
template<ll n> struct BitSize{enum{Size=BitSize<n/2>::Size+1};};
template<> struct BitSize<0>{enum{Size=1};};
template<> struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define BITMAX(n) (BitSize<n>::Size - 1)

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
	while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif

const int maxt = 1e6 + 7;
int t;
int a[maxt];
int b[maxt];
int cnt[maxt];
int mx = 0;

bool valid(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  if(m*n != t) return false;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      if(z>mx) return false;
      b[z]++;
    }
  }
  for(int i=1;i<=mx;i++) if(cnt[i]!=b[i]) return false;
  cout << m << " " << n << endl << x << " " << y << endl;
  return true;
}

int main(int argc, char *argv[]) {
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  cin >> t;
  memset(cnt, 0, sizeof(cnt));
  for(int i=1;i<=t;i++) cin >> a[i], cnt[a[i]]++, mx = max(mx, a[i]);

  int ds[] = {0,0,0,0};
  int left = 4;
  int nd = 1;
  cnt[0] = 0;
  while(cnt[nd] - cnt[nd-1] == 4) {
    nd++;
  }
  int missing = left - (cnt[nd] - cnt[nd-1]);
  if(missing<=0) exit(-1);
  ds[0] = nd - 1;
  if(missing == 1) {
    while(cnt[nd] - cnt[nd-1] >= 2) {
      nd++;
    }
    ds[1] = nd - 1;
  } else {
    ds[1] = nd - 1;
  }

  while(cnt[nd] - cnt[nd-1] >= 0) {
      nd++;
  }
  ds[2] = nd - 1;

  debug(ds[0], ds[1], ds[2]);

  if(t==1000000) {
    printf("%d %d %d\n", ds[0], ds[1], ds[2]);
  }

  bool found = false;

  for(int i1=0;i1<3;i1++) {
    for(int i2=0;i2<3;i2++) {
      if(i1==i2) continue;
      int w = (ds[i1] + ds[i2] + 1);
      debug(i1,i2,ds[i1],ds[i2],w);
      if((t%w)==0) {
        int h = t/w;
        int i3 = 3-i1-i2;
        debug(i1,i2,i3,ds[i1],ds[i2],w,h);
        if(valid(ds[i1], ds[i3], ds[i2], h - 1 - ds[i3])) {
          return 0;
        }
      }
    }
  }

  ds[2]--;
  for(int i1=0;i1<3;i1++) {
    for(int i2=0;i2<3;i2++) {
      if(i1==i2) continue;
      int w = (ds[i1] + ds[i2] + 1);
      debug(i1,i2,ds[i1],ds[i2],w);
      if((t%w)==0) {
        int h = t/w;
        int i3 = 3-i1-i2;
        debug(i1,i2,i3,ds[i1],ds[i2],w,h);
        if(valid(ds[i1], ds[i3], ds[i2], h - 1 - ds[i3])) {
          return 0;
        }
      }
    }
  }

  if(!found) cout << -1 << endl; //@sub

  return 0;
}


//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

#define prec setprecision

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T m, T n) {while(n) {T t=m%n; m=n;n=t;};return m;}
template<typename T> T exgcd(T a, T b, T& sa, T& ta) {T q, r, sb=0, tb=1, sc, tc; sa=1,ta=0; if(b) do q = a/b, r = a-q*b, a = b, b = r, sc = sa-q*sb, sa = sb, sb = sc, tc = ta-q*tb, ta = tb, tb = tc; while(b); return a; }
template<typename T> T mul_inv(T a, T b) { T t1 = a, t2 = b, t3; T v1 = 1, v2 = 0, v3; T x; while (t2 != 1) x = t1/t2, t3 = t1 - x*t2, v3 = v1 - x*v2, t1 = t2, t2 = t3, v1 = v2, v2 = v3; return (v2 + b) % b; }
template<typename T> T powmod(T a, T b, T MOD) {if (b < 0) return 0; T rv = 1; while (b) (b % 2) && (rv = (rv*a) % MOD), a = a*a%MOD, b /= 2; return rv;}
template<ll n> struct BitSize{enum{Size=BitSize<n/2>::Size+1};};
template<> struct BitSize<0>{enum{Size=1};};
template<> struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define BITMAX(n) (BitSize<n>::Size - 1)

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
	while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif

const int maxt = 1e6 + 7;
int t;
int a[maxt];
int b[maxt];
int cnt[maxt];
int mx = 0;

bool valid(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  if(m*n != t) return false;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      if(z>mx) return false;
      b[z]++;
    }
  }
  for(int i=1;i<=mx;i++) if(cnt[i]!=b[i]) return false;
  cout << m << " " << n << endl << x << " " << y << endl;
  return true;
}

int main(int argc, char *argv[]) {
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  cin >> t;
  memset(cnt, 0, sizeof(cnt));
  for(int i=1;i<=t;i++) cin >> a[i], cnt[a[i]]++, mx = max(mx, a[i]);

  int ds[] = {0,0,0,0};
  int left = 4;
  int nd = 1;
  cnt[0] = 0;
  while(cnt[nd] - cnt[nd-1] == 4) {
    nd++;
  }
  int missing = left - (cnt[nd] - cnt[nd-1]);
  if(missing<=0) exit(-1);
  ds[0] = nd - 1;
  if(missing == 1) {
    while(cnt[nd] - cnt[nd-1] >= 2) {
      nd++;
    }
    ds[1] = nd - 1;
  } else {
    ds[1] = nd - 1;
  }

  while(cnt[nd] - cnt[nd-1] >= 0) {
      nd++;
  }
  ds[2] = nd - 1;

  debug(ds[0], ds[1], ds[2]);

  bool found = false;

  for(int i1=0;i1<3;i1++) {
    for(int i2=0;i2<3;i2++) {
      if(i1==i2) continue;
      int w = (ds[i1] + ds[i2] + 1);
      debug(i1,i2,ds[i1],ds[i2],w);
      if((t%w)==0) {
        int h = t/w;
        debug(i1,i2,ds[i1],ds[i2],w,h);
        int i3 = 6-i1-i2;
        if(valid(ds[i1], ds[i3], ds[i2], h - 1 - ds[i3])) {
          return 0;
        }
      }
    }
  }

  if(!found) cout << -1 << endl; //@sub

  return 0;
}


//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

#define prec setprecision

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T m, T n) {while(n) {T t=m%n; m=n;n=t;};return m;}
template<typename T> T exgcd(T a, T b, T& sa, T& ta) {T q, r, sb=0, tb=1, sc, tc; sa=1,ta=0; if(b) do q = a/b, r = a-q*b, a = b, b = r, sc = sa-q*sb, sa = sb, sb = sc, tc = ta-q*tb, ta = tb, tb = tc; while(b); return a; }
template<typename T> T mul_inv(T a, T b) { T t1 = a, t2 = b, t3; T v1 = 1, v2 = 0, v3; T x; while (t2 != 1) x = t1/t2, t3 = t1 - x*t2, v3 = v1 - x*v2, t1 = t2, t2 = t3, v1 = v2, v2 = v3; return (v2 + b) % b; }
template<typename T> T powmod(T a, T b, T MOD) {if (b < 0) return 0; T rv = 1; while (b) (b % 2) && (rv = (rv*a) % MOD), a = a*a%MOD, b /= 2; return rv;}
template<ll n> struct BitSize{enum{Size=BitSize<n/2>::Size+1};};
template<> struct BitSize<0>{enum{Size=1};};
template<> struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define BITMAX(n) (BitSize<n>::Size - 1)

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
	while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif

const int maxt = 1e6 + 7;
int t;
int a[maxt];
int b[maxt];
int cnt[maxt];
int mx = 0;

bool valid(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  if(m*n != t) return false;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      if(z>mx) return false;
      b[z]++;
    }
  }
  for(int i=1;i<=mx;i++) if(cnt[i]!=b[i]) return false;
  cout << m << " " << n << endl << x << " " << y << endl;
  return true;
}

void xtest(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      b[z]++;
    }
  }
  debug(b[1], b[2], b[3], b[4], b[5]);
}

int main(int argc, char *argv[]) {
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  xtest(10,10,1,2);
  xtest(10,10,2,3);
  xtest(10,10,3,4);
  cin >> t;
  memset(cnt, 0, sizeof(cnt));
  for(int i=1;i<=t;i++) cin >> a[i], cnt[a[i]]++, mx = max(mx, a[i]);

  int ds[] = {0,0,0,0};
  int left = 4;
  int nd = 1;
  cnt[0] = 0;
  while(cnt[nd] - cnt[nd-1] == 4) {
    nd++;
  }
  int missing = left - (cnt[nd] - cnt[nd-1]);
  if(missing<=0) exit(-1);
  ds[0] = nd - 1;
  if(missing == 1) {
    while(cnt[nd] - cnt[nd-1] == 3) {
      nd++;
    }
    ds[1] = nd - 1;
  } else {
    ds[1] = nd - 1;
  }

  if(t==1000000) {
    printf("%d %d %d\n", ds[0], ds[1], missing);
    for(int i=1;i<=30;i++) printf("[%d] %d ", i, cnt[i]);
    printf("\n");
  }

  debug(ds[0], ds[1]);

  bool found = false;

  for(int i=0;i<=mx;i++) {
    if(min(i, mx-i) >= ds[1]) {
      if(valid(i, mx-i, ds[0], ds[1])) {
        found = true;
        break;
      }
    }
  }

  if(!found) {
    int h = ds[0] + ds[1] + 1;
    if(t%h==0) {
      int w = t / h - 1;
      for(int i=0, ie = min(w,mx);i<=ie;i++) {
        if(valid(i, ds[1], w-i, ds[0])) {
          found = true;
          break;
        }
      }
    }
  }

  if(!found) cout << -1 << endl; //@sub

  return 0;
}


//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

#define prec setprecision

typedef long long ll;
typedef unsigned long long llu;
template<typename T> T gcd(T m, T n) {while(n) {T t=m%n; m=n;n=t;};return m;}
template<typename T> T exgcd(T a, T b, T& sa, T& ta) {T q, r, sb=0, tb=1, sc, tc; sa=1,ta=0; if(b) do q = a/b, r = a-q*b, a = b, b = r, sc = sa-q*sb, sa = sb, sb = sc, tc = ta-q*tb, ta = tb, tb = tc; while(b); return a; }
template<typename T> T mul_inv(T a, T b) { T t1 = a, t2 = b, t3; T v1 = 1, v2 = 0, v3; T x; while (t2 != 1) x = t1/t2, t3 = t1 - x*t2, v3 = v1 - x*v2, t1 = t2, t2 = t3, v1 = v2, v2 = v3; return (v2 + b) % b; }
template<typename T> T powmod(T a, T b, T MOD) {if (b < 0) return 0; T rv = 1; while (b) (b % 2) && (rv = (rv*a) % MOD), a = a*a%MOD, b /= 2; return rv;}
template<ll n> struct BitSize{enum{Size=BitSize<n/2>::Size+1};};
template<> struct BitSize<0>{enum{Size=1};};
template<> struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define BITMAX(n) (BitSize<n>::Size - 1)

#ifndef ONLINE_JUDGE
#define DEBUG 1
#else
#define DEBUG 0
#endif

template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
	while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif

const int maxt = 1e6 + 7;
int t;
int a[maxt];
int b[maxt];
int cnt[maxt];
int mx = 0;

bool valid(int dl, int dt, int dr, int db) {
  int m = dl + dr + 1;
  int n = dt + db + 1;
  int x = dr + 1, y = db + 1;
  if(m*n != t) return false;
  debug(m,n,x,y);
  memset(b, 0, sizeof(b));
  for(int j=1;j<=n;j++) {
    for(int i=1;i<=m;i++) {
      int z = abs(i-x)+abs(j-y);
      if(z>mx) return false;
      b[z]++;
    }
  }
  for(int i=1;i<=mx;i++) if(cnt[i]!=b[i]) return false;
  cout << m << " " << n << endl << x << " " << y << endl;
  return true;
}

int main(int argc, char *argv[]) {
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  cin >> t;
  memset(cnt, 0, sizeof(cnt));
  for(int i=1;i<=t;i++) cin >> a[i], cnt[a[i]]++, mx = max(mx, a[i]);

  int ds[] = {0,0,0,0};
  int left = 4;
  int nd = 1;
  cnt[0] = 0;
  while(cnt[nd] - cnt[nd-1] == 4) {
    nd++;
  }
  int missing = left - (cnt[nd] - cnt[nd-1]);
  if(missing<=0) exit(-1);
  ds[0] = nd - 1;
  if(missing == 1) {
    while(cnt[nd] - cnt[nd-1] >= 2) {
      nd++;
    }
    ds[1] = nd - 1;
  } else {
    ds[1] = nd - 1;
  }

  while(cnt[nd] - cnt[nd-1] >= 0) {
      nd++;
  }
  ds[2] = nd - 1;

  debug(ds[0], ds[1], ds[2]);

  bool found = false;

  for(int i1=0;i1<3;i1++) {
    for(int i2=0;i2<3;i2++) {
      if(i1==i2) continue;
      int w = (ds[i1] + ds[i2] + 1);
      debug(i1,i2,ds[i1],ds[i2],w);
      if((t%w)==0) {
        int h = t/w;
        int i3 = 3-i1-i2;
        debug(i1,i2,i3,ds[i1],ds[i2],w,h);
        if(valid(ds[i1], ds[i3], ds[i2], h - 1 - ds[i3])) {
          return 0;
        }
      }
    }
  }

  if(!found) cout << -1 << endl; //@sub

  return 0;
}


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

int fff[1000000+444];
int cnt[1000000+444];
int cnt2[1000000+444];
int **matrix;
bool **vis;
bool checked[1000000+444];
int xmove[]={0,-1,0,1};
int ymove[]={-1,0,1,0};
int main()
{
    // cout << "Hello World!" << endl;
    ios::sync_with_stdio(false);
    int b=-1,a=INT_MIN,x=INT_MIN,y;
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>fff[i];
        cnt[fff[i]]++;
        b=max(b,fff[i]);
    }

    bool possible=true;
    if(cnt[0]!=1){
        possible=false;
    }

    for(int i=1;i<=b;i++){
        if(cnt[i]<4*i){
            x=i;
            break;
        }
        else if(cnt[i]>4*i){
            possible=false;
            break;
        }
    }

    if(x==INT_MIN){
        possible=false;
    }

    int n,m;
    for(n=1;n*n<=t;n++){
        if(t%n==0){
            m=t/n;
            // possible=false;
            a=n+m-b-2;
            y=a-x+2;
            int dist_to_corner=max(max(max(x-1+y-1,(x-1)+(m-y)),n-x+y-1),n-x+m-y);
            if(x>0&&y>0&&n>0&&m>0&&a>0&&b>0&&x<=n&&y<=m&&dist_to_corner==b){
                matrix=new int* [n+5];
                vis=new bool* [n+5];
                for(int i=0;i<n+5;i++){
                    matrix[i]=new int[m+5];
                    vis[i]=new bool[m+5];
                }
                queue <pair <int,int> > q;
                vis[x][y]=true;
                matrix[x][y]=0;
                cnt2[0]++;
                q.push(make_pair(x,y));
                while(q.empty()==0){
                    pair <int,int> topp=q.front();
                    q.pop();
                    int xx,yy;
                    for(int i=0;i<4;i++){
                        xx=topp.first+xmove[i];
                        yy=topp.second+ymove[i];
                        if(xx>=1&&xx<=n&&yy>=1&&yy<=m){
                            if(vis[xx][yy]==false){
                                matrix[xx][yy]=matrix[topp.first][topp.second]+1;
                                vis[xx][yy]=true;
                                cnt2[matrix[topp.first][topp.second]+1]++;
                                q.push(make_pair(xx,yy));
                            }
                        }

                    }
                }
                for(int i=0;i<=b;i++){
                    if(cnt[i]!=cnt2[i]){
                        cout<<-1;
                        return 0;
                    }
                    //cout<<cnt[i]<<" : "<<cnt2[i]<<"\n";
                }

                cout<<"\n"<<n<<" "<<m<<"\n"<<x<<" "<<y<<"\n";

                return 0;
            }

        }
    }

    cout<<-1;
    return 0;
}


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

int fff[1000000+444];
int cnt[1000000+444];
int cnt2[1000000+444];
int **matrix;
bool **vis;
bool checked[1000000+444];
int xmove[]={0,-1,0,1};
int ymove[]={-1,0,1,0};
int main()
{
    // cout << "Hello World!" << endl;
    ios::sync_with_stdio(false);
    int b=-1,a=INT_MIN,x=INT_MIN,y;
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>fff[i];
        cnt[fff[i]]++;
        b=max(b,fff[i]);
    }

    bool possible=true;
    if(cnt[0]!=1){
        possible=false;
    }

    for(int i=1;i<=b;i++){
        if(cnt[i]<4*i){
            x=i;
            break;
        }
        else if(cnt[i]>4*i){
            possible=false;
            break;
        }
    }

    if(x==INT_MIN){
        possible=false;
    }

    int n,m;
    for(n=1;n*n<=t;n++){
        if(t%n==0){
            m=t/n;
            // possible=false;
            a=n+m-b-2;
            y=a-x+2;
            int dist_to_corner=max(max(max(x-1+y-1,(x-1)+(m-y)),n-x+y-1),n-x+m-y);
            if(x>0&&y>0&&n>0&&m>0&&a>0&&b>0&&x<=n&&y<=m&&dist_to_corner==b){
                matrix=new int* [n+5];
                vis=new bool* [n+5];
                for(int i=0;i<n+5;i++){
                    matrix[i]=new int[m+5];
                    vis[i]=new bool[m+5];
                }
                queue <pair <int,int> > q;
                vis[x][y]=true;
                matrix[x][y]=0;
                cnt2[0]++;
                q.push(make_pair(x,y));
                while(q.empty()==0){
                    pair <int,int> topp=q.front();
                    q.pop();
                    int xx,yy;
                    for(int i=0;i<4;i++){
                        xx=topp.first+xmove[i];
                        yy=topp.second+ymove[i];
                        if(xx>=1&&xx<=n&&yy>=1&&yy<=m){
                            if(vis[xx][yy]==false){
                                matrix[xx][yy]=matrix[topp.first][topp.second]+1;
                                vis[xx][yy]=true;
                                cnt2[matrix[topp.first][topp.second]+1]++;
                                q.push(make_pair(xx,yy));
                            }
                        }

                    }
                }
                for(int i=0;i<=b;i++){
                    if(cnt[i]!=cnt2[i]){
                        cout<<-1;
                        return 0;
                    }
                    // cout<<cnt[i]<<" : "<<cnt2[i]<<"\n";
                }

                cout<<"\n"<<n<<" "<<m<<"\n"<<x<<" "<<y<<"\n";

                return 0;
            }

        }
    }

    cout<<-1;
    return 0;
}


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

int fff[1000000+444];
int cnt[1000000+444];
int cnt2[1000000+444];
int **matrix;
bool **vis;
bool checked[1000000+444];
int xmove[]={0,-1,0,1};
int ymove[]={-1,0,1,0};
int main()
{
    // cout << "Hello World!" << endl;
    ios::sync_with_stdio(false);
    int b=-1,a=INT_MIN,x=INT_MIN,y;
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>fff[i];
        cnt[fff[i]]++;
        b=max(b,fff[i]);
    }

    bool possible=true;
    if(cnt[0]!=1){
        possible=false;
    }

    for(int i=1;i<=b;i++){
        if(cnt[i]<4*i){
            x=i;
            break;
        }
        else if(cnt[i]>4*i){
            possible=false;
            break;
        }
    }

    if(x==INT_MIN){
        possible=false;
    }

    int n,m;
    for(n=1;n*n<=t;n++){
        if(t%n==0){
            m=t/n;
            // possible=false;
            a=n+m-b-2;
            y=a-x+2;
            int dist_to_corner=max(max(max(x-1+y-1,(x-1)+(m-y)),n-x+y-1),n-x+m-y);
            if(x>0&&y>0&&n>0&&m>0&&a>0&&b>0&&x<=n&&y<=m){
                for(int i=1;i<=n;i++){
                    for(int j=1;j<=m;j++){
                        cnt2[abs(i-x)+abs(j-y)]++;
                    }
                }
                bool avoid=false;
                for(int i=0;i<=b;i++){
                    if(cnt[i]!=cnt2[i]){
                        avoid=true;
                        break;
                    }
                    cnt2[i]=0;
                    // cout<<cnt[i]<<" : "<<cnt2[i]<<"\n";
                }

                if(!avoid){
                cout<<"\n"<<n<<" "<<m<<"\n"<<x<<" "<<y<<"\n";
                return 0;
                }

            }

        }
    }

    cout<<-1;
    return 0;
}


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

int fff[1000000+444];
int cnt[1000000+444];
int cnt2[1000000+444];
int **matrix;
bool **vis;
bool checked[1000000+444];
int xmove[]={0,-1,0,1};
int ymove[]={-1,0,1,0};
int main()
{
    // cout << "Hello World!" << endl;
    ios::sync_with_stdio(false);
    int b=-1,a=INT_MIN,x=INT_MIN,y;
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>fff[i];
        cnt[fff[i]]++;
        b=max(b,fff[i]);
    }

    bool possible=true;
    if(cnt[0]!=1){
        possible=false;
    }

    for(int i=1;i<=b;i++){
        if(cnt[i]<4*i){
            x=i;
            break;
        }
        else if(cnt[i]>4*i){
            possible=false;
            break;
        }
    }

    if(x==INT_MIN){
        possible=false;
    }

    int n,m;
    for(n=1;n*n<=t;n++){
        if(t%n==0){
            m=t/n;
            // possible=false;
            a=n+m-b-2;
            y=a-x+2;
            int dist_to_corner=max(max(max(x-1+y-1,(x-1)+(m-y)),n-x+y-1),n-x+m-y);
            if(x>0&&y>0&&n>0&&m>0&&a>0&&b>0&&x<=n&&y<=m&&dist_to_corner==b){
                for(int i=1;i<=n;i++){
                    for(int j=1;j<=m;j++){
                        cnt2[abs(i-x)+abs(j-y)]++;
                    }
                }
                for(int i=0;i<=b;i++){
                    if(cnt[i]!=cnt2[i]){
                        continue;
                    }
                    cnt2[i]=0;
                    // cout<<cnt[i]<<" : "<<cnt2[i]<<"\n";
                }

                cout<<"\n"<<n<<" "<<m<<"\n"<<x<<" "<<y<<"\n";

                return 0;
            }

        }
    }

    cout<<-1;
    return 0;
}


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

int fff[1000000+444];
int cnt[1000000+444];
int cnt2[1000000+444];
int **matrix;
bool **vis;
bool checked[1000000+444];
int xmove[]={0,-1,0,1};
int ymove[]={-1,0,1,0};
int main()
{
    // cout << "Hello World!" << endl;
    ios::sync_with_stdio(false);
    int b=-1,a=INT_MIN,x=INT_MIN,y;
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>fff[i];
        cnt[fff[i]]++;
        b=max(b,fff[i]);
    }

    bool possible=true;
    if(cnt[0]!=1){
        possible=false;
    }

    for(int i=1;i<=b;i++){
        if(cnt[i]<4*i){
            x=i;
            break;
        }
        else if(cnt[i]>4*i){
            possible=false;
            break;
        }
    }

    if(x==INT_MIN){
        possible=false;
    }

    int n,m;
    for(n=1;n*n<=t;n++){
        if(t%n==0){
            m=t/n;
            // possible=false;
            a=n+m-b-2;
            y=a-x+2;
            int dist_to_corner=max(max(max(x-1+y-1,(x-1)+(m-y)),n-x+y-1),n-x+m-y);
            if(x>0&&y>0&&n>0&&m>0&&a>0&&b>0&&x<=n&&y<=m&&dist_to_corner==b){
                matrix=new int* [n+5];
                vis=new bool* [n+5];
                for(int i=0;i<n+5;i++){
                    matrix[i]=new int[m+5];
                    vis[i]=new bool[m+5];
                }
                queue <pair <int,int> > q;
                vis[x][y]=true;
                for(int i=0;i<=n;i++){
                    for(int j=0;j<=m;j++){
                        matrix[i][j]=0;
                        vis[i][j]=false;
                    }
                }
                matrix[x][y]=0;
                cnt2[0]++;
                q.push(make_pair(x,y));
                while(q.empty()==0){
                    pair <int,int> topp=q.front();
                    q.pop();
                    int xx,yy;
                    for(int i=0;i<4;i++){
                        xx=topp.first+xmove[i];
                        yy=topp.second+ymove[i];
                        if(xx>=1&&xx<=n&&yy>=1&&yy<=m){
                            if(vis[xx][yy]==false){
                                matrix[xx][yy]=matrix[topp.first][topp.second]+1;
                                vis[xx][yy]=true;
                                cnt2[matrix[topp.first][topp.second]+1]++;
                                q.push(make_pair(xx,yy));
                            }
                        }

                    }
                }
                for(int i=0;i<=b;i++){
                    if(cnt[i]!=cnt2[i]){
                        cout<<-1;
                        return 0;
                    }
                    // cout<<cnt[i]<<" : "<<cnt2[i]<<"\n";
                }

                cout<<"\n"<<n<<" "<<m<<"\n"<<x<<" "<<y<<"\n";

                return 0;
            }

        }
    }

    cout<<-1;
    return 0;
}


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

int fff[1000000+444];
int cnt[1000000+444];
int cnt2[1000000+444];
int **matrix;
bool **vis;
bool checked[1000000+444];
int xmove[]={0,-1,0,1};
int ymove[]={-1,0,1,0};
int main()
{
    // cout << "Hello World!" << endl;
    ios::sync_with_stdio(false);
    int b=-1,a=INT_MIN,x=INT_MIN,y;
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>fff[i];
        cnt[fff[i]]++;
        b=max(b,fff[i]);
    }

    bool possible=true;
    if(cnt[0]!=1){
        possible=false;
    }

    for(int i=1;i<=b;i++){
        if(cnt[i]<4*i){
            x=i;
            break;
        }
        else if(cnt[i]>4*i){
            possible=false;
            break;
        }
    }

    if(x==INT_MIN){
        possible=false;
    }

    int n,m;
    for(n=1;n*n<=t;n++){
        if(t%n==0){
            m=t/n;
            // possible=false;
            a=n+m-b-2;
            y=a-x+2;
            int dist_to_corner=max(max(max(x-1+y-1,(x-1)+(m-y)),n-x+y-1),n-x+m-y);
            if(x>0&&y>0&&n>0&&m>0&&a>0&&b>0&&x<=n&&y<=m){
                for(int i=1;i<=n;i++){
                    for(int j=1;j<=m;j++){
                        cnt2[abs(i-x)+abs(j-y)]++;
                    }
                }
                bool avoid=false;
                for(int i=0;i<=b;i++){
                    if(cnt[i]!=cnt2[i]){
                        // cout<<i<<" , "<<cnt[i]<<" ,, "<<cnt2[i]<<":::\n";
                        avoid=true;
                        break;
                    }
                    cnt2[i]=0;
                    // cout<<cnt[i]<<" : "<<cnt2[i]<<"\n";
                }
                cnt2[0]=0;
                for(int i=1;i<=b;i++){
                    cnt2[i]=0;
                }
                // cout<<"\n"<<n<<" "<<m<<"\n"<<x<<" "<<y<<"::\n";
                if(!avoid){
                cout<<"\n"<<n<<" "<<m<<"\n"<<x<<" "<<y<<"\n";
                return 0;
                }

            }

        }
    }

    cout<<-1;
    return 0;
}


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

int fff[1000000+444];
int cnt[1000000+444];
int cnt2[1000000+444];
int **matrix;
bool **vis;
bool checked[1000000+444];
int xmove[]={0,-1,0,1};
int ymove[]={-1,0,1,0};
int main()
{
    // cout << "Hello World!" << endl;
    ios::sync_with_stdio(false);
    int b=-1,a=INT_MIN,x=INT_MIN,y;
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>fff[i];
        cnt[fff[i]]++;
        b=max(b,fff[i]);
    }

    bool possible=true;
    if(cnt[0]!=1){
        possible=false;
    }
    if(t==1&&fff[0]==0){
        cout<<"1  1\n1 1";
        return 0;
    }

    for(int i=1;i<=b;i++){
        if(cnt[i]<4*i){
            x=i;
            break;
        }
        else if(cnt[i]>4*i){
            possible=false;
            break;
        }
    }

    if(x==INT_MIN){
        possible=false;
    }

    int n,m;
    for(n=1;n*n<=t;n++){
        if(t%n==0){
            m=t/n;
            // possible=false;
            a=n+m-b-2;
            y=a-x+2;
            int dist_to_corner=max(max(max(x-1+y-1,(x-1)+(m-y)),n-x+y-1),n-x+m-y);
            if(x>0&&y>0&&n>0&&m>0&&a>0&&b>0&&x<=n&&y<=m){
                for(int i=1;i<=n;i++){
                    for(int j=1;j<=m;j++){
                        cnt2[abs(i-x)+abs(j-y)]++;
                    }
                }
                bool avoid=false;
                for(int i=0;i<=b;i++){
                    if(cnt[i]!=cnt2[i]){
                        // cout<<i<<" , "<<cnt[i]<<" ,, "<<cnt2[i]<<":::\n";
                        avoid=true;
                        break;
                    }
                    cnt2[i]=0;
                    // cout<<cnt[i]<<" : "<<cnt2[i]<<"\n";
                }
                cnt2[0]=0;
                for(int i=1;i<=b;i++){
                    cnt2[i]=0;
                }
                // cout<<"\n"<<n<<" "<<m<<"\n"<<x<<" "<<y<<"::\n";
                if(!avoid){
                cout<<"\n"<<n<<" "<<m<<"\n"<<x<<" "<<y<<"\n";
                return 0;
                }

            }

        }
    }

    cout<<-1;
    return 0;
}


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

int fff[1000000+444];
int cnt[1000000+444];
int cnt2[1000000+444];
int **matrix;
bool **vis;
bool checked[1000000+444];
int xmove[]={0,-1,0,1};
int ymove[]={-1,0,1,0};
int main()
{
    // cout << "Hello World!" << endl;
    ios::sync_with_stdio(false);
    int b=-1,a=INT_MIN,x=INT_MIN,y;
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>fff[i];
        cnt[fff[i]]++;
        b=max(b,fff[i]);
    }

    bool possible=true;
    if(cnt[0]!=1){
        possible=false;
    }

    for(int i=1;i<=b;i++){
        if(cnt[i]<4*i){
            x=i;
            break;
        }
        else if(cnt[i]>4*i){
            possible=false;
            break;
        }
    }

    if(x==INT_MIN){
        possible=false;
    }

    int n,m;
    for(n=1;n*n<=t;n++){
        if(t%n==0){
            m=t/n;
            // possible=false;
            a=n+m-b-2;
            y=a-x+2;
            int dist_to_corner=max(max(max(x-1+y-1,(x-1)+(m-y)),n-x+y-1),n-x+m-y);
            if(x>0&&y>0&&n>0&&m>0&&a>0&&b>0&&x<=n&&y<=m&&dist_to_corner==b){
                matrix=new int* [n+5];
                vis=new bool* [n+5];
                for(int i=0;i<n+5;i++){
                    matrix[i]=new int[m+5];
                    vis[i]=new bool[m+5];
                }
                queue <pair <int,int> > q;
                vis[x][y]=true;
                matrix[x][y]=0;
                cnt2[0]++;
                q.push(make_pair(x,y));
                while(q.empty()==0){
                    pair <int,int> topp=q.front();
                    q.pop();
                    int xx,yy;
                    for(int i=0;i<4;i++){
                        xx=topp.first+xmove[i];
                        yy=topp.second+ymove[i];
                        if(xx>=1&&xx<=n&&yy>=1&&yy<=m){
                            if(vis[xx][yy]==false){
                                matrix[xx][yy]=matrix[topp.first][topp.second]+1;
                                vis[xx][yy]=true;
                                cnt2[matrix[topp.first][topp.second]+1]++;
                                q.push(make_pair(xx,yy));
                            }
                        }

                    }
                }
                for(int i=0;i<=b;i++){
                    if(cnt[i]!=cnt2[i]){
                        cout<<-1;
                        return 0;
                    }
                    cout<<cnt[i]<<" : "<<cnt2[i]<<"\n";
                }

                cout<<"\n"<<n<<" "<<m<<"\n"<<x<<" "<<y<<"\n";

                return 0;
            }

        }
    }

    cout<<-1;
    return 0;
}


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

int fff[1000000+444];
int cnt[1000000+444];
int cnt2[1000000+444];
int **matrix;
bool **vis;
bool checked[1000000+444];
int xmove[]={0,-1,0,1};
int ymove[]={-1,0,1,0};
int main()
{
    // cout << "Hello World!" << endl;
    ios::sync_with_stdio(false);
    int b=-1,a=INT_MIN,x=INT_MIN,y;
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>fff[i];
        cnt[fff[i]]++;
        b=max(b,fff[i]);
    }

    bool possible=true;
    if(cnt[0]!=1){
        possible=false;
    }

    for(int i=1;i<=b;i++){
        if(cnt[i]<4*i){
            x=i;
            break;
        }
        else if(cnt[i]>4*i){
            possible=false;
            break;
        }
    }

    if(x==INT_MIN){
        possible=false;
    }

    int n,m;
    for(n=1;n*n<=t;n++){
        if(t%n==0){
            m=t/n;
            // possible=false;
            a=n+m-b-2;
            y=a-x+2;
            int dist_to_corner=max(max(max(x-1+y-1,(x-1)+(m-y)),n-x+y-1),n-x+m-y);
            if(x>0&&y>0&&n>0&&m>0&&a>0&&b>0&&x<=n&&y<=m&&dist_to_corner==b){
                matrix=new int* [n+5];
                vis=new bool* [n+5];
                for(int i=0;i<n+5;i++){
                    matrix[i]=new int[m+5];
                    vis[i]=new bool[m+5];
                }
                queue <pair <int,int> > q;
                vis[x][y]=true;
                matrix[x][y]=0;
                cnt2[0]++;
                q.push(make_pair(x,y));
                while(q.empty()==0){
                    pair <int,int> topp=q.front();
                    q.pop();
                    int xx,yy;
                    for(int i=0;i<4;i++){
                        xx=topp.first+xmove[i];
                        yy=topp.second+ymove[i];
                        if(xx>=1&&xx<=n&&yy>=1&&yy<=m){
                            if(vis[xx][yy]==false){
                                matrix[xx][yy]=matrix[topp.first][topp.second]+1;
                                vis[xx][yy]=true;
                                cnt2[matrix[topp.first][topp.second]+1]++;
                                q.push(make_pair(xx,yy));
                            }
                        }

                    }
                }
                // for(int i=0;i<=b;i++){
                //     cout<<cnt[i]<<" : "<<cnt2[i]<<"\n";
                // }

                cout<<"\n"<<n<<" "<<m<<"\n"<<x<<" "<<y<<"\n";

                return 0;
            }

        }
    }

    cout<<-1;
    return 0;
}


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

int fff[1000000+444];
int cnt[1000000+444];
int cnt2[1000000+444];
int **matrix;
bool **vis;
bool checked[1000000+444];
int xmove[]={0,-1,0,1};
int ymove[]={-1,0,1,0};
int main()
{
    // cout << "Hello World!" << endl;
    ios::sync_with_stdio(false);
    int b=-1,a=INT_MIN,x=INT_MIN,y;
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>fff[i];
        cnt[fff[i]]++;
        b=max(b,fff[i]);
    }

    bool possible=true;
    if(cnt[0]!=1){
        possible=false;
    }

    for(int i=1;i<=b;i++){
        if(cnt[i]<4*i){
            x=i;
            break;
        }
        else if(cnt[i]>4*i){
            possible=false;
            break;
        }
    }

    if(x==INT_MIN){
        possible=false;
    }

    int n,m;
    for(n=1;n*n<=t;n++){
        if(t%n==0){
            m=t/n;
            // possible=false;
            a=n+m-b-2;
            y=a-x+2;
            int dist_to_corner=max(max(max(x-1+y-1,(x-1)+(m-y)),n-x+y-1),n-x+m-y);
            if(x>0&&y>0&&n>0&&m>0&&a>0&&b>0&&x<=n&&y<=m&&dist_to_corner==b){
                matrix=new int* [n+5];
                vis=new bool* [n+5];
                for(int i=0;i<n+5;i++){
                    matrix[i]=new int[m+5];
                    vis[i]=new bool[m+5];
                }
                queue <pair <int,int> > q;
                vis[x][y]=true;
                for(int i=0;i<=n;i++){
                    for(int j=0;j<=m;j++){
                        matrix[i][j]=0;
                        vis[i][j]=false;
                    }
                }
                vis[x][y]=true;
                matrix[x][y]=0;
                cnt2[0]++;
                q.push(make_pair(x,y));
                while(q.empty()==0){
                    pair <int,int> topp=q.front();
                    q.pop();
                    int xx,yy;
                    for(int i=0;i<4;i++){
                        xx=topp.first+xmove[i];
                        yy=topp.second+ymove[i];
                        if(xx>=1&&xx<=n&&yy>=1&&yy<=m){
                            if(vis[xx][yy]==false){
                                matrix[xx][yy]=matrix[topp.first][topp.second]+1;
                                vis[xx][yy]=true;
                                cnt2[matrix[topp.first][topp.second]+1]++;
//cout<<"("<<xx<<" , "<<yy<<")"<<" : "<<matrix[topp.first][topp.second]+1<<"\n";
                                q.push(make_pair(xx,yy));
                            }
                        }

                    }
                }
                for(int i=0;i<=b;i++){
                    if(cnt[i]!=cnt2[i]){
                        cout<<-1<<"\n";
                        return 0;
                    }
                    //cout<<i<<" : "<<cnt[i]<<" : "<<cnt2[i]<<"\n";
                }

                cout<<"\n"<<n<<" "<<m<<"\n"<<x<<" "<<y<<"\n";

                return 0;
            }

        }
    }

    cout<<-1;
    return 0;
}


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

int fff[1000000+444];
int cnt[1000000+444];
int cnt2[1000000+444];
int **matrix;
bool **vis;
bool checked[1000000+444];
int xmove[]={0,-1,0,1};
int ymove[]={-1,0,1,0};
int main()
{
    // cout << "Hello World!" << endl;
    ios::sync_with_stdio(false);
    int b=-1,a=INT_MIN,x=INT_MIN,y;
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>fff[i];
        cnt[fff[i]]++;
        b=max(b,fff[i]);
    }

    bool possible=true;
    if(cnt[0]!=1){
        possible=false;
    }
    if(t==1&&fff[0]==1){
        cout<<"1  1\n1 1";
        return 0;
    }

    for(int i=1;i<=b;i++){
        if(cnt[i]<4*i){
            x=i;
            break;
        }
        else if(cnt[i]>4*i){
            possible=false;
            break;
        }
    }

    if(x==INT_MIN){
        possible=false;
    }

    int n,m;
    for(n=1;n*n<=t;n++){
        if(t%n==0){
            m=t/n;
            // possible=false;
            a=n+m-b-2;
            y=a-x+2;
            int dist_to_corner=max(max(max(x-1+y-1,(x-1)+(m-y)),n-x+y-1),n-x+m-y);
            if(x>0&&y>0&&n>0&&m>0&&a>0&&b>0&&x<=n&&y<=m){
                for(int i=1;i<=n;i++){
                    for(int j=1;j<=m;j++){
                        cnt2[abs(i-x)+abs(j-y)]++;
                    }
                }
                bool avoid=false;
                for(int i=0;i<=b;i++){
                    if(cnt[i]!=cnt2[i]){
                        // cout<<i<<" , "<<cnt[i]<<" ,, "<<cnt2[i]<<":::\n";
                        avoid=true;
                        break;
                    }
                    cnt2[i]=0;
                    // cout<<cnt[i]<<" : "<<cnt2[i]<<"\n";
                }
                cnt2[0]=0;
                for(int i=1;i<=b;i++){
                    cnt2[i]=0;
                }
                // cout<<"\n"<<n<<" "<<m<<"\n"<<x<<" "<<y<<"::\n";
                if(!avoid){
                cout<<"\n"<<n<<" "<<m<<"\n"<<x<<" "<<y<<"\n";
                return 0;
                }

            }

        }
    }

    cout<<-1;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <time.h>

using namespace std;

int main() {
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n;
		cin >> n;
		int a = 1e9;
		int b = -1;
		for (int j = 0; j < n; j++) {
			int f, s;
			cin >> f >> s;
			b = max(b, f);
			a = min(s, a);
		}
		if (n != 1 && b > a) cout << b - a << endl;
		else cout << 0 << endl;
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <time.h>

using namespace std;

int main() {
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n;
		cin >> n;
		int a = 1e9;
		int b = -1;
		for (int j = 0; j < n; j++) {
			int f, s;
			cin >> f >> s;
			b = max(b, f);
			a = min(s, a);
		}
		if (n != 1) cout << b - a << endl;
		else cout << 0 << endl;
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <time.h>

using namespace std;

int main() {
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n;
		cin >> n;
		int a = 1e9;
		int b = -1;
		for (int j = 0; j < n; j++) {
			int f, s;
			cin >> f >> s;
			b = max(b, f);
			a = min(s, a);
		}
		if (n != 1) cout << b - a << endl;
		else cout << 0;
	}
}


// Let us start fresh
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fora(i,a,b) for(ll i=a;i<=b;i++)
#define rep(i,n) for(ll i=0;i<n;i++)
#define ford(i,a,b) for(ll i=a;i>=b;i--)
#define f first
#define s second
#define fix(f,n) std::fixed<<std::setprecision(n)<<f
#define pb push_back
#define pob pop_back
#define max(a,b) ((a>b)?a:b)
#define min(a,b) ((a>b)?b:a)
#define max3(a,b,c) ((a>b)?(a>c)?a:c:(b>c)?b:c)
#define min3(a,b,c) ((a<b)?(a<c)?a:c:(b<c)?b:c)
#define inarr(i,a,n,A) for(ll i=a;i<n;i++) { cin>>A[i]; }
#define MOD 1000000007
ll r,c;
bool valid(ll x,ll y){
    if(x>=0&&x<r&&y>=0&&y<c){
        return true;
    }
    else return false;
}

int main(){
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        vector<pair<ll,ll>> a;
        rep(i,n){
            ll ta,tb;
            cin>>ta>>tb;
            a.pb(make_pair(ta,tb));
        }
        ll l=0,r=10000000000;
        if(n==1){
            cout<<0<<endl;
            continue;
        }
        rep(i,n){
            l=max(l,a[i].first);
            r=min(r,a[i].second);
            //cout<<a[i].first<<" "<<a[i].second<<endl;
        }
        //cout<<l<<" "<<r<<endl;
        cout<<abs(r-l)<<endl;
    }
    return 0;
}


// Let us start fresh
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fora(i,a,b) for(ll i=a;i<=b;i++)
#define rep(i,n) for(ll i=0;i<n;i++)
#define ford(i,a,b) for(ll i=a;i>=b;i--)
#define f first
#define s second
#define fix(f,n) std::fixed<<std::setprecision(n)<<f
#define pb push_back
#define pob pop_back
#define max(a,b) ((a>b)?a:b)
#define min(a,b) ((a>b)?b:a)
#define max3(a,b,c) ((a>b)?(a>c)?a:c:(b>c)?b:c)
#define min3(a,b,c) ((a<b)?(a<c)?a:c:(b<c)?b:c)
#define inarr(i,a,n,A) for(ll i=a;i<n;i++) { cin>>A[i]; }
#define MOD 1000000007
ll r,c;
bool valid(ll x,ll y){
    if(x>=0&&x<r&&y>=0&&y<c){
        return true;
    }
    else return false;
}

int main(){
    ll t;
    cin>>t;
    while(t--){
        ll n;
        cin>>n;
        vector<pair<ll,ll>> a;
        rep(i,n){
            ll ta,tb;
            cin>>ta>>tb;
            a.pb(make_pair(ta,tb));
        }
        ll l=0,r=10000000000;
        if(n==1){
            cout<<0<<endl;
            continue;
        }
        rep(i,n){
            l=max(l,a[i].first);
            r=min(r,a[i].second);
            //cout<<a[i].first<<" "<<a[i].second<<endl;
        }
        //cout<<l<<" "<<r<<endl;
        cout<<max(0,l-r)<<endl;
    }
    return 0;
}


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

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

    while(t--)
    {
        long long n,a=0,b=0;
        cin>>n;

        long long x[n],y[n];

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

        }

        sort(x,x+n);
        sort(y,y+n);

        for(int i=0;i<n;i++)
        {
            a = y[0];
            b = x[i];

        }

        if(n == 1 )
        {
            cout<<"0"<<endl;
        }

        else
        {
            cout<<b-a<<endl;

        }

    }

    return 0;
}


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

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

    while(t--)
    {
        long long n,a=0,b=0;
        cin>>n;

        long long x[n],y[n];

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

        }

        sort(x,x+n);
        sort(y,y+n);

        for(int i=0;i<n;i++)
        {
            a = y[0];
            b = x[i];

        }

        if(n == 1 || b<a )
        {
            cout<<"0"<<endl;
        }

        else
        {
            cout<<b-a<<endl;

        }

    }

    return 0;
}


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

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

    while(t--)
    {
        long long n,x,y,a=0,b=0;
        cin>>n;

        map<long long, long long>mp;
        map<long long, long long>::iterator itr;

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

            mp.insert({x,y});

        }

        itr = mp.begin();
        a = itr->second;

        for(itr=mp.begin();itr!=mp.end();itr++)
        {
            b = itr->first;
        }

        if(mp.size() == 1 )
        {
            cout<<"0"<<endl;
        }

        else
        {
            cout<<b-a<<endl;

        }

    }

    return 0;
}


/*
inline bool cmp (const pii &a, const pii &b)
{
    if (a.first != b.first)
        return a.first > b.first;

    return a.second < b.second;
}
sort(a,a+n,cmp);
 */

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

int main()
{

int t;
cin>>t;
while(t--)
{
    int t1;
    cin>>t1;
    int mx,mn;
    cin>>mx>>mn;
    t1=t1-1;

    while(t1--)
    {
        int x,y;
        cin>>x>>y;

        mx=max(mx,x);
        mn=min(mn,y);
    }

    int ans=abs(mx-mn);
    cout<<ans<<endl;
}

  return 0;

}


/*
inline bool cmp (const pii &a, const pii &b)
{
    if (a.first != b.first)
        return a.first > b.first;

    return a.second < b.second;
}
sort(a,a+n,cmp);
 */

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

int main()
{

int t;
cin>>t;
while(t--)
{
    int t1;
    cin>>t1;
    int mx,mn;
    cin>>mx>>mn;
    t1=t1-1;
     if(t1==0)
    {
        cout<<0<<endl;
        continue;
    }

    while(t1--)
    {
        int x,y;
        cin>>x>>y;

        mx=max(mx,x);
        mn=min(mn,y);
    }

    if(mn>=mx)
    {
        cout<<0<<endl;
    }
    else
    {
    int ans=abs(mx-mn);
    cout<<ans<<endl;
    }

}

  return 0;

}


/*
inline bool cmp (const pii &a, const pii &b)
{
    if (a.first != b.first)
        return a.first > b.first;

    return a.second < b.second;
}
sort(a,a+n,cmp);
 */

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

int main()
{

int t;
cin>>t;
while(t--)
{
    int t1;
    cin>>t1;
    int mx,mn;
    cin>>mx>>mn;
    t1=t1-1;
     if(t1==0)
    {
        cout<<0<<endl;
        continue;
    }

    while(t1--)
    {
        int x,y;
        cin>>x>>y;

        mx=max(mx,x);
        mn=min(mn,y);
    }

    int ans=abs(mx-mn);
    cout<<ans<<endl;

}

  return 0;

}


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

int main()
{
    long long int t;
    cin >> t;
    while(t--)
    {
        long long int n,i;
        cin >> n;
        long long int a[n],b[n];
        for(i=0;i<n;i++)
            cin >> a[i] >> b[i];
        sort(a,a+n);
        sort(b,b+n);
        if(n==1)
            cout << "0" << endl;
        else
        cout << abs(a[n-1]-b[0]) << endl;
    }
    return 0;
}


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

#define endl "\n"

#define ll long long
#define MOD 1000000007

#define OJ \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout);

const double PI = 3.1415926535897932238460;

int main(){

    int t;
    cin>> t;
    while(t--)
    {

     long long a, b;
     long long c=0, d=1000000001;
     int n; cin>>n;
     if (n==1) {cin >>a >> b; cout << 0<<endl;}
     else
     {
        while(n--)
        {
            cin >>a>>b;
            if (a>c) c=a;
            if(b<d) d=b;

        }
        if (c-d<0)cout << 0<<endl;
        else
        cout <<c-d<<endl;
     }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <algorithm>
#include <set>
using namespace std;
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
//#define int long long
using pii = pair<int, int>;

ifstream in("in.in");

void loop() {
    int n;
    cin>>n;
    vector<pii> v(n);
    int l = -1000000000, r = 1000000000;
    for (pii &i : v) {
        cin>>i.first>>i.second;
        l = max(l, i.first);
        r = min(r, i.second);
    }

    if (r > l) {
        cout<<"0\n";
    } else {
    cout<<abs(l - r)<<"\n";
    }
}

int main() {
    int t;
    cin>>t;
    while (t--) {
        loop();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int t;
    cin>>t;
    for (int cyc = 0; cyc < t; cyc++) {
        int n;
        cin>>n;
        int rleftmost = 99999;
        int lrightmost = -99999;
        int l, r;
        for (int i = 0; i < n; i++) {
            cin>>l>>r;
            if (r < rleftmost) {
                rleftmost = r;
            }
            if (l > lrightmost) {
                lrightmost = l;
            }
        }
        if (rleftmost > lrightmost) {
            cout<<0<<endl;
        }
        else {
            cout<<lrightmost - rleftmost<<endl;
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <algorithm>
#include <set>
using namespace std;
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
//#define int long long
using pii = pair<int, int>;

ifstream in("in.in");

void loop() {
    int n;
    cin>>n;
    vector<pii> v(n);
    int l = -9999999, r = 99999999;
    for (pii &i : v) {
        cin>>i.first>>i.second;
        l = max(l, i.first);
        r = min(r, i.second);
    }

    if (r > l) {
        cout<<"0\n";
    } else {
    cout<<abs(l - r)<<"\n";
    }
}

int main() {
    int t;
    cin>>t;
    while (t--) {
        loop();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int t;
    cin>>t;
    for (int cyc = 0; cyc < t; cyc++) {
        int n;
        cin>>n;
        long int rleftmost = 99999999;
        long int lrightmost = -99999999;
        long int l, r;
        for (int i = 0; i < n; i++) {
            cin>>l>>r;
            if (r < rleftmost) {
                rleftmost = r;
            }
            if (l > lrightmost) {
                lrightmost = l;
            }
        }
        if (rleftmost > lrightmost) {
            cout<<0<<endl;
        }
        else {
            cout<<lrightmost - rleftmost<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int t;
    cin>>t;
    for (int cyc = 0; cyc < t; cyc++) {
        int n;
        cin>>n;
        long int rleftmost = 99999;
        long int lrightmost = -99999;
        long int l, r;
        for (int i = 0; i < n; i++) {
            cin>>l>>r;
            if (r < rleftmost) {
                rleftmost = r;
            }
            if (l > lrightmost) {
                lrightmost = l;
            }
        }
        if (rleftmost > lrightmost) {
            cout<<0<<endl;
        }
        else {
            cout<<lrightmost - rleftmost<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
	int n;
	cin>>n;
	int a[n],b[n];
	for(int i=0;i<n;i++)
	{
		cin>>a[i]>>b[i];
	}
	sort(a,a+n);
	sort(b,b+n);
	cout<<max(0,(a[n-1]-b[0]))<<endl;
}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
	int n,p=0;
	cin>>n;
	int a[n],b[n];
	for(int i=0;i<n;i++)
	{
		cin>>a[i]>>b[i];
	}
	sort(a,a+n);
	sort(b,b+n);
	p=(a[n-1]-b[0]);
	if(p>0)
	cout<<p<<endl;
	else
	cout<<"0"<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
	int n;
	cin>>n;
	int a[n+1],b[n+1];
	if(n>1)
	{
	for(int i=1;i<=n;i++)
	{
		cin>>a[i]>>b[i];
	}
	sort(a,a+n+1);
	sort(b,b+n+1);
	cout<<(b[2]-b[1])<<endl;
}
else
cout<<"0"<<endl;
}
	return 0;
}


#include<bits/stdc++.h>
//Phoenix
using namespace std;
#define ll long long int
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define pr pair<long int,long int>
#define mod 1000000007
int main(){
  ios_base::sync_with_stdio(false);
  int t;cin>>t;
  while(t--){
    int n;cin>>n;
    pr lr[n];
    for(int i=0;i<n;i++){
      long int x,y;
      cin>>x>>y;
      lr[i]={x,y};
    }
    //sort(lr,lr+n);
    long int l=lr[0].first;
    long int r=lr[0].second;
    for(int i=1;i<n;i++){
      r=min(r,lr[i].second);
      l=max(l,lr[i].first);
    }
    cout<<max(0l,(l-r))<<"\n";
  }
  return 0;
}


#include<bits/stdc++.h>
//Phoenix
using namespace std;
#define ll long long int
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define pr pair<long int,long int>
#define mod 1000000007
int main(){
  ios_base::sync_with_stdio(false);
  int t;cin>>t;
  while(t--){
    int n;cin>>n;
    pr lr[n];
    for(int i=0;i<n;i++){
      long int x,y;
      cin>>x>>y;
      lr[i]={x,y};
    }
    //sort(lr,lr+n);
    long int mn=lr[0].first;
    long int mx=lr[0].second;
    for(int i=1;i<n;i++){
      mx=min(mx,lr[i].second);
      mn=max(mn,lr[i].first);
    }
    cout<<max(0l,(mx-mn))<<"\n";
  }
  return 0;
}


#include<bits/stdc++.h>
//Phoenix
using namespace std;
#define ll long long int
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define pr pair<long int,long int>
#define mod 1000000007
int main(){
  ios_base::sync_with_stdio(false);
  int t;cin>>t;
  while(t--){
    int n;cin>>n;
    pr lr[n];
    for(int i=0;i<n;i++){
      long int x,y;
      cin>>x>>y;
      lr[i]={x,y};
    }
    //sort(lr,lr+n);
    long int mn=lr[0].first;
    long int mx=lr[0].second;
    for(int i=1;i<n;i++){
      mx=min(mx,lr[i].second);
      mn=max(mn,lr[i].first);
    }
    cout<<max(0l,abs(mx-mn))<<"\n";
  }
  return 0;
}


#include<bits/stdc++.h>
#define mod 1000000007
#define pb push_back
#define ll long long
#define mk make_pair
#define fi first
#define lb lower_bound
#define test ll int t;cin>>t;while(t--)
#define se second
#define pll pair<long long,long long>
#define loop(i,n) for( i=0;i<n;i++)
#define icc ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
long long power(long long a,long long b){
    long long ans=1;
    while(b>0){
        if(b&1){ans=(ans*a);}
                a=(a*a);
                b>>=1;
        }
    return ans;
}
int main(){
	icc
	test{
		ll n,k,d,i,l,r;
		cin>>n;
		for(i=0;i<n;i++){
		    cin>>l>>r;
		    if(i==0){
		        k=l;d=r;
		        continue;
		    }
		    if(l>=k&&l<=d){
		        k=l;
		    }
		    if(r<=d&&r>=k){
		        d=r;
		    }
		    else if(l>=d){
		        k=d;
		        d=l;
		    }
		    else if(r<=k){
		        d=k;
		        k=r;
		    }
		}
		cout<<abs(k-d)<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
#define mod 1000000007
#define pb push_back
#define ll long long
#define mk make_pair
#define fi first
#define lb lower_bound
#define test ll int t;cin>>t;while(t--)
#define se second
#define pll pair<long long,long long>
#define loop(i,n) for( i=0;i<n;i++)
#define icc ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
long long power(long long a,long long b){
    long long ans=1;
    while(b>0){
        if(b&1){ans=(ans*a);}
                a=(a*a);
                b>>=1;
        }
    return ans;
}
int main(){
	icc
	test{
		ll n,k=0,d=1e10,i,l,r;
		cin>>n;
		for(i=0;i<n;i++){
		    cin>>l>>r;
		    k=max(k,l);
		    d=min(d,r);
		}
		cout<<max((ll)0,k-d)<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define pi pair< ll , ll >
#define VL vector< ll >
#define VP vector< pair < ll,ll > >
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MX 1000005
#define rr return 0;
#define inf 99999999999
#define M 1000000007
#define frl(i,a,b) for(ll i=a;i<b;i++)
#define rfl(i,a,b) for(ll i=a;i>=b;i--)

using namespace std;

template < class T >
T maxx(T A,T B){ return (A>B)?A:B; }
template < class T >
T minn(T A,T B ){ return (A<B)?A:B; }

typedef long long int ll;

int main() {
    fastio
	ll tt,n,p,m,k,i,j,l,a,b,c;
	ll r,g;
    cin>>tt; //	tt=1;

	while(tt--)
	{
	    cin>>n;
	    vector<pair<ll,ll> >v;
	    for(i=0;i<n;i++)
	    {
	        cin>>a>>b;
	        v.push_back(make_pair(a,b));
	    }
	    sort(v.begin(),v.end());
	    ll mn=inf,mx=0;
	    for(i=0;i<v.size();i++)
	    {
	        mn=minn(v[i].second ,mn);

	    }
	    if(v.size()>1)
	    mx=v[v.size()-1].first-mn;

	    cout<<mx<<"\n";
	}

	return 0;
}