You are given an array consisting of n non-negative integers a1, a2, ..., an.You are going to destroy integers in the array one by one. Thus, you are given the permutation of integers from 1 to n defining the order elements of the array are destroyed.After each element is destroyed you have to find out the segment of the array, such that it contains no destroyed elements and the sum of its elements is maximum possible. The sum of elements in the empty segment is considered to be 0.

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the length of the array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).  The third line contains a permutation of integers from 1 to n — the order used to destroy elements.

Print n lines. The i-th line should contain a single integer — the maximum possible sum of elements on the segment containing no destroyed elements, after first i operations are performed.

NoteConsider the first sample:   Third element is destroyed. Array is now 1 3  *  5. Segment with maximum sum 5 consists of one integer 5.  Fourth element is destroyed. Array is now 1 3  *   * . Segment with maximum sum 4 consists of two integers 1 3.  First element is destroyed. Array is now  *  3  *   * . Segment with maximum sum 3 consists of one integer 3.  Last element is destroyed. At this moment there are no valid nonempty segments left in this array, so the answer is equal to 0.


According to a new ISO standard, a flag of every country should have a chequered field n × m, each square should be of one of 10 colours, and the flag should be «striped»: each horizontal row of the flag should contain squares of the same colour, and the colours of adjacent horizontal rows should be different. Berland's government asked you to find out whether their flag meets the new ISO standard.

The first line of the input contains numbers n and m (1 ≤ n, m ≤ 100), n — the amount of rows, m — the amount of columns on the flag of Berland. Then there follows the description of the flag: each of the following n lines contain m characters. Each character is a digit between 0 and 9, and stands for the colour of the corresponding square.

Output YES, if the flag meets the new ISO standard, and NO otherwise.

<?php
list($n,$m) = explode(' ',rtrim(fgets(STDIN)));
$prev=-1;
for ($i=0;$i<$n;$i++) {
  $line=rtrim(fgets(STDIN));
  if ($prev==$line[0]) {
    echo "NO\n";
    exit(0);
  }
  $prev=$line[0];
  for ($j=1;$j<$m;$j++) {
    if ($line[$j-1]!=$line[$j]) {
      echo "NO\n";
      exit(0);
    }
  }
}
echo "YES\n";

<?php
$nm= explode(" ",strval(trim(fgets(STDIN))));
$n = intval($nm[0]);
$m = intval($nm[1]);
$result = "YES";
$prevColor="";
for ($i=0; $i < $n; $i++) {

	$line = str_split(trim(fgets(STDIN)));
	if(count($line)!=$m) {echo "NO";die();}
	if($line[0]==$prevColor && $prevColor!="") {echo "NO";die();}
	foreach ($line as $l) {
		if($l!=$line[0]) {echo "NO";die();}
	}
	$prevColor = $line[0];
}
echo $result;

<?php

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

    $n_m = explode(" ", fgets($stdin));
    $n = $n_m[0];
    $m = $n_m[1];

    for($i=0; $i<$n; ++$i){
        $matrix[$i] = fgets($stdin);
    }

    for($i=0;$i<$n;++$i){
        for($j=1;$j<$m;++$j){
            if($matrix[$i][$j-1] != $matrix[$i][$j]) {
                fputs($stdout, "NO");
                return;
            }
        }
        if(($i!=0)&&($matrix[$i][0] == $matrix[$i-1][0])){
            fputs($stdout, "NO");
            return;
        }
    }
    fputs($stdout, "YES");
?>

<?php

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

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

    if ($die) {
        die();
    }

    echo '</pre>';
}

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

    return $stdin;
}

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

$stdin = stdin();
/****************************************************** Solving block *****************************************************/
list($n, $m) = explode(' ', g());

$flag = true;
$temp = -1;
for ($i = 0; $i < $n; $i++) {
    $line = g();
    $array = str_split($line);
    $sum = array_sum($array);

    if($sum != substr($line, 0, 1) * $m || $temp == $sum) {
        $flag = false;
    }

    $temp = $sum;
}

echo $flag ? 'YES' : 'NO';

?>

<?php

$line = trim(fgets(STDIN));
$data = explode(" ", $line);

$stripes = array();

for ($i = 0; $i < $data[0]; $i++) {
    $stripes[] = trim(fgets(STDIN));
}

$rows = $data[0];
$cols = $data[1];

function oneColor($array) {
    foreach ($array as $value) {
        for ($i = 1; $i < strlen($value); $i++) {
            if ($value[$i] == $value[$i-1]) $one_color = true;
            else {
                $one_color = false;
                break;
            }

        }
        if ($one_color == false) break;
    }
    return $one_color;
}

function alt($array) {
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i-1][0] != $array[$i][0]) {
            $shift = true;
        }
        else {
            $shift = false;
            break;
        }
    }
    return $shift;
}

if (($cols == 1) && (count($stripes) == 1)) echo 'YES';
else if (count($stripes) == 1) {
    if (oneColor($stripes)) echo 'YES';
    else echo 'NO';
}
else if ($cols == 1){
    if (alt($stripes)) echo 'YES';
    else echo 'NO';
}
else if (oneColor($stripes) && alt($stripes)) echo 'YES';
    else echo 'NO';

?>

<?php

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

    $n_m = fgets($stdin);
    $n = $n_m[0];
    $m = $n_m[2];

    for($i=0; $i<$n; ++$i){
        $matrix[$i] = fgets($stdin);
    }

    for($i=0;$i<$n;++$i){
        for($j=1;$j<$m;++$j){
            if($matrix[$i][$j-1] != $matrix[$i][$j]) {
                fputs($stdout, "NO");
                return;
            }
        }
        if(($i!=0)&&($matrix[$i][0] == $matrix[$i-1][0])){
            fputs($stdout, "NO");
            return;
        }
    }
    fputs($stdout, "YES");
?>

<?php

$line = trim(fgets(STDIN));
$data = explode(" ", $line);

$stripes = array();

for ($i = 0; $i < $data[0]; $i++) {
    $stripes[] = trim(fgets(STDIN));
}

$rows = $data[0];
$cols = $data[1];

function oneColor($array) {
    foreach ($array as $value) {
        for ($i = 1; $i < strlen($value); $i++) {
            if ($value[$i] == $value[$i-1]) $one_color = true;
            else {
                $one_color = false;
                break;
            }

        }
        if ($one_color == false) break;
    }
    return $one_color;
}

function alt($array) {
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i-1][0] != $array[$i][0]) {
            $shift = true;
        }
        else {
            $shift = false;
            break;
        }
    }
    return $shift;
}
//if (!oneColor($stripes)) echo "color failure";
//if (!alt($stripes)) echo "shift failure";
/*
function oneElem($array) {
    foreach ($array as $value) {
        if (strlen($value) == 1) $one = true;
        else {
            $one = false;
            break;
        }
    }
    return $one;
}
*/

if (count($stripes) == 1) {
    if (oneColor($stripes)) echo 'YES';
    else echo 'NO';
}
else if ($cols == 1){//(oneElem($stripes)) {
    if (alt($stripes)) echo 'YES';
    else echo 'NO';
}
else if (oneColor($stripes) && alt($stripes)) echo 'YES';
    else echo 'NO';

?>

<?php

$line = trim(fgets(STDIN));
$data = explode(" ", $line);

$stripes = array();

for ($i = 0; $i < $data[0]; $i++) {
    $stripes[] = trim(fgets(STDIN));
}

$rows = $data[0];
$cols = $data[1];

//print_r($stripes);

function oneColor($array) {
    foreach ($array as $value) {
        for ($i = 1; $i < strlen($value); $i++) {
            if ($value[$i] == $value[$i-1]) $one_color = true;
            else {
                $one_color = false;
                break;
            }

        }
        if ($one_color == false) break;
    }
    return $one_color;
}

function alt($array) {
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i-1][0] != $array[$i][0]) {
            $shift = true;
        }
        else {
            $shift = false;
            break;
        }
    }
    return $shift;
}
//if (!oneColor($stripes)) echo "color failure";
//if (!alt($stripes)) echo "shift failure";

if (count($stripes) == 1) {
    if (oneColor($stripes)) echo 'YES';
    else echo 'NO';
    }
else if (oneColor($stripes) && alt($stripes)) echo 'YES';
    else echo 'NO';

?>

<?php

$line = trim(fgets(STDIN));
$data = explode(" ", $line);

//$stripes = array();

for ($i = 0; $i < $data[0]; $i++) {
    $stripes[] = trim(fgets(STDIN));
}

$rows = $data[0];
$cols = $data[1];

print_r($stripes);

function oneColor($array) {
    foreach ($array as $value) {
        for ($i = 1; $i < strlen($value); $i++) {
            if ($value[$i] == $value[$i-1]) $one_color = true;
            else {
                $one_color = false;
                break;
            }

        }
        if ($one_color == false) break;
    }
    return $one_color;
}

function alt($array) {
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i-1][0] != $array[$i][0]) {
            $shift = true;
        }
        else {
            $shift = false;
            break;
        }
    }
    return $shift;
}
//if (!oneColor($stripes)) echo "color failure";
//if (!alt($stripes)) echo "shift failure";
/*
function oneElem($array) {
    foreach ($array as $value) {
        if (strlen($value) == 1) $one = true;
        else {
            $one = false;
            break;
        }
    }
    return $one;
}
*/

if (count($stripes) == 1) {
    if (oneColor($stripes)) echo 'YES';
    else echo 'NO';
}
else if ($cols == 1){//(oneElem($stripes)) {
    if (alt($stripes)) echo 'YES';
    else echo 'NO';
}
else if (oneColor($stripes) && alt($stripes)) echo 'YES';
    else echo 'NO';

?>

<?php

$line = trim(fgets(STDIN));
$data = explode(" ", $line);

$stripes = array();

for ($i = 0; $i < $data[0]; $i++) {
    $stripes[] = trim(fgets(STDIN));
}

$rows = $data[0];
$cols = $data[1];

print_r($stripes);

function oneColor($array) {
    foreach ($array as $value) {
        for ($i = 1; $i < strlen($value); $i++) {
            if ($value[$i] == $value[$i-1]) $one_color = true;
            else {
                $one_color = false;
                break;
            }

        }
        if ($one_color == false) break;
    }
    return $one_color;
}

