function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

// let p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $(), a = $(n), b = Array(n), c = Array(n).fill(0), x = 0, sum = 0
  for (let i = 29; i >= 0; i--) {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      for (let j = dau; j < cuoi; j++) {
        if (a[j] & (1 << i)) { s1++; b[--icuoi] = a[j]; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = a[j] };
      }
      // log(i, dau, cuoi, s, s1, idau, icuoi);
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      for (let j = idau; j < (cuoi + idau) / 2; j++) [b[j], b[cuoi - 1 - j + idau]] = [b[cuoi - 1 - j + idau], b[j]]
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    // log(a, b, c);
    [a, b] = [b, a];
  }

  log(sum, x);
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
// Array.prototype.rev = function (first, last) {
//   // for (let i = first; i < (first + last) / 2; i++) [this[i],this[last-]]
// }
Array.prototype.merge = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

// let p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $(), a = $(n), b = Array(n), c = Array(n).fill(0), x = 0, sum = 0, y = [].splice()
  for (let i = 29; i >= 0; i--) {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      for (let j = dau; j < cuoi; j++) {
        if (a[j] & (1 << i)) { s1++; b[--icuoi] = a[j]; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = a[j] };
      }
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      b.merge(idau, b.slice(idau, cuoi).reverse())
      // for (let j = idau; j < (cuoi + idau) / 2; j++) [b[j], b[cuoi - 1 - j + idau]] = [b[cuoi - 1 - j + idau], b[j]]
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    [a, b] = [b, a];
  }

  log(sum, x);
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
Array.prototype.rev = function (first, last) {
  for (let i = first; i < (first + last) / 2; i++) {
    [this[i], this[last - 1 - i + first]] = [this[last - 1 - i + first], this[i]]
  }
}
Array.prototype.set = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

// let p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $(), a = $(n), b = Array(n), c = Array(n).fill(0), x = 0, sum = 0
  For(29, 0, -1, i => {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      for (let j = dau; j < cuoi; j++) {
        if (a[j] & (1 << i)) { s1++; b[--icuoi] = a[j]; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = a[j] };
      }
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      b.rev(idau, cuoi);
      // b.merge(idau, b.slice(idau, cuoi).reverse())
      // for (let j = idau; j < (cuoi + idau) / 2; j++) [b[j], b[cuoi - 1 - j + idau]] = [b[cuoi - 1 - j + idau], b[j]]
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    [a, b] = [b, a];
  })

  log(sum, x);
}

function For(a, b, fn) {
  if (!fn) return For(0, a, b);
  let res;
  for (let i = a; i <= b; i++) {
    if ((res = fn(i)) != null) return res;
  }
}
function ForR(a, b, fn) {
  if (!fn) return ForR(a, 0, b);
  let res;
  for (let i = a; i >= b; i--) {
    if ((res = fn(i)) != null) return res;
  }
}

