let index = 0;

require('readline').createInterface({
    input: process.stdin
}).on('line', (line) => {
    if(index > 0 && index % 2 == 0) {
        solve(line)
    }

    index++;
});

function solve(str) {
    const nums = str.split(' ').map(num => parseInt(num));
    nums.sort((a, b) => b - a)

    // console.log(nums)
    console.log(binarySearch(nums));
    // console.log(canAliceWin(2, [...nums]))
}

function binarySearch(nums) {
    let low = 0
    let hi = nums.length

    while(low !== hi && low !== hi - 1) {
        let mid = Math.floor((low + hi) / 2);
        // console.log(low, hi, mid )

        if(canAliceWin(mid, [...nums])) {
            low = mid
        } else {
            hi = mid - 1
        }
    }

    return canAliceWin(hi, [...nums]) ? hi : low;
}

function canAliceWin(k, nums) {
    let startLookingAtIndex = 0;
    let round = 1;
    let bobIndex = nums.length - 1;

    for(round = 1; round <= k ; round++) {
        const indexToDelete = nums.findIndex((num, index) => index >= startLookingAtIndex && num <= (k - round + 1));

        if(indexToDelete === -1) {
            return false;
        }

        // "delete"
        startLookingAtIndex = indexToDelete + 1;

        // run out of elements
        if(startLookingAtIndex === nums.length) {
            return round === k;
        }

        // bob turn
        nums[bobIndex] += (k - round + 1);
        bobIndex--;
    }

    return true;
}

const fs = require("fs");

const input = fs.readFileSync(0, "utf8").trim().split("\n");

const testCases = [];
let T;

function parseInput() {
  T = parseInt(input[0]);
  let index = 0;

  for (let i = 0; i < T; i++) {
    index++;
    const n = parseInt(input[index]);
    index++;
    const a = input[index].split(" ").map((item) => parseInt(item));
    const testCase = {
      n,
      a,
    };

    testCases.push(testCase);
  }
}

function solution(testCase) {
  const { n, a } = testCase;

  a.sort((b, c) => b - c);

  let result = Math.ceil(n / 2);
  let found = false;
  for (let k = 0; 2 * k - 1 <= n; k++) {
    for (let i = k - 1; i < k - 1 + k; i++) {
      if (a[i] > i - k + 2) {
        found = true;
        break;
      }
    }
    if (found) {
      result = k - 1;
      break;
    }
  }
  console.log(result);
}

parseInput();

testCases.forEach(solution);

'use strict';

var fs = require('fs');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);

function solve(read) {
  const [rn, rb, rns, re, rbs] = convertInput(read);
  const n = rn(),
        a = rns();
  a.sort((a, b) => b - a);
  const check = k => {
    let ans = 0;
    let t = k,
        i = 1;
    for (let l = 0, r = n - 1; l <= r; l++, r--) {
      let cur = k - i + 1;
      while (l <= r && a[l] > cur) l++;
      if (l <= r && a[l] <= cur) ans++;else break;
      i++;
    }
    return ans >= t;
  };
  let l = 0,
      r = n;
  while (l < r) {
    const m = l + r + 1 >> 1;
    if (check(m)) {
      l = m;
    } else {
      r = m - 1;
    }
  }
  return l;
}
function convertInput(read) {
  const rn = () => Number(read());
  const rb = () => BigInt(read());
  const rns = () => read().split(/\s+/).map(Number);
  const re = () => read().split(/\s+/).map(s => Number(s) - 1);
  const rbs = () => read().split(/\s+/).map(BigInt);
  return [rn, rb, rns, re, rbs];
}
function main(r) {
  try {
    let t = Number(r());
    let res = new Array(t);
    let limit = 0;
    for (let i = 0; i < t; i++) {
      var _solve;
      const s = (_solve = solve(r)) !== null && _solve !== void 0 ? _solve : '';
      if (t < limit) {
        process.stdout.write(s.toString());
        process.stdout.write('\n');
      } else {
        res[i] = s;
      }
    }
    if (t >= limit) {
      return res.join('\n');
    } else {
      return '';
    }
  } catch (error) {
    console.log(error);
  }
}

const inputs = fs__default["default"].readFileSync(0, 'utf-8').split(/\r?\n/);
void function () {
  const read = ((i = 0) => () => inputs[i++])();
  const output = main(read);
  process.stdout.write(output);
}();

"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
function iInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => parseInt(cur));
}
function BInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => BigInt(cur));
}
function string() {
  return readline().trim();
}
function stringArr() {
  return readline().trim().split("");
}

function stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
function iInpArr1() {
  return readline()
    .trim()
    .split("")
    .map((cur) => parseInt(cur));
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
const sqrt = (n) => {
  let low = 1n,
    high = n,
    ans;
  while (low <= high) {
    let mid = (low + high) / 2n;
    if (mid * mid <= n) {
      ans = mid;
      low = mid + 1n;
    } else high = mid - 1n;
  }
  return ans;
};
const solve = () => {
  let t = getValue();
  while (t--) {
    let n = getValue();
    let arr = iInpArr();
    if (n === 1) {
      console.log(arr[0]);
      continue;
    }
    arr.sort((a, b) => b - a);
    const helper = (pos, end) => {
      let st = 0;
      while (pos !== 0) {
        let flag = 0;
        for (let i = st; i <= end; i++) {
          if (arr[i] <= pos) {
            st = i + 1;
            flag = 1;
            break;
          }
        }
        if (flag === 0) return false;
        end--;
        pos--;
      }
      return true;
    };
    let ans = 0,
      st = Math.ceil(n / 2);
    for (let i = 0; i < n && st > 0; i++, st--) {
      if (helper(st, n - 1)) {
        ans = st;
        break;
      }
    }
    console.log(ans);
  }
};
solve();

'use strict';

var fs = require('fs');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);

function solve(read) {
  const [rn, rb, rns, re, rbs] = convertInput(read);
  const n = rn(),
        a = rns();
  a.sort((a, b) => b - a);
  const check = k => {
    let ans = 0;
    for (let l = 0, r = n - 1; l <= r; l++, r--) {
      let cur = k - l + 1;
      while (l < r && a[l] > cur) l++;
      if (l <= r && a[l] <= cur) ans++;else break;
    }
    return ans >= k;
  };
  let l = 0,
      r = n * 2;
  while (l < r) {
    const m = l + r + 1 >> 1;
    if (check(m)) {
      l = m;
    } else {
      r = m - 1;
    }
  }
  return l;
}
function convertInput(read) {
  const rn = () => Number(read());
  const rb = () => BigInt(read());
  const rns = () => read().split(/\s+/).map(Number);
  const re = () => read().split(/\s+/).map(s => Number(s) - 1);
  const rbs = () => read().split(/\s+/).map(BigInt);
  return [rn, rb, rns, re, rbs];
}
function main(r) {
  try {
    let t = Number(r());
    let res = new Array(t);
    let limit = 0;
    for (let i = 0; i < t; i++) {
      var _solve;
      const s = (_solve = solve(r)) !== null && _solve !== void 0 ? _solve : '';
      if (t < limit) {
        process.stdout.write(s.toString());
        process.stdout.write('\n');
      } else {
        res[i] = s;
      }
    }
    if (t >= limit) {
      return res.join('\n');
    } else {
      return '';
    }
  } catch (error) {
    console.log(error);
  }
}

const inputs = fs__default["default"].readFileSync(0, 'utf-8').split(/\r?\n/);
void function () {
  const read = ((i = 0) => () => inputs[i++])();
  const output = main(read);
  process.stdout.write(output);
}();

'use strict';

var fs = require('fs');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);

function solve(read) {
  const [rn, rb, rns, re, rbs] = convertInput(read);
  const n = rn(),
        a = rns();
  a.sort((a, b) => b - a);
  const check = k => {
    let ans = 0;
    for (let l = 0, r = n - 1; l <= r; l++, r--) {
      let cur = k - l + 1;
      while (l < r && a[l] > cur) l++;
      if (l <= r && a[l] <= cur) ans++;else break;
    }
    return ans >= k;
  };
  let l = 0,
      r = n;
  while (l < r) {
    const m = l + r + 1 >> 1;
    if (check(m)) {
      l = m;
    } else {
      r = m - 1;
    }
  }
  return l;
}
function convertInput(read) {
  const rn = () => Number(read());
  const rb = () => BigInt(read());
  const rns = () => read().split(/\s+/).map(Number);
  const re = () => read().split(/\s+/).map(s => Number(s) - 1);
  const rbs = () => read().split(/\s+/).map(BigInt);
  return [rn, rb, rns, re, rbs];
}
function main(r) {
  try {
    let t = Number(r());
    let res = new Array(t);
    let limit = 0;
    for (let i = 0; i < t; i++) {
      var _solve;
      const s = (_solve = solve(r)) !== null && _solve !== void 0 ? _solve : '';
      if (t < limit) {
        process.stdout.write(s.toString());
        process.stdout.write('\n');
      } else {
        res[i] = s;
      }
    }
    if (t >= limit) {
      return res.join('\n');
    } else {
      return '';
    }
  } catch (error) {
    console.log(error);
  }
}

const inputs = fs__default["default"].readFileSync(0, 'utf-8').split(/\r?\n/);
void function () {
  const read = ((i = 0) => () => inputs[i++])();
  const output = main(read);
  process.stdout.write(output);
}();

'use strict';

var fs = require('fs');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);

function solve(read) {
  const [rn, rb, rns, re, rbs] = convertInput(read);
  const n = rn(),
        a = rns();
  a.sort((a, b) => b - a);
  const check = k => {
    let ans = 0;
    for (let l = 0, r = n - 1; l <= r; l++, r--) {
      let cur = k - l + 1;
      while (l <= r && a[l] > cur) l++;
      if (l <= r && a[l] <= cur) ans++;
    }
    return ans >= k;
  };
  let l = 0,
      r = n;
  while (l < r) {
    const m = l + r + 1 >> 1;
    if (check(m)) {
      l = m;
    } else {
      r = m - 1;
    }
  }
  return l;
}
function convertInput(read) {
  const rn = () => Number(read());
  const rb = () => BigInt(read());
  const rns = () => read().split(/\s+/).map(Number);
  const re = () => read().split(/\s+/).map(s => Number(s) - 1);
  const rbs = () => read().split(/\s+/).map(BigInt);
  return [rn, rb, rns, re, rbs];
}
function main(r) {
  try {
    let t = Number(r());
    let res = new Array(t);
    let limit = 0;
    for (let i = 0; i < t; i++) {
      var _solve;
      const s = (_solve = solve(r)) !== null && _solve !== void 0 ? _solve : '';
      if (t < limit) {
        process.stdout.write(s.toString());
        process.stdout.write('\n');
      } else {
        res[i] = s;
      }
    }
    if (t >= limit) {
      return res.join('\n');
    } else {
      return '';
    }
  } catch (error) {
    console.log(error);
  }
}

