<?php
$t = trim(fgets(STDIN));

for ($i = 0; $i < $t; $i++) {
    $aR = explode(' ', trim(fgets((STDIN))));
    $a = $aR[0];
    $b = $aR[1];
    $c = $aR[2];
    $n = $aR[3];
    unset($aR[3]);
    $max = max($aR);

    $k  = bcsub($n, (bcsub(bcmul(2, $max), (bcsub(bcadd(bcadd($a, $b) , $c) , $max)))));
    if ($k >= 0 && bcmod($k, 3) ==0) {
        echo "YES\n";
    } else {
        echo "NO\n";
    }

}
?>

<?php

// your code goes here
function sistersdivide($polycrap,$sisters){
sort($sisters);
$rest = 0;
$arraysize = count($sisters);
  for($i = $arraysize - 1; $i >= 0; $i--){

      $rest +=  max($sisters) - $sisters[$i];

  }
  if(($polycrap - $rest) % $arraysize == 0 && ($polycrap - $rest) >= 0){
    return "YES\n";
  }else {
    return "NO\n";
  }
}
fscanf(STDIN, "%d\n", $size);
for($i = 0; $i < $size; $i++){
   $cases = fgets(STDIN);
    $x = array_map('intval', preg_split('/ /', $cases, -1, PREG_SPLIT_NO_EMPTY));
    $polycrap = $x[count($x)-1];
    unset($x[count($x)-1]);
    $sisters = $x;
    $results = sistersdivide($polycrap,$sisters);
    echo $results;
}

<?php
    $t = trim(fgets(STDIN));
    for($i=0; $i<$t; $i++) {
        list($a, $b, $c, $n) = explode(' ', trim(fgets(STDIN)));

        if(($a + $b + $c +$n ) % 3 == 0) {
            echo "YES\n";
        }else {
            echo "NO\n";
        }

    }

<?php
$t = trim(fgets(STDIN));

for ($i = 0; $i < $t; $i++) {
    $aR = explode(' ', trim(fgets((STDIN))));
    $a = $aR[0];
    $b = $aR[1];
    $c = $aR[2];
    $n = $aR[3];
    unset($aR[3]);
    $max = max($aR);

    $k  = bcsub($n, (bcsub(bcmul(2, $max), (bcsub(bcadd(bcadd($a, $b) , $c) , $max)))));
    if (bcmod($k, 3) ==0) {
        echo "YES\n";
    } else {
        echo "NO\n";
    }

}
?>

<?php
$i = trim(fgets(STDIN));

for($j = 0; $j < $i; $j++){
    $k = trim(fgets(STDIN));
    $a = array();
    for($m = 0; $m < $k; $m++){
        $d = explode(" ", trim(fgets(STDIN)));
        $a[$d[0]][] = $d[1];
    }
    ksort($a);
    $x = 0;
    $y = 0;
    $res = '';
    foreach($a as $key => $val){
        sort($val);
        if($key >= $x){
            for($t = 0; $t < $key - $x; $t++){
                    $res .= 'R';
                }
            $x = $key;
        } else {
            echo "NO\n";
            continue 2;
        }
        foreach($val as $v){
            if($v >= $y){
                for($t = 0; $t < $v - $y; $t++){
                    $res .= 'U';
                }
                $y = $v;
            } else {
                echo "NO\n";
                continue 3;
            }
        }

    }
    echo "YES\n";
    echo $res."\n";

}


<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     print trim(fgets(STDIN)) . "\n";
}
?>

<?php
    define("MAXN", 200001) ;

    function solve($n, $k)
    {
    }

    $T = intval(fgets(STDIN));
    while($T --)
    {
        $n = intval(fgets(STDIN)) ;

        echo $n . "\n" ;
    }
?>

<?php
$in = fopen("php://stdin", "r"); //or simply $in = STDIN;
$t = trim(fgets($in));

for($i = 0;$i <$t; $i++)
{
    $n = trim(fgets($in));
    print $n."\n";
}
?>

<?php

for($t = intval(trim(fgets(STDIN))); $t > 0; $t --){
    //print pow(2, intval(fgets(STDIN)) - 1)."\n";
    print intval(fgets(STDIN))."\n";
}

<?php

for($t = intval(trim(fgets(STDIN))); $t > 0; $t --){
    print pow(2, intval(fgets(STDIN)) - 1)."\n";
}

<?php

for($t = intval(trim(fgets(STDIN))); $t > 0; $t --){
    //print pow(2, intval(fgets(STDIN)) - 1)."\n";
    print intval(fgets(STDIN))."/n";
}


<?php

$t = (int) trim(fgets(STDIN));
for ($i = 0; $i < $t; ++$i) {
    //i[]
    list($ni[], $xi[]) = array_map('intval', explode(' ', trim(fgets(STDIN))));
    $ai[] = array_map('intval', explode(' ', trim(fgets(STDIN))));
}
for ($i = 0; $i < $t; ++$i) {
    $n = $ni[$i];
    $x = $xi[$i];
    $a = $ai[$i];
    $sum = 0;
    foreach ($a as $v) {
        $sum += ceil($v / $x);
    }
    echo nf(ceil(array_sum($a) / $x)).' '.nf($sum).PHP_EOL;
}
function nf($n)
{
    return number_format($n, 0, '', '');
}

<?php

$file = fopen("php://stdin", "r");

$rowsNumber = fgets($file);

function getArrayBeauty($array, $x) {
    $sum = 0;

    foreach($array as $element) {
        $sum += ceil($element / $x);
    }

    return $sum;
}

function getSum($array, $n) {
    $sum = 0;

    for ($k=0; $k<$n; $k++) {
        $sum += $array[$k];
    }

    return $sum;
}

for ($i=0; $i<$rowsNumber; $i++) {
    list($n, $x) = explode(' ', fgets($file));
    $array       = explode(' ', fgets($file));

    printf("%0.0f", getArrayBeauty([getSum($array, $n)], $x));
    echo " ";
    printf("%0.0f", getArrayBeauty($array, $x));
    echo "\n";
}

<?php
$fileName = 'php://stdin';
$file = fopen($fileName, "r");

$t = trim(fgets($file));
for ($i = 0; $i < $t; $i++) {
  list($n, $x) = explode(" ", trim(fgets($file)));
  $a = explode(" ", trim(fgets($file)));
  $m = 0; $k = 0;

  for ($j = 0; $j < $n; $j++) {
    $m += ceil($a[$j] / $x);
    $k += $a[$j];
  }
  $k = ceil($k / $x);
  $min = min($m, $k);
  $max = max($m, $k);
  printf('%0.0f',$min);
  echo ' ';
  printf('%0.0f',$max);
  echo PHP_EOL;
}
?>

<?php
function solve() {
    list($n, $x) = explode(' ', Read::string());
    $a = Read::array_int();
    $min = 0;
    $max = 0;
    for ($i = 0; $i < $n; $i++) {
        $min = Bigint::add($min, $a[$i]);
        $max = Bigint::add($max, Bigint::div($a[$i], $x, 'ceil'));
    }

    echo Bigint::div($min, $x, 'ceil')." ".$max;
}

#$t = 1;
$t = Read::int();
while ($t--) {
    solve();
    echo "\n";
}

class Read
{
    public static function string()
    {
        return trim(fgets(STDIN));
    }

    public static function int()
    {
        return intval(fgets(STDIN));
    }

    public static function array_int($sep = ' ')
    {
        $s = trim(fgets(STDIN));
        return preg_split("/[\s,]+/", $s);
    }

}

class Bigint
{
    /**
     * @param string $bigint1
     * @param string $bigint2
     * @param string $scale (ceil or floor or round)
     * @return string $bigint1 / $bigint2
     */
    public static function div($bigint1, $bigint2, $scale = '') {
        switch ($scale) {
            case 'ceil':
                return bcmod($bigint1, $bigint2) == 0 ? bcdiv($bigint1, $bigint2) : bcadd(bcdiv($bigint1, $bigint2), 1);
            case 'floor':
                return bcdiv($bigint1, $bigint2);
            case 'round':
                $sResult = bcdiv($bigint1, $bigint2);
                return bcsub(bcdiv($bigint1, $bigint2, 2),  $sResult, 2) >= 0.5 ? bcadd($sResult, 1) : $sResult;
            default:
                return bcdiv($bigint1, $bigint2);
        }
    }

    /**
     * @param string $bigint1
     * @param string $bigint2
     * @param string $scale (ceil or floor or round)
     * @return string $bigint1 + $bigint2
     */
    public static function add($bigint1, $bigint2, $scale = '') {
        switch ($scale) {
            case 'ceil':
                $sResult = bcadd($bigint1, $bigint2);
                return bcsub(bcadd($bigint1, $bigint2, 2),  $sResult, 2) == 0 ? $sResult : bcadd($sResult, 1);
            case 'floor':
                return bcadd($bigint1, $bigint2);
            case 'round':
                $sResult = bcadd($bigint1, $bigint2);
                return bcsub(bcadd($bigint1, $bigint2, 2),  $sResult, 2) >= 0.5 ? bcadd($sResult, 1) : $sResult;
            default:
                return bcadd($bigint1, $bigint2);
        }
    }

    /**
     * @param string $bigint1
     * @param string $bigint2
     * @param string $scale (ceil or floor or round)
     * @return string $bigint1 - $bigint2
     */
    public static function bigintSub($bigint1, $bigint2, $scale = '') {
        switch ($scale) {
            case 'ceil':
                $sResult = bcsub($bigint1, $bigint2);
                return bcsub(bcsub($bigint1, $bigint2, 2),  $sResult, 2) == 0 ? $sResult : bcadd($sResult, 1);
            case 'floor':
                return bcsub($bigint1, $bigint2);
            case 'round':
                $sResult = bcsub($bigint1, $bigint2);
                return bcsub(bcsub($bigint1, $bigint2, 2),  $sResult, 2) >= 0.5 ? bcadd($sResult, 1) : $sResult;
            default:
                return bcsub($bigint1, $bigint2);
        }
    }

    /**
     * @param string $bigint1
     * @param string $bigint2
     * @param string $scale (ceil or floor or round)
     * @return string $bigint1 * $bigint2
     */
    public static function mul($bigint1, $bigint2, $scale = '') {
        switch ($scale) {
            case 'ceil':
                $sResult = bcmul($bigint1, $bigint2);
                return bcsub(bcmul($bigint1, $bigint2, 2),  $sResult, 2) == 0 ? $sResult : bcadd($sResult, 1);
            case 'floor':
                return bcmul($bigint1, $bigint2);
            case 'round':
                $sResult = bcmul($bigint1, $bigint2);
                return bcsub(bcmul($bigint1, $bigint2, 2),  $sResult, 2) >= 0.5 ? bcadd($sResult, 1) : $sResult;
            default:
                return bcmul ($bigint1, $bigint2);
        }
    }

    /**
     * @param string $bigint1
     * @param string $bigint2
     * @return string $bigint1 % $bigint2
     */
    public static function mod($bigint1, $bigint2) {
        return bcmod($bigint1, $bigint2);
    }

    /**
     * @param string $bigint
     * @param string $scale (ceil or floor or round)
     * @return string sqrt($bigint)
     */
    public static function sqrt($bigint, $scale = '') {
        switch ($scale) {
            case 'ceil':
                return bcadd(bcsqrt ($bigint), 1);
            case 'floor':
                return bcsqrt($bigint);
            case 'round':
                $sResult = bcsqrt($bigint);
                return bcsub(bcsqrt($bigint, 2),  $sResult, 2) >= 0.5 ? bcadd($sResult, 1) : $sResult;
            default:
                return bcsqrt($bigint);
        }
    }

    /**
     * @param string $bigint
     * @param string $exponent
     * @param string $scale (ceil or floor or round)
     * @return string pow($bigint, $exponent)
     */
    public static function pow($bigint, $exponent, $scale = '') {
        switch ($scale) {
            case 'ceil':
                $sResult = bcpow($bigint, $exponent);
                return bcsub(bcpow($bigint, $exponent, 2),  $sResult, 2) == 0 ? $sResult : bcadd($sResult, 1);
            case 'floor':
                return bcpow($bigint, $exponent);
            case 'round':
                $sResult = bcpow($bigint, $exponent);
                return bcsub(bcpow($bigint, $exponent, 2),  $sResult, 2) >= 0.5 ? bcadd($sResult, 1) : $sResult;
            default:
                return bcpow ($bigint, $exponent);
        }
    }
}

?>

<?php
function solve() {
    $s1 = trim(fgets(STDIN));
    list($n, $x) = explode(' ', $s1);
    $s = trim(fgets(STDIN));
    $a = preg_split("/[\s,]+/", $s);
    $min = 0;
    $max = 0;
    for ($i = 0; $i < $n; $i++) {
        $min = bigintAdd($min, $a[$i]);
        $max = bigintAdd($max, bigintDiv($a[$i], $x, 'ceil'));
    }

    echo bigintDiv($min, $x, 'ceil')." ".$max;
}

function bigintDiv($sBigint1, $sBigint2, $sCale = '') {
    switch ($sCale) {
        case 'ceil':
            return bcmod($sBigint1, $sBigint2) == 0 ? bcdiv($sBigint1, $sBigint2) : bcadd(bcdiv($sBigint1, $sBigint2), 1);
        case 'floor':
            return bcdiv($sBigint1, $sBigint2);
        case 'round':
            $sResult = bcdiv($sBigint1, $sBigint2);
            return bcsub(bcdiv($sBigint1, $sBigint2, 2),  $sResult, 2) >= 0.5 ? bcadd($sResult, 1) : $sResult;
        default:
            return bcdiv($sBigint1, $sBigint2);
    }
}

function bigintAdd($sBigint1, $sBigint2, $sCale = '') {
    switch ($sCale) {
        case 'ceil':
            return bcadd(bcadd($sBigint1, $sBigint2), 1);
        case 'floor':
            return bcadd($sBigint1, $sBigint2);
        case 'round':
            $sResult = bcadd($sBigint1, $sBigint2);
            return bcsub(bcadd($sBigint1, $sBigint2, 2),  $sResult, 2) >= 0.5 ? bcadd($sResult, 1) : $sResult;
        default:
            return bcadd($sBigint1, $sBigint2);
    }
}

$t = trim(fgets(STDIN));
while ($t--) {
    solve();
    echo "\n";
}
?>

<?php

$file = fopen("php://stdin", "r");

$rowsNumber = fgets($file);

