<?php

//$src = 'input.txt';
$src = 'php://stdin';

$data = file($src);

list($n, $m) = array_map('intval', explode(' ', array_shift($data)));

$prices = array_map('intval', explode(' ', array_shift($data)));

$exists = false;
$map = array();
foreach ($data as $match) {
	if (!$match) {
		break;
	}
	$new_match = array_map('intval', explode(' ', $match));
	$map[$new_match[0] - 1][$new_match[1] - 1] = $map[$new_match[1] - 1][$new_match[0] - 1] = true;
}

$min = -1;
foreach ($map as $item => $matches) {
	if (count($matches) < 2) {
		continue;
	}
	$matches_keys = array_keys($matches);
	$m = count($matches);
	for ($i = 0; $i < $m - 1; $i++) {
		$item_2 = $matches_keys[$i];
		for ($j = $i + 1; $j < $m; $j++) {
			$item_3 = $matches_keys[$j];
			if (!isset($map[$item_2][$item_3])) {
				continue;
			}
			$price = $prices[$item] + $prices[$item_2] + $prices[$item_3];
			if ($price < $min || $min == -1) {
				$min = $price;
			}
		}
	}
}

echo $min;

<?php

//$src = 'input.txt';
$src = 'php://input';

$data = file_get_contents($src);
$data = explode("\r\n", $data); // If this was the case, I blame the contest staff as highly unprofessional

list($n, $m) = array_map('intval', explode(' ', array_shift($data)));

$prices = array_map('intval', explode(' ', array_shift($data)));

$exists = false;
$map = array();
foreach ($data as $match) {
	if (!$match) {
		break;
	}
	$new_match = array_map('intval', explode(' ', $match));
	foreach ($new_match as $k => $item) {
		$new_match[$k] = $item - 1;
	}
	foreach ($new_match as $k => $item) {
		$other_item = $new_match[(int) !$k];
		$map[$item][$other_item] = true;
	}
}

$min = -1;
foreach ($map as $item => $matches) {
	if (count($matches) < 2) {
		foreach ($map as $k => $it) {
			unset($map[$k][$item]);
		}
	}
}

foreach ($map as $item => $matches) {
	if (count($matches) < 2) {
		continue;
	}
	$matches_keys = array_keys($matches);
	for ($i = 0; $i < count($matches) - 1; $i++) {
		$item_2 = $matches_keys[$i];
		for ($j = $i + 1; $j < count($matches); $j++) {
			$item_3 = $matches_keys[$j];
			if (!isset($map[$item_2][$item_3])) {
				continue;
			}
			$price = array_sum(array_intersect_key($prices, array($item, $item_2, $item_3)));
			if ($price < $min || $min == -1) {
				$min = $price;
			}
		}
	}
}

echo $min;

<?php

//$src = 'input.txt';
$src = 'php://input';

$data = file($src);

list($n, $m) = array_map('intval', explode(' ', array_shift($data)));

$prices = array_map('intval', explode(' ', array_shift($data)));

$exists = false;
$map = array();
foreach ($data as $match) {
	if (!$match) {
		break;
	}
	$new_match = array_map('intval', explode(' ', $match));
	foreach ($new_match as $k => $item) {
		$new_match[$k] = $item - 1;
	}
	foreach ($new_match as $k => $item) {
		$other_item = $new_match[(int) !$k];
		$map[$item][$other_item] = true;
	}
}

$min = -1;
foreach ($map as $item => $matches) {
	if (count($matches) < 2) {
		foreach ($map as $k => $it) {
			unset($map[$k][$item]);
		}
	}
}

foreach ($map as $item => $matches) {
	if (count($matches) < 2) {
		continue;
	}
	$matches_keys = array_keys($matches);
	for ($i = 0; $i < count($matches) - 1; $i++) {
		$item_2 = $matches_keys[$i];
		for ($j = $i + 1; $j < count($matches); $j++) {
			$item_3 = $matches_keys[$j];
			if (!isset($map[$item_2][$item_3])) {
				continue;
			}
			$price = array_sum(array_intersect_key($prices, array($item, $item_2, $item_3)));
			if ($price < $min || $min == -1) {
				$min = $price;
			}
		}
	}
}

echo $min . "\r\n";

<?php

//$src = 'input.txt';
$src = 'php://input';

$data = file($src);

list($n, $m) = array_map('intval', explode(' ', array_shift($data)));

$prices = array_map('intval', explode(' ', array_shift($data)));

$exists = false;
$map = array();
foreach ($data as $match) {
	if (!$match) {
		break;
	}
	$new_match = array_map('intval', explode(' ', $match));
	foreach ($new_match as $k => $item) {
		$new_match[$k] = $item - 1;
	}
	foreach ($new_match as $k => $item) {
		$other_item = $new_match[(int) !$k];
		$map[$item][$other_item] = true;
	}
}

$min = PHP_INT_MAX;
foreach ($map as $item => $matches) {
	if (count($matches) < 2) {
		foreach ($map as $k => $it) {
			unset($map[$k][$item]);
		}
	}
}

foreach ($map as $item => $matches) {
	if (count($matches) < 2) {
		continue;
	}
	$matches_keys = array_keys($matches);
	for ($i = 0; $i < count($matches) - 1; $i++) {
		$item_2 = $matches_keys[$i];
		for ($j = $i + 1; $j < count($matches); $j++) {
			$item_3 = $matches_keys[$j];
			if (!isset($map[$item_2][$item_3])) {
				continue;
			}
			$price = array_sum(array_intersect_key($prices, array($item, $item_2, $item_3)));
			if ($price < $min) {
				$min = $price;
			}
		}
	}
}

echo $min == PHP_INT_MAX ? -1 : $min;
echo PHP_EOL;

<?php

//$src = 'input.txt';
$src = 'php://input';

$data = file($src);

list($n, $m) = array_map('intval', explode(' ', array_shift($data)));

$prices = array_map('intval', explode(' ', array_shift($data)));

$exists = false;
$map = array();
foreach ($data as $match) {
	if (!$match) {
		break;
	}
	$new_match = array_map('intval', explode(' ', $match));
	foreach ($new_match as $k => $item) {
		$new_match[$k] = $item - 1;
	}
	foreach ($new_match as $k => $item) {
		$other_item = $new_match[(int) !$k];
		$map[$item][$other_item] = true;
	}
}

$min = -1;
foreach ($map as $item => $matches) {
	if (count($matches) < 2) {
		foreach ($map as $k => $it) {
			unset($map[$k][$item]);
		}
	}
}

foreach ($map as $item => $matches) {
	if (count($matches) < 2) {
		continue;
	}
	$matches_keys = array_keys($matches);
	for ($i = 0; $i < count($matches) - 1; $i++) {
		$item_2 = $matches_keys[$i];
		for ($j = $i + 1; $j < count($matches); $j++) {
			$item_3 = $matches_keys[$j];
			if (!isset($map[$item_2][$item_3])) {
				continue;
			}
			$price = array_sum(array_intersect_key($prices, array($item, $item_2, $item_3)));
			if ($price < $min || $min == -1) {
				$min = $price;
			}
		}
	}
}

echo $min;

<?php

//$src = 'input.txt';
$src = 'php://stdin';

$data = file($src);

list($n, $m) = array_map('intval', explode(' ', array_shift($data)));

$prices = array_map('intval', explode(' ', array_shift($data)));

$exists = false;
$map = array();
foreach ($data as $match) {
	if (!$match) {
		break;
	}
	$new_match = array_map('intval', explode(' ', $match));
	foreach ($new_match as $k => $item) {
		$new_match[$k] = $item - 1;
	}
	foreach ($new_match as $k => $item) {
		$other_item = $new_match[(int) !$k];
		$map[$item][$other_item] = true;
	}
}

$min = -1;
foreach ($map as $item => $matches) {
	if (count($matches) < 2) {
		foreach ($map as $k => $it) {
			unset($map[$k][$item]);
		}
	}
}

foreach ($map as $item => $matches) {
	if (count($matches) < 2) {
		continue;
	}
	$matches_keys = array_keys($matches);
	for ($i = 0; $i < count($matches) - 1; $i++) {
		$item_2 = $matches_keys[$i];
		for ($j = $i + 1; $j < count($matches); $j++) {
			$item_3 = $matches_keys[$j];
			if (!isset($map[$item_2][$item_3])) {
				continue;
			}
			$price = array_sum(array_intersect_key($prices, array($item, $item_2, $item_3)));
			if ($price < $min || $min == -1) {
				$min = $price;
			}
		}
	}
}

echo $min;

<?php

//$src = 'input.txt';
$src = 'php://input';

$data = file_get_contents($src);
$data = explode("\n", $data); // If this was the case, I blame the contest staff as highly unprofessional

list($n, $m) = array_map('intval', explode(' ', array_shift($data)));

$prices = array_map('intval', explode(' ', array_shift($data)));

$exists = false;
$map = array();
foreach ($data as $match) {
	if (!$match) {
		break;
	}
	$new_match = array_map('intval', explode(' ', $match));
	foreach ($new_match as $k => $item) {
		$new_match[$k] = $item - 1;
	}
	foreach ($new_match as $k => $item) {
		$other_item = $new_match[(int) !$k];
		$map[$item][$other_item] = true;
	}
}

$min = -1;
foreach ($map as $item => $matches) {
	if (count($matches) < 2) {
		foreach ($map as $k => $it) {
			unset($map[$k][$item]);
		}
	}
}

foreach ($map as $item => $matches) {
	if (count($matches) < 2) {
		continue;
	}
	$matches_keys = array_keys($matches);
	for ($i = 0; $i < count($matches) - 1; $i++) {
		$item_2 = $matches_keys[$i];
		for ($j = $i + 1; $j < count($matches); $j++) {
			$item_3 = $matches_keys[$j];
			if (!isset($map[$item_2][$item_3])) {
				continue;
			}
			$price = array_sum(array_intersect_key($prices, array($item, $item_2, $item_3)));
			if ($price < $min || $min == -1) {
				$min = $price;
			}
		}
	}
}

echo $min;

<?php

//$src = 'input.txt';
$src = 'php://input';

$data = file_get_contents($src);
die($data);
$data = explode("\n", $data); // If this was the case, I blame the contest staff as highly unprofessional

list($n, $m) = array_map('intval', explode(' ', array_shift($data)));

$prices = array_map('intval', explode(' ', array_shift($data)));

$exists = false;
$map = array();
foreach ($data as $match) {
	if (!$match) {
		break;
	}
	$new_match = array_map('intval', explode(' ', $match));
	foreach ($new_match as $k => $item) {
		$new_match[$k] = $item - 1;
	}
	foreach ($new_match as $k => $item) {
		$other_item = $new_match[(int) !$k];
		$map[$item][$other_item] = true;
	}
}

$min = -1;
foreach ($map as $item => $matches) {
	if (count($matches) < 2) {
		foreach ($map as $k => $it) {
			unset($map[$k][$item]);
		}
	}
}

foreach ($map as $item => $matches) {
	if (count($matches) < 2) {
		continue;
	}
	$matches_keys = array_keys($matches);
	for ($i = 0; $i < count($matches) - 1; $i++) {
		$item_2 = $matches_keys[$i];
		for ($j = $i + 1; $j < count($matches); $j++) {
			$item_3 = $matches_keys[$j];
			if (!isset($map[$item_2][$item_3])) {
				continue;
			}
			$price = array_sum(array_intersect_key($prices, array($item, $item_2, $item_3)));
			if ($price < $min || $min == -1) {
				$min = $price;
			}
		}
	}
}