function alt($array) {
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i-1][0] != $array[$i][0]) {
            $shift = true;
        }
        else {
            $shift = false;
            break;
        }
    }
    return $shift;
}
//if (!oneColor($stripes)) echo "color failure";
//if (!alt($stripes)) echo "shift failure";
/*
function oneElem($array) {
    foreach ($array as $value) {
        if (strlen($value) == 1) $one = true;
        else {
            $one = false;
            break;
        }
    }
    return $one;
}
*/

if (count($stripes) == 1) {
    if (oneColor($stripes)) echo 'YES';
    else echo 'NO';
}
else if ($cols == 1){//(oneElem($stripes)) {
    if (alt($stripes)) echo 'YES';
    else echo 'NO';
}
else if (oneColor($stripes) && alt($stripes)) echo 'YES';
    else echo 'NO';

?>

<?php

$line = trim(fgets(STDIN));
$data = explode(" ", $line);

$stripes = array();

for ($i = 0; $i < $data[0]; $i++) {
    $stripes[] = trim(fgets(STDIN));
}

$rows = $data[0];
$cols = $data[1];

//print_r($stripes);

function oneColor($array) {
    foreach ($array as $value) {
        for ($i = 1; $i < strlen($value); $i++) {
            if ($value[$i] == $value[$i-1]) $one_color = true;
            //if ($one_color == true) echo "True";
            else {
                $one_color = false;
                //if ($one_color == false) echo "false";
                break;
            }

        }
        if ($one_color == false) break;
    }
    if ($one_color == true) echo "True";
    return $one_color;
}

function alt($array) {
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i-1][0] != $array[$i][0]) {
            $shift = true;
        }
        else {
            $shift = false;
            break;
        }
    }
    return $shift;
}
if (!oneColor($stripes)) echo "color failure";
if (!alt($stripes)) echo "shift failure";
if (oneColor($stripes) && alt($stripes)) echo 'YES';
else echo 'NO';

?>

<?php

$line = trim(fgets(STDIN));
$data = explode(" ", $line);

$stripes = array();

for ($i = 0; $i < $data[0]; $i++) {
    $stripes[] = trim(fgets(STDIN));
}

$rows = $data[0];
$cols = $data[1];

//print_r($stripes);

function oneColor($array) {
    foreach ($array as $value) {
        for ($i = 1; $i < strlen($value); $i++) {
            if ($value[$i] == $value[$i-1]) $one_color = true;
            else {
                $one_color = false;
                break;
            }
        }
        if (one_color == false) break;
    }
    return $one_color;
}

function alt($array) {
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i-1][0] != $array[$i][0]) {
            $shift = true;
        }
        else {
            $shift = false;
            break;
        }
    }
    return $shift;
}

//if (!alt($stripes)) echo "shift failure";
if (oneColor($stripes) && alt($stripes)) echo 'YES';
else echo 'NO';

?>

<?php

$line = trim(fgets(STDIN));
$data = explode(" ", $line);

$stripes = array();

for ($i = 0; $i < $data[0]; $i++) {
    $stripes[] = trim(fgets(STDIN));
}

$rows = $data[0];
$cols = $data[1];

//print_r($stripes);

function oneColor($array) {
    foreach ($array as $value) {
        for ($i = 1; $i < strlen($value); $i++) {
            if ($value[$i] == $value[$i-1]) $one_color = true;
            else {
                $one_color = false;
                break;
            }
            if ($one_color == false) break;
        }
    }
    return $one_color;
}

function alt($array) {
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i-1][0] != $array[$i][0]) {
            $shift = true;
        }
        else {
            $shift = false;
            break;
        }
    }
    return $shift;
}

//if (!alt($stripes)) echo "shift failure";
if (oneColor($stripes) && alt($stripes)) echo 'YES';
else echo 'NO';

?>

<?php

$line = trim(fgets(STDIN));
$data = explode(" ", $line);

$stripes = array();

for ($i = 0; $i < $data[0]; $i++) {
    $stripes[] = trim(fgets(STDIN));
}

$rows = $data[0];
$cols = $data[1];

function oneColor($array) {
    foreach ($array as $value) {
        for ($i = 1; $i < strlen($value); $i++) {
            if ($value[$i] == $value[$i-1]) $one_color = true;
            else {
                $one_color = false;
                break;
            }

        }
        if ($one_color == false) break;
    }
    return $one_color;
}

function alt($array) {
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i-1][0] != $array[$i][0]) {
            $shift = true;
        }
        else {
            $shift = false;
            break;
        }
    }
    return $shift;
}

if (count($stripes) == 1) {
    if (oneColor($stripes)) echo 'YES';
    else echo 'NO';
}
else if ($cols == 1){//(oneElem($stripes)) {
    if (alt($stripes)) echo 'YES';
    else echo 'NO';
}
else if (($cols == 1) && count($stripes) == 1) echo 'YES';
else if (oneColor($stripes) && alt($stripes)) echo 'YES';
    else echo 'NO';

?>

<?php

$line = trim(fgets(STDIN));
$data = explode(" ", $line);

$stripes = array();

for ($i = 0; $i < $data[0]; $i++) {
    $stripes[] = trim(fgets(STDIN));
}

$rows = $data[0];
$cols = $data[1];

//print_r($stripes);

function oneColor($array) {
    foreach ($array as $value) {
        for ($i = 1; $i < strlen($value); $i++) {
            if ($value[$i] == $value[$i-1]) $one_color = true;
            else {
                $one_color = false;
                //break;
               // goto a;
            }
            if ($one_color == false) break;
        }
    }
     return $one_color;
}

function alt($array) {
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i-1][0] != $array[$i][0]) {
            $shift = true;
        }
        else {
            $shift = false;
            break;
        }
    }
    return $shift;
}

//if (!alt($stripes)) echo "shift failure";
if (oneColor($stripes) && alt($stripes)) echo 'YES';
else echo 'NO';

?>

<?php

$line = trim(fgets(STDIN));
$data = explode(" ", $line);

$stripes = array();

for ($i = 0; $i < $data[0]; $i++) {
    $stripes[] = trim(fgets(STDIN));
}

$rows = $data[0];
$cols = $data[1];

//print_r($stripes);

function oneColor($array) {
    foreach ($array as $value) {
        for ($i = 1; $i < strlen($value); $i++) {
            if ($value[$i] == $value[$i-1]) $one_color = true;
            else {
                $one_color = false;
                break;
            }
        }
    }
    return $one_color;
}

function alt($array) {
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i-1][0] != $array[$i][0]) {
            $shift = true;
        }
        else {
            $shift = false;
            break;
        }
    }
    return $shift;
}

//if (!alt($stripes)) echo "shift failure";
if (oneColor($stripes) && alt($stripes)) echo 'YES';
else echo 'NO';

?>

<?php

$line = trim(fgets(STDIN));
$data = explode(" ", $line);

$stripes = array();

for ($i = 0; $i < $data[0]; $i++) {
    $stripes[] = trim(fgets(STDIN));
}

$rows = $data[0];
$cols = $data[1];

//print_r($stripes);

function oneColor($array) {
    foreach ($array as $value) {
        for ($i = 1; $i < strlen($value); $i++) {
            if ($value[$i] == $value[$i-1]) $one_color = true;
            else {
                $one_color = false;
                break;
            }
        }
        break;
    }
    return $one_color;
}

function alt($array) {
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i-1][0] != $array[$i][0]) {
            $shift = true;
        }
        else {
            $shift = false;
            break;
        }
    }
    return $shift;
}

//if (!alt($stripes)) echo "shift failure";
if (oneColor($stripes) && alt($stripes)) echo 'YES';
else echo 'NO';

?>

<?php

$line = trim(fgets(STDIN));
$data = explode(" ", $line);

$stripes = array();

for ($i = 0; $i < $data[0]; $i++) {
    $stripes[] = trim(fgets(STDIN));
}

$rows = $data[0];
$cols = $data[1];

print_r($stripes);

function oneColor($array) {
    foreach ($array as $value) {
        for ($i = 1; $i < strlen($value); $i++) {
            if ($value[$i] == $value[$i-1]) $one_color = true;
            else {
                $one_color = false;
                break;
            }

        }
        if ($one_color == false) break;
    }
    return $one_color;
}

function alt($array) {
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i-1][0] != $array[$i][0]) {
            $shift = true;
        }
        else {
            $shift = false;
            break;
        }
    }
    return $shift;
}
//if (!oneColor($stripes)) echo "color failure";
//if (!alt($stripes)) echo "shift failure";

if (count($stripes) == 1) {
    if (oneColor($stripes)) echo 'YES';
    else echo 'NO';
    }
else if (oneColor($stripes) && alt($stripes)) echo 'YES';
    else echo 'NO';

?>

<?php

$line = trim(fgets(STDIN));
$data = explode(" ", $line);

$stripes = array();

for ($i = 0; $i < $data[0]; $i++) {
    $stripes[] = trim(fgets(STDIN));
}

$rows = $data[0];
$cols = $data[1];

//print_r($stripes);

function oneColor($array) {
    foreach ($array as $value) {
        for ($i = 1; $i < strlen($value); $i++) {
            if ($value[$i] == $value[$i-1]) $one_color = true;
            //if ($one_color == true) echo "True";
            else {
                $one_color = false;
                //if ($one_color == false) echo "false";
                break;
            }

        }
        if ($one_color == false) break;
    }
    return $one_color;
}

function alt($array) {
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i-1][0] != $array[$i][0]) {
            $shift = true;
        }
        else {
            $shift = false;
            break;
        }
    }
    return $shift;
}
//if (!oneColor($stripes)) echo "color failure";
//if (!alt($stripes)) echo "shift failure";
if (oneColor($stripes) && alt($stripes)) echo 'YES';
else echo 'NO';

?>

<?php

$line = trim(fgets(STDIN));
$data = explode(" ", $line);

$stripes = array();

for ($i = 0; $i < $data[0]; $i++) {
    $stripes[] = trim(fgets(STDIN));
}

$rows = $data[0];
$cols = $data[1];

//print_r($stripes);

