<?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();
	$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

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


<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = range(1, $a);
$d = array_slice($c, 0, -$b);
$e = array_slice($c, -$b);
$f = array_reverse($e);
$g = array_merge($f, $d);
if($b == 0)
{
    print implode(" ", $c);
}
else
{
    print implode(" ", $g);
}
?>

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


<?php
list($a, $b, $c, $d, $e, $f) = explode(" ", trim(fgets(STDIN)));
$i = array();
$j = array();
$k = array();
$l = array();
$p = array();
$s = array();
$u = array();
for($x = 0; $x < $a; $x++)
{
    list($g, $h) = explode(" ", trim(fgets(STDIN)));
    array_push($i, $h - $g);
    array_push($j, $g);
    array_push($k, $h);
    array_push($l, $j[$x] - $k[$x - 1]);
}
$l = array_splice($l, 1);
$m = array_sum($i);
$o = $m * $b; // нормальный режим
for($x = 0; $x < count($l); $x++)
{
    if($l[$x] - $e > 0)
    {
        $n = $e * $b; // нормальный режим
        array_push($p, $n);
        if($l[$x] - $e - $f > 0)
        {
            $r = $f * $c; // скринсейвер
            array_push($s, $r);
            $t = ($l[$x] - $e - $f) * $d; // спящий режим
            array_push($u, $t);
        }
        else
        {
            $r = ($l[$x] - $e) * $c; // скринсейвер
            array_push($s, $r);
        }
    }
    else
    {
        $n = $l[$x] * $b; // нормальный режим
        array_push($p, $n);
    }
}
$v = array_sum($p);
$q = array_sum($s);
$w = array_sum($u);
print $o + $v + $q + $w;
?>

<?php
list($a, $b, $c, $d, $e, $f) = explode(" ", trim(fgets(STDIN)));
$i = array();
$j = array();
$k = array();
$l = array();
$p = array();
$s = array();
$u = array();
for($x = 0; $x < $a; $x++)
{
    list($g, $h) = explode(" ", trim(fgets(STDIN)));
    array_push($i, $h - $g);
    array_push($j, $g);
    array_push($k, $h);
    array_push($l, $j[$x] - $k[$x - 1]);
}
$l = array_splice($l, 1);
$m = array_sum($i);
$o = $m * $b; // нормальный режим
for($x = 0; $x < count($l); $x++)
{
    if($l[$x] - $e > 0)
    {
        $n = $e * $b; // нормальный режим
        array_push($p, $n);
        if($l[$x] - $e - $f > 0)
        {
            $r = $f * $c; // скринсейвер
            array_push($s, $r);
            $t = ($l[$x] - $e - $f) * $d; // спящий режим
            array_push($u, $t);
        }
        else
        {
            $r = $f * $c; // скринсейвер
            array_push($s, $r);
        }
    }
    else
    {
        $n = $l[$x] * $b; // нормальный режим
        array_push($p, $n);
    }
}
$v = array_sum($p);
$q = array_sum($s);
$w = array_sum($u);
print $o + $v + $q + $w;
?>


<?php
$a = trim(fgets(STDIN));
$b = array();
for($x = 1; $x <= $a; $x++)
{
    $b[$x] = $x;
}
$c = array();
$d = array();
for($x = 1; $x <= $a; $x++)
{
    $e = $x % 2;
    if($e != 0)
    {
        array_push($c, $b[$x]);
    }
    else
    {
        array_push($d, $b[$x]);
    }
}
$f = array_merge($c, $d);
if(count($f) == 2)
{
    unset($f[count($f) - 1]);
}
elseif(count($f) == 3)
{
    unset($f[count($f) - 1]);
}
elseif(count($f) == 4)
{
    array_unshift($f, $f[count($f) - 1]);
    unset($f[4]);
    array_unshift($f, $f[count($f) - 1]);
    unset($f[4]);
}
print count($f) . "\n";
print implode(" ", $f);
?>

<?php

fscanf(STDIN, "%d", $size); // читает число из STDIN

$data = array();