const inputs = fs__default["default"].readFileSync(0, 'utf-8').split(/\r?\n/);
void function () {
  const read = ((i = 0) => () => inputs[i++])();
  const output = main(read);
  process.stdout.write(output);
}();

'use strict';

var fs = require('fs');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);

function solve(read) {
  const [rn, rb, rns, re, rbs] = convertInput(read);
  const n = rn(),
        a = rns();
  a.sort((a, b) => b - a);
  const check = k => {
    let ans = 0;
    for (let l = 0, r = n - 1; l <= r; l++, r--) {
      while (l <= r && a[l] > k) l++;
      if (l <= r && a[l] <= k) ans++;
    }
    return ans >= k;
  };
  let l = 0,
      r = n;
  while (l < r) {
    const m = l + r + 1 >> 1;
    if (check(m)) {
      l = m;
    } else {
      r = m - 1;
    }
  }
  return l;
}
function convertInput(read) {
  const rn = () => Number(read());
  const rb = () => BigInt(read());
  const rns = () => read().split(/\s+/).map(Number);
  const re = () => read().split(/\s+/).map(s => Number(s) - 1);
  const rbs = () => read().split(/\s+/).map(BigInt);
  return [rn, rb, rns, re, rbs];
}
function main(r) {
  try {
    let t = Number(r());
    let res = new Array(t);
    let limit = 0;
    for (let i = 0; i < t; i++) {
      var _solve;
      const s = (_solve = solve(r)) !== null && _solve !== void 0 ? _solve : '';
      if (t < limit) {
        process.stdout.write(s.toString());
        process.stdout.write('\n');
      } else {
        res[i] = s;
      }
    }
    if (t >= limit) {
      return res.join('\n');
    } else {
      return '';
    }
  } catch (error) {
    console.log(error);
  }
}

const inputs = fs__default["default"].readFileSync(0, 'utf-8').split(/\r?\n/);
void function () {
  const read = ((i = 0) => () => inputs[i++])();
  const output = main(read);
  process.stdout.write(output);
}();

let index = 0;

require('readline').createInterface({
    input: process.stdin
}).on('line', (line) => {
    if(index > 0 && index % 2 == 0) {
        solve(line)
    }

    index++;
});

function solve(str) {
    const nums = str.split(' ').map(num => parseInt(num));
    nums.sort((a, b) => b - a)

    // console.log(nums)
    console.log(binarySearch(nums));
    // console.log(canAliceWin(2, [...nums]))
}

function binarySearch(nums) {
    let low = 0
    let hi = nums.length

    while(low !== hi && low !== hi - 1) {
        let mid = Math.floor((low + hi) / 2);
        // console.log(low, hi, mid )

        if(canAliceWin(mid, [...nums])) {
            low = mid
        } else {
            hi = mid - 1
        }
    }

    return canAliceWin(hi, [...nums]) ? hi : low;
}

function canAliceWin(k, nums) {
    let startLookingAtIndex = 0;
    let round = 1;
    for(round = 1; round <= k ; round++) {
        const indexToDelete = nums.findIndex((num, index) => index >= startLookingAtIndex && num <= (k - round + 1));

        if(indexToDelete === -1) {
            return false;
        }

        // "delete"
        startLookingAtIndex = indexToDelete + 1;

        // run out of elements
        if(startLookingAtIndex === nums.length) {
            return round === k;
        }

        // bob turn
        const indexToModify = nums.findIndex((num, index) => index >= startLookingAtIndex && num <= (k - round));
        // console.log(indexToModify)
        if(indexToModify > -1) {
            nums[indexToModify] += (k - round + 1);
        }
    }

    return true;
}

let index = 0;

require('readline').createInterface({
    input: process.stdin
}).on('line', (line) => {
    if(index > 0 && index % 2 == 0) {
        solve(line)
    }

    index++;
});

function solve(str) {
    const nums = str.split(' ').map(num => parseInt(num));
    nums.sort((a, b) => b - a)

    // console.log(nums)
    console.log(binarySearch(nums));
    // console.log(canAliceWin(1, [...nums]))
}

function binarySearch(nums) {
    let low = 0
    let hi = nums.length

    while(low !== hi && low !== hi - 1) {
        let mid = Math.floor((low + hi) / 2);
        // console.log(low, hi, mid )

        if(canAliceWin(mid, [...nums])) {
            low = mid
        } else {
            hi = mid - 1
        }
    }

    return canAliceWin(hi, [...nums]) ? hi : low;
}

function canAliceWin(k, nums) {
    let startLookingAtIndex = 0;
    let round = 1;
    for(round = 1; round <= k ; round++) {
        const indexToDelete = nums.findIndex((num, index) => index >= startLookingAtIndex && num <= (k - round + 1));

        if(indexToDelete === -1) {
            return false;
        }

        // "delete"
        startLookingAtIndex = indexToDelete + 1;

        // run out of elements
        if(startLookingAtIndex === nums.length) {
            return round === k;
        }

        // bob turn
        nums[startLookingAtIndex] += (k - round + 1);
    }

    return true;
}

"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
function iInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => parseInt(cur));
}
function BInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => BigInt(cur));
}
function string() {
  return readline().trim();
}
function stringArr() {
  return readline().trim().split("");
}

function stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
function iInpArr1() {
  return readline()
    .trim()
    .split("")
    .map((cur) => parseInt(cur));
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
const sqrt = (n) => {
  let low = 1n,
    high = n,
    ans;
  while (low <= high) {
    let mid = (low + high) / 2n;
    if (mid * mid <= n) {
      ans = mid;
      low = mid + 1n;
    } else high = mid - 1n;
  }
  return ans;
};
const solve = () => {
  let t = getValue();
  while (t--) {
    let n = getValue();
    let arr = iInpArr();
    if (n === 1) {
      console.log(arr[0]);
      continue;
    }
    arr.sort((a, b) => b - a);
    const helper = (pos, st, end) => {
      while (pos !== 0 && st <= end) {
        if (arr[st] > pos) return false;
        pos--;
        st++;
        end--;
      }
      if (pos === 0) return true;
    };
    let ans = 0;
    for (let i = 0; i < n; i++) {
      if (helper(arr[i], i, n - 1)) {
        ans = arr[i];
        break;
      }
    }
    console.log(ans);
  }
};
solve();

"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
function iInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => parseInt(cur));
}
function BInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => BigInt(cur));
}
function string() {
  return readline().trim();
}
function stringArr() {
  return readline().trim().split("");
}

function stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
function iInpArr1() {
  return readline()
    .trim()
    .split("")
    .map((cur) => parseInt(cur));
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
const sqrt = (n) => {
  let low = 1n,
    high = n,
    ans;
  while (low <= high) {
    let mid = (low + high) / 2n;
    if (mid * mid <= n) {
      ans = mid;
      low = mid + 1n;
    } else high = mid - 1n;
  }
  return ans;
};
const solve = () => {
  let t = getValue();
  while (t--) {
    let n = getValue();
    let arr = iInpArr();
    if (n === 1) {
      console.log(arr[0]);
      continue;
    }
    arr.sort((a, b) => b - a);
    const helper = (pos, st, end) => {
      while (pos !== 0 && st <= end) {
        if (arr[st] > pos) return false;
        pos--;
        st++;
        end--;
      }
      if (pos === 0) return true;
    };
    let ans = 0;
    for (let i = 0; i < n; i++) {
      if (helper(arr[i], i, n - i - 1)) {
        ans = arr[i];
        break;
      }
    }
    console.log(ans);
  }
};
solve();

const fs = require("fs");

const input = fs.readFileSync(0, "utf8").trim().split("\n");

const testCases = [];
let T;

function parseInput() {
  T = parseInt(input[0]);
  let index = 0;

  for (let i = 0; i < T; i++) {
    index++;
    const n = parseInt(input[index]);
    index++;
    const a = input[index].split(" ").map((item) => parseInt(item));
    const testCase = {
      n,
      a,
    };

    testCases.push(testCase);
  }
}

function solution(testCase) {
  const { n, a } = testCase;

  let result = a.filter((v) => v == 1).length;
  if (result == n && n > 1) result = n - 1;
  console.log(result);
}

parseInput();

testCases.forEach(solution);

const fs = require("fs");

const input = fs.readFileSync(0, "utf8").trim().split("\n");

const testCases = [];
let T;

function parseInput() {
  T = parseInt(input[0]);
  let index = 0;

  for (let i = 0; i < T; i++) {
    index++;
    const n = parseInt(input[index]);
    index++;
    const a = input[index].split(" ").map((item) => parseInt(item));
    const testCase = {
      n,
      a,
    };

    testCases.push(testCase);
  }
}

function solution(testCase) {
  const { n, a } = testCase;

  let result = a.filter((v) => v == 1).length;
  console.log(result);
}

parseInput();

testCases.forEach(solution);


function solve() {
   const n = Number(read());
   let currentMap = [];
   read().split(' ').forEach((x, i) => {
      currentMap[Number(x)] = i + 1;
   });
   const ans = [];
   for (let i = n; i > 0; i--) {
      const nextMap = [];
      const index = currentMap[i];
      if (index === i) {
         ans.push(0);
         continue;
      }
      const diff = i - index;
      ans.push(index);
      for (let j = 1; j <= n; j++) {
         const jindex = currentMap[j];
         if (j > i) {
            nextMap[j] = jindex;
            continue;
         }
         if (jindex <= index) {
            nextMap[j] = jindex + diff;
            continue;
         }
         nextMap[j] = jindex - index;
      }
      currentMap = nextMap;
   }
   ans.reverse();
   write(ans.join(' '));
}

function sortF(a, b) {
   if (a < b) {
      return -1;
   }
   if (a > b) {
      return 1;
   }
   return 0;
}

function pDivCount(n) {
   let ans = 0;
   let i = 1;
   while(n > 1) {
      i++;
      while(n % i === 0) {
         ans++;
         n = Math.floor(n / i);
      }
   }
   return ans;
}

function gcd(a, b) {
   if (a === 0) {
      return b;
   }
   return gcd(b % a, a);
}

function lcm(a, b) {
   return Math.floor(a / gcd(a, b)) * b;
}

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

function readArray(mapF) {
   var res = read().split(' ');
   if (mapF) {
      res = res.map(mapF);
   }
   return res;
}

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

   var inTextName;
   var outTextName;
   if (process.argv.length === 5) {
      inTextName = process.argv[3];
      outTextName = process.argv[4];
   }
   MEMORY = fs.readFileSync(inTextName ? require('path').join(__dirname, inTextName) : 0, 'utf8').split('\r\n');
}
init();

