<?php
function solve() {
    list($n, $x) = explode(' ', Read::string());
    $a = Read::array_int();
    $b = [];
    for ($i = 0; $i < $n; $i++) {
        $k = $a[$i];
        $b[$i] = 1;
        while ($k % $x  == 0) {
            $k = $k / $x;
            $b[$i] +=1;
        }
    }
    $min = min($b);
    $iSum = 0;
    $iCounter = 1;
    for ($i = 0; $i < $n; $i++) {
        if ($min == $b[$i]) {
            $iCounter = 0;
        }
        $iSum = Bigint::add($iSum, $a[$i] * ($min + $iCounter));
    }

    return $iSum;
}

#$t = 1;
$t = Read::int();
while ($t--) {
    echo 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 sub($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);
        }
    }
}

class Algorithms
{

    /** Факторизация - Разложение числа на простые множители
     * @param int $n
     * @return array
     */
    public static function factorization($n)
    {
        $a = [];
        for ($i = 2; $i * $i <= $n; ++$i) {
            $cnt = 0;
            while (bcmod($n, $i) == 0) {
                ++$cnt;
                $n = bcdiv($n, $i);
            }
            if ($cnt > 0) {
                $a[$i] = $cnt;
            }
        }
        if ($n > 1) {
            $a[$n] = 1;
        }

        return $a;
    }

    /**
     * @param int $a
     * @param int $b
     * @return int Наибольший общий делитель (НОД)
     */
    public static function gcd($a, $b)
    {
        if ($b == 0) {
            return $a;
        } else {
            $a = $a % $b;
            return self::gcd($b, $a);
        }
    }

    /**
     * @param int $a
     * @param int $b
     * @return int Наименьшее общее кратное (НОК)
     */
    public static function lcm($a, $b)
    {
        return abs($a * $b) / self::gcd($a, $b);
    }

    /**Последовательность Фибоначчи =  0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987
     * return Фибоначчи{n}
     * @param int $n
     * @return int
     */
    public static function fibonacci($n)
    {
        $a = [0, 1];

        for ($i = 2; $i <= $n; $i++) {
            $a[$i] = $a[$i-1] + $a[$i-2];
        }

        return $a[$n];
    }

}

?>

<?php

$times = (int) trim(fgets(STDIN));
for ($idx = 0; $idx < $times; ++$idx) {
    list($ni[], $xi[]) = array_map('intval', explode(' ', trim(fgets(STDIN))));
    $ai[] = array_map('intval', explode(' ', trim(fgets(STDIN))));
}
for ($idx = 0; $idx < $times; ++$idx) {
    $n = &$ni[$idx];
    $x = &$xi[$idx];
    $a = &$ai[$idx];
    $b = [];
    for ($i = 0; $i < $n; ++$i) {
        $cnt = 0;
        $tmp = $a[$i];
        while (0 == $tmp % $x) {
            $tmp /= $x;
            ++$cnt;
        }
        $b[] = $cnt;
    }
    $m = min($b);
    $sm = array_sum($a) * ($m + 1);
    for ($i = 0; $i < $n; ++$i) {
        if ($b[$i] > $m) {
            $sm += $a[$i];
        } else {
            break;
        }
    }
    echo nf($sm).PHP_EOL;
}
function nf($n)
{
    return number_format($n, 0, '', '');
}

<?php
function solve() {
    list($n, $x) = explode(' ', Read::string());
    $a = Read::array_int();
    $b = $a;
    $iSum = 0;
    $min = -1;
    for ($i = 0; $i < $n; $i++) {
        $iCounter2 = 1;
        while (Bigint::mod($a[$i], $x)  == 0 && ($min == -1 || $min > $iCounter2)) {
            $a[$i] = Bigint::div($a[$i], $x);
            $iCounter2 +=1;
        }
        $min = $min == -1 ? $iCounter2 : min($min, $iCounter2);
        $iSum = Bigint::add($iSum, Bigint::mul($b[$i], $min));
    }

    return $iSum;
}

#$t = 1;
$t = Read::int();
while ($t--) {
    echo 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 sub($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);
        }
    }
}

class Algorithms
{

    /** Факторизация - Разложение числа на простые множители
     * @param int $n
     * @return array
     */
    public static function factorization($n)
    {
        $a = [];
        for ($i = 2; $i * $i <= $n; ++$i) {
            $cnt = 0;
            while (bcmod($n, $i) == 0) {
                ++$cnt;
                $n = bcdiv($n, $i);
            }
            if ($cnt > 0) {
                $a[$i] = $cnt;
            }
        }
        if ($n > 1) {
            $a[$n] = 1;
        }

        return $a;
    }

    /**
     * @param int $a
     * @param int $b
     * @return int Наибольший общий делитель (НОД)
     */
    public static function gcd($a, $b)
    {
        if ($b == 0) {
            return $a;
        } else {
            $a = $a % $b;
            return self::gcd($b, $a);
        }
    }

    /**
     * @param int $a
     * @param int $b
     * @return int Наименьшее общее кратное (НОК)
     */
    public static function lcm($a, $b)
    {
        return abs($a * $b) / self::gcd($a, $b);
    }

    /**Последовательность Фибоначчи =  0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987
     * return Фибоначчи{n}
     * @param int $n
     * @return int
     */
    public static function fibonacci($n)
    {
        $a = [0, 1];

        for ($i = 2; $i <= $n; $i++) {
            $a[$i] = $a[$i-1] + $a[$i-2];
        }

        return $a[$n];
    }

}

?>

<?php
function solve() {
    list($n, $x) = explode(' ', Read::string());
    $a = Read::array_int();
    $b = $a;
    $iSum = array_sum($a);
    $status = true;
    while($status) {
         for ($j = 0; $j < $n; $j++) {
            if ($a[$j] % $x  == 0) {
                $iSum += $b[$j];
                $a[$j] = $a[$j] / $x;
            } else {
                $status = false;
                break;
            }
        }
    }

    return $iSum;
}

#$t = 1;
$t = Read::int();
while ($t--) {
    echo 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);
        }
    }
}

class Algorithms
{

    /** Факторизация - Разложение числа на простые множители
     * @param int $n
     * @return array
     */
    public static function factorization($n)
    {
        $a = [];
        for ($i = 2; $i * $i <= $n; ++$i) {
            $cnt = 0;
            while (bcmod($n, $i) == 0) {
                ++$cnt;
                $n = bcdiv($n, $i);
            }
            if ($cnt > 0) {
                $a[$i] = $cnt;
            }
        }
        if ($n > 1) {
            $a[$n] = 1;
        }

        return $a;
    }

    /**
     * @param int $a
     * @param int $b
     * @return int Наибольший общий делитель (НОД)
     */
    public static function gcd($a, $b)
    {
        if ($b == 0) {
            return $a;
        } else {
            $a = $a % $b;
            return self::gcd($b, $a);
        }
    }

    /**
     * @param int $a
     * @param int $b
     * @return int Наименьшее общее кратное (НОК)
     */
    public static function lcm($a, $b)
    {
        return abs($a * $b) / self::gcd($a, $b);
    }

    /**Последовательность Фибоначчи =  0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987
     * return Фибоначчи{n}
     * @param int $n
     * @return int
     */
    public static function fibonacci($n)
    {
        $a = [0, 1];

        for ($i = 2; $i <= $n; $i++) {
            $a[$i] = $a[$i-1] + $a[$i-2];
        }

        return $a[$n];
    }

}

?>

<?php
function solve() {
    list($n, $x) = explode(' ', Read::string());
    $a = Read::array_int();
    $iCounter = 0;
    $k = $a[0];
    while ($k % $x == 0) {
        $iCounter += 1;
        $k = $k / $x;
    }
    for ($i = 1; $i < $n; $i++) {
        $iCounter2 = 1;
        $k = $a[$i];
        while ($k % $x == 0) {
            $iCounter2 += 1;
            $k = $k / $x;
        }

        $iCounter = min($iCounter, $iCounter2);
    }
    $iSum = $a[0];
    for ($i = 0; $i < $n; $i++) {
        $iSum += $a[$i] * $iCounter;
    }

    return $iSum;
}

#$t = 1;
$t = Read::int();
while ($t--) {
    echo 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);
        }
    }
}

class Algorithms
{

    /** Факторизация - Разложение числа на простые множители
     * @param int $n
     * @return array
     */
    public static function factorization($n)
    {
        $a = [];
        for ($i = 2; $i * $i <= $n; ++$i) {
            $cnt = 0;
            while (bcmod($n, $i) == 0) {
                ++$cnt;
                $n = bcdiv($n, $i);
            }
            if ($cnt > 0) {
                $a[$i] = $cnt;
            }
        }
        if ($n > 1) {
            $a[$n] = 1;
        }

        return $a;
    }

    /**
     * @param int $a
     * @param int $b
     * @return int Наибольший общий делитель (НОД)
     */
    public static function gcd($a, $b)
    {
        if ($b == 0) {
            return $a;
        } else {
            $a = $a % $b;
            return self::gcd($b, $a);
        }
    }

    /**
     * @param int $a
     * @param int $b
     * @return int Наименьшее общее кратное (НОК)
     */
    public static function lcm($a, $b)
    {
        return abs($a * $b) / self::gcd($a, $b);
    }

    /**Последовательность Фибоначчи =  0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987
     * return Фибоначчи{n}
     * @param int $n
     * @return int
     */
    public static function fibonacci($n)
    {
        $a = [0, 1];

        for ($i = 2; $i <= $n; $i++) {
            $a[$i] = $a[$i-1] + $a[$i-2];
        }

        return $a[$n];
    }

}

?>

<?php
function solve() {
    list($n, $x) = explode(' ', Read::string());
    $a = Read::array_int();
    $iCounter = 0;
    $k = $a[0];
    while ($k % $x == 0) {
        $iCounter += 1;
        $k = $k / $x;
    }
    for ($i = 1; $i < $n; $i++) {
        $iCounter2 = 1;
        $k = $a[$i];
        while ($k % $x == 0) {
            $iCounter2 += 1;
            $k = $k / $x;
        }

        $iCounter = min($iCounter, $iCounter2);
    }
    $iSum = $a[0];
    for ($i = 0; $i < $n; $i++) {
        $iSum += $a[$i] * $iCounter;
    }

    echo $iCounter."\n";

    return $iSum;
}