function Arr(a, b, fn, init) {
  if (typeof (b) == 'function') return Arr(0, a, b, fn);
  if (b < a) return [];
  let arr = Array(b - a + 1).fill(0);
  if (init) init(arr);
  for (let i = a; i <= b; i++) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
Array.prototype.rev = function (first, last) {
  for (let i = first; i < (first + last) / 2; i++) {
    [this[i], this[last - 1 - i + first]] = [this[last - 1 - i + first], this[i]]
  }
}
Array.prototype.set = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}
Array.prototype.for = function (a, b, fn) {
  let res
  for (let i = a; i <= b; i++) {
    if ((res = fn(this[i], i, this)) != null) return res;
  }
}
Array.prototype.forR = function (a, b, fn) {
  let res
  for (let i = a; i >= b; i--) {
    if ((res = fn(this[i], i, this)) != null) return res;
  }
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => gt[n] * rgt[k] * rgt[n - k] % MOD;
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

// let p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $(), a = $(n), b = Array(n), c = Array(n).fill(0), x = 0, sum = 0
  ForR(29, i => {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      if (dau >= n) break;
      cuoi = For(dau + 1, n - 1, i => c[i] != c[i - 1] ? i : null) || n;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      a.for(dau, cuoi - 1, v => {
        if (v & (1 << i)) { s1++; b[--icuoi] = v; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = v };
      })
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      b.rev(idau, cuoi);
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else sum += ss0;
    [a, b] = [b, a];
  })

  log(sum, x);
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
Array.prototype.rev = function (first, last) {
  for (let i = first; i < (first + last) / 2; i++) {
    [this[i], this[last - 1 - i + first]] = [this[last - 1 - i + first], this[i]]
  }
}
Array.prototype.merge = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

// let p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $(), a = Float64Array.from($(n)), b = a.slice(), c = a.slice().fill(0), x = 0, sum = 0
  For(29, 0, -1, i => {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      for (let j = dau; j < cuoi; j++) {
        if (a[j] & (1 << i)) { s1++; b[--icuoi] = a[j]; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = a[j] };
      }
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      // b.rev(idau, cuoi);
      b.subarray(idau, cuoi).reverse();
      // b.merge(idau, b.slice(idau, cuoi).reverse())
      // for (let j = idau; j < (cuoi + idau) / 2; j++) [b[j], b[cuoi - 1 - j + idau]] = [b[cuoi - 1 - j + idau], b[j]]
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    [a, b] = [b, a];
  })

  log(sum, x);
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
Array.prototype.rev = function (first, last) {
  for (let i = first; i < (first + last) / 2; i++) {
    [this[i], this[last - 1 - i + first]] = [this[last - 1 - i + first], this[i]]
  }
}
Array.prototype.set = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

// let p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $(), a = $(n), b = Array(n), c = Array(n).fill(0), x = 0, sum = 0
  For(29, 0, -1, i => {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      for (let j = dau; j < cuoi; j++) {
        if (a[j] & (1 << i)) { s1++; b[--icuoi] = a[j]; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = a[j] };
      }
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      // b.rev(idau, cuoi);
      b.set(idau, b.slice(idau, cuoi).reverse())
      // for (let j = idau; j < (cuoi + idau) / 2; j++) [b[j], b[cuoi - 1 - j + idau]] = [b[cuoi - 1 - j + idau], b[j]]
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    [a, b] = [b, a];
  })

  log(sum, x);
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
Array.prototype.rev = function (first, last) {
  for (let i = first; i < (first + last) / 2; i++) {
    [this[i], this[last - 1 - i + first]] = [this[last - 1 - i + first], this[i]]
  }
}
Array.prototype.merge = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

// let p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $(), a = Int32Array.from($(n)), b = a.slice(), c = a.slice().fill(0), x = 0, sum = 0
  For(29, 0, -1, i => {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      for (let j = dau; j < cuoi; j++) {
        if (a[j] & (1 << i)) { s1++; b[--icuoi] = a[j]; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = a[j] };
      }
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      // b.rev(idau, cuoi);
      b.subarray(idau, cuoi).reverse()
      // b.merge(idau, b.slice(idau, cuoi).reverse())
      // for (let j = idau; j < (cuoi + idau) / 2; j++) [b[j], b[cuoi - 1 - j + idau]] = [b[cuoi - 1 - j + idau], b[j]]
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    [a, b] = [b, a];
  })

  log(sum, x);
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
Array.prototype.rev = function (first, last) {
  for (let i = first; i < (first + last) / 2; i++) {
    [this[i], this[last - 1 - i + first]] = [this[last - 1 - i + first], this[i]]
  }
}
Array.prototype.set = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}
Array.prototype.for = function (a, b, fn) {
  for (let i = a; i <= b; i++) fn(this[i], i, this);
}
Array.prototype.forR = function (a, b, fn) {
  for (let i = a; i >= b; i--) fn(this[i], i, this);
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

// let p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $(), a = $(n), b = Array(n), c = Array(n).fill(0), x = 0, sum = 0
  For(29, 0, -1, i => {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      a.for(dau, cuoi - 1, v => {
        if (v & (1 << i)) { s1++; b[--icuoi] = v; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = v };
      })
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      b.rev(idau, cuoi);
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    [a, b] = [b, a];
  })

  log(sum, x);
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

// let p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $(), a = $(n), t = [a], x = 0, sum = 0
  for (let i = 29; i >= 0; i--) {
    let nt = [];
    let ss = 0, ss0 = 0, ss1 = 0;
    t.forEach(ta => {
      let nt0 = [], nt1 = []
      let s1 = 0, s = 0;
      ta.forEach(v => {
        if (v & (1 << i)) { s1++; nt1.push(v) }
        else { s += s1; nt0.push(v) };
      });
      ss0 += s;
      ss1 += s1 * (ta.length - s1) - s;
      ss += s1 * (ta.length - s1) - s - s;
      if (nt0.length) nt.push(nt0)
      if (nt1.length) nt.push(nt1)
    })
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    t = nt;
  }

  log(sum, x);
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
Array.prototype.rev = function (first, last) {
  for (let i = first; i < (first + last) / 2; i++) {
    [this[i], this[last - 1 - i + first]] = [this[last - 1 - i + first], this[i]]
  }
}
Array.prototype.merge = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

// let p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $(), a = $(n), b = Array(n), c = Array(n).fill(0), x = 0, sum = 0, y = [].splice()
  for (let i = 29; i >= 0; i--) {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      for (let j = dau; j < cuoi; j++) {
        if (a[j] & (1 << i)) { s1++; b[--icuoi] = a[j]; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = a[j] };
      }
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      b.rev(idau, cuoi);
      // b.merge(idau, b.slice(idau, cuoi).reverse())
      // for (let j = idau; j < (cuoi + idau) / 2; j++) [b[j], b[cuoi - 1 - j + idau]] = [b[cuoi - 1 - j + idau], b[j]]
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    [a, b] = [b, a];
  }

  log(sum, x);
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

// let p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $(), a = $(n), b = Array(n), c = Array(n).fill(0), x = 0, sum = 0
  for (let i = 3; i >= 0; i--) {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      for (let j = dau; j < cuoi; j++) {
        if (a[j] & (1 << i)) { s1++; b[--icuoi] = a[j]; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = a[j] };
      }
      // log(i, dau, cuoi, s, s1, idau, icuoi);
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      for (let j = idau; j < (cuoi + idau) / 2; j++) [b[j], b[cuoi - 1 - j + idau]] = [b[cuoi - 1 - j + idau], b[j]]
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    // log(a, b, c);
    [a, b] = [b, a];
  }

  log(sum, x);
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

// let p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $(), a = $(n), t = [a], x = 0, sum = 0
  for (let i = 29; i >= 0; i--) {
    let nt = [];
    let ss0 = 0, ss1 = 0, ss = 0;
    t.forEach(ta => {
      let nt0 = [], nt1 = []
      let s1 = 0, s = 0;
      ta.forEach(v => {
        if (v & (1 << i)) { s1++; nt1.push(v) }
        else { s += s1; nt0.push(v) };
      });
      ss0 += s;
      ss1 += s1 * (ta.length - s1) - s;
      ss += ss1 - ss0;
      if (nt0.length) nt.push(nt0)
      if (nt1.length) nt.push(nt1)
    })
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    t = nt;
  }

  log(sum, x);
}