function write(value) {
   isNode ? console.log('' + value) : print('' + value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}
function readAll() {
   if (isNode) {
      return MEMORY;
   }
   var res = [];
   var x;
   while (x = read()) {
      res.push(x);
   }
   return res;
}

"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
function iInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => parseInt(cur));
}
function BInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => BigInt(cur));
}
function string() {
  return readline().trim();
}
function stringArr() {
  return readline().trim().split("");
}

function stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
function iInpArr1() {
  return readline()
    .trim()
    .split("")
    .map((cur) => parseInt(cur));
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
const solve = () => {
  let t = getValue();
  while (t--) {
    let n = getValue();
    let arr = iInpArr();
    let obj = {};
    arr.map((i, j) => {
      obj[i] = j + 1;
      return i;
    });
    const help = (j, rotate) => {
      let k = n;
      for (let i = 0; i < rotate.length; i++, k--) j = (j + rotate[i]) % k;
      return j;
    };
    let rotate = [],
      res = new Array(n).fill(0);
    for (let i = n; i >= 1; i--) {
      let j = obj[i];
      j = help(j, rotate);
      if (j === i) {
        rotate.push(0);
        continue;
      } else {
        res[i - 1] = j;
        rotate.push(i - j);
      }
    }
    console.log(res.join(" "));
  }
};
solve();

'use strict';

var fs = require('fs');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);

function solve(n, data) {
  const map = [];

  for (let i = 0; i < data.length; i++) {
    map[data[i]] = i + 1;
  }

  let res = [];

  for (let i = map.length - 1; i > 0; i--) {
    let cur = map[i];

    if (i === cur) {
      res.push(0);
      continue;
    }

    res.push(cur);

    for (let j = 1; j < i; j++) {
      map[j] = (map[j] - cur + i) % i;
    }
  }

  return res.reverse();
}

// const inputs = fs.readFileSync(path.join(__dirname, './codeforces.test.txt'), 'utf-8').split('\n')

const inputs = fs__default["default"].readFileSync(0, 'utf-8').split('\n');
void function main() {
  const _ = Number(inputs[0]);

  for (let __ = 0; __ < _; __++) {
    const n = Number(inputs[__ * 2 + 1]);
    const data = inputs[__ * 2 + 2].trim().split(' ').map(Number);
    const res = solve(n, data);
    console.log(res.join(' '));
  }
}();

'use strict';
const readline = require('readline');
const util = require('util');
const rl = readline.createInterface({
    input: process.stdin,
});
let pos = 0;
const words = [];
rl.on('line', (line) => {
    words.push.apply(words, line.split(' ').filter(s => s != ''));
});
rl.on('close', main);
function check(b) {
    if (!b) {
        process.stderr.write(`${new Error().stack}\n`);
        process.exit(-1);
    }
}
function printf(...args) {
    process.stdout.write(util.format(...args));
}
function nextInt() {
    return Math.trunc(Number(words[pos++]));
}
function nextStr() {
    return words[pos++];
}
function af(l) {
    return Array.from({ length: l });
}
const INIT = -1;
let a;
// [l,r)
function reverse(l, r) {
    r--;
    while (l < r) {
        let t = a[l];
        a[l] = a[r];
        a[r] = t;
        l++;
        r--;
    }
}
// [l,r)
function rotate(r, n) {
    reverse(0, r);
    reverse(r, n);
    reverse(0, n);
}
function solve(n) {
    const ans = [];
    for (let i = n - 1; i >= 0; i--) {
        let want = i + 1;
        if (a[i] == want) {
            ans.push(0);
            continue;
        }
        let pos = a.findIndex((e) => e == want);
        rotate(pos + 1, i + 1);
        ans.push(pos + 1);
    }
    for (let i = n - 1; i >= 0; i--) {
        printf("%d ", ans[i]);
    }
    printf("\n");
}
function main() {
    let tc = nextInt();
    while (tc-- > 0) {
        let n = nextInt();
        a = af(n);
        for (let i = 0; i < n; i++) {
            a[i] = nextInt();
        }
        solve(n);
    }
}

'use strict'; /*jslint node:true, browser:true, es6:true*/
const print = console.log
const E = {};
const lines = []
if (!process.env.JEST_WORKER_ID){
  const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout })
  rl.on('line', line => {
      lines.push(line);
  });
  rl.on('close', main)
}
let rli = 0;
function readline() {
    return lines[rli++];
}

module.exports = E;

const ra = ()=>readline().split(' ').map(a=>+a)

const EPS = 1e-6;

const calc = (n, arr)=>{
  let ops = [];
  for (let i=n; i>=1; i--){
    let index = arr.indexOf(i);
    if (index==-1)
      throw 'what'
    ops.push((index+1)%i);
    //console.log('ARR ', {index, i}, arr)
    arr = [].concat(arr.slice(index+1), arr.slice(0, index));
    //console.log('=>', arr)
  }
  //console.log('ANS')
  return ops.reverse();
};

function main() {
    let T = +readline()
    while (T--){
      let [n] = ra();
      let arr = ra();
      print(calc(n, arr).join(' '));
    }
}

E.calc = calc;

const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})
const lines = []
rl.on('line', (input) => {
    lines.push(input);
})
rl.on('close', () => {
// (function() {
    // const lines = require('fs').readFileSync('test.in', 'utf8').split('\n')
    let l = 0;
    let t = +lines[l++]
    const output = []
    for (let i = 0; i < t; i++) {
        const n = +lines[l++]
        const arr = lines[l++].trim().split(' ').map(Number)
        output[i] = solve(n, arr)
    }
    console.log(output.join('\n'))
// })()
})

function solve(n, arr) {
    let cur = n
    const ans = Array(n)
    let p = n - 1
    while (cur >= 1) {
        let idx = arr.indexOf(cur) + 1
        if (idx <= 0) return -1
        ans[p--] = idx % cur

        const next = Array(cur)
        for (let i = 0; i < cur; i++) {
            next[(i - idx + cur) % cur] = arr[i]
        }
    // console.log(next)
        arr = next
        cur--
    }
    return ans.join(' ')
}


const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;
let l, p, q;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    l = +d;
    return;
  }

  if (c === 1) {
    c++;
    p = +d;
    return;
  }

  q = +d;

  c++;
});

rl.on('close', () => {
  console.log((l * (p / (p + q))).toFixed(4));
});