#$t = 1;
$t = Read::int();
while ($t--) {
    echo 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);
        }
    }
}

class Algorithms
{

    /** Факторизация - Разложение числа на простые множители
     * @param int $n
     * @return array
     */
    public static function factorization($n)
    {
        $a = [];
        for ($i = 2; $i * $i <= $n; ++$i) {
            $cnt = 0;
            while (bcmod($n, $i) == 0) {
                ++$cnt;
                $n = bcdiv($n, $i);
            }
            if ($cnt > 0) {
                $a[$i] = $cnt;
            }
        }
        if ($n > 1) {
            $a[$n] = 1;
        }

        return $a;
    }

    /**
     * @param int $a
     * @param int $b
     * @return int Наибольший общий делитель (НОД)
     */
    public static function gcd($a, $b)
    {
        if ($b == 0) {
            return $a;
        } else {
            $a = $a % $b;
            return self::gcd($b, $a);
        }
    }

    /**
     * @param int $a
     * @param int $b
     * @return int Наименьшее общее кратное (НОК)
     */
    public static function lcm($a, $b)
    {
        return abs($a * $b) / self::gcd($a, $b);
    }

    /**Последовательность Фибоначчи =  0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987
     * return Фибоначчи{n}
     * @param int $n
     * @return int
     */
    public static function fibonacci($n)
    {
        $a = [0, 1];

        for ($i = 2; $i <= $n; $i++) {
            $a[$i] = $a[$i-1] + $a[$i-2];
        }

        return $a[$n];
    }

}

?>

<?php
function solve() {
    $s1 = trim(fgets(STDIN));
    list($n, $x) = explode(' ', $s1);
    $s = trim(fgets(STDIN));
    $a = preg_split("/[\s,]+/", $s);
    $iCounter = 0;
    $k = $a[0];
    while ($k % $x == 0) {
        $iCounter += 1;
        $k = $k / $x;
    }

    $iSum = 0;
    for ($i = 0; $i < $n; $i++) {
        $iSum += $a[$i] * min($iCounter + 1, floor($a[$i] / $x));
    }

    echo $iSum;
}

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

<?php

$times = (int) trim(fgets(STDIN));
for ($idx = 0; $idx < $times; ++$idx) {
    list($ni[], $xi[]) = array_map('intval', explode(' ', trim(fgets(STDIN))));
    $ai[] = array_map('intval', explode(' ', trim(fgets(STDIN))));
}
for ($idx = 0; $idx < $times; ++$idx) {
    $n = &$ni[$idx];
    $x = &$xi[$idx];
    $a = &$ai[$idx];
    $b = [];
    for ($i = 0; $i < $n; ++$i) {
        $cnt = 0;
        $tmp = $a[$i];
        while (0 == $tmp % $x) {
            $tmp /= $x;
            ++$cnt;
        }
        $b[] = $cnt;
    }
    $m = min($b);
    $sm = array_sum($a) * ($m + 1);
    for ($i = 0; $i < $n; ++$i) {
        if ($b[$i] > $m) {
            $sm += $a[$i];
        } else {
            break;
        }
    }
    echo $sm.PHP_EOL;
}

<?php

$times = (int) trim(fgets(STDIN));
for ($idx = 0; $idx < $times; ++$idx) {
    list($ni[], $xi[]) = array_map('intval', explode(' ', trim(fgets(STDIN))));
    $ai[] = array_map('intval', explode(' ', trim(fgets(STDIN))));
}
for ($idx = 0; $idx < $times; ++$idx) {
    $n = &$ni[$idx];
    $x = &$xi[$idx];
    $a = &$ai[$idx];
    $mn = PHP_INT_MAX;
    for ($i = 0; $i < $n; ++$i) {
        $cnt = 0;
        $tmp = $a[$i];
        while (0 == $tmp % $x) {
            $tmp /= $x;
            ++$cnt;
            if ($cnt == $mn) {
                break;
            }
        }
        $mn > $cnt && $mn = $cnt;
        $a[$i] *= $mn + 1;
    }
    echo array_sum($a).PHP_EOL;
}


<?php
	$n = trim(fgets(STDIN));
	$b = explode(' ', trim(fgets(STDIN)));
	$mx = max($b);
	$mn = min($b);
	$cmx = $cmn = 0;
	foreach($b as $v)
	{
		$cmx += ($v == $mx);
		$cmn += ($v == $mn);
	}
	echo $mx-$mn.' '.($mx == $mn ? floor($n*($n-1)/2) : $cmx*$cmn);
?>

<?php

function FindFlowers($flowers,$beauty){

    $beauty = explode(" ", $beauty);
    $count =0;
    $max_beauty = max($beauty);
    $min_beauty = min($beauty);
    $diff_max_beauty = $max_beauty-$min_beauty;

    $maxposition  = count(array_keys($beauty, max($beauty)));
    $min_position = count(array_keys($beauty, min($beauty)));

    if($diff_max_beauty==0){
        $count = $maxposition*($maxposition-1)/2;
    }
 else {
        $count = $maxposition*$min_position;
    }
    echo $diff_max_beauty." ". $count;

}

$flowers = trim(fgets(STDIN));
$beauty  = trim(fgets(STDIN));

FindFlowers($flowers,$beauty);

<?php
// 459B     Пашмак и цветы
$in = fopen("php://stdin", 'r');

$input_line = fgets($in);
$n = (int) $input_line;

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

sort($arr);

$max = $arr[$n-1] - $arr[0];

$colfirst = $colend = 1;
for ($i=1; $i < $n; $i++) {
    if ($arr[$i] === $arr[0]) {
        $colfirst ++;
    } else {
        break;
    }
}

for ($i=$n-2; $i >= 0; $i--) {
    if ($arr[$i] === $arr[$n-1]) {
        $colend ++;
    } else {
        break;
    }
}

if ($i < 0) {
    $sum = 0;
    for ($j=1; $j < $n; $j++) {
        $sum += $j;
    }
    $col = $n * ($n - 1) - $sum;
} else {
    $col = $colfirst * $colend;
}

echo "$max $col";

?>

<?
$n = trim(fgets(STDIN));
$m = explode(" ",trim(fgets(STDIN)));
$mx = max($m);
$mn = min($m);
foreach($m as $v){
    if($v == $mx)$cmx++;
    if($v == $mn)$cmn++;
}
if($mx != $mn)echo ($mx-$mn)." ".($cmx*$cmn);
else echo ($mx-$mn)." ".(($n*($n-1))/2);
?>

<?php

function FindFlowers($flowers,$beauty){

    $beauty = explode(" ", $beauty);
    $count =0;
    $max_beauty = max($beauty);
    $min_beauty = min($beauty);
    $diff_max_beauty = $max_beauty-$min_beauty;

    $maxposition  = count(array_keys($beauty, max($beauty)));
    $min_position = count(array_keys($beauty, min($beauty)));

    if($diff_max_beauty==0){
        $count = factorial($maxposition)/(factorial(2)*factorial($maxposition-2));
    }
 else {
        $count = $maxposition*$min_position;
    }
    echo $diff_max_beauty." ". $count;

}

function factorial($number) {

    if ($number < 2) {
        return 1;
    } else {
        return ($number * factorial($number-1));
    }
}

$flowers = trim(fgets(STDIN));
$beauty  = trim(fgets(STDIN));

FindFlowers($flowers,$beauty);

<?php

function FindFlowers($flowers,$beauty){

    $beauty = explode(" ", $beauty);
    $count =0;
    $max_beauty = max($beauty);
    $min_beauty = min($beauty);
    $diff_max_beauty = $max_beauty-$min_beauty;

    $maxposition  = array_keys($beauty, max($beauty));
    $min_position = array_keys($beauty, min($beauty));

    $count = count($min_position)*  count($maxposition);
    if($count==0)
        $count=1;
    echo $diff_max_beauty." ". $count;

}

$flowers = trim(fgets(STDIN));
$beauty  = trim(fgets(STDIN));

FindFlowers($flowers,$beauty);

<?php
// 459B     Пашмак и цветы
$in = fopen("php://stdin", 'r');

$input_line = fgets($in);
$n = (int) $input_line;

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

sort($arr);

$max = $arr[$n-1] - $arr[0];

$colfirst = $colend = 1;
for ($i=1; $i < $n; $i++) {
    if ($arr[$i] === $arr[0]) {
        $colfirst ++;
    } else {
        break;
    }
}

for ($i=$n-2; $i > 0; $i--) {
    if ($arr[$i] === $arr[$n-1]) {
        $colend ++;
    } else {
        break;
    }
}

$col = $colfirst * $colend;

echo "$max $col";

?>

<?
$n = trim(fgets(STDIN));
$m = explode(" ",trim(fgets(STDIN)));
$mx = max($m);
$mn = min($m);
foreach($m as $v){
    if($v == $mx)$cmx++;
    if($v == $mn)$cmn++;
}
echo ($mx-$mn)." ".($cmx*$cmn);
?>

<?
$n = trim(fgets(STDIN));
$m = explode(" ",trim(fgets(STDIN)));
$mx = max($m);
$mn = min($m);
foreach($m as $v){
    if($v == $mx)$cmx++;
    if($v == $mn)$cmn++;
}
if($mx != $mn)echo ($mx-$mn)." ".($cmx*$cmn);
else echo ($mx-$mn)." ".($n*($n-1));
?>


<?php
// Retrieve input
$stdin = fopen('php://stdin', 'r');
function g() {
	global $stdin;
	return explode(" ", trim(fgets($stdin)));
}

// Codeforces Problem 225A
// Codeforces Contest 139 Div 2 Problem A
$raw = g();
$n = $raw[0];
$raw = g();
$x = $raw[0];
$done = false;
for ($i = 0; $i < $n; $i++) {
	$raw = g();
	$a = $raw[0];
	$b = $raw[1];
	if (($a == $x) || ($b == $x) || ($a == (7 - $x)) || ($b == (7 - $x))) {
		$done = true;
	}
}
if (!$done) {echo "YES";} else {echo "NO";}

// End of submission file
// Submission by chaotic_iak
?>