const process = require('process');

let inp = "";
process.stdin.on('data', chunk => inp += chunk);
process.stdin.on('end', () => {
    inp = inp.split('\n');
    let ic = 0;
    for (let ntest = +inp[ic++]; ntest--; ) {
        let [a, b, n, m] = inp[ic++].split(' ').map(BigInt);
        if (n + m > a + b) {
            console.log("No");
            continue;
        }
        if (m > (a < b ? a : b)) {
            console.log("No");
            continue;
        }
        console.log("Yes");
    }
});

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = ''
let currentLine = 0

process.stdin.on('data', inputStdin => {
    inputString += inputStdin
})

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n')

    main()
})

function readLine() {
    return inputString[currentLine++]
}

const s2i = s => parseInt(s, 10)

const readLineOfBigInts = () => {
  const split = readLine().split(' ')
  const result = []
  for (let i = 0, l = split.length; i < l; i++) {
    result.push(BigInt(split[i]));
  }
  return result
}

function main() {
  let T = s2i(readLine())
  let out = ''

  while (T--) {
    const [a, b, n, m] = readLineOfBigInts()
    // a = vainilla
    // b = chocolate
    // n = tipo 1
    // m = tipo 2
    let v = a, c = b, t1 = n, t2 = m

    let result = 'Yes'

    if (v + c < t1 + t2) {
      // Más invitados que galletas
      result = 'No'
    } else if (v - c > t1) {
      // siempre v > c
      // t1 siempre come vainilla, t2 siempre come chocolate
      if (c < t2) {
        result = 'No'
      }
    } else if (c - v >= t1) {
      // siempre v <= c
      // t1 siempre come chocolate, t2 siempre come vainilla
      if (v < t2) {
        result = 'No'
      }
    } else {
      // t1 come galletas hasta que v == c
      if (v > c) {
        t1 -= v - c
        v = c
      } else {
        t1 -= c - v
        c = v
      }

      if (t2 > v && t1 < t2) {
        result = 'No'
      }
    }

    out += result + '\n'
  }

  console.log(out)
}

function f(a, b, n, m) {
   if (a >= m && b >= m) {
      return true;
   }
   return false;
}

function solve() {
   var abnm = read().split(' ').map(BigInt);
   var a = abnm[0];
   var b = abnm[1];
   var n = abnm[2];
   var m = abnm[3];
   if (f(a, b, n, m) && a + b >= n + m) {
      write('yes');
   } else {
      write('no');
   }
}