echo $min;


<?php

$testCases = (int) fgets(STDIN);
$answers = [];

for ($j=0; $j < $testCases; $j++) {
    $line = explode(' ', fgets(STDIN));
    $time = $line[0];
    $minutes = (int) $line[1];

    $lastTime = null;
    $palindromeCount = 0;
    while ($time != $lastTime) {
        $lastTime = date('H:i', strtotime("+" . $minutes . " minutes", strtotime($lastTime ?? $time)));
        $palindromeCount += ($lastTime === strrev($lastTime)) ? 1 : 0;
    }

    $answers[] = $palindromeCount;
}

foreach ($answers as $item) {
    echo $item . PHP_EOL;
}

<?php
function getPal(){
    $time = explode(" ", trim(fgets(STDIN)));
    $out_dt='';
    $pal = 0;
    while ($out_dt!=$time[0]){
        if ($out_dt!=''){
            $in_dt = $out_dt;
        } else {
            $in_dt = $time[0];
        }
        $data_time = explode(":",$in_dt);
        $date = new DateTime('2001-01-01');
        $dt= $date->setTime($data_time[0],$data_time[1]);
        $dt->add(new DateInterval('PT' . $time[1] . 'M'));
        $out_dt = $dt->format('H:i');
        if ($out_dt[0]==$out_dt[4] && $out_dt[1]==$out_dt[3]){
            ++$pal;
        }
    }
    echo "$pal\n";

}
$num = trim(fgets(STDIN));
for($k = 0; $k<$num; $k++){
    getPal();
}
?>

<?php
fscanf(STDIN, "%d", $t);
while ($t--) {
    fscanf(STDIN, "%s %d", $s, $x);
    $s2 = $s;
    $i = 0;
    do {
        $s2 = date_add(new DateTime($s2), new DateInterval("PT{$x}M"))->format('H:i')
        and $s2 == strrev($s2)
        and $i++;
    } while ($s2 != $s);
    echo $i, "\n";
}

<?php

$testCases = (int) fgets(STDIN);
$answers = [];

for ($j=0; $j < $testCases; $j++) {
    $line = explode(' ', fgets(STDIN));
    $time = $line[0];
    $minutes = (int) $line[1];

    $loop = ceil(1440/$minutes);
    $lastTime = $time;

    $palindromeCount = 0;

    for ($i=0; $i < $loop; $i++) {
        $lastTime = date('H:i', strtotime("+" . $minutes . " minutes", strtotime($lastTime)));
        $splitTime = explode(':', $lastTime);
        $palindromeCount += $splitTime[0] === strrev($splitTime[1]) ? 1 : 0;
    }

    $answers[] = $palindromeCount;
}

foreach ($answers as $item) {
    echo $item . PHP_EOL;
}


<?php

function main()
{
    fscanf(STDIN, "%d", $n);
    $segment = -1;
    $leftmost = 1e9;
    $rightmost = 0;
    for ($i = 1; $i <= $n; ++$i)
    {
        fscanf(STDIN, "%d %d", $l, $r);
        if ($l <= $leftmost && $r >= $rightmost)
            $segment = $i;
        else if ($l >= $leftmost && $r <= $rightmost)
            continue;
        else
            $segment = -1;
        $leftmost = min($l, $leftmost);
        $rightmost = max($r, $rightmost);
    }
    fprintf(STDOUT, "%d\n", $segment);
}

main();


<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = array_slice($c, 1);
$e = array();
$f = $b;
$g = range(1, $a);
array_unshift($g, " ");
unset($g[0]);
for($x = 1; $x <= $a; $x++)
{
    if($x == $f)
    {
        array_push($e, $g[$x]);
        $f += $b;
    }
}
$h = array_merge($d, $e);
$i = array_unique($h);
print count($i);
?>


<?php

list($line, $length) = explode(' ', fgets(STDIN));

$mainLine  = str_repeat('#', $length);
$leftLine  = '#' . str_repeat('.', $length - 1);
$rightLine = str_repeat('.', $length - 1) . '#';

for ($curLine = 0; $curLine < $line; ++$curLine) {
    if ($curLine % 2 === 0) {
        echo $mainLine . PHP_EOL;
    }

    if ($curLine % 4 === 1) {
        echo $rightLine . PHP_EOL;
    }
    if ($curLine % 4 === 3) {
        echo $leftLine . PHP_EOL;
    }
}

<?php
// 510A     Лиса и змейка
$in = fopen("php://stdin", 'r');

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

$n = (int) $n;
$m = (int) $m;

$strC = str_repeat('#', $m) . "\n";
$strL = '#' . str_repeat('.', $m - 1) . "\n";
$strR = str_repeat('.', $m - 1) . "#\n";

for ($i=0; $i < $n; $i+=4) {
    echo $strC;
    if (($i + 1) < $n) {
        echo $strR;
    }
    if (($i + 2) < $n) {
        echo $strC;
    }
    if (($i + 3) < $n) {
        echo $strL;
    }
}

?>

<?php

fscanf(STDIN, '%d%d', $n, $m);

$out = array_fill(0, $n, array_fill(0, $m, '.'));

$i = $j = 0;
$dir = 1;

while ($i < $n) {
    $out[$i][$j] = '#';
    $j += $dir;
    if ($j == $m || $j == -1) {
        $j -= $dir;
        $out[$i + 1][$j] = '#';
        $i += 2;
        $dir *= -1;
    }
}

unset($out[$n]);

function glue(array $strings)
{
    return join('', $strings);
}

echo join(PHP_EOL, array_map('glue', $out));


<?php

function print_rect($matrix, $lines, $positions)
{
    for ($k=1; $k <= $matrix; $k++) {
        for ($l=1; $l <= $matrix; $l++) {
            if ($k == $lines[0] || $k == $lines[1]) {
                if ($l == $positions[0] || $l == $positions[1]) {
                    echo "*";
                } else {
                    echo ".";
                }

            }else {
                echo ".";
            }
        }
        echo "\n";
    }
}

function position_maker($lines, $positions)
{
    if ($positions[0] == $positions[1]) {
        if ($positions[0] == $lines) {
            $positions[0]-= 1;
        }else {
            $positions[1]+= 1;
        }
    }

    return $positions;
}

function line_maker($lines, $star_line)
{
    if ($star_line[0] == $star_line[1]) {
        if ($star_line[1] == $lines) {
            $star_line[1]-= 1;
        }else {
            $star_line[1]+= 1;
        }
    }else {

    }

    return $star_line;
}

// --------------
$looper = readline();

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

    $matrix = readline();

    $star_line = array();
    $position = array();

    for ($i=0; $i < $matrix; $i++) {
        $s_line = readline();

        for ($j=0; $j < $matrix; $j++) {
            if ($s_line[$j] == "*") {
                array_push($star_line, $i+1);
                array_push($position, $j+1);
            }
        }
    }

    $star_line = line_maker($matrix, $star_line);
    $position = position_maker($matrix, $position);

    // (4, [1,3], [3,1]);
    print_rect($matrix, $star_line, $position);
}

<?php

$t = readline();

for ($k=0;$k<$t;$k++){

    $n = readline();
    $matrix=[];
    $count = 0;
    $position_i1 = "";
    $position_i2 = "";
    $position_j1 = "";
    $position_j2 = "";

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

        $char = readline();
        $char_element = str_split($char);

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

            $matrix[$i][$j] = $char_element[$j];
        }
    }

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

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

            if($matrix[$i][$j] == "*"){
                if($count>0){

                    $position_i2 = $i;
                    $position_j2 = $j;
                }
                else{
                    $position_i1 = $i;
                    $position_j1 = $j;
                }
                $count++;
            }

            if($count==2){
                break;
            }
        }
    }

    if(($position_i1<$position_i2 && $position_j1>$position_j2) ||
        ($position_i1<$position_i2 && $position_j1<$position_j2) ){

        $matrix[$position_i1][$position_j2] = "*";
        $matrix[$position_i2][$position_j1] = "*";
    }

    elseif (($position_i1<$position_i2 && $position_j1==$position_j2)){

        if($position_j1 == ($n-1)){

            $matrix[$position_i1][$position_j1-1] = "*";
            $matrix[$position_i2][$position_j2-1] = "*";
        }
        else{
            $matrix[$position_i1][$position_j1+1] = "*";
            $matrix[$position_i2][$position_j2+1] = "*";
        }
    }
    elseif($position_i1==$position_i2){

        if($position_i1 == ($n-1)){
            $matrix[$position_i1-1][$position_j1] = "*";
            $matrix[$position_i2-1][$position_j2] = "*";
        }
        else{
            $matrix[$position_i1+1][$position_j1] = "*";
            $matrix[$position_i2+1][$position_j2] = "*";
        }
    }

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

        for ($j = 0; $j < $n; $j++) {
            echo $matrix[$i][$j];
        }
        echo "\n";

    }
}

