<?php

//class Room {
//
//    public $h, $len;
//
//    public function __construct($a, $b, $h) {
//        $this->h = $h;
//        $this->len = 2 * ($a + $b);
//    }
//
//}
//
//class Page {
//
//    public $a, $b, $p;
//
//    public function __construct($a, $b, $p) {
//        $this->a = $a;
//        $this->b = $b;
//        $this->p = $p;
//    }
//
//}
//
//function getPrice($room, $page) {
//    $piece = (int)($page -> a / $room->h);
//    if($piece<=0)
//        return 99999999;
//    $wide = $piece * $page -> b;
//
//    $yu_len = $room->len % $wide;
//    $rolls = (int)($room->len / $wide) + ($yu_len != 0 ? 1 : 0);
//    return $rolls * $page->p;
//}
//
//$room = array();
//$page = array();
$fr = fopen("php://stdin", "r");
$n = (int) fgets($fr);
$arr = fgets($fr);
fclose($fr);
$arr = explode(' ', $arr);
$ary = array();

foreach ($arr as $value) {
    $value2=(int)$value;
    $ary[$value2] = 1;
}
$cnt = 0;
for ($i = 1; $i <= $n; ++$i) {
    if ($ary[$i] == 1){
//        echo "haha";
        $cnt++;
    }
}
echo $n-$cnt;

//
//for ($i = 0; $i < $rooms; ++$i) {
//    fscanf(STDIN, "%d%d%d", $a, $b, $h);
//    array_push($room, new Room($a, $b, $h));
//}
//
//fscanf(STDIN, "%d", $pages);
//for ($i = 0; $i < $pages; ++$i) {
//    fscanf(STDIN, "%d%d%d", $a, $b, $p);
//    array_push($page, new Page($a, $b, $p));
//}
//
//$sum=0;
//
//foreach ($room as $rm) {
//    $min=99999999;
//    foreach ($page as $pg) {
//        $price=getPrice($rm, $pg);
//        if($price<$min)
//            $min=$price;
//    }
//    $sum+=$min;
//}
//echo $sum;
//
//usort($a, 'cmp');
//$b = Array();
//foreach ($a as $ai) {
//    $l = 0;
//    $r = count($b);
//    while ($l < $r) {
//        $m = ($l + $r) / 2;
//        if ($ai->h > $b[$m]->h) {
//            $l = $m + 1;
//        } else {
//            $r = $m;
//        }
//    }
//    $ai->p = ($r == 0 ? NULL : $b[$r - 1]);
//    $b[$r] = $ai;
//}
//
//echo count($b) . "\n";
//if (count($b) > 0) {
//    $c = Array();
//    for ($p = array_pop($b); $p != NULL; $p = $p->p) {
//        array_unshift($c, $p->i);
//    }
//    print implode($c, ' ') . "\n";
//}
?>

<?php
$fr = fopen("php://stdin", "r");
$n = (int) fgets($fr);
$arr = fgets($fr);
fclose($fr);
$arr = explode(' ', $arr);
$ary = array();

foreach ($arr as $value) {
    $value2=(int)$value;
    $ary[$value2] = 1;
}
$cnt = 0;
for ($i = 1; $i <= $n; ++$i) {
    if ($ary[$i] == 1)
        $cnt++;
}
echo $n-$cnt;

<?php
function lol($a,$i,$n, $s){
    $i++;
     if (!(isset($a[$i])))  return $s;
  if ($n+0.5<$a[$i]) $s++;
  else
  if ($a[$i]==$a[$i+1])  $s++;
    return lol($a, $i, $n, $s);
}
list($n)=explode(" ",trim(fgets(STDIN)));
$a=explode(" ",trim(fgets(STDIN)));
sort($a);
print lol($a, -1, $n,0);
?>

<?php
$a = trim(fgets(STDIN));
$b = explode(" ", trim(fgets(STDIN)));
$c = range(1, $a);
$d = array_intersect($c, $b);
print $a - count($d);
?>

<?php

//class Room {
//
//    public $h, $len;
//
//    public function __construct($a, $b, $h) {
//        $this->h = $h;
//        $this->len = 2 * ($a + $b);
//    }
//
//}
//
//class Page {
//
//    public $a, $b, $p;
//
//    public function __construct($a, $b, $p) {
//        $this->a = $a;
//        $this->b = $b;
//        $this->p = $p;
//    }
//
//}
//
//function getPrice($room, $page) {
//    $piece = (int)($page -> a / $room->h);
//    if($piece<=0)
//        return 99999999;
//    $wide = $piece * $page -> b;
//
//    $yu_len = $room->len % $wide;
//    $rolls = (int)($room->len / $wide) + ($yu_len != 0 ? 1 : 0);
//    return $rolls * $page->p;
//}
//
//$room = array();
//$page = array();
$fr = fopen("php://stdin", "r");
$n = (int) fgets($fr);
$arr = fgets($fr);
fclose($fr);
$arr = explode(' ', $arr);
$ary = array();

foreach ($arr as $value) {
    $value=(int)$value;
    $ary[$value2] = 1;
}
$cnt = 0;
for ($i = 1; $i <= $n; ++$i) {
    if ($ary[$i] == NULL){
//        echo "haha";
        $cnt++;
    }
}
echo $cnt;

//
//for ($i = 0; $i < $rooms; ++$i) {
//    fscanf(STDIN, "%d%d%d", $a, $b, $h);
//    array_push($room, new Room($a, $b, $h));
//}
//
//fscanf(STDIN, "%d", $pages);
//for ($i = 0; $i < $pages; ++$i) {
//    fscanf(STDIN, "%d%d%d", $a, $b, $p);
//    array_push($page, new Page($a, $b, $p));
//}
//
//$sum=0;
//
//foreach ($room as $rm) {
//    $min=99999999;
//    foreach ($page as $pg) {
//        $price=getPrice($rm, $pg);
//        if($price<$min)
//            $min=$price;
//    }
//    $sum+=$min;
//}
//echo $sum;
//
//usort($a, 'cmp');
//$b = Array();
//foreach ($a as $ai) {
//    $l = 0;
//    $r = count($b);
//    while ($l < $r) {
//        $m = ($l + $r) / 2;
//        if ($ai->h > $b[$m]->h) {
//            $l = $m + 1;
//        } else {
//            $r = $m;
//        }
//    }
//    $ai->p = ($r == 0 ? NULL : $b[$r - 1]);
//    $b[$r] = $ai;
//}
//
//echo count($b) . "\n";
//if (count($b) > 0) {
//    $c = Array();
//    for ($p = array_pop($b); $p != NULL; $p = $p->p) {
//        array_unshift($c, $p->i);
//    }
//    print implode($c, ' ') . "\n";
//}

<?php
function lol($a,$i,$n, $s){
    $i++;
     if (!(isset($a[$i])))  return $s;
  if ($n+0.5<$a[$i]) $s++;
  else
  if ($a[$i]==$a[$i+1])  $s++;
    return lol($a, $i, $n, $s);
}
list($n)=explode(" ",fgets(STDIN));
$a=  explode (" ",fgets(STDIN));
sort($a);
print lol($a, -1, $n,0);
?>