function init() {
   var T;
   T = 1;
   T = Number(read());
   for (var t = 0; t < T; t++) {
      solve();
   }
}

var isNode = typeof console !== 'undefined';
var MEMORY = [];
var MEMORY_INDEX = 0;
if (isNode) {
   var fs = require('fs');
   var path = require('path');

   var inTextName;
   var outTextName;
   if (process.argv.length === 5) {
      inTextName = process.argv[3];
      outTextName = process.argv[4];
   }

   if (inTextName) {
      fs.readFile(path.join(__dirname, inTextName), 'utf8', (err, data) => {
         MEMORY = data.split('\r\n');
         init();
      });
   } else {
      var RL = require('readline').createInterface({
         input : process.stdin,
         output : process.stdout
      });

      RL.on('line', (line) => {
         MEMORY.push(line);
      });

      RL.on('close', init);
   }
} else {
   init();
}

function write(value) {
   isNode ? console.log(value) : print(value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}

function f(a, b, n, m) {
   if (a >= m && b >= m) {
      return true;
   }
   if (a >= b && a - n <= b && b + 1n >= m) {
      return true;
   }
   return false;
}

function solve() {
   var abnm = read().split(' ').map(BigInt);
   var a = abnm[0];
   var b = abnm[1];
   var n = abnm[2];
   var m = abnm[3];
   if (f(a, b, n, m) && a + b >= n + m) {
      write('yes');
   } else {
      write('no');
   }
}

function init() {
   var T;
   T = 1;
   T = Number(read());
   for (var t = 0; t < T; t++) {
      solve();
   }
}

var isNode = typeof console !== 'undefined';
var MEMORY = [];
var MEMORY_INDEX = 0;
if (isNode) {
   var fs = require('fs');
   var path = require('path');

   var inTextName;
   var outTextName;
   if (process.argv.length === 5) {
      inTextName = process.argv[3];
      outTextName = process.argv[4];
   }

   if (inTextName) {
      fs.readFile(path.join(__dirname, inTextName), 'utf8', (err, data) => {
         MEMORY = data.split('\r\n');
         init();
      });
   } else {
      var RL = require('readline').createInterface({
         input : process.stdin,
         output : process.stdout
      });

      RL.on('line', (line) => {
         MEMORY.push(line);
      });

      RL.on('close', init);
   }
} else {
   init();
}

function write(value) {
   isNode ? console.log(value) : print(value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}


'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

// Make a Snippet for the code above this and then write your logic in main();

function main() {
    const x = readline();

    // 1 1 7
    // 6 9, 15, 18
    Array(Number(x)).fill(1).map((t, iii) => {
        // var х = parseInt(readline())
        // var [n, k] = readline().split(' ').map((x, iii) => {
        //     return parseInt(x)
        // })
        // var a = new Array(n + 1).fill(0)

        var [p, a, b, c] = readline().split(' ').map((x, iii) => {
            return BigInt(x)
        })
        var aa = (p + a - 1n) / a * a - p
        var bb = (p + b - 1n) / b * b - p
        var cc = (p + c - 1n) / c * c - p
        var min = aa < bb ? aa : bb
        min = cc < min ? cc : min
        return console.log(min.toString())
        // console.log(n, k)
        for (var i = 1; i <= n; i++) {
            count[a[i]] = Math.max(count[a[i]], i - last[a[i]])
            last[a[i]] = i
        }

        for (var i = 1; i <= n; i++) {
            var max = Math.max(count[i], n - last[i] + 1)
            for (var j = max; j <= n && ans[j] === -1; j++) {
                ans[j] = i
            }
        }

        // console.log(count)
        // console.log(a)
        console.log(ans.slice(1).join(' '))
        // console.log(map)
        // console.log(a)
    })
}

// let INPUT = `4
// 9 5 4 8
// 2 6 10 9
// 10 2 5 10
// 10 9 9 9
// `
// let INPUT_ARR = INPUT.split("\n");
// solve(INPUT_ARR.slice(1))

let i = "";
process.stdin.on("data", (c) => (i += c));
process.stdin.on("end", () => {
  const { EOL } = require("os");
  const lines = i.split(EOL); /*your input text, split by lines*/
  solve(lines.slice(1));
});

// const bigIntMax = (...args) => args.reduce((m, e) => (e > m ? e : m));
const bigIntMin = (...args) => args.reduce((m, e) => (e < m ? e : m));
// const bigIntCeil = (a) => {
//   if (a % BigInt(1) === BigInt(0)) return a;
//   return (a + BigInt(1)) % BigInt(1);
// };

function solve(lines) {
  lines.forEach((line) => {
    if (!line) return;
    const lineParsed = line.split(" ").map((e) => BigInt(e));
    const [min, a, b, c] = lineParsed;

    function calculateMin(swim) {
        if (min % swim === BigInt(0)) return BigInt(0);

      let top = (min / swim) * swim;

      while (top < min) {
        top += swim;
      }

      return top - min;
    }

    const result = bigIntMin(calculateMin(a), calculateMin(b), calculateMin(c));
    console.log(String(result));
  });
}

const bigIntMin = (...args) => args.reduce((m, e) => e < m ? e : m);
const processData = (lines) => {
    const n = +lines[0]
    for (let i=0; i<n; i++) {
        const [p, a, b, c] = lines[i+1].split(' ').map(x => BigInt(x))
        let min = bigIntMin(p%a === 0n ? 0 : a - p%a, p%b === 0n ? 0 : b - p%b, p%c === 0n? 0 : c - p%c)
        console.log(min.toString(10))
    }
}

let i = []
process.stdin.on('data', c => i.push(c))
process.stdin.on('end', () => {
    const {EOL} = require('os')
    const lines = i.join('').split(EOL) /*your input text, split by lines*/
    processData(lines)
})

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    let t = Number(readline());
    while(t--) {
        let [p, a, b, c] = readline().split(" ").map(num => BigInt(num));
        let [ra, rb, rc] = [a, b, c];

        if(a > p) ra -= p;
        else if(a < p) ra = div(p, ra) * ra - p;

        if(b > p) rb -= p;
        else if(b < p) rb = div(p, rb) * rb - p;

        if(c > p) rc -= p;
        else if(c < p) rc = div(p, rc) * rc - p;

        if(a === p || b === p || c === p) console.log(0);
        else console.log(String(min(ra, min(rb, rc))));
    }
}

function div(dividened, quotient) {
    if(dividened % quotient === 0n) return (dividened/quotient);
    return ((dividened/quotient) + 1n);
}

function min(num1, num2) {
    let ans;
    if(num1 === num2 || num1 < num2) return num1;
    if(num2 < num1) return num2;
}

// let INPUT = `4
// 9 5 4 8
// 2 6 10 9
// 10 2 5 10
// 10 9 9 9
// `
// let INPUT_ARR = INPUT.split("\n");
// solve(INPUT_ARR.slice(1))

let i = "";
process.stdin.on("data", (c) => (i += c));
process.stdin.on("end", () => {
  const { EOL } = require("os");
  const lines = i.split(EOL); /*your input text, split by lines*/
  solve(lines.slice(1));
});

// const bigIntMax = (...args) => args.reduce((m, e) => (e > m ? e : m));
// const bigIntMin = (...args) => args.reduce((m, e) => (e < m ? e : m));
// const bigIntCeil = (a) => {
//   if (a % BigInt(1) === BigInt(0)) return a;
//   return (a + BigInt(1)) % BigInt(1);
// };

function solve(lines) {
  lines.forEach((line) => {
    if (!line) return;
    const lineParsed = line.split(" ").map((e) => Number(e));
    const [min, a, b, c] = lineParsed;

    // console.log(">", [min, a, b, c])

    // if (min % a === Number(0) || min % b === Number(0) || min % c === Number(0)) {
    //   console.log(0);
    //   return;
    // }

    // if (min === Number(0)) {
    //   console.log(Math.min(a,b,c));
    //   return;
    // }

    function calculateMin(swim) {
      return Math.ceil(min / swim) * swim - min;
    }

    const result = Math.min(calculateMin(a), calculateMin(b), calculateMin(c));
    console.log(Number(result));
  });
}

// let INPUT = `4
// 9 5 4 8
// 2 6 10 9
// 10 2 5 10
// 10 9 9 9
// `
// let INPUT_ARR = INPUT.split("\n");
// solve(INPUT_ARR.slice(1))

let i = "";
process.stdin.on("data", (c) => (i += c));
process.stdin.on("end", () => {
  const { EOL } = require("os");
  const lines = i.split(EOL); /*your input text, split by lines*/
  solve(lines.slice(1));
});

const bigIntMax = (...args) => args.reduce((m, e) => e > m ? e : m);
const bigIntMin = (...args) => args.reduce((m, e) => e < m ? e : m);

function solve(lines) {
  lines.forEach((line) => {
    if (!line) return;
    const lineParsed = line.split(" ").map((e) => BigInt(e));
    const [min, a, b, c] = lineParsed;

    // console.log(">", [min, a, b, c])

    if (min % a === BigInt(0) || min % b === BigInt(0) || min % c === BigInt(0)) {
        console.log(0);
        return;
    }

    if (min === BigInt(0)) {
        console.log(Number(bigIntMin(a,b,c)))
        return;
    }

    function calculateMin(swim) {
        return (swim - (min % swim)) % swim;
    }

    const result = bigIntMin(calculateMin(a), calculateMin(b), calculateMin(c));
    console.log(Number(result));
  });
}

// let INPUT = `4
// 9 5 4 8
// 2 6 10 9
// 10 2 5 10
// 10 9 9 9
// `
// let INPUT_ARR = INPUT.split("\n");
// solve(INPUT_ARR.slice(1))

let i = "";
process.stdin.on("data", (c) => (i += c));
process.stdin.on("end", () => {
  const { EOL } = require("os");
  const lines = i.split(EOL); /*your input text, split by lines*/
  solve(lines.slice(1));
});

const bigIntMax = (...args) => args.reduce((m, e) => e > m ? e : m);
const bigIntMin = (...args) => args.reduce((m, e) => e < m ? e : m);

function solve(lines) {
  lines.forEach((line) => {
    if (!line) return;
    const lineParsed = line.split(" ").map((e) => BigInt(e));
    const [min, a, b, c] = lineParsed;

    // console.log(">", [min, a, b, c])

    if (min % a === 0 || min % b === 0 || min % c === 0) {
        console.log(0);
        return;
    }

    if (min === BigInt(0)) {
        console.log(Number(bigIntMin(a,b,c)))
        return;
    }

    function calculateMin(swim) {
        return (swim - (min % swim)) % swim;
    }

    const result = bigIntMin(calculateMin(a), calculateMin(b), calculateMin(c));
    console.log(Number(result));
  });
}

// let INPUT = `4
// 9 5 4 8
// 2 6 10 9
// 10 2 5 10
// 10 9 9 9
// `
// let INPUT_ARR = INPUT.split("\n");
// solve(INPUT_ARR.slice(1))

let i = "";
process.stdin.on("data", (c) => (i += c));
process.stdin.on("end", () => {
  const { EOL } = require("os");
  const lines = i.split(EOL); /*your input text, split by lines*/
  solve(lines.slice(1));
});

const bigIntMax = (...args) => args.reduce((m, e) => e > m ? e : m);
const bigIntMin = (...args) => args.reduce((m, e) => e < m ? e : m);

function solve(lines) {
  lines.forEach((line) => {
    if (!line) return;
    const lineParsed = line.split(" ").map((e) => BigInt(e));
    const [min, a, b, c] = lineParsed;

    // console.log(">", [min, a, b, c])

    if (min == a || min == b || min == c) {
        console.log(0);
        return;
    }

    if (min === BigInt(0)) {
        console.log(Number(bigIntMin(a,b,c)))
        return;
    }

    function calculateMin(swim) {
        return (swim - (min % swim)) % swim;
    }

    const result = bigIntMin(calculateMin(a), calculateMin(b), calculateMin(c));
    console.log(Number(result));
  });
}

// let INPUT = `4
// 9 5 4 8
// 2 6 10 9
// 10 2 5 10
// 10 9 9 9
// `
// let INPUT_ARR = INPUT.split("\n");
// solve(INPUT_ARR.slice(1))

let i = "";
process.stdin.on("data", (c) => (i += c));
process.stdin.on("end", () => {
  const { EOL } = require("os");
  const lines = i.split(EOL); /*your input text, split by lines*/
  solve(lines.slice(1));
});

const bigIntMax = (...args) => args.reduce((m, e) => e > m ? e : m);
const bigIntMin = (...args) => args.reduce((m, e) => e < m ? e : m);

function solve(lines) {
  lines.forEach((line) => {
    if (!line) return;
    const lineParsed = line.split(" ").map((e) => BigInt(e));
    const [min, a, b, c] = lineParsed;

    // console.log(">", [min, a, b, c])

    if (min == a || min == b || min == c) {
        console.log(0);
        return;
    }

    if (min === BigInt(0)) {
        console.log(bigIntMin(a,b,c))
        return;
    }

    function calculateMin(swim) {
        return (swim - (min % swim)) % swim;
    }

    const result = bigIntMin(calculateMin(a), calculateMin(b), calculateMin(c));
    console.log(Number(result));
  });
}

// let INPUT = `4
// 9 5 4 8
// 2 6 10 9
// 10 2 5 10
// 10 9 9 9
// `
// let INPUT_ARR = INPUT.split("\n");
// solve(INPUT_ARR.slice(1))
let i = "";
process.stdin.on("data", (c) => (i += c));
process.stdin.on("end", () => {
  const { EOL } = require("os");
  const lines = i.split(EOL); /*your input text, split by lines*/
  solve(lines.slice(1));
});

const bigIntMax = (...args) => args.reduce((m, e) => e > m ? e : m);
const bigIntMin = (...args) => args.reduce((m, e) => e < m ? e : m);

function solve(lines) {
  lines.forEach((line) => {
    if (!line) return;
    const lineParsed = line.split(" ").map((e) => BigInt(e));
    const [min, a, b, c] = lineParsed;

    // console.log(">", [min, a, b, c])

    if (min == a || min == b || min == c) {
        console.log(0);
        return;
    }

    if (min === 0) {
        console.log(bigIntMin(a,b,c))
        return;
    }

    function calculateMin(swim) {
        return (swim - (min % swim)) % swim;
    }

    const result = bigIntMin(calculateMin(a), calculateMin(b), calculateMin(c));
    console.log(Number(result));
  });
}

// let INPUT = `4
// 9 5 4 8
// 2 6 10 9
// 10 2 5 10
// 10 9 9 9
// `
// let INPUT_ARR = INPUT.split("\n");
// solve(INPUT_ARR.slice(1))
let i = "";
process.stdin.on("data", (c) => (i += c));
process.stdin.on("end", () => {
  const { EOL } = require("os");
  const lines = i.split(EOL); /*your input text, split by lines*/
  solve(lines.slice(1));
});

function solve(lines) {
  lines.forEach((line) => {
    if (!line) return;
    const lineParsed = line.split(" ").map((e) => Math.floor(e));
    const [min, a, b, c] = lineParsed;

    // console.log(">", [min, a, b, c])

    if (min == a || min == b || min == c) {
        console.log(0);
        return;
    }

    if (min === 0) {
        console.log(Math.min(a,b,c))
        return;
    }

    function calculateMin(swim) {
        return (swim - (min % swim)) % swim;
    }

    const result = Math.min(calculateMin(a), calculateMin(b), calculateMin(c));
    console.log(result);
  });
}

let i = "";
process.stdin.on("data", (c) => (i += c));
process.stdin.on("end", () => {
  const { EOL } = require("os");
  const lines = i.split(EOL); /*your input text, split by lines*/
  solve(lines.slice(1));
});

function solve(lines) {
  lines.forEach((line) => {
    if (!line) return;
    const lineParsed = line.split(" ").map((e) => Number(e));
    const [min, a, b, c] = lineParsed;

    // console.log(">", [min, a, b, c])

    if (min == a || min == b || min == c) {
        console.log(0);
        return;
    }

    const result = Math.min((a - (min % a)), (b - (min % b)), (c - (min % c)));
    console.log(result);
  });
}

// let INPUT = `4
// 9 5 4 8
// 2 6 10 9
// 10 2 5 10
// 10 9 9 9
// `
// let INPUT_ARR = INPUT.split("\n");
// solve(INPUT_ARR.slice(1))

let i = "";
process.stdin.on("data", (c) => (i += c));
process.stdin.on("end", () => {
  const { EOL } = require("os");
  const lines = i.split(EOL); /*your input text, split by lines*/
  solve(lines.slice(1));
});

function solve(lines) {
  lines.forEach((line) => {
    if (!line) return;
    const lineParsed = line.split(" ").map((e) => Number(e));
    const [min, a, b, c] = lineParsed;
    const result = Math.min((a - (min % a)) % a, (b - (min % b)) % b, (c - (min % c)) % c);
    console.log(result);
  });
}

// let INPUT = `4
// 9 5 4 8
// 2 6 10 9
// 10 2 5 10
// 10 9 9 9
// `
// let INPUT_ARR = INPUT.split("\n");
// solve(INPUT_ARR.slice(1))

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
    const {EOL} = require('os')
    const lines = i.split(EOL) /*your input text, split by lines*/
    console.log(lines);

    solve(lines.slice(1))
})