<?php
    $rectangles = [];
    $cases = trim(fgets(STDIN));
    for($i=0; $i<$cases; $i++){
        $lines = [];
        $n = trim(fgets(STDIN));
        for($j=0; $j<$n; $j++){
            array_push($lines,trim(fgets(STDIN)));
        }
        array_push($rectangles,$lines);
    }

    //v poli (obrazci) hledáme řádek a v něm porovnáme každý znak s *
    for($i=0; $i<count($rectangles); $i++){
        $firstAstX = 0;
        $firstAstY = 0;
        $secondAstX = 0;
        $secondAstY = 0;
        $thirdAstX = 0;
        $thirdAstY = 0;
        $fourthAstX = 0;
        $fourthAstY = 0;
        for($j=0; $j<count($rectangles[$i]); $j++){
            for($k=0; $k<strlen($rectangles[$i][$j]); $k++){
                if(substr($rectangles[$i][$j],$k,1) == "*"){
                    //nalezení druhé * vyvolá komplet změny
                    if($firstAstX > 0){
                        $secondAstX = $k+1;
                        $secondAstY = $j+1;
                        //pokud jsou dvě * nad sebou
                        if($firstAstX == $secondAstX){
                            if($firstAstX > 1){
                                $thirdAstX = $firstAstX-1;
                                $thirdAstY = $firstAstY;
                                $fourthAstX = $secondAstX-1;
                                $fourthAstY = $secondAstY;

                                //přepsání řádku se čtvrtou *
                                if($fourthAstX == 1){
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],1);
                                    $rectangles[$i][$firstAstY-1] = "*" . $fourthZ;
                                }elseif($fourthAstX == strlen($rectangles[$i][$firstAstY-1])){
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],0,strlen($rectangles[$i][$firstAstY-1])-1);
                                    $rectangles[$i][$firstAstY-1] = $fourthZ . "*";
                                }else{
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],0,$fourthAstX-1);
                                    $fourthZz = substr($rectangles[$i][$firstAstY-1],$fourthAstX,strlen($rectangles[$i][$firstAstY-1])-$fourthAstX);
                                    $rectangles[$i][$firstAstY-1] = $fourthZ . "*" . $fourthZz;
                                }

                                //přepsání řádku se třetí *
                                if($thirdAstX == 1){
                                    $thirdZ = substr($rectangles[$i][$j],1);
                                    $rectangles[$i][$j] = "*" . $thirdZ;
                                }elseif($thirdAstX == strlen($rectangles[$i][$j])){
                                    $thirdZ = substr($rectangles[$i][$j],0,strlen($rectangles[$i][$j])-1);
                                    $rectangles[$i][$j] = $thirdZ . "*";
                                }else{
                                    $thirdZ = substr($rectangles[$i][$j],0,$thirdAstX-1);
                                    $thirdZz = substr($rectangles[$i][$j],$thirdAstX,strlen($rectangles[$i][$j])-$thirdAstX);
                                    $rectangles[$i][$j] = $thirdZ . "*" . $thirdZz;
                                }

                                for($l=0; $l<count($rectangles[$i]); $l++){
                                    echo $rectangles[$i][$l] . "\n";
                                }

                                continue 3;
                            }else{
                                $thirdAstX = $firstAstX+1;
                                $thirdAstY = $firstAstY;
                                $fourthAstX = $secondAstX+1;
                                $fourthAstY = $secondAstY;

                                //přepsání řádku se čtvrtou *
                                if($fourthAstX == 1){
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],1);
                                    $rectangles[$i][$firstAstY-1] = "*" . $fourthZ;
                                }elseif($fourthAstX == strlen($rectangles[$i][$firstAstY-1])){
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],0,strlen($rectangles[$i][$firstAstY-1])-1);
                                    $rectangles[$i][$firstAstY-1] = $fourthZ . "*";
                                }else{
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],0,$fourthAstX-1);
                                    $fourthZz = substr($rectangles[$i][$firstAstY-1],$fourthAstX,strlen($rectangles[$i][$firstAstY-1])-$fourthAstX);
                                    $rectangles[$i][$firstAstY-1] = $fourthZ . "*" . $fourthZz;
                                }

                                //přepsání řádku se třetí *
                                if($thirdAstX == 1){
                                    $thirdZ = substr($rectangles[$i][$j],1);
                                    $rectangles[$i][$j] = "*" . $thirdZ;
                                }elseif($thirdAstX == strlen($rectangles[$i][$j])){
                                    $thirdZ = substr($rectangles[$i][$j],0,strlen($rectangles[$i][$j])-1);
                                    $rectangles[$i][$j] = $thirdZ . "*";
                                }else{
                                    $thirdZ = substr($rectangles[$i][$j],0,$thirdAstX-1);
                                    $thirdZz = substr($rectangles[$i][$j],$thirdAstX,strlen($rectangles[$i][$j])-$thirdAstX);
                                    $rectangles[$i][$j] = $thirdZ . "*" . $thirdZz;
                                }

                                for($l=0; $l<count($rectangles[$i]); $l++){
                                    echo $rectangles[$i][$l] . "\n";
                                }

                                continue 3;
                            }
                        }
                        //pokud jsou dvě * vedle sebe na řádku
                        if($firstAstY == $secondAstY){
                            if($firstAstY > 1){
                                $thirdAstX = $firstAstX;
                                $thirdAstY = $firstAstY-1;
                                $fourthAstX = $secondAstX;
                                $fourthAstY = $secondAstY-1;

                                //přepsání řádku se třetí a čtvrtou *
                                if($thirdAstX == 1){
                                    if($fourthAstX == 2){
                                        $space = substr($rectangles[$i][$firstAstY-2],$fourthAstX,strlen($rectangles[$i][$firstAstY-2])-$fourthAstX);
                                        $rectangles[$i][$firstAstY-2] = "**" . $space;
                                    }else{
                                        $thirdZ = substr($rectangles[$i][$firstAstY-2],1,$fourthAstX-$thirdAstX-1);
                                        $fourthZ = substr($rectangles[$i][$firstAstY-2],$thirdAstX,strlen($rectangles[$i][$firstAstY-2])-$fourthAstX);
                                        $rectangles[$i][$firstAstY-2] = "*" . $thirdZ . "*" . $fourthZ;
                                    }
                                }else{
                                    $thirdZ = substr($rectangles[$i][$firstAstY-2],0,$thirdAstX-1);
                                    $fourthZ = substr($rectangles[$i][$firstAstY-2],$thirdAstX,$fourthAstX-$thirdAstX-1);
                                    $space = substr($rectangles[$i][$firstAstY-2],$fourthAstX,strlen($rectangles[$i][$firstAstY-2])-$fourthAstX);
                                    $rectangles[$i][$firstAstY-2] = $thirdZ . "*" . $fourthZ . "*" . $space;
                                }

                                for($l=0; $l<count($rectangles[$i]); $l++){
                                    echo $rectangles[$i][$l] . "\n";
                                }

                                continue 3;
                            }else{
                                $thirdAstX = $firstAstX;
                                $thirdAstY = $firstAstY+1;
                                $fourthAstX = $secondAstX;
                                $fourthAstY = $secondAstY+1;

                                //přepsání řádku se třetí a čtvrtou *
                                if($thirdAstX == 1){
                                    if($fourthAstX == 2){
                                        $space = substr($rectangles[$i][$firstAstY],$fourthAstX,strlen($rectangles[$i][$firstAstY])-$fourthAstX);
                                        $rectangles[$i][$firstAstY] = "**" . $space;
                                    }else{
                                        $thirdZ = substr($rectangles[$i][$firstAstY],1,$fourthAstX-$thirdAstX-1);
                                        $fourthZ = substr($rectangles[$i][$firstAstY],$thirdAstX,strlen($rectangles[$i][$firstAstY])-$fourthAstX);
                                        $rectangles[$i][$firstAstY] = "*" . $thirdZ . "*" . $fourthZ;
                                    }
                                }else{
                                    $thirdZ = substr($rectangles[$i][$firstAstY],0,$thirdAstX-1);
                                    $fourthZ = substr($rectangles[$i][$firstAstY],$thirdAstX,$fourthAstX-$thirdAstX-1);
                                    $space = substr($rectangles[$i][$firstAstY],$fourthAstX,strlen($rectangles[$i][$firstAstY])-$fourthAstX);
                                    $rectangles[$i][$firstAstY] = $thirdZ . "*" . $fourthZ . "*" . $space;
                                }

                                for($l=0; $l<count($rectangles[$i]); $l++){
                                    echo $rectangles[$i][$l] . "\n";
                                }

                                continue 3;
                            }
                        }
                        //pokud jsou dvě * na různých pozicích
                        $thirdAstX = $firstAstX;
                        $thirdAstY = $secondAstY;
                        $fourthAstX = $secondAstX;
                        $fourthAstY = $firstAstY;

                        //přepsání řádku se čtvrtou *
                        if($fourthAstX == 1){
                            $fourthZ = substr($rectangles[$i][$firstAstY-1],1);
                            $rectangles[$i][$firstAstY-1] = "*" . $fourthZ;
                        }elseif($fourthAstX == strlen($rectangles[$i][$firstAstY-1])){
                            $fourthZ = substr($rectangles[$i][$firstAstY-1],0,strlen($rectangles[$i][$firstAstY-1])-1);
                            $rectangles[$i][$firstAstY-1] = $fourthZ . "*";
                        }else{
                            $fourthZ = substr($rectangles[$i][$firstAstY-1],0,$fourthAstX-1);
                            $fourthZz = substr($rectangles[$i][$firstAstY-1],$fourthAstX,strlen($rectangles[$i][$firstAstY-1])-$fourthAstX);
                            $rectangles[$i][$firstAstY-1] = $fourthZ . "*" . $fourthZz;
                        }

                        //přepsání řádku se třetí *
                        if($thirdAstX == 1){
                            $thirdZ = substr($rectangles[$i][$j],1);
                            $rectangles[$i][$j] = "*" . $thirdZ;
                        }elseif($thirdAstX == strlen($rectangles[$i][$j])){
                            $thirdZ = substr($rectangles[$i][$j],0,strlen($rectangles[$i][$j])-1);
                            $rectangles[$i][$j] = $thirdZ . "*";
                        }else{
                            $thirdZ = substr($rectangles[$i][$j],0,$thirdAstX-1);
                            $thirdZz = substr($rectangles[$i][$j],$thirdAstX,strlen($rectangles[$i][$j])-$thirdAstX);
                            $rectangles[$i][$j] = $thirdZ . "*" . $thirdZz;
                        }

                        for($l=0; $l<count($rectangles[$i]); $l++){
                            echo $rectangles[$i][$l] . "\n";
                        }

                        continue 3;
                    }
                    $firstAstX = $k+1;
                    $firstAstY = $j+1;
                }
            }
        }
    }
?>