<?php
 $i= trim(fgets(STDIN));
 $point=7-trim(fgets(STDIN));
 for ($p=1;$p<=$i;$p++)
     { list($a,$b)=explode(" ",trim(fgets(STDIN)));
      if (($point==$a)or($point==$b)or($point==(7-$a)) or ($point==(7-$b))) {$res=0; break;}
       else {$res++;}; };
 if ($res==$i) {print ("YES");} else {print ("NO");};
?>

<?php
 $i= trim(fgets(STDIN));
 $point=7-trim(fgets(STDIN));
 for ($p=1;$p<=$i;$p++)
     { list($a,$b)=explode(" ",trim(fgets(STDIN)));
      if (($point==$a)or($point==$b)or($point==(7-$a)) or ($point==(7-$b))) {$res=0;}
       else {$res++;}; };
 if ($res==$i) {print ("YES");} else {print ("NO");};
?>

<?PHP
$n = fgets(STDIN);
$u = fgets(STDIN);
list($l, $r) = explode(" ", fgets(STDIN));
$res = "YES";

for ($i = 1; $i < $n; $i++) {
    $d = 7 - $u;
    list($l, $r) = explode(" ", fgets(STDIN));
    $di = array(intval($l), intval($r), 7 - $l, 7 - $r);
    //print_r ($di);
    if (in_array($d, $di))
        $res = "NO";
}
echo "$res\n";
?>

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));
$e = 0;
list($c, $d) = explode(" ", trim(fgets(STDIN)));
for($x = 1; $x < $a; $x++)
{
    list($c, $d) = explode(" ", trim(fgets(STDIN)));
    if(($b == $c) || ($b == $d) || ((7 - $b) == $c) || ((7 - $b) == $d))
    {
        $e++;
        break;
    }
}
if($e == 0)
{
    print "YES";
}
else
{
    print "NO";
}
?>

<?php
// Retrieve input
$stdin = fopen('php://stdin', 'r');
function g() {
	global $stdin;
	return explode(" ", trim(fgets($stdin)));
}

// Codeforces Problem 225A
// Codeforces Contest 139 Div 2 Problem A
$raw = g();
$n = $raw[0];
$raw = g();
$x = $raw[0];
$done = false;
for ($i = 0; $i < $n; $i++) {
	$raw = g();
	$a = $raw[0];
	$b = $raw[1];
	if (($a == $x) || ($b == $x) || ($a == (7 - $x)) || ($b == (7 - $x))) {
		echo "NO";
		$done = true;
	}
}
if (!$done) {echo "YES";}

// End of submission file
// Submission by chaotic_iak
?>

<?PHP
$n = fgets(STDIN);
$u = fgets(STDIN);
list($l, $r) = explode(" ", fgets(STDIN));
$res = "SI";

for ($i = 1; $i < $n; $i++) {
    $d = 7 - $u;
    list($l, $r) = explode(" ", fgets(STDIN));
    $di = array(intval($l), intval($r), 7 - $l, 7 - $r);
    //print_r ($di);
    if (in_array($d, $di))
        $res = "NO";
}
echo "$res\n";
?>


<?php
// 431A     Черный квадрат
$in = fopen("php://stdin", 'r');

//$input_line = fgets($in);
//$n = (int) $input_line;

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$input_line = fgets($in);
$str = substr($input_line, 0, strlen($input_line)-2);
$len = strlen($str);

$col = 0;
for ($i=0; $i < $len ; $i++) {
    $k = (int) $str[$i] -1;
    $col += $arr[$k];
}

echo $col;

?>

<?php
list($a, $b, $c, $d) = explode(" ", trim(fgets(STDIN)));
$e = str_split(trim(fgets(STDIN)));
$f = 0;
for($x = 0; $x < count($e); $x++)
{
    switch($e[$x])
    {
        case 1:
        $f += $a;
        break;
        case 2:
        $f += $b;
        break;
        case 3:
        $f += $c;
        break;
        case 4:
        $f += $d;
        break;
    }
}
print $f;
?>

<?php

//=====================================
// link: https://codeforces.com/contest/431/problem/A
//=====================================

// get inputs
$calories = explode(' ', trim(fgets(STDIN)));
$strips   = trim(fgets(STDIN));

// solution
$burning_calories = 0;

for ($i = 0; $i < strlen($strips); $i++) {
    $burning_calories += $calories[$strips[$i] - 1];
}

echo "$burning_calories\n";

<?php

$stdout = fopen(getenv("OUTPUT_PATH"), "w");
$stdin = fopen("php://stdin", "r");

fscanf($stdin, "%[^\n]", $str);
$arr = array_map( 'intval' , preg_split('/ /' , $str , -1 , PREG_SPLIT_NO_EMPTY ) );

fscanf($stdin, "%s", $s );

$result = 0 ;

for( $i=0 ; $i< strlen($s) ; $i++)
{
     $result += $arr[ $s[$i] - 1 ] ;
}
echo $result ;

fwrite($stdout);

fclose($stdin);
fclose($stdout);

?>

<?php
$c = explode(" ", trim(fgets(STDIN)));
$s = str_split(trim(fgets(STDIN)));
$sum = 0;
foreach($s as $value){
    $sum += $c[$value-1];
}
echo $sum;
?>

<?php

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

	$a = explode(" ", fgets($temp));

	for ($i=0; $i < count($a) ; $i++) {
		$a[$i] = intval($a[$i]);
	}

	$s = fgets($temp);

	$sum = 0;
	for($i=0; $i < strlen($s)-2 ; $i++) {
		$sum += $a[intval($s[$i])-1];
	}

	echo $sum;

?>

<?php
error_reporting("off");
//431A: Black Square
$cal = explode(" ", trim(fgets(STDIN))); //$cal = explode(" ", "1 2 3 4");
$string = str_split(trim(fgets(STDIN))); //$string = str_split("123214");
$count = array_count_values($string);
$calcount = 0;
foreach ($count as $key => $value) {
    $calcount += $cal[$key - 1] * $value;
}
echo $calcount;
?>

<?php
$c = explode(" ", trim(fgets(STDIN)));
$s = explode("", trim(fgets(STDIN)));
$sum = 0;
foreach($s as $value){
    $sum += $c[$value-1];
}
echo $sum;
?>

<?php
    $in = 'php://stdin';

    $input = fopen($in, "r");

    $c = explode(" ", trim(fgets($input)));
    $s = trim(fgets($input));

    function MultArray ($a, $b) { return $a*$b; }

    $o = array_count_values(str_split($s));
    ksort($o);

    echo array_sum(array_map("MultArray", $c, $o));
?>


<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = array();
array_unshift($c, " ");
unset($c[0]);
$d = array();
array_unshift($d, " ");
unset($d[0]);
$g = array();
for($x = 1; $x <= $a; $x++)
{
    for($y = 1; $y <= $b; $y++)
    {
        $e = array_search($x, $c);
        $f = array_search($y, $d);
        array_push($c, $x);
        array_push($d, $y);
        if(($e > 0) && ($f > 0))
        {
            continue;
        }
        else
        {
            array_push($g, $x . " " . $y);
        }
    }
}
print count($g) . "\n";
for($z = 0; $z < count($g) - 1; $z++)
{
    print $g[$z] . "\n";
}
print $g[$z];
?>

<?php
/** chaotic_iak's solution in PHP 5.4 to the problem 322A (Round 190 Div 2 Problem A) in Codeforces **/

/** Module for retrieving input **/

// If this is a local test (a variable declared in a specialized page to open this script), open from localtest.txt instead of php://stdin (because I'm too lazy to supply the arguments by stdin)
if (isset($localtest)) {$stdin = fopen("localtest.txt", "r");} else {$stdin = fopen("php://stdin", "r");}

// A function to retrieve the next line. Takes one optional argument $single, which should be true if the next line (based on problem's specification) has only one number/string/whatever, so I don't have to do silly things like $raw = g(); $num = $raw[0];
function g($single = false) {

	// Take the global $stdin variable aka the file handler so we can access it from this function
	global $stdin;

	// Take the input from the handler, trim it, and explode it based on space (" 1 2 3 " becomes array("1", "2", "3")); trim is usually to remove trailing \r
	$input = explode(" ", trim(fgets($stdin)));

	// If the line is not a single number, return the whole array, otherwise simply return the single string that we exploded for nothing
	if (!$single) {return $input;} else {return $input[0];}
}

/** Imported functions from chaotic_iak's library **/
// none

/** chaotic_iak's solution **/
// This is a competitive programming solution. The code is not remarked (because I want speed).

$raw = g();
$n = $raw[0];
$m = $raw[1];

echo $n + $m - 1 . "\n";
for ($i = 1; $i <= $n; $i++) {
	echo $i . " 1\n";
}
for ($i = 2; $i <= $m; $i++) {
	echo "1 " . $i . "\n";
}

<?php

list($n, $m) = explode(' ', getline());

$n = intval($n);
$m = intval($m);

$bisdance = array();
$gisdance = array();

init_array($bisdance, $n);
init_array($gisdance, $m);

$answer = 0;
$output = PHP_EOL;

for ($i = 1; $i <= $n; $i ++) {
  for ($j = 1; $j <= $m; $j ++) {
    if (!$bisdance[$i] || !$gisdance[$j]) {
      $bisdance[$i] = true;
      $gisdance[$j] = true;
      $answer ++;
      $output .= $i . ' ' . $j . PHP_EOL;
    }
  }
}

print $answer;
print $output;

function init_array(&$array, $n) {
  for ($i = 1; $i <= $n; $i ++) {
    $array[$i] = false;
  }
}

function getline() {
  return trim(fgets(STDIN));
}


<?php
$no = 'NO';
$yes = 'YES';
$mod = 1000000007;
$alph = 'abcdefghijklmnopqrstuvwxyz';
$inter = "\n";

  $t = getT();
//   $point = ord('a') + ord('b') ;

  for($q=0; $q <$t; $q++) {
      $ans = true;
      $n = getT();
      $s = getT();
  $temp = [
      'B' => 0,
      'R' => 0
      ];
      foreach(str_split($s) as $ch) {
          if($ch == 'W') {
              if(($temp['B'] == 0 && $temp['R'] != 0) || ($temp['B'] != 0 && $temp['R'] == 0)) {
                //  $bA = false;
                 break;
              }
               $temp = [
                  'B' => 0,
                  'R' => 0
                 ];
          } else {
              $temp[$ch] += 1;
          }
      }
    if(($temp['B'] == 0 && $temp['R'] != 0) || ($temp['B'] != 0 && $temp['R'] == 0)) {
                 pr($no);
              } else {
                  pr($yes);
              }

  }