if($size >= 5) {
    $data = range(1, $size);
    $result = array();

    $head = array();
    $tail = array();

    $i = 1;
    foreach($data as $elem) {
        if($i % 2 == 0) {
          $head[] = $elem;
        } else {
          $tail[] = $elem;
        }
        $i++;
    }
    $result = array_merge($head, $tail);
} else {
    if($size == 1 || $size == 2) {
        $result = array(1);
    } else {
        $result = range(1, $size);
        if($size == 4) {
            $result = array(2, 4, 1, 3);
        } elseif ($size == 3) {
            $result = array(1, 3);
        }

    }
}
echo count($result) . "\n";
array_map(function($e) {echo $e . " ";}, $result);

<?php
$z = fopen('php://stdin','r');
$n = intval(fgets($z));
switch ($n) {
    case 1: case 2: echo "1\n1"; break;
    case 3: echo "2\n1 3"; break;
    default:
        echo "$n\n";
        for ($i = 2; $i <= $n; $i+=2) echo "$i ";
        for ($i = 1; $i <= $n; $i+=2) echo "$i ";
        break;
}

<?php
$a = trim(fgets(STDIN));
$b = array();
for($x = 1; $x <= $a; $x++)
{
    $b[$x] = $x;
}
$c = array();
$d = array();
for($x = 1; $x <= $a; $x++)
{
    $e = $x % 2;
    if($e != 0)
    {
        array_push($c, $b[$x]);
    }
    else
    {
        array_push($d, $b[$x]);
    }
}
$f = array_merge($c, $d);
if(count($f) == 2)
{
    unset($f[count($f) - 1]);
}
elseif(count($f) == 3)
{
    unset($f[count($f) - 1]);
}
elseif(count($f) == 4)
{
    array_unshift($f, $f[count($f) - 1]);
    unset($f[3]);
    unset($f[4]);
}
print count($f) . "\n";
print implode(" ", $f);
?>

fscanf(STDIN, "%d", $size); // читает число из STDIN

$data = [];

if($size >= 5) {
        $data = range(1, $size);
        $result = [];

        $head = [];
        $tail = [];

        $i = 1;
        foreach($data as $elem) {
                if($i % 2 == 0) {
                  $head[] = $elem;
                } else {
                  $tail[] = $elem;
                }
                $i++;
        }
        $result = array_merge($head, $tail);
} else {
        if($size == 1 || $size == 2) {
                $result = [1];
        } else {
                $result = range(1, $size);
                if($size == 4) {
                        $result = [4, 1, 3];
                } elseif ($size == 3) {
                        $result = [1, 3];
                }
        }
}
echo count($result) . "\n";
array_map(function($e) {echo $e . " ";}, $result);
~
~
~

fscanf(STDIN, "%d", $size); // читает число из STDIN

$data = [];

if($size >= 5) {
    $data = range(1, $size);
    $result = [];

    $head = [];
    $tail = [];

    $i = 1;
    foreach($data as $elem) {
        if($i % 2 == 0) {
          $head[] = $elem;
        } else {
          $tail[] = $elem;
        }
        $i++;
    }
    $result = array_merge($head, $tail);
} else {
    if($size == 1) {
        $result = [1];
    } else {
        $result = range(1, $size);
        if($size == 4) {
            $result = [4, 1, 3];
        } elseif ($size == 3 || $size == 2) {
            $result = [1, 3];
        }

    }
}
echo count($result) . "\n";
array_map(function($e) {echo $e . " ";}, $result);

<?php

fscanf(STDIN, "%d", $size); // читает число из STDIN

$data = array();

if($size >= 5) {
    $data = range(1, $size);
    $result = array();

    $head = array();
    $tail = array();

    $i = 1;
    foreach($data as $elem) {
        if($i % 2 == 0) {
          $head[] = $elem;
        } else {
          $tail[] = $elem;
        }
        $i++;
    }
    $result = array_merge($head, $tail);
} else {
    if($size == 1 || $size == 2) {
        $result = array(1);
    } else {
        $result = range(1, $size);
        if($size == 4) {
            $result = array(4, 1, 3);
        } elseif ($size == 3) {
            $result = array(1, 3);
        }

    }
}
echo count($result) . "\n";
array_map(function($e) {echo $e . " ";}, $result);