function getNumbersSum($string1, $string2)
{
    $string1 = trim($string1);
    $string2 = trim($string2);

    $baseArray    = array_fill(0, 28, 0);
    $numberArray1 = array_reverse(str_split($string1));
    $numberArray2 = array_reverse(str_split($string2));

    $count1 = count($numberArray1);
    $count2 = count($numberArray2);

    $maxCount = $count1 >= $count2 ? $count1 : $count2;

    for ($i=0; $i<$count1; $i++) {
        $baseArray[$i] += $numberArray1[$i];
    }

    $memory = 0;
    for ($i=0; $i<$count2; $i++) {
        $elementsSum   = $baseArray[$i] + $numberArray2[$i];
        $elementsSum   += $memory;
        $baseArray[$i] = ($elementsSum)%10;
        $memory        = floor($elementsSum/10);
    }

    while ($memory > 0) {
        $elementsSum   = $baseArray[$i] + $memory;
        $baseArray[$i] = ($elementsSum)%10;
        $memory        = round($elementsSum/10);
        $i++;
    }

    $digitsCount = ($i >= $maxCount) ? $i : $maxCount;

    $resultArray = array_slice($baseArray, 0, $digitsCount);
    $resultArray = array_reverse($resultArray);

    return implode($resultArray);
}

function getArrayBeauty($array, $x) {
    $sum = "0";

    foreach($array as $element) {
        $sum = getNumbersSum($sum, ceil($element / $x));
    }

    return $sum;
}

function getSum($array, $n) {
    $sum = "0";

    for ($k=0; $k<$n; $k++) {
        $sum = getNumbersSum($sum, $array[$k]);
    }

    return $sum;
}

for ($i=0; $i<$rowsNumber; $i++) {
    list($n, $x) = explode(' ', fgets($file));
    $array       = explode(' ', fgets($file));

    echo getArrayBeauty([getSum($array, $n)], $x);
    echo " ";
    echo getArrayBeauty($array, $x);
    echo "\n";
}

<?php

$file = fopen("php://stdin", "r");

$rowsNumber = fgets($file);

function getArrayBeauty($array, $x) {
    $sum = (float) 0.0;

    foreach($array as $element) {
        $sum += (float) ceil($element / $x);
    }

    return $sum;
}

function getSum($array, $n) {
    $sum = (float) 0.0;

    for ($k=0; $k<$n; $k++) {
        $sum += (float) $array[$k];
    }

    return $sum;
}

for ($i=0; $i<$rowsNumber; $i++) {
    list($n, $x) = explode(' ', fgets($file));
    $array       = explode(' ', fgets($file));

    echo getArrayBeauty([getSum($array, $n)], $x);
    echo " ";
    echo getArrayBeauty($array, $x);
    echo "\n";
}

<?php

$file = fopen("php://stdin", "r");

$rowsNumber = fgets($file);

function getArrayBeauty($array, $x) {
    $sum = (float) 0.0;

    foreach($array as $element) {
        $sum += ceil($element / $x);
    }

    return $sum;
}

function getSum($array, $n) {
    $sum = (float) 0.0;

    for ($k=0; $k<$n; $k++) {
        $sum += $array[$k];
    }

    return $sum;
}

for ($i=0; $i<$rowsNumber; $i++) {
    list($n, $x) = explode(' ', fgets($file));
    $array       = explode(' ', fgets($file));

    echo getArrayBeauty([getSum($array, $n)], $x);
    echo " ";
    echo getArrayBeauty($array, $x);
    echo "\n";
}

<?php

$file = fopen("php://stdin", "r");

$rowsNumber = fgets($file);

function getArrayBeauty($array, $x) {
    $sum = 0.0;

    foreach($array as $element) {
        $sum += ceil($element / $x);
    }

    return $sum;
}

function getSum($array, $n) {
    $sum = 0.0;

    for ($k=0; $k<$n; $k++) {
        $sum += $array[$k];
    }

    return $sum;
}

for ($i=0; $i<$rowsNumber; $i++) {
    list($n, $x) = explode(' ', fgets($file));
    $array       = explode(' ', fgets($file));

    echo getArrayBeauty([getSum($array, $n)], $x);
    echo " ";
    echo getArrayBeauty($array, $x);
    echo "\n";
}

<?php

$file = fopen("php://stdin", "r");

$rowsNumber = fgets($file);

function getArrayBeauty($array, $x) {
    $sum = 0;

    foreach($array as $element) {
        $sum += ceil($element / $x);
    }

    return $sum;
}

function getSum($array, $n) {
    $sum = 0;

    for ($k=0; $k<$n; $k++) {
        $sum += $array[$k];
    }

    return $sum;
}

for ($i=0; $i<$rowsNumber; $i++) {
    list($n, $x) = explode(' ', fgets($file));
    $array       = explode(' ', fgets($file));

    echo getArrayBeauty([getSum($array, $n)], $x);
    echo " ";
    echo getArrayBeauty($array, $x);
    echo "\n";
}

<?php

$file = fopen("php://stdin", "r");

$rowsNumber = fgets($file);

function getArrayBeauty($array, $n, $x) {
    $sum = 0;

    foreach($array as $element) {
        // if ($n == 9) {
        //     var_dump($element / $x, ceil($element / $x));
        // }
        $sum += ceil($element / $x);
    }

    // if ($n == 9) {
    //     var_dump("sum ", $sum); exit;
    // }

    return $sum;
}

function isMultipleOfThree($number) {
    return $number%3 === 0;
}

function minimizeArray($array, $n) {
    if ($n == 1) {
        return $array;
    }

    for ($k=0; $k<$n-1; $k++) {
        if (isMultipleOfThree($array[$k]) || isMultipleOfThree($array[$k+1])) {
            continue;
        }

        $array[$k] = $array[$k] + $array[$k+1];
        unset($array[$k+1]);
        $array = array_values($array);
        $k = 0;
    }

    // if ($n == 9) {
    //     var_dump($array);
    // }

    return $array;
}

for ($i=0; $i<$rowsNumber; $i++) {
    list($n, $x) = explode(' ', fgets($file));
    $array       = explode(' ', fgets($file));

    echo getArrayBeauty(minimizeArray($array, $n), $n, $x);
    echo " ";
    echo getArrayBeauty($array, $n, $x);
    echo "\n";
}

<?php

$file = fopen("php://stdin", "r");

$rowsNumber = fgets($file);

function getArrayBeauty($array, $n, $x) {
    $sum = 0;

    foreach($array as $element) {
        // if ($n == 9) {
        //     var_dump($element / $x, ceil($element / $x));
        // }
        $sum += ceil($element / $x);
    }

    if ($n == 9) {
        var_dump("sum ", $sum); exit;
    }

    return $sum;
}

function isMultipleOfThree($number) {
    return $number%3 === 0;
}

function minimizeArray($array, $n) {
    if ($n == 1) {
        return $array;
    }

    for ($k=0; $k<$n-1; $k++) {
        if (isMultipleOfThree($array[$k]) || isMultipleOfThree($array[$k+1])) {
            continue;
        }

        $array[$k] = $array[$k] + $array[$k+1];
        unset($array[$k+1]);
        $array = array_values($array);
        $k = 0;
    }

    // if ($n == 9) {
    //     var_dump($array);
    // }

    return $array;
}

for ($i=0; $i<$rowsNumber; $i++) {
    list($n, $x) = explode(' ', fgets($file));
    $array       = explode(' ', fgets($file));

    echo getArrayBeauty(minimizeArray($array, $n), $n, $x);
    echo " ";
    echo getArrayBeauty($array, $n, $x);
    echo "\n";
}

<?php

$file = fopen("php://stdin", "r");

$rowsNumber = fgets($file);

function getArrayBeauty($array, $n, $x) {
    $sum = 0;

    foreach($array as $element) {
        if ($n == 9) {
            var_dump($element / $x, ceil($element / $x));
        }
        $sum += ceil($element / $x);
    }

    if ($n == 9) {
        var_dump("sum ", $sum);
    }

    return $sum;
}

function isMultipleOfThree($number) {
    return $number%3 === 0;
}

function minimizeArray($array, $n) {
    if ($n == 1) {
        return $array;
    }

    for ($k=0; $k<$n-1; $k++) {
        if (isMultipleOfThree($array[$k]) || isMultipleOfThree($array[$k+1])) {
            continue;
        }

        $array[$k] = $array[$k] + $array[$k+1];
        unset($array[$k+1]);
        $array = array_values($array);
        $k = 0;
    }

    // if ($n == 9) {
    //     var_dump($array);
    // }

    return $array;
}

for ($i=0; $i<$rowsNumber; $i++) {
    list($n, $x) = explode(' ', fgets($file));
    $array       = explode(' ', fgets($file));

    echo getArrayBeauty(minimizeArray($array, $n), $n, $x);
    echo " ";
    echo getArrayBeauty($array, $n, $x);
    echo "\n";
}

<?php

$file = fopen("php://stdin", "r");

$rowsNumber = fgets($file);

function getArrayBeauty($array, $n, $x) {
    $sum = 0;

    foreach($array as $element) {
        if ($n == 9) {
            var_dump($element / $x, ceil($element / $x));
        }
        $sum += ceil($element / $x);
    }

    var_dump("sum ", $sum);

    return $sum;
}

function isMultipleOfThree($number) {
    return $number%3 === 0;
}

function minimizeArray($array, $n) {
    if ($n == 1) {
        return $array;
    }

    for ($k=0; $k<$n-1; $k++) {
        if (isMultipleOfThree($array[$k]) || isMultipleOfThree($array[$k+1])) {
            continue;
        }

        $array[$k] = $array[$k] + $array[$k+1];
        unset($array[$k+1]);
        $array = array_values($array);
        $k = 0;
    }

    // if ($n == 9) {
    //     var_dump($array);
    // }

    return $array;
}

for ($i=0; $i<$rowsNumber; $i++) {
    list($n, $x) = explode(' ', fgets($file));
    $array       = explode(' ', fgets($file));

    echo getArrayBeauty(minimizeArray($array, $n), $n, $x);
    echo " ";
    echo getArrayBeauty($array, $n, $x);
    echo "\n";
}

<?php

$file = fopen("php://stdin", "r");

$rowsNumber = fgets($file);

function getArrayBeauty($array, $n, $x) {
    $sum = 0;

    foreach($array as $element) {
        if ($n == 9) {
            var_dump($element / $x, ceil($element / $x));
        }
        $sum += ceil($element / $x);
    }

    return $sum;
}

function isMultipleOfThree($number) {
    return $number%3 === 0;
}

function minimizeArray($array, $n) {
    if ($n == 1) {
        return $array;
    }

    for ($k=0; $k<$n-1; $k++) {
        if (isMultipleOfThree($array[$k]) || isMultipleOfThree($array[$k+1])) {
            continue;
        }

        $array[$k] = $array[$k] + $array[$k+1];
        unset($array[$k+1]);
        $array = array_values($array);
        $k = 0;
    }

    // if ($n == 9) {
    //     var_dump($array);
    // }

    return $array;
}

for ($i=0; $i<$rowsNumber; $i++) {
    list($n, $x) = explode(' ', fgets($file));
    $array       = explode(' ', fgets($file));

    echo getArrayBeauty(minimizeArray($array, $n), $n, $x);
    echo " ";
    echo getArrayBeauty($array, $n, $x);
    echo "\n";
}

<?php

$file = fopen("php://stdin", "r");

$rowsNumber = fgets($file);

function getArrayBeauty($array, $n, $x) {
    $sum = 0;

    foreach($array as $element) {
        $sum += ceil($element / $x);
    }

    return $sum;
}

function isMultipleOfThree($number) {
    return $number%3 === 0;
}

function minimizeArray($array, $n) {
    if ($n == 1) {
        return $array;
    }

    for ($k=0; $k<$n-1; $k++) {
        if (isMultipleOfThree($array[$k]) || isMultipleOfThree($array[$k+1])) {
            continue;
        }

        $array[$k] = $array[$k] + $array[$k+1];
        unset($array[$k+1]);
        $array = array_values($array);
        $k = 0;
    }

    if ($n == 9) {
        var_dump($array); exit;
    }

    return $array;
}

for ($i=0; $i<$rowsNumber; $i++) {
    list($n, $x) = explode(' ', fgets($file));
    $array       = explode(' ', fgets($file));

    echo getArrayBeauty(minimizeArray($array, $n), $n, $x);
    echo " ";
    echo getArrayBeauty($array, $n, $x);
    echo "\n";
}

<?php
// ???
$file = fopen("php://stdin", "r");

$rowsNumber = fgets($file);

function getArrayBeauty($array, $n, $x) {
    $sum = 0;

    foreach($array as $element) {
        $sum += ceil($element / $x);
    }

    return $sum;
}

function isMultipleOfThree($number) {
    return $number%3 === 0;
}

function minimizeArray($array, $n) {
    if ($n == 1) {
        return $array;
    }

    for ($k=0; $k<$n-1; $k++) {
        if (isMultipleOfThree($array[$k]) || isMultipleOfThree($array[$k+1])) {
            continue;
        }

        $array[$k] = $array[$k] + $array[$k+1];
        unset($array[$k+1]);
        $array = array_values($array);
        $k = 0;
    }

    return $array;
}

for ($i=0; $i<$rowsNumber; $i++) {
    list($n, $x) = explode(' ', fgets($file));
    $array       = explode(' ', fgets($file));

    echo getArrayBeauty(minimizeArray($array, $n), $n, $x);
    echo " ";
    echo getArrayBeauty($array, $n, $x);
    echo "\n";
}

<?php

$file = fopen("php://stdin", "r");

$rowsNumber = fgets($file);

function getArrayBeauty($array, $n, $x) {
    $sum = 0;

    foreach($array as $element) {
        $sum += ceil($element / $x);
    }

    return $sum;
}

function isMultipleOfThree($number) {
    return $number%3 === 0;
}

function minimizeArray($array, $n) {
    if ($n == 1) {
        return $array;
    }

    for ($k=0; $k<$n-1; $k++) {
        if (isMultipleOfThree($array[$k]) || isMultipleOfThree($array[$k+1])) {
            continue;
        }

        $array[$k] = $array[$k] + $array[$k+1];
        unset($array[$k+1]);
        $array = array_values($array);
        $k = 0;
    }

    return $array;
}

for ($i=0; $i<$rowsNumber; $i++) {
    list($n, $x) = explode(' ', fgets($file));
    $array       = explode(' ', fgets($file));

    echo getArrayBeauty(minimizeArray($array, $n), $n, $x);
    echo " ";
    echo getArrayBeauty($array, $n, $x);
    echo "\n";
}

<?php
$fileName = 'php://stdin';
$file = fopen($fileName, "r");