// bcadd — Add two arbitrary precision numbers
// bccomp — Compare two arbitrary precision numbers
// bcdiv — Divide two arbitrary precision numbers
// bcmod — Get modulus of an arbitrary precision number
// bcmul — Multiply two arbitrary precision numbers
// bcpow — Raise an arbitrary precision number to another
// bcpowmod — Raise an arbitrary precision number to another, reduced by a specified modulus
// bcscale — Set or get default scale parameter for all bc math functions
// bcsqrt — Get the square root of an arbitrary precision number
// bcsub — Subtract one arbitrary precision number from another
function getDar($n) {
    $dar = [2];
    for($i=0; $i<$n; $i++) {
        $dar[] = $dar[$i] * 2;
    }
    return $dar;
}
function getDar2($n, $k) {
    if($k == 0) {
        return 1;
    }
    return bcmul($n, getDar2($n, $k-1));
}
  function ComandR($aCordinate) {
      return [$aCordinate[0] + 1, $aCordinate[1]];
  }
  function ComandL($aCordinate) {
      return [$aCordinate[0] - 1, $aCordinate[1]];
  }
  function ComandU($aCordinate) {
      return [$aCordinate[0], $aCordinate[1] + 1];
  }
  function ComandD($aCordinate) {
      return [$aCordinate[0], $aCordinate[1] - 1];
  }

  function getT() {
      return trim(fgets(STDIN));
  }
  function getA() {
      return explode(' ', trim(fgets(STDIN)));
  }

  function is_prime($n) {
    $d = 2;
        while ($n % $d != 0) {
        	 $d += 1;
        }

        return $d == $n;
    }
function getMinValue($x, $y) {
        return $x > $y ? $y : $x;
    }
    function getMaxValue($x, $y) {
        return $x > $y ? $x : $y;
    }

    function getMaxValueBc($x, $y) {
        return bccomp($x, $y) == 1 ? $x : $y;
    }
    function getRotate($a, $s, $e, $ind) {
 		$ar = [];
        for ($i= $s; $i<=$e; $i++) {
        	$ar[$i] = $a[$i];
        	$j = $i + $ind > $e ? $i + $ind - $e - 1 : $i + $ind;
        	// echo $i, ' ', $j, ' ', $j < $i ? $ar[$j] : $a[$j] , "\n";
        	$a[$i] = $j < $i ? $ar[$j] : $a[$j];
        }
        return $a;
    }
    function pr($s) {
        if(is_array($s)) {
            echo implode( ' ', $s), PHP_EOL;
        } else {
            echo $s, PHP_EOL;
        }
    }
    function fab2($n, $a) {
        if($n <= 1) {
            return $a[$n];
        }
       for($i=2; $i<=$n; $i++) {
           $a[$i] = $a[$i-1] + $a[$i-2];
       }

        return $a[$n];
    }

<?php
$server = "php://stdin";
$local = "input.txt";
$handle = fopen($server, "r");
$t = trim(fgets($handle));
for ($i = 0; $i < $t; $i++) {
    $l = trim(fgets($handle));
    $s = trim(fgets($handle));
    $arr= explode( 'W', $s);

    $hasOdd= false;

    for($j = 0;$j<count($arr);$j++){
        if(strlen($arr[$j]) > 0 && ( strpos( $arr[$j], 'R') === false || strpos( $arr[$j], 'B') === false)){
            $hasOdd =true;
        }
    }
    echo ($hasOdd? 'No':'Yes')."\n";
}

<?php
$server = "php://stdin";
$local = "input.txt";
$handle = fopen($server, "r");
$t = trim(fgets($handle));
for ($i = 0; $i < $t; $i++) {
    $l = trim(fgets($handle));
    $s = trim(fgets($handle));
    if ($l == 1) {
        echo 'No' . "\n";
    }elseif( $l == 2) {
        echo (strpos($s,"W") > -1 ? 'No': 'Yes'). "\n";
    }
        else {
        $hasOdd = false;
        $x='';
        $res = [];
        for ($k = 0; $k < $l; $k++) {
            if($s[$k] != 'W') {
                $x .= $s[$k];
            }else {
                $res[]= $x;
                $x = '';
            }
        }
        for($k = 0; $k < count($res); $k++)
        {
            if(strlen($res[$k])%2!=0 && strlen($res[$k]) < $l-1){
                $hasOdd = true;
            }
        }

        echo ($hasOdd ? "No": "Yes"). "\n";
    }
}


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

while (STDIN && $paragraphs != 0) {
    $paragraph[] = explode(' ', trim(fgets(STDIN)));
    $paragraphs--;
}

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

$i = 0;
foreach ($paragraph as $val) {
    if (filter_var(
        $readed,
        FILTER_VALIDATE_INT,
        array(
      'options' => array(
          'min_range' => $val[0],
          'max_range' => $val[1]
      )
    )
    )) {
        echo $unreaded - $i . PHP_EOL;
    } else {
        $i++;
    }
}

<?php

$n = trim(fgets(STDIN));

$r = [];

for($i = 0; $i<$n; $i++){
	$data = explode(' ', trim(fgets(STDIN)));
	$r[] = $data[1];
}

$k = trim(fgets(STDIN));
$c = 0;
for($i = 0; $i<$n; $i++){
	if($k > $r[$i]){
		$c++;
	}
}

fwrite(STDOUT, $n-$c);

?>

<?php
$n = fgets(STDIN);
$array;
for($i = 0; $i < $n; $i++){
    list($array[$i][0], $array[$i][1]) = explode(' ', fgets(STDIN));
}
$k = (int) fgets(STDIN);
for($i = 0; $i < $n; $i++){
    if($array[$i][0] <= $k && $array[$i][1] >= $k){
        $n = $n - $i;
        echo $n;
        return 0;
    }
}
?>

<?php

    $stdin = fopen('php://stdin', 'r');
    // $stdout = fopen('php://stdout', 'w');

    // $input = $_GET['url'];
    // $inputArray = explode('/', $input);

    // $nrParams = count($inputArray);

    // $nrChapters = (int)$nrChapters;

    $input = stream_get_contents($stdin);

    $arr = preg_split("/\r\n|\n|\r/", $input);

    $nrChapters = (int)$arr[0];

    for($i = 1; $i <= $nrChapters; $i++) {
        $chaptersDescription[$i] = array_map(function($val){ return (int)$val;}, explode(" ", trim($arr[$i])));
        // echo $inputArray[$i][0] . " : " . $inputArray[$i][1] . "\n";
    }

    $unreadPageIndex = (int)trim($arr[count($arr) - 2]);
    // echo $arr[count($arr) - 2] . "\n";
    // echo $unreadPageIndex . "\n";
    // exit();
    // for($i = 0; $i < $nrChapters; $i++) {

    // }

    // if ($nrParams < 3) {
    //     echo "There are less parameters than expected!";
    //     exit();
    // }

    // $nrChapters = $inputArray[0];
    // $nrLinesDescribingChapters = $nrParams - 2;
    // $unreadPageIndex = $inputArray[$nrParams - 1];

    // unset($inputArray[0], $inputArray[$nrParams - 1]);

    // if ($nrChapters != $nrParams - 2) {
    //     echo "Wrong number of parameters!";
    //     exit();
    // }

    foreach ($chaptersDescription as $index => $chapter) {
        if ($unreadPageIndex <= (int)$chapter[1]) {
            $chaptersRemained = $nrChapters - $index + 1;
            echo $chaptersRemained;
            exit();
        }

        if ($index == $nrChapters){
            echo "Wrong unread page index!";
            exit();
        }
    }

<?php
$a = trim(fgets(STDIN));
$b = array();
$c = 1;
for($x = 0; $x < $a; $x++)
{
     list($d, $e) = explode(" ", trim(fgets(STDIN)));
     for($y = $d; $y <= $e; $y++)
     {
          $b[$y] = $c;
     }
     $c++;
}
$f = trim(fgets(STDIN));
print $a - $b[$f] + 1;
?>

<?php
$n = fgets(STDIN);
$array;
$count = 0;
for($i = 0; $i < $n; $i++){
    list($array[$i][0], $array[$i][1]) = explode(' ', fgets(STDIN));
}
$k = fgets(STDIN);
for($i = 0; $i < $n; $i++){
    if($array[$i][0] <= $k && $array[$i][1] >= $k){
        echo $n - $i;
        return 0;
    }
}
?>

<?php

    $stdin = fopen('php://stdin', 'r');
    // $stdout = fopen('php://stdout', 'w');

    // $input = $_GET['url'];
    // $inputArray = explode('/', $input);

    // $nrParams = count($inputArray);

    // $nrChapters = (int)$nrChapters;

    $input = stream_get_contents($stdin);

    $arr = preg_split("/\r\n|\n|\r/", $input);

    $nrChapters = (int)$arr[0];

    for($i = 1; $i <= $nrChapters; $i++) {
        $inputArray[$i] = array_map(function($val){ return (int)$val;}, explode(" ", trim($arr[$i])));
    }

    $unreadPageIndex = (int)trim(end($arr));
    echo $unreadPageIndex;
    exit();
    // for($i = 0; $i < $nrChapters; $i++) {

    // }

    // if ($nrParams < 3) {
    //     echo "There are less parameters than expected!";
    //     exit();
    // }

    // $nrChapters = $inputArray[0];
    // $nrLinesDescribingChapters = $nrParams - 2;
    // $unreadPageIndex = $inputArray[$nrParams - 1];

    // unset($inputArray[0], $inputArray[$nrParams - 1]);

    // if ($nrChapters != $nrParams - 2) {
    //     echo "Wrong number of parameters!";
    //     exit();
    // }

    $chaptersDescription = array_map(
        function ($value) {
            $array = explode(' ', $value);

            return $array;
        },
        $inputArray
    );

    foreach ($chaptersDescription as $index => $chapter) {
        if ($unreadPageIndex <= (int)$chapter[1]) {
            $chaptersRemained = $nrChapters - $index + 1;
            echo $chaptersRemained;
            exit();
        }

        if ($index == $nrChapters){
            echo "Wrong unread page index!";
            exit();
        }
    }