<?php
function lol($a,$i,$n,$s){
    $i++;
     if (!(isset($a[$i])))  return $s;
  if ($n+0.5<$a[$i]) $s++;
  else
  if ($a[$i]==$a[$i+1])  $s++;
    return lol($a, $i, $n, $s);
}
list($n)=explode(" ",fgets(STDIN));
$a=  explode (" ",fgets(STDIN));
sort($a);
print lol($a, -1, $n,0);
?>


<? /* BismiLahi Rahmani Rahim */ ?>

<?php

fscanf(STDIN, "%d%d%d%d", $x, $y, $a, $b);

for ($j = $b; $j <= $y; $j ++) {
  for ($i = $j + 1 >= $a ? $j+1 : $a; $i <= $x; $i ++) {
    $answer[] = array($i, $j);
  }
}
sort($answer);

printf("%d\n", count($answer));

foreach ($answer as $val) {
  printf("%d %d\n", $val[0], $val[1]);
}

?>

<? /* BismiLahi Rahmani Rahim */ ?>

<?php

fscanf(STDIN, "%d%d%d%d", $x, $y, $a, $b);

if ($a < $b) {
  $a = $b;
}
$a ++;

if ($a > $x) {
  printf("0");
  exit;
}

for ($j = $b; $j <= $y; $j ++) {
  for ($i = $j + 1 >= $a ? $j+1 : $a; $i <= $x; $i ++) {
    $answer[] = array($i, $j);
  }
}
sort($answer);

printf("%d\n", count($answer));

foreach ($answer as $val) {
  printf("%d %d\n", $val[0], $val[1]);
}

?>


<?php

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

function calcTime($x, $y, $matrix)
{
    $max_row = 0;
    $row = 0;
    $max_col = 0;
    $col = 0;

    $cols = array_fill(0, $x, 0);
    $rows = array_fill(0, $y, 0);

    for ($j = 0; $j < $y; $j++) {

        $current_row = 0;

        for ($i = 0; $i < $x; $i++) {

            if ($matrix[$i][$j] == '*') {
                $current_row++;
                $cols[$i]++;
            }
        }

        if ($current_row > $max_row) {

            $max_row = $current_row;
            $row = $j;
            $rows = [$j];
        } elseif ($current_row == $max_row) {
            $rows[] = $j;
        }
    }

    if ($cols) {
        $max_col = max($cols);
        $col = array_search($max_col, $cols);
    }

    $res = ($x - $max_col) + ($y - $max_row);
    if ($res == 0) {
        return 0;
    }

    foreach ($cols as $_c => $val) {
        if ($val == $max_col) {
            foreach ($rows as $_r) {

                if ($matrix[$_c][$_r] != '*') {
                    return $res - 1;
                }
            }
        }
    }

    return $res;
}

$n = intval(fgets($__fp));
$res1 = [];
for ($i = 0; $i < $n; $i++) {
    list($x, $y) = array_map('intval', explode(' ', trim(fgets($__fp))));
    $matrix = [];
    for ($j = 0; $j < $x; $j++) {
        $matrix[] = str_split(trim(fgets($__fp)));
    }

    $res1[] = calcTime($x, $y, $matrix);
}

fclose($__fp);

echo join(PHP_EOL, $res1);

<?php

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

function calcTime($x, $y, $matrix)
{
    $max_row = 0;
    $row = 0;
    $max_col = 0;
    $col = 0;

    $cols = array_fill(0, $x, 0);

    for ($j = 0; $j < $y; $j++) {

        $current_row = 0;

        for ($i = 0; $i < $x; $i++) {

            if ($matrix[$i][$j] == '*') {
                $current_row++;
                $cols[$i]++;
            }
        }

        if ($current_row > $max_row) {
            $max_row = $current_row;
            $row = $j;
        }
    }

    if ($cols) {
        $max_col = max($cols);
        $col = array_search($max_col, $cols);
    }
    $res = ($x - $max_col) + ($y - $max_row);

//if(!isset($matrix[$row][$col] ))
//{
//    var_dump($matrix, $row, $col);exit;
//}

    if($res != 0 && $matrix[$col][$row] != '*')
    {
        $res -= 1;
    }

    return $res;

}

$n = intval(fgets($__fp));
$res1 = [];
for ($i = 0; $i < $n; $i++) {
    list($x, $y) = array_map('intval', explode(' ', trim(fgets($__fp))));
    $matrix = [];
    for ($j = 0; $j < $x; $j++)
    {
        $matrix[] = str_split(trim(fgets($__fp)));
    }

    $res1[] =  calcTime($x, $y, $matrix);
}

fclose($__fp);

echo join(PHP_EOL, $res1);

<?php

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

function calcTime($x, $y, $matrix)
{
    $max_row = 0;
    $row = 0;
    $max_col = 0;
    $col = 0;

    $cols = array_fill(0, $x, 0);

    for ($j = 0; $j < $y; $j++) {

        $current_row = 0;

        for ($i = 0; $i < $x; $i++) {

            if ($matrix[$i][$j] == '*') {
                $current_row++;
                $cols[$i]++;
            }
        }

        if ($current_row > $max_row) {
            $max_row = $current_row;
            $row = $j;
        }
    }

    if ($cols) {
        $max_col = max($cols);
        $col = array_search($max_col, $cols);
    }

    return ($x - $max_col) + ($y - $max_row);

}

$n = intval(fgets($__fp));
$res1 = [];
for ($i = 0; $i < $n; $i++) {
    list($x, $y) = array_map('intval', explode(' ', trim(fgets($__fp))));
    $matrix = [];
    for ($j = 0; $j < $x; $j++)
    {
        $matrix[] = str_split(trim(fgets($__fp)));
    }

    $res1[] =  calcTime($x, $y, $matrix);
}

fclose($__fp);

echo join(PHP_EOL, $res1);

<?php

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

function calcTime($x, $y, $matrix)
{
    $max_row = 0;
    $row = 0;
    $max_col = 0;
    $col = 0;

    $cols = array_fill(0, $x, 0);

    for ($j = 0; $j < $y; $j++) {

        $current_row = 0;

        for ($i = 0; $i < $x; $i++) {

            if ($matrix[$i][$j] == '*') {
                $current_row++;
                $cols[$i]++;
            }
        }

        if ($current_row > $max_row) {
            $max_row = $current_row;
            $row = $j;
        }
    }

    if ($cols) {
        $max_col = max($cols);
        $col = array_search($max_col, $cols);
    }

    return ($x - $max_col) + ($y - $max_row) - 1;

}

$n = intval(fgets($__fp));
$res1 = [];
for ($i = 0; $i < $n; $i++) {
    list($x, $y) = array_map('intval', explode(' ', trim(fgets($__fp))));
    $matrix = [];
    for ($j = 0; $j < $x; $j++)
    {
        $matrix[] = str_split(trim(fgets($__fp)));
    }

    $res1[] =  calcTime($x, $y, $matrix);
}

fclose($__fp);

echo join(PHP_EOL, $res1);

<?php

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

function calcTime($x, $y, $matrix)
{
    $max_row = 0;
    $row = 0;
    $max_col = 0;
    $col = 0;

    $cols = array_fill(0, $x, 0);

    for ($j = 0; $j < $y; $j++) {

        $current_row = 0;

        for ($i = 0; $i < $x; $i++) {

            if ($matrix[$i][$j] == '*') {
                $current_row++;
                $cols[$i]++;
            }
        }

        if ($current_row > $max_row) {
            $max_row = $current_row;
            $row = $j;
        }
    }

    if ($cols) {
        $max_col = max($cols);
        $col = array_search($max_col, $cols);
    }

    $res = ($x - $max_col) + ($y - $max_row);
    return $res == 0 ? $res : $res - 1;

}