/******/ (function(modules) { // webpackBootstrap
/******/ 	// The module cache
/******/ 	var installedModules = {};

/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {

/******/ 		// Check if module is in cache
/******/ 		if(installedModules[moduleId])
/******/ 			return installedModules[moduleId].exports;

/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = installedModules[moduleId] = {
/******/ 			exports: {},
/******/ 			id: moduleId,
/******/ 			loaded: false
/******/ 		};

/******/ 		// Execute the module function
/******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/ 		// Flag the module as loaded
/******/ 		module.loaded = true;

/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}

/******/ 	// expose the modules object (__webpack_modules__)
/******/ 	__webpack_require__.m = modules;

/******/ 	// expose the module cache
/******/ 	__webpack_require__.c = installedModules;

/******/ 	// __webpack_public_path__
/******/ 	__webpack_require__.p = "";

/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {

	var d8 = __webpack_require__(1);
	var L = d8.readnumbers()[0];
	var p = d8.readnumbers()[0];
	var q = d8.readnumbers()[0];
	var t = L / (p + q);
	print(p * t);

/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {

	var List = __webpack_require__(2);
	function readnumbers() {
	    return List.map(parseInt, readline().split(' '));
	}
	exports.readnumbers = readnumbers;

/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {

	var Prelude_1 = __webpack_require__(3);
	function add(xs, ys) {
	    return xs.concat(ys);
	}
	exports.add = add;
	function head(xs) {
	    return xs[0];
	}
	exports.head = head;
	function last(xs) {
	    return xs[xs.length - 1];
	}
	exports.last = last;
	function tail(xs) {
	    return xs.slice(1);
	}
	exports.tail = tail;
	function init(xs) {
	    return xs.slice(0, xs.length - 1);
	}
	exports.init = init;
	function map(f, xs) {
	    var result = new Array(xs.length);
	    for (var i = 0; i < xs.length; i++) {
	        result[i] = f(xs[i]);
	    }
	    return result;
	}
	exports.map = map;
	function reverse(xs) {
	    return xs.slice().reverse();
	}
	exports.reverse = reverse;
	function intersperse(x, xs) {
	    if (xs.length == 0) {
	        return [];
	    }
	    var result = new Array(xs.length + xs.length - 1);
	    for (var i = 0; i + 1 < xs.length; i++) {
	        result[i + i] = xs[i];
	        result[i + i + 1] = x;
	    }
	    result[result.length - 1] = xs[xs.length - 1];
	    return result;
	}
	exports.intersperse = intersperse;
	function intercalate(xs, xss) {
	    return concat(intersperse(xs, xss));
	}
	exports.intercalate = intercalate;
	function foldl(f, initial, xs) {
	    var result = initial;
	    for (var i = 0; i < xs.length; i++) {
	        result = f(result, xs[i]);
	    }
	    return result;
	}
	exports.foldl = foldl;
	function foldr(f, initial, xs) {
	    var result = initial;
	    for (var i = xs.length - 1; i >= 0; i--) {
	        result = f(xs[i], result);
	    }
	    return result;
	}
	exports.foldr = foldr;
	function concat(xss) {
	    var total = sum(map(function (xs) { return xs.length; }, xss));
	    var result = new Array(total);
	    var m = 0;
	    for (var i = 0; i < xss.length; i++) {
	        var xs = xss[i];
	        for (var j = 0; j < xs.length; j++) {
	            result[m++] = xs[j];
	        }
	    }
	    return result;
	}
	exports.concat = concat;
	function sum(xs) {
	    var result = 0;
	    for (var i = 0; i < xs.length; i++) {
	        result += xs[i];
	    }
	    return result;
	}
	exports.sum = sum;
	function product(xs) {
	    var result = 1;
	    for (var i = 0; i < xs.length; i++) {
	        result *= xs[i];
	    }
	    return result;
	}
	exports.product = product;
	function sort(xs, compare) {
	    return copy(xs).sort(compare);
	}
	exports.sort = sort;
	function sortInPlace(xs, compare) {
	    xs.sort(compare);
	    return xs;
	}
	exports.sortInPlace = sortInPlace;
	function maximumInRange(xs, s, e) {
	    var result = xs[s];
	    for (var i = s + 1; i <= e; i++) {
	        if (result < xs[i])
	            result = xs[i];
	    }
	    return result;
	}
	exports.maximumInRange = maximumInRange;
	function maximum(xs) {
	    return maximumInRange(xs, 0, xs.length - 1);
	}
	exports.maximum = maximum;
	function maximumInRangeWith(xs, s, e, f) {
	    var result = f(xs[s]);
	    for (var i = s + 1; i <= e; i++) {
	        var candidate = f(xs[i]);
	        if (result < candidate)
	            result = candidate;
	    }
	    return result;
	}
	exports.maximumInRangeWith = maximumInRangeWith;
	function maximumWith(xs, f) {
	    return maximumInRangeWith(xs, 0, xs.length - 1, f);
	}
	exports.maximumWith = maximumWith;
	function minimumInRange(xs, s, e) {
	    var result = xs[s];
	    for (var i = s + 1; i <= e; i++) {
	        if (result > xs[i])
	            result = xs[i];
	    }
	    return result;
	}
	exports.minimumInRange = minimumInRange;
	function minimum(xs) {
	    return minimumInRange(xs, 0, xs.length - 1);
	}
	exports.minimum = minimum;
	function minimumInRangeWith(xs, s, e, f) {
	    var result = f(xs[s]);
	    for (var i = s + 1; i <= e; i++) {
	        var candidate = f(xs[i]);
	        if (result > candidate)
	            result = candidate;
	    }
	    return result;
	}
	exports.minimumInRangeWith = minimumInRangeWith;
	function minimumWith(xs, f) {
	    return minimumInRangeWith(xs, 0, xs.length - 1, f);
	}
	exports.minimumWith = minimumWith;
	function replicate(n, x) {
	    var result = new Array(n);
	    for (var i = 0; i < result.length; i++) {
	        result[i] = x;
	    }
	    return result;
	}
	exports.replicate = replicate;
	function take(n, xs) {
	    return xs.slice(0, n);
	}
	exports.take = take;
	function drop(n, xs) {
	    return xs.slice(n);
	}
	exports.drop = drop;
	function splitAt(n, xs) {
	    return [take(n, xs), drop(n, xs)];
	}
	exports.splitAt = splitAt;
	function takeWhile(f, xs) {
	    for (var i = 0; i < xs.length; i++) {
	        if (!f(xs[i])) {
	            return xs.slice(0, i);
	        }
	    }
	    return xs.slice();
	}
	exports.takeWhile = takeWhile;
	function dropWhile(f, xs) {
	    for (var i = 0; i < xs.length; i++) {
	        if (!f(xs[i])) {
	            return xs.slice(i);
	        }
	    }
	    return [];
	}
	exports.dropWhile = dropWhile;
	function group(xs) {
	    if (xs.length == 0)
	        return [];
	    var result = [];
	    var last = [xs[0]];
	    for (var i = 1; i < xs.length; i++) {
	        if (last[0] === xs[i]) {
	            last.push(xs[i]);
	        }
	        else {
	            result.push(last);
	            last = [xs[i]];
	        }
	    }
	    result.push(last);
	    return result;
	}
	exports.group = group;
	function nub(xs) {
	    if (xs.length == 0)
	        return [];
	    var result = [];
	    result.push(xs[0]);
	    for (var i = 1; i < xs.length; i++) {
	        if (xs[i] !== xs[i - 1]) {
	            result.push(xs[i]);
	        }
	    }
	    return result;
	}
	exports.nub = nub;
	function filter(f, xs) {
	    var result = [];
	    for (var i = 0; i < xs.length; i++) {
	        if (f(xs[i]))
	            result.push(xs[i]);
	    }
	    return result;
	}
	exports.filter = filter;
	function zip(xs, ys) {
	    var n = Prelude_1.min(xs.length, ys.length);
	    var result = new Array(n);
	    for (var i = 0; i < n; i++) {
	        result[i] = [xs[i], ys[i]];
	    }
	    return result;
	}
	exports.zip = zip;
	function unzip(xs) {
	    var r1 = new Array(xs.length);
	    var r2 = new Array(xs.length);
	    for (var i = 0; i < xs.length; i++) {
	        r1[i] = xs[i][0];
	        r2[i] = xs[i][1];
	    }
	    return [r1, r2];
	}
	exports.unzip = unzip;
	function range(from, to) {
	    var result = Array(to - from + 1);
	    for (var i = from; i <= to; i++) {
	        result[i - from] = i;
	    }
	    return result;
	}
	exports.range = range;
	function copy(xs) {
	    return xs.slice(0);
	}
	exports.copy = copy;
	function apply_permutation(p, xs) {
	    var n = xs.length;
	    var result = new Array(n);
	    for (var i = 0; i < n; i++) {
	        result[i] = xs[p[i]];
	    }
	    return result;
	}
	exports.apply_permutation = apply_permutation;
	function next_permutation(p) {
	    var n = p.length;
	    if (n < 2)
	        return null;
	    var r = copy(p);
	    var k = n - 2;
	    for (; k >= 0 && r[k] >= r[k + 1]; k--)
	        ;
	    if (k < 0)
	        return null;
	    for (var i = k + 1, j = n - 1; i < j; i++, j--) {
	        var t_1 = r[i];
	        r[i] = r[j];
	        r[j] = t_1;
	    }
	    var next = k + 1;
	    for (; r[next] <= r[k]; next++)
	        ;
	    var t = r[k];
	    r[k] = r[next];
	    r[next] = t;
	    return r;
	}
	exports.next_permutation = next_permutation;
	function create(n, value) {
	    return replicate(n, value);
	}
	exports.create = create;
	function createWith(n, f) {
	    var result = new Array(n);
	    for (var i = 0; i < n; i++) {
	        result[i] = f(i);
	    }
	    return result;
	}
	exports.createWith = createWith;
	function create2D(n1, n2, value) {
	    return map(function (_) { return replicate(n2, value); }, replicate(n1, 0));
	}
	exports.create2D = create2D;
	function create2DWith(n1, n2, f) {
	    var result = new Array(n1);
	    for (var i = 0; i < n1; i++) {
	        result[i] = new Array(n2);
	        for (var j = 0; j < n2; j++) {
	            result[i][j] = f(i, j);
	        }
	    }
	    return result;
	}
	exports.create2DWith = create2DWith;

/***/ },
/* 3 */
/***/ function(module, exports) {

	function min(a, b) {
	    return a < b ? a : b;
	}
	exports.min = min;
	function max(a, b) {
	    return a < b ? b : a;
	}
	exports.max = max;
	function curry(f) {
	    return function (x) { return function (y) { return f(x, y); }; };
	}
	exports.curry = curry;
	function uncurry(f) {
	    return function (x, y) { return f(x)(y); };
	}
	exports.uncurry = uncurry;
	function id(x) {
	    return x;
	}
	exports.id = id;
	function constant(x) {
	    return function (_) { return x; };
	}
	exports.constant = constant;
	function flip(f) {
	    return function (y) { return function (x) { return f(x)(y); }; };
	}
	exports.flip = flip;
	function flip2(f) {
	    return function (y, x) { return f(x, y); };
	}
	exports.flip2 = flip2;
	function compose(g, f) {
	    return function (x) { return g(f(x)); };
	}
	exports.compose = compose;
	function gcd(a, b) {
	    var r = a % b;
	    while (r !== 0) {
	        a = b;
	        b = r;
	        r = a % b;
	    }
	    return b;
	}
	exports.gcd = gcd;

/***/ }
/******/ ]);

var l = parseInt(readline()),
	p = parseInt(readline()),
	q = parseInt(readline()),
	t1, t2;

t1 = l / (p+q);
t2 = t1 * p;

print(t2);

try {require('codef');} catch (e){global = this};
function readLineAsArray(separator){return readline().split(typeof separator == "undefined" ? ' ' : separator)}
function readLineAsIntegerArray(separator){return readLineAsArray(separator).map(function(v){return +v})}
Array.prototype.list = function() {var length = arguments.length; for(var i=0; i < length; i++){global[arguments[i]] = this[i]}};
String.prototype.repeat = function(n){return new Array(n + 1).join(this);}

var l = +readline(),
    p = +readline(),
    q = +readline();

write(l * p / (p + q));

var l = parseInt(readline(), 10),
    p = parseInt(readline(), 10),
    q = parseInt(readline(), 10);

function calcLength (l, p, q) {
    return (l / (p + q)) * p;
}

write(calcLength(l, p, q));

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;
let l, p, q;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    l = +d;
    return;
  }

  if (c === 1) {
    c++;
    p = +d;
    return;
  }

  q = +d;

  c++;
});

rl.on('close', () => {
  const max = Math.max(p, q);
  let ans = max / 100 * l;

  if (max >= l) {
    ans = l * 2 / ((p + q) * 2);
    // console.log({ ans})
  }

  console.log(ans.toFixed(4));
});

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;
let l, p, q;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    l = +d;
    return;
  }

  if (c === 1) {
    c++;
    p = +d;
    return;
  }

  q = +d;

  c++;
});

rl.on('close', () => {
  if (p === q) {
    console.log((l / 2).toFixed(4));
    return;
  }

  if (p + q <= l) {
    console.log((Math.max(p, q) / 100 * l).toFixed(4));
    return;
  }

  if (p > l) {
    console.log(((p * 100 / (p + q)) / 100).toFixed(4));
    return;
  }

  console.log((p * 100 / (p + q)).toFixed(4));
});

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;
let l, p, q;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    l = +d;
    return;
  }

  if (c === 1) {
    c++;
    p = +d;
    return;
  }

  q = +d;

  c++;
});