<?php

    $stdin = fopen('php://stdin', 'r');
    // $stdout = fopen('php://stdout', 'w');

    // $input = $_GET['url'];
    // $inputArray = explode('/', $input);

    // $nrParams = count($inputArray);

    // $nrChapters = (int)$nrChapters;

    $input = stream_get_contents($stdin);

    $arr = preg_split("/\r\n|\n|\r/", $input);

    $nrChapters = (int)$arr[0];

    for($i = 1; $i <= $nrChapters; $i++) {
        $inputArray[$i] = array_map(function($val){ return (int)$val;}, explode(" ", trim($arr[$i])));
    }

    $unreadPageIndex = (int)trim($arr[count($arr) - 1]);
    echo $unreadPageIndex;
    exit();
    // for($i = 0; $i < $nrChapters; $i++) {

    // }

    // if ($nrParams < 3) {
    //     echo "There are less parameters than expected!";
    //     exit();
    // }

    // $nrChapters = $inputArray[0];
    // $nrLinesDescribingChapters = $nrParams - 2;
    // $unreadPageIndex = $inputArray[$nrParams - 1];

    // unset($inputArray[0], $inputArray[$nrParams - 1]);

    // if ($nrChapters != $nrParams - 2) {
    //     echo "Wrong number of parameters!";
    //     exit();
    // }

    $chaptersDescription = array_map(
        function ($value) {
            $array = explode(' ', $value);

            return $array;
        },
        $inputArray
    );

    foreach ($chaptersDescription as $index => $chapter) {
        if ($unreadPageIndex <= (int)$chapter[1]) {
            $chaptersRemained = $nrChapters - $index + 1;
            echo $chaptersRemained;
            exit();
        }

        if ($index == $nrChapters){
            echo "Wrong unread page index!";
            exit();
        }
    }

<?php

    $stdin = fopen('php://stdin', 'r');
    // $stdout = fopen('php://stdout', 'w');

    // $input = $_GET['url'];
    // $inputArray = explode('/', $input);

    // $nrParams = count($inputArray);

    // $nrChapters = (int)$nrChapters;

    $input = stream_get_contents($stdin);

    $arr = preg_split("/\r\n|\n|\r/", $input);

    $nrChapters = (int)$arr[0];

    for($i = 1; $i <= $nrChapters; $i++) {
        $inputArray[$i] = array_map(function($val){ return (int)$val;}, explode(" ", trim($arr[$i])));
        echo $inputArray[$i][0] . " : " . $inputArray[$i][1] . "\n";
    }

    $unreadPageIndex = (int)trim($arr[count($arr) - 2]);
    // echo $arr[count($arr) - 2] . "\n";
    // echo $unreadPageIndex . "\n";
    // exit();
    // for($i = 0; $i < $nrChapters; $i++) {

    // }

    // if ($nrParams < 3) {
    //     echo "There are less parameters than expected!";
    //     exit();
    // }

    // $nrChapters = $inputArray[0];
    // $nrLinesDescribingChapters = $nrParams - 2;
    // $unreadPageIndex = $inputArray[$nrParams - 1];

    // unset($inputArray[0], $inputArray[$nrParams - 1]);

    // if ($nrChapters != $nrParams - 2) {
    //     echo "Wrong number of parameters!";
    //     exit();
    // }

    $chaptersDescription = array_map(
        function ($value) {
            $array = explode(' ', $value);

            return $array;
        },
        $inputArray
    );

    foreach ($chaptersDescription as $index => $chapter) {
        if ($unreadPageIndex <= (int)$chapter[1]) {
            $chaptersRemained = $nrChapters - $index + 1;
            echo $chaptersRemained;
            exit();
        }

        if ($index == $nrChapters){
            echo "Wrong unread page index!";
            exit();
        }
    }

<?php

    $stdin = fopen('php://stdin', 'r');
    $stdout = fopen('php://stdout', 'w');

    // $input = $_GET['url'];
    // $inputArray = explode('/', $input);

    // $nrParams = count($inputArray);

    // $nrChapters = (int)$nrChapters;

    echo "Epta : '" . stream_get_contents($stdin) . "'";
    exit();

    // for($i = 0; $i < $nrChapters; $i++) {

    // }

    // if ($nrParams < 3) {
    //     echo "There are less parameters than expected!";
    //     exit();
    // }

    // $nrChapters = $inputArray[0];
    // $nrLinesDescribingChapters = $nrParams - 2;
    // $unreadPageIndex = $inputArray[$nrParams - 1];

    // unset($inputArray[0], $inputArray[$nrParams - 1]);

    if ($nrChapters != $nrParams - 2) {
        echo "Wrong number of parameters!";
        exit();
    }

    $chaptersDescription = array_map(
        function ($value) {
            $array = explode(' ', $value);

            return $array;
        },
        $inputArray
    );

    foreach ($chaptersDescription as $index => $chapter) {
        if ($unreadPageIndex <= $chapter[1]) {
            $chaptersRemained = $nrChapters - $index + 1;
            echo $chaptersRemained;
            exit();
        }

        if ($index == $nrChapters){
            echo "Wrong unread page index!";
            exit();
        }
    }

<?php

    $stdin = fopen('php://stdin', 'r');
    $stdout = fopen('php://stdout', 'w');

    // $input = $_GET['url'];
    // $inputArray = explode('/', $input);

    // $nrParams = count($inputArray);

    $nrChapters = stream_get_contents($dir);
    // $nrChapters = (int)$nrChapters;

    fwrite($stdout, "Epta : '" . $nrChapters . "'");
    exit();

    // for($i = 0; $i < $nrChapters; $i++) {

    // }

    // if ($nrParams < 3) {
    //     echo "There are less parameters than expected!";
    //     exit();
    // }

    // $nrChapters = $inputArray[0];
    // $nrLinesDescribingChapters = $nrParams - 2;
    // $unreadPageIndex = $inputArray[$nrParams - 1];

    // unset($inputArray[0], $inputArray[$nrParams - 1]);

    if ($nrChapters != $nrParams - 2) {
        echo "Wrong number of parameters!";
        exit();
    }

    $chaptersDescription = array_map(
        function ($value) {
            $array = explode(' ', $value);

            return $array;
        },
        $inputArray
    );

    foreach ($chaptersDescription as $index => $chapter) {
        if ($unreadPageIndex <= $chapter[1]) {
            $chaptersRemained = $nrChapters - $index + 1;
            echo $chaptersRemained;
            exit();
        }

        if ($index == $nrChapters){
            echo "Wrong unread page index!";
            exit();
        }
    }

<?php

    $stdin = fopen('php://stdin', 'r');
    // $stdout = fopen('php://stdout', 'w');

    // $input = $_GET['url'];
    // $inputArray = explode('/', $input);

    // $nrParams = count($inputArray);

    // $nrChapters = (int)$nrChapters;

    $input = stream_get_contents($stdin);

    $arr = preg_split("/\r\n|\n|\r/", $input);

    $nrChapters = (int)$arr[0];

    for($i = 1; $i <= $nrChapters; $i++) {
        $inputArray[$i] = array_map(function($val){ return (int)$val;}, explode(" ", trim($arr[$i])));
    }

    $unreadPageIndex = (int)trim($arr[count($arr) - 1]);
    // for($i = 0; $i < $nrChapters; $i++) {

    // }

    // if ($nrParams < 3) {
    //     echo "There are less parameters than expected!";
    //     exit();
    // }

    // $nrChapters = $inputArray[0];
    // $nrLinesDescribingChapters = $nrParams - 2;
    // $unreadPageIndex = $inputArray[$nrParams - 1];

    // unset($inputArray[0], $inputArray[$nrParams - 1]);

    // if ($nrChapters != $nrParams - 2) {
    //     echo "Wrong number of parameters!";
    //     exit();
    // }

    $chaptersDescription = array_map(
        function ($value) {
            $array = explode(' ', $value);

            return $array;
        },
        $inputArray
    );

    foreach ($chaptersDescription as $index => $chapter) {
        if ($unreadPageIndex <= $chapter[1]) {
            $chaptersRemained = $nrChapters - $index + 1;
            echo $chaptersRemained;
            exit();
        }

        if ($index == $nrChapters){
            echo "Wrong unread page index!";
            exit();
        }
    }

<?php

    $stdin = fopen('php://stdin', 'r');
    // $stdout = fopen('php://stdout', 'w');

    // $input = $_GET['url'];
    // $inputArray = explode('/', $input);

    // $nrParams = count($inputArray);

    // $nrChapters = (int)$nrChapters;

    $input = stream_get_contents($stdin);

    $arr = preg_split("/\r\n|\n|\r/", $input);

    $nrChapters = (int)$arr[0];

    for($i = 1; $i <= $nrChapters; $i++) {
        $inputArray[$i] = array_map(function($val){ return (int)$val;}, explode(" ", trim($arr[$i])));
    }

    $unreadPageIndex = (int)trim($arr[count($arr) - 2]);
    // echo $arr[count($arr) - 2] . "\n";
    // echo $unreadPageIndex . "\n";
    // exit();
    // for($i = 0; $i < $nrChapters; $i++) {

    // }

    // if ($nrParams < 3) {
    //     echo "There are less parameters than expected!";
    //     exit();
    // }

    // $nrChapters = $inputArray[0];
    // $nrLinesDescribingChapters = $nrParams - 2;
    // $unreadPageIndex = $inputArray[$nrParams - 1];

    // unset($inputArray[0], $inputArray[$nrParams - 1]);

    // if ($nrChapters != $nrParams - 2) {
    //     echo "Wrong number of parameters!";
    //     exit();
    // }

    $chaptersDescription = array_map(
        function ($value) {
            $array = explode(' ', $value);

            return $array;
        },
        $inputArray
    );

    foreach ($chaptersDescription as $index => $chapter) {
        if ($unreadPageIndex <= (int)$chapter[1]) {
            $chaptersRemained = $nrChapters - $index + 1;
            echo $chaptersRemained;
            exit();
        }

        if ($index == $nrChapters){
            echo "Wrong unread page index!";
            exit();
        }
    }