$t = trim(fgets($file));
for ($i = 0; $i < $t; $i++) {
  list($n, $x) = explode(" ", trim(fgets($file)));
  $a = explode(" ", trim(fgets($file)));
  $m = 0; $k = 0;

  for ($j = 0; $j < $n; $j++) {
    $m += ceil($a[$j] / $x);
    $k += $a[$j];
  }
  $k = ceil($k / $x);
  $min = min($m, $k);
  $max = max($m, $k);
  echo $min.' '.$max.PHP_EOL;
}
?>

<?php
$fileName = 'php://stdin';
$file = fopen($fileName, "r");

$t = trim(fgets($file));
for ($i = 0; $i < $t; $i++) {
  list($n, $x) = explode(" ", trim(fgets($file)));
  $a = explode(" ", trim(fgets($file)));
  $m = 0; $k = 0;

  for ($j = 0; $j < $n; $j++) {
    $m += ceil($a[$j] / $x);
    $k += $a[$j];
  }
  $k = $k / $x;
  $min = min($m, $k);
  $max = max($m, $k);
  echo $min.' '.$max.PHP_EOL;
}
?>

<?php
function solve() {
    $s1 = trim(fgets(STDIN));
    list($n, $x) = explode(' ', $s1);
    $s = trim(fgets(STDIN));
    $a = preg_split("/[\s,]+/", $s);
    $min = ceil(array_sum($a) / $x);
    $max = 0;
    for ($i = 0; $i < $n; $i++) {
        $max += ceil($a[$i] / $x);
    }

    echo returnFloat($min)." ".returnFloat($max);
}

function returnFloat($bigint) {
    return floatval($bigint);
}

$t = trim(fgets(STDIN));
while ($t--) {
    solve();
    echo "\n";
}
?>

<?php
function solve() {
    $s1 = trim(fgets(STDIN));
    list($n, $x) = explode(' ', $s1);
    $s = trim(fgets(STDIN));
    $a = preg_split("/[\s,]+/", $s);
    $min = ceil(array_sum($a) / $x);
    $max = 0;
    for ($i = 0; $i < $n; $i++) {
        $max += ceil($a[$i] / $x);
    }

    echo returnInt($min)." ".returnInt($max);
}

function returnInt($bigint) {
    return number_format($bigint);
}

$t = trim(fgets(STDIN));
while ($t--) {
    solve();
    echo "\n";
}
?>

<?php
function solve() {
    $s1 = trim(fgets(STDIN));
    list($n, $x) = explode(' ', $s1);
    $s = trim(fgets(STDIN));
    $a = preg_split("/[\s,]+/", $s);
    $min = ceil(array_sum($a) / $x);
    $max = 0;
    for ($i = 0; $i < $n; $i++) {
        $max += ceil($a[$i] / $x);
    }

    echo returnInt($min)." ".returnInt($max);
}

function returnInt($i) {
    return strval($i);
}

$t = trim(fgets(STDIN));
while ($t--) {
    solve();
    echo "\n";
}
?>

<?php
function solve() {
    $s1 = trim(fgets(STDIN));
    list($n, $x) = explode(' ', $s1);
    $s = trim(fgets(STDIN));
    $a = preg_split("/[\s,]+/", $s);
    $min = ceil(array_sum($a) / $x);
    $max = 0;
    for ($i = 0; $i < $n; $i++) {
        $max += ceil($a[$i] / $x);
    }

    echo $min." ".$max;
}

$t = trim(fgets(STDIN));
while ($t--) {
    solve();
    echo "\n";
}
?>

<?php

$t = (int) trim(fgets(STDIN));
for ($i = 0; $i < $t; ++$i) {
    //i[]
    list($ni[], $xi[]) = array_map('intval', explode(' ', trim(fgets(STDIN))));
    $ai[] = explode(' ', trim(fgets(STDIN)));
}
for ($i = 0; $i < $t; ++$i) {
    $n = $ni[$i];
    $x = $xi[$i];
    $a = $ai[$i];
    $sum = 0;
    $cnt = 0;
    foreach ($a as $v) {
        $sum = bc1($sum, '+', $v);
        $cnt += ceil($v / $x);
    }
    echo cl($sum, $x).' '.$cnt.PHP_EOL;
}

function fl($x, $y = 1)
{
    return bcdiv(bcsub($x, bcmod($x, $y)), $y);
}
function cl($x, $y = 1)
{
    return bcadd(fl($x, $y), bcmod($x, $y) > 0);
}
function bc1($x, $e, $y = null)
{
    is_null($y) && list($e, $y) = explode(' ', trim($e));
    '+' == $e && $ans = bcadd($x, $y);
    '-' == $e && $ans = bcsub($x, $y);
    '*' == $e && $ans = bcmul($x, $y);
    '/' == $e && $ans = bcdiv($x, $y);
    '%' == $e && $ans = bcmod($x, $y);

    return $ans;
}

<?php

$t = (int) trim(fgets(STDIN));
for ($i = 0; $i < $t; ++$i) {
    //i[]
    list($ni[], $xi[]) = array_map('intval', explode(' ', trim(fgets(STDIN))));
    $ai[] = array_map('intval', explode(' ', trim(fgets(STDIN))));
}
for ($i = 0; $i < $t; ++$i) {
    $n = $ni[$i];
    $x = $xi[$i];
    $a = $ai[$i];
    $sum = 0;
    foreach ($a as $v) {
        $sum += ceil($v / $x);
    }
    echo ceil(array_sum($a) / $x).' '.$sum.PHP_EOL;
}


<?php
$STDIN = fopen('./data.txt', 'r');
define('STDIN', $STDIN);
$n = trim(fgets(STDIN));
if ($n == 1) {
    echo '-1';
} else {
    echo $n, ' ', $n + 1, ' ', $n * ($n + 1);
}
?>


<?
$stdin = fopen('php://stdin', 'r');
ini_set('display_errors', '1');
function read() {
    return trim(fgets(STDIN));
}
/////////////////

$n = read();
$arr = explode(" ", read());
$resStr = '';
$x = 0;
$resNum = 0;
arsort($arr);
foreach ($arr as $k=>$v) {
    $resStr .= ($k+1).' ';
    $resNum += $v*$x+1;
    $x++;
}

echo $resNum."\n";
echo $resStr."\n";


<?php

function isPrime($number)
{

    if ($number == 2 || $number == 3)
        return true;

    if ($number % 2 == 0 || $number % 3 == 0)
        return false;

    $divisor = 6;
    while ($divisor * $divisor - 2 * $divisor + 1 <= $number)
    {

        if ($number % ($divisor - 1) == 0)
            return false;

        if ($number % ($divisor + 1) == 0)
            return false;

        $divisor += 6;

    }

    return true;

}

function nextPrime($n)
{
    $n = $n + 1;
    while(!isPrime($n)){
        $n++;
    }

    return $n;
}

function gcd($a, $b)
{
    while($b != 0){
        $t = $a % $b;
        $a = $b;
        $b = $t;
    }
    return $a;
}

for($t = intval(trim(fgets(STDIN))); $t > 0; $t --) {
    $n = intval(trim(fgets(STDIN)));

    $array = explode(' ', trim(fgets(STDIN)));

    if($array[0] == 1 && $array[$n - 1] == $n){
        $new_arr = array_slice($array, 1, $n - 2);

        sort($new_arr);

        if($new_arr == array_slice($array, 1, $n - 2))
            echo "0\n";

        else
            echo "1\n";

        continue;

    }

    if($array[0] == 1 || $array[$n - 1] == $n){
        echo "1\n";
        continue;
    }

    if($array[0] == $n && $array[$n - 1] == 1){
        echo "3\n";
        continue;
    }

    echo "2\n";

}

<?php

function isPrime($number)
{

    if ($number == 2 || $number == 3)
        return true;

    if ($number % 2 == 0 || $number % 3 == 0)
        return false;

    $divisor = 6;
    while ($divisor * $divisor - 2 * $divisor + 1 <= $number)
    {

        if ($number % ($divisor - 1) == 0)
            return false;

        if ($number % ($divisor + 1) == 0)
            return false;

        $divisor += 6;

    }

    return true;

}

function nextPrime($n)
{
    $n = $n + 1;
    while(!isPrime($n)){
        $n++;
    }

    return $n;
}

function gcd($a, $b)
{
    while($b != 0){
        $t = $a % $b;
        $a = $b;
        $b = $t;
    }
    return $a;
}

for($t = intval(trim(fgets(STDIN))); $t > 0; $t --) {
    $n = intval(trim(fgets(STDIN)));

    $array = explode(' ', trim(fgets(STDIN)));

    if($array[0] == 1 && $array[$n - 1] == $n){
        $new_arr = array_slice($array, 1, $n - 2);

        sort($new_arr);

        if($new_arr == array_slice($array, 1, $n - 2))
            echo "0\n";

        else
            echo "1\n";

        continue;

    }

    if($array[0] == 1 || $array[$n - 1] == $n){
        echo "1\n";
        continue;
    }

    echo "2\n";

}


<?php
  $inp = preg_split('#\s+#', trim(file_get_contents("php://stdin")));
  $p = 0;
  $tests = intval($inp[$p++]);
  for($test=0;$test<$tests;$test++)
  {
    $n = intval($inp[$p++]);
    $m = intval($inp[$p++]);
    echo (1+($n>>1))." ".(1+($m>>1))."\n";
  }

<?php
$mod = 1000000007;
$intMaxValue = 2147483646; //2147483647

$loop = true;
$t = $loop ? IO::get() : 1;
while($t--) {
    solve();
}
function solve()
{

   list($n, $m) = IO::getArray();
   $ans = '';
    if($n == 3 && $m == 3) {
        $ans = '2 2';
    } elseif($n == 3 && $m == 2) {
       $ans = '2 2';
    } elseif($n == 2 && $m == 3) {
       $ans = '2 2';
    } else {
        $ans = sprintf('%s %s', $n, $m);
    }
    IO::pr($ans);
}

class IO {
    public static function get()
    {
        return trim(fgets(STDIN));
    }

    public static function getArray()
    {
        return explode(' ', trim(fgets(STDIN)));
    }
    public static function pr($s)
    {
        echo $s, PHP_EOL;
    }
    public static function prArray($s)
    {
        echo implode( ' ', $s), PHP_EOL;
    }
    public static function getMinValue($x, $y) {
        return $x > $y ? $y : $x;
    }
    public static function getMaxValue($x, $y) {
        return $x > $y ? $x : $y;
    }
}

<?php
$mod = 1000000007;
$intMaxValue = 2147483646; //2147483647

$loop = true;
$t = $loop ? IO::get() : 1;
while($t--) {
    solve();
}
function solve()
{

   list($n, $m) = IO::getArray();
   $ans = '';
    if($n == 3 && $m == 3) {
        $ans = '2 2';
    } elseif($n < 3 && $m < 3) {
         $ans = '1 1';
    } else {
        $ans = sprintf('%s %s', $n, $m);
    }
    IO::pr($ans);
}

class IO {
    public static function get()
    {
        return trim(fgets(STDIN));
    }

    public static function getArray()
    {
        return explode(' ', trim(fgets(STDIN)));
    }
    public static function pr($s)
    {
        echo $s, PHP_EOL;
    }
    public static function prArray($s)
    {
        echo implode( ' ', $s), PHP_EOL;
    }
    public static function getMinValue($x, $y) {
        return $x > $y ? $y : $x;
    }
    public static function getMaxValue($x, $y) {
        return $x > $y ? $x : $y;
    }
}

<?php
$mod = 1000000007;
$intMaxValue = 2147483646; //2147483647

$loop = true;
$t = $loop ? IO::get() : 1;
while($t--) {
    solve();
}
function solve()
{

   list($n, $m) = IO::getArray();
   $ans = '';
    if($n == 3 && $m == 3) {
        $ans = '2 2';
    } elseif($n < 3 && $m < 3) {
         $ans = '1 1';
    } else {
        $ans = sprintf('%s %s', $n-1, $m-1);
    }
    IO::pr($ans);
}

class IO {
    public static function get()
    {
        return trim(fgets(STDIN));
    }

    public static function getArray()
    {
        return explode(' ', trim(fgets(STDIN)));
    }
    public static function pr($s)
    {
        echo $s, PHP_EOL;
    }
    public static function prArray($s)
    {
        echo implode( ' ', $s), PHP_EOL;
    }
    public static function getMinValue($x, $y) {
        return $x > $y ? $y : $x;
    }
    public static function getMaxValue($x, $y) {
        return $x > $y ? $x : $y;
    }
}

<?php
$mod = 1000000007;
$intMaxValue = 2147483646; //2147483647

$loop = true;
$t = $loop ? IO::get() : 1;
while($t--) {
    solve();
}
function solve()
{

   list($n, $m) = IO::getArray();
   $ans = '';
    if($n == 3 && $m == 3) {
        $ans = '2 2';
    } elseif($n < 3 || $m < 3) {
         $ans = '1 1';
    } else {
        $ans = sprintf('%s %s', $n-1, $m-1);
    }
    IO::pr($ans);
}

class IO {
    public static function get()
    {
        return trim(fgets(STDIN));
    }

    public static function getArray()
    {
        return explode(' ', trim(fgets(STDIN)));
    }
    public static function pr($s)
    {
        echo $s, PHP_EOL;
    }
    public static function prArray($s)
    {
        echo implode( ' ', $s), PHP_EOL;
    }
    public static function getMinValue($x, $y) {
        return $x > $y ? $y : $x;
    }
    public static function getMaxValue($x, $y) {
        return $x > $y ? $x : $y;
    }
}


<?php
list($n,$d) = explode(' ',trim(fgets(STDIN)));
list($a,$b) = explode(' ',trim(fgets(STDIN)));

$c = array();
for($i=1;$i<=$n;$i++){
 list($x,$y) = explode(' ',trim(fgets(STDIN)));
 $c[$i] = $a*$x+$b*$y;
}
asort($c);

$s = 0; $r = array();
foreach($c as $k=>$v){
 if($s+$v>$d) break;
 $s += $v;
 $r[] = $k;
}
print count($r)."\r\n".implode(' ',$r);

?>


<?php
   fscanf(STDIN, "%d",$s);
    $arreglo1 =array();
    $arreglo2 =array();
    $arreglo3 =array();
    $var;
   for ($i=0; $i <$s ; $i++) {
      fscanf(STDIN, "%s %s",$let1, $let2);
      if($let2=="rat")
      {
         array_push($arreglo1, $let1);
      }
      else if($let2=="woman")
      {
        array_push($arreglo2, $let1);
      }else if($let2=="child")
      {
        array_push($arreglo2, $let1);
      }else if($let2=="man")
      {
        array_push($arreglo3,$let1);
      }else
      {
        $var=$let1;
      }
   }
   foreach ($arreglo1 as $it) {
       echo $it."\n";
   }
   foreach ($arreglo2 as $it) {
       echo $it."\n";
   }
   foreach ($arreglo3 as $it) {
       echo $it."\n";
   }
   echo $var."\n";
 ?>