<?php

fscanf(STDIN, "%d", $size); // читает число из STDIN

$data = array();

if($size >= 5) {
    $data = range(1, $size);
    $result = array();

    $head = array();
    $tail = array();

    $i = 1;
    foreach($data as $elem) {
        if($i % 2 == 0) {
          $head[] = $elem;
        } else {
          $tail[] = $elem;
        }
        $i++;
    }
    $result = array_merge($head, $tail);
} else {
    if($size == 1) {
        $result = array(1);
    } else {
        $result = range(1, $size);
        if($size == 4) {
            $result = array(4, 1, 3);
        } elseif ($size == 3 || $size == 2) {
            $result = array(1, 3);
        }

    }
}
echo count($result) . "\n";
array_map(function($e) {echo $e . " ";}, $result);

<?php
function scanf($format) {
    return fscanf(STDIN, $format);
}

list($n) = scanf("%d");

if ($n == 3) {
    printf("2\n1 3");
    die();
} else if ($n == 4) {
    printf("4\n2 4 1 3");
    die();
}
echo "$n\n";
$x = ceil($n / 2);
$y = floor($n / 2);
if ($n % 2) {
    echo $x . " ";
}
for ($i = 1; $i <= $y; $i ++) {
    echo "$i " . ($i + $x) . " ";
}


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

$a = explode(" ",$a);
$b = explode(" ",$b);
$c = explode(" ",$c);

$inputTime = $a;
$inputFail = $b;
$inputExtraScore = $c;
$defaultScore = [500.0, 1000.0, 1500.0, 2000.0, 2500.0];
$finalScore = 0.0;

foreach($defaultScore as $key => $value){
 $score = calculateScore($key+1, $value, $inputFail[$key], $inputTime[$key]);
 $finalScore += $score;
}
$finalScore = $finalScore + $inputExtraScore[0] * 100;
$finalScore = $finalScore - $inputExtraScore[1] * 50;
echo "$finalScore";

function calculateScore ($num, $defaultScore, $failNum, $time){
   $score = (1-($time/250.0))*$defaultScore - 50.0*$failNum;
   return max((0.3*$defaultScore), $score);
}
?>


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

$loop = true;
$t = $loop ? IO::get() : 1;
while($t--) {
    solve();
}
function solve()
{
     $n = IO::get();
     $a = IO::getArray();
     $ans = 0;
     $tt = [];
    for($i=$n-1; $i>= 0; $i--) {
         if(isset($tt[$a[$i]])) {
              $ans = $i + 1;
              break;
         }
         $tt[$a[$i]] = 1;

    }
    IO::pr( $ans);

}

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

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

<?php
fscanf(STDIN, "%d", $t);
while ($t--) {
    fscanf(STDIN, "%d", $n);
    $a = fgetcsv(STDIN, 0, ' ');
    for ($i = $n-1, $b = []; $i >= 0; $i--) {
        if (isset($b[$a[$i]])) break;
        $b[$a[$i]] = true;
    }
    echo count($a) - count($b), "\n";
}


<?php

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

$t = fgets($fh);

$data = [];

for ($i = 0; $i < $t; $i++)
{
     $line = fgets($fh);
     $left = explode(' ', $line)[0];
     $right = explode(' ', $line)[1];

     if ($right == 1 && $left == 1)
                    $res = 1;
                    else
     if ($right == 1 && $left > 1)
                     $res = ceil($left/2)  ;

     else
     if ($right > 1 && $left == 1)
               $res = ceil($right/2) ;
                    else
     if ($left % 2 == 0)
     {
       $res = ($left/2) * $right;
     }
     else
     {
       $res = (($left -1)/2) * $right + ceil($right/2);
     }

     echo $res. "\n";
}

fclose($fh);

?>

<?php

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

$t = fgets($file);

for ($i = 0; $i < $t; $i++) {
  $pair = explode(" ", fgets($file));
  $n = $pair[0];
  $m = $pair[1];
  $nm = $n * $m;
  $result = $nm % 2 == 0 ? $nm / 2 : ($nm + 1) / 2;
  echo $result . "\n";
}