rl.on('close', () => {
  const max = Math.max(p, q);
  const ans = max / 100 * l;

  console.log(ans.toFixed(4));
});

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;
let l, p, q;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    l = +d;
    return;
  }

  if (c === 1) {
    c++;
    p = +d;
    return;
  }

  q = +d;

  c++;
});

rl.on('close', () => {
  const max = Math.max(p, q);
  let ans = max / 100 * l;

  if (max === l) {
    ans = max / 2;
  }

  console.log(ans.toFixed(4));
});

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;
let l, p, q;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    l = +d;
    return;
  }

  if (c === 1) {
    c++;
    p = +d;
    return;
  }

  q = +d;

  c++;
});

rl.on('close', () => {
  const max = Math.max(p, q);
  let ans = max / 100 * l;

  if (p + q > l) {
    ans = l * 2 / ((p + q) * 2);

    if (p > q) {
      ans = l - ans;
    }
  }

  console.log(ans.toFixed(4));
});


const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;

function readline() {
    return input[currentline++];
}
var tc = parseInt(readline());
while(tc--)
{
    var n = parseInt(readline());
    var arr = readline().split(' ').map(Number);
    var poss = false;
    arr.sort((a,b)=>b-a);
    var sumRed = 0, sumBlue = arr[n-1];
    for(var i=0; i<parseInt((n-1)/2); i++)
    {
        sumBlue += arr[n-i-2];
        sumRed  += arr[i];
        if(sumRed > sumBlue)
        {
            poss = true;
            break;
        }
    }
    if(poss) console.log("YES");
    else console.log("NO");
}

'use strict';
const { throws } = require('assert');

 /*jslint node:true, browser:true, es6:true*/
const print = console.log
const E = {};
const lines = []
if (!process.env.JEST_WORKER_ID){
  const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout })
  rl.on('line', line => {
      lines.push(line);
  });
  rl.on('close', main)
}
let rli = 0;
function readline() {
    return lines[rli++];
}
const l = process.env.DEBUG ? console.log : ()=>{};

module.exports = E;

const ra = ()=>readline().split(' ').map(a=>+a)

const EPS = 1e-6;
const MOD = 1e9+7;

const calc = (arr)=>{
  arr.sort((a, b)=>a-b);
  let blue_sum = arr[0]+arr[1];
  let blue_cnt = 2;
  let red_sum = arr[arr.length-1];
  let bi = 2;
  let ri = arr.length-2;
  while (bi<ri && blue_sum>=red_sum){
    blue_sum += arr[bi];
    red_sum += arr[ri];
    bi++;
    ri--;
  }
  return red_sum>blue_sum
};

function main() {
    let T = +readline()
    while (T--){
      let [n] = ra();
      let arr = ra();
      print(calc(arr) ? 'YES' : 'NO');
    }
}

E.calc = calc;

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});
function readLine() {
    return inputString[currentLine++];
};
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });
    main();
    // cmd(not powershell): node cp.js < input.txt
});

function main() {
    let t = parseInt(readLine());
    while (t-- > 0) {
        solve();
    }
}
function solve() {
    readLine();
    let input = readLine().split(" ").map(i => parseInt(i)).sort((a,b)=>a-b);
    const rarr = [], barr = [];
    let l = 0; r = input.length - 1;
    let rsum = 0; bsum = 0;
    bsum += input[l];
    barr.push(input[l++]);
    while (l < r) {
        bsum += input[l];
        rsum += input[r];
        barr.push(input[l]);
        rarr.push(input[r]);
        if (rsum > bsum && rarr.length < barr.length) {
            console.log("YES");
            return;
        } else {
            l++;
            r--;
        }
    }
    console.log("NO");
}

const fs = require('fs');

const input = fs.readFileSync(0, 'utf8').trim().split('\n');

let currentline = 0;

function readline() {
    return input[currentline++];
}

let str = readline();

for(let q=1;q<=str;q++) {
const inp = readline()
   let data = readline().split(' ').map(numb => parseInt(numb));
   data.sort((a,b) => a-b);
   let sum = [];
   sum[-1] = 0;
   for(let i=0; i<inp; i++) {
       sum[i]=sum[i-1]+data[i]
   }
   let mid = Math.floor(inp/2)+1;
   mid--;
   let result = "NO";
   let j = 2;

   for(var i=1;i<=mid;i++) {
       if(sum[i] < (sum[inp-1]-sum[inp-j])) result = "YES";
       j++;
   }
   console.log(result);
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
    return input[currentline++];
}
var T = readline();
for(var q=1;q<=T;q++) {
   var n = readline()
   var a = readline().split(' ').map(w => parseInt(w));
    // var a =[3, 5, 4, 2]
    // var n = a.length;
   a.sort(function(ar,br) { return ar-br })
   var sum = []
   sum[-1] = 0;
   for(var i=0;i<n;i++) {
       sum[i]=sum[i-1]+a[i]
   }
   var mid = Math.floor(n/2)+1;
   mid--;
   var ok = false;
   var j = 2;

   for(var i=1;i<=mid;i++) {
       if(sum[i] < (sum[n-1]-sum[n-j])) ok = true;
       j++;
   }
   console.log(ok ? 'YES' : 'NO');
   //var [n,s] =[7,0]
//    var sum = 0;
//    var j = n*n;
//    for(var i=1;i<=n+1;i++) {
//        var nw = sum
//        if(sum+j <= s && s-sum ) {
//            sum += j;
//        }
//        else {
//            i--;
//            break;
//        }
//    }
//    if(i > n+1) i--;
//    console.log(i);
}

'use strict';

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

let inputString = '';
let currentLine = 0;

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

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

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

function output(x) {
    if (typeof x !== 'string') {
        x = x.toString();
    }
    //process.stdout.write(x);   // without auto '\n' (newline)
    console.log(x);  // with auto '\n' (newline)
}

function main() {
    let N = readline();

    for (let test = 0; test < N; test++) {
        let n = parseInt(readline(), 10);
        let arr = readline().split(' ').map(Number);

        arr.sort((a, b) => b - a);

        let R, B;

        if (n % 2 === 0) {
            let div = (n / 2) - 1;
            R = arr.slice(0, div);
            B = arr.slice(div + 1, n);
        } else {
            R = arr.slice(0, Math.floor(n / 2));
            B = arr.slice(Math.floor(n / 2), n);
        }

        let sumR = R.reduce((a, b) => a + b, 0);
        let sumB = B.reduce((a, b) => a + b, 0);

        output(sumR > sumB ? 'YES': 'NO');
    }
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');

let currentline = 0;
function readline(){
    return input[currentline++];
}
let t = +readline();
let result = 0;
while(t--){
    let n = +readline();
    let arr = readline().split(' ').map(x=>+x);
    console.log(`${solve(arr, n)}`)
}

function solve(nums, n){
    if(n < 3) return 'NO';

    nums.sort((a, b)=>a-b);
    let redSum = nums[n-1];
    let blueSum = nums[0] + nums[1];
    if(redSum > blueSum) return 'YES';
    let l = 2;
    let r = n-2;

    while(l < r){
        blueSum += nums[l];
        redSum += nums[r];
        l++;
        r--;
        if(redSum > blueSum) return 'YES';
    }
    return 'NO';
}

let stop = '';
process.stdin.on('data', c => stop += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = stop.split(EOL);
	var t = lines[0];
	var n = 0;
	for(i = 1; i <= t * 2; i++){
        if(i % 2 == 1){
            n = lines[i];
        }else{
            var k = lines[i].split(' ').map(Number).sort(function(a, b){return a - b});
            var blue = k[0] + k[1];
            var red = k[n - 1];
            var l = 2, r = n - 2;
            var flag = false;
            while(l < r){
                if(red > blue){
                    flag = true;
                    break;
                }
                blue += k[l], red +=k[r];
                l++;
                r--;
            }
            console.log(flag || red > blue ? 'YES':'NO');
        }
    }
});

 var tests = parseInt(readline());
    var n, s;
    for (var t=0; t < tests; t++) {
        n = parseInt(readline());
        s = readline().split(' ').map(x=>parseInt(x));
        s.sort((a, b)=>a-b);
        var suma = 0, sumb = s[0];
        var a = n-1, b = 1;
        while ( b < a && suma <= sumb ) {
            suma += s[a];
            sumb += s[b];
            b++;
            a--;
        }
        if (suma > sumb) {
            print('YES');
        } else {
            print('NO');
        }
    }

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;