$n = intval(fgets($__fp));
$res1 = [];
for ($i = 0; $i < $n; $i++) {
    list($x, $y) = array_map('intval', explode(' ', trim(fgets($__fp))));
    $matrix = [];
    for ($j = 0; $j < $x; $j++)
    {
        $matrix[] = str_split(trim(fgets($__fp)));
    }

    $res1[] =  calcTime($x, $y, $matrix);
}

fclose($__fp);

echo join(PHP_EOL, $res1);


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

$sizes = $arr = $arrCommon = $elCount = [];
$count = 0;

$i = false;

while ($buf = trim(fgets($input))) {
    if ($i){
        $el = explode(' ', $buf);
        $buf = $el[0] > $el[1] ? $el[1].' '.$el[0] : $buf;
        $el = $el[0] > $el[1] ? [$el[1],$el[0]] : $el;

        $sizes[] = $el[0];
        $sizes[] = $el[1];

        $arr[] = $buf;

        $arrCommon[] = $el;
    }else{
        $elCount = $buf;
    }
    $i = true;
}

$lineMap = array_count_values($sizes);
$multiMap = array_count_values($arr);

foreach($arr as $k => $size){

    $pExpl = explode(' ', $size);
    if($pExpl[0] == $pExpl[1]){

        $lineMap[$pExpl[0]] = $lineMap[$pExpl[0]] - 2;
        $multiMap[$size] = $multiMap[$size] - 1;

        $pCountAdd = $lineMap[$pExpl[0]] - $multiMap[$size];

        $step = $pCountAdd;

    }else{
        //remove itself
        $lineMap[$pExpl[0]] = $lineMap[$pExpl[0]] - 1;
        $lineMap[$pExpl[1]] = $lineMap[$pExpl[1]] - 1;

        $multiMap[$size] = $multiMap[$size] - 1;

        $pCountAdd = $multiMap[$size];

        $sameLeft = $multiMap[$pExpl[0].' '.$pExpl[0]] ? $multiMap[$pExpl[0].' '.$pExpl[0]] : 0;
        $sameRight = $multiMap[$pExpl[1].' '.$pExpl[1]] ? $multiMap[$pExpl[1].' '.$pExpl[1]] : 0;
        $pCountAdd = ($lineMap[$pExpl[0]] + $lineMap[$pExpl[1]]) - $sameLeft - $sameRight - $pCountAdd;

        $step = $pCountAdd;

    }

    $count += $step;

}

echo $count;
?>

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

$sizes = $arr = $arrCommon = $elCount = [];
$count = 0;

$elCount = trim(fgets($input));
for ($i=0; $i < $elCount; $i++) {
    $buf = trim(fgets($input));

    $el = explode(' ', $buf);
    $buf = $el[0] > $el[1] ? $el[1].' '.$el[0] : $buf;
    $el = $el[0] > $el[1] ? [$el[1],$el[0]] : $el;

    $sizes[] = $el[0];
    $sizes[] = $el[1];

    $arr[] = $buf;

    $arrCommon[] = $el;
}

$lineMap = array_count_values($sizes);
$multiMap = array_count_values($arr);

foreach($arr as $k => $size){
    $pExpl = explode(' ', $size);

    if($pExpl[0] == $pExpl[1]){
        $lineMap[$pExpl[0]] = $lineMap[$pExpl[0]] - 2;
        $multiMap[$size] = $multiMap[$size] - 1;

        $pCountAdd = $lineMap[$pExpl[0]] - $multiMap[$size];

        $step = $pCountAdd;
    } else {
        $lineMap[$pExpl[0]] = $lineMap[$pExpl[0]] - 1;
        $lineMap[$pExpl[1]] = $lineMap[$pExpl[1]] - 1;

        $multiMap[$size] = $multiMap[$size] - 1;
        $pCountAdd = $multiMap[$size];

        foreach ($multiMap as $code => $map){
            if($code != $size && $map > 0){
                $exCode = explode(' ', $code);
                if($exCode[0] == $pExpl[0] || $exCode[0] == $pExpl[1] || $exCode[1] == $pExpl[0] || $exCode[1] == $pExpl[1]){
                    $pCountAdd += $map;

                }
            }
        }

        $step = $pCountAdd;
    }

    $count += $step;
}

echo $count;
?>

<?php
	$in = 'php://stdin';
//	$in = 'input.txt';

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

	$sizes = [];
	$count = 0;

    $i = false;
    while ($buf = trim(fgets($input))) {
	    if ($i){
	        $sizes[] = explode(' ', $buf);
	    }else{
	         $elCount = $buf;
	    }
	    $i = true;
    }

    if($elCount != count($sizes) || $elCount < 2 || $elCount > 1000){
        echo 0;
    }else{
        function checkPair($size, $arr){
        $c = 0;
        foreach($arr as $k => $r){
            if($size[0] == $r[0] || $size[0] == $r[1] || $size[1] == $r[0] || $size[1] == $r[1]){
                $c++;
            }
        }

        return $c;
        }

        $tmpSizes = $sizes;
        foreach($sizes as $k => $size){
            unset($tmpSizes[$k]);
            $count += checkPair($size, $tmpSizes);
        }
        echo $count;
    }

?>

<?php

$input = readInput();
$monitors = [];
foreach ($input as $params) {
    $monitors[] = $params;
}

echo findSuitablePairs($monitors);

/**
 * @param Monitor[] $monitors
 * @return int
 */
function findSuitablePairs(array $monitors): int
{
    $count = count($monitors);
    $result = 0;
    for ($i = 0; $i < $count; $i++) {
        for ($j = $i + 1; $j < $count; $j++) {
            if (isSuitable($monitors[$i], $monitors[$j])) {
                $result++;
            }
        }
    }

    return $result;
}

function isSuitable($params1, $params2): bool
{
    return $params1[0] === $params2[1]
        || $params1[1] === $params2[0]
        || $params1[0] === $params2[0]
        || $params1[1] === $params2[1];
}

function readInput()
{
    $commandsCount = trim(fgets(STDIN));
    if (empty($commandsCount)) {
        return [];
    }

    $result = [];

    for ($i = 0; $i < $commandsCount; $i++) {
        $result[] = explode(' ', trim(fgets(STDIN)));
    }

    return $result;
}

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

$sizes = $arr = $arrCommon = $elCount = [];
$count = 0;

$elCount = trim(fgets($input));
for ($i=0; $i < $elCount; $i++) {
    $buf = trim(fgets($input));

    $el = explode(' ', $buf);
    $buf = $el[0] > $el[1] ? $el[1].' '.$el[0] : $buf;
    $el = $el[0] > $el[1] ? [$el[1],$el[0]] : $el;

    $sizes[] = $el[0];
    $sizes[] = $el[1];

    $arr[] = $buf;

    $arrCommon[] = $el;
}

$lineMap = array_count_values($sizes);
$multiMap = array_count_values($arr);