function solve(lines) {

  lines.forEach(line => {
      if (!line) return;
      const lineParsed = line.split(" ").map(e => Number(e));

  const [min, a, b, c] = lineParsed

  const result = Math.min((a - min % a) % a, (b - min % b) % b, (c - min % c) % c)
  console.log(result)
  })

}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    let t = Number(readline());
    while(t--) {
        let [p, a, b, c] = readline().split(" ").map(num => BigInt(num));
        let [ra, rb, rc] = [a, b, c];

        if(a > p) ra -= p;
        else if(a < p) ra = div(p, ra) * ra - p;

        if(b > p) rb -= p;
        else if(b < p) rb = div(p, rb) * rb - p;

        if(c > p) rc -= p;
        else if(c < p) rc = div(p, rc) * rc - p;

        if(a === p || b === p || c === p) console.log(0);
        else console.log(String(min(ra, min(rb, rc))));
    }
}

function div(dividened, quotient) {
    if(dividened % quotient === 0) return (dividened/quotient);
    return ((dividened/quotient) + 1n);
}

function min(num1, num2) {
    let ans;
    if(num1 === num2 || num1 < num2) return num1;
    if(num2 < num1) return num2;
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    let t = Number(readline());
    while(t--) {
        let [p, a, b, c] = readline().split(" ").map(num => BigInt(num));

        if(a > p) a -= p;
        else if(a < p) a = div(p, a) * a - p;

        if(b > p) b -= p;
        else if(b < p) b = div(p, b) * b - p;

        if(c > p) c -= p;
        else if(c < p) c = div(p, c) * c - p;

        if(a === p || b === p || c === p) console.log(0);
        else console.log(String(min(a, min(b, c))));
    }
}