<?php
    $rectangles = [];
    $cases = trim(fgets(STDIN));
    for($i=0; $i<$cases; $i++){
        $lines = [];
        $n = trim(fgets(STDIN));
        for($j=0; $j<$n; $j++){
            array_push($lines,trim(fgets(STDIN)));
        }
        array_push($rectangles,$lines);
    }

    //v poli (obrazci) hledáme řádek a v něm porovnáme každý znak s *
    for($i=0; $i<count($rectangles); $i++){
        $firstAstX = 0;
        $firstAstY = 0;
        $secondAstX = 0;
        $secondAstY = 0;
        $thirdAstX = 0;
        $thirdAstY = 0;
        $fourthAstX = 0;
        $fourthAstY = 0;
        for($j=0; $j<count($rectangles[$i]); $j++){
            for($k=0; $k<strlen($rectangles[$i][$j]); $k++){
                if(substr($rectangles[$i][$j],$k,1) == "*"){
                    //nalezení druhé * vyvolá komplet změny
                    if($firstAstX > 0){
                        $secondAstX = $k+1;
                        $secondAstY = $j+1;
                        //pokud jsou dvě * nad sebou
                        if($firstAstX == $secondAstX){
                            if($firstAstX > 1){
                                $thirdAstX = $firstAstX-1;
                                $thirdAstY = $firstAstY;
                                $fourthAstX = $secondAstX-1;
                                $fourthAstY = $secondAstY;

                                //přepsání řádku se čtvrtou *
                                if($fourthAstX == 1){
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],1);
                                    $rectangles[$i][$firstAstY-1] = "*" . $fourthZ;
                                }elseif($fourthAstX == strlen($rectangles[$i][$firstAstY-1])){
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],0,strlen($rectangles[$i][$firstAstY-1])-1);
                                    $rectangles[$i][$firstAstY-1] = $fourthZ . "*";
                                }else{
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],0,$fourthAstX-1);
                                    $fourthZz = substr($rectangles[$i][$firstAstY-1],$fourthAstX,strlen($rectangles[$i][$firstAstY-1])-$fourthAstX);
                                    $rectangles[$i][$firstAstY-1] = $fourthZ . "*" . $fourthZz;
                                }

                                //přepsání řádku se třetí *
                                if($thirdAstX == 1){
                                    $thirdZ = substr($rectangles[$i][$j],1);
                                    $rectangles[$i][$j] = "*" . $thirdZ;
                                }elseif($thirdAstX == strlen($rectangles[$i][$j])){
                                    $thirdZ = substr($rectangles[$i][$j],0,strlen($rectangles[$i][$j])-1);
                                    $rectangles[$i][$j] = $thirdZ . "*";
                                }else{
                                    $thirdZ = substr($rectangles[$i][$j],0,$thirdAstX-1);
                                    $thirdZz = substr($rectangles[$i][$j],$thirdAstX,strlen($rectangles[$i][$j])-$thirdAstX);
                                    $rectangles[$i][$j] = $thirdZ . "*" . $thirdZz;
                                }

                                for($l=0; $l<count($rectangles[$i]); $l++){
                                    echo $rectangles[$i][$l] . "\n";
                                }

                                continue 3;
                            }else{
                                $thirdAstX = $firstAstX+1;
                                $thirdAstY = $firstAstY;
                                $fourthAstX = $secondAstX+1;
                                $fourthAstY = $secondAstY;

                                //přepsání řádku se čtvrtou *
                                if($fourthAstX == 1){
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],1);
                                    $rectangles[$i][$firstAstY-1] = "*" . $fourthZ;
                                }elseif($fourthAstX == strlen($rectangles[$i][$firstAstY-1])){
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],0,strlen($rectangles[$i][$firstAstY-1])-1);
                                    $rectangles[$i][$firstAstY-1] = $fourthZ . "*";
                                }else{
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],0,$fourthAstX-1);
                                    $fourthZz = substr($rectangles[$i][$firstAstY-1],$fourthAstX,strlen($rectangles[$i][$firstAstY-1])-$fourthAstX);
                                    $rectangles[$i][$firstAstY-1] = $fourthZ . "*" . $fourthZz;
                                }

                                //přepsání řádku se třetí *
                                if($thirdAstX == 1){
                                    $thirdZ = substr($rectangles[$i][$j],1);
                                    $rectangles[$i][$j] = "*" . $thirdZ;
                                }elseif($thirdAstX == strlen($rectangles[$i][$j])){
                                    $thirdZ = substr($rectangles[$i][$j],0,strlen($rectangles[$i][$j])-1);
                                    $rectangles[$i][$j] = $thirdZ . "*";
                                }else{
                                    $thirdZ = substr($rectangles[$i][$j],0,$thirdAstX-1);
                                    $thirdZz = substr($rectangles[$i][$j],$thirdAstX,strlen($rectangles[$i][$j])-$thirdAstX);
                                    $rectangles[$i][$j] = $thirdZ . "*" . $thirdZz;
                                }

                                for($l=0; $l<count($rectangles[$i]); $l++){
                                    echo $rectangles[$i][$l] . "\n";
                                }

                                continue 3;
                            }
                        }
                        //pokud jsou dvě * vedle sebe na řádku
                        if($firstAstY == $secondAstY){
                            if($firstAstY > 1){
                                $thirdAstX = $firstAstX;
                                $thirdAstY = $firstAstY-1;
                                $fourthAstX = $secondAstX;
                                $fourthAstY = $secondAstY-1;

                                //přepsání řádku se třetí a čtvrtou *
                                if($thirdAstX == 1){
                                    if($fourthAstX == 2){
                                        $space = substr($rectangles[$i][$firstAstY-2],$fourthAstX,strlen($rectangles[$i][$firstAstY-2])-$fourthAstX);
                                        $rectangles[$i][$firstAstY-2] = "**" . $space;
                                    }else{
                                        $thirdZ = substr($rectangles[$i][$firstAstY-2],1,strlen($rectangles[$i][$firstAstY-2])-$fourthAstX);
                                        $fourthZ = substr($rectangles[$i][$firstAstY-2],$thirdAstX,strlen($rectangles[$i][$firstAstY-2])-$fourthAstX);
                                        $rectangles[$i][$firstAstY-2] = "*" . $thirdZ . "*" . $fourthZ;
                                    }
                                }else{
                                    $thirdZ = substr($rectangles[$i][$firstAstY-2],0,$thirdAstX-1);
                                    $fourthZ = substr($rectangles[$i][$firstAstY-2],$thirdAstX,$fourthAstX-$thirdAstX-1);
                                    $space = substr($rectangles[$i][$firstAstY-2],$fourthAstX,strlen($rectangles[$i][$firstAstY-2])-$fourthAstX);
                                    $rectangles[$i][$firstAstY-2] = $thirdZ . "*" . $fourthZ . "*" . $space;
                                }

                                for($l=0; $l<count($rectangles[$i]); $l++){
                                    echo $rectangles[$i][$l] . "\n";
                                }

                                continue 3;
                            }else{
                                $thirdAstX = $firstAstX;
                                $thirdAstY = $firstAstY+1;
                                $fourthAstX = $secondAstX;
                                $fourthAstY = $secondAstY+1;

                                //přepsání řádku se třetí a čtvrtou *
                                if($thirdAstX == 1){
                                    if($fourthAstX == 2){
                                        $space = substr($rectangles[$i][$firstAstY],$fourthAstX,strlen($rectangles[$i][$firstAstY])-$fourthAstX);
                                        $rectangles[$i][$firstAstY] = "**" . $space;
                                    }else{
                                        $thirdZ = substr($rectangles[$i][$firstAstY],1,strlen($rectangles[$i][$firstAstY])-$fourthAstX);
                                        $fourthZ = substr($rectangles[$i][$firstAstY],$thirdAstX,strlen($rectangles[$i][$firstAstY])-$fourthAstX);
                                        $rectangles[$i][$firstAstY] = "*" . $thirdZ . "*" . $fourthZ;
                                    }
                                }else{
                                    $thirdZ = substr($rectangles[$i][$firstAstY],0,$thirdAstX-1);
                                    $fourthZ = substr($rectangles[$i][$firstAstY],$thirdAstX,$fourthAstX-$thirdAstX-1);
                                    $space = substr($rectangles[$i][$firstAstY],$fourthAstX,strlen($rectangles[$i][$firstAstY])-$fourthAstX);
                                    $rectangles[$i][$firstAstY] = $thirdZ . "*" . $fourthZ . "*" . $space;
                                }

                                for($l=0; $l<count($rectangles[$i]); $l++){
                                    echo $rectangles[$i][$l] . "\n";
                                }

                                continue 3;
                            }
                        }
                        //pokud jsou dvě * na různých pozicích
                        $thirdAstX = $firstAstX;
                        $thirdAstY = $secondAstY;
                        $fourthAstX = $secondAstX;
                        $fourthAstY = $firstAstY;

                        //přepsání řádku se čtvrtou *
                        if($fourthAstX == 1){
                            $fourthZ = substr($rectangles[$i][$firstAstY-1],1);
                            $rectangles[$i][$firstAstY-1] = "*" . $fourthZ;
                        }elseif($fourthAstX == strlen($rectangles[$i][$firstAstY-1])){
                            $fourthZ = substr($rectangles[$i][$firstAstY-1],0,strlen($rectangles[$i][$firstAstY-1])-1);
                            $rectangles[$i][$firstAstY-1] = $fourthZ . "*";
                        }else{
                            $fourthZ = substr($rectangles[$i][$firstAstY-1],0,$fourthAstX-1);
                            $fourthZz = substr($rectangles[$i][$firstAstY-1],$fourthAstX,strlen($rectangles[$i][$firstAstY-1])-$fourthAstX);
                            $rectangles[$i][$firstAstY-1] = $fourthZ . "*" . $fourthZz;
                        }

                        //přepsání řádku se třetí *
                        if($thirdAstX == 1){
                            $thirdZ = substr($rectangles[$i][$j],1);
                            $rectangles[$i][$j] = "*" . $thirdZ;
                        }elseif($thirdAstX == strlen($rectangles[$i][$j])){
                            $thirdZ = substr($rectangles[$i][$j],0,strlen($rectangles[$i][$j])-1);
                            $rectangles[$i][$j] = $thirdZ . "*";
                        }else{
                            $thirdZ = substr($rectangles[$i][$j],0,$thirdAstX-1);
                            $thirdZz = substr($rectangles[$i][$j],$thirdAstX,strlen($rectangles[$i][$j])-$thirdAstX);
                            $rectangles[$i][$j] = $thirdZ . "*" . $thirdZz;
                        }

                        for($l=0; $l<count($rectangles[$i]); $l++){
                            echo $rectangles[$i][$l] . "\n";
                        }

                        continue 3;
                    }
                    $firstAstX = $k+1;
                    $firstAstY = $j+1;
                }
            }
        }
    }
?>