function readline() {
    return input[currentline++];
}
var tc = parseInt(readline());
while(tc--)
{
    var n = parseInt(readline());
    var arr = readline().split(' ').map(Number);
    var poss = false;
    arr.sort((a,b)=>b-a);
    var sumRed = 0, sumBlue = arr[n-1];
    for(var i=1; i<parseInt((n-1)/2); i++)
    {
        sumBlue += arr[n-i-2];
        sumRed  += arr[i];
        if(sumRed > sumBlue)
        {
            poss = true;
            break;
        }
    }
    if(poss) console.log("YES");
    else console.log("NO");
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;

function readline() {
    return input[currentline++];
}
var tc = parseInt(readline());
while(tc--)
{
    var n = parseInt(readline());
    var arr = readline().split(' ').map(Number);
    var sumBlue = 0, sumRed = 0;
    var poss = false;
    for(var i=0;i<n;i++)
    {
        sumBlue += arr[i];
    }
    arr.sort((a,b)=>b-a);
    for(var i=0; i<n; i++)
    {
        let countRed = i+1, countBlue = n-i-1;
        sumBlue -= arr[i]; sumRed += arr[i];
        if(countRed < countBlue && sumRed > sumBlue)
        {
            poss = true;
            break;
        }
    }
    if(poss) console.log("YES");
    else console.log("NO");
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;

function readline() {
    return input[currentline++];
}
var tc = parseInt(readline());
while(tc--)
{
    var n = parseInt(readline());
    var arr = readline().split(' ').map(Number);
    var sumBlue = 0, sumRed = 0;
    var poss = false;
    for(var i=0;i<n;i++)
    {
        sumBlue += arr[i];
    }
    arr.sort((a,b)=>b-a);
    for(var i=0; i<n; i++)
    {
        let countRed = i+1, countBlue = n-i-1;
        if(countRed < countBlue && sumRed > sumBlue)
        {
            poss = true;
            break;
        }
    }
    if(poss) console.log("YES");
    else console.log("NO");
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;

function readline() {
    return input[currentline++];
}
var tc = parseInt(readline());
console.log(tc);
while(tc--)
{
    var n = parseInt(readline());
    var arr = readline().split(' ').map(Number);
    var sumBlue = 0, sumRed = 0;
    var poss = false;
    for(var i=0;i<n;i++)
    {
        sumBlue += arr[i];
    }
    arr.sort((a,b)=>b-a);
    for(var i=0; i<n; i++)
    {
        let countRed = i+1, countBlue = n-i-1;
        sumBlue -= arr[i]; sumRed += arr[i];
        if(countRed < countBlue && sumRed > sumBlue)
        {
            poss = true;
            break;
        }
    }
    if(poss) console.log("YES");
    else console.log("NO");
}

'use strict';
const { throws } = require('assert');

 /*jslint node:true, browser:true, es6:true*/
const print = console.log
const E = {};
const lines = []
if (!process.env.JEST_WORKER_ID){
  const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout })
  rl.on('line', line => {
      lines.push(line);
  });
  rl.on('close', main)
}
let rli = 0;
function readline() {
    return lines[rli++];
}
const l = process.env.DEBUG ? console.log : ()=>{};

module.exports = E;

const ra = ()=>readline().split(' ').map(a=>+a)

const EPS = 1e-6;
const MOD = 1e9+7;

const calc = (arr)=>{
  arr.sort((a, b)=>a-b);
  let blue_sum = arr[0]+arr[1];
  let blue_cnt = 2;
  let red_sum = arr[arr.length-1];
  let bi = 2;
  let ri = arr.length-2;
  while (bi<ri && blue_sum>=red_sum){
    bi++;
    ri--;
  }
  return red_sum>blue_sum
};

function main() {
    let T = +readline()
    while (T--){
      let [n] = ra();
      let arr = ra();
      print(calc(arr) ? 'YES' : 'NO');
    }
}

E.calc = calc;

'use strict';
const { throws } = require('assert');

 /*jslint node:true, browser:true, es6:true*/
const print = console.log
const E = {};
const lines = []
if (!process.env.JEST_WORKER_ID){
  const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout })
  rl.on('line', line => {
      lines.push(line);
  });
  rl.on('close', main)
}
let rli = 0;
function readline() {
    return lines[rli++];
}
const l = process.env.DEBUG ? console.log : ()=>{};

module.exports = E;

const ra = ()=>readline().split(' ').map(a=>+a)

const EPS = 1e-6;
const MOD = 1e9+7;

const calc = (arr)=>{
  arr.sort((a, b)=>a-b);
  return arr[0]+arr[1]<arr[arr.length-1];
};

function main() {
    let T = +readline()
    while (T--){
      let [n] = ra();
      let arr = ra();
      print(calc(arr) ? 'YES' : 'NO');
    }
}

E.calc = calc;

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});
function readLine() {
    return inputString[currentLine++];
};
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });
    main();
    // cmd(not powershell): node cp.js < input.txt
});

function main() {
    let t = parseInt(readLine());
    while (t-- > 0) {
        solve();
    }
}
function solve() {
    readLine();
    let input = readLine().split(" ").map(i => parseInt(i)).sort();
    const rarr = [], barr = [];
    let l = 0; r = input.length - 1;
    let rsum = 0; bsum = 0;
    bsum += input[l];
    barr.push(input[l++]);
    while (l < r) {
        bsum += input[l];
        rsum += input[r];
        barr.push(input[l]);
        rarr.push(input[r]);
        if (rsum > bsum && rarr.length < barr.length) {
            console.log("YES");
            return;
        } else {
            l++;
            r--;
        }
    }
    console.log("NO");
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});
function readLine() {
    return inputString[currentLine++];
};
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });
    main();
    // cmd(not powershell): node cp.js < input.txt
});

function main() {
    let t = parseInt(readLine());
    while (t--) {
        solve();
    }
}
function solve() {
    readLine();
    let input = readLine().split(" ").map(i => parseInt(i)).sort();
    const rarr = [], barr = [];
    let l = 0; r = input.length - 1;
    let rsum = 0; bsum = 0;
    bsum += input[l];
    barr.push(input[l++]);
    while (l < r) {
        bsum += input[l];
        rsum += input[r];
        barr.push(input[l]);
        rarr.push(input[r]);
        if (rsum > bsum && rarr.length < barr.length) {
            console.log("YES");
            break;
        } else {
            l++;
            r--;
        }
    }
    console.log("NO");
}

let input = "";
process.stdin.on("data", (chunk) => {
  input += chunk;
});

 process.stdin.on("end", () => {
  let lines = input.trim().split(" ");
  qualityVSQuantity(lines)

 })

function findBestCase(arr) {
    var sortedArr = arr.sort((a, b) => a - b);
    var maxElem = sortedArr[sortedArr.length - 1];
    var blueArr = ''
    var redArr = [];
    redArr[0] =(sortedArr[sortedArr.length - 1]);
    var sumOfBlue = 0;
    var sumOfRed = maxElem;

    for (var i = 0; i < arr.length; i++) {
      if (arr[i] < maxElem && sumOfBlue < maxElem) {
        if (blueArr.split(" ").length === 2) {
          var item = Math.max(blueArr.split(" ")[0], blueArr.split(" ")[1]);
          blueArr = item;
        }
        blueArr = blueArr + ' ' + arr[i]
      }
    }
    if (blueArr.split(" ").length) sumOfBlue = blueArr.split(" ").reduce(function (a, b) {return  +a + +b});
    if (sumOfBlue > sumOfRed) findBestCase(arr.filter(function(el) {return el !== maxElem}));
    else if (sumOfRed > sumOfBlue && redArr.length < blueArr.split(" ").length) console.log("YES");
    else console.log("NO");
  }

function qualityVSQuantity (a) {
    if (a.length <= 1) {
    console.log("NO");
    }
    findBestCase(a);
  }

let input = "";
process.stdin.on("data", (chunk) => {
  input += chunk;
});

 process.stdin.on("end", () => {
  let lines = input.trim().split(" ");
  qualityVSQuantity(lines)

 })

function findBestCase(arr) {
    var sortedArr = arr.sort((a, b) => a - b);
    var maxElem = sortedArr[sortedArr.length - 1];
    var blueArr = ''
    var redArr = [];
    redArr[0] =(sortedArr[sortedArr.length - 1]);
    var sumOfBlue = 0;
    var sumOfRed = maxElem;

    for (var i = 0; i < arr.length; i++) {
      if (arr[i] < maxElem && sumOfBlue < maxElem) {
        if (blueArr.length === 2) {
          var item = Math.max(blueArr[0], blueArr[1]);
          blueArr = item;
        }
        blueArr = blueArr + ' ' + arr[i]
      }
    }
    if (blueArr.split(" ").length) sumOfBlue = blueArr.split(" ").reduce(function (a, b) {return  a + b});

    if (sumOfBlue > sumOfRed) findBestCase(arr.filter(function(el) {return el !== maxElem}));
    else if (sumOfRed > sumOfBlue && redArr.length < blueArr.length) console.log("YES");
    else console.log("NO");
  }

function qualityVSQuantity (a) {
    if (a.length <= 1) {
    console.log("NO");
    }
    findBestCase(a);
  }

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
    return input[currentline++];
}
var T = readline();
for(var q=1;q<=T;q++) {
   var n = readline()
   var a = readline().split(' ').map(w => parseInt(w));
    // var a =[3, 5, 4, 2]
    // var n = a.length;
   a.sort(function(ar,br) { return ar-br })
   var sum = []
   sum[-1] = 0;
   for(var i=0;i<n;i++) {
       sum[i]=sum[i-1]+a[i]
   }
   var mid = Math.floor(n/2)+1;
   mid--;
   var ok = false;
   var j = 2;

   for(var i=1;i<=mid;i++) {
       if(sum[i] < (sum[n-1]-sum[n-j])) ok = true;
   }
   console.log(ok ? 'YES' : 'NO');
   //var [n,s] =[7,0]
//    var sum = 0;
//    var j = n*n;
//    for(var i=1;i<=n+1;i++) {
//        var nw = sum
//        if(sum+j <= s && s-sum ) {
//            sum += j;
//        }
//        else {
//            i--;
//            break;
//        }
//    }
//    if(i > n+1) i--;
//    console.log(i);
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
    return input[currentline++];
}
var T = readline();
for(var q=1;q<=T;q++) {
   var n = readline()
   var a = readline().split(' ').map(w => parseInt(w));
//    var a =[1,2,3]
//    var n = a.length;
   a.sort(function(ar,br) { return ar-br })
   var sum = []
   sum[-1] = 0;
   for(var i=0;i<n;i++) {
       sum[i]=sum[i-1]+a[i]
   }
   var mid = Math.floor(n/2)+1;
   mid--;
   var ok = false;

   for(var i=mid;i<n;i++) {
       //console.log(sum[i], sum[n-1])
       if(sum[i] < (sum[n-1]-sum[i])) ok = true;
   }
   console.log(ok ? 'YES' : 'NO');
   //var [n,s] =[7,0]
//    var sum = 0;
//    var j = n*n;
//    for(var i=1;i<=n+1;i++) {
//        var nw = sum
//        if(sum+j <= s && s-sum ) {
//            sum += j;
//        }
//        else {
//            i--;
//            break;
//        }
//    }
//    if(i > n+1) i--;
//    console.log(i);
}

'use strict';

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

let inputString = '';
let currentLine = 0;

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

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

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

function output(x) {
    if (typeof x !== 'string') {
        x = x.toString();
    }
    //process.stdout.write(x);   // without auto '\n' (newline)
    console.log(x);  // with auto '\n' (newline)
}

function main() {
    let N = readline();

    for (let test = 0; test < N; test++) {
        let n = parseInt(readline(), 10);
        let arr = readline().split(' ').map(Number);

        arr.sort((a, b) => b - a);

        let R, B;

        if (n % 2 === 0) {
            let div = (n / 2) - 1;
            R = arr.slice(0, div);
            B = arr.slice(div, n);
        } else {
            R = arr.slice(0, Math.floor(n / 2));
            B = arr.slice(Math.floor(n / 2), n);
        }

        let sumR = R.reduce((a, b) => a + b, 0);
        let sumB = B.reduce((a, b) => a + b, 0);

        output(sumR > sumB ? 'YES': 'NO');
    }
}