<?php

    $stdin = fopen('php://stdin', 'r');
    $stdout = fopen('php://stdout', 'w');

    // $input = $_GET['url'];
    // $inputArray = explode('/', $input);

    // $nrParams = count($inputArray);

    readline($nrChapters);
    readline($sec);
    // $nrChapters = (int)$nrChapters;

    fwrite($stdout, "Epta : '" . $nrChapters . "' | '$sec'");
    exit();

    // for($i = 0; $i < $nrChapters; $i++) {

    // }

    // if ($nrParams < 3) {
    //     echo "There are less parameters than expected!";
    //     exit();
    // }

    // $nrChapters = $inputArray[0];
    // $nrLinesDescribingChapters = $nrParams - 2;
    // $unreadPageIndex = $inputArray[$nrParams - 1];

    // unset($inputArray[0], $inputArray[$nrParams - 1]);

    if ($nrChapters != $nrParams - 2) {
        echo "Wrong number of parameters!";
        exit();
    }

    $chaptersDescription = array_map(
        function ($value) {
            $array = explode(' ', $value);

            return $array;
        },
        $inputArray
    );

    foreach ($chaptersDescription as $index => $chapter) {
        if ($unreadPageIndex <= $chapter[1]) {
            $chaptersRemained = $nrChapters - $index + 1;
            echo $chaptersRemained;
            exit();
        }

        if ($index == $nrChapters){
            echo "Wrong unread page index!";
            exit();
        }
    }

<?php

    $stdin = fopen('php://stdin', 'r');
    // $stdout = fopen('php://stdout', 'w');

    // $input = $_GET['url'];
    // $inputArray = explode('/', $input);

    // $nrParams = count($inputArray);

    // $nrChapters = (int)$nrChapters;

    $input = stream_get_contents($stdin);

    $arr = preg_split("/\r\n|\n|\r/", $input);

    $nrChapters = (int)$arr[0];

    for($i = 1; $i <= $nrChapters; $i++) {
        $inputArray[] = array_map(function($val){ return (int)$val;}, explode(" ", trim($arr[$i])));
    }

    $unreadPageIndex = (int)trim($arr[count($arr) - 1]);
    // for($i = 0; $i < $nrChapters; $i++) {

    // }

    // if ($nrParams < 3) {
    //     echo "There are less parameters than expected!";
    //     exit();
    // }

    // $nrChapters = $inputArray[0];
    // $nrLinesDescribingChapters = $nrParams - 2;
    // $unreadPageIndex = $inputArray[$nrParams - 1];

    // unset($inputArray[0], $inputArray[$nrParams - 1]);

    // if ($nrChapters != $nrParams - 2) {
    //     echo "Wrong number of parameters!";
    //     exit();
    // }

    $chaptersDescription = array_map(
        function ($value) {
            $array = explode(' ', $value);

            return $array;
        },
        $inputArray
    );

    foreach ($chaptersDescription as $index => $chapter) {
        if ($unreadPageIndex <= $chapter[1]) {
            $chaptersRemained = $nrChapters - $index + 1;
            echo $chaptersRemained;
            exit();
        }

        if ($index == $nrChapters){
            echo "Wrong unread page index!";
            exit();
        }
    }

<?php

    $stdin = fopen('php://stdin', 'r');
    // $stdout = fopen('php://stdout', 'w');

    // $input = $_GET['url'];
    // $inputArray = explode('/', $input);

    // $nrParams = count($inputArray);

    // $nrChapters = (int)$nrChapters;

    $input = stream_get_contents($stdin);

    $arr = preg_split("/\r\n|\n|\r/", $input);

    $nrChapters = (int)$arr[0];

    for($i = 1; $i <= $nrChapters; $i++) {
        $inputArray[$i] = array_map(function($val){ return (int)$val;}, explode(" ", trim($arr[$i])));
    }

    $unreadPageIndex = (int)trim(end($arr));
    echo end($arr) . "\n";
    echo $unreadPageIndex . "\n";
    exit();
    // for($i = 0; $i < $nrChapters; $i++) {

    // }

    // if ($nrParams < 3) {
    //     echo "There are less parameters than expected!";
    //     exit();
    // }

    // $nrChapters = $inputArray[0];
    // $nrLinesDescribingChapters = $nrParams - 2;
    // $unreadPageIndex = $inputArray[$nrParams - 1];

    // unset($inputArray[0], $inputArray[$nrParams - 1]);

    // if ($nrChapters != $nrParams - 2) {
    //     echo "Wrong number of parameters!";
    //     exit();
    // }

    $chaptersDescription = array_map(
        function ($value) {
            $array = explode(' ', $value);

            return $array;
        },
        $inputArray
    );

    foreach ($chaptersDescription as $index => $chapter) {
        if ($unreadPageIndex <= (int)$chapter[1]) {
            $chaptersRemained = $nrChapters - $index + 1;
            echo $chaptersRemained;
            exit();
        }

        if ($index == $nrChapters){
            echo "Wrong unread page index!";
            exit();
        }
    }

<?php

    $stdin = fopen('php://stdin', 'r');
    $stdout = fopen('php://stdout', 'w');

    // $input = $_GET['url'];
    // $inputArray = explode('/', $input);

    // $nrParams = count($inputArray);

    fread($stdin, $nrChapters);
    fread($stdin, $sec);
    // $nrChapters = (int)$nrChapters;

    fwrite($stdout, "Epta : '" . $nrChapters . "' | '$sec'");
    exit();

    // for($i = 0; $i < $nrChapters; $i++) {

    // }

    // if ($nrParams < 3) {
    //     echo "There are less parameters than expected!";
    //     exit();
    // }

    // $nrChapters = $inputArray[0];
    // $nrLinesDescribingChapters = $nrParams - 2;
    // $unreadPageIndex = $inputArray[$nrParams - 1];

    // unset($inputArray[0], $inputArray[$nrParams - 1]);

    if ($nrChapters != $nrParams - 2) {
        echo "Wrong number of parameters!";
        exit();
    }

    $chaptersDescription = array_map(
        function ($value) {
            $array = explode(' ', $value);

            return $array;
        },
        $inputArray
    );

    foreach ($chaptersDescription as $index => $chapter) {
        if ($unreadPageIndex <= $chapter[1]) {
            $chaptersRemained = $nrChapters - $index + 1;
            echo $chaptersRemained;
            exit();
        }

        if ($index == $nrChapters){
            echo "Wrong unread page index!";
            exit();
        }
    }

<?php

    $stdin = fopen('php://stdin', 'r');
    // $stdout = fopen('php://stdout', 'w');

    // $input = $_GET['url'];
    // $inputArray = explode('/', $input);

    // $nrParams = count($inputArray);

    // $nrChapters = (int)$nrChapters;

    $input = stream_get_contents($stdin);

    $arr = preg_split("/\r\n|\n|\r/", $input);

    $nrChapters = (int)$arr[0];

    for($i = 1; $i <= $nrChapters; $i++) {
        $inputArray[] = array_map(function($val){ return (int)$val;}, explode(" ", trim($arr[$i])));
    }

    $unreadPageIndex = (int)trim($arr[count($arr) - 1]);
    // for($i = 0; $i < $nrChapters; $i++) {

    // }

    // if ($nrParams < 3) {
    //     echo "There are less parameters than expected!";
    //     exit();
    // }

    // $nrChapters = $inputArray[0];
    // $nrLinesDescribingChapters = $nrParams - 2;
    // $unreadPageIndex = $inputArray[$nrParams - 1];

    // unset($inputArray[0], $inputArray[$nrParams - 1]);

    if ($nrChapters != $nrParams - 2) {
        echo "Wrong number of parameters!";
        exit();
    }

    $chaptersDescription = array_map(
        function ($value) {
            $array = explode(' ', $value);

            return $array;
        },
        $inputArray
    );

    foreach ($chaptersDescription as $index => $chapter) {
        if ($unreadPageIndex <= $chapter[1]) {
            $chaptersRemained = $nrChapters - $index + 1;
            echo $chaptersRemained;
            exit();
        }

        if ($index == $nrChapters){
            echo "Wrong unread page index!";
            exit();
        }
    }

<?php

    $stdin = fopen('php://stdin', 'r');
    $stdout = fopen('php://stdout', 'w');

    // $input = $_GET['url'];
    // $inputArray = explode('/', $input);

    // $nrParams = count($inputArray);

    fread($stdin, $nrChapters);
    $nrChapters = (int)$nrChapters;

    fwrite($stdout, "Epta : '" . $nrChapters . "'");
    exit();

    // for($i = 0; $i < $nrChapters; $i++) {

    // }

    // if ($nrParams < 3) {
    //     echo "There are less parameters than expected!";
    //     exit();
    // }

    // $nrChapters = $inputArray[0];
    // $nrLinesDescribingChapters = $nrParams - 2;
    // $unreadPageIndex = $inputArray[$nrParams - 1];

    // unset($inputArray[0], $inputArray[$nrParams - 1]);

    if ($nrChapters != $nrParams - 2) {
        echo "Wrong number of parameters!";
        exit();
    }

    $chaptersDescription = array_map(
        function ($value) {
            $array = explode(' ', $value);

            return $array;
        },
        $inputArray
    );

    foreach ($chaptersDescription as $index => $chapter) {
        if ($unreadPageIndex <= $chapter[1]) {
            $chaptersRemained = $nrChapters - $index + 1;
            echo $chaptersRemained;
            exit();
        }

        if ($index == $nrChapters){
            echo "Wrong unread page index!";
            exit();
        }
    }