<?
$n=trim(fgets(STDIN));
for($i=0;$i<$n;$i++){
    $arr[]=trim(fgets(STDIN));
}
foreach($arr as $people){
    $a[]=explode(' ',$people);
}
foreach($a as $val){
    if($val[1]=='rat')
        echo $val[0]."\n";
}
foreach($a as $val){
    if($val[1]=='child' || $val[1]=='woman')
        echo $val[0]."\n";
}
foreach($a as $val){
    if($val[1]=='man')
        echo $val[0]."\n";
}
foreach($a as $val){
    if($val[1]=='captain')
        echo $val[0];
}
?>

<?php

$t = trim(fgets(STDIN));

$r = $w = $m = $c = array();
for($i=0;$i<$t;$i++){
 list($n,$p) = explode(' ',trim(fgets(STDIN)));
 switch($p){
  case 'captain': $c[] = $n; break;
  case 'woman': $w[] = $n; break;
  case 'man': $m[] = $n; break;
  case 'child': $w[] = $n; break;
  case 'rat': $r[] = $n; break;
 }
}

foreach($r as $v) print $v."\r\n";
foreach($w as $v) print $v."\r\n";
foreach($m as $v) print $v."\r\n";
foreach($c as $v) print $v."\r\n";

?>

<?
$fp=fopen('php://stdin', 'r');
$lines=stream_get_contents($fp);

$lines=trim($lines);
$lines=explode("\n", $lines);

$rat=array();
$woman_child=array();
$man=array();
$captain="";
$res=array();
for ($i=1; $i<count($lines); $i++)
{
    list ($name, $status)=explode(" ", trim($lines[$i]));
    if ($status=="rat")
        $rat[]=$name;
    if ($status=="woman" or $status=="child")
        $woman_child[]=$name;
    if ($status=="man")
        $man[]=$name;
    if ($status=="captain")
        $captain=$name;
}

foreach($rat as $name)
    $res[]=$name;
foreach($woman_child as $name)
    $res[]=$name;
foreach($man as $name)
    $res[]=$name;
$res[]=$captain;
$res=implode("\n", $res);
echo $res;
?>

<?php

/*
 * Secdra @2020
 */

class Member
{
    public $name;
    public $category;
    public static $priority = [
        'rat' => 1,
        'woman' => 2,
        'child' => 2,
        'man' => 3,
        'captain' => 4,
    ];

    public function __construct($name, $category)
    {
        $this->name = $name;
        $this->category = self::$priority[$category];
    }
}

fscanf(STDIN, "%d", $n);
$data = [];
for ($i = 0; $i < $n; $i++) {
    fscanf(STDIN, "%s %s", $name, $category);
    $member = new Member(trim($name), trim($category));
    $data[$member->category][] = $member->name;
}

ksort($data);
foreach ($data as $category => $name) {
    printf("%s\n", implode("\n", $name));
}

<?php
/*
 * Secdra @2020
 */

fscanf(STDIN, "%d", $n);
$data = [];
for ($i = 0; $i < $n; $i++) {
    fscanf(STDIN, "%s %s", $name, $category);
    if ($category == 'woman' || $category == 'child') $category = 'wc';
    $data[$category][] = $name;
}
if (isset($data['rat'])) {
    foreach ($data['rat'] as $member) {
        printf("%s\n", $member);
    }
}
if (isset($data['wc'])) {
    foreach ($data['wc'] as $member) {
        printf("%s\n", $member);
    }
}
if (isset($data['man'])) {
    foreach ($data['man'] as $member) {
        printf("%s\n", $member);
    }
}
if (isset($data['captain'])) {
    foreach ($data['captain'] as $member) {
        printf("%s\n", $member);
    }
}

<?php
fscanf(STDIN,"%d",$n);
$name=array();
$status=array();
$rat=array();
$womanChild=array();
$man=array();
$captain=array();
$r=0;
$wc=0;
$m=0;
$c=0;
for ($i=0; $i <$n; $i++)
 {
	fscanf(STDIN,"%s %s",$name[$i],$status[$i]);
	if($status[$i]=='rat')
		$rat[$r++]=$name[$i];
	elseif ($status[$i]=='woman'||$status[$i]=='child')
	{
		$womanChild[$wc++]=$name[$i];
	}
	elseif ($status[$i]=='man')
	 {
		$man[$m++]=$name[$i];
	 }
	 else
	 	$captain[$c++]=$name[$i];

 }
 for($i=0;$i<count($rat);$i++)
 echo "$rat[$i]\n";
for($i=0;$i<count($womanChild);$i++)
 echo "$womanChild[$i]\n";
for($i=0;$i<count($man);$i++)
 echo "$man[$i]\n";
for($i=0;$i<count($captain);$i++)
 echo "$captain[$i]\n";

<?php
    $n = intval(trim(fgets(STDIN)));

    $rev = array("captain" => "captain", "man" => "man", "rat" => "rat", "woman" => "mid", "child" => "mid");
    $peoples = array("captain" => array(), "man" => array(), "rat" => array(), "mid" => array());

    for ($i = 0; $i < $n; ++$i) {
        $str = trim(fgets(STDIN));
        $strs = split(" ", $str);

        $peoples[$rev[$strs[1]]][] = $strs[0];
    }

    krsort($peoples);
    foreach ($peoples as $key => $value) {
        foreach ($value as $name) {
            echo $name."\n";
        }
    }
?>

<?php

/*
 * Secdra @2020
 */

class Member
{
    public $name;
    public $category;
    public static $priority = [
        'rat' => 1,
        'woman' => 2,
        'child' => 2,
        'man' => 3,
        'captain' => 4,
    ];

    public function __construct($name, $category)
    {
        $this->name = $name;
        $this->category = $category;
    }
}

function cmp($mem1, $mem2)
{
    return Member::$priority[$mem1->category] > Member::$priority[$mem2->category];
}

fscanf(STDIN, "%d", $n);
$data = [];
for ($i = 0; $i < $n; $i++) {
    fscanf(STDIN, "%s %s", $name, $category);
    $data[] = new Member($name, $category);
}
usort($data, "cmp");
foreach ($data as $member) {
    printf("%s\n", $member->name);
}

<?php
/*
 * Secdra @2020
 */

fscanf(STDIN, "%d", $n);
$data = [];
for ($i = 0; $i < $n; $i++) {
    fscanf(STDIN, "%s %s", $name, $category);
    $data[$category][] = $name;
}
if (isset($data['rat'])) {
    foreach ($data['rat'] as $member) {
        printf("%s\n", $member);
    }
}
if (isset($data['woman'])) {
    foreach ($data['woman'] as $member) {
        printf("%s\n", $member);
    }
}
if (isset($data['child'])) {
    foreach ($data['child'] as $member) {
        printf("%s\n", $member);
    }
}
if (isset($data['man'])) {
    foreach ($data['man'] as $member) {
        printf("%s\n", $member);
    }
}
if (isset($data['captain'])) {
    foreach ($data['captain'] as $member) {
        printf("%s\n", $member);
    }
}

<?
$n=trim(fgets(STDIN));
for($i=0;$i<$n;$i++){
	$arr[]=trim(fgets(STDIN));
}
/* $n=6;
$arr=array( 'Jack captain',
			'Alice woman',
			'Charlie man',
			'Teddy rat',
			'Bob child',
			'Julia woman',
			'Kos man'
		); */
foreach($arr as $people){
	$a[]=explode(' ',$people);
}
foreach($a as $val){
	if($val[1]=='rat')
		echo $val[0]."
";
}
foreach($a as $val){
	if($val[1]=='child' || $val[1]=='woman')
		echo $val[0]."
";
}
foreach($a as $val){
	if($val[1]=='man')
		echo $val[0]."
";
}
foreach($a as $val){
	if($val[1]=='captain')
		echo $val[0];
}
?>

<?php

$t = trim(fgets(STDIN));

$r = $w = $m = $c = array();
for($i=0;$i<$t;$i++){
 list($n,$p) = explode(' ',trim(fgets(STDIN)));
 switch($p){
  case 'captain': $c[] = $n; break;
  case 'woman': $w[] = $n; break;
  case 'man': $m[] = $n; break;
  case 'child': $w[] = $n; break;
  case 'rat': $r[] = $n; break;
 }
}

print implode("\r\n",$r)."\r\n".implode("\r\n",$w)."\r\n".implode("\r\n",$m)."\r\n".implode("\r\n",$c);
?>


<?php
    $n = trim(fgets(STDIN));

    for ($i = 0; $i < $n; $i++) {
    list($a, $b) = explode(" ", trim(fgets(STDIN)));

    if ($a==0 || $b ==0) {
        if ($a * 2 == $b) {
            echo "YES\n";
        } else {
             echo "NO\n";
        }
    } else {
        if ($a != $b) {
            if ($a > $b) {
                $max = $a;
                $min = $b;
            } else {
                $max = $b;
                $min = $a;
            }

            $iSum = $max - (($max - $min) * 2);
        } else {
            $iSum = $a;
        }

        if ($iSum >= 0 && $iSum%3 == 0 ) {
            echo "YES\n";
        } else {
            echo "NO\n";
        }
    }

}

?>

<?php
    $n = trim(fgets(STDIN));

    for ($i = 0; $i < $n; $i++) {
    list($a, $b) = explode(" ", trim(fgets(STDIN)));
    if ($a == $b && $a ==0) {
        echo "NO\n";
    } else{
    if ($a != $b) {
        if ($a > $b) {
            $max = $a;
            $min = $b;
        } else {
            $max = $b;
            $min = $a;
        }

        $iSum = $max - (($max - $min) * 2);
    } else {
        $iSum = $a;
    }

    if ($iSum%3 == 0) {
        echo "YES\n";
    } else {
        echo "NO\n";
    }}
}

?>

<?php
    $n = trim(fgets(STDIN));

    for ($i = 0; $i < $n; $i++) {
        list($a, $b) = explode(" ", trim(fgets(STDIN)));
if ($a != $b) {
    if ($a > $b) {
        $max = $a;
        $min = $b;
    } else {
        $max = $b;
        $min = $a;
    }

    $iSum = $max - (($max - $min) * 2);

} else {
    $iSum = $a;
}

if ($iSum%3 == 0) {
        echo "YES\n";
} else {
        echo "NO\n";
 }
    }

?>

<?php
    $n = trim(fgets(STDIN));

    for ($i = 0; $i < $n; $i++) {
    list($a, $b) = explode(" ", trim(fgets(STDIN)));

    if ($a==0 || $b ==0) {
        if ($a * 2 == $b) {
            echo "YES\n";
        } else {
             echo "NO\n";
        }
    } else {
        if ($a != $b) {
            if ($a > $b) {
                $max = $a;
                $min = $b;
            } else {
                $max = $b;
                $min = $a;
            }

            $iSum = $max - (($max - $min) * 2);
        } else {
            $iSum = $a;
        }

        if ($iSum%3 == 0) {
            echo "YES\n";
        } else {
            echo "NO\n";
        }
    }

}

?>

<?php
    $n = trim(fgets(STDIN));

    for ($i = 0; $i < $n; $i++) {
        list($a, $b) = explode(" ", trim(fgets(STDIN)));
        if (abs($a - $b) % 2 == 0) {
            echo 'NO'."\n";
        } else {
            echo 'YES'."\n";
        }
    }

?>

<?php
    $n = trim(fgets(STDIN));

    for ($i = 0; $i < $n; $i++) {
    list($a, $b) = explode(" ", trim(fgets(STDIN)));
    if ( $a ==0 || $b == 0) {
        echo "NO\n";
    } else{
    if ($a != $b) {
        if ($a > $b) {
            $max = $a;
            $min = $b;
        } else {
            $max = $b;
            $min = $a;
        }

        $iSum = $max - (($max - $min) * 2);
    } else {
        $iSum = $a;
    }

    if ($iSum%3 == 0) {
        echo "YES\n";
    } else {
        echo "NO\n";
    }}
}

?>


<?php
list($n, $m, $k) = explode(" ", trim(fgets(STDIN)));
$b = array();
$imax = 0;
for($i = 0; $i <= $m; $i++){
    $iVal = (int)trim(fgets(STDIN));
    $sBin = "" . decbin($iVal);
    $b[$i] = $sBin;
}

$iResult = 0;
for($i = 0; $i < $m; $i++){

    if(strlen($b[$i]) > strlen($b[$m])){
        $sTest = $b[$i];
        $sTest2 = str_pad($b[$m], strlen($b[$i]), "0", STR_PAD_LEFT);
    }else{
        $sTest = str_pad($b[$i], strlen($b[$m]), "0", STR_PAD_LEFT);
        $sTest2 = $b[$m];
    }

    $aTest = str_split($sTest);
    $aTest2 = str_split($sTest2);
    $iRep = 0;
    foreach($aTest2 as $iK => $iV){
        if($aTest[$iK] != $iV ){
            $iRep++;
        }
    }

    if($iRep <= $k){
        $iResult++;
    }
}

echo $iResult;

<?php
// 467B     Федя и новая игра
$in = fopen("php://stdin", 'r');

$input_line = fgets($in);
list($n, $m, $k) = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);
$n = (int) $n;
$m = (int) $m;
$k = (int) $k;

$arr_X = array();
for ($i=1; $i <= $m+1 ; $i++) {
    $input_line = fgets($in);
    $arr_X[$i] = (int) $input_line;
}

$col = 0;
$f = $arr_X[$m+1];

for ($i=1; $i <= $m ; $i++) {
    $x = $arr_X[$i];

    $bit = $x ^ $f;
    $col_bit = 0;
    while ($bit !== 0)  {
        if ($bit == ($bit | 1 )) $col_bit ++;
        $bit = $bit >> 1;
    }

    // echo "x=$x col_bit=$col_bit\n";
    if ($col_bit <= $k) {
        $col ++;
    }
}

echo "$col\n";

?>

<?php
$t = fgets(STDIN);
$t = explode(' ', $t);

// виды солдат
$n = $t[0];
// игроки
$m = $t[1];
// отличие
$k = $t[2];

$i = $m+1;

$sCount = 0;

$aArm = array();
for($j=1;$j<=$i;$j++) {
    $x = fgets(STDIN);
    $x = decbin($x);
    $aArms[$j] = str_pad($x, $n, "0", STR_PAD_LEFT);
    //$x = str_pad($x, $n, "0", STR_PAD_LEFT);
}