function oneColor($array) {
    foreach ($array as $value) {
        for ($i = 1; $i < strlen($value); $i++) {
            if ($value[$i] == $value[$i-1]) $one_color = true;
            else {
                $one_color = false;
                //break;
            }

        }
        if ($one_color == false) break;
    }
    return $onecolor;
}

function alt($array) {
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i-1][0] != $array[$i][0]) {
            $shift = true;
        }
        else {
            $shift = false;
            break;
        }
    }
    return $shift;
}

//if (!alt($stripes)) echo "shift failure";
if (oneColor($stripes) && alt($stripes)) echo 'YES';
else echo 'NO';

?>


A binary string is a string consisting only of the characters 0 and 1. You are given a binary string $$$s_1 s_2 \ldots s_n$$$. It is necessary to make this string non-decreasing in the least number of operations. In other words, each character should be not less than the previous. In one operation, you can do the following: Select an arbitrary index $$$1 \leq i \leq n$$$ in the string; For all $$$j \geq i$$$, change the value in the $$$j$$$-th position to the opposite, that is, if $$$s_j = 1$$$, then make $$$s_j = 0$$$, and vice versa.What is the minimum number of operations needed to make the string non-decreasing?

Each test consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of test cases follows. The first line of each test cases a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the length of the string. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output a single integer — the minimum number of operations that are needed to make the string non-decreasing.

NoteIn the first test case, the string is already non-decreasing.In the second test case, you can select $$$i = 1$$$ and then $$$s = \mathtt{01}$$$.In the third test case, you can select $$$i = 1$$$ and get $$$s = \mathtt{010}$$$, and then select $$$i = 2$$$. As a result, we get $$$s = \mathtt{001}$$$, that is, a non-decreasing string.In the sixth test case, you can select $$$i = 5$$$ at the first iteration and get $$$s = \mathtt{100001}$$$. Then choose $$$i = 2$$$, then $$$s = \mathtt{111110}$$$. Then we select $$$i = 1$$$, getting the non-decreasing string $$$s = \mathtt{000001}$$$.

<?php

//[$a,$b] = array_map('intval', explode(' ', trim(fgets(STDIN))));
//[$s] = explode(' ', trim(fgets(STDIN)));
//$a = array_map('intval', explode(' ', trim(fgets(STDIN))));
//$h = array_map('intval', explode(' ', trim(fgets(STDIN))));
//$mod = 998244353;
//$mod = 1000000007;
//$alpha = "abcdefghijklmnopqrstuvwxyz";
$ans = [];
[$t] = array_map('intval', explode(' ', trim(fgets(STDIN))));
for($i=0;$i<$t;$i++){
    solve();
}
echo implode(PHP_EOL,$ans);
function solve(){
    global $ans;
    [$n] = array_map('intval', explode(' ', trim(fgets(STDIN))));
    [$s] = explode(' ', trim(fgets(STDIN)));
    $ok = true;
    for($i=1;$i<$n;$i++){
        if($s[$i]!=$s[$i-1]){
            $ok = false;
            break;
        }
    }
    if($ok){
        $ans[] = 0;
        return;
    }
    $cnt = 0;
    if($s[0]=="1"){
        $cnt++;
    }
    for($i=1;$i<$n;$i++){
        if($s[$i]!=$s[$i-1]){
            $cnt++;
        }
    }
    $ans[] = $cnt-1;

}


The statement of this problem is the same as the statement of problem C2. The only difference is that, in problem C1, $$$n$$$ is always even, and in C2, $$$n$$$ is always odd.You are given a regular polygon with $$$2 \cdot n$$$ vertices (it's convex and has equal sides and equal angles) and all its sides have length $$$1$$$. Let's name it as $$$2n$$$-gon.Your task is to find the square of the minimum size such that you can embed $$$2n$$$-gon in the square. Embedding $$$2n$$$-gon in the square means that you need to place $$$2n$$$-gon in the square in such way that each point which lies inside or on a border of $$$2n$$$-gon should also lie inside or on a border of the square.You can rotate $$$2n$$$-gon and/or the square.

The first line contains a single integer $$$T$$$ ($$$1 \le T \le 200$$$) — the number of test cases. Next $$$T$$$ lines contain descriptions of test cases — one per line. Each line contains single even integer $$$n$$$ ($$$2 \le n \le 200$$$). Don't forget you need to embed $$$2n$$$-gon, not an $$$n$$$-gon.

Print $$$T$$$ real numbers — one per test case. For each test case, print the minimum length of a side of the square $$$2n$$$-gon can be embedded in. Your answer will be considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$.

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

    define("GAP_VAL", 1e-7) ;

    function gap($n)
    {
        if($n < GAP_VAL) return true ;
        return false ;
    }

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

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

        $sin_n = sin(M_PI / $n) ;

        $l = 0.5 ;
        $r = $n / 2.0 ;

        while(!gap($r - $l))
        {
            $tmp = ($r + $l) / 2 ;

            $tmp_s = ($tmp * $tmp + 0.25) * $sin_n ;

            if($tmp_s > $tmp) $r = $tmp ;
            else $l = $tmp ;
        }

        echo 2 * $l . "\n" ;
    }
?>


Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends a, b know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.Jenny is given the number 0.

The first line of the input contains the number of friends n (3 ≤ n ≤ 100). The next n - 1 lines each contain three space-separated integers u, v and c (0 ≤ u, v ≤ n - 1, 1 ≤ c ≤ 104), meaning that u and v are friends (know each other directly) and the cost for travelling between u and v is c. It is guaranteed that the social network of the input forms a tree.

Output a single integer – the maximum sum of costs.

NoteIn the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2).


There is an integer $$$n$$$ without zeros in its decimal representation. Alice and Bob are playing a game with this integer. Alice starts first. They play the game in turns.On her turn, Alice must swap any two digits of the integer that are on different positions. Bob on his turn always removes the last digit of the integer. The game ends when there is only one digit left.You have to find the smallest integer Alice can get in the end, if she plays optimally.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Description of the test cases follows. The first and the only line of each test case contains the integer $$$n$$$ ($$$10 \le n \le 10^9$$$) — the integer for the game. $$$n$$$ does not have zeros in its decimal representation.

For each test case output a single integer — the smallest integer Alice can get in the end of the game.

NoteIn the first test case Alice has to swap $$$1$$$ and $$$2$$$. After that Bob removes the last digit, $$$1$$$, so the answer is $$$2$$$.In the second test case Alice can swap $$$3$$$ and $$$1$$$: $$$312$$$. After that Bob deletes the last digit: $$$31$$$. Then Alice swaps $$$3$$$ and $$$1$$$: $$$13$$$ and Bob deletes $$$3$$$, so the answer is $$$1$$$.

<?php
    $t = trim(fgets(STDIN));
    for($n = 1; $n <= $t; $n++) {
        $s = trim(fgets(STDIN));

        $a = str_split($s);
        if (count($a) == 2) {
            echo $a[1] . PHP_EOL;
        } else {
            sort($a);
            echo $a[0] . PHP_EOL;
        }
    }
?>

<?php
$no = 'NO';
$yes = 'YES';
$mod = 1000000007;
$intMaxValue = 2147483646; //2147483647
$alph = 'abcdefghijklmnopqrstuvwxyz';
$inter = "\n";
define("INTMAXVALUE",     2147483646);

   $t = IO::getInt();
//   $point = ord('a') + ord('b') ;

   for($q=0; $q <$t; $q++) {
       $ans = 0;
       $n = IO::getInt();

       $len = strlen($n);
        if($len == 2) {
             $ans = $n[1];
        } else {
             $ans = $n[0];
             for($i=1; $i<$len; $i++) {
                  if($ans > $n[$i]) {
                       $ans = $n[$i];
                  }
             }
        }
       IO::pr($ans);

   }

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

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

 class Algor {
    public static function isPrime($n) {
         $d = 2;
         while ($n % $d != 0) {
         $d += 1;
         }

        return $d == $n;
    }

     public static function fabicon($n, $a) {
         if($n <= 1) {
             return $a[$n];
         }
         for($i=2; $i<=$n; $i++) {
             $a[$i] = $a[$i-1] + $a[$i-2];
         }

         return $a[$n];
     }

     function getRotate($a, $s, $e, $ind) {
         $ar = [];
         for ($i= $s; $i<=$e; $i++) {
             $ar[$i] = $a[$i];
             $j = $i + $ind > $e ? $i + $ind - $e - 1 : $i + $ind;
             // echo $i, ' ', $j, ' ', $j < $i ? $ar[$j] : $a[$j] , "\n";
             $a[$i] = $j < $i ? $ar[$j] : $a[$j];
         }
         return $a;
     }

}

class bigOperation {
    public static function  isBigNumber($n)
    {
        return bccomp($n, INTMAXVALUE) == 1;
    }
    // bcadd — Сложить 2 числа произвольной точности
    public static function  bcAdd($x, $y)
    {
        return bcadd($x, $y);
    }
// bccomp — Сравнение двух чисел произвольной точности
//return 1 if x>y, 0= x==y, -1 x < y
    public static function  bcComp($x, $y)
    {
        return bccomp($x, $y);
    }
// bcdiv — Операция деления для чисел произвольной точности
    public static function  bcDiv($x, $y)
    {
        return bcdiv($x, $y);
    }
// bcmod — Получает остаток от деления чисел с произвольной точностью
    public static function  bcMod($x, $y)
    {
        return bcmod($x, $y);
    }
// bcmul — Умножение двух чисел с произвольной точностью
    public static function  bcMul($x, $y)
    {
        return bcmul($x, $y);
    }
// bcpow — Возведение в степень чисел с произвольной точностью
    public static function  bcPow($x, $y)
    {
        return bcpow($x, $y);
    }
// bcpowmod — Возводит одно число в степень другого и возвращает остаток от деления результата на третье число
// bcscale — Задает количество чисел после десятичной точки по умолчанию для всех bc math функций.
// bcsqrt — Извлекает квадратный корень из числа с произвольной точностью
    public static function  bcSqrt($x, $y)
    {
        return bcsqrt($x, $y);
    }
// bcsub — Вычитает одно число с произвольной точностью из другого
    public static function  bcSub($x, $y)
    {
        return bcsub($x, $y);
    }
    public static function getMaxValue($x, $y) {
        return bccomp($x, $y) == 1 ? $x : $y;
    }
    public static function getMinValue($x, $y) {
        return bccomp($x, $y) == 1 ? $y : $x;
    }