<?php

    $stdin = fopen('php://stdin', 'r');
    // $stdout = fopen('php://stdout', 'w');

    // $input = $_GET['url'];
    // $inputArray = explode('/', $input);

    // $nrParams = count($inputArray);

    // $nrChapters = (int)$nrChapters;

    $input = stream_get_contents($stdin);

    $arr = preg_split("/\r\n|\n|\r/", $input);

    $nrChapters = (int)$arr[0];

    for($i = 1; $i <= $nrChapters; $i++) {
        $inputArray[$i] = array_map(function($val){ return (int)$val;}, explode(" ", trim($arr[$i])));
    }

    $unreadPageIndex = (int)trim($arr[count($arr) - 2]);
    echo $arr[count($arr) - 2] . "\n";
    echo $unreadPageIndex . "\n";
    exit();
    // for($i = 0; $i < $nrChapters; $i++) {

    // }

    // if ($nrParams < 3) {
    //     echo "There are less parameters than expected!";
    //     exit();
    // }

    // $nrChapters = $inputArray[0];
    // $nrLinesDescribingChapters = $nrParams - 2;
    // $unreadPageIndex = $inputArray[$nrParams - 1];

    // unset($inputArray[0], $inputArray[$nrParams - 1]);

    // if ($nrChapters != $nrParams - 2) {
    //     echo "Wrong number of parameters!";
    //     exit();
    // }

    $chaptersDescription = array_map(
        function ($value) {
            $array = explode(' ', $value);

            return $array;
        },
        $inputArray
    );

    foreach ($chaptersDescription as $index => $chapter) {
        if ($unreadPageIndex <= (int)$chapter[1]) {
            $chaptersRemained = $nrChapters - $index + 1;
            echo $chaptersRemained;
            exit();
        }

        if ($index == $nrChapters){
            echo "Wrong unread page index!";
            exit();
        }
    }


<?php

$l = trim(fgets(STDIN));
$r = trim(fgets(STDIN));

// base case sanity check
if($r > strlen($l))
{
    echo "impossible";
    exit;
}

$cc = array();
foreach(str_split($l) as $c) {
    if (!isset($cc[$c]))
        $cc[$c] = true;
}

$rr = max(0, $r - sizeof($cc));
echo ($rr);

<?php
$p = fopen('php://stdin', 'r');
$s = trim(fgets($p));
$k = intval(fgets($p));
fclose($p);
if (strlen($s) < $k) {
    echo 'impossible', PHP_EOL; die;
}
$chars = str_split($s);
$distinctLength = count(array_unique($chars));
$missing = $k - $distinctLength;
if ($missing < 0) {
    echo 0, PHP_EOL;
} else {
    echo $missing, PHP_EOL;
}

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));
if(strlen($a) < $b)
{
     print "impossible";
}
else
{
     $c = array_unique(str_split($a));
     sort($c);
     if(count($c) >= $b)
     {
          print "0";
     }
     else
     {
          print $b - count($c);
     }
}
?>

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));
if(strlen($a) < $b)
{
     print "impossible";
}
else
{
     $c = array_unique(str_split($a));
     sort($c);
     $d = $b - count($c);
     print $d;
}
?>

<?php

$r = trim(fgets(STDIN));
$l = explode("", trim(fgets(STDIN)));

// base case sanity check
if($r > $l)
{
    echo "impossible";
    exit;
}

$cc = array();
foreach($l as $c) {
    if (!isset($cc[$c]))
        $cc[$c] = true;
}

echo ($r - sizeof($cc));

<?php

$l = trim(fgets(STDIN));
$r = trim(fgets(STDIN));

// base case sanity check
if($r > strlen($l))
{
    echo "impossible";
    exit;
}

$cc = array();
foreach(str_split($l) as $c) {
    if (!isset($cc[$c]))
        $cc[$c] = true;
}

echo ($r - sizeof($cc));


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

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

function solve()
{
    $n = IO::getInt();
    $s = IO::getInt();
    $ans = $n;
    for($i=1; $i<$n; $i++) {
         if($s[$i] !=  $s[$i-1]) {
               $ans += $i;
         }
    }

     IO::pr($ans);
}