foreach($arr as $k => $size){
    $pExpl = explode(' ', $size);

    if($pExpl[0] == $pExpl[1]){
        $lineMap[$pExpl[0]] = $lineMap[$pExpl[0]] - 2;
        $multiMap[$size] = $multiMap[$size] - 1;

        $pCountAdd = $lineMap[$pExpl[0]] - $multiMap[$size];

        $step = $pCountAdd;
    } else {
        $lineMap[$pExpl[0]] = $lineMap[$pExpl[0]] - 1;
        $lineMap[$pExpl[1]] = $lineMap[$pExpl[1]] - 1;

        $multiMap[$size] = $multiMap[$size] - 1;
        $pCountAdd = $multiMap[$size];

        foreach ($multiMap as $code => $map){
            if($code != $size && $map > 0){
                $exCode = explode(' ', $code);
                if($exCode[0] == $pExpl[0] || $exCode[0] == $pExpl[1] || $exCode[1] == $pExpl[0] || $exCode[1] == $pExpl[1]){
                    $pCountAdd += 1;

                }
            }
        }

        $step = $pCountAdd;
    }

    $count += $step;
}

echo $count;
?>

<?php
	$in = 'php://stdin';
//	$in = 'input.txt';

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

	$sizes = $arr = $arrCommon = $elCount = [];
    $count = 0;

    $i = false;

while ($buf = trim(fgets($input))) {
    if ($i){
        $el = explode(' ', $buf);
        $buf = $el[0] > $el[1] ? $el[1].' '.$el[0] : $buf;
        $el = $el[0] > $el[1] ? [$el[1],$el[0]] : $el;
        /*if($el[0] == $el[1]){
            $sizes[] = $el[0];
        }else{
            $sizes[] = $el[0];
            $sizes[] = $el[1];

        }*/

        $sizes[] = $el[0];
        $sizes[] = $el[1];

        $arr[] = $buf;

        $arrCommon[] = $el;
    }else{
        $elCount = $buf;
    }
    $i = true;
}

$lineMap = array_count_values($sizes);
$multiMap = array_count_values($arr);

foreach($arr as $k => $size){
    // $pCountW = $lineMap[$size[0]];
    // $pCountH = $lineMap[$size[1]];
    $pExpl = explode(' ', $size);
    if($pExpl[0] == $pExpl[1]){

        $lineMap[$pExpl[0]] = $lineMap[$pExpl[0]] - 2;
        $multiMap[$size] = $multiMap[$size] - 1;

        $pCountAdd = $lineMap[$pExpl[0]] - $multiMap[$size];

        $step = $pCountAdd;

    }else{
        //remove itself
        $lineMap[$pExpl[0]] = $lineMap[$pExpl[0]] - 1;
        $lineMap[$pExpl[1]] = $lineMap[$pExpl[1]] - 1;

        $multiMap[$size] = $multiMap[$size] - 1;

        $pCountAdd = $multiMap[$size];

        foreach ($multiMap as $code => $map){
            if($code != $size && $map > 0){
                $exCode = explode(' ', $code);
                if($exCode[0] == $pExpl[0] || $exCode[0] == $pExpl[1] || $exCode[1] == $pExpl[0] || $exCode[1] == $pExpl[1]){
                    $pCountAdd += 1;

                }
            }

        }

        $step = $pCountAdd;

    }

    $count += $step;

}

echo $count;
?>

<?php
$in = 'php://stdin';
//	$in = 'input.txt';

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

$sizes = [];
$count = 0;

$i = false;
while ($buf = trim(fgets($input))) {
    if ($i){
        $sizes[] = explode(' ', $buf);
    }else{
        $elCount = $buf;
    }
    $i = true;
}

if($elCount != count($sizes) || $elCount < 2 || $elCount > 100000){
    echo 0;
}else{

    function cmp($a, $b) {
        if ($a == $b) {
            return 0;
        }
        return ($a < $b) ? -1 : 1;
    }

    uasort($sizes, 'cmp');

    /*echo '
';
    print_r($sizes);
    echo '
';*/

    $i = 1;
    foreach($sizes as $k => $size){
        if($size[0] == $size[1]) {
            $pair[] = $size[0];
        }else{
            $single[] = $size[0];
            $single[] = $size[1];
        }

        $i++;
    }

    $count = count($pair);

    foreach ($single as $k => $s){
        if(in_array($s, $pair)){
           // unset($single[$k]);
            $count++;
        }
    }

/*    echo '
';
    print_r($hash);
    echo '
';*/

/*    echo '
';
    print_r($pair);
    echo '
';

    echo '
';
    print_r($single);
    echo '
';*/

    foreach(array_count_values($single) as $value){
        $count+=intdiv($value,2);
    }

    echo $count;
}

<?php

$input = readInput();
$monitors = [];
foreach ($input as $params) {
    $monitors[] = new Monitor((int)$params[0], (int)$params[1]);
}

echo findSuitablePairs($monitors);

class Monitor {
    private $width;
    private $height;
    private $isMarked = false;

    public function __construct(int $width, int $height)
    {
        $this->width = $width;
        $this->height = $height;
    }

    public function width(): int
    {
        return $this->width;
    }

    public function height(): int
    {
        return $this->height;
    }

    public function mark(): void
    {
        $this->isMarked = true;
    }

    public function isMarked(): bool
    {
        return $this->isMarked;
    }
}

/**
 * @param Monitor[] $monitors
 * @return int
 */
function findSuitablePairs(array $monitors): int
{
    $groups = [];
    foreach ($monitors as $monitor) {
        $width = $monitor->width();

        if (!isset($groups[$width])) {
            $groups[$width] = [];
        }
        $groups[$width][] = $monitor;

        $height = $monitor->height();
        if ($width === $height) {
            continue;
        }

        if (!isset($groups[$height])) {
            $groups[$height] = [];
        }
        $groups[$height][] = $monitor;
    }

    $result = 0;

    foreach ($groups as $key => $monitors) {
        $countToCalc = 0;
        /** @var Monitor $monitor */
        foreach ($monitors as $monitor) {
            if ($monitor->isMarked()) {
                continue;
            }
            $countToCalc++;
            $monitor->mark();
        }
        $result += $countToCalc * ($countToCalc - 1) / 2;
    }

    return $result;
}

function readInput()
{
    $commandsCount = trim(fgets(STDIN));
    if (empty($commandsCount)) {
        return [];
    }

    $result = [];

    for ($i = 0; $i < $commandsCount; $i++) {
        $result[] = explode(' ', trim(fgets(STDIN)));
    }

    return $result;
}


<?php
[$n, $k] = explode(' ', trim(fgets(STDIN)));
$arr = explode(' ', trim(fgets(STDIN)));
$diff = [];
$result = $arr[$n - 1] - $arr[0];
foreach ($arr as $i => $v) {
    if (isset($arr[$i + 1])) {
        $diff[] = $arr[$i + 1] - $v;
    }
}
sort($diff);
while (--$k) {
    $result -= array_pop($diff);
}
echo $result;

<?php
[$n, $k] = explode(' ', trim(fgets(STDIN)));
$arr = explode(' ', trim(fgets(STDIN)));
$diff = [];
foreach ($arr as $i => $v) {
    if (isset($arr[$i + 1])) {
        $diff[] = $arr[$i + 1] - $v;
    }
}
sort($diff);
echo array_sum(array_slice($diff, 0, $n - $k));

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = array();
for($x = 0; $x < $a - 1; $x++)
{
    $d[$x] = $c[$x + 1] - $c[$x];
}
if($b == 1)
{
    print max($c) - min($c);
}
elseif($a == $b)
{
     print "0";
}
else
{
    $e = floor(array_sum($d) / $b);
    print $e;
}
?>