<?php
    $rectangles = [];
    $cases = trim(fgets(STDIN));
    for($i=0; $i<$cases; $i++){
        $lines = [];
        $n = trim(fgets(STDIN));
        for($j=0; $j<$n; $j++){
            array_push($lines,trim(fgets(STDIN)));
        }
        array_push($rectangles,$lines);
    }

    //v poli (obrazci) hledáme řádek a v něm porovnáme každý znak s *
    for($i=0; $i<count($rectangles); $i++){
        $firstAstX = 0;
        $firstAstY = 0;
        $secondAstX = 0;
        $secondAstY = 0;
        $thirdAstX = 0;
        $thirdAstY = 0;
        $fourthAstX = 0;
        $fourthAstY = 0;
        for($j=0; $j<count($rectangles[$i]); $j++){
            for($k=0; $k<strlen($rectangles[$i][$j]); $k++){
                if(substr($rectangles[$i][$j],$k,1) == "*"){
                    //nalezení druhé * vyvolá komplet změny
                    if($firstAstX > 0){
                        $secondAstX = $k+1;
                        $secondAstY = $j+1;
                        //pokud jsou dvě * nad sebou
                        if($firstAstX == $secondAstX){
                            if($firstAstX > 1){
                                $thirdAstX = $firstAstX-1;
                                $thirdAstY = $firstAstY;
                                $fourthAstX = $secondAstX-1;
                                $fourthAstY = $secondAstY;

                                //přepsání řádku se čtvrtou *
                                if($fourthAstX == 1){
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],1);
                                    $rectangles[$i][$firstAstY-1] = "*" . $fourthZ;
                                }elseif($fourthAstX == strlen($rectangles[$i][$firstAstY-1])){
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],0,strlen($rectangles[$i][$firstAstY-1])-1);
                                    $rectangles[$i][$firstAstY-1] = $fourthZ . "*";
                                }else{
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],0,$fourthAstX-1);
                                    $fourthZz = substr($rectangles[$i][$firstAstY-1],$fourthAstX,strlen($rectangles[$i][$firstAstY-1])-$fourthAstX);
                                    $rectangles[$i][$firstAstY-1] = $fourthZ . "*" . $fourthZz;
                                }

                                //přepsání řádku se třetí *
                                if($thirdAstX == 1){
                                    $thirdZ = substr($rectangles[$i][$j],1);
                                    $rectangles[$i][$j] = "*" . $thirdZ;
                                }elseif($thirdAstX == strlen($rectangles[$i][$j])){
                                    $thirdZ = substr($rectangles[$i][$j],0,strlen($rectangles[$i][$j])-1);
                                    $rectangles[$i][$j] = $thirdZ . "*";
                                }else{
                                    $thirdZ = substr($rectangles[$i][$j],0,$thirdAstX-1);
                                    $thirdZz = substr($rectangles[$i][$j],$thirdAstX,strlen($rectangles[$i][$j])-$thirdAstX);
                                    $rectangles[$i][$j] = $thirdZ . "*" . $thirdZz;
                                }

                                for($l=0; $l<count($rectangles[$i]); $l++){
                                    echo $rectangles[$i][$l] . "\n";
                                }

                                continue 3;
                            }else{
                                $thirdAstX = $firstAstX+1;
                                $thirdAstY = $firstAstY;
                                $fourthAstX = $secondAstX+1;
                                $fourthAstY = $secondAstY;

                                //přepsání řádku se čtvrtou *
                                if($fourthAstX == 1){
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],1);
                                    $rectangles[$i][$firstAstY-1] = "*" . $fourthZ;
                                }elseif($fourthAstX == strlen($rectangles[$i][$firstAstY-1])){
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],0,strlen($rectangles[$i][$firstAstY-1])-1);
                                    $rectangles[$i][$firstAstY-1] = $fourthZ . "*";
                                }else{
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],0,$fourthAstX-1);
                                    $fourthZz = substr($rectangles[$i][$firstAstY-1],$fourthAstX,strlen($rectangles[$i][$firstAstY-1])-$fourthAstX);
                                    $rectangles[$i][$firstAstY-1] = $fourthZ . "*" . $fourthZz;
                                }

                                //přepsání řádku se třetí *
                                if($thirdAstX == 1){
                                    $thirdZ = substr($rectangles[$i][$j],1);
                                    $rectangles[$i][$j] = "*" . $thirdZ;
                                }elseif($thirdAstX == strlen($rectangles[$i][$j])){
                                    $thirdZ = substr($rectangles[$i][$j],0,strlen($rectangles[$i][$j])-1);
                                    $rectangles[$i][$j] = $thirdZ . "*";
                                }else{
                                    $thirdZ = substr($rectangles[$i][$j],0,$thirdAstX-1);
                                    $thirdZz = substr($rectangles[$i][$j],$thirdAstX,strlen($rectangles[$i][$j])-$thirdAstX);
                                    $rectangles[$i][$j] = $thirdZ . "*" . $thirdZz;
                                }

                                for($l=0; $l<count($rectangles[$i]); $l++){
                                    echo $rectangles[$i][$l] . "\n";
                                }

                                continue 3;
                            }
                        }
                        //pokud jsou dvě * vedle sebe na řádku
                        if($firstAstY == $secondAstY){
                            if($firstAstY > 1){
                                $thirdAstX = $firstAstX;
                                $thirdAstY = $firstAstY-1;
                                $fourthAstX = $secondAstX;
                                $fourthAstY = $secondAstY-1;

                                //přepsání řádku se třetí a čtvrtou *
                                if($thirdAstX == 1){
                                    if($fourthAstX == 2){
                                        $space = substr($rectangles[$i][$firstAstY-2],$fourthAstX,strlen($rectangles[$i][$firstAstY-2])-$fourthAstX);
                                        $rectangles[$i][$firstAstY-2] = "**" . $space;
                                    }else{
                                        $thirdZ = substr($rectangles[$i][$firstAstY-2],1,strlen($rectangles[$i][$firstAstY-2])-$fourthAstX-1);
                                        $fourthZ = substr($rectangles[$i][$firstAstY-2],$fourthAstX,strlen($rectangles[$i][$firstAstY-2])-$fourthAstX);
                                        $rectangles[$i][$firstAstY-2] = "*" . $thirdZ . "*" . $fourthZ;
                                    }
                                }else{
                                    $thirdZ = substr($rectangles[$i][$firstAstY-2],0,$thirdAstX-1);
                                    $fourthZ = substr($rectangles[$i][$firstAstY-2],$thirdAstX,$fourthAstX-$thirdAstX-1);
                                    $space = substr($rectangles[$i][$firstAstY-2],$fourthAstX,strlen($rectangles[$i][$firstAstY-2])-$fourthAstX);
                                    $rectangles[$i][$firstAstY-2] = $thirdZ . "*" . $fourthZ . "*" . $space;
                                }

                                for($l=0; $l<count($rectangles[$i]); $l++){
                                    echo $rectangles[$i][$l] . "\n";
                                }

                                continue 3;
                            }else{
                                $thirdAstX = $firstAstX;
                                $thirdAstY = $firstAstY+1;
                                $fourthAstX = $secondAstX;
                                $fourthAstY = $secondAstY+1;

                                //přepsání řádku se třetí a čtvrtou *
                                if($thirdAstX == 1){
                                    if($fourthAstX == 2){
                                        $space = substr($rectangles[$i][$firstAstY],$fourthAstX,strlen($rectangles[$i][$firstAstY])-$fourthAstX);
                                        $rectangles[$i][$firstAstY] = "**" . $space;
                                    }else{
                                        $thirdZ = substr($rectangles[$i][$firstAstY],1,strlen($rectangles[$i][$firstAstY])-$fourthAstX-1);
                                        $fourthZ = substr($rectangles[$i][$firstAstY],$fourthAstX,strlen($rectangles[$i][$firstAstY])-$fourthAstX);
                                        $rectangles[$i][$firstAstY] = "*" . $thirdZ . "*" . $fourthZ;
                                    }
                                }else{
                                    $thirdZ = substr($rectangles[$i][$firstAstY],0,$thirdAstX-1);
                                    $fourthZ = substr($rectangles[$i][$firstAstY],$thirdAstX,$fourthAstX-$thirdAstX-1);
                                    $space = substr($rectangles[$i][$firstAstY],$fourthAstX,strlen($rectangles[$i][$firstAstY])-$fourthAstX);
                                    $rectangles[$i][$firstAstY] = $thirdZ . "*" . $fourthZ . "*" . $space;
                                }

                                for($l=0; $l<count($rectangles[$i]); $l++){
                                    echo $rectangles[$i][$l] . "\n";
                                }

                                continue 3;
                            }
                        }
                        //pokud jsou dvě * na různých pozicích
                        $thirdAstX = $firstAstX;
                        $thirdAstY = $secondAstY;
                        $fourthAstX = $secondAstX;
                        $fourthAstY = $firstAstY;

                        //přepsání řádku se čtvrtou *
                        if($fourthAstX == 1){
                            $fourthZ = substr($rectangles[$i][$firstAstY-1],1);
                            $rectangles[$i][$firstAstY-1] = "*" . $fourthZ;
                        }elseif($fourthAstX == strlen($rectangles[$i][$firstAstY-1])){
                            $fourthZ = substr($rectangles[$i][$firstAstY-1],0,strlen($rectangles[$i][$firstAstY-1])-1);
                            $rectangles[$i][$firstAstY-1] = $fourthZ . "*";
                        }else{
                            $fourthZ = substr($rectangles[$i][$firstAstY-1],0,$fourthAstX-1);
                            $fourthZz = substr($rectangles[$i][$firstAstY-1],$fourthAstX,strlen($rectangles[$i][$firstAstY-1])-$fourthAstX);
                            $rectangles[$i][$firstAstY-1] = $fourthZ . "*" . $fourthZz;
                        }

                        //přepsání řádku se třetí *
                        if($thirdAstX == 1){
                            $thirdZ = substr($rectangles[$i][$j],1);
                            $rectangles[$i][$j] = "*" . $thirdZ;
                        }elseif($thirdAstX == strlen($rectangles[$i][$j])){
                            $thirdZ = substr($rectangles[$i][$j],0,strlen($rectangles[$i][$j])-1);
                            $rectangles[$i][$j] = $thirdZ . "*";
                        }else{
                            $thirdZ = substr($rectangles[$i][$j],0,$thirdAstX-1);
                            $thirdZz = substr($rectangles[$i][$j],$thirdAstX,strlen($rectangles[$i][$j])-$thirdAstX);
                            $rectangles[$i][$j] = $thirdZ . "*" . $thirdZz;
                        }

                        for($l=0; $l<count($rectangles[$i]); $l++){
                            echo $rectangles[$i][$l] . "\n";
                        }

                        continue 3;
                    }
                    $firstAstX = $k+1;
                    $firstAstY = $j+1;
                }
            }
        }
    }
?>