$sVedaArm = $aArms[$i];
unset($aArms[$i]);
//echo $sVedaArm."\n";

//print_r($aArms);

foreach($aArms as $sKey => $sArm) {
      $sOtl = 0;
      for($j=0;$j<=$n-1;$j++) {
        if($sArm[$j] != $sVedaArm[$j]) {
            $sOtl++;
        }
      }
      //echo $sOtl."\n";

      if($k >= $sOtl) {
          $sCount++;
      }
}
//echo "\n";
echo $sCount;
?>

<?php
list($n, $m, $k) = explode(" ", trim(fgets(STDIN)));
$b = array();

for($i = 0; $i <= $m; $i++){
    $iVal = (int)trim(fgets(STDIN));
    $sBin = "" . decbin($iVal);
    $a = str_split($sBin);
    $iCount = count(array_keys($a, '1'));

    //echo $iVal . '=' . $sBin . '=' . $iCount . ' ';
    $b[$i] = $iCount;
}

$iResult = 0;
for($i = 0; $i < $m; $i++){
    if(abs($b[$i] - $b[$m]) <= $k){
        $iResult++;
    }
}

echo $iResult;

<?php
$t = fgets(STDIN);
$t = explode(' ', $t);

// виды солдат
$n = $t[0];
// игроки
$m = $t[1];
// отличие
$k = $t[2];

$i = $m+1;

$sCount = 0;

$aArm = array();
for($j=1;$j<=$i;$j++) {
    $x = fgets(STDIN);
    $x = decbin($x);
    $aArms[$j] = sprintf("%0".$n."d", $x);
}

$sVedaArm = $aArms[$i];
unset($aArms[$i]);
//echo $sVedaArm."\n";

//print_r($aArms);

foreach($aArms as $sKey => $sArm) {
      $sOtl = 0;
      for($j=0;$j<=$n-1;$j++) {
        if($sArm[$j] != $sVedaArm[$j]) {
            $sOtl++;
        }
      }
      //echo $sOtl."\n";

      if($k >= $sOtl) {
          $sCount++;
      }
}
//echo "\n";
echo $sCount;
?>

<?php
$t = fgets(STDIN);
$t = explode(' ', $t);

// виды солдат
$n = $t[0];
// игроки
$m = $t[1];
// отличие
$k = $t[2];

$i = $m+1;

$sCount = 0;

$aArm = array();
for($j=1;$j<=$i;$j++) {
    $x = fgets(STDIN);
    $x = decbin($x);
    $aArms[$j] = sprintf("%0".$n."d", $x);
    unset($aArms[$i]);
}

$sVedaArm = $aArms[$i];

foreach($aArms as $j => $sArm) {
      $sOtl = 0;
      for($j=1;$j<=$n;$j++) {
        if($sArm[$j] != $sVedaArm[$j]) {
            $sOtl++;
        }
      }
      //echo $sOtl."\n";

      if($k >= $sOtl) {
          $sCount++;
      }
}
//echo "\n";
echo $sCount;
?>

<?php
$t = fgets(STDIN);
$t = explode(' ', $t);

// виды солдат
$n = $t[0];
// игроки
$m = $t[1];
// отличие
$k = $t[2];

$i = $m+1;

$sCount = 0;

$aArm = array();
for($j=1;$j<=$i;$j++) {
    $x = fgets(STDIN);
    $x = decbin($x);
    $aArms[$j] = sprintf("%0".$n."d", $x);
    unset($aArms[$i]);
}

$sVedaArm = $aArms[$i];

foreach($aArms as $j => $sArm) {
      $sOtl = 0;
      for($j=1;$j<=$n;$j++) {
        if($sArm[$j] != $sVedaArm[$j]) {
            $sOtl++;
        }
      }
      //echo $sOtl."\n";

      if($k >= $sOtl) {
          $sCount++;
      }
}
//echo "\n";
//echo $sCount;
?>

<?php
$t = fgets(STDIN);
$t = explode(' ', $t);

// виды солдат
$n = $t[0];
// игроки
$m = $t[1];
// отличие
$k = $t[2];

$i = $m+1;

$sCount = 0;

$aArm = array();
for($j=1;$j<=$i;$j++) {
    $x = fgets(STDIN);
    $x = decbin($x);
    $aArms[$j] = sprintf("%0".$n."d", $x);
}

$sVedaArm = $aArms[$i];
unset($aArms[$i]);
echo $sVedaArm."\n";

print_r($aArms);

foreach($aArms as $sKey => $sArm) {
      $sOtl = 0;
      for($j=0;$j<=$n-1;$j++) {
        if($sArm[$j] != $sVedaArm[$j]) {
            $sOtl++;
        }
      }
      echo $sOtl."\n";

      if($k >= $sOtl) {
          $sCount++;
      }
}
echo "\n";
echo $sCount;
?>

<?php
$t = fgets(STDIN);
$t = explode(' ', $t);

// виды солдат
$n = $t[0];
// игроки
$m = $t[1];
// отличие
$k = $t[2];

$i = $m+1;

$sCount = 0;

$aArm = array();
for($j=1;$j<=$i;$j++) {
    $x = fgets(STDIN);
    $x = decbin($x);
    $aArms[$j] = sprintf("%0".$n."d", $x);
}

$sVedaArm = $aArms[$i];
unset($aArms[$i]);
//echo $sVedaArm."\n";

//print_r($aArms);

foreach($aArms as $sKey => $sArm) {
      $sOtl = 0;
      for($j=0;$j<=$n;$j++) {
        if($sArm[$j] != $sVedaArm[$j]) {
            $sOtl++;
        }
      }
      //echo $sOtl."\n";

      if($k >= $sOtl) {
          $sCount++;
      }
}
//echo "\n";
echo $sCount;
?>

<?php
$t = fgets(STDIN);
$t = explode(' ', $t);

// виды солдат
$n = $t[0];
// игроки
$m = $t[1];
// отличие
$k = $t[2];

$i = $m+1;

$sCount = 0;

$aArm = array();
for($j=1;$j<=$i;$j++) {
    $x = fgets(STDIN);
    $x = decbin($x);
    $aArms[$j] = sprintf("%0".$n."d", $x);
}

$sVedaArm = $aArms[$i];
foreach($aArms as $j => $sArm) {
  if($sArm != $sVedaArm) {
      $sOtl = 0;
      for($j=1;$j<=$n;$j++) {
        if($sArm[$j] != $sVedaArm[$j]) {
            $sOtl++;
        }
      }
      if($k >= $sOtl) {
          $sCount++;
      }
  }
}

echo $sCount;
?>

<?php
$t = fgets(STDIN);
$t = explode(' ', $t);

// виды солдат
$n = $t[0];
// игроки
$m = $t[1];
// отличие
$k = $t[2];

$i = $m+1;

$sCount = 0;

$aArm = array();
for($j=1;$j<=$i;$j++) {
    $x = fgets(STDIN);
    $x = decbin($x);
    $aArms[$j] = sprintf("%0".$n."d", $x);
}

$sVedaArm = $aArms[$i];
unset($aArms[$i]);

foreach($aArms as $j => $sArm) {
      $sOtl = 0;
      for($j=1;$j<=$n;$j++) {
        if($sArm[$j] != $sVedaArm[$j]) {
            $sOtl++;
        }
      }
      //echo $sOtl."\n";

      if($k >= $sOtl) {
          $sCount++;
      }
}
//echo "\n";
echo $sCount;
?>

<?php
$t = fgets(STDIN);
$t = explode(' ', $t);

// виды солдат
$n = $t[0];
// игроки
$m = $t[1];
// отличие
$k = $t[2];

$i = $m+1;

$sCount = 0;

$aArm = array();
for($j=1;$j<=$i;$j++) {
    $x = fgets(STDIN);
    $x = decbin($x);
    $aArms[$j] = sprintf("%0".$n."d", $x);
}

$sVedaArm = $aArms[$i];
unset($aArms[$i]);
echo $sVedaArm."\n";

print_r($aArms);

foreach($aArms as $sKey => $sArm) {
      $sOtl = 0;
      for($j=0;$j<=$n;$j++) {
        if($sArm[$j] != $sVedaArm[$j]) {
            $sOtl++;
        }
      }
      echo $sOtl."\n";

      if($k >= $sOtl) {
          $sCount++;
      }
}
echo "\n";
echo $sCount;
?>

<?php
$t = fgets(STDIN);
$t = explode(' ', $t);

// виды солдат
$n = $t[0];
// игроки
$m = $t[1];
// отличие
$k = $t[2];

$i = $m+1;

$sCount = 0;

$aArm = array();
for($j=1;$j<=$i;$j++) {
    $x = fgets(STDIN);
    $x = decbin($x);
    $aArms[$j] = sprintf("%0".$n."d", $x);
}

$sVedaArm = $aArms[$i];
unset($aArms[$i]);
//echo $sVedaArm;

//print_r($aArms);

foreach($aArms as $sKey => $sArm) {
      $sOtl = 0;
      for($j=1;$j<=$n;$j++) {
        if($sArm[$j] != $sVedaArm[$j]) {
            $sOtl++;
        }
      }
      //echo $sOtl."\n";

      if($k > $sOtl) {
          $sCount++;
      }
}
//echo "\n";
echo $sCount;
?>

<?php
    list($n, $m, $k) = explode(' ', trim(fgets(STDIN)));
    $arr = array();
    for ($j=1; $j <= $m+1; $j++) {
        $arr[] = trim(fgets(STDIN));
    }

    for ($u=0; $u <= $m; $u++) {
        $dec = $arr[$u];
        $binary = array();
        do{
        if($dec % 2) {
            $binary[] = 1;
        }
        else {
            $binary[] = 0;
        }
        $dec = (int)($dec/2);
        } while ($dec);
        $array[] = $binary;
    }
    $array = array_reverse($array);
    $b = 0;
    for ($i=0; $i < $m; $i++) {
        $l = 0;
        $count = count($array[$m]) - count($array[$i]);
        if(-$k <= $count && $count<= $k) {
            if(count($array[$m]) <= count($array[$i])) {
                for($p=0; $p < count($array[$m]); $p++){
                    if($array[$m][$p] === $array[$i][$p]) {
                        $l++;
                    }
                }
                if(count($array[$i]) - $l <= $k) {
                    $b++;
                }
            } else {
                for($p=0; $p < count($array[$i]); $p++){
                    if($array[$m][$p] === $array[$i][$p]) {
                        $l++;
                    }
                }
                if(count($array[$m]) - $l <= $k) {
                    $b++;
                }
            }
        }
    }

    echo $b;

?>


<?php

fscanf(STDIN, '%u', $t);

while ($t--) {
    [$a, $b, $c] = fscanf(STDIN, '%d %d %d');

    if ($a + $b === $c || $a + $c === $b || $b + $c === $a) {
        echo 'YES' . PHP_EOL;
    } else {
        echo 'NO' . PHP_EOL;
    }
}

<?php

$n = (int) trim(fgets(STDIN));

for ($index = 0; $index < $n; $index++)
{
	list($a, $b, $c) = explode(' ', trim(fgets(STDIN)));

	if($a + $b == $c || $a + $c == $b || $b + $c == $a) {
		echo("YES") . PHP_EOL;

		continue;
	}

	echo("NO") . PHP_EOL;
}

<?php

$n = (int) trim(fgets(STDIN));

for ($index = 0; $index < $n; $index++)
{
	list($a, $b, $c) = explode(' ', trim(fgets(STDIN)));

	if($a + $b == $c || $a + $c == $b || $b + $c == $a) {
		echo("YES");

		continue;
	}

	echo("NO");
}


<?php
$n = intval(fgets(STDIN));
$arMinus = array();
$arPlus = array();
for ($i=1;$i<=$n;$i++)
{
   $str = trim(fgets(STDIN));
   list($key, $val) = explode(' ', $str);
   if ($key < 0)
    $arMinus[abs($key)] = $val;
    else
    $arPlus[abs($key)] = $val;
}

ksort($arMinus);
ksort($arPlus);

$minCount = (count($arMinus) < count($arPlus)) ? count($arMinus) : count($arPlus);

$rM = reset($arMinus);
$rP = reset($arPlus);

$total = $rM + $rP;

if ($rM > 0 && $rP > 0)
    do
    {
        $nP = next($arPlus);
        $nM = next($arMinus);
        $total = $total + $nP + $nM;
    } while ($nP > 0 && $nM > 0);

echo $total;

<?php
$a = trim(fgets(STDIN));
$b = array();
$c = array();
$d = array();
$e = array();
for($x = 0; $x < $a; $x++)
{
    list($f, $g) = explode(" ", trim(fgets(STDIN)));
    if($f < 0)
    {
        array_push($b, $f);
        array_push($c, $g);
    }
    elseif($f > 0)
    {
        array_push($d, $f);
        array_push($e, $g);
    }
}
$h = $b;
arsort($h);
$i = array_keys($h);
$j = array();
for($x = 0; $x < count($b); $x++)
{
    $j[$x] = $c[$i[$x]];
}
$k = $d;
asort($k);
$l = array_keys($k);
$m = array();
for($x = 0; $x < count($d); $x++)
{
    $m[$x] = $e[$l[$x]];
}
if(count($j) < count($m))
{
    $n = array_sum($j);
    $o = array_sum(array_slice($m, 0, count($j) + 1));
    print $n + $o;
}
elseif(count($j) == count($m))
{
    $n = array_sum($j);
    $o = array_sum($m);
    print $n + $o;
}
elseif(count($j) > count($m))
{
    $n = array_sum(array_slice($j, 0, count($m) + 1));
    $o = array_sum($m);
    print $n + $o;
}
?>

<?php
    $n = trim(fgets(STDIN));
    $max_right_length = 0;
    $max_left_length = 0;
    $a_left = array();
    $a_right = array();
    for($i=0;$i<$n;$i++){
        $a = explode(" ", trim(fgets(STDIN)));
        $arr[$a[0]] = $a[1];
        if($a[0] < 0){
            $max_left_length ++;
            $a_left[$a[0]] = $a[1];
        }
        if($a[0] > 0){
            $max_right_length ++;
            $a_right[$a[0]] = $a[1];
        }
    }
    //print_r($a_left);
    ksort($a_left);
    ksort($a_right);
    //print_r($a_left);
    $a_left = array_values($a_left);
    $a_right = array_values($a_right);
    //print_r($a_left);
    //print_r(array_slice($a_right, $max_left_length+1));
    //echo $max_left_length." ".$max_right_length."\n";
    if($max_left_length > $max_right_length){
        $sum = array_sum($a_right) + array_sum(array_slice($a_left, -$max_right_length-1));
        echo $sum+(isset($arr[0]) ? $arr[0] : 0);
    }else if ($max_left_length < $max_right_length) {
        $sum = array_sum($a_left) + array_sum(array_slice($a_right, 0, $max_left_length+1));
        echo $sum+(isset($arr[0]) ? $arr[0] : 0);
    }else{
        echo array_sum($arr);
    }
    echo  "\n";