"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}

const solve = () => {
  let t = parseInt(readline());
  while (t--) {
    let n = parseInt(readline());
    let arr = readline()
      .trim()
      .split(" ")
      .map((cur) => parseInt(cur));
    if (n === 1) {
      console.log(arr[0]);
      continue;
    }
    let sum = arr[0] + arr[n - 1];
    for (let i = 1; i < n; i++) {
      sum += Math.abs(arr[i] - arr[i - 1]);
    }
    let r = 0;
    for (let i = 1; i < n - 1; i++) {
      if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
        r += (arr[i] - Math.max(arr[i - 1], arr[i + 1])) * 2;
        sum += arr[i] - Math.max(arr[i - 1], arr[i + 1]);
      }
    }
    if (arr[0] > arr[1]) {
      r += (arr[0] - arr[1]) * 2;
      sum += arr[0] - arr[1];
    }
    if (arr[n - 1] > arr[n - 2]) {
      r += (arr[n - 1] - arr[n - 2]) * 2;
      sum += arr[n - 1] - arr[n - 2];
    }
    console.log(sum - r);
  }
};
solve();

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const mxll = (...args) => args.reduce((m, e) => e > m ? e : m);
const mill = (...args) => args.reduce((m, e) => e < m ? e : m);
const sqll = (v) => { if (v < 0n) throw 'negative input'; if (v < 2n) return v; const dfs = (n, x0) => { let x1 = ((n / x0) + x0) >> 1n; if (x0 === x1 || x0 === (x1 - 1n)) return x0; return dfs(n, x1); }; return dfs(v, 1n); }; // has >> 0
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const ll = BigInt;
const combination = (m, n) => { return factorial(m, n) / factorial(n, n); };
const factorial = (m, n) => { let num = 1n; let cnt = 0; for (let i = ll(m); i > 0; i--) { if (cnt == n) break; num *= i; cnt++; } return num; };
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const bitCount = (n) => { n = n - ((n >> 1) & 0x55555555); n = (n & 0x33333333) + ((n >> 2) & 0x33333333); return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; };
const powmod = (a, b, mod) => { let r = 1n; while (b > 0n) { if (b % 2n == 1) r = r * a % mod; b >>= 1n; a = a * a % mod; } return r; };
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const sortPart = (a, k) => { let l = a.slice(0, k); l.sort((x, y) => x - y); let r = a.slice(k); return l.concat(r); }; // sort first kth
const initialize2DArrayNew = (m, n) => { let data = []; for (let i = 0; i < m; i++) { let tmp = Array(n).fill(0); data.push(tmp); } return data; };
const stmkey_in = (m) => new Map([...m].sort((x, y) => x[0] - y[0]));
const stmkey_de = (m) => new Map([...m].sort((x, y) => y[0] - x[0]));
const stmvalue_in = (m) => new Map([...m].sort((x, y) => x[1] - y[1]));
const stmvalue_de = (m) => new Map([...m].sort((x, y) => y[1] - x[1]));
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
const counter_value_in_indexA_in = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).push(i); } return m; };
const counter_value_in_indexA_de = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).unshift(i); } return m; };
const reverse2 = (s) => { let res = ""; for (let i = s.length - 1; i >= 0; i--) { res += s[i]; } return res; };
const isPalindrome = (s) => { let n = s.length; let i = 0; let j = n - 1; while (i < j) { if (s[i++] != s[j--]) return false; } return true; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 06/13/21 noon

const solve = (n, a) => {
    let res = ugliness(a);
    for (let i = 0; i < n; i++) {
        if (i + 1 < n && a[i] <= a[i + 1]) continue;
        if (i - 1 >= 0 && a[i] <= a[i - 1]) continue;
        let h = 0;
        if (i - 1 >= 0) h = mx(h, a[i - 1]);
        if (i + 1 < n) h = mx(h, a[i + 1]);
        if (a[i] > h) res -= a[i] - h;
    }
    pr(res);
};

// my code
const ugliness = (A) => {
    let a = [...A];
    a.unshift(0);
    a.push(0);
    let n = a.length;
    let sum = 0;
    let pre = a[0];
    for (let i = 1; i < n; i++) {
        let tmp = abs(a[i] - pre);
        sum += tmp;
        pre = a[i];
    }
    return sum;
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        let t = input[0][0];
        let i = 1;
        while (t--) {
            solve(input[i][0], input[i + 1]);
            i += 2;
        }
    });
};

main()

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const mxll = (...args) => args.reduce((m, e) => e > m ? e : m);
const mill = (...args) => args.reduce((m, e) => e < m ? e : m);
const sqll = (v) => { if (v < 0n) throw 'negative input'; if (v < 2n) return v; const dfs = (n, x0) => { let x1 = ((n / x0) + x0) >> 1n; if (x0 === x1 || x0 === (x1 - 1n)) return x0; return dfs(n, x1); }; return dfs(v, 1n); }; // has >> 0
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const ll = BigInt;
const combination = (m, n) => { return factorial(m, n) / factorial(n, n); };
const factorial = (m, n) => { let num = 1n; let cnt = 0; for (let i = ll(m); i > 0; i--) { if (cnt == n) break; num *= i; cnt++; } return num; };
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const bitCount = (n) => { n = n - ((n >> 1) & 0x55555555); n = (n & 0x33333333) + ((n >> 2) & 0x33333333); return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; };
const powmod = (a, b, mod) => { let r = 1n; while (b > 0n) { if (b % 2n == 1) r = r * a % mod; b >>= 1n; a = a * a % mod; } return r; };
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const sortPart = (a, k) => { let l = a.slice(0, k); l.sort((x, y) => x - y); let r = a.slice(k); return l.concat(r); }; // sort first kth
const initialize2DArrayNew = (m, n) => { let data = []; for (let i = 0; i < m; i++) { let tmp = Array(n).fill(0); data.push(tmp); } return data; };
const stmkey_in = (m) => new Map([...m].sort((x, y) => x[0] - y[0]));
const stmkey_de = (m) => new Map([...m].sort((x, y) => y[0] - x[0]));
const stmvalue_in = (m) => new Map([...m].sort((x, y) => x[1] - y[1]));
const stmvalue_de = (m) => new Map([...m].sort((x, y) => y[1] - x[1]));
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
const counter_value_in_indexA_in = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).push(i); } return m; };
const counter_value_in_indexA_de = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).unshift(i); } return m; };
const reverse2 = (s) => { let res = ""; for (let i = s.length - 1; i >= 0; i--) { res += s[i]; } return res; };
const isPalindrome = (s) => { let n = s.length; let i = 0; let j = n - 1; while (i < j) { if (s[i++] != s[j--]) return false; } return true; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 06/13/21 noon

const solve = (n, a) => {
    let res = ugliness(a);
    for (let i = 0; i < n; i++) {
        let max = 0;
        if (i - 1 >= 0) max = mx(max, a[i - 1]);
        if (i + 1 < n) max = mx(max, a[i + 1]);
        if (a[i] > max) res -= a[i] - max;
    }
    pr(res);
};

const ugliness = (A) => {
    let a = [...A];
    a.unshift(0);
    a.push(0);
    let n = a.length;
    let sum = 0;
    let pre = a[0];
    for (let i = 1; i < n; i++) {
        let tmp = abs(a[i] - pre);
        sum += tmp;
        pre = a[i];
    }
    return sum;
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        let t = input[0][0];
        let i = 1;
        while (t--) {
            solve(input[i][0], input[i + 1]);
            i += 2;
        }
    });
};

main()

const lib = require('readline');
const rl = lib.createInterface({
    input: process.stdin,
    output: process.stdout
});

const getLine = (function () {
    const getLineGen = (async function* () {
        for await (const line of rl) {
            yield line;
        }
    })();
    return async () => ((await getLineGen.next()).value);
})();

let solve = async () => {
    const nsc = parseInt(await getLine());
    for(let sc = 0; sc < nsc; sc++) {
        const n = parseInt(await getLine());
        const g = (await getLine()).split(' ').map(num => parseInt(num));
        g.unshift(0);
        g.push(0);
        let ugly = 0;
        for(let i = 1; i <= n; i++) {
            ugly += Math.abs(g[i] - g[i - 1]);
            if (g[i] > g[i-1] && g[i] > g[i+1]) {
                ugly -= Math.min(g[i] - g[i-1], g[i] - g[i+1]);
            }
        }
        ugly += g[n];
        console.log(ugly);
    }
}