<?php

$handle = fopen('php://stdin', 'r');
$count = fgets($handle);
for($i = 0; $i < $count; $i++){
    $tmp_string = fgets($handle);
    $tmp_arr = explode(' ', $tmp_string);
    print_r(intval($tmp_arr[0] * $tmp_arr[1] / 2) + ($tmp_arr[0] * $tmp_arr[1] % 2));
    echo "\n";
}

fclose($handle);

?>


<?php

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

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

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

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

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

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

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

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

function getAnswer($is_test = false, $f = false) {
	if ($is_test)
		$stream = $f;
	else
		$stream = STDIN;
	/* begin body */
	$str = readString($stream);

	$preg = '![a-z]{1,10}(?<price>[0-9.]+)!i';
	if (preg_match_all($preg, $str, $res)) {
		$sum = 0;
		foreach ($res['price'] as $price) {
			$parts = explode('.', $price);
			$lastPart = $parts[count($parts) - 1];
			if (strlen($lastPart) == 2) {
				$kop = array_pop($parts);
				$rub = intval(implode('', $parts));
				$add = $rub + $kop / 100;

				$sum+=$add;
			} else {
				$rub = intval(implode('', $parts));
				$add = $rub;
				$sum+=$add;
			}
		}
		$signs = 0;
		if (floor($sum) != $sum)
			$signs = 2;
		echo number_format($sum, $signs, '.', '.');
	} else {
		echo 'invalid';
	}
	/* / end body */
}

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

<?php
$s = file_get_contents("php://stdin");
preg_match_all("/[a-z]+((?:\\d\\d?\\d?)(?:\.\\d\\d\\d)*(?:\.\d\d)?)/", $s, $matches);
$ans = 0;
foreach($matches[1] as $price) {
  if (strlen($price) >= 3 && substr($price, -3, 1) == '.') {
    $ans += str_replace(".", "", $price) / 100;
  } else {
    $ans += str_replace(".", "", $price);
  }
}
$t = str_replace(",", ".", number_format($ans, 2));
if (substr($t, -3, 3) == ".00") {
  echo substr($t, 0, strlen($t) - 3);
} else {
  echo $t;
}

<?php
$line1 = trim(fgets(STDIN));
$itog = preg_replace('/[a-z]+/', '#', $line1);
$mach = explode('#', $itog);
$sum = 0;
$koope = 0;
foreach($mach as $item){
    $reo = explode('.', $item);
    if($reo[0] == ''){
        continue;
    }
    $kop = 0;
    if(strlen($reo[count($reo)-1]) == 2){
        $kop = $reo[count($reo)-1];
        unset($reo[count($reo)-1]);
    }
    $l = 0;
    $chisl = 0;
    while($l <= count($reo)-1){
        $chisl += $reo[$l] * pow(1000, count($reo)-1-$l);
        $l++;
    }
    $sum += $chisl;
    $koope += $kop;
}
$nsum = 0;
if($koope >= 100){
    $nsum = floor($koope/100);
    $koope = $koope % 100;
}
$sum += $nsum;

if($koope == 0){
    echo number_format($sum, 0, ',', '.');
}else{
   if($koope < 10){
    $koope = '.0'.$koope;
}else{
	$koope = '.'.$koope;
}
    echo number_format($sum, 0, ',', '.').$koope;
}

<?php
$line1 = trim(fgets(STDIN));
$itog = preg_replace('/[a-z]+/', '#', $line1);
$mach = explode('#', $itog);
$sum = 0;
$koope = 0;
foreach($mach as $item){
    $reo = explode('.', $item);
    if($reo[0] == ''){
        continue;
    }
    $kop = 0;
    if(strlen($reo[count($reo)-1]) == 2){
        $kop = $reo[count($reo)-1];
        unset($reo[count($reo)-1]);
    }
    $l = 0;
    $chisl = 0;
    while($l <= count($reo)-1){
        $chisl += $reo[$l] * pow(1000, count($reo)-1-$l);
        $l++;
    }
    $sum += $chisl;
    $koope += $kop;
}
$nsum = 0;
if($koope >> 100){
    $nsum = floor($koope/100);
    $koope = $koope % 100;
}
$sum += $nsum;
if($koope < 10){
    $koope = '.0'.$koope;
}else if($koope == 0){
	$koope = '';
}else{
	$koope = '.'.$koope;
}
echo number_format($sum, 0, ',', '.').$koope;