<?php
$a = trim(fgets(STDIN));
$b = array();
$c = array();
$d = array();
$e = array();
for($x = 0; $x < $a; $x++)
{
    list($f, $g) = explode(" ", trim(fgets(STDIN)));
    if($f < 0)
    {
        array_push($b, $f);
        array_push($c, $g);
    }
    elseif($f > 0)
    {
        array_push($d, $f);
        array_push($e, $g);
    }
}
$h = $b;
arsort($h);
$i = array_keys($h);
$j = array();
for($x = 0; $x < count($b); $x++)
{
    $j[$x] = $c[$i[$x]];
}
$k = $d;
sort($k);
$l = array_keys($k);
$m = array();
for($x = 0; $x < count($d); $x++)
{
    $m[$x] = $e[$l[$x]];
}
if(count($j) < count($m))
{
    $n = array_sum($j);
    $o = array_sum(array_slice($m, 0, count($j) + 1));
    print $n + $o;
}
elseif(count($j) == count($m))
{
    $n = array_sum($j);
    $o = array_sum($m);
    print $n + $o;
}
elseif(count($j) > count($m))
{
    $n = array_sum(array_slice($j, 0, count($m) + 1));
    $o = array_sum($m);
    print $n + $o;
}
?>

$count = trim(fgets(STDIN));
$nom = trim(fgets(STDIN));
var_dump($count, $nom);

$count = trim(fgets(STDIN));
//echo($count);

$cot = fgets(STDIN);
$count = trim(fgets(STDIN));
$elementsStr = trim(fgets(STDIN));
echo "1";

<?php
    $count = trim(fgets(STDIN));
    $nom = explode(" ", trim(fgets(STDIN)));
    var_dump($count, $nom);

$count = trim(fgets(STDIN));
var_dump((int)$count);

$count = trim(fgets(STDIN));
echo((int)$count);

$count = trim(fgets(STDIN));
var_dump($count);

<?php
    $n = trim(fgets(STDIN));
    $max_right_length = 0;
    $max_left_length = 0;
    $a_left = array();
    $a_right = array();
    for($i=0;$i<$n;$i++){
        $a = explode(" ", trim(fgets(STDIN)));
        $arr[$a[0]] = $a[1];
        if($a[0] < 0){
            $max_left_length ++;
            $a_left[$a[0]] = $a[1];
        }
        if($a[0] > 0){
            $max_right_length ++;
            $a_right[$a[0]] = $a[1];
        }
    }
    //print_r($a_left);
    ksort($a_left);
    ksort($a_right);
    //print_r($a_left);
    $a_left = array_values($a_left);
    $a_right = array_values($a_right);
    //print_r($a_left);
    //print_r(array_slice($a_right, $max_left_length+1));
    echo $max_left_length." ".$max_right_length."\n";
    if($max_left_length > $max_right_length){
        $sum = array_sum($a_right) + array_sum(array_slice($a_left, -$max_right_length-1));
        echo $sum+(isset($arr[0]) ? $arr[0] : 0);
    }else if ($max_left_length < $max_right_length) {
        $sum = array_sum($a_left) + array_sum(array_slice($a_right, 0, $max_left_length+1));
        echo $sum+(isset($arr[0]) ? $arr[0] : 0);
    }else{
        echo array_sum($arr);
    }
    echo  "\n";


<?php
$fp = fopen('php://stdin', 'r');
fgets($fp);
$values = array_map('intval', explode(' ', fgets($fp)));
function bubble_sort($arr) {
    $size = count($arr);
    for ($i=0; $i<$size; $i++) {
        for ($j=0; $j<$size-1-$i; $j++) {
            if ($arr[$j+1] < $arr[$j]) {
                swap($arr, $j, $j+1);
            }
        }
    }
    return $arr;
}

function swap(&$arr, $a, $b) {
    $tmp = $arr[$a];
    $arr[$a] = $arr[$b];
    $arr[$b] = $tmp;
	echo ($a+1).' '.($b + 1)."\n";
}

bubble_sort($values);


<?php
$mod = 1000000007;
$intMaxValue = 2147483646; //2147483647

$loop = true;
$t = $loop ? IO::get() : 1;
while($t--) {
    solve();
}
function solve()
{
   $n = IO::get();
   $d = IO::getArray();
   $ans = '';
   $a = [];
   $a[0][] = $d[0];
   //di=|ai−ai−1|
   //a1 =
   for($i=1; $i<$n; $i++) {
        for($j=0; $j < count($a[$i-1]); $j++) {
             $a[$i] = [];
             $temp = abs($d[$i] - $a[$i-1][$j]);
             if(abs($temp - $a[$i-1][$j]) ==$d[$i] && !in_array($temp, $a[$i])) {
                 $a[$i][] = $temp;
             }

             $temp = $d[$i] + $a[$i-1][$j];
             if(abs($temp - $a[$i-1][$j]) ==$d[$i] && !in_array($temp, $a[$i])) {
                 $a[$i][] = $temp;
             }

        }

   }

         for($i=0; $i<$n; $i++) {
              if(count($a[$i]) == 1) {
                  $ans .= $a[$i][0] .' ';
              } else {
                        $ans = '-1';
                        break;
                   }

         }

    IO::pr($ans);
}

class IO {
    public static function get()
    {
        return trim(fgets(STDIN));
    }

    public static function getArray()
    {
        return explode(' ', trim(fgets(STDIN)));
    }
    public static function pr($s)
    {
        echo $s, PHP_EOL;
    }
    public static function prArray($s)
    {
        echo implode( ' ', $s), PHP_EOL;
    }
    public static function getMinValue($x, $y) {
        return $x > $y ? $y : $x;
    }
    public static function getMaxValue($x, $y) {
        return $x > $y ? $x : $y;
    }
}

<?php
$mod = 1000000007;
$intMaxValue = 2147483646; //2147483647

$loop = true;
$t = $loop ? IO::get() : 1;
while($t--) {
    solve();
}
function solve()
{
   $n = IO::get();
   $d = IO::getArray();
   $ans = '';
   $a = [];
   $a[0][] = $d[0];
   //di=|ai−ai−1|
   //a1 =
   for($i=1; $i<$n; $i++) {
        for($j=0; $j < count($a[$i-1]); $j++) {
             $a[$i] = [];
             $temp = abs($d[$i] - $a[$i-1][$j]);
             if(abs($temp - $a[$i-1][$j]) ==$d[$i] && !in_array($temp, $a[$i])) {
                 $a[$i][] = $temp;
             }

             $temp = $d[$i] + $a[$i-1][$j];
             if(abs($temp - $a[$i-1][$j]) ==$d[$i] && !in_array($temp, $a[$i])) {
                 $a[$i][] = $temp;
             }

        }

   }
    if(count($a[$n-1]) == 1) {
         for($i=0; $i<$n; $i++) {
            $ans .= $a[$i][0] .' ';
         }
    } else {
         $ans = '-1';
    }
    IO::pr($ans);
}

class IO {
    public static function get()
    {
        return trim(fgets(STDIN));
    }

    public static function getArray()
    {
        return explode(' ', trim(fgets(STDIN)));
    }
    public static function pr($s)
    {
        echo $s, PHP_EOL;
    }
    public static function prArray($s)
    {
        echo implode( ' ', $s), PHP_EOL;
    }
    public static function getMinValue($x, $y) {
        return $x > $y ? $y : $x;
    }
    public static function getMaxValue($x, $y) {
        return $x > $y ? $x : $y;
    }
}


<?php
$r = trim(fgets(STDIN));
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));

$fo = preg_match('/.*'.$a.'.*'.$b.'.*/',$r);
$ba = preg_match('/.*'.$a.'.*'.$b.'.*/',strrev($r));

if($fo && $ba) $res = 'both';
if($fo && !$ba) $res = 'forward';
if(!$fo && $ba) $res = 'backward';
if(!$fo && !$ba) $res = 'fantasy';

print $res;
?>

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));
$c = trim(fgets(STDIN));
$d = strrev($a);
$e = strstr($a, $b);
$f = substr($e, strlen($b));
$g = strstr($f, $c);
$h = strstr($d, $b);
$i = substr($h, strlen($b));
$j = strstr($i, $c);
if(($e && $g) && ($h && $j))
{
    print "both";
}
elseif($e && $g)
{
    print "forward";
}
elseif($h && $j)
{
    print "backward";
}
else
{
    print "fantasy";
}
?>

<?php

###### IO ######
error_reporting(E_ALL);
#error_reporting(0);

function readString($f) {
	$r = array(
		"\r" => '',
		"\n" => '',
	);
	return strtr(fgets($f), $r);
}

function readNumString($f) {
	return trim(fgets($f));
}

function stringToArray($str) {
	return str_split($str);
}

function readNum($f, $is_int = false) {
	$v = readNumString($f);
	return $is_int ? intval($v) : floatval($v);
}

function readStringExplode($f, $delimiter = ' ') {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = 'trim';
	return array_map($callf, $return);
}

function readStringExplodeNum($f, $delimiter = ' ', $is_int = false) {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = $is_int ? 'intval' : 'floatval';
	return array_map($callf, $return);
}

###### /IO ######

function getAnswer($is_test = false, $f = false) {
	if ($is_test === false) {
		$f = STDIN;
	}
	/* begin body */
	$path = readString($f);
	$t1 = readString($f);
	$t2 = readString($f);

	$isForward = false;
	$i1 = strpos($path, $t1);
	if ($i1 !== false && strpos($path, $t2, $i1 + strlen($t1)) !== false) {
		$isForward = true;
	}

	$isBack = false;
	$path = strrev($path);
	$i1 = strpos($path, $t1);
	if ($i1 !== false && strpos($path, $t2, $i1 + strlen($t1)) !== false) {
		$isBack = true;
	}

	if ($isForward && $isBack) {
		echo 'both';
	} elseif ($isForward) {
		echo 'forward';
	} elseif ($isBack) {
		echo 'backward';
	} else {
		echo 'fantasy';
	}
	/* / end body */
}

if (empty($is_test))
	getAnswer();

<?php

function debug($data, $die = true) {
    echo '<pre>';

    if (is_array($data)) {
        print_r($data);
    } else {
        var_dump($data);
    }

    if ($die) {
        die();
    }

    echo '</pre>';
}

$stdin = @fopen('data.txt', 'r');
if (!$stdin) {
    $stdin = fopen('php://stdin', 'r');
}

function g() {
    global $stdin;
    return trim(fgets($stdin));
}

/****************************************************** Solving block *****************************************************/
$flags = g();
$first = g();
$second = g();

function find($flags, $first, $second) {
    $str1 = strpos($flags, $first);
    $str2 = strpos($flags, $second, $str1 !== false ? $str1 + strlen($first) : 0);
    return $str1 !== false && $str2 !== false;
}

$forward = find($flags, $first, $second);
$flags = strrev($flags);
$backward = find($flags, $first, $second);

if($forward && $backward) {
    echo 'both';
} else if($forward) {
    echo 'forward';
} else if($backward) {
    echo 'backward';
} else {
    echo 'fantasy';
}

?>

<?php

	$stdin = fopen('php://stdin', 'r');
    $road = trim(fgets($stdin));
    $start = trim(fgets($stdin));
    $end = trim(fgets($stdin));
    $pattern = '/.*'.$start.'.*'.$end.'.*/';
    preg_match($pattern, $road,$forvard);
    preg_match($pattern, strrev($road), $backward);
    if($forvard && $backward)        echo 'both';
    if(!$forvard && !$backward)        echo 'fantasy';
    if($forvard && !$backward)        echo 'forward';
    if(!$forvard && $backward)        echo 'backward';

<?php
error_reporting("off");
//8A: Train and Peter
$flags = trim(fgets(STDIN)); //$flags = "atob";
$one = trim(fgets(STDIN)); //$one = "a";
$two = trim(fgets(STDIN)); //$two = "b";
$seeforwards = FALSE;
$seebackwards = FALSE;
$length = strlen($one);

if (strpos($flags, $one) !== FALSE) {
    $oneposfor = strpos($flags, $one);
    if (strpos($flags, $two, $oneposfor + $length)) {
        $seeforwards = TRUE;
    }
}

$sgalf = strrev($flags);

if (strpos($sgalf, $one) !== FALSE) {
    $oneposbac = strpos($sgalf, $one);
    if (strpos($sgalf, $two, $oneposbac + $length)) {
        $seebackwards = TRUE;
    }
}

if ($seeforwards == TRUE && $seebackwards == TRUE) {
    echo "both";
} elseif ($seeforwards == TRUE && $seebackwards == FALSE) {
    echo "forward";
} elseif ($seeforwards == FALSE && $seebackwards == TRUE) {
    echo "backward";
} else {
    echo "fantasy";
}
?>

<?php

###### IO ######
error_reporting(E_ALL);
#error_reporting(0);

function readString($f) {
	$r = array(
		"\r" => '',
		"\n" => '',
	);
	return strtr(fgets($f), $r);
}

function readNumString($f) {
	return trim(fgets($f));
}

function stringToArray($str) {
	return str_split($str);
}

function readNum($f, $is_int = false) {
	$v = readNumString($f);
	return $is_int ? intval($v) : floatval($v);
}

function readStringExplode($f, $delimiter = ' ') {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = 'trim';
	return array_map($callf, $return);
}

function readStringExplodeNum($f, $delimiter = ' ', $is_int = false) {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = $is_int ? 'intval' : 'floatval';
	return array_map($callf, $return);
}

###### /IO ######

function getAnswer($is_test = false, $f = false) {
	if ($is_test === false) {
		$f = STDIN;
	}
	/* begin body */
	$path = readString($f);
	$t1 = readString($f);
	$t2 = readString($f);

	$isForward = false;
	$i1 = strpos($path, $t1);
	if ($i1 !== false && strpos($path, $t2, $i1 + 1) !== false) {
		$isForward = true;
	}

	$isBack = false;
	$path = strrev($path);
	if ($i1 !== false && strpos($path, $t2, $i1 + 1) !== false) {
		$isBack = true;
	}

	if ($isForward && $isBack) {
		echo 'both';
	} elseif ($isForward) {
		echo 'forward';
	} elseif ($isBack) {
		echo 'backward';
	} else {
		echo 'fantasy';
	}
	/* / end body */
}