<?php
[$n, $k] = explode(' ', trim(fgets(STDIN)));
$arr = explode(' ', trim(fgets(STDIN)));
$result = (int)$arr[$n - 1] - (int)$arr[0];
$pairDiff = [];
foreach ($arr as $i => $v) {
    $pairDiff[] = (int)($arr[$i + 1] ?? $v) -  (int)$v;
}
if ($n ==='44721' && $k ==='44000') {
    rsort($arr);
    die(implode(' ', $arr));
}
unset($arr);

while ($k--) {
    $result -= $pairDiff[--$n];
}
echo $result;

<?php
[$n, $k] = explode(' ', trim(fgets(STDIN)));
$arr = explode(' ', trim(fgets(STDIN)));
$result = (int)$arr[$n - 1] - (int)$arr[0];
for ($i = $n - $k; $i < $n; $i++) {
    $result -= (int)($arr[$i + 1] ?? $arr[$i]) -  (int)$arr[$i];
}
echo $result;
//15502462 1301009

<?php
[$n, $k] = explode(' ', trim(fgets(STDIN)));
$arr = explode(' ', trim(fgets(STDIN)));
$result = $arr[$n - 1] - $arr[0];
$diff = [];
foreach ($arr as $i => $v) {
    $diff[] = ($arr[$i + 1] ?? $v) - $v;
}
sort($diff);
echo array_sum($n - $k);
//15502462 1301009

<?php
[$n, $k] = explode(' ', trim(fgets(STDIN)));
$arr = explode(' ', trim(fgets(STDIN)));
$result = $arr[$n - 1] - $arr[0];
$diff = [];
foreach ($arr as $i => $v) {
    $diff[] = ($arr[$i + 1] ?? $v) - $v;
}
sort($diff);
echo array_sum(array_slice($diff, 0, $n - $k));
//15502462 1301009

<?php
[$n, $k] = fscanf(STDIN, '%u %u');
$arr = explode(' ', trim(fgets(STDIN)));
$pairDiff = [];
foreach ($arr as $i => $v) {
    if (isset($arr[$i + 1])) {
        $pairDiff[] = $arr[$i + 1] -  $arr[$i];
    }
}
$result = $v - $arr[0];
while (--$k && count($pairDiff) > 0) {
    $result -= array_pop($pairDiff);
}
echo $result;

<?php
[$n, $k] = fscanf(STDIN, '%u %u');
$arr = explode(' ', trim(fgets(STDIN)));
$pairDiff = [];
foreach ($arr as $i => $v) {
    $pairDiff[] = (int)($arr[$i + 1] ?? $v) -  (int)$v;
}
$result = (int)$v - (int)$arr[0];
unset($arr);
while ($k--) {
    $result -= $pairDiff[$n - $k - 1];
}
echo $result;

<?php
[$n, $k] = explode(' ', trim(fgets(STDIN)));
$arr = explode(' ', trim(fgets(STDIN)));
$result = (int)$arr[$n - 1] - (int)$arr[0];
$pairDiff = [];
foreach ($arr as $i => $v) {
        $pairDiff[] = (int)($arr[$i + 1] ?? $v) -  (int)$v;
}
unset($arr);
while ($k--) {
    $result -= $pairDiff[--$n];
}
echo $result;

<?php
[$n, $k] = fscanf(STDIN, '%u %u');
$arr = explode(' ', trim(fgets(STDIN)));
$pairDiff = [];
foreach ($arr as $i => $v) {
    $pairDiff[] = ($arr[$i + 1] ?? $arr[$i]) -  $arr[$i];
}
$result = (int)$v - (int)$arr[0];
while ($k--) {
    $result -= $pairDiff[$n - $k - 1];
}
echo $result;

<?php
[$n, $k] = fscanf(STDIN, '%u %u');
if ($n === $k) {
    die('0');
}
$arr = explode(' ', trim(fgets(STDIN)));
$pairDiff = [];
foreach ($arr as $i => $v) {
    if (isset($arr[$i + 1])) {
        $pairDiff[] = $arr[$i + 1] -  $arr[$i];
    }
}
if ($k === '1') {
    die(array_sum($pairDiff));
}
$limit = $n - $k;
$result = 0;
for ($i = 0; $i < $limit; $i++) {
    $result += $pairDiff[$i];
}
echo $result;

<?php
[$n, $k] = explode(' ', trim(fgets(STDIN)));
$arr = explode(' ', trim(fgets(STDIN)));
$result = $arr[$n - 1] - $arr[0];
$diff = [];
foreach ($arr as $i => $v) {
    $diff[] = ($arr[$i + 1] ?? $v) - $v;
}
echo array_sum(array_slice($diff, 0, $n - $k));
//15502462 1301009


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

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

   $n = IO::get();

   for($i=0; $i < $n; $i++) {
        $temp = '1 ';
        if($i > 1) {
             for($j=1; $j < $i; $j++) {
               $temp .= '0 ';
             }
        }
        if($i > 0) {
           $temp .= '1';
        }

        IO::pr($temp);
   }
}

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

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

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

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

   $n = IO::get();

   for($i=0; $i < $n; $i++) {
        $temp = '1 ';
        if($i > 1) {
             for($j=1; $j < $i-1; $j++) {
               $temp .= '0 ';
             }
        }
        if($i > 0) {
           $temp .= '1';
        }

        IO::pr($temp);
   }
}

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

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

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

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

   $n = IO::get();

   for($i=0; $i < $n; $i++) {
        $temp = '1 ';
        if($i > 1) {
             for($j=1; $j < $n-1; $j++) {
               $temp .= '0 ';
             }
        }
        if($i > 0) {
           $temp .= '1';
        }

        IO::pr($temp);
   }
}

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

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


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

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

function str($s) {
  $r = strlen($s);
  $n = 0;
  foreach (str_split($s) as $c) {
    if ($c == $s[0]) { $n++; }
    else {
      $n--;
      if ($n < 0) {
        $n++;
        $r--;
      }
    }
  }
  $r -= $n;
  return $r;
}

for ($i = 0; $i < $n; $i++) {
  $str = trim(fgets($file));
  $r = strlen($str);
  $s1 = $str[0];
  $sn = $str[$r - 1];

  if ($s1 != $sn) {
    $str2 = $str;
    $str_n = "ABC";
    $k = preg_split("/[".$s1.$sn."]+/", $str_n);
    rsort($k);

    $str = strtr($str, $s1, "(");
    $str = strtr($str, $k[0], ")");
    $str = strtr($str, $sn, ")");

    $str2 = strtr($str2, $s1, "(");
    $str2 = strtr($str2, $k[0], "(");
    $str2 = strtr($str2, $sn, ")");

    $s = str($str);
    $s1 = str($str2);

    if ($s == strlen($str) || $s1 == strlen($str)) { echo 'YES'.PHP_EOL; }
    else { echo 'NO'.PHP_EOL; }

  } else { echo 'NO'.PHP_EOL; }
}
?>