    public static function getDareje($n, $k) {
        if($k == 0) {
            return 1;
        }
        return bcmul($n, self::getDareje($n, $k-1));
    }
}

class Sorting
{
    public static function mergeSort($aArray) {
        if(count($aArray) <= 1) {
            return $aArray;
        }
        $aLeft = [];
        $aRight = [];
        $n =floor(count($aArray) / 2);
        for($i = 0; $i <$n; $i++) {
            $aLeft[] = $aArray[$i];
        }
        for($i = $n; $i <count($aArray); $i++) {
            $aRight[] = $aArray[$i];
        }

        return self::merge(self::mergeSort($aLeft), self::mergeSort($aRight));
    }

    public static function merge($aLeft, $aRight) {
        $aResult = [];
        $iL = 0;
        $iR = 0;

        while($iL < count($aLeft) && $iR < count($aRight)) {
            if($aLeft[$iL] <= $aRight[$iR]) {
                $aResult[] = $aLeft[$iL];
                $iL++;
            } else {
                $aResult[] = $aRight[$iR];
                $iR++;
            }
        }

        while($iL < count($aLeft)) {
            $aResult[] = $aLeft[$iL];
            $iL++;
        }
        while($iR < count($aRight)) {
            $aResult[] = $aRight[$iR];
            $iR++;
        }

        return $aResult;

    }
}

class SolveArchive
{

}


«Bersoft» company is working on a new version of its most popular text editor — Bord 2010. Bord, like many other text editors, should be able to print out multipage documents. A user keys a sequence of the document page numbers that he wants to print out (separates them with a comma, without spaces).Your task is to write a part of the program, responsible for «standardization» of this sequence. Your program gets the sequence, keyed by the user, as input. The program should output this sequence in format l1-r1,l2-r2,...,lk-rk, where ri + 1 < li + 1 for all i from 1 to k - 1, and li ≤ ri. The new sequence should contain all the page numbers, keyed by the user, and nothing else. If some page number appears in the input sequence several times, its appearances, starting from the second one, should be ignored. If for some element i from the new sequence li = ri, this element should be output as li, and not as «li - li».For example, sequence 1,2,3,1,1,2,6,6,2 should be output as 1-3,6.

The only line contains the sequence, keyed by the user. The sequence contains at least one and at most 100 positive integer numbers. It's guaranteed, that this sequence consists of positive integer numbers, not exceeding 1000, separated with a comma, doesn't contain any other characters, apart from digits and commas, can't end with a comma, and the numbers don't contain leading zeroes. Also it doesn't start with a comma or contain more than one comma in a row.

Output the sequence in the required format.


In a far away kingdom young pages help to set the table for the King. As they are terribly mischievous, one needs to keep an eye on the control whether they have set everything correctly. This time the royal chef Gerasim had the impression that the pages have played a prank again: they had poured the juice from one cup to another. Now Gerasim wants to check his hypothesis. The good thing is that chef Gerasim always pour the same number of milliliters of juice to all cups in the royal kitchen. Having thoroughly measured the juice in each cup, Gerasim asked you to write a program that will determine from which cup juice was poured to which one; otherwise, the program should determine that this time the pages set the table diligently.To simplify your task we shall consider the cups to be bottomless so that the juice never overfills a cup and pours out, however much it can be. Besides, by some strange reason in a far away kingdom one can only pour to a cup or from one cup to another an integer number of milliliters of juice.

The first line contains integer n — the number of cups on the royal table (1 ≤ n ≤ 1000). Next n lines contain volumes of juice in each cup — non-negative integers, not exceeding 104.

If the pages didn't pour the juice, print "Exemplary pages." (without the quotes). If you can determine the volume of juice poured during exactly one juice pouring, print "v ml. from cup #a to cup #b." (without the quotes), where v represents the volume of poured juice, a represents the number of the cup from which the juice was poured (the cups are numbered with consecutive positive integers starting from one in the order in which the cups are described in the input data), b represents the number of the cup into which the juice was poured. Finally, if the given juice's volumes cannot be obtained using no more than one pouring (for example, the pages poured the juice from one cup to another more than once or the royal kitchen maids poured the juice into the cups incorrectly), print "Unrecoverable configuration." (without the quotes).

20 ml. from cup #4 to cup #1.
Exemplary pages.
Unrecoverable configuration.

<?php

    fscanf(STDIN, "%d", $n);
    for($i=0;$i<$n;$i++)
    {
        fscanf(STDIN, "%d", $a);
        $graf[$i]=$a;
    }
    $graf1=$graf;
    sort($graf1, SORT_NUMERIC);asort($graf, SORT_NUMERIC);
    if($n==1){printf("%s\n", "Exemplary pages.");exit;}

    if($graf1[0]==$graf1[$n-1])
    {
        printf("%s\n", "Exemplary pages.");
        exit;
    }else{

        if(((array_sum($graf1)/$n)==ceil(array_sum($graf1)/$n) and ($graf1[0]+$graf1[$n-1])==(2*$graf1[1])) or ((($graf1[0]+$graf1[$n-1])/2)==ceil(($graf1[0]+$graf1[$n-1])/2) and $n==2))
        {
            reset($graf);
            current($graf);
            $w1=key($graf);
            end($graf);
            $w2=key($graf);
            printf("%d ml. from cup #%d to cup #%d.\n", ($graf1[$n-1]-$graf1[0])/2, $w1+1, $w2+1);exit;
        }else{
            printf("%s\n", "Unrecoverable configuration.");exit;
        }
    }

?>

<?php
error_reporting(0);
$dir = fopen("php://stdin", 'r');
$datatxt = stream_get_contents($dir);
// $datatxt = "5
// 270
// 250
// 250
// 230
// 250
// ";
$text = explode("\n",$datatxt);
$total = intval($text[0]);
unset($text[0]);
foreach($text as $key => $value) {
if(empty($value)){
unset($text[$key]);
}
}
$cups = array_map('intval', $text);
$sum = array_sum($cups);
if(($sum%$total) !=0)
{
echo "Unrecoverable configuration.";
exit;
} else {
$tmp = $sum/$total;
$diff = array();
foreach($cups as $key => $value) {
if ($tmp != $value){
$diff[$key]=$value;
}
}

if (empty($diff)) {
echo "Exemplary pages.";
exit;

}
if (count($diff)!=2) {
echo "Unrecoverable configuration.";
exit;
}
$max = 0;
$min = 0;
$minkey = 0;
$maxkey = 0;
$first =true;
foreach($diff as $key => $value){
if ($first) {
$max = $value;
$maxkey = $key;
$min = $value;
$minkey = $key;
}
if ($value>$max) {
$min = $max;
$minkey = $maxkey;
$max = $value;
$maxkey = $key;
} else {
$min = $value;
$minkey = $key;
}
$first = false;
}
$ml = ($max-$min)/2;
echo "{$ml} ml. from cup #{$minkey} to cup #{$maxkey}.";
}
?>

<?php

while (FALSE !== ($line = fgets(STDIN))) {

    $stat = array();
    $unrecoverableFlag = false;

    for($i = 1; $i <= $line; $i++){
        $cupValue = fgets(STDIN);

        if($unrecoverableFlag === true){
            continue;
        }

        $cupValue = trim($cupValue);

        if($stat[$cupValue]){
            $stat[$cupValue][0] = $i;    //line number
            $stat[$cupValue][1]++;       //count
        }else{
            $stat[$cupValue] = array($i, 1);
        }

        if(count($stat) > 3) {
            $unrecoverableFlag = true;
        }
    } //end of input taking and general statistics part ..

    $count = count($stat);

    if($unrecoverableFlag){
        echo 'Unrecoverable configuration.' . PHP_EOL;
    }

    if($count == 2){
        if($line == 2){
            //very special case ..
            $sorted = array_keys($stat);
            sort($sorted);
            $cupOneValue = $sorted[0];
            $cupTwoValue = $sorted[1];

            if( $stat[$cupOneValue][1] == 1 && $stat[$cupTwoValue][1] == 1){

                $diff = $cupTwoValue - $cupOneValue;
                if($diff % 2 == 1){
                    echo 'Unrecoverable configuration.' . PHP_EOL;
                }else{
                    $volum = $diff / 2;

                    echo $volum . ' ml. from cup #' . $stat[$cupOneValue][0] .
                       ' to cup #' . $stat[$cupTwoValue][0] . '.' . PHP_EOL;
                }

            }else{
                echo 'Unrecoverable configuration.' . PHP_EOL;
            }
        }else{
            echo 'Unrecoverable configuration.' . PHP_EOL;
        }
    }

    if($count == 1) {
        echo 'Exemplary pages.' . PHP_EOL;
    }

    if($count == 3){

        $sorted = array_keys($stat);

        sort($sorted);

        $cupOneValue = $sorted[0];
        $cupTwoValue = $sorted[2];

        if( $stat[$cupOneValue][1] == 1 && $stat[$cupTwoValue][1] == 1){

           $volum = ($cupTwoValue - $cupOneValue) / 2;

            if($volum  == ($sorted[1] - $cupOneValue)){
                echo $volum . ' ml. from cup #' . $stat[$cupOneValue][0] .
                   ' to cup #' . $stat[$cupTwoValue][0] . '.' . PHP_EOL;
            }else{
                echo 'Unrecoverable configuration.' . PHP_EOL;
            }

        }else{
            echo 'Unrecoverable configuration.' . PHP_EOL;
        }
    }
}

<?php
$a = trim(fgets(STDIN));
$b = array();
for($x = 0; $x < $a; $x++)
{
    $c = trim(fgets(STDIN));
    $b[$x] = $c;
}
$d = array_unique($b);
rsort($d);
if(count($d) == 1)
{
    print "Exemplary pages.";
}
elseif(count($d) == 3)
{
    $e = ($d[0] - $d[2]) / 2;
    $f = 0;
    $g = 0;
    for($x = 0; $x < $a; $x++)
    {
        if($b[$x] == $d[0])
        {
            $f = $x + 1;
        }
        if($b[$x] == $d[2])
        {
            $g = $x + 1;
        }
    }
    print $e . " ml. from cup #" . $g . " to cup #" . $f . ".";
}
else
{
    print "Unrecoverable configuration.";
}
?>