class IO {
    public static function getInt()
    {
        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::getInt() : 1;
while($t--) {
    solve();
}

function solve()
{
    $n = IO::getInt();
    $s = IO::getInt();
    $ans = 0;
    $t0 = 0;
    $t1 = 0;
    for($i=0; $i<$n; $i++) {
         if($s[$i] == '0') {
              $t0++;
         } else {
             $t1++;
         }
    }
     $max = IO::getMaxValue($t0, $t1);
     $min = $n - $max;
     $ans = $n;
     if($min > 0) {
         $ans += pow($min, $max);

     }
     IO::pr($ans);
}

class IO {
    public static function getInt()
    {
        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::getInt() : 1;
while($t--) {
    solve();
}

function solve()
{
    $n = IO::getInt();
    $s = IO::getInt();
    $ans = $n;
    while($n > 1) {
        $sN = '';
        $tt = $n;
         for($i=0; $i<$tt-1; $i++) {
              if($s[$i] == '0' && $s[$i+1] == '1') {
                    $sN .= '1';
                    $n--;
                    $i++;
              } elseif($s[$i] == '1' && $s[$i+1] == '0') {
                  $sN .= '0';
                  $n--;
                    $i++;
              } else {
                 $sN .= $s[$i];
              }
         }
         if($tt == $n) {
              break;
         } else {
              $ans += $tt - $n == 1 ? 1 : ($tt - $n) + 1 ;
              $s = $sN;
         }
    }

     IO::pr($ans);
}

class IO {
    public static function getInt()
    {
        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

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

$n = fgets($handle);

$list = array_map('intval', explode(' ', trim(fgets($handle))));

sort($list);

$value = 0;
for($i = 0; $i < $n - 1; $i++)
{
	if($list[$i] + 1 != $list[$i + 1])
	{
		$value+=($list[$i + 1] - $list[$i] - 1);
	}
}

echo $value;

<?php
$a = trim(fgets(STDIN));
$b = explode(" ", trim(fgets(STDIN)));
$c = min($b);
$d = max($b);
$e = $d - $c + 1 - $a;
print $e;
?>

<?php
fscanf(STDIN, "%d", $N);
$a = explode(" ", trim(fgets(STDIN)));

$num = max($a) - min($a) + 1;

echo $num - $N;

<?php
$a = trim(fgets(STDIN));
$b = explode(" ", trim(fgets(STDIN)));
$c = min($b);
$d = max($b);
$e = range($c, $d - $c + $c);
$f = array_diff($e, $b);
print count($f);
?>

<?php

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

$n = fgets($handle);

$list = array_map('intval', explode(' ', trim(fgets($handle))));

sort($list);

$value = 0;
for($i = 0; $i < $n - 1; $i++)
{
	if($list[$i] + 1 != $list[$i + 1])
	{
		$value++;
	}
}

echo $value;


<?

error_reporting(false);
//error_reporting(E_ALL);

$handle = fopen('php://stdin', 'r');
//$handle = fopen('in.txt', 'r');

$str = trim(fgets($handle));

//set_time_limit(3);

$c = 0;
$hh = 0;
$mm = 0;

for($i=0; $i<strlen($str); $i++) {

    if (h($str, $i)) {
        $hh++;
    }

    if (m($str, $i)) {
        $mm++;
        $c += $hh;
    }
}

function h($str, $ind){
    $t = "heavy";
    for($i=0; $i<5; $i++) {

        if($t[$i] != $str[$i + $ind]) return false;
    }
    return true;
}

function m($str, $ind){
    $t = "metal";
    for($i=$ind; $i<$ind+5; $i++) if($t[$i-$ind]!=$str[$i]) return false;
    return true;
}

echo $c;

?>

<?php

function FindSubString($string){

    $heavy_positions = FindPosition($string, "heavy");
    $metal_positions = FindPosition($string, "metal");
    $substring       = 0;
    $j               = 0;
    $metal_length    = count($metal_positions);
    //print_r($heavy_positions);echo "\n";
    //print_r($metal_positions);
    for($i=0;$i<count($heavy_positions);$i++){
        if($metal_length>0){
            while ($metal_length>$j && $heavy_positions[$i]>$metal_positions[$j]){
                $j++;
            }
            if($metal_length>$j){
                if($heavy_positions[$i]<$metal_positions[$j]){
                    $substring += $metal_length-$j;
                }
            }
        }
    }
    return $substring;
}

function FindPosition($string,$needle){
    $lastPos = 0;
    $positions = array();

    while (($lastPos = strpos($string, $needle, $lastPos))!== false) {
        $positions[] = $lastPos;
        $lastPos = $lastPos + strlen($needle);
    }
    return $positions;
}

$string      = trim(fgets(STDIN));
echo FindSubString($string);


<?php
class IO {
    public static function arr($sDelimeter = ' ') { return explode($sDelimeter, trim(fgets(STDIN))); }
    public static function str() { return trim(fgets(STDIN)); }
    public static function yes() { echo 'YES' . PHP_EOL; }
    public static function no() { echo 'NO' . PHP_EOL; }
    public static function line($sLine) { echo $sLine . PHP_EOL; }
    public static function lcm($x, $y) {
        $p = $x;
        $q = $y;
        while ($x != $y)
        {
            if ($x > $y)
                $x = $x - $y;
            else
                $y = $y - $x;
        }
        return ($p*$q)/$x;
    }
    public static function gcd($a, $b) {
        return ($a % $b) ? self::gcd($b,$a % $b) : $b;
    }
}

//$iQueryCount = 1;
$iQueryCount = IO::str();

while($iQueryCount > 0) {
    $iQueryCount--;
    solve();
}
function solve() {
    $n = IO::str();
    $l = strlen($n);
    $last = 1;
    $res = 0;
    for ($i = 1; $i < 10; $i++) {
        $r = '';
        for ($j = 0; $j < $l; $j++) {
            $r .= $i;
            if ((int)$r <= $n) {
                $res++;
                $last = (int)$r + 1;
            }
        }
    }

    IO::line($res);
}

?>

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

$t = trim(fgets($file));

for ($i = 0; $i < $t; $i++) {
  $n = trim(fgets($file));

  if ($n < 10) { echo $n.PHP_EOL; }
  else {
    if ($n == 10) { echo '9'.PHP_EOL; }
    else {
      $len = strlen($n);
      if ($len >= 2) { $ans = 9 * ($len - 1); }

      $s = (string) $n;
      $f = $s[0];
      $m = '';
      for ($j = 0; $j < $len; $j++) { $m .= $f; }

      if ($m > $n) { $ans += $f - 1; }
      else { $ans += $f; }

      echo $ans.PHP_EOL;
    }
  }
}

?>

<?php
class IO {
    public static function arr($sDelimeter = ' ') { return explode($sDelimeter, trim(fgets(STDIN))); }
    public static function str() { return trim(fgets(STDIN)); }
    public static function yes() { echo 'YES' . PHP_EOL; }
    public static function no() { echo 'NO' . PHP_EOL; }
    public static function line($sLine) { echo $sLine . PHP_EOL; }
    public static function lcm($x, $y) {
        $p = $x;
        $q = $y;
        while ($x != $y)
        {
            if ($x > $y)
                $x = $x - $y;
            else
                $y = $y - $x;
        }
        return ($p*$q)/$x;
    }
    public static function gcd($a, $b) {
        return ($a % $b) ? self::gcd($b,$a % $b) : $b;
    }

    public static function res($res) {
        foreach ($res as $r) {
            echo $r . "\n";
        }
    }

    public static function dd($val){
        echo $val; exit;
    }
}

$nums = array(
    2 => 9,
    3 => 18,
    4 => 27,
    5 => 36,
    6 => 45,
    7 => 54,
    8 => 63,
    9 => 72,
    10 => 81,
    11 => 90,
);

$t = IO::str();

function makeDel($q)
{
    $s = '';
    while($q--) {
        $s .= '1';
    }
    return $s;
}
while($t--){
    $n = IO::str();

    if ($n < 10) {
        $res[] = $n;
        continue;
    }

    $s = $nums[strlen($n)];
    $del = makeDel(strlen($n));

    while($n >= $del) {
        $s++;
        $n = $n - $del;
    }

    $res[] = $s;
}

IO::res($res);

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

$t = trim(fgets($file));

for ($i = 0; $i < $t; $i++) {
  $n = trim(fgets($file));

  if ($n < 10) { echo $n.PHP_EOL; }
  else {
    if ($n == 10) { echo '9'.PHP_EOL; }
    else {
      $len = strlen($n);
      if ($len >= 2) { $ans = 9 * ($len - 1); }

      $s = (string) $n;
      $f = $s[0];
      $m = '';
      for ($j = 0; $j < $len; $j++) { $m .= $f; }

      if ($m > $v[$i]) { $ans += $f - 1; }
      else { $ans += $f; }
      echo $ans.PHP_EOL;
    }
  }
}

?>

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

$t = trim(fgets($file));

for ($i = 0; $i < $t; $i++) {
  $n = trim(fgets($file));

  if ($n < 10) { echo $n.PHP_EOL; }
  else {
    if ($n == 10) { echo '9'.PHP_EOL; }
    else {
      $len = strlen($n);
      if ($len > 2) { $ans = 9 * ($len - 1); }

      $s = (string) $n;
      $f = $s[0];
      $m = '';
      for ($j = 0; $j < $len; $j++) { $m .= $f; }

      if ($m > $v[$i]) { $ans += $f - 1; }
      else { $ans += $f; }
      echo $ans.PHP_EOL;
    }
  }
}

?>

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

$t = trim(fgets($file));

for ($i = 0; $i < $t; $i++) {
  $n = trim(fgets($file));

  if ($n < 10) { echo $n.PHP_EOL; }
  else {
    $ans = 9;
    $len = strlen($n);
    if ($len > 2) { $ans += 9 * ($len - 2); }

    $s = (string) $n;
    $f = $s[0];
    $m = '';
    for ($j = 0; $j < $len; $j++) { $m .= $f; }

    if ($m > $v[$i]) { $ans += $f - 1; }
    else { $ans += $f; }
    echo $ans.PHP_EOL;
  }
}

?>

<?php
class IO {
    public static function arr($sDelimeter = ' ') { return explode($sDelimeter, trim(fgets(STDIN))); }
    public static function str() { return trim(fgets(STDIN)); }
    public static function yes() { echo 'YES' . PHP_EOL; }
    public static function no() { echo 'NO' . PHP_EOL; }
    public static function line($sLine) { echo $sLine . PHP_EOL; }
    public static function lcm($x, $y) {
        $p = $x;
        $q = $y;
        while ($x != $y)
        {
            if ($x > $y)
                $x = $x - $y;
            else
                $y = $y - $x;
        }
        return ($p*$q)/$x;
    }
    public static function gcd($a, $b) {
        return ($a % $b) ? self::gcd($b,$a % $b) : $b;
    }

    public static function res($res) {
        foreach ($res as $r) {
            echo $r . "\n";
        }
    }

    public static function dd($val){
        echo $val; exit;
    }
}

$nums = array(
    2 => 9,
    3 => 18,
    4 => 27,
    5 => 36,
    6 => 45,
    7 => 54,
    8 => 63,
    9 => 72,
    10 => 81,
    11 => 90,
);

$t = IO::str();

function makeDel($q)
{
    $s = '';
    while($q--) {
        $s .= '1';
    }
    return $s;
}
while($t--){
    $n = IO::str();

    if ($n < 10) {
        $res[] = $n;
        continue;
    }

    $s = $nums[strlen($n)];
    $del = makeDel(strlen($n));

    while($n > $del) {
        $s++;
        $n = $n - $del;
    }

    $res[] = $s;
}

IO::res($res);


<?php

$line = array_map('intval',explode(' ', trim(fgets(STDIN))));
$n = $line[0];
$k = $line[1];

for ($i = 0; $i < $n; $i++) {
	for ($j = 0; $j < $n; $j++) {
		if ($i == $j) {
			echo $k." ";
		}
		else {
			echo "0 ";
		}
	}
	echo "\n";
}


<?php
define('LOCAL_ENV', false);
if (LOCAL_ENV) {
    error_reporting(E_ALL & !E_NOTICE);
    $test = <<<INPUT
5 1 4
12 6 11 9 1
INPUT;
    $stdin = fopen('php://temp/in', 'rw');
    fwrite($stdin, $test);
    rewind($stdin);
} else {
    error_reporting(0);
    $stdin = STDIN;
}
define('MY_STDIN', $stdin);

list($n,$a,$b) = explode(' ', trim(fgets(MY_STDIN)));
$ww = explode(' ', trim(fgets(MY_STDIN)));
$res = array_fill(0, $n, 0);
for ($i = 0; $i < $n; $i++) {
    $wa = bcmul($ww[$i], $a);
    $x = bcdiv($wa, $b, 0);
    $xb = bcmul($x, $b);
    $r = bcsub($wa, $xb);
    $ws = bcdiv($r, $a, 0);
    $res[$i] = $ws;
}

echo join(' ', $res);

<?PHP

$fileIN = fopen("php://stdin", "r");
$fileOUT = fopen("php://stdout", "w");

$line = explode(" ", trim(fgets($fileIN)));

$n = $line[0];
$a = $line[1];
$b = $line[2];

$line = explode(" ", trim(fgets($fileIN)));

for ($i = 0; $i < $n; ++$i) {
    fprintf($fileOUT, (int) (fmod($line[$i] * $a, $b) / $a) . " ");
}

fclose($fileIN);
fclose($fileOUT);

?>

<?php
define('LOCAL_ENV', FALSE);
if (LOCAL_ENV) {
    error_reporting(E_ALL & !E_NOTICE);
    $test = <<<INPUT
1 1 1
1
INPUT;
    $stdin = fopen('php://temp/in', 'rw');
    fwrite($stdin, $test);
    rewind($stdin);
} else {
    error_reporting(0);
    $stdin = STDIN;
}
define('MY_STDIN', $stdin);

list($n,$a,$b) = explode(' ', trim(fgets(MY_STDIN)));
$x = explode(' ', trim(fgets(MY_STDIN)));
$res = array_fill(0, $n, 0);
for ($i = 0; $i < $n; $i++) {
    $aa = bcmod(bcmul($a, $x[$i]), $b);
    $res[$i] = $aa;
}

echo join(' ', $res);

<?php
define('LOCAL_ENV', false);
if (LOCAL_ENV) {
    error_reporting(E_ALL & !E_NOTICE);
    $test = <<<INPUT
3 1 2
1 2 3
INPUT;
    $stdin = fopen('php://temp/in', 'rw');
    fwrite($stdin, $test);
    rewind($stdin);
} else {
    error_reporting(0);
    $stdin = STDIN;
}
define('MY_STDIN', $stdin);

list($n,$a,$b) = explode(' ', trim(fgets(MY_STDIN)));
$x = explode(' ', trim(fgets(MY_STDIN)));
$res = array_fill(0, $n, 0);
for ($i = 0; $i < $n; $i++) {
    $aa = bcmod($x[$i], $b);
    $res[$i] = $aa;
}

echo join(' ', $res);

<?PHP

$fileIN = fopen("php://stdin", "r");
$fileOUT = fopen("php://stdout", "w");

$line = explode(" ", trim(fgets($fileIN)));

$n = $line[0];
$a = $line[1];
$b = $line[2];

$line = explode(" ", trim(fgets($fileIN)));

for ($i = 0; $i < $n; ++$i) {
    fprintf($fileOUT, ((int) ((int) $line[$i] * (int) $a) % (int) $b / (int) $a) . " ");
}

fclose($fileIN);
fclose($fileOUT);

?>

<?PHP

$fileIN = fopen("php://stdin", "r");
$fileOUT = fopen("php://stdout", "w");

$line = explode(" ", trim(fgets($fileIN)));

$n = $line[0];
$a = $line[1];
$b = $line[2];

$line = explode(" ", trim(fgets($fileIN)));

for ($i = 0; $i < $n; ++$i) {
    fprintf($fileOUT, floor((float) (($line[$i] * $a) % $b) / (int) $a) . " ");
}

fclose($fileIN);
fclose($fileOUT);

?>

<?PHP

$fileIN = fopen("php://stdin", "r");
$fileOUT = fopen("php://stdout", "w");

$line = explode(" ", trim(fgets($fileIN)));

$n = $line[0];
$a = $line[1];
$b = $line[2];

$line = explode(" ", trim(fgets($fileIN)));

for ($i = 0; $i < $n; ++$i) {
    fprintf($fileOUT, (int) ((int) ($line[$i] * $a) % (int) $b / (int) $a) . " ");
}

fclose($fileIN);
fclose($fileOUT);

?>

<?PHP

$fileIN = fopen("php://stdin", "r");
$fileOUT = fopen("php://stdout", "w");

$line = explode(" ", trim(fgets($fileIN)));

$n = $line[0];
$a = $line[1];
$b = $line[2];

$line = explode(" ", trim(fgets($fileIN)));

for ($i = 0; $i < $n; ++$i) {
    fprintf($fileOUT, (($line[$i] * $a) % $b / $a) . " ");
}

fclose($fileIN);
fclose($fileOUT);

?>