<?php
    $rectangles = [];
    $cases = trim(fgets(STDIN));
    for($i=0; $i<$cases; $i++){
        $lines = [];
        $n = trim(fgets(STDIN));
        for($j=0; $j<$n; $j++){
            array_push($lines,trim(fgets(STDIN)));
        }
        array_push($rectangles,$lines);
    }

    //v poli (obrazci) hledáme řádek a v něm porovnáme každý znak s *
    for($i=0; $i<count($rectangles); $i++){
        $firstAstX = 0;
        $firstAstY = 0;
        $secondAstX = 0;
        $secondAstY = 0;
        $thirdAstX = 0;
        $thirdAstY = 0;
        $fourthAstX = 0;
        $fourthAstY = 0;
        for($j=0; $j<count($rectangles[$i]); $j++){
            for($k=0; $k<strlen($rectangles[$i][$j]); $k++){
                if(substr($rectangles[$i][$j],$k,1) == "*"){
                    //nalezení druhé * vyvolá komplet změny
                    if($firstAstX > 0){
                        $secondAstX = $k+1;
                        $secondAstY = $j+1;
                        //pokud jsou dvě * nad sebou
                        if($firstAstX == $secondAstX){
                            if($firstAstX > 1){
                                $thirdAstX = $firstAstX-1;
                                $thirdAstY = $firstAstY;
                                $fourthAstX = $secondAstX-1;
                                $fourthAstY = $secondAstY;

                                //přepsání řádku se čtvrtou *
                                if($fourthAstX == 1){
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],1);
                                    $rectangles[$i][$firstAstY-1] = "*" . $fourthZ;
                                }elseif($fourthAstX == strlen($rectangles[$i][$firstAstY-1])){
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],0,strlen($rectangles[$i][$firstAstY-1])-1);
                                    $rectangles[$i][$firstAstY-1] = $fourthZ . "*";
                                }else{
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],0,$fourthAstX-1);
                                    $fourthZz = substr($rectangles[$i][$firstAstY-1],$fourthAstX,strlen($rectangles[$i][$firstAstY-1])-$fourthAstX);
                                    $rectangles[$i][$firstAstY-1] = $fourthZ . "*" . $fourthZz;
                                }

                                //přepsání řádku se třetí *
                                if($thirdAstX == 1){
                                    $thirdZ = substr($rectangles[$i][$j],1);
                                    $rectangles[$i][$j] = "*" . $thirdZ;
                                }elseif($thirdAstX == strlen($rectangles[$i][$j])){
                                    $thirdZ = substr($rectangles[$i][$j],0,strlen($rectangles[$i][$j])-1);
                                    $rectangles[$i][$j] = $thirdZ . "*";
                                }else{
                                    $thirdZ = substr($rectangles[$i][$j],0,$thirdAstX-1);
                                    $thirdZz = substr($rectangles[$i][$j],$thirdAstX,strlen($rectangles[$i][$j])-$thirdAstX);
                                    $rectangles[$i][$j] = $thirdZ . "*" . $thirdZz;
                                }

                                for($l=0; $l<count($rectangles[$i]); $l++){
                                    echo $rectangles[$i][$l] . "\n";
                                }

                                continue 3;
                            }else{
                                $thirdAstX = $firstAstX+1;
                                $thirdAstY = $firstAstY;
                                $fourthAstX = $secondAstX+1;
                                $fourthAstY = $secondAstY;

                                //přepsání řádku se čtvrtou *
                                if($fourthAstX == 1){
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],1);
                                    $rectangles[$i][$firstAstY-1] = "*" . $fourthZ;
                                }elseif($fourthAstX == strlen($rectangles[$i][$firstAstY-1])){
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],0,strlen($rectangles[$i][$firstAstY-1])-1);
                                    $rectangles[$i][$firstAstY-1] = $fourthZ . "*";
                                }else{
                                    $fourthZ = substr($rectangles[$i][$firstAstY-1],0,$fourthAstX-1);
                                    $fourthZz = substr($rectangles[$i][$firstAstY-1],$fourthAstX,strlen($rectangles[$i][$firstAstY-1])-$fourthAstX);
                                    $rectangles[$i][$firstAstY-1] = $fourthZ . "*" . $fourthZz;
                                }

                                //přepsání řádku se třetí *
                                if($thirdAstX == 1){
                                    $thirdZ = substr($rectangles[$i][$j],1);
                                    $rectangles[$i][$j] = "*" . $thirdZ;
                                }elseif($thirdAstX == strlen($rectangles[$i][$j])){
                                    $thirdZ = substr($rectangles[$i][$j],0,strlen($rectangles[$i][$j])-1);
                                    $rectangles[$i][$j] = $thirdZ . "*";
                                }else{
                                    $thirdZ = substr($rectangles[$i][$j],0,$thirdAstX-1);
                                    $thirdZz = substr($rectangles[$i][$j],$thirdAstX,strlen($rectangles[$i][$j])-$thirdAstX);
                                    $rectangles[$i][$j] = $thirdZ . "*" . $thirdZz;
                                }

                                for($l=0; $l<count($rectangles[$i]); $l++){
                                    echo $rectangles[$i][$l] . "\n";
                                }

                                continue 3;
                            }
                        }
                        //pokud jsou dvě * vedle sebe na řádku
                        if($firstAstY == $secondAstY){
                            if($firstAstY > 1){
                                $thirdAstX = $firstAstX;
                                $thirdAstY = $firstAstY-1;
                                $fourthAstX = $secondAstX;
                                $fourthAstY = $secondAstY-1;

                                //přepsání řádku se třetí a čtvrtou *
                                if($thirdAstX == 1){
                                    $thirdZ = substr($rectangles[$i][$firstAstY-2],1,strlen($rectangles[$i][$firstAstY-2])-$fourthAstX-1);
                                    $fourthZ = substr($rectangles[$i][$firstAstY-2],$fourthAstX,strlen($rectangles[$i][$firstAstY-2])-$fourthAstX);
                                    $rectangles[$i][$firstAstY-2] = "*" . $thirdZ . "*" . $fourthZ;
                                }else{
                                    $thirdZ = substr($rectangles[$i][$firstAstY-2],0,$thirdAstX-1);
                                    $fourthZ = substr($rectangles[$i][$firstAstY-2],$thirdAstX,$fourthAstX-$thirdAstX-1);
                                    $space = substr($rectangles[$i][$firstAstY-2],$fourthAstX,strlen($rectangles[$i][$firstAstY-2])-$fourthAstX);
                                    $rectangles[$i][$firstAstY-2] = $thirdZ . "*" . $fourthZ . "*" . $space;
                                }

                                for($l=0; $l<count($rectangles[$i]); $l++){
                                    echo $rectangles[$i][$l] . "\n";
                                }

                                continue 3;
                            }else{
                                $thirdAstX = $firstAstX;
                                $thirdAstY = $firstAstY+1;
                                $fourthAstX = $secondAstX;
                                $fourthAstY = $secondAstY+1;

                                //přepsání řádku se třetí a čtvrtou *
                                if($thirdAstX == 1){
                                    $thirdZ = substr($rectangles[$i][$firstAstY],1,strlen($rectangles[$i][$firstAstY])-$fourthAstX-1);
                                    $fourthZ = substr($rectangles[$i][$firstAstY],$fourthAstX,strlen($rectangles[$i][$firstAstY])-$fourthAstX);
                                    $rectangles[$i][$firstAstY] = "*" . $thirdZ . "*" . $fourthZ;
                                }else{
                                    $thirdZ = substr($rectangles[$i][$firstAstY],0,$thirdAstX-1);
                                    $fourthZ = substr($rectangles[$i][$firstAstY],$thirdAstX,$fourthAstX-$thirdAstX-1);
                                    $space = substr($rectangles[$i][$firstAstY],$fourthAstX,strlen($rectangles[$i][$firstAstY])-$fourthAstX);
                                    $rectangles[$i][$firstAstY] = $thirdZ . "*" . $fourthZ . "*" . $space;
                                }

                                for($l=0; $l<count($rectangles[$i]); $l++){
                                    echo $rectangles[$i][$l] . "\n";
                                }

                                continue 3;
                            }
                        }
                        //pokud jsou dvě * na různých pozicích
                        $thirdAstX = $firstAstX;
                        $thirdAstY = $secondAstY;
                        $fourthAstX = $secondAstX;
                        $fourthAstY = $firstAstY;

                        //přepsání řádku se čtvrtou *
                        if($fourthAstX == 1){
                            $fourthZ = substr($rectangles[$i][$firstAstY-1],1);
                            $rectangles[$i][$firstAstY-1] = "*" . $fourthZ;
                        }elseif($fourthAstX == strlen($rectangles[$i][$firstAstY-1])){
                            $fourthZ = substr($rectangles[$i][$firstAstY-1],0,strlen($rectangles[$i][$firstAstY-1])-1);
                            $rectangles[$i][$firstAstY-1] = $fourthZ . "*";
                        }else{
                            $fourthZ = substr($rectangles[$i][$firstAstY-1],0,$fourthAstX-1);
                            $fourthZz = substr($rectangles[$i][$firstAstY-1],$fourthAstX,strlen($rectangles[$i][$firstAstY-1])-$fourthAstX);
                            $rectangles[$i][$firstAstY-1] = $fourthZ . "*" . $fourthZz;
                        }

                        //přepsání řádku se třetí *
                        if($thirdAstX == 1){
                            $thirdZ = substr($rectangles[$i][$j],1);
                            $rectangles[$i][$j] = "*" . $thirdZ;
                        }elseif($thirdAstX == strlen($rectangles[$i][$j])){
                            $thirdZ = substr($rectangles[$i][$j],0,strlen($rectangles[$i][$j])-1);
                            $rectangles[$i][$j] = $thirdZ . "*";
                        }else{
                            $thirdZ = substr($rectangles[$i][$j],0,$thirdAstX-1);
                            $thirdZz = substr($rectangles[$i][$j],$thirdAstX,strlen($rectangles[$i][$j])-$thirdAstX);
                            $rectangles[$i][$j] = $thirdZ . "*" . $thirdZz;
                        }

                        for($l=0; $l<count($rectangles[$i]); $l++){
                            echo $rectangles[$i][$l] . "\n";
                        }

                        continue 3;
                    }
                    $firstAstX = $k+1;
                    $firstAstY = $j+1;
                }
            }
        }
    }
?>

<?php

function print_rect($matrix, $lines, $positions)
{
    for ($k=1; $k <= $matrix; $k++) {
        for ($l=1; $l <= $matrix; $l++) {
            if ($k == $lines[0] || $k == $lines[1]) {
                if ($l == $positions[0] || $l == $positions[1]) {
                    echo "*";
                } else {
                    echo ".";
                }

            }else {
                echo ".";
            }
        }
        echo "\n";
    }
}

function position_maker($lines, $positions)
{
    if ($positions[0] == $positions[1]) {
        if ($positions[0] == $lines) {
            $positions[0]-= 1;
        }else {
            $positions[1]+= 1;
        }
    }

    return $positions;
}

function line_maker($lines, $star_line)
{
    if ($star_line[0] == $star_line[1]) {
        if ($star_line[1] == $lines) {
            $star_line[1]-= 1;
        }else {
            $star_line[1]+= 1;
        }
    }else {

    }

    return $star_line;
}

// --------------
fscanf(STDIN, "%d", $looper);

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

    $matrix = readline();

    $star_line = array();
    $position = array();

    for ($i=0; $i < $matrix; $i++) {
        $s_line = readline();

        for ($j=0; $j < $matrix; $j++) {
            if ($s_line[$j] == "*") {
                array_push($star_line, $i+1);
                array_push($position, $j+1);
            }
        }
    }

    $star_line = line_maker($matrix, $star_line);
    $position = position_maker($matrix, $position);

    // (4, [1,3], [3,1]);
    print_rect($matrix, $star_line, $position);
}

<?php

function print_rect($matrix, $lines, $positions)
{
    for ($i=1; $i <= $matrix; $i++) {
        for ($j=1; $j <= $matrix; $j++) {
            if ($i == $lines[0] || $i == $lines[1]) {
                if ($j == $positions[0] || $j == $positions[1]) {
                    echo "*";
                } else {
                    echo ".";
                }

            }else {
                echo ".";
            }
        }
        echo "\n";
    }
}

function position_maker($lines, $positions)
{
    if ($positions[0] == $positions[1]) {
        if ($positions[0] == $lines) {
            $positions[0]-= 1;
        }else {
            $positions[1]+= 1;
        }
    }

    return $positions;
}

function line_maker($lines, $star_line)
{
    if ($star_line[0] == $star_line[1]) {
        if ($star_line[1] == $lines) {
            $star_line[1]-= 1;
        }else {
            $star_line[1]+= 1;
        }
    }else {

    }

    return $star_line;
}

fscanf(STDIN, "%d", $looper);

while ($looper--) {

    fscanf(STDIN, "%d", $matrix);

    $star_line = [];
    $position = [];

    for ($i=0; $i < $matrix; $i++) {
        $s_line = readline();

        for ($j=0; $j < 4; $j++) {
            if ($s_line[$j] == "*") {
                array_push($star_line, $i+1);
                array_push($position, $j+1);
            }
        }
    }

    $star_line = line_maker($matrix, $star_line);
    $position = position_maker($matrix, $position);

    // (4, [1,3], [3,1]);
    print_rect($matrix, $star_line, $position);
}


<?php
ini_set("memory_limit", "256M");
ini_set("max_execution_time", 3);

$fd = fopen("php://stdin", 'r');
$count = fgets($fd);

function compare($a, $b) {
    $result = 0;
    for ($i = 0; $i < 6; $i++) {
        if ($a[$i] == $b[$i]) {
            $result++;
        }
    }
    return $result;
}

$similar = 0;
$numbers = [];

while (!feof($fd)) {
    $num = substr(fgets($fd), 0, 6);
    foreach ($numbers as $stored) {
        $cmp = compare($num, $stored);
        if ($cmp > $similar) {
            $similar = $cmp;
        }
    }
    $numbers[] = $num;
}
if ($count == 1) {
    echo 6;
} else {
    echo max(floor((5 - $similar) / 2), 0);
}

<?

$n = (int) fgets(STDIN);

$length = 6;
$codes = [];

$k = $length;
for ($i = 0; $i < $n; $i++) {
    $code = trim(fgets(STDIN));

    $min_differences = $length;
    foreach ($codes as $item) {
        $differences = 0;
        for ($j = 0; $j < $length; $j++) {
            if ($item{$j} !== $code{$j}) {
                $differences++;
            }
        }

        if ($min_differences > $differences) {
            $min_differences = $differences;
        }
    }

    if ($k > $min_differences) {
        $k = $min_differences;
    }

    $codes[] = $code;
}

echo ($n == 1 ? $length : floor(($k - 1) / 2));

<?php
ini_set("memory_limit", "256M");
ini_set("max_execution_time", 3);

$fd = fopen("php://stdin", 'r');
fgets($fd);

function compare($a, $b) {
    $result = 0;
    for ($i = 0; $i < 6; $i++) {
        if ($a[$i] == $b[$i]) {
            $result++;
        }
    }
    return $result;
}

$similar = 0;
$numbers = [];

while (!feof($fd)) {
    $num = substr(fgets($fd), 0, 6);
    foreach ($numbers as $stored) {
        $cmp = compare($num, $stored);
        if ($cmp > $similar) {
            $similar = $cmp;
        }
    }
    $numbers[] = $num;
}

echo max(floor((5 - $similar) / 2), 0);

<?php
ini_set("memory_limit", "256M");
ini_set("max_execution_time", 3);

$fd = fopen("php://stdin", 'r');
fgets($fd);