<?php
$a = trim(fgets(STDIN));
$b = array();
for($x = 0; $x < $a; $x++)
{
    $c = trim(fgets(STDIN));
    $b[$x] = $c;
}
$d = array_unique($b);
rsort($d);
if(count($d) == 1)
{
    print "Exemplary pages.";
}
elseif(count($d) == 3)
{
    $e = ($d[0] - $d[2]) / 2;
    $f = 0;
    $g = 0;
    for($x = 0; $x < $a; $x++)
    {
        if($b[$x] == $d[0])
        {
            $f = $x + 1;
        }
        if($b[$x] == $d[2])
        {
            $g = $x + 1;
        }
    }
    print $e . " ml. from cup #" . $g . " to cup #" . $f . ".";
}
elseif(count($d) > 3)
{
    print "Unrecoverable configuration.";
}
?>

<?php

    fscanf(STDIN, "%d", $n);
    for($i=0;$i<$n;$i++)
    {
        fscanf(STDIN, "%d", $a);
        $graf[$i]=$a;
    }
    $graf1=$graf;
    sort($graf1, SORT_NUMERIC);asort($graf, SORT_NUMERIC);
    if($n==1){printf("%s\n", "Exemplary pages.");exit;}

    if($graf1[0]==$graf1[$n-1])
    {
        printf("%s\n", "Exemplary pages.");
        exit;
    }else{

        if(($graf1[0]+$graf1[$n-1])==(2*$graf1[1]))
        {
            reset($graf);
            current($graf);
            $w1=key($graf);
            end($graf);
            $w2=key($graf);
            printf("%d ml. from cup #%d to cup #%d.\n", ($graf1[$n-1]-$graf1[0])/2, $w1+1, $w2+1);exit;
        }else{
            printf("%s\n", "Unrecoverable configuration.");exit;
        }
    }

?>

<?php

    fscanf(STDIN, "%d", $n);
    for($i=0;$i<$n;$i++)
    {
        fscanf(STDIN, "%d", $a);
        $graf[$i]=$a;
    }
    $graf1=$graf;
    sort($graf1, SORT_NUMERIC);asort($graf, SORT_NUMERIC);
    if($n==1){printf("%s\n", "Exemplary pages.");exit;}

    if($graf1[0]==$graf1[$n-1])
    {
        printf("%s\n", "Exemplary pages.");
        exit;
    }else{

        if(($graf1[0]+$graf1[$n-1])==(2*$graf1[1]))
        {
            reset($graf);
            current($graf);
            $w1=key($graf);
            end($graf);
            $w2=key($graf);
            printf("%d ml. from cup #%d to cup #%d.\n", ($graf1[$n-1]-$graf1[0]), $w1+1, $w2+1);exit;
        }else{
            printf("%s\n", "Unrecoverable configuration.");exit;
        }
    }

?>

<?php

    fscanf(STDIN, "%d", $n);
    for($i=0;$i<$n;$i++)
    {
        fscanf(STDIN, "%d", $a);
        $graf[$i]=$a;
    }
    $graf1=$graf;
    sort($graf1, SORT_NUMERIC);asort($graf, SORT_NUMERIC);
    if($n==1){printf("%s\n", "Exemplary pages.");exit;}

    if($graf1[0]==$graf1[$n-1])
    {
        printf("%s\n", "Exemplary pages.");
        exit;
    }else{

        if(($graf1[0]+$graf1[$n-1])==(2*$graf1[1]) or ((($graf1[0]+$graf1[$n-1])/2)==ceil(($graf1[0]+$graf1[$n-1])/2) and $n==2))
        {
            reset($graf);
            current($graf);
            $w1=key($graf);
            end($graf);
            $w2=key($graf);
            printf("%d ml. from cup #%d to cup #%d.\n", ($graf1[$n-1]-$graf1[0])/2, $w1+1, $w2+1);exit;
        }else{
            printf("%s\n", "Unrecoverable configuration.");exit;
        }
    }

?>

<?php
error_reporting(0);
$dir = fopen("php://stdin", 'r');
$datatxt = stream_get_contents($dir);
$text = explode("\n",$datatxt);
$total = intval($text[0]);
unset($text[0]);
$cups = array_map('intval', $text);
$sum = array_sum($cups);
if(($sum%$total) !=0)
{
echo "Unrecoverable configuration.";
exit;
} else {
$tmp = $sum/$total;
$diff = array();
foreach($cups as $key => $value) {
if ($tmp != $value){
$diff[$key]=$value;
}
}
if (empty($diff)) {
echo "Exemplary pages.";
exit;
}
sort($diff);
$max = 0;
$min = 0;
foreach($diff as $key => $value){
if($value>$max) {
$max = $value;
$maxkey = $key;
} else {
$min = $value;
$minkey = $key;
}
}
$ml = ($max-$min)/2;
echo "{$ml} ml. from cup #{$minkey} to cup #{$maxkey}.";
}
?>

<?php
error_reporting(0);
$dir = fopen("php://stdin", 'r');
$datatxt = stream_get_contents($dir);
// $datatxt = "5
// 270
// 250
// 250
// 230
// 250
// ";
$text = explode("\n",$datatxt);
$total = intval($text[0]);
unset($text[0]);
foreach($text as $key => $value) {
if(empty($value)){
unset($text[$key]);
}
}
$cups = array_map('intval', $text);
$sum = array_sum($cups);
if(($sum%$total) !=0)
{
echo "Unrecoverable configuration.";
exit;
} else {
$tmp = $sum/$total;
$diff = array();
foreach($cups as $key => $value) {
if ($tmp != $value){
$diff[$key]=$value;
}
}

if (empty($diff)) {
echo "Exemplary pages.";
exit;

}
$max = 0;
$min = 0;
$minkey = 0;
$maxkey = 0;
$first =true;
foreach($diff as $key => $value){
if ($first) {
$max = $value;
$maxkey = $key;
$min = $value;
$minkey = $key;
}
if ($value>$max) {
$min = $max;
$minkey = $maxkey;
$max = $value;
$maxkey = $key;
} else {
$min = $value;
$minkey = $key;
}
$first = false;
}
$ml = ($max-$min)/2;
echo "{$ml} ml. from cup #{$minkey} to cup #{$maxkey}.";
}
?>

<?php

while (FALSE !== ($line = fgets(STDIN))) {

    $stat = array();
    $unrecoverableFlag = false;

    for($i = 1; $i <= $line; $i++){
        $cupValue = fgets(STDIN);

        if($unrecoverableFlag === true){
            continue;
        }

        $cupValue = trim($cupValue);

        if($stat[$cupValue]){
            $stat[$cupValue][0] = $i;    //line number
            $stat[$cupValue][1]++;       //count
        }else{
            $stat[$cupValue] = array($i, 1);
        }

        if(count($stat) > 3) {
            $unrecoverableFlag = true;
        }
    } //end of input taking and general statistics part ..

    $count = count($stat);

    if($unrecoverableFlag){
        echo 'Unrecoverable configuration.' . PHP_EOL;
    }

    if($count == 2){
        if($line == 2){
            //very special case ..
            $sorted = array_keys($stat);
            sort($sorted);
            $cupOneValue = $sorted[0];
            $cupTwoValue = $sorted[1];

            if( $stat[$cupOneValue][1] == 1 && $stat[$cupTwoValue][1] == 1){

                $diff = $cupTwoValue - $cupOneValue;
                if($diff % 2 == 1){
                    echo 'Unrecoverable configuration.' . PHP_EOL;
                }else{
                    $volum = $diff / 2;

                    echo $volum . ' ml. from cup #' . $stat[$cupOneValue][0] .
                       ' to cup #' . $stat[$cupTwoValue][0] . '.' . PHP_EOL;
                }

            }else{
                echo 'Unrecoverable configuration.' . PHP_EOL;
            }
        }else{
            echo 'Unrecoverable configuration.' . PHP_EOL;
        }
    }

    if($count == 1) {
        echo 'Exemplary pages.' . PHP_EOL;
    }

    if($count == 3){

        $sorted = array_keys($stat);

        sort($sorted);

        $cupOneValue = $sorted[0];
        $cupTwoValue = $sorted[2];

        if( $stat[$cupOneValue][1] == 1 && $stat[$cupTwoValue][1] == 1){

            $diff = $cupTwoValue - $cupOneValue;

            if($diff % 2 == 1){
                echo 'Unrecoverable configuration.' . PHP_EOL;
            }else{
                $volum = $diff / 2;

                echo $volum . ' ml. from cup #' . $stat[$cupOneValue][0] .
                   ' to cup #' . $stat[$cupTwoValue][0] . '.' . PHP_EOL;
            }

        }else{
            echo 'Unrecoverable configuration.' . PHP_EOL;
        }
    }
}


Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks.Sergey totally forgot about the task until half an hour before the next lesson and hastily scribbled something down. But then he recollected that in the last lesson he learned the grammar of N-ish. The spelling rules state that N-ish contains some "forbidden" pairs of letters: such letters can never occur in a sentence next to each other. Also, the order of the letters doesn't matter (for example, if the pair of letters "ab" is forbidden, then any occurrences of substrings "ab" and "ba" are also forbidden). Also, each pair has different letters and each letter occurs in no more than one forbidden pair.Now Sergey wants to correct his sentence so that it doesn't contain any "forbidden" pairs of letters that stand next to each other. However, he is running out of time, so he decided to simply cross out some letters from the sentence. What smallest number of letters will he have to cross out? When a letter is crossed out, it is "removed" so that the letters to its left and right (if they existed), become neighboring. For example, if we cross out the first letter from the string "aba", we get the string "ba", and if we cross out the second letter, we get "aa".