<?php
$line1 = trim(fgets(STDIN));
$itog = preg_replace('/[a-z]+/', '#', $line1);
$mach = explode('#', $itog);
$sum = 0;
$koope = 0;
foreach($mach as $item){
    $reo = explode('.', $item);
    if($reo[0] == ''){
        continue;
    }
    $kop = 0;
    if(strlen($reo[count($reo)-1]) == 2){
        $kop = $reo[count($reo)-1];
        unset($reo[count($reo)-1]);
    }
    $l = 0;
    $chisl = 0;
    while($l <= count($reo)-1){
        $chisl += $reo[$l] * pow(1000, count($reo)-1-$l);
        $l++;
    }
    $sum += $chisl;
    $koope += $kop;
}
$nsum = 0;
if($koope >> 100){
    $nsum = floor($koope/100);
    $koope = $koope % 100;
}
$sum += $nsum;
if($koope < 10){
    $koope = '0'.$koope;
}
echo number_format($sum, 0, ',', '.').'.'.$koope;

<?php
$s = file_get_contents("php://stdin");
preg_match_all("/[a-z]+((?:\\d\\d?\\d?)(?:\.\\d\\d\\d)*(?:\.\d\d)?)/", $s, $matches);
$ans = 0;
foreach($matches[1] as $price) {
  if (strlen($price) >= 3 && substr($price, -3, 1) == '.') {
    $ans += str_replace(".", "", $price) / 100;
  } else {
    $ans += str_replace(".", "", $price);
  }
}
echo str_replace(",", ".", number_format($ans, 2));

<?php
$s = 'chipsy48.32televizor12.390';//file_get_contents("php://stdin");
preg_match_all("/[a-z]+((?:\\d\\d?\\d?)(?:\.\\d\\d\\d)*(?:\.\d\d)?)/", $s, $matches);
$ans = 0;
foreach($matches[1] as $price) {
  if (strlen($price) >= 3 && substr($price, -3, 1) == '.') {
    $ans += str_replace(".", "", $price) / 100;
  } else {
    $ans += str_replace(".", "", $price);
  }
}
echo str_replace(",", ".", number_format($ans, 2));

<?php

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

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

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

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

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

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

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

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

function getAnswer($is_test = false, $f = false) {
	if ($is_test)
		$stream = $f;
	else
		$stream = STDIN;
	/* begin body */
	$str = readString($stream);

	$preg = '![a-z]{1,10}(?<price>[0-9.]+)!i';
	if (preg_match_all($preg, $str, $res)) {
		$sum = 0;
		foreach ($res['price'] as $price) {
			$parts = explode('.', $price);
			$lastPart = $parts[count($parts) - 1];
			if (strlen($lastPart) == 2) {
				$kop = array_pop($parts);
				$rub = intval(implode('', $parts));
				$add = $rub + $kop / 100;

				$sum+=$add;
			} else {
				$rub = intval(implode('', $parts));
				$add = $rub;
				$sum+=$add;
			}
		}
		echo number_format($sum, 2, '.', '.');
	} else {
		echo 'invalid';
	}
	/* / end body */
}

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


<?php
// https://codeforces.com/contest/1729/problem/A

$cin = fopen("php://stdin", "r"); // IO
$cout = fopen("php://stdout", "w"); // IO

$t;
fscanf($cin, "%d\n", $t);
for ($T = 0; $T < $t; $T++) {
	$a; $b; $c;
	fscanf($cin, "%d %d %d\n", $a, $b, $c);
	$first = abs($a - 1);
	$second = abs($b - $c) + abs($c - 1);
	if ($first == $second) {
		fprintf($cout, "3\n");
	} else if ($first > $second) {
		fprintf($cout, "2\n");
	} else {
		fprintf($cout, "1\n");
	}
} ?>