function compare($a, $b) {
    $result = 0;
    for ($i = 0; $i < 6; $i++) {
        if ($a[$i] == $b[$i]) {
            $result++;
        }
    }
    return $result;
}

$similar = 0;
$numbers = [];

while (!feof($fd)) {
    $num = substr(fgets($fd), 0, 6);
    foreach ($numbers as $stored) {
        $cmp = compare($num, $stored);
        if ($cmp > $similar) {
            $similar = $cmp;
        }
    }
    $numbers[] = $num;
}
if (count($numbers) == 1) {
    echo 6;
} else {
    echo max(floor((5 - $similar) / 2), 0);
}

<?php
ini_set("memory_limit", "256M");
ini_set("max_execution_time", 3);

$fd = fopen("php://stdin", 'r');
fgets($fd);

function compare($a, $b) {
    $result = 0;
    for ($i = 0; $i < 6; $i++) {
        if ($a[$i] == $b[$i]) {
            $result++;
        }
    }
    return $result;
}

$similar = 0;
$numbers = [];

while (!feof($fd)) {
    $num = substr(fgets($fd), 0, 6);
    foreach ($numbers as $stored) {
        $cmp = compare($num, $stored);
        if ($cmp > $similar) {
            $similar = $cmp;
        }
    }
    $numbers[] = $num;
}

echo floor((6 - $similar) / 2);

<?

$n = (int) fgets(STDIN);

$length = 6;
$codes = [];

$k = $length;
for ($i = 0; $i < $n; $i++) {
    $code = trim(fgets(STDIN));

    $min_differences = $length;
    foreach ($codes as $item) {
        $differences = 0;
        for ($j = 0; $j < $length; $j++) {
            if ($item{$j} !== $code{$j}) {
                $differences++;
            }
        }

        if ($min_differences > $differences) {
            $min_differences = $differences;
        }
    }

    if ($k > $min_differences) {
        $k = $min_differences;
    }

    $codes[] = $code;
}

echo ($k ? floor(($k - 1) / 2) : 0);


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

<?php
$tests = readline();
$answers = [];
for ($t = 1; $t <= $tests; $t++) {

	# read input and convert sequences into arrays
	$n = readline();
	if($n%2) $n--;
	$answers[]= $n - (floor($n/2) - 1);
}

foreach($answers as $a) echo $a.PHP_EOL;


<?php

fscanf(STDIN, "%s\n", $number);
bcscale(0);

$number = bcsub($number, "2");

if ("0" === bcmod($number, "3")) {
    $second = 2;
    $third = bcsub($number, "1");
} else {
    $second = 1;
    $third = $number;
}

echo sprintf('1 %s %s', $second, $third);


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

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

$id = 0;

$users = array();
$newuser = array();
$olduser = array();

for ($i = 0; $i < $n; ++$i) {
	$line = explode(' ', trim(fgets($fileIN)));
	if (!isset($users[$line[0]])) {
		$users[$line[0]] = $id;
		$olduser[$id] = $line[0];
		$id++;
	}
	$tempid = $users[$line[0]];
	unset($users[$line[0]]);
	$users[$line[1]] = $tempid;
	$newuser[$tempid] = $line[1];
}

fprintf($fileOUT, $id . "\n");
for ($i = 0; $i < $id; ++$i) {
	fprintf($fileOUT, $olduser[$i] . " " . $newuser[$i] . "\n");
}

fclose($fileIN);
fclose($fileOUT);
?>

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

<?php
function getInputString(){
    return trim(fgets(STDIN));
}

function writeOut($out){
    $out = $out."\n";
    fwrite(STDOUT,$out);
}

$reqCount = getInputString();
$reqs = array();
$result = array();
for($i = 0; $i<$reqCount;$i++){
    $reqs[] =getInputString();
}
foreach($reqs as $req){
    $req = explode(' ',$req);
    $old = array_search($req[0],$result,true);
    if(false===$old){
        $result[$req[0]] = $req[1];
    }
    else{
        $result[$old] = $req[1];
    }
}
$counResult = count($result);
writeOut($counResult);
foreach($result as $old=>$new){
    $out = $old.' '.$new;
    writeOut($out);
}
?>

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

<?php
$a = trim(fgets(STDIN));
$b = array();
$c = array();
for($x = 1; $x <= $a; $x++)
{
    list($d, $e) = explode(" ", trim(fgets(STDIN)));
    array_push($b, $d);
    array_push($c, $e);
}
for($x = 0; $x < $a; $x++)
{
    $f = array_search($c[$x], $b);
    if($f == TRUE)
    {
        $b[$f] = $b[$x];
        unset($b[$x]);
        unset($c[$x]);
    }
}
$g = range(0, count($b) - 1);
$h = array_combine($g, $b);
$i = array_combine($g, $c);
print count($b) . "\n";
for($x = 0; $x < count($g) - 1; $x++)
{
    print $h[$x] . " " . $i[$x] . "\n";
}
print $h[$x] . " " . $i[$x];
?>

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

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

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

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

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

<?php
function getInputString(){
    return trim(fgets(STDIN));
}

function writeOut($out){
    $out = $out."\n";
    fwrite(STDOUT,$out);
}

$reqCount = getInputString();
$reqs = array();
$result = array();
for($i = 0; $i<$reqCount;$i++){
    $reqs[] =getInputString();
}
foreach($reqs as $req){
    $req = explode(' ',$req);
    $req[0] = strval($req[0]);
    $req[1] = strval($req[1]);
    $old = array_search($req[0],$result);
    if(false===$old){
        $result[$req[0]] = $req[1];
    }
    else{
        $result[$old] = $req[1];
    }
}
$counResult = count($result);
writeOut($counResult);
foreach($result as $old=>$new){
    $out = $old.' '.$new;
    writeOut($out);
}
?>

<?php
function getInputString(){
    return trim(fgets(STDIN));
}

function writeOut($out){
    $out = $out."\n";
    fwrite(STDOUT,$out);
}

$reqCount = getInputString();
$reqs = array();
$result = array();
for($i = 0; $i<$reqCount;$i++){
    $reqs[] =getInputString();
}
foreach($reqs as $req){
    $req = explode(' ',$req);
    $req[0] = strval($req[0]);
    $req[1] = strval($req[1]);
    $old = array_search($req[0],$result);
    if(!$old){
        $result[$req[0]] = $req[1];
    }
    else{
        $result[$old] = $req[1];
    }
}
$counResult = count($result);
writeOut($counResult);
foreach($result as $old=>$new){
    $out = $old.' '.$new;
    writeOut($out);
}
?>

<?php
function getInputString(){
    return trim(fgets(STDIN));
}

function writeOut($out){
    $out = $out."\n";
    fwrite(STDOUT,$out);
}

$reqCount = getInputString();
$reqs = array();
$result = array();
for($i = 0; $i<$reqCount;$i++){
    $reqs[] =getInputString();
}
foreach($reqs as $req){
    $req = explode(' ',$req);
    $req[0] = strval($req[0]);
    $req[1] = strval($req[1]);
    $old = array_search($req[0],$result);
    if(false!==$old){
        $result[$req[0]] = $req[1];
    }
    else{
        $result[$old] = $req[1];
    }
}
$counResult = count($result);
writeOut($counResult);
foreach($result as $old=>$new){
    $out = $old.' '.$new;
    writeOut($out);
}
?>

<?php
function getInputString(){
    return trim(fgets(STDIN));
}

function writeOut($out){
    $out = $out."\n";
    fwrite(STDOUT,$out);
}

$reqCount = getInputString();
$reqs = array();
$result = array();
for($i = 0; $i<$reqCount;$i++){
    $reqs[] =getInputString();
}
foreach($reqs as $req){
    $req = explode(' ',$req);
    $old = array_search($req[0],$result);
    if(false===$old){
        $result[$req[0]] = $req[1];
    }
    else{
        $result[$old] = $req[1];
    }
}
$counResult = count($result);
writeOut($counResult);
foreach($result as $old=>$new){
    $out = $old.' '.$new;
    writeOut($out);
}
?>

<?php
function getInputString(){
    return trim(fgets(STDIN));
}

function writeOut($out){
    $out = $out."\n";
    fwrite(STDOUT,$out);
}

$reqCount = getInputString();
$reqs = array();
$result = array();
for($i = 0; $i<$reqCount;$i++){
    $reqs[] =getInputString();
}
foreach($reqs as $req){
    $req = explode(' ',$req);
    $old = array_search($req[0],$result);
    if(!$old){
        $result[$req[0]] = $req[1];
    }
    else{
        $result[$old] = $req[1];
    }
}
$counResult = count($result);
writeOut($counResult);
foreach($result as $old=>$new){
    $out = $old.' '.$new;
    writeOut($out);
}
?>

<?php
$fileIN = fopen("in.txt", "r");
$fileOUT = fopen("out.txt", "w");

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

$id = 0;

$users = array();
$newuser = array();
$olduser = array();

for ($i = 0; $i < $n; ++$i) {
	$line = explode(' ', trim(fgets($fileIN)));
	if (!isset($users[$line[0]])) {
		$users[$line[0]] = $id;
		$olduser[$id] = $line[0];
		$id++;
	}
	$tempid = $users[$line[0]];
	unset($users[$line[0]]);
	$users[$line[1]] = $tempid;
	$newuser[$tempid] = $line[1];
}

fprintf($fileOUT, $id . '\n');
for ($i = 0; $i < $id; ++$i) {
	fprintf($fileOUT, $olduser[$i] . ' ' . $newuser[$i] . '\n');
}

fclose($fileIN);
fclose($fileOUT);
?>

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

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

$id = 0;

$users = array();
$newuser = array();
$olduser = array();

for ($i = 0; $i < $n; ++$i) {
	$line = explode(' ', trim(fgets($fileIN)));
	if (!isset($users[$line[0]])) {
		$users[$line[0]] = $id;
		$olduser[$id] = $line[0];
		$id++;
	}
	$tempid = $users[$line[0]];
	unset($users[$line[0]]);
	$users[$line[1]] = $tempid;
	$newuser[$tempid] = $line[1];
}

fprintf($fileOUT, $id . '\n');
for ($i = 0; $i < $id; ++$i) {
	fprintf($fileOUT, $olduser[$i] . ' ' . $newuser[$i] . '\n');
}

fclose($fileIN);
fclose($fileOUT);
?>


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

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

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

$t = IO::str();

function iter($arr, $ar) {
    $tmp = array();
    foreach ($arr as $i => $a) {
        $tmp[$i] = $ar[$a];
    }
    return $tmp;
}

function add($arr) {
    $ar = array();
    foreach ($arr as $a) {
        if (!isset($ar[$a])) {
            $ar[$a] = 1;
        } else {
            $ar[$a] += 1;
        }
    }
    return $ar;
}

while ($t--) {
    $n = IO::str();
    $arr = IO::arr();
    $ar = add($arr);
//    $arr = iter($arr, $ar);
//    $ar = add($arr);
//    $arr = iter($arr, $ar);
//    $ar = add($arr);
//    $arr = iter($arr, $ar);
//
//    var_dump($arr);
//    var_dump($ar);

    $tmp = array();
    $q = array();

    while ($arr != $tmp) {
        if (count($q) > 1 && $q[count($q)-1] == $arr) {
            break;
        }

        $q[] = $arr;

        $arr = iter($arr, $ar);
        $ar = add($arr);
    }

    $qs = IO::str();
//    foreach ($q as $aq) {
//        foreach ($aq as $qz) {
//            echo $qz . ' ';
//        }
//        echo PHP_EOL;
//    }
//    print_r($q[0][1]);
    while ($qs--) {
        list($a, $b) = IO::arr();
        if ($b >= count($q)) {
            $b = count($q) - 1;
        }
        echo $q[$b][$a-1] . PHP_EOL;
    }
}