The first line contains a non-empty string s, consisting of lowercase Latin letters — that's the initial sentence in N-ish, written by Sergey. The length of string s doesn't exceed 105. The next line contains integer k (0 ≤ k ≤ 13) — the number of forbidden pairs of letters. Next k lines contain descriptions of forbidden pairs of letters. Each line contains exactly two different lowercase Latin letters without separators that represent the forbidden pairs. It is guaranteed that each letter is included in no more than one pair.

Print the single number — the smallest number of letters that need to be removed to get a string without any forbidden pairs of neighboring letters. Please note that the answer always exists as it is always possible to remove all letters.

NoteIn the first sample you should remove two letters b.In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution.

<?php

function solve(){
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_s = $Token->nextToken();
	$inp_arr1 = str_split($inp_s);
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_k = $Token->nextInt();
	$c = 0;
	$temp_c = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$Token = new Token(rtrim(fgets(STDIN)), " ");
		$inp_pair = $Token->nextToken();
		$inp_arr2 = str_split($inp_pair);
		$inp_leftc = $inp_arr2[0];
		$inp_right = $inp_arr2[1];
		$len = count($inp_arr1);

		//$temp_c = 0;
		$temp_c_lelfc = 0;
		$temp_c_rightc = 0;
		$temp_flag = 0;
		for ($j = 0; $j < $len; $j++) {
			if ($inp_arr1[$j] === $inp_leftc) {
				$temp_c_lelfc++;
				$temp_flag = 0;
			} elseif ($inp_arr1[$j] === $inp_right) {
				$temp_c_rightc++;
				$temp_flag = 0;
			} else {
				$temp_flag = 1;
			}

			if (($temp_c_lelfc !== 0 && $temp_flag === 1) || ($temp_c_rightc !== 0 && $temp_flag === 1)|| $j === $len - 1) {
				if ($temp_c_lelfc !== 0 && $temp_c_rightc !== 0) {
					$temp_c += min($temp_c_lelfc, $temp_c_rightc);
				}
				//$temp_flag = 0;
				$temp_c_lelfc = 0;
				$temp_c_rightc = 0;
			}

		}
	}
	printf("%d", $temp_c);

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $_