function div(dividened, quotient) {
    if(dividened % quotient === 0) return (dividened/quotient);
    return ((dividened/quotient) + 1n);
}

function min(num1, num2) {
    let ans;
    if(num1 === num2 || num1 < num2) return num1;
    if(num2 < num1) return num2;
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    let t = Number(readline());
    while(t--) {
        let [p, a, b, c] = readline().split(" ").map(num => Number(num));

        if(a > p) a -= p;
        else if(a < p) a = Math.ceil(p / a) * a - p;

        if(b > p) b -= p;
        else if(b < p) b = Math.ceil(p / b) * b - p;

        if(c > p) c -= p;
        else if(c < p) c = Math.ceil(p / c) * c - p;

        if(a === p || b === p || c === p) console.log(0);
        else console.log(Math.min(a, b, c));
    }
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    const t = Number(readline());
    for(let i = 0; i < t; i++) {
        console.log(0);
    }
}

const bigIntMin = (...args) => args.reduce((m, e) => e < m ? e : m);
const processData = (lines) => {
    const n = +lines[0]
    for (let i=0; i<n; i++) {
        const [p, a, b, c] = lines[i+1].split(' ').map(x => BigInt(x))
        let min = bigIntMin(p%a === 0 ? 0 : a - p%a, p%b === 0 ? 0 : b - p%b, p%c === 0? 0 : c - p%c)
        console.log(min.toString(10))
    }
}