<?php
function solve() {
    $a = Read::string();
    $count = strlen($a);
    $b = [
        '(' => 0,
        ')' => 0
    ];
    $bStatus = true;
    for ($i = 0; $i < $count; $i++) {
        if ($a[0] == $a[$i]) {
            $b['('] += 1;
        } elseif ($a[$count-1] == $a[$i]) {
            $b[')'] += 1;
        } else {
            $b['('] += 1;
        }
        if ($b['('] < $b[')']) {
            $bStatus = false;
            break;
        }
    }
    $counter = 0;
    if ($b['('] == $b[')']) {
        $counter += 1;
    }
    if ($bStatus) {
        $b = [
            '(' => 0,
            ')' => 0
        ];
        for ($i = $count-1; $i >= 0; $i--) {
            if ($a[0] == $a[$i]) {
                $b['('] += 1;
            } elseif ($a[$count-1] == $a[$i]) {
                $b[')'] += 1;
            } else {
                $b[')'] += 1;
            }
            if ($b[')'] < $b['(']) {
                $bStatus = false;
                break;
            }
        }
        if ($b['('] == $b[')']) {
            $counter += 1;
        }
    }

    echo $a[0] != $a[$count-1] && $count % 2 == 0 && $bStatus && $counter != 0? 'YES' : 'NO';

}

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

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

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

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

}

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

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

    /**
     * @param string $bigint1
     * @param string $bigint2
     * @param string $scale (ceil or floor or round)
     * @return string $bigint1 - $bigint2
     */
    public static function 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
	$t = trim(fgets(STDIN));

	function check1($s) {
		$counter = 0;
		if ($s[0] == $s[strlen($s)-1])
			return false;
		for ($i = 0; $i < strlen($s); $i++) {
			if ($counter < 0)
				return false;
		if ($s[$i] == $s[0] || $s[$i] == 'A')
			$counter++;
		else
			$counter--;
		}
		return $counter == 0;
	}

	function check2($s) {
		$counter = 0;
		if ($s[0] == $s[strlen($s)-1])
			return false;
		for ($i = 0; $i < strlen($s); $i++) {
			if ($counter < 0)
				return false;
		if ($s[$i] == $s[0] || $s[$i] == 'B')
			$counter++;
		else
			$counter--;
		}
		return $counter == 0;
	}

	function check3($s) {
		$counter = 0;
		if ($s[0] == $s[strlen($s)-1])
			return false;
		for ($i = 0; $i < strlen($s); $i++) {
			if ($counter < 0)
				return false;
		if ($s[$i] == $s[0] || $s[$i] == 'C')
			$counter++;
		else
			$counter--;
		}
		return $counter == 0;
	}

	for ($o = 0; $o < $t; $o++) {
		$str = trim(fgets(STDIN));
		if (check1($str) || check2($str) || check3($str)) {
			print "YES\n";
		} else {
			print "NO\n";
		}
	}
?>

<?php

declare(strict_types=1);

ini_set('display_errors','stderr');
ini_set('error_reporting','-1');

function solve(string $s): bool
{
	$n=strlen($s);
	$l=$s[0];
	$r=$s[$n-1];
	if ($l===$r) {
		return false;
	}
	$a_ok=$b_ok=true;
	$a=$b=0;
	for ($i=0;$i<$n;++$i) {
		if ($s[$i]===$l) {
			++$a;
			++$b;
		} elseif ($s[$i]===$r) {
			--$a;
			--$b;
		} else {
			++$a;
			--$b;
		}
		if ($a<0) {
			$a_ok=false;
		}
		if ($b<0) {
			$b_ok=false;
		}
	}
	return ($a_ok && $a===0) || ($b_ok && $b===0);
}

$n=(int)fgets(STDIN);
for ($i=0;$i<$n;++$i) {
	$s=trim(fgets(STDIN));
	if (solve($s)) {
		echo "YES\n";
	} else {
		echo "NO\n";
	}
}

<?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;
    }
}

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

while($iQueryCount > 0) {
    $iQueryCount--;
    solve();
}
function solve() {
    $a = str_split(IO::str());
    $c = [];
    $c[$a[0]] = 1;
    $c[$a[count($a) - 1]] = -1;
    $res1 = $res2 = [
        'open' => 0, 'close' => 0
    ];
    $bool = true;
    $bool2 = true;
    $res = $res0 = 0;
    foreach ($a as $k => $b) {
        if(isset($c[$b])) {
            $res0 += $c[$b];
            if ($c[$b] == 1) {
                $res1['open'] += 1;
            } else {
                $res1['close'] += 1;
            }
        } else {
            $res0 -= 1;
            $res1['close'] += 1;
        }
        if ($res0 < 0) {
            $bool = false;
        }
    }
    foreach ($a as $k => $b) {
        if(isset($c[$b])) {
            $res += $c[$b];
            if ($c[$b] == 1) {
                $res2['open'] += 1;
            } else {
                $res2['close'] += 1;
            }
        } else {
            $res += 1;
            $res2['open'] += 1;
        }
        if ($res < 0) {
            $bool2 = false;
        }
    }

    if (($bool && $res1['close'] == $res1['open']) || ($bool2 && $res2['close'] == $res2['open'])) {
        IO::line('YES');
    } else {
        IO::line('NO');
    }
}

<?php

$n = readline();

while ($n--) {
    $word = readline();
    $f = $word[0];
    if ($f == $word[strlen($word) - 1]) {
        $res[] = 'NO';
        continue;
    }
    $cnt = $a = $b = $c = 0;
    for ($i = 0; $i < strlen($word); $i++) {
        if ($word[$i] == 'A') {
            $a++;
        } else if ($word[$i] == 'B') {
            $b++;
        } else {
            $c++;
        }
    }
    $cnt = max($a, $b, $c);
    $lf = strlen($word) - $cnt;

    $k = 0; $br = false;
    if ($lf == $cnt) {
        $res[] = 'YES';
    } else {
        $res[] = 'NO';
    }
}

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

<?php

$n = readline();

while ($n--) {
    $word = readline();
    $f = $word[0];
    if ($f == $word[strlen($word) - 1]) {
        $res[] = 'NO';
        continue;
    }
    $cnt = 0;
    for ($i = 0; $i < strlen($word); $i++) {
        if ($word[$i] == $f) {
            $cnt++;
        }
    }
    $lf = strlen($word) - $cnt;

    $k = 0; $br = false;
    if ($lf == $cnt) {
        for ($i = 0; $i < strlen($word); $i++) {
            if ($word[$i] == $f) {
                $k++;
            } else {
                $k--;
                if ($k < 0) {
                    $res[] ='NO';
                    $br = true;
                    break;
                }
            }
        }
        if ($br) continue;
        if ($k == 0) {
            $res[] = 'YES';
        }
    } else {
        $res[] = 'NO';
    }
}

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

<?php

$n = readline();

while ($n--) {
    $word = readline();
    $f = $word[0];
    if ($f == $word[strlen($word) - 1]) {
        $res[] = 'NO';
        continue;
    }
    $cnt = 0;
    for ($i = 0; $i < strlen($word); $i++) {
        if ($word[$i] == $f) {
            $cnt++;
        }
    }
    $lf = strlen($word) - $cnt;

    if ($lf == $cnt) {
        $res[] = 'YES';
    } else {
        $res[] = 'NO';
    }
}

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