class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}
/*
	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
}

/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php
error_reporting(0);

function solve(){

	$Scanner = new Scanner("php://stdin");
	$inp_s = $Scanner->Scan();
	$inp_arr1 = str_split($inp_s);
	$inp_k = $Scanner->Scan();
	$c = 0;
	$temp_c = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_pair = $Scanner->Scan();
		$inp_arr2 = str_split($inp_pair);
		$inp_leftc = $inp_arr2[0];
		$inp_right = $inp_arr2[1];
		$len = count($inp_arr1);

		$temp_c_lelfc = 0;
		$temp_c_rightc = 0;
		$temp_flag = 0;
		for ($j = 0; $j < $len; $j++) {
			if ($inp_arr1[$j] === $inp_leftc) {
				$temp_c_lelfc++;
				$temp_flag = 0;
			} elseif ($inp_arr1[$j] === $inp_right) {
				$temp_c_rightc++;
				$temp_flag = 0;
			} else {
				$temp_flag = 1;
			}

			if (($temp_c_lelfc !== 0 && $temp_flag === 1) || ($temp_c_rightc !== 0 && $temp_flag === 1)|| $j === $len - 1) {
				if ($temp_c_lelfc !== 0 && $temp_c_rightc !== 0) {
					$temp_c += min($temp_c_lelfc, $temp_c_rightc);
				}
				$temp_c_lelfc = 0;
				$temp_c_rightc = 0;
			}

		}
	}
	printf("%d", $temp_c);

	$Scanner->close();

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		/*if(!is_file($opend) && !is_link($opend)){
  			print("file or link err");
  			exit;
		}
		*/
		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);
		$contents = str_replace("\n", " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if(!is_file($opend) && !is_link($opend)){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}

/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php

function solve(){
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_s = $Token->nextToken();
	$inp_arr1 = str_split($inp_s);
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_k = $Token->nextInt();
	$c = 0;
	$temp_c = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$Token = new Token(rtrim(fgets(STDIN)), " ");
		$inp_pair = $Token->nextToken();
		$inp_arr2 = str_split($inp_pair);
		$inp_leftc = $inp_arr2[0];
		$inp_right = $inp_arr2[1];
		$len = count($inp_arr1);

		//$temp_c = 0;
		for ($j = 0; $j < $len; $j++) {
			$temp_c_lelfc = 0;
			$temp_c_rightc = 0;
			if ($inp_arr1[$j] === $inp_leftc) {
				$temp_c_lelfc++;
				$j++;
				while ($j < $len && (($inp_arr1[$j] === $inp_leftc || $inp_arr1[$j] === $inp_right))) {
					if ($inp_arr1[$j] === $inp_leftc) $temp_c_lelfc++;
					if ($inp_arr1[$j] === $inp_right) $temp_c_rightc++;
					$j++;
				}
				if ($temp_c_rightc === 0) continue;
				else $temp_c += min($temp_c_lelfc, $temp_c_rightc);
			} elseif ($inp_arr1[$j] === $inp_right) {
				$temp_c_rightc++;
				$j++;
				while ($j < $len && (($inp_arr1[$j] === $inp_leftc || $inp_arr1[$j] === $inp_right))) {
					if ($inp_arr1[$j] === $inp_leftc) $temp_c_lelfc++;
					if ($inp_arr1[$j] === $inp_right) $temp_c_rightc++;
					$j++;
				}
				if ($temp_c_lelfc === 0) continue;
				else $temp_c += min($temp_c_lelfc, $temp_c_rightc);
			}
		}
	}
	printf("%d", $temp_c);

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $_

class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}
/*
	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
}

/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php

function solve(){
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_s = $Token->nextToken();
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_k = $Token->nextInt();

	$inp_arr1 = str_split($inp_s);
	$temp_counter = 1;
	$temp_point = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$Token = new Token(rtrim(fgets(STDIN)), " ");
		$inp_pair = $Token->nextToken();
		$inp_arr2 = str_split($inp_pair);
		$c = 0;
		$len = count($inp_arr1);
		for ($i = 0; $i < $len; $i++) {
			if ($inp_arr1[i] = $inp_arr2[0] && $inp_arr1[i + 1] = $inp_arr2[1]) {
				$c++;
				$i = $i + 2;
			}
			if ($inp_arr1[i + 1] = $inp_arr2[0] && $inp_arr1[i] = $inp_arr2[1]) {
				$c++;
				$i = $i + 2;
			}
		}
	}

	printf("%d", $c);

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $_

class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}
/*
	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
}

/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php

function solve(){
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_s = $Token->nextToken();
	$inp_arr1 = str_split($inp_s);
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_k = $Token->nextInt();
	$c = 0;
	$temp_c = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$Token = new Token(rtrim(fgets(STDIN)), " ");
		$inp_pair = $Token->nextToken();
		$inp_arr2 = str_split($inp_pair);
		$inp_leftc = $inp_arr2[0];
		$inp_right = $inp_arr2[1];
		$len = count($inp_arr1);

		//$temp_c = 0;
		$temp_flag = 0;
		for ($j = 0; $j < $len; $j++) {
			$temp_c_lelfc = 0;
			$temp_c_rightc = 0;
			if ($inp_arr1[$j] === $inp_leftc) {
				$temp_c_lelfc++;
				$temp_flag = 1;
			} elseif ($inp_arr1[$j] === $inp_right) {
				$temp_c_rightc++;
				$temp_flag = 1;
			} else {
				if ($temp_flag == 1) {
					if ($temp_c_lelfc !== 0 && $temp_c_rightc !== 0) {
						$temp_c += min($temp_c_lelfc, $temp_c_rightc);
					}
				}
				$temp_flag = 0;
			}
		}
	}
	printf("%d", $temp_c);

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $_

class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}
/*
	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
}

/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php

function solve(){
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_s = $Token->nextToken();
	$inp_arr1 = str_split($inp_s);
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_k = $Token->nextInt();
	$c = 0;
	$temp_c = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$Token = new Token(rtrim(fgets(STDIN)), " ");
		$inp_pair = $Token->nextToken();
		$inp_arr2 = str_split($inp_pair);
		$inp_leftc = $inp_arr2[0];
		$inp_right = $inp_arr2[1];
		$len = count($inp_arr1);

		//$temp_c = 0;
		$temp_c_lelfc = 0;
		$temp_c_rightc = 0;
		$temp_flag = 0;
		for ($j = 0; $j < $len; $j++) {
			if ($inp_arr1[$j] === $inp_leftc) {
				$temp_c_lelfc++;
				$temp_flag = 0;
			} elseif ($inp_arr1[$j] === $inp_right) {
				$temp_c_rightc++;
				$temp_flag = 0;
			} else {
				$temp_flag = 1;
			}
			if (($temp_c_lelfc !== 0 && $temp_flag === 1) || ($temp_c_rightc !== 0 && $temp_flag === 1)|| $i === $inp_k - 1) {
				if ($temp_c_lelfc !== 0 && $temp_c_rightc !== 0) {
					$temp_c += min($temp_c_lelfc, $temp_c_rightc);
				}
				//$temp_flag = 0;
				$temp_c_lelfc = 0;
				$temp_c_rightc = 0;
			}

		}
	}
	printf("%d", $temp_c);

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $_

class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}
/*
	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
}

/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php

function solve(){
	$Scanner = new Scanner("php://stdin");
	$inp_s = $Scanner->Scan();
	$inp_arr1 = str_split($inp_s);
	$inp_k = (int)$Scanner->Scan();
	$c = 0;
	$temp_c = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_pair = $Scanner->Scan();
		$inp_arr2 = str_split($inp_pair);
		$inp_leftc = $inp_arr2[0];
		$inp_right = $inp_arr2[1];
		$len = count($inp_arr1);

		//$temp_c = 0;
		$temp_c_lelfc = 0;
		$temp_c_rightc = 0;
		$temp_flag = 0;
		for ($j = 0; $j < $len; $j++) {
			if ($inp_arr1[$j] === $inp_leftc) {
				$temp_c_lelfc++;
				$temp_flag = 0;
			} elseif ($inp_arr1[$j] === $inp_right) {
				$temp_c_rightc++;
				$temp_flag = 0;
			} else {
				$temp_flag = 1;
			}

			if (($temp_c_lelfc !== 0 && $temp_flag === 1) || ($temp_c_rightc !== 0 && $temp_flag === 1)|| $j === $len - 1) {
				if ($temp_c_lelfc !== 0 && $temp_c_rightc !== 0) {
					$temp_c += min($temp_c_lelfc, $temp_c_rightc);
				}
				//$temp_flag = 0;
				$temp_c_lelfc = 0;
				$temp_c_rightc = 0;
			}

		}
	}
	printf("%d", $temp_c);

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $_

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "1230942 laurenceHR"
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $LineId_From_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanId_Of_LineIds = array();
	private $scan_number = 0;
	private $scanline_number = 0;

	public function __construct($opened){
		$this->Stream = fopen($opened, 'r');
		$contents = stream_get_contents($this->stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;

			$ttemp_line_scanners = explode(" ",$as_line);
			$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineId_From_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanId_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}

		$this->Scanners  = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->scan_number;
		$y = $this->scanline_number;
		if ($this->LineId_From_ScanIds[$x] === false || $this->LastScanId_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		$this->scan_number = $x++;
		$this->scanline_number = $this->LineId_From_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->scan_number;
		$y = $this->scanline_number;
		if ($this->LineId_From_ScanIds[$x] === false || $this->LastScanId_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->scan_number = $this->LastScanId_Of_LineIds[$y] + 1;
		$this->scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->stream);
	}
}

/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php

function solve(){
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_s = $Token->nextToken();
	$inp_arr1 = str_split($inp_s);
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_k = $Token->nextInt();
	$c = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$Token = new Token(rtrim(fgets(STDIN)), " ");
		$inp_pair = $Token->nextToken();
		$inp_arr2 = str_split($inp_pair);
		$len = count($inp_arr1);
		//for ($i = 0; $i < $len; $i++) {
		for ($j = 0; $j < $len; $j++) {
			//if ($inp_arr1[i] = $inp_arr2[0] && $inp_arr1[i + 1] = $inp_arr2[1]) {
			if ($inp_arr1[$j] === $inp_arr2[0] && $inp_arr1[$j + 1] === $inp_arr2[1]) {
				$c++;
				//$i = $i + 2;
				$j = $j + 2;
			//} elseif ($inp_arr1[$j + 1] = $inp_arr2[0] && $inp_arr1[$j] = $inp_arr2[1]) {
	} elseif ($inp_arr1[$j + 1] === $inp_arr2[0] && $inp_arr1[$j] === $inp_arr2[1]) {
				$c++;
				//$i = $i + 2;
				$j = $j + 2;
			}
		}
	}

	printf("%d", $c);

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $_

class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}
/*
	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
}

/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php
error_reporting(0);

function solve(){

	$Scanner = new Scanner("php://stdin");
	$inp_s = $Scanner->Scan();
	$inp_arr1 = str_split($inp_s);
	$inp_k = $Scanner->Scan();
	$c = 0;
	$temp_c = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$inp_pair = $Scanner->Scan();
		$inp_arr2 = str_split($inp_pair);
		$inp_leftc = $inp_arr2[0];
		$inp_right = $inp_arr2[1];
		$len = count($inp_arr1);

		$temp_c_lelfc = 0;
		$temp_c_rightc = 0;
		$temp_flag = 0;
		for ($j = 0; $j < $len; $j++) {
			if ($inp_arr1[$j] === $inp_leftc) {
				$temp_c_lelfc++;
				$temp_flag = 0;
			} elseif ($inp_arr1[$j] === $inp_right) {
				$temp_c_rightc++;
				$temp_flag = 0;
			} else {
				$temp_flag = 1;
			}

			if (($temp_c_lelfc !== 0 && $temp_flag === 1) || ($temp_c_rightc !== 0 && $temp_flag === 1)|| $j === $len - 1) {
				if ($temp_c_lelfc !== 0 && $temp_c_rightc !== 0) {
					$temp_c += min($temp_c_lelfc, $temp_c_rightc);
				}
				$temp_c_lelfc = 0;
				$temp_c_rightc = 0;
			}

		}
	}
	printf("%d", $temp_c);

	$Scanner->close();

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $(Upper)

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
class Scanner{
	private $Stream;
	private $Scanners = array();
	private $Id = 0;

	public function __construct($opend){
		if(!is_file($opend) && !is_link($opend)){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);
		$contents = str_replace("\n", " ", $contents);
		$this->Scanners = explode(" ", $contents);
	}

	function Scan(){
		$x = $this->Id;
		$sc = $this->Scanners[$x];
		$this->Id = ++$x;
		return $sc;
	}

	function close(){
		fclose($this->Stream);
	}
}

//error_reporting(0);
//$Scanner = new Scanner("php://stdin");
//ref "#109_div2_B_1230942 laurenceHR"
class LineScanner{
	private $Stream;
	private $Scanners = array();
	private $LineIds_Of_ScanIds= array();
	private $LinesScanners = array();
	private $LastScanIds_Of_LineIds = array();
	private $Scan_number = 0;
	private $Scanline_number = 0;

	public function __construct($opend){
		if(!is_file($opend) && !is_link($opend)){
  			print("file or link err");
  			exit;
		}

		$this->Stream = fopen($opend, 'r');
		$contents = stream_get_contents($this->Stream);

		$temp_lines = explode("\n", $contents);
		$temp_linescanners = array();
		$temp_x = 0;
		$temp_y = 0;
		//--attention for initting of array--(these is not needed but for easily reading)//
		$ttemp_scanners = array();
		//$del = Array("\n","\r","\n\r");
		foreach ($temp_lines as $as_line) {
			if (strlen($as_line) === 0) {
				print("input err");
			}

			//$as_line = str_replace($del,'',$as_line);
			$temp_linescanners[] = $as_line;
			$ttemp_line_scanners = explode(" ", $as_line);
			//$ttemp_scanners = array();
			foreach ($ttemp_line_scanners as $as_sc) {
				$ttemp_scanners[] = $as_sc;
				$this->LineIds_Of_ScanIds[$temp_x] = $temp_y;
				$temp_x++;
			}
			$this->LastScanIds_Of_LineIds[$temp_y] = $temp_x - 1;
			$temp_y++;
		}
		$this->Scanners = $ttemp_scanners;
		$this->LinesScanners = $temp_linescanners;
	}

	function Scan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("scan err");
		}

		$sc = $this->Scanners[$x];
		//--attention for ++$x//
		//$this->Scan_number = $x++;
		$this->Scan_number = ++$x;
		$this->Scanline_number = $this->LineIds_Of_ScanIds[$x];
		return $sc;
	}

	function LineScan(){
		$x = $this->Scan_number;
		$y = $this->Scanline_number;
		if ($this->LineIds_Of_ScanIds[$x] === false || $this->LastScanIds_Of_LineIds[$y] === false) {
			print("linescan err");
		}

		$lsc = $this->LinesScanners[$y];
		$this->Scan_number = $this->LastScanIds_Of_LineIds[$y] + 1;
		$this->Scanline_number = $y + 1;
		return $lsc;
	}

	function close(){
		fclose($this->Stream);
	}
}

/*
class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}

	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php

function solve(){
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_s = $Token->nextToken();
	$inp_arr1 = str_split($inp_s);
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_k = $Token->nextInt();
	$c = 0;
	$temp_c = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$Token = new Token(rtrim(fgets(STDIN)), " ");
		$inp_pair = $Token->nextToken();
		$inp_arr2 = str_split($inp_pair);
		$inp_leftc = $inp_arr2[0];
		$inp_right = $inp_arr2[1];
		$len = count($inp_arr1);

		//$temp_c = 0;
		$temp_flag1 = 0;
		$temp_flag2 = 0;
		for ($j = 0; $j < $len; $j++) {
			if ($inp_arr1[$j] === $inp_leftc) {
				$temp_c_lelfc++;
				$temp_flag1 = 1;
				$temp_flag2 = 0;
			} elseif ($inp_arr1[$j] === $inp_right) {
				$temp_c_rightc++;
				$temp_flag1 = 1;
				$temp_flag2 = 0;
			} else {
				$temp_flag2 = 1;
			}
			if (($temp_flag1 === 1 && $temp_flag2 = 1 || $i === $inp_k - 1)) {
				if ($temp_c_lelfc !== 0 && $temp_c_rightc !== 0) {
					$temp_c += min($temp_c_lelfc, $temp_c_rightc);
				}
				$temp_flag1 = 0;
				$temp_flag2 = 0;
				$temp_c_lelfc = 0;
				$temp_c_rightc = 0;
			}

		}
	}
	printf("%d", $temp_c);

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $_

class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}
/*
	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
}

/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php

function solve(){
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_s = $Token->nextToken();
	$inp_arr1 = str_split($inp_s);
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_k = $Token->nextInt();
	$c = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$Token = new Token(rtrim(fgets(STDIN)), " ");
		$inp_pair = $Token->nextToken();
		$inp_arr2 = str_split($inp_pair);
		$inp_leftc = $inp_arr2[0];
		$inp_right = $inp_arr2[1];
		$len = count($inp_arr1);
		$temp_c_lelfc = 0;
		$temp_c_rightc = 0;
		$temp_c = 0;
		for ($j = 0; $j < $len; $j++) {
			if ($inp_arr1[$j] === $inp_leftc) {
				$temp_c_lelfc++;
				while ($j < $len && (($inp_arr1[$j] === $inp_leftc || $inp_arr1[$j] === $inp_right))) {
					$j++;
					if ($inp_arr1[$j] === $inp_leftc) $temp_c_lelfc++;
					if ($inp_arr1[$j] === $inp_right) $temp_c_rightc++;
				}
				if ($temp_c_rightc === 0) continue;
				else $temp_c += min($temp_c_lelfc, $temp_c_rightc);
			} elseif ($inp_arr1[$j] === $inp_right) {
				$temp_c_rightc++;
				while ($j < $len && (($inp_arr1[$j] === $inp_leftc || $inp_arr1[$j] === $inp_right))) {
					$j++;
					if ($inp_arr1[$j] === $inp_leftc) $temp_c_lelfc++;
					if ($inp_arr1[$j] === $inp_right) $temp_c_rightc++;
				}
				if ($temp_c_lelfc === 0) continue;
				else $temp_c += min($temp_c_lelfc, $temp_c_rightc);
			}
		}
	}
	printf("%d", $temp_c);

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $_

class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}
/*
	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
}

/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php

function solve(){
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_s = $Token->nextToken();
	$inp_arr1 = str_split($inp_s);
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_k = $Token->nextInt();
	$c = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$Token = new Token(rtrim(fgets(STDIN)), " ");
		$inp_pair = $Token->nextToken();
		$inp_arr2 = str_split($inp_pair);
		$inp_leftc = $inp_arr2[0];
		$inp_right = $inp_arr2[1];
		$len = count($inp_arr1);

		$temp_c = 0;
		for ($j = 0; $j < $len; $j++) {
			$temp_c_lelfc = 0;
			$temp_c_rightc = 0;
			if ($inp_arr1[$j] === $inp_leftc) {
				$temp_c_lelfc++;
				while ($j < $len && (($inp_arr1[$j] === $inp_leftc || $inp_arr1[$j] === $inp_right))) {
					$j++;
					if ($inp_arr1[$j] === $inp_leftc) $temp_c_lelfc++;
					if ($inp_arr1[$j] === $inp_right) $temp_c_rightc++;
				}
				if ($temp_c_rightc === 0) continue;
				else $temp_c += min($temp_c_lelfc, $temp_c_rightc);
			} elseif ($inp_arr1[$j] === $inp_right) {
				$temp_c_rightc++;
				while ($j < $len && (($inp_arr1[$j] === $inp_leftc || $inp_arr1[$j] === $inp_right))) {
					$j++;
					if ($inp_arr1[$j] === $inp_leftc) $temp_c_lelfc++;
					if ($inp_arr1[$j] === $inp_right) $temp_c_rightc++;
				}
				if ($temp_c_lelfc === 0) continue;
				else $temp_c += min($temp_c_lelfc, $temp_c_rightc);
			}
		}
	}
	printf("%d", $temp_c);

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $_

class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}
/*
	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
}

/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>

<?php

function solve(){
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_s = $Token->nextToken();
	$inp_arr1 = str_split($inp_s);
	$Token = new Token(rtrim(fgets(STDIN)), " ");
	$inp_k = $Token->nextInt();
	$c = 0;
	$temp_c = 0;
	for ($i = 0; $i < $inp_k; $i++) {
		$Token = new Token(rtrim(fgets(STDIN)), " ");
		$inp_pair = $Token->nextToken();
		$inp_arr2 = str_split($inp_pair);
		$inp_leftc = $inp_arr2[0];
		$inp_right = $inp_arr2[1];
		$len = count($inp_arr1);

		//$temp_c = 0;
		$temp_c_lelfc = 0;
		$temp_c_rightc = 0;
		$temp_flag = 0;
		for ($j = 0; $j < $len; $j++) {
			if ($inp_arr1[$j] === $inp_leftc) {
				$temp_c_lelfc++;
				$temp_flag = 0;
			} elseif ($inp_arr1[$j] === $inp_right) {
				$temp_c_rightc++;
				$temp_flag = 0;
			} else {
				$temp_flag = 1;
			}

			if (($temp_c_lelfc !== 0 && $temp_flag === 1) || ($temp_c_rightc !== 0 && $temp_flag === 1)|| $i === $len - 1) {
				if ($temp_c_lelfc !== 0 && $temp_c_rightc !== 0) {
					$temp_c += min($temp_c_lelfc, $temp_c_rightc);
				}
				//$temp_flag = 0;
				$temp_c_lelfc = 0;
				$temp_c_rightc = 0;
			}

		}
	}
	printf("%d", $temp_c);

}

function run(){
	solve();
}

ob_end_clean();
run();

//input -  $inp_
//block    $temp_
//two block$ttemp_
//as 	   $as_
//instance $(Upper)
//ref      $ref
//class menba $_

class Token{
	private $token;
	private $tok;

	public function __construct($str_line, $tok) {
		$this->tok = $tok;
		$this->token = strtok($str_line, $this->tok);
	}

	public function nextToken() {
		$thisToken = $this->token;
		$this->token = strtok($this->tok);
		return $thisToken;
	}

	public function nextFloat() {
		return (float)$this->nextToken();
	}

  	public function nextInt() {
		return (int)$this->nextToken();
	}

	public function nextIntArray() {
		$temp_arr = array();
		$flag = -1;
		while ($flag !== 1) {
			$thisToken = $this->nextToken();
			if ($thisToken !== false) {
				$temp_arr[] = $thisToken;
			} else {
				$flag = 1;
			}
		}
		return $temp_arr;
	}
/*
	public function nnextIntArray($num) {
		$temp_arr = array();
		for ($i = 0; $i < $num; $i++) {
			$temp_arr[] = (int)$this->token;
		}
		return $temp_arr;
	}
}
*/
}