solve();

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const mxll = (...args) => args.reduce((m, e) => e > m ? e : m);
const mill = (...args) => args.reduce((m, e) => e < m ? e : m);
const sqll = (v) => { if (v < 0n) throw 'negative input'; if (v < 2n) return v; const dfs = (n, x0) => { let x1 = ((n / x0) + x0) >> 1n; if (x0 === x1 || x0 === (x1 - 1n)) return x0; return dfs(n, x1); }; return dfs(v, 1n); }; // has >> 0
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const ll = BigInt;
const combination = (m, n) => { return factorial(m, n) / factorial(n, n); };
const factorial = (m, n) => { let num = 1n; let cnt = 0; for (let i = ll(m); i > 0; i--) { if (cnt == n) break; num *= i; cnt++; } return num; };
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const bitCount = (n) => { n = n - ((n >> 1) & 0x55555555); n = (n & 0x33333333) + ((n >> 2) & 0x33333333); return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; };
const powmod = (a, b, mod) => { let r = 1n; while (b > 0n) { if (b % 2n == 1) r = r * a % mod; b >>= 1n; a = a * a % mod; } return r; };
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const sortPart = (a, k) => { let l = a.slice(0, k); l.sort((x, y) => x - y); let r = a.slice(k); return l.concat(r); }; // sort first kth
const initialize2DArrayNew = (m, n) => { let data = []; for (let i = 0; i < m; i++) { let tmp = Array(n).fill(0); data.push(tmp); } return data; };
const stmkey_in = (m) => new Map([...m].sort((x, y) => x[0] - y[0]));
const stmkey_de = (m) => new Map([...m].sort((x, y) => y[0] - x[0]));
const stmvalue_in = (m) => new Map([...m].sort((x, y) => x[1] - y[1]));
const stmvalue_de = (m) => new Map([...m].sort((x, y) => y[1] - x[1]));
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
const counter_value_in_indexA_in = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).push(i); } return m; };
const counter_value_in_indexA_de = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).unshift(i); } return m; };
const reverse2 = (s) => { let res = ""; for (let i = s.length - 1; i >= 0; i--) { res += s[i]; } return res; };
const isPalindrome = (s) => { let n = s.length; let i = 0; let j = n - 1; while (i < j) { if (s[i++] != s[j--]) return false; } return true; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 06/13/21 noon
 * https://codeforces.com/contest/1534/problem/B
 */
const solve = (n, a) => {
    // pr(a)
    let m = counter(a);
    let min = Number.MAX_SAFE_INTEGER;
    let max = 0;
    for(const e of a) {
        max = mx(max, e);
        if (e != 0) min = mi(min, e);
    }
    let op = 0;
    let curMax = max;
    let ugly = ugliness(a);
    let res = ugly;
    while(curMax >= min) {
        // pr(curMax);
        for (let i = 0; i < n; i++) {
            if (a[i] > curMax) {
                a[i]--;
                op++;
                // if (i == 0) {
                //     if (a[i] > a[i + 1]) ugly--;
                // } else if (i == n - 1) {
                //     if (a[i] > a[i - 1]) ugly--;
                // } else {
                //     if (a[i] > a[i - 1] && a[i] > a[i + 1]) ugly--;
                // }
            }
        }
        let curUg = ugliness(a);
        // pr(a, curUg, op);
        res = mi(res, curUg + op);
        curMax--;
    }
    pr(res);
};

const ugliness = (A) => {
    let a = [...A];
    a.unshift(0);
    a.push(0);
    let n = a.length;
    let sum = 0;
    let pre = a[0];
    // pr(a);
    for (let i = 1; i < n; i++) {
        let tmp = abs(a[i] - pre);
        sum += tmp;
        pre = a[i];
        // pr(a[i], tmp);
    }
    return sum;
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        let t = input[0][0];
        let i = 1;
        while (t--) {
            solve(input[i][0], input[i + 1]);
            i += 2;
        }
    });
};

main()

///////////////////////////////// pre-define ////////////////////////////////////////////////////////////////////////////////
const pr = console.log;
const mi = Math.min;
const mx = Math.max;
const mxll = (...args) => args.reduce((m, e) => e > m ? e : m);
const mill = (...args) => args.reduce((m, e) => e < m ? e : m);
const sqll = (v) => { if (v < 0n) throw 'negative input'; if (v < 2n) return v; const dfs = (n, x0) => { let x1 = ((n / x0) + x0) >> 1n; if (x0 === x1 || x0 === (x1 - 1n)) return x0; return dfs(n, x1); }; return dfs(v, 1n); }; // has >> 0
const abs = Math.abs;
const fl = Math.floor;
const ce = Math.ceil;
const sq = Math.sqrt;
const lge = Math.log;
const lg10 = Math.log10;
const ll = BigInt;
const combination = (m, n) => { return factorial(m, n) / factorial(n, n); };
const factorial = (m, n) => { let num = 1n; let cnt = 0; for (let i = ll(m); i > 0; i--) { if (cnt == n) break; num *= i; cnt++; } return num; };
const gcd = (a, b) => b == 0 ? a : gcd(b, a % b);
const lcm = (a, b) => (a / gcd(a, b)) * b;
const bitCount = (n) => { n = n - ((n >> 1) & 0x55555555); n = (n & 0x33333333) + ((n >> 2) & 0x33333333); return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; };
const powmod = (a, b, mod) => { let r = 1n; while (b > 0n) { if (b % 2n == 1) r = r * a % mod; b >>= 1n; a = a * a % mod; } return r; };
const amax = (a) => mx.apply(Math, a);
const amin = (a) => mi.apply(Math, a);
const sm = (a) => a.reduce(((x, y) => x + y), 0);
const aeq = (a, b) => JSON.stringify(a) == JSON.stringify(b);
const swap = (a, i, j) => [a[i], a[j]] = [a[j], a[i]];
const stin = (a) => a.sort((x, y) => x - y);
const stde = (a) => a.sort((x, y) => y - x);
const sortPart = (a, k) => { let l = a.slice(0, k); l.sort((x, y) => x - y); let r = a.slice(k); return l.concat(r); }; // sort first kth
const initialize2DArrayNew = (m, n) => { let data = []; for (let i = 0; i < m; i++) { let tmp = Array(n).fill(0); data.push(tmp); } return data; };
const stmkey_in = (m) => new Map([...m].sort((x, y) => x[0] - y[0]));
const stmkey_de = (m) => new Map([...m].sort((x, y) => y[0] - x[0]));
const stmvalue_in = (m) => new Map([...m].sort((x, y) => x[1] - y[1]));
const stmvalue_de = (m) => new Map([...m].sort((x, y) => y[1] - x[1]));
const counter = (a_or_s) => { let map = new Map(); for (const i of a_or_s) map.set(i, map.get(i) + 1 || 1); return map; };
const counter_value_in_indexA_in = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).push(i); } return m; };
const counter_value_in_indexA_de = (a_or_s) => { let m = new Map(); let n = a_or_s.length; for (let i = 0; i < n; i++) { if (!m.has(a_or_s[i])) m.set(a_or_s[i], []); m.get(a_or_s[i]).unshift(i); } return m; };
const reverse2 = (s) => { let res = ""; for (let i = s.length - 1; i >= 0; i--) { res += s[i]; } return res; };
const isPalindrome = (s) => { let n = s.length; let i = 0; let j = n - 1; while (i < j) { if (s[i++] != s[j--]) return false; } return true; };
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * 06/13/21 noon
 * https://codeforces.com/contest/1534/problem/B
 */
const solve = (n, a) => {
    // pr(a)
    let m = counter(a);
    let min = Number.MAX_SAFE_INTEGER;
    let max = 0;
    for(const e of a) {
        max = mx(max, e);
        if (e != 0) min = mi(min, e);
    }
    let op = 0;
    let curMax = max - 1;
    let ugly = ugliness(a);
    let res = ugly;
    while(curMax >= min) {
        // pr(curMax);
        for (let i = 0; i < n; i++) {
            if (a[i] > curMax) {
                a[i]--;
                op++;
                // if (i == 0) {
                //     if (a[i] > a[i + 1]) ugly--;
                // } else if (i == n - 1) {
                //     if (a[i] > a[i - 1]) ugly--;
                // } else {
                //     if (a[i] > a[i - 1] && a[i] > a[i + 1]) ugly--;
                // }
            }
        }
        let curUg = ugliness(a);
        // pr(a, curUg, op);
        res = mi(res, curUg + op);
        curMax--;
    }
    pr(res);
};

const ugliness = (A) => {
    let a = [...A];
    a.unshift(0);
    a.push(0);
    let n = a.length;
    let sum = 0;
    let pre = a[0];
    // pr(a);
    for (let i = 1; i < n; i++) {
        let tmp = abs(a[i] - pre);
        sum += tmp;
        pre = a[i];
        // pr(a[i], tmp);
    }
    return sum;
};

const main = () => {
    const readline = require('readline');
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    let input = [];
    rl.on('line', (line) => {
        input.push(line.split(" ").map(x => Number(x)));
    });
    rl.on('close', () => {
        let t = input[0][0];
        let i = 1;
        while (t--) {
            solve(input[i][0], input[i + 1]);
            i += 2;
        }
    });
};

main()

// pr(ugliness([4, 8, 9, 6]), 0)
// pr(ugliness([4, 8, 8, 6]), 1)
// pr(ugliness([4, 7, 7, 6]), 3)

"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}

const solve = () => {
  let t = parseInt(readline());
  while (t--) {
    let n = parseInt(readline());
    let arr = readline()
      .trim()
      .split(" ")
      .map((cur) => parseInt(cur));
    let sum = arr[0] + arr[n - 1];
    for (let i = 1; i < n; i++) {
      sum += Math.abs(arr[i] - arr[i - 1]);
    }
    let r = 0;
    for (let i = 1; i < n - 1; i++) {
      if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
        r += (arr[i] - Math.max(arr[i - 1], arr[i + 1])) * 2;
        sum += arr[i] - Math.max(arr[i - 1], arr[i + 1]);
      }
    }
    if (arr[0] > arr[1]) {
      r += (arr[0] - arr[1]) * 2;
      sum += arr[0] - arr[1];
    }
    if (arr[n - 1] > arr[n - 2]) {
      r += (arr[n - 1] - arr[n - 2]) * 2;
      sum += arr[n - 1] - arr[n - 2];
    }
    console.log(sum - r);
  }
};
solve();


;(function () {
	var n = readline().split(' ');
	var k = +n[1];
	n = +n[0];

	var s = readline().split(' ').map(Number);
	var t = 0;
	for (var i = 1; i < n; i++) {
		var a = readline().split(' ').map(Number);

		t += Math.pow( Math.pow(a[0] - s[0], 2) + Math.pow(a[1] - s[1], 2), 0.5);

		s = a;
	}

	print(t * k / 50);

}).call(this);

print(function(n, k) {

	var a = [];
	for (var i = 0; i < n; i++)
		a.push(readline().split(' ').map(Number));

	var d = 0;
	a.reduce(function(a, b) {
		var x = a[0] - b[0];
		var y = a[1] - b[1];
		d += Math.sqrt(x * x + y * y);
		return b;
	});

	return d * k / 50;

}.apply(0, readline().split(' ').map(Number)));

var nk = readline();
nk = nk.split(" ");
nk[0] = parseInt(nk[0]);
nk[1] = parseInt(nk[1]);
var v = 50, s = 0;
var xy0 = readline();
xy0 = xy0.split(" ");
xy0[0] = parseInt(xy0[0]);
xy0[1] = parseInt(xy0[1]);
for(var i = 1; i < nk[0]; ++i)
{
    var xy = readline();
    xy = xy.split(" ");
    xy[0] = parseInt(xy[0]);
    xy[1] = parseInt(xy[1]);
    s += Math.sqrt(Math.pow((xy[0] - xy0[0]), 2) + Math.pow((xy[1] - xy0[1]), 2));
    xy0 = xy;
}
print(((s / v)*nk[1]).toFixed(6));