if (empty($is_test))
	getAnswer();

<?php

###### IO ######
error_reporting(E_ALL);
#error_reporting(0);

function readString($f) {
	$r = array(
		"\r" => '',
		"\n" => '',
	);
	return strtr(fgets($f), $r);
}

function readNumString($f) {
	return trim(fgets($f));
}

function stringToArray($str) {
	return str_split($str);
}

function readNum($f, $is_int = false) {
	$v = readNumString($f);
	return $is_int ? intval($v) : floatval($v);
}

function readStringExplode($f, $delimiter = ' ') {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = 'trim';
	return array_map($callf, $return);
}

function readStringExplodeNum($f, $delimiter = ' ', $is_int = false) {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = $is_int ? 'intval' : 'floatval';
	return array_map($callf, $return);
}

###### /IO ######

function getAnswer($is_test = false, $f = false) {
	if ($is_test === false) {
		$f = STDIN;
	}
	/* begin body */
	$path = readString($f);
	$t1 = readString($f);
	$t2 = readString($f);

	$isForward = false;
	$i1 = strpos($path, $t1);
	if ($i1 !== false && strpos($path, $t2, $i1 + 1) !== false) {
		$isForward = true;
	}

	$isBack = false;
	$path = strrev($path);
	$i1 = strpos($path, $t1);
	if ($i1 !== false && strpos($path, $t2, $i1 + 1) !== false) {
		$isBack = true;
	}

	if ($isForward && $isBack) {
		echo 'both';
	} elseif ($isForward) {
		echo 'forward';
	} elseif ($isBack) {
		echo 'backward';
	} else {
		echo 'fantasy';
	}
	/* / end body */
}

if (empty($is_test))
	getAnswer();

<?php

	$stdin = fopen('php://stdin', 'r');
    $road = trim(fgets($stdin));
    $start = trim(fgets($stdin));
    $end = trim(fgets($stdin));
    $pattern = '/.*'.$start.'.*'.$end.'.*/';
    preg_match($pattern, $road,$forvard);
    preg_match($pattern, strrev($road), $backward);
    if($forvard && $backward)        echo 'both';
    if(!$forvard && !$backward)        echo 'fantasy';
    if($forvard && !$backward)        echo 'forvard';
    if(!$forvard && $backward)        echo 'backward';


<?php
function buildMap($arr)
{
	$MAX = 0;
	$s = sizeof($arr);
	for($i=$s-1; $i>=0; $i--)
	{
		$el = $arr[$i];
		if($i + $el < $s)
			$arr[$i] = $el + $arr[$i + $el];
		if($arr[$i] > $MAX)
			$MAX = $arr[$i];
	}
	return $MAX;
}

$t = fgets(STDIN);
for($i=0;$i<$t;$i++)
{
	$arr = array();
	$n = fgets(STDIN);
	$arr_str = fgets(STDIN);
	$arr = preg_split("/[\s,]+/", $arr_str);
	print(buildMap($arr)."\r\n");
}
//$end = fgets(STDIN);
?>

<?php

$times = (int) trim(fgets(STDIN));
for ($idx = 0; $idx < $times; ++$idx) {
    $ni[] = (int) trim(fgets(STDIN));
    $ai[] = array_map('intval', explode(' ', trim(fgets(STDIN))));
}
for ($idx = 0; $idx < $times; ++$idx) {
    $n = &$ni[$idx];
    $a = &$ai[$idx];

    $s=[];
    for ($i = $n - 1; $i >= 0; --$i) {
        $s[$i] = $a[$i];
        if ($i + $a[$i] < $n) {
            $s[$i] += $s[$i + $a[$i]];
        }
    }
    echo max($s).PHP_EOL;
}

<?php
function solve() {
    $n = intval(fgets(STDIN));
    $s = trim(fgets(STDIN));
    $a = preg_split("/[\s,]+/", $s);
    $aRes = [];
    for ($i = $n - 1; $i >= 0; $i--) {
        $aRes[$i] = $a[$i];
        $k = $i + $a[$i];
        if ($k < $n) {
            $aRes[$i] += $aRes[$k];
        }
    }
    echo max($aRes);
}

$t = trim(fgets(STDIN));
while ($t--) {
    solve();
    echo "\n";
}
?>

<?php
function solve() {
    $n = intval(fgets(STDIN));
    $s = trim(fgets(STDIN));
    $a = preg_split("/[\s,]+/", $s);
    $aRes = [];
    for ($i = $n; $i >= 0; $i--) {
        if (isset($aRes[$i])) {
            $aRes[$i] += $a[$i];
        } else {
            $aRes[$i] = $a[$i];
        }
        $k = $i + $a[$i];
        if ($k < $n) {
            $aRes[$i] += $aRes[$k];
        }
    }

    echo max($aRes);
}

$t = trim(fgets(STDIN));
while ($t--) {
    solve();
    echo "\n";
}
?>

<?php
function buildMap($arr)
{
	$MAX = 0;
	$s = sizeof($arr);
	for($i=$s-1; $i>=0; $i--)
	{
		$el = $arr[$i];
		if($i + $el < $s)
			$arr[$i] = $el + $arr[$i + $el];
		if($arr[$i] > $MAX)
			$MAX = $arr[$i];
	}
	return $MAX;
}

$t = fgets(STDIN);
for($i=0;$i<$t;$i++)
{
	$arr = array();
	$n = fgets(STDIN);
	$arr_str = fgets(STDIN);
	$arr = explode(" ", trim($arr_str));
	print(buildMap($arr)."\r\n");
}
//$end = fgets(STDIN);
?>

<?php
function buildMap($arr)
{
	$MAX = 0;
	$s = sizeof($arr);
	for($i=$s-1; $i>=0; $i--)
	{
		$el = $arr[$i];
		if($i + $el < $s)
		{
			$arr[$i] = $el + $arr[$i + $el];
			$el = $arr[$i];
		}
		if($el > $MAX)
			$MAX = $el;
	}
	return $MAX;
}

$t = fgets(STDIN);
for($i=0;$i<$t;$i++)
{
	$arr = array();
	$n = fgets(STDIN);
	$arr_str = fgets(STDIN);
	$arr = preg_split("/[\s,]+/", $arr_str);
	print(buildMap($arr)."\r\n");
}
//$end = fgets(STDIN);
?>

<?php
function buildMap($arr)
{
	$s = sizeof($arr);
	for($i=$s-1; $i>=0; $i--)
	{
		$el = $arr[$i];
		if($i + $el < $s)
			$arr[$i] = $el + $arr[$i + $el];
	}
	return max($arr);
}

$t = fgets(STDIN);
for($i=0;$i<$t;$i++)
{
	$arr = array();
	$n = fgets(STDIN);
	$arr_str = fgets(STDIN);;
	$arr = preg_split("/[\s,]+/", $arr_str);
	print(buildMap($arr)."\r\n");
}
$end = fgets(STDIN);
?>

<?php
function buildMap($arr)
{
	$MAX = 0;
	$s = sizeof($arr);
	for($i=$s-1; $i>=0; $i--)
	{
		$el = $arr[$i];
		if($i + $el < $s)
			$arr[$i] = $el + $arr[$i + $el];
		if($arr[$i] > $MAX)
			$MAX = $arr[$i];
	}
	return $MAX;
}

$t = fgets(STDIN);
for($i=0;$i<$t;$i++)
{
	$arr = array();
	$n = fgets(STDIN);
	$arr_str = fgets(STDIN);
	$arr = explode(" ", $arr_str);
	print(buildMap($arr)."\r\n");
}
//$end = fgets(STDIN);
?>

<?php
function buildMap($arr)
{
	$MAX = 0;
	$s = sizeof($arr);
	for($i=$s-1; $i>=0; $i--)
	{
		$el = $arr[$i];
		if($i + $el < $s)
		{
			$arr[$i] = $el + $arr[$i + $el];
			$el = $arr[$i];
		}
		if($el > $MAX)
			$MAX = $el;
	}
	return $MAX;
}

$t = fgets(STDIN);
for($i=0;$i<$t;$i++)
{
	$arr = array();
	$n = fgets(STDIN);
	$arr_str = fgets(STDIN);
	$arr = explode(" ", $arr_str);
	print(buildMap($arr)."\r\n");
}
$end = fgets(STDIN);
?>

<?php
function buildMap($arr)
{
	$MAX = 0;
	$MAP = array();
	$s = sizeof($arr);
	for($i=$s-1; $i>=0; $i--)
	{
		$el = $arr[$i];
		if($i + $el >= $s)
		{
			if($el > $MAX)
				$MAX = $el;
			$MAP[$i] = $el;
		}
		else
		{
			$el2 = $MAP[$i + $el];
			if($el + $el2 > $MAX)
				$MAX = $el + $el2;
			$MAP[$i] = $el + $el2;
		}
	}
	return $MAX;
}

$ans = array();
$t = readline();
for($i=0;$i<$t;$i++)
{
	$arr = array();
	$n = readline();
	$arr = fscanf(STDIN, str_repeat("%d ", $n)."\n");
	array_push($ans, buildMap($arr)."\r\n");
}
foreach($ans as $key => $value)
	print($value);
//$end = readline();
?>

<?php
function buildMap($arr)
{
	$MAX = 0;
	$MAP = array();
	$s = sizeof($arr);
	for($i=$s-1; $i>=0; $i--)
	{
		$el = $arr[$i];
		if($i + $el >= $s)
		{
			if($el > $MAX)
				$MAX = $el;
			$MAP[$i] = $el;
		}
		else
		{
			$el2 = $MAP[$i + $el];
			if($el + $el2 > $MAX)
				$MAX = $el + $el2;
			$MAP[$i] = $el + $el2;
		}
	}
	return $MAX;
}

$ans = array();
$t = readline();
for($i=0;$i<$t;$i++)
{
	$arr = array();
	$n = readline();
	$arr = fscanf(STDIN, str_repeat("%d ", $n));
	array_push($ans, buildMap($arr)."\r\n");
}
foreach($ans as $key => $value)
	print($value);
$end = readline();
?>

<?php
function buildMap($arr)
{
	$MAX = 0;
	$MAP = array();
	$s = sizeof($arr);
	for($i=$s-1; $i>=0; $i--)
	{
		$el = $arr[$i];
		if($i + $el >= $s)
		{
			if($el > $MAX)
				$MAX = $el;
			$MAP[$i] = $el;
		}
		else
		{
			if($el > $MAX)
				$MAX = $el;
			$MAP[$i] = $el + $MAP[$i + $el];
		}
	}
	return $MAX;
}

$arr = array();
$ans = array();
$t = readline();
for($i=0;$i<$t;$i++)
{
	$n = readline();
	$arr_str = readline();
	$arr = explode(" ", $arr_str);
	array_push($ans, buildMap($arr)."\r\n");
}
foreach($ans as $key => $value)
	print($value);
$end = readline();
?>

<?php
$MAX = 0;
function maxs($a)
{
	global $MAX;
	if($a > $MAX)
		$MAX = $a;
	return $a;
}

function buildMap($arr)
{
	global $MAX;
	$MAP = array();
	$s = sizeof($arr);
	for($i=$s-1; $i>=0; $i--)
	{
		$el = $arr[$i];
		if($i + $el >= $s)
			$MAP[$i] = maxs($el);
		else
			$MAP[$i] = maxs($el + $MAP[$i + $el]);
	}
	return $MAX;
}

$arr = array();
$ans = array();
$t = readline();
for($i=0;$i<$t;$i++)
{
	$n = readline();
	$arr_str = readline();
	$arr = explode(" ", $arr_str);
	array_push($ans, buildMap($arr)."\r\n");
}
foreach($ans as $key => $value)
	print($value);
$end = readline();
?>

<?php

$times = (int) trim(fgets(STDIN));
for ($idx = 0; $idx < $times; ++$idx) {
    $ni[] = (int) trim(fgets(STDIN));
    $ai[] = array_map('intval', explode(' ', trim(fgets(STDIN))));
}
for ($idx = 0; $idx < $times; ++$idx) {
    $n = &$ni[$idx];
    $a = &$ai[$idx];
    for ($i = $n - 1; $i >= 0; --$i) {
        $s[$i] = $a[$i];
        if ($i + $a[$i] < $n) {
            $s[$i] += $s[$i + $a[$i]];
        }
    }
    echo max($s).PHP_EOL;
}


<?php
define('maxx',(int)1e9);

class Rectangle
{
    public $bottom_left_x;
    public $bottom_left_y;
    public $upper_right_x;
    public $upper_right_y;
    public function __construct($xl,$yl,$xr,$yr)
    {
        $this->bottom_left_x = $xl;
        $this->bottom_left_y = $yl;
        $this->upper_right_x = $xr;
        $this->upper_right_y = $yr;
    }
    public function uion(Rectangle $a)
    {
         $this->bottom_left_x = (int)max($this->bottom_left_x,$a->bottom_left_x);
         $this->bottom_left_y = (int)max($this->bottom_left_y,$a->bottom_left_y);
         $this->upper_right_x = (int)min($this->upper_right_x,$a->upper_right_x);
         $this->upper_right_y = (int)min($this->upper_right_y,$a->upper_right_y);
         if($this->bottom_left_x <= $this->upper_right_x&&$this->bottom_left_y <= $this->upper_right_y)
         {
             //var_dump('1111111');
             return true;
         }
         return false;
    }
}
fscanf(STDIN,"%d",$number);
$arr_l = array();
$arr_r = array();
for($i = 1;$i <= $number;$i++)
{
    fscanf(STDIN,"%d %d %d %d",$xl,$yl,$xr,$yr);
    $arr_l[$i] = new Rectangle($xl,$yl,$xr,$yr);
    $arr_r[$i] = new Rectangle($xl,$yl,$xr,$yr);
}
$arr_l[0] = new Rectangle(-maxx,-maxx,maxx,maxx);
$arr_l[$number + 1] = new Rectangle(-maxx,-maxx,maxx,maxx);
$arr_r[$number + 1] = new Rectangle(-maxx,-maxx,maxx,maxx);
$arr_r[0] = new Rectangle(-maxx,-maxx,maxx,maxx);
$arr_l[$number+1] = $arr_r[0] = $arr_r[$number+1] = new Rectangle(-maxx,-maxx,maxx,maxx);
for($i = 2;$i <= $number;$i++)
{
    $arr_l[$i]->uion($arr_l[$i-1]);
    //var_dump($arr_l[$i]);
    $arr_r[$number-$i + 1]->uion($arr_r[$number-$i + 2]);
}
for($i = 1;$i <= $number;$i++)
{
    //if($i == $number)
    // var_dump($arr_l[$i-1],$arr_r[$i+1]);
    if($arr_l[$i-1]->uion($arr_r[$i+1]))
    {
        echo $arr_l[$i-1]->bottom_left_x.' '.$arr_l[$i-1]->bottom_left_y;
        break;
    }
   // var_dump($arr_l[$i-1]);
}