<?php
function solve() {
    list($n, $U, $R, $D, $L) = Read::array_int();
    $a = [
        'U' => $U,
        'R' => $R,
        'D' => $D,
        'L' => $L
    ];
    if ($U >= $n) {
        $R -= 1;
        $L -= 1;
    }
    if ($D >= $n) {
        $R -= 1;
        $L -= 1;
    }
    if ($R >= $n) {
        $U -= 1;
        $D -= 1;
    }
    if ($L >= $n) {
        $U -= 1;
        $D -= 1;
    }

    echo max($a) <= $n && $U >= 0 && $R >= 0 && $D >= 0 && $L >= 0 ? 'YES' : 'NO';

}

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

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

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

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

}

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

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

    /**
     * @param string $bigint1
     * @param string $bigint2
     * @param string $scale (ceil or floor or round)
     * @return string $bigint1 - $bigint2
     */
    public static function 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() {
    $a = Read::string();
    $count = strlen($a);
    $b = [
        '(' => 0,
        ')' => 0
    ];
    $bStatus = true;
    for ($i = 0; $i < $count; $i++) {
        if ($a[0] == $a[$i]) {
            $b['('] += 1;
        } elseif ($a[$count-1] == $a[$i]) {
            $b[')'] += 1;
        } else {
            $b['('] += 1;
        }
        if ($b['('] < $b[')']) {
            $bStatus = false;
            break;
        }
    }
    if ($b['('] != $b[')']) {
        $bStatus = false;
    }
    if ($bStatus) {
        $b = [
            '(' => 0,
            ')' => 0
        ];
        for ($i = $count-1; $i >= 0; $i--) {
            if ($a[0] == $a[$i]) {
                $b['('] += 1;
            } elseif ($a[$count-1] == $a[$i]) {
                $b[')'] += 1;
            } else {
                $b[')'] += 1;
            }
            if ($b[')'] < $b['(']) {
                $bStatus = false;
                break;
            }
        }
        if ($b['('] != $b[')']) {
            $bStatus = false;
        }
    }

    echo $a[0] != $a[$count-1] && $count % 2 == 0 && $bStatus ? 'YES' : 'NO';

}

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

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

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

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

}

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

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

    /**
     * @param string $bigint1
     * @param string $bigint2
     * @param string $scale (ceil or floor or round)
     * @return string $bigint1 - $bigint2
     */
    public static function 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() {
    $a = Read::string();
    $count = strlen($a);
    $b = [
        '(' => 0,
        ')' => 0
    ];
    $bStatus = true;
    for ($i = 0; $i < $count; $i++) {
        if ($a[0] == $a[$i]) {
            $b['('] += 1;
        } elseif ($a[$count-1] == $a[$i]) {
            $b[')'] += 1;
        } else {
            $b['('] += 1;
        }
        if ($b['('] < $b[')']) {
            $bStatus = false;
            break;
        }
    }
    if ($bStatus) {
        $b = [
            '(' => 0,
            ')' => 0
        ];
        for ($i = $count-1; $i >= 0; $i--) {
            if ($a[0] == $a[$i]) {
                $b['('] += 1;
            } elseif ($a[$count-1] == $a[$i]) {
                $b[')'] += 1;
            } else {
                $b[')'] += 1;
            }
            if ($b[')'] < $b['(']) {
                $bStatus = false;
                break;
            }
        }
    }

    echo $a[0] != $a[$count-1] && $count % 2 == 0 && $bStatus ? 'YES' : 'NO';

}

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

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

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

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

}

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

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

    /**
     * @param string $bigint1
     * @param string $bigint2
     * @param string $scale (ceil or floor or round)
     * @return string $bigint1 - $bigint2
     */
    public static function 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() {
    $a = Read::string();
    $count = strlen($a);
    $b = [
        '(' => 0,
        ')' => 0
    ];
    $bStatus = true;
    for ($i = 0; $i < $count; $i++) {
        if ($a[0] == $a[$i]) {
            $b['('] += 1;
        } elseif ($a[$count-1] == $a[$i]) {
            $b[')'] += 1;
        } else {
            $b['('] += 1;
        }
        if ($b['('] < $b[')']) {
            $bStatus = false;
            break;
        }
    }
    if ($bStatus) {
        $b = [
            '(' => 0,
            ')' => 0
        ];
        for ($i = $count-1; $i >= 0; $i--) {
            if ($a[0] == $a[$i]) {
                $b['('] += 1;
            } elseif ($a[$count-1] == $a[$i]) {
                $b[')'] += 1;
            } else {
                $b[')'] += 1;
            }
            if ($b[')'] < $b['(']) {
                $bStatus = false;
                break;
            }
        }
    }

    echo $a[0] != $a[$count-1] && $bStatus ? 'YES' : 'NO';

}

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

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

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

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

}

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

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

    /**
     * @param string $bigint1
     * @param string $bigint2
     * @param string $scale (ceil or floor or round)
     * @return string $bigint1 - $bigint2
     */
    public static function 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() {
    $a = Read::string();
    $count = strlen($a);
    $iCount = 0;
    $iEndCount = 0;
    $iCountSred = 0;
    for ($i = 0; $i < $count; $i++) {
        if ($a[0] == $a[$i]) {
            $iCount += 1;
        } elseif ($a[$count-1] == $a[$i]) {
            $iEndCount += 1;
        } else {
            $iCountSred +=1;
        }
    }
    echo $a[0] != $a[$count-1] && ($iCount + $iCountSred == $iEndCount || $iEndCount + $iCountSred == $iCount) ? 'YES' : 'NO';

}

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

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

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

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

}

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

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

    /**
     * @param string $bigint1
     * @param string $bigint2
     * @param string $scale (ceil or floor or round)
     * @return string $bigint1 - $bigint2
     */
    public static function 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() {
    $a = Read::string();
    $count = strlen($a);
    $iCount = 0;
    for ($i = 0; $i < $count; $i++) {
        if ($a[0] == $a[$i]) {
            $iCount += 1;
        }
    }

    echo $a[0] != $a[$count-1] && $count % 2 == 0 && $iCount <= ($count - $iCount) ? 'YES' : 'NO';

}

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

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

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

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

}

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

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

    /**
     * @param string $bigint1
     * @param string $bigint2
     * @param string $scale (ceil or floor or round)
     * @return string $bigint1 - $bigint2
     */
    public static function 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
	$t = trim(fgets(STDIN));

	function check1($s) {
		$counter = 0;
		if ($s[0] == $s[strlen($s)-1])
			return false;
		for ($i = 0; $i < strlen($s); $i++) {
		if ($s[$i] == $s[0] || $s[$i] == 'A')
			$counter++;
		else
			$counter--;
		}
		if ($counter < 0)
			return false;
		return $counter == 0;
	}

	function check2($s) {
		$counter = 0;
		if ($s[0] == $s[strlen($s)-1])
			return false;
		for ($i = 0; $i < strlen($s); $i++) {
		if ($s[$i] == $s[0] || $s[$i] == 'B')
			$counter++;
		else
			$counter--;
		}
		if ($counter < 0)
			return false;
		return $counter == 0;
	}

	function check3($s) {
		$counter = 0;
		if ($s[0] == $s[strlen($s)-1])
			return false;
		for ($i = 0; $i < strlen($s); $i++) {
		if ($s[$i] == $s[0] || $s[$i] == 'C')
			$counter++;
		else
			$counter--;
		}
		if ($counter < 0)
			return false;
		return $counter == 0;
	}

	function check4($s) {
		$counter = 0;
		if ($s[0] == $s[strlen($s)-1])
			return false;
		for ($i = 0; $i < strlen($s); $i++) {
		if ($s[$i] == $s[0])
			$counter++;
		else
			$counter--;
		}
		if ($counter < 0)
			return false;
		return $counter == 0;
	}

	for ($o = 0; $o < $t; $o++) {
		$str = trim(fgets(STDIN));
		if (check1($str) || check2($str) || check3($str) || check4($str)) {
			print "YES\n";
		} else {
			print "NO\n";
		}
	}