<?php
class IO {
    public static $aAlphabet = ['A','B','C','D', 'E', 'F', 'G', 'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
    //Input/Output
    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 asLine($mArr, $sep = ' ') { echo implode($sep, $mArr) . PHP_EOL; }

    //Z-function
    public static function z($sString) {
        $s = str_split($sString);
        $n = count($s);
        $z = array_fill(0, $n, 0);
        $r = $l = 0;
        for ($i = 1; $i < $n; $i++) {
            if ($r >= $i) {
                $z[$i] = min($z[$i - $l], $r - $i + 1);
            }
            while ($z[$i] + $i < $n && $s[$z[$i]] == $s[$z[$i] + $i]) {
                $z[$i]++;
            }
            if ($i + $z[$i] - 1 > $r) {
                $l = $i;
                $r = $i + $z[$i] - 1;
            }
        }
        return $z;
    }
    public static function getCountOfDiffSubstrWithZ($sString)
    {
        $s = str_split($sString);
        $n = count($s);
        $count = 0;
        $t = '';
        for ($i = $n - 1; $i >= 0; $i--) {
            $t = $s[$i] . $t;
            $count += $i + 1 - max(self::z($t));
        }
        return $count;
    }
    public static function getMinPeriodOfSubstrWithZ($sString)
    {
        $s = str_split($sString);
        $res = count($s);
        $z = self::z($sString);
        foreach ($z as $iI => $sV) {
            if ((int) $iI + $z[$iI] == $res && $res % (int) $iI == 0) {
                $res = $iI;
                break;
            }
        }
        return $res;
    }

    //Other functions
    public static function gcd($a, $b) {
        return ($a % $b) ? self::gcd($b,$a % $b) : $b;
    }
    public static function lcm($x, $y) {
        $p = $x;
        $q = $y;
        while ($x != $y)
        {
            if ($x > $y)
                $x = $x - $y;
            else
                $y = $y - $x;
        }
        return ($p*$q)/$x;
    }
    public static function factorial($number)
    {
        $factorial = 1;
        for ($i = 1; $i <= $number; $i++){
            $factorial = $factorial * $i;
        }
        return $factorial;
    }

}

//$iQueryCount = 1;
$iQueryCount = IO::str();
$aAllResults = [];
while($iQueryCount > 0) {
    $iQueryCount--;
    solve();
}
function solve() {
   $n = IO::str();
   $a = IO::arr();
   $q = IO::str();
   $res = [$a];
   for ($i = 0; $i < 2005; $i++) {
       $tmp = array_count_values($res[$i]);
       $nt = [];
       foreach ($res[$i] as $nv) {
           $nt[] = $tmp[$nv];
       }
       $res[$i+1] = $nt;
   }
   while ($q > 0) {
       $q--;
       list($x, $k) = IO::arr();
       if (isset($res[$k])) {
           IO::line($res[$k][$x - 1]);
       } else {
           IO::line($res[2004][$x - 1]);
       }
   }
}

?>

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

function solve($a, $n) {
  $f = 0;
	$d = 1;

	while ($f !== 1) {
	  $a[$d] = $a[$d - 1];
	  $b = array_count_values($a[$d]);
	  for ($i = 0; $i < $n; $i++) {
	    if ($b[$a[$d][$i]] !== $a[$d][$i]) {
	      $a[$d][$i] = $b[$a[$d][$i]];
	    }
	  }

	  $c = 0;
	  foreach ($b as $key => $val) {
	    if ($key == $val) {
	      $c++;
	    }
	  }

	  if ($c === count($b)) {
	    return $a;
	  }
	  $d++;
	}
}

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

for ($m = 0; $m < $t; $m++) {
	$n = trim(fgets($file));
	$s = explode(" ", trim(fgets($file)));
	$a = [];
	$a[0] = $s;

	$ans = solve($a, $n);

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

	for ($i = 0; $i < $q; $i++) {
	  list($x, $k) = explode(" ", trim(fgets($file)));
	  if ($k == 0) {
	    echo $a[0][$x - 1].PHP_EOL;
	  } else {
	    if ($k >= count($ans)) {
	      echo $ans[count($ans) - 1][$x - 1].PHP_EOL;
	    } else {
	      echo $ans[$k][$x - 1].PHP_EOL;
	    }
	  }

	}

}

?>

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

function solve($a, $n) {
  $f = 0;
	$d = 1;
	$c = 0;

	while ($f !== 1) {
	  $b = array_count_values($a[$d - 1]);
	  for ($i = 0; $i < $n; $i++) {
	    $a[$d][$i] = $b[$a[$d - 1][$i]];
	  }

	  $c = 0;
	  foreach ($b as $key => $val) {
	    if ($key == $val) {
	      $c++;
	    }
	  }

	  if ($c === count($b)) {
	    return $a;
	  }
	  $d++;
	}
}

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

for ($m = 0; $m < $t; $m++) {
	$n = trim(fgets($file));
	$a[0] = explode(" ", trim(fgets($file)));

	$ans = solve($a, $n);

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

	for ($i = 0; $i < $q; $i++) {
	  list($x, $k) = explode(" ", trim(fgets($file)));
	  echo $ans[min($k, $n)][$x - 1].PHP_EOL;
	}

}

?>

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

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

function solveEnd($a, $n, $x, $k) {
  $f = 0;
	$d = 1;
	$c = 0;

	while ($f !== 1) {
	  $b = array_count_values($a);
	  for ($i = 0; $i < $n; $i++) {
	    if ($b[$a[$i]] !== $a[$i]) {
	      $a[$i] = $b[$a[$i]];
	    }
	  }

	  $c = 0;
	  foreach ($b as $key => $val) {
	    if ($key == $val) {
	      $c++;
	    }
	  }

	  if ($c === count($b)) {
	    $ans[0] = $a;
	    $ans[1] = $d;
	    return $ans;
	  }
	  $d++;
	}
}

function solve($a, $n, $x, $k) {
  $f = 0;
	$d = 1;
	$c = 0;

	while ($f !== 1) {
	  $b = array_count_values($a);
	  for ($i = 0; $i < $n; $i++) {
	    if ($b[$a[$i]] !== $a[$i]) {
	      $a[$i] = $b[$a[$i]];
	    }
	  }

	  if ($d == $k) return $a;
	  $d++;

	  $c = 0;
	  foreach ($b as $key => $val) {
	    if ($key == $val) {
	      $c++;
	    }
	  }

	  if ($c === count($b)) return $a;
	}
	return $a;
}

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

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

	  if ($k == 0) {
	    echo $a[$x - 1].PHP_EOL;
	  } else {
	    if ($ans[1] < $k) {
	      echo $ans[0][$x - 1].PHP_EOL;
	    } else {
	      $ans = solve($a, $n, $x, $k);
	      echo $ans[$x - 1].PHP_EOL;
	    }
	  }
	}
}

?>

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

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

function solveEnd($a, $n, $x, $k) {
  $f = 0;
	$d = 1;
	$c = 0;

	while ($f !== 1) {
	  $b = array_count_values($a);
	  for ($i = 0; $i < $n; $i++) {
	    if ($b[$a[$i]] !== $a[$i]) {
	      $a[$i] = $b[$a[$i]];
	    }
	  }

	  $c = 0;
	  foreach ($b as $key => $val) {
	    if ($key == $val) {
	      $c++;
	    }
	  }

	  if ($c === count($b)) {
	    $ans[0] = $a;
	    $ans[1] = $d;
	    return $ans;
	  }
	  $d++;
	}
}

function solve($a, $n, $x, $k) {
  $f = 0;
	$d = 1;
	$c = 0;

	while ($f !== 1) {
	  $b = array_count_values($a);
	  for ($i = 0; $i < $n; $i++) {
	    if ($b[$a[$i]] !== $a[$i]) {
	      $a[$i] = $b[$a[$i]];
	    }
	  }

	  if ($d == $k) return $a;
	  $d++;

	  $c = 0;
	  foreach ($b as $key => $val) {
	    if ($key == $val) {
	      $c++;
	    }
	  }

	  if ($c === count($b)) return $a;
	}
	return $a;
}

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

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

	  if ($k == 0) {
	    echo $a[$x - 1].PHP_EOL;
	  } else {
	    if ($ans[1] <= $k) {
	      echo $ans[0][$x - 1].PHP_EOL;
	    } else {
	      $ans = solve($a, $n, $x, $k);
	      echo $ans[$x - 1].PHP_EOL;
	    }
	  }
	}
}

?>

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

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

function solve($a, $n, $x, $k) {
  $f = 0;
	$d = 1;
	$c = 0;

	while ($f !== 1) {
	  $b = array_count_values($a);
	  for ($i = 0; $i < $n; $i++) {
	    if ($b[$a[$i]] !== $a[$i]) {
	      $a[$i] = $b[$a[$i]];
	    } else {
	      $c++;
	    }
	  }
	  if ($d == $k) return $a[$x - 1];
	  $d++;
	  if ($c >= $n) return $a[$x - 1];
	}
}

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

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

	  if ($k == 0) {
	    echo $a[$x - 1].PHP_EOL;
	  } else {
	    $ans = solve($a, $n, $x, $k);
	    echo $ans.PHP_EOL;
	  }

	}
}

?>

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

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

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

$t = IO::str();

function iter($arr, $ar) {
    $tmp = array();
    foreach ($arr as $i => $a) {
        $tmp[$i] = $ar[$a];
    }
    return $tmp;
}

function add($arr) {
    $ar = array();
    foreach ($arr as $a) {
        if (!isset($ar[$a])) {
            $ar[$a] = 1;
        } else {
            $ar[$a] += 1;
        }
    }
    return $ar;
}

while ($t--) {
    $n = IO::str();
    $arr = IO::arr();
    $ar = add($arr);
//    $arr = iter($arr, $ar);
//    $ar = add($arr);
//    $arr = iter($arr, $ar);
//    $ar = add($arr);
//    $arr = iter($arr, $ar);
//
//    var_dump($arr);
//    var_dump($ar);

    $tmp = array();
    $q = array();

    while ($arr != $tmp) {
        if (count($q) > 1 && $q[count($q)-1] == $arr) {
            break;
        }

        $q[] = $arr;

        $arr = iter($arr, $ar);
        $ar = add($arr);
    }

    $qs = IO::str();
//    foreach ($q as $aq) {
//        foreach ($aq as $qz) {
//            echo $qz . ' ';
//        }
//        echo PHP_EOL;
//    }
    while ($qs--) {
        list($a, $b) = IO::arr();
        if ($b > count($q)) {
            $b = count($q) - 1;
        }
        echo $q[$b][$a-1] . PHP_EOL;
    }
}


<?php

  $f = fopen('php://stdin','r');
  $n = intval(fgets($f));
  for($i=1;$i<=$n;$i++){
    $row = fgets($f);
    if(preg_match('/([0-9\-]+)\s+([0-9\-]+)/',$row,$pregs)){
      $x[$pregs[1]] = $pregs[2];
    }
  }
  fclose($f);

  foreach($x as $key=>$val){
    if(isset($x[$key+$val]) && isset($x[$key+$val+$x[$key+$val]]) && $x[$key+$val] == -$val){
      echo "YES";
      die();
    }
  }
  echo "NO";

?>