let i = []
process.stdin.on('data', c => i.push(c))
process.stdin.on('end', () => {
    const {EOL} = require('os')
    const lines = i.join('').split(EOL) /*your input text, split by lines*/
    processData(lines)
})

const processData = (lines) => {
    const n = +lines[0]
    for (let i=0; i<n; i++) {
        const [p, a, b, c] = lines[i+1].split(' ').map(x => +x)
        let min = Math.min(p%a === 0 ? 0 : a - p%a, p%b === 0 ? 0 : b - p%b, p%c === 0? 0 : c - p%c)
        console.log(min)
    }
}

let i = []
process.stdin.on('data', c => i.push(c))
process.stdin.on('end', () => {
    const {EOL} = require('os')
    const lines = i.join('').split(EOL) /*your input text, split by lines*/
    processData(lines)
})

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

// Make a Snippet for the code above this and then write your logic in main();

function main() {
    const x = readline();

    // 1 1 7
    // 6 9, 15, 18
    Array(Number(x)).fill(1).map((t, iii) => {
        // var х = parseInt(readline())
        // var [n, k] = readline().split(' ').map((x, iii) => {
        //     return parseInt(x)
        // })
        // var a = new Array(n + 1).fill(0)

        var [p, a, b, c] = readline().split(' ').map((x, iii) => {
            return parseInt(x)
        })
        var aa = Math.ceil(p/a)*a - p
        var bb = Math.ceil(p/b)*b - p
        var cc = Math.ceil(p/c)*c - p
        var min = Math.min(aa, bb)
         min = Math.min(min, cc)
        return console.log(min)
        // console.log(n, k)
        for (var i = 1; i <= n; i++) {
            count[a[i]] = Math.max(count[a[i]], i - last[a[i]])
            last[a[i]] = i
        }

        for (var i = 1; i <= n; i++) {
            var max = Math.max(count[i], n - last[i] + 1)
            for (var j = max; j <= n && ans[j] === -1; j++) {
                ans[j] = i
            }
        }

        // console.log(count)
        // console.log(a)
        console.log(ans.slice(1).join(' '))
        // console.log(map)
        // console.log(a)
    })
}