<?php
define('maxx',(int)1e9);

class Rectangle
{
    public $bottom_left_x;
    public $bottom_left_y;
    public $upper_right_x;
    public $upper_right_y;
    public function __construct($xl,$yl,$xr,$yr)
    {
        $this->bottom_left_x = $xl;
        $this->bottom_left_y = $yl;
        $this->upper_right_x = $xr;
        $this->upper_right_y = $yr;
    }
    public function uion(Rectangle $a)
    {
         $this->bottom_left_x = (int)max($this->bottom_left_x,$a->bottom_left_x);
         $this->bottom_left_y = (int)max($this->bottom_left_y,$a->bottom_left_y);
         $this->upper_right_x = (int)min($this->upper_right_x,$a->upper_right_x);
         $this->upper_right_y = (int)min($this->upper_right_y,$a->upper_right_y);
         if($this->bottom_left_x <= $this->upper_right_x&&$this->bottom_left_y <= $this->upper_right_y)
         {
             //var_dump('1111111');
             return true;
         }
         return false;
    }
}
fscanf(STDIN,"%d",$number);
$arr_l = array();
$arr_r = array();
for($i = 1;$i <= $number;$i++)
{
    fscanf(STDIN,"%d %d %d %d",$xl,$yl,$xr,$yr);
    $arr_l[$i] = new Rectangle($xl,$yl,$xr,$yr);
    $arr_r[$i] = new Rectangle($xl,$yl,$xr,$yr);
}
$arr_l[0] = new Rectangle(-maxx,-maxx,maxx,maxx);
$arr_l[$number + 1] = new Rectangle(-maxx,-maxx,maxx,maxx);
$arr_r[$number + 1] = new Rectangle(-maxx,-maxx,maxx,maxx);
$arr_r[0] = new Rectangle(-maxx,-maxx,maxx,maxx);
for($i = 2;$i <= $number;$i++)
{
    $arr_l[$i]->uion($arr_l[$i-1]);
    //var_dump($arr_l[$i]);
    $arr_r[$number-$i + 1]->uion($arr_r[$number-$i + 2]);
}
for($i = 1;$i <= $number;$i++)
{
    //if($i == $number)
    // var_dump($arr_l[$i-1],$arr_r[$i+1]);
    if($arr_l[$i-1]->uion($arr_r[$i+1]))
    {
        echo $arr_l[$i-1]->bottom_left_x.' '.$arr_l[$i-1]->bottom_left_y;
        break;
    }
   // var_dump($arr_l[$i-1]);
}


<?php
	$s = str_split(trim(fgets(STDIN)));

	$ns = [];
	$ans = '';

	for($i = 0; $i<count($s); $i++){
		if($s[$i] != 'a') $ns[] = $s[$i];
	}
	$ok = true;
	if(count($ns) > 0){
		if(count($ns) % 2 == 0){
			if(substr(implode('', $ns), 0, count($ns)/2) == substr(implode('', $ns), count($ns)/2, count($ns))){

				for($i = count($s)-1; $i>count($s)-count($ns)/2-1; $i--){
					//echo "(".$s[$i].")";
					if($s[$i] == 'a'){
						echo ":(";
						$ok = false;
						break;
					}
				}
				if($ok) echo substr(implode('', $s), 0, -(count($ns)/2));
			}else{
				echo ":(";
			}
		}else{
			echo ":(";
		}
	}else{
		echo implode('',$s);
	}
?>

<?php
# 1146B

$input = fopen('php://stdin', 'rb');
$str = trim(fgets($input));
$length = strlen($str);
$clearStr = str_replace('a', '', $str);
$clearStrLength = strlen($clearStr);

if ($clearStr === '') {
    die($str);
}

if ($clearStrLength % 2) {
    die(':(');
}
$_sLength = $clearStrLength/2;
$_s = substr($clearStr, $_sLength * -1);
$s = substr($str, 0, $length - $_sLength);
if (
    substr($str, $length - strlen($_s)) === $_s
    && substr($clearStr, 0, $clearStrLength/2) === $_s
) {
    echo $s;
} else {
    echo ':(';
}

<?php
# 1146B

$input = fopen('php://stdin', 'rb');
$str = trim(fgets($input));
$length = strlen($str);
$clearStr = str_replace('a', '', $str);
$clearStrLength = strlen($clearStr);

if ($clearStr === '') {
    die($str);
}

if ($clearStrLength % 2) {
    die(':(');
}

$_s = substr($clearStr, $clearStrLength/2 * -1);
if (
    substr($str, $length - strlen($_s)) === $_s
    && substr($clearStr, 0, $clearStrLength/2) === $_s
) {
    echo $_s;
} else {
    echo ':(';
}

<?php
	$s = str_split(trim(fgets(STDIN)));

	$ns = [];
	$ans = '';

	for($i = 0; $i<count($s); $i++){
		if($s[$i] != 'a') $ns[] = $s[$i];
	}
	$ok = true;
	if(count($ns) > 0){
		if(count($ns) % 2 == 0){
			if(substr(implode('', $ns), 0, count($ns)/2) == substr(implode('', $ns), count($ns)/2, count($ns))){

				for($i = count($s)-1; $i>count($s)-count($ns)/2; $i--){
					if($s[$i] == 'a'){
						echo ":(";
						$ok = false;
						break;
					}
				}
				if($ok) echo substr(implode('', $s), 0, -(count($ns)/2));
			}else{
				echo ":(";
			}
		}else{
			echo ":(";
		}
	}else{
		echo implode('',$s);
	}
?>

<?php
	$s = str_split(trim(fgets(STDIN)));

	$ns = [];
	$ans = '';

	for($i = 0; $i<count($s); $i++){
		if($s[$i] != 'a') $ns[] = $s[$i];
	}
	$ok = true;
	if(count($ns) > 0){
		if(count($ns) % 2 == 0){
			for($i = count($s)-1; $i>count($s)-count($ns)/2; $i--){
				if($s[$i] == 'a'){
					echo ":(";
					$ok = false;
					break;
				}
			}
			if($ok) echo substr(implode('', $s), 0, -(count($ns)/2));
		}else{
			echo ":(";
		}
	}else{
		echo implode('',$s);
	}
?>

<?php
	$s = str_split(trim(fgets(STDIN)));

	$ns = [];
	$ans = '';

	for($i = 0; $i<count($s); $i++){
		if($s[$i] != 'a') $ns[] = $s[$i];
	}
	$ok = true;
	if(count($ns) > 0){
		if(count($ns) % 2 == 0){
			if(substr(implode('', $ns), 0, count($ns)/2) == substr(implode('', $ns), count($ns)/2, count($ns))){

				for($i = count($s)-1; $i>count($s)-count($ns)/2-1; $i--){
					echo "(".$s[$i].")";
					if($s[$i] == 'a'){
						echo ":(";
						$ok = false;
						break;
					}
				}
				if($ok) echo substr(implode('', $s), 0, -(count($ns)/2));
			}else{
				echo ":(";
			}
		}else{
			echo ":(";
		}
	}else{
		echo implode('',$s);
	}
?>


<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     list($b, $c, $d) = explode(" ", trim(fgets(STDIN)));
     $e = explode(" ", trim(fgets(STDIN)));
     $f = explode(" ", trim(fgets(STDIN)));
     $g = max($e);
     $h = max($f);
     if($g > $h)
     {
          print "YES\n";
     }
     else
     {
          print "NO\n";
     }
}
?>

<?php
// $stdin = fopen('input.txt', 'r');

$t = intval(trim(fgets(STDIN)));

for (; $t!=0 ; $t--){
	list($n, $k1, $k2) = explode(' ', trim(fgets(STDIN)));
	$a1 = explode(' ', trim(fgets(STDIN)));
	$a2 = explode(' ', trim(fgets(STDIN)));

	if(max($a1) > max($a2)){
		echo "YES\n";
	} else {
		echo "NO\n";
	}
}

<?php

$server = "php://stdin";
$local = "input.txt";
$handle = fopen($server, "r");
$t = trim(fgets($handle));

for ($i=0;$i<$t;$i++)
{
    $x = trim(fgets($handle));
    $a = explode(' ', $x);
    $p = trim(fgets($handle));
    $p1 = explode(' ', $p);
    $p2 = trim(fgets($handle));
    $p22 = explode(' ', $p2);
    $max=0;
    for($j=0;$j<$a[1];$j++)
    {
        if($p1[$j]>$max)
        {
            $max=$p1[$j];
        }
    }
    $max1=0;
    for($j=0;$j<$a[2];$j++)
    {
        if($p22[$j]>$max1)
        {
            $max1=$p22[$j];
        }
    }
    if($max<$max1)
    {
        echo "NO"."\n";
    }else
    {
        echo "YES"."\n";
    }
}

?>


<?php
$line1 = explode(' ', trim(fgets(STDIN)));
$n = intval($line1[0]);
$b = intval($line1[1]);
$prices = explode(' ', trim(fgets(STDIN)));

$k = $b;

for($i = 0; $i < $n - 1; $i++){

	$t = floor($b / $prices[$i]);
	for($j  = $i + 1; $j < $n; $j++){
		$tmp = $prices[$j] * $t + $b - $t * $prices[$i];
		if($k < $tmp)$k = $tmp;
	}
}
echo $b > $k ? $b : $k;

<?php
$line1 = explode(' ', trim(fgets(STDIN)));
$n = intval($line1[0]);
$b = intval($line1[1]);
$prices = explode(' ', trim(fgets(STDIN)));

$k = $b;

for($i = 0; $i < $n - 1; $i++){

	$t = $b / $prices[$i];
	for($j  = $i + 1; $j < $n; $j++){
		$tmp = $prices[$j] * $t + $b - $t * $prices[$i];
		if($k < $tmp)$k = $tmp;
	}
}
echo $b > $k ? $b : $k;

<?php
$line1 = explode(' ', trim(fgets(STDIN)));
$n = intval($line1[0]);
$b = intval($line1[1]);
$prices = explode(' ', trim(fgets(STDIN)));

$segments = array();
for($i = 0; $i < $n; $i++){
	for($j = $i + 1; $j < $n; $j++){
		if($prices[$i] > $prices[$j]){
			break;
		}
		if($j + 1 == $n){// end of line
			$segments[] = array($i, $j);
			$i = $j;
			break;
		}
		if($prices[$j] > $prices[$j + 1]){// end of a segment
			$segments[] = array($i, $j);
			$i = $j;
			break;
		}
	}
}

foreach($segments as $segment){
	$unit = floor($b / $prices[$segment[0]]);
	$b -= $unit * $prices[$segment[0]];
	$sell = $unit * $prices[$segment[1]];
	$b += $sell;
}
echo $b;

<?php
$line1 = explode(' ', trim(fgets(STDIN)));
$n = intval($line1[0]);
$b = intval($line1[1]);
$prices = explode(' ', trim(fgets(STDIN)));

$segments = array();
for($i = 0; $i < $n; $i++){
	for($j = $i + 1; $j < $n; $j++){
		if($prices[$i] > $prices[$j]){
			break;
		}
		if($j + 1 == $n){// end of line
			$segments[] = array($i, $j);
			break;
		}
		if($prices[$j] > $prices[$j + 1]){// end of a segment
			$segments[] = array($i, $j);
			$i = $j;
			break;
		}
	}
}

foreach($segments as $segment){
	$unit = floor($b / $prices[$segment[0]]);
	$b -= $unit * $prices[$segment[0]];
	$sell = $unit * $prices[$segment[1]];
	$b += $sell;
}
echo $b;

<?php
$line1 = explode(' ', trim(fgets(STDIN)));
$n = intval($line1[0]);
$b = intval($line1[1]);
$prices = explode(' ', trim(fgets(STDIN)));

$segments = array();
for($i = 0; $i < $n; $i++){
	for($j = $i + 1; $j < $n; $j++){
		if($prices[$i] > $prices[$j]){
			break;
		}
		if($j + 1 == $n){// end of line
			$segments[] = array($i, $j);
			$i = $j + 1;
			break;
		}
		if($prices[$j] > $prices[$j + 1]){// end of a segment
			$segments[] = array($i, $j);
			$i = $j + 1;
			break;
		}
	}
}

foreach($segments as $segment){
	$unit = floor($b / $prices[$segment[0]]);
	$b -= $unit * $prices[$segment[0]];
	$sell = $unit * $prices[$segment[1]];
	$b += $sell;
}
echo $b;

<?php
$line1 = explode(' ', trim(fgets(STDIN)));
$n = intval($line1[0]);
$b = intval($line1[1]);
$prices = explode(' ', trim(fgets(STDIN)));

$segments = array();
for($i = 0; $i < $n; $i++){
	$max = $i;
	for($j = $i + 1; $j < $n; $j++){
		if($prices[$j] > $prices[$max]){
			$max = $j;
			break;
		}
	}
	if($max > $i){
		$segments[] = array($i, $max);
	}
}

foreach($segments as $segment){
	$unit = floor($b / $prices[$segment[0]]);
	$b -= $unit * $prices[$segment[0]];
	$sell = $unit * $prices[$segment[1]];
	$b += $sell;
}
echo $b;

<?php
$line1 = explode(' ', trim(fgets(STDIN)));
$n = intval($line1[0]);
$b = intval($line1[1]);
$prices = explode(' ', trim(fgets(STDIN)));

$segments = array();
for($i = 0; $i < $n; $i++){
	$max = $i;
	for($j = $i + 1; $j < $n; $j++){
		if($prices[$j] > $prices[$max]){
			$max = $j;
			break;
		}
	}
	if($max > $i){
		$segments[] = array($i, $max);
	}
}
$max_segment = array();
$max_diff = 0;
foreach($segments as $segment){
	$diff = $prices[$segment[1]] - $prices[$segment[0]];
	if($diff > $max_diff){
		$max_diff = $diff;
		$max_segment = $segment;
	}
}
if(!empty($max_segment)){
	$unit = floor($b / $prices[$max_segment[0]]);
	$b -= $unit * $prices[$max_segment[0]];
	$sell = $unit * $prices[$max_segment[1]];
	$b += $sell;
}
echo $b;