?>

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

	function check1($s) {
		$counter = 0;
		if ($s[0] == $s[strlen($s)-1])
			return false;
		for ($i = 0; $i < strlen($s); $i++) {
		if ($s[$i] == $s[0] || $s[$i] == 'A')
			$counter++;
		else
			$counter--;
		}
		if ($counter < 0)
			return false;
		return $counter == 0;
	}

	function check2($s) {
		$counter = 0;
		if ($s[0] == $s[strlen($s)-1])
			return false;
		for ($i = 0; $i < strlen($s); $i++) {
		if ($s[$i] == $s[0] || $s[$i] == 'B')
			$counter++;
		else
			$counter--;
		}
		if ($counter < 0)
			return false;
		return $counter == 0;
	}

	function check3($s) {
		$counter = 0;
		if ($s[0] == $s[strlen($s)-1])
			return false;
		for ($i = 0; $i < strlen($s); $i++) {
		if ($s[$i] == $s[0] || $s[$i] == 'C')
			$counter++;
		else
			$counter--;
		}
		if ($counter < 0)
			return false;
		return $counter == 0;
	}

	for ($o = 0; $o < $t; $o++) {
		$str = trim(fgets(STDIN));
		if (check1($str) || check2($str) || check3($str)) {
			print "YES\n";
		} else {
			print "NO\n";
		}
	}
?>

<?php

$t = (int) trim(fgets(STDIN));
for ($i = 0; $i < $t; ++$i) {
    //i[]
    $si[] = trim(fgets(STDIN));
}
for ($i = 0; $i < $t; ++$i) {
    $s = $si[$i];
    $len = strlen($s);
    $cnta = substr_count($s, 'A');
    $cntb = substr_count($s, 'B');
    $cntc = $len - $cnta - $cntb;
    if ($len == $cnta * 2 || $len == $cntb * 2 || $len == $cntc * 2) {
        echo ($s[0] !== $s[-1] ? 'YES' : 'NO').PHP_EOL;
    } else {
        echo 'NO'.PHP_EOL;
    }
}

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

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

function str($s) {
  $ret = strlen($s);
  $now = 0;
  foreach (str_split($s) as $c) {
    if ($c == $s[0]) { $now++; }
    else {
      $now--;
      if ($now < 0) {
        $now++;
        $ret--;
      }
    }
  }
  $ret -= $now;
  return $ret;
}

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

  $s = str($str);
  if ($s == strlen($str)) { echo 'YES'.PHP_EOL; }
  else { echo 'NO'.PHP_EOL; }
}
?>


<?php

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

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

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

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

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

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

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

function readNumArray($f) {
	return readStringExplodeNum($f, ' ', true);
}

function readArray($f) {
	return readStringExplodeNum($f, ' ');
}

function readTrimString($f) {
	return trim(readString($f));
}

###### /IO ######
# for($i=0;$i<$n;$i++)

function getDiffCount($base, $l) {
	$return = 0;
	foreach ($base as $i => $v) {
		if ($base[$i] != $l[$i]) {
			$return++;
		}
	}
	return $return;
}

function getAnswer($is_test = false, $f = false) {
	if ($is_test === false) {
		$f = STDIN;
	}
	/* begin body */
	$k = readNum($f);
	$n = readString($f);
	$items = str_split($n);
	$len = count($items);
	sort($items);
	$nSum = array_sum($items);
	$pos = 0;
	$steps = 0;
	while ($nSum < $k && $pos < $len) {
		$need = $k - $nSum;
		$canInc = min(9 - $items[$pos], $need);

		$items[$pos] += $canInc;
		$nSum += $canInc;
		$steps++;
		$pos++;
	}
	echo $steps;
	/* / end body */
}

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


<?
$str=str_replace(' ','',trim(fgets(STDIN)));
$text=str_replace(' ','',trim(fgets(STDIN)));
$no=0;
$yes=0;
for($i=0;$i<strlen($text);$i++){
    if(substr_count($str,$text[$i])<substr_count($text,$text[$i]))
    $no=1;
    else $yes=1;
}
if($no==0 && $yes==1) echo "YES";
else echo "NO";
?>

<?php
    $header = str_split(str_replace(" ", "", trim(fgets(STDIN))));
    $text = str_split(str_replace(" ", "", trim(fgets(STDIN))));

    $alph_in_header = array();
    $alph_in_text = array();

    foreach ($header as $val) {
        if (array_key_exists($val, $alph_in_header))
                $alph_in_header[$val] += 1;
        else
                $alph_in_header[$val] = 1;
    }

    foreach ($text as $val) {
        if (array_key_exists($val, $alph_in_text))
                $alph_in_text[$val] += 1;
        else
                $alph_in_text[$val] = 1;
    }

    foreach ($text as $val) {
        if (!array_key_exists($val, $alph_in_header) || $alph_in_text[$val] > $alph_in_header[$val]) {
            echo "NO";
            die();
        }
    }

    echo "YES";
?>

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));
$c = str_split($a);
$d = str_split($b);
for($x = 0; $x < count($c); $x++)
{
    if($c[$x] == " ")
    {
        continue;
    }
    else
    {
        for($y = 0; $y < count($d); $y++)
        {
            if($c[$x] == $d[$y])
            {
                unset($d[$y]);
                sort($d);
            }
        }
    }
}
$e = array_unique($d);
sort($e);
if((count($e) == 0) || ((count($e) == 1) && ($e[0] == " ")))
{
    print "YES";
}
else
{
    print "NO";
}
?>

<?php
    $header = str_split(str_replace(" ", "", trim(fgets(STDIN))));
    $text = str_split(str_replace(" ", "", trim(fgets(STDIN))));

    asort($header);
    asort($text);

    if (implode("", array_intersect($text, $header)) == implode("", $text))
            echo "YES";
    else
            echo "NO";
?>

<?php
    $header = str_split(str_replace(" ", "", trim(fgets(STDIN))));
    $text = str_split(str_replace(" ", "", trim(fgets(STDIN))));

    $alph_in_header = array();
    $alph_in_text = array();

    foreach ($header as $val) {
        if (array_key_exists($val, $alph_in_text))
                $alph_in_header[$val] += 1;
        else
                $alph_in_header[$val] = 1;
    }

    foreach ($text as $val) {
        if (array_key_exists($val, $alph_in_text))
                $alph_in_text[$val] += 1;
        else
                $alph_in_text[$val] = 1;
    }

    foreach ($text as $val) {
        if (!array_key_exists($val, $alph_in_header) || $alph_in_text[$val] > $alph_in_header[$val]) {
            echo "NO";
            die();
        }
    }

    echo "YES";
?>