/*
function get_line() {
        $stdin = fopen("php://stdin", "r");
        $line = rtrim($stdin, "\n");
        fclose($stdin);
        return $line;
}
*/

function num_printf($i){
	if (is_float($i)) {
		printf("%.10f", $i);
	}
	if (is_int($i)) {
		printf("%10d", $i);
	}
}

function array_printf(array $array) {
		$message = "";
		foreach ($array as $value) {
			$message .= "$value" . " ";
		}
		$message = rtrim($message);
		printf('%s', $message);
}
?>


This is an interactive problem. You should use flush operation after each printed line. For example, in C++ you should use fflush(stdout), in Java you should use System.out.flush(), and in Pascal — flush(output).In this problem you should guess an array a which is unknown for you. The only information you have initially is the length n of the array a.The only allowed action is to ask the sum of two elements by their indices. Formally, you can print two indices i and j (the indices should be distinct). Then your program should read the response: the single integer equals to ai + aj.It is easy to prove that it is always possible to guess the array using at most n requests.Write a program that will guess the array a by making at most n requests.

NoteThe format of a test to make a hack is:  The first line contains an integer number n (3 ≤ n ≤ 5000) — the length of the array. The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 105) — the elements of the array to guess.

<?php
$a = trim(fgets(STDIN));
$b = array("!");
for($x = 1; $x <= $a; $x++)
{
     if($x == 1)
     {
          print "? 1 2\n";
          flush();
          $c = trim(fgets(STDIN));
     }
     elseif($x == 2)
     {
          print "? 2 3\n";
          flush();
          $d = trim(fgets(STDIN));
     }
     elseif($x == 3)
     {
          print "? 1 3\n";
          flush();
          $e = trim(fgets(STDIN));
          $f = ($c - $d - $e) / -2;
          $g = $e - $f;
          $h = $d - $f;
          array_push($b, $g);
          array_push($b, $h);
          array_push($b, $f);
     }
     else
     {
          print "? 1 " . $x . "\n";
          flush();
          $i = trim(fgets(STDIN));
          $j = $i - $g;
          $b[$x] = $j;
     }
}
print implode(" ", $b);
?>

<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     print "? 1 2\n";
     flush();
     $b = trim(fgets(STDIN));
}
print "! 1 2 3 4 5\n";
?>

<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     print "? 1 2\n";
     flush();
     $b = trim(fgets(STDIN));
}
print "! 1 2 3 4 5";
?>

<?php
$a = trim(fgets(STDIN));
$b = array("!");
for($x = 1; $x <= $a; $x++)
{
     if($x == 1)
     {
          print "? 1 2\n";
          flush();
          $c = trim(fgets(STDIN));
     }
     elseif($x == 2)
     {
          print "? 2 3\n";
          flush();
          $d = trim(fgets(STDIN));
     }
     elseif($x == 3)
     {
          print "? 1 3\n";
          flush();
          $e = trim(fgets(STDIN));
          $f = ($c - $d - $e) / -2;
          $g = $d - $f;
          $h = $e - $f;
          array_push($b, $g);
          array_push($b, $h);
          array_push($b, $f);
     }
     else
     {
          print "? 1 " . $x . "\n";
          flush();
          $i = trim(fgets(STDIN));
          $j = $i - $g;
          $b[$x] = $j;
     }
}
print implode(" ", $b);
?>

<?php
$a = trim(fgets(STDIN));
$b = array("!");
for($x = 1; $x <= $a; $x++)
{
     if($x == 1)
     {
          print "? 1 2\n";
          flush();
          $c = trim(fgets(STDIN));
     }
     elseif($x == 2)
     {
          print "? 2 3\n";
          flush();
          $d = trim(fgets(STDIN));
     }
     elseif($x == 3)
     {
          print "? 1 3\n";
          flush();
          $e = trim(fgets(STDIN));
          $f = ($c - $d - $e) / -2;
          $g = $d - $f;
          $h = $e - $f;
          array_push($b, $g);
          array_push($b, $h);
          array_push($b, $f);
     }
     else
     {
          print "? 1 " . $x . "\n";
          flush();
          $i = trim(fgets(STDIN));
          $j = $i - $g;
          $b[$x] = $j;
     }
}
print implode(" ", $b);
flush();
?>

<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     print "? 1 2\n";
     flush();
     $b = trim(fgets(STDIN));
}
print "! 1 2 3 4 5";
flush();
?>

<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     print "? 1 2";
     print "\n";
     flush();
     $b = trim(fgets(STDIN));
}
print "! 1 2 3 4 5";
?>

<?php
$a = 0;
$b = 0;
print "2\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
     $b++;
}
print "3\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
     $b++;
}
print "4\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
}
print "5\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
     $b++;
}
print "7\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
     $b++;
}
print "9\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
}
print "11\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
}
print "13\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
}
print "17\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
}
print "19\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
}
print "23\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
}
print "25\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
}
print "29\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
}
print "31\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
}
print "37\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
}
print "41\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
}
print "43\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
}
print "47\n";
flush();
$c = trim(fgets(STDIN));
if($c == "yes")
{
     $a++;
}
if(($b == 1) && ($a == 1))
{
     print "prime";
     flush();
}
elseif($a <= 1)
{
     print "prime";
     flush();
}
elseif($a > 1)
{
     print "composite";
     flush();
}
?>