<?php

error_reporting(0);

solve();

function solve(){
	//$Input = new Input("input.txt");
	$Input = new Input();

	$S = $Input->in();
	$S_array = str_split($S);
	$len = count($S_array);

	$u_cnt = array_fill(-1, $len, 0);
	$min = 100000 + 1;
	for ($i = -1; $i < $len - 1; $i++) {
		if ($S_array[$i + 1] >= "A" && $S_array[$i + 1] <= "Z") {
			$u_cnt[$i + 1] = $u_cnt[$i] + 1;
		} else {
			$u_cnt[$i + 1] = $u_cnt[$i];
		}
	}
	for ($i = -1; $i < $len; $i++) {
		$min = min($min, $u_cnt[$len - 1] - $u_cnt[$i] + $i + 1 - $u_cnt[$i]);
	}

	print($min);
}

class Input{
	private $handle = "";
	private $values = array();
	private $index = 0;

	public function __construct($localfile){

		if (is_file($localfile)) {

			$this->handle = fopen($localfile, "r");
			$contents = stream_get_contents($this->handle);

		} else {

			$contents = stream_get_contents(STDIN);

		}

		$no_newlines = str_replace(array("\r\n", "\r", "\n"), " ", $contents);
		$this->values = explode(" ", $no_newlines);
	}

	public function in(){

		return $this->values[$this->index++];

	}

	public function close(){

		if (is_file($localfile) && feof($this->handle)) fclose($this->handle);

	}
}

?>

<?php

error_reporting(0);

solve();

function solve(){
	//$Input = new Input("input.txt");
	$Input = new Input();

	$S = $Input->in();
	$S_array = str_split($S);
	$len = count($S_array);

	$alp = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
	$l_pos = "";
	$u_pos = "";
	$l_count = array_fill(0,count($len), 0);
	$u_count = array_fill(0,count($len), 0);
	$min = 100000 + 1;
	$l_pre = 0;
	$u_pre = 0;
	for ($i = 0; $i < $len; $i++) {
		for ($j = 0; $j < count($alp); $j++) {
			$l_count[$i] = $l_pre;
			$u_count[$len - 1 - $i] = $u_pre;

			if ($S_array[$i] === $alp[$j]) {
				$l_count[$i] += 1;
			}
			if ($S_array[$len - 1 - $i] === strtoupper($alp[$j])) {
				$u_count[$len - 1 - $i] += 1;
			}

			$l_pre = $l_count[$i];
			$u_pre = $u_count[$len - 1 - $i];
		}
	}
	for ($i = 0; $i < $len; $i++) {

		$min = min($min, $l_count[$i] + $u_count[$i + 1]);
	}

	print($min);
}

class Input{
	private $handle = "";
	private $values = array();
	private $index = 0;

	public function __construct($localfile){

		if (is_file($localfile)) {

			$this->handle = fopen($localfile, "r");
			$contents = stream_get_contents($this->handle);

		} else {

			$contents = stream_get_contents(STDIN);

		}

		$no_newlines = str_replace(array("\r\n", "\r", "\n"), " ", $contents);
		$this->values = explode(" ", $no_newlines);
	}

	public function in(){

		return $this->values[$this->index++];

	}

	public function close(){

		if (is_file($localfile) && feof($this->handle)) fclose($this->handle);

	}
}

?>

<?php

error_reporting(0);

solve();

function solve(){
	//$Input = new Input("input.txt");
	$Input = new Input();

	$S = $Input->in();
	$S_array = str_split($S);
	$len = count($S_array);

	$alp = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
	$l_pos = "";
	$u_pos = "";
	$l_count = 0;
	$u_count = 0;

	for ($i = 0; $i < $len; $i++) {
		for ($j = 0; $j < count($alp); $j++) {
		if ($S_array[$i] === $alp[$j] && $l_pos === "") {
			$l_pos = $i;
			break;
		}
		if ($l_pos !== "" && $S_array[$i] === strtoupper($alp[$j])) {
				$u_count++;
				break;
			}
		}
	}

	for ($i = $len - 1; $i >= 0; $i--) {
		for ($j = 0; $j < count($alp); $j++) {
			if ($S_array[$i] === strtoupper($alp[$j]) && $u_pos === "") {
				$u_pos = $i;
				break;
			}
			if ($u_pos !== "" && $S_array[$i] === $alp[$j]) {
					$l_count++;
					break;
				}
			}
	}

	print(min($u_count, $l_count));
}

class Input{
	private $handle = "";
	private $values = array();
	private $index = 0;

	public function __construct($localfile){

		if (is_file($localfile)) {

			$this->handle = fopen($localfile, "r");
			$contents = stream_get_contents($this->handle);

		} else {

			$contents = stream_get_contents(STDIN);

		}

		$no_newlines = str_replace(array("\r\n", "\r", "\n"), " ", $contents);
		$this->values = explode(" ", $no_newlines);
	}

	public function in(){

		return $this->values[$this->index++];

	}

	public function close(){

		if (is_file($localfile) && feof($this->handle)) fclose($this->handle);

	}
}

?>


<?php
# 1155D
[$itemsCount, $x] = fscanf(STDIN, '%d %d');

$items = explode(' ', fgets(STDIN));
$max = $max1 = $max2 = $max3 = 0;
foreach ($items as $item) {
    $item = (float) $item;
    $max1 = max($max1 + $item, 0);
    $max2 = max($max2 + $item * $x, $max1);
    $max3 = max($max3 + $item, $max2);
    $max = max($max3, $max);
}
printf('%.0f', $max);

<?php
# 1155D
[$itemsCount, $x] = fscanf(STDIN, '%d %d');

$max = $max1 = $max2 = $max3 = 0;
$item = '';
do {
    $char = trim(fgetc(STDIN));
    $item .= $char;
    if ($char === '') {
        $item = (float) $item;
        $max1 = max($max1 + $item, 0);
        $max2 = max($max2 + $item * $x, $max1);
        $max3 = max($max3 + $item, $max2);
        $max = max($max3, $max);
        $item = '';
    }
} while (feof(STDIN) !== true);
printf('%.0f', $max);

<?php
echo PHP_INT_MAX . PHP_EOL;
echo PHP_FLOAT_MAX . PHP_EOL;

<?php
# 1155D
[$itemsCount, $x] = fscanf(STDIN, '%d %d');
$items = explode(' ', fgets(STDIN));
$max = $max1 = $max2 = $max3 = 0;
foreach ($items as $item) {
    $item = (int) $item;
    $max1 = max($max1 + $item, 0);
    $max2 = max($max2 + $item * $x, $max1);
    $max3 = max($max3 + $item, $max2);
    $max = max($max3, $max);
}
echo printf('%.0f', $max);

<?php
$input = fopen('php://stdin', 'rb');

[$itemsCount, $x] = explode(' ', fgets($input));
$items = explode(' ', fgets($input));

$max = array_sum($items);
for($length = 1; $length <= $itemsCount; $length++) {
    for ($i = 0; $length + $i <= $itemsCount; $i++) {
        $limit = $length + $i;
        $arr = $items;
        for($j = $i; $j < $limit; $j++) {
            $arr[$j] = (int)$items[$j] * (int)$x;
        }

        $max = max($max, array_sum($arr));
    }
}
echo $max;

<?php
echo PHP_INT_MAX;
die();
//9223372036854775807
//13346856128091
//13526989544447800
$input = fopen('php://stdin', 'rb');

[$itemsCount, $x] = explode(' ', fgets($input));
$itemsCount = (int)$itemsCount;
$x = (int)$x;
$items = explode(' ', fgets($input));
$max = $max1 = $max2 = $max3 = 0;
foreach ($items as $item) {
    $max1 = max($max1 + $item, 0);
    $max2 = max($max2 + $item * $x, $max1);
    $max3 = max($max3 + $item, $max2);
    $max = max($max3, $max);
}
echo $max;

<?php
$input = fopen('php://stdin', 'rb');

[$itemsCount, $x] = explode(' ', fgets($input));
$items = explode(' ', fgets($input));

$max = array_sum($items);
for($length = 1; $length <= $itemsCount; $length++) {
    for ($i = 0; $length + $i <= $itemsCount; $i++) {
        $limit = $length + $i;
        $arr = [];
        for($j = $i; $j < $limit; $j++) {
            $arr[$j] = (int)$items[$j] * (int)$x;
        }
        $summ = 0;
        $j = 0;
        while($j < $itemsCount && ($arr[$j] ?? $items[$j]) < 0) {
            $j++;
        }
        for (; $j < $itemsCount; $j++) {
            $summ += (int)($arr[$j] ?? $items[$j]);
        }
        $max = max($max, $summ);
    }
}
echo $max;

<?php
$input = fopen('php://stdin', 'rb');

[$itemsCount, $x] = explode(' ', fgets($input));
$items = explode(' ', fgets($input));
$max = 0;
for($length = 1; $length <= $itemsCount; $length++) {
    for ($i = 0; $length + $i <= $itemsCount; $i++) {
        $limit = $length + $i;
        $arr = $items;
        for($j = $i; $j < $limit; $j++) {
            $arr[$j] = (int)$items[$j] * (int)$x;
        }
        while(isset($arr[0]) && $arr[0] < 0) {
            array_shift($arr);
        }

        $max = max($max, array_sum($arr));
    }
}
echo $max;

<?php
$input = fopen('php://stdin', 'rb');

[$itemsCount, $x] = explode(' ', fgets($input));
$itemsCount = (int)$itemsCount;
$x = (int)$x;
$items = explode(' ', fgets($input));
$max = $max1 = $max2 = $max3 = 0;
foreach ($items as $item) {
    $max1 = max($max1 + $item, 0);
    $max2 = max($max2 + $item * $x, $max1);
    $max3 = max($max3 + $item, $max2);
    $max = max($max3, $max);
}
echo $max;

<?php
# 1155D
[$itemsCount, $x] = fscanf(STDIN, '%u %u');
$items = explode(' ', fgets(STDIN));
$max = $max1 = $max2 = $max3 = 0;
foreach ($items as $item) {
    $max1 = max($max1 + $item, 0);
    $max2 = max($max2 + $item * $x, $max1);
    $max3 = max($max3 + $item, $max2);
    $max = max($max3, $max);
}
echo printf('%.0f', $max);


<?php
list($n, $k) = explode(' ', trim(fgets(STDIN)));
$items = explode(' ', trim(fgets(STDIN)));
sort($items);
$i = 0;
$min = $items[$i++];
$sum = $min;
echo $min ."\n";

while ($k > 1) {
    if ($i >= $n) {
        echo "0\n";
        $k--;
        continue;
    }

    $min = $items[$i++] - $sum;

    if ($min <= 0) {
        continue;
    }

    $sum += $min;
    echo $min ."\n";

    $k--;
    while ($i < $n && $items[$i] == $sum) {
        $i++;
    }
}


<?
  list($a, $b, $c) = explode(' ', trim(fgets(STDIN)));
  $n = trim(fgets(STDIN));
  for ($i = 0; $i < $n; $i++) {
    list($x, $s) = explode(' ', trim(fgets(STDIN)));
    if ($s == "USB") {
      $u[] = $x;
    } elseif ($s == "PS/2") {
      $p[] = $x;
    }
  }

  sort($u);

  $e = array();

  function arrSum($inArray) {
    $result = 0;
    foreach ($inArray as $value) {
      $result = bcadd($result, $value);
    }
    return $result;
  }

  if (count($u) <= $a) {
    $ans += count($u);
    $sum = bcadd($sum, arrSum($u));
  } else {
    $e = array_slice($u, $a, count($u) - $a);
    $ans += $a;
    $sum = bcadd($sum, arrSum(array_slice($u, 0, $a)));
  }

  sort($p);

  if (count($p) <= $b) {
    $ans += count($p);
    $sum = bcadd($sum, arrSum($p));
  } else {
    $e = array_merge($e, array_slice($p, $b, count($p) - $b));
    $ans += $b;
    $sum = bcadd($sum, arrSum(array_slice($p, 0, $b)));
  }

  sort($e);

  if (count($e) <= $c) {
    $ans += count($e);
    $sum = bcadd($sum, arrSum($e));
  } else {
    $ans += $c;
    $sum = bcadd($sum, arrSum(array_slice($e, 0, $c)));
  }

  printf("%d %s\n", $ans, $sum);

?>

<?
  list($a, $b, $c) = explode(' ', trim(fgets(STDIN)));
  $n = trim(fgets(STDIN));
  for ($i = 0; $i < $n; $i++) {
    list($x, $s) = explode(' ', trim(fgets(STDIN)));
    if ($s == "USB") {
      $u[] = $x;
    } elseif ($s == "PS/2") {
      $p[] = $x;
    }
  }

  sort($u);

  $e = array();

  if (count($u) <= $a) {
    $ans += count($u);
    $sum += array_sum($u);
  } else {
    $e = array_slice($u, $a, count($u) - $a);
    $ans += $a;
    $sum += array_sum(array_slice($u, 0, $a));
  }
  sort($p);

  if (count($p) <= $b) {
    $ans += count($p);
    $sum += array_sum($p);
  } else {
    $e = array_merge($e, array_slice($p, $b, count($p) - $b));
    $ans += $b;
    $sum += array_sum(array_slice($p, 0, $b));
  }

  sort($e);

  if (count($e) <= $c) {
    $ans += count($e);
    $sum += array_sum($e);
  } else {
    $ans += $c;
    $sum += array_sum(array_slice($e, 0, $c));
  }

  printf("%d %.0f\n", $ans, $sum);

?>

<?
  list($a, $b, $c) = explode(' ', trim(fgets(STDIN)));
  $n = trim(fgets(STDIN));
  for ($i = 0; $i < $n; $i++) {
    list($x, $s) = explode(' ', trim(fgets(STDIN)));
    if ($s == "USB") {
      $u[] = $x;
    } elseif ($s == "PS/2") {
      $p[] = $x;
    }
  }

  sort($u);

  $f = array();

  if (count($u) <= $a) {
    $ans += count($u);
    $sum += array_sum($u);
  } else {
    $f = array_slice($u, $a, count($u) - $a);
    $ans += $a;
    $sum += array_sum(array_slice($u, 0, $a));
  }
  sort($p);

  if (count($p) <= $b) {
    $ans += count($p);
    $sum += array_sum($p);
  } else {
    $s = array_merge($f, array_slice($p, $b, count($p) - $b));
    $ans += $b;
    $sum += array_sum(array_slice($p, 0, $b));
  }

  sort($s);

  if (count($s) <= $c) {
    $ans += count($s);
    $sum += array_sum($s);
  } else {
    $ans += $c;
    $sum += array_sum(array_slice($s, 0, $c));
  }

  printf("%d %d\n", $ans, $sum);

?>


<?php
$input = [];
function readLine(){
    global $input;
    while(empty($input))$input=explode(' ',fgets(STDIN));
    $res = $input;
    $input = [];
    return array_map(function($x){return (int)$x;},$res);
}
function readToken(){
    global $input;
    while(empty($input))$input=explode(' ',fgets(STDIN));
    return array_shift($input);
}
function readInt(){
    return (int)readToken();
}
class Solution{
    public $debugMode;
    function __construct($flag){
        $this->debugMode = $flag;

    }
    function D($x){
        if($this->debugMode)var_dump($x);
    }
    function run(){
        list($b1,$q,$l,$m) = readLine();
        $a = readLine();
        $a = array_flip($a);
        if($q == 0) {
            if(abs($b1) > $l)echo "0\n";
            else if(!array_key_exists(0,$a)) echo "inf\n";
            else {
                if(array_key_exists($b1,$a)) echo "0\n";
                else echo "1\n";
            }

        } else if ($q == -1){
            if(abs($b1)>$l)echo "0\n";
            else if(array_key_exists($b1,$a) && array_key_exists(-$b1,$a))
                echo "0\n";
            else
                echo "inf\n";
        } else if ($q == 1){
            if(array_key_exists($b1,$a)||abs($b1)>$l)
                echo "0\n";
            else
                echo "inf\n";
        } else if($b1) {
            $ans=0;
            while(abs($b1)<=$l) {
                if(!array_key_exists($b1,$a))$ans++;
                $b1 = $b1 * $q;
            }
            echo "$ans\n";
        } else {
            if(array_key_exists(0,$a)){
                if(isset($a[$b1])||abs($b1)>$l)echo "0\n";
                else echo "1\n";
            }
            else echo "inf\n";
        }
    }
};
$caseNum=1;
for($caseId = 1;$caseId <= $caseNum; $caseId++){
    //echo 'Case #' . $caseId . ': ';
        $sol = new Solution(true);
            $sol->run();
           }

<?php
$input = [];
function readLine(){
    global $input;
    while(empty($input))$input=explode(' ',fgets(STDIN));
    $res = $input;
    $input = [];
    return array_map(function($x){return (int)$x;},$res);
}
function readToken(){
    global $input;
    while(empty($input))$input=explode(' ',fgets(STDIN));
    return array_shift($input);
}
function readInt(){
    return (int)readToken();
}
class Solution{
    public $debugMode;
    function __construct($flag){
        $this->debugMode = $flag;

    }
    function D($x){
        if($this->debugMode)var_dump($x);
    }
    function run(){
        list($b1,$q,$l,$m) = readLine();
        $a = readLine();
        $a = array_flip($a);
        if($q == 0) {
            if(!array_key_exists(0,$a)) echo "inf\n";
            else {
                if(array_key_exists($b1,$a)|| abs($b1) > $l) echo "0\n";
                else echo "1\n";
            }

        } else if ($q == -1){
            if(abs($b1)>$l)echo "0\n";
            else if(array_key_exists($b1,$a) && array_key_exists(-$b1,$a))
                echo "0\n";
            else
                echo "inf\n";
        } else if ($q == 1){
            if(array_key_exists($b1,$a)||abs($b1)>$l)
                echo "0\n";
            else
                echo "inf\n";
        } else if($b1) {
            $ans=0;
            while(abs($b1)<=$l) {
                if(!array_key_exists($b1,$a))$ans++;
                $b1 = $b1 * $q;
            }
            echo "$ans\n";
        } else {
            if(array_key_exists(0,$a)){
                if(isset($a[$b1])||abs($b1)>$l)echo "0\n";
                else echo "1\n";
            }
            else echo "inf\n";
        }
    }
};
$caseNum=1;
for($caseId = 1;$caseId <= $caseNum; $caseId++){
    //echo 'Case #' . $caseId . ': ';
        $sol = new Solution(true);
            $sol->run();
           }

<?php
$input = [];
function readLine(){
    global $input;
    while(empty($input))$input=explode(' ',fgets(STDIN));
    $res = $input;
    $input = [];
    return array_map(function($x){return (int)$x;},$res);
}
function readToken(){
    global $input;
    while(empty($input))$input=explode(' ',fgets(STDIN));
    return array_shift($input);
}
function readInt(){
    return (int)readToken();
}
class Solution{
    public $debugMode;
    function __construct($flag){
        $this->debugMode = $flag;

    }
    function D($x){
        if($this->debugMode)var_dump($x);
    }
    function run(){
        list($b1,$q,$l,$m) = readLine();
        $a = readLine();
        $a = array_flip($a);
        if($q == 0) {
            if(!array_key_exists(0,$a)) echo "inf\n";
            else {
                if(array_key_exists($b1,$a)) echo "0\n";
                else echo "1\n";
            }

        } else if ($q == -1){
            if(array_key_exists($b1,$a) && array_key_exists(-$b1,$a))
                echo "0\n";
            else
                echo "inf\n";
        } else if ($q == 1){
            if(array_key_exists($b1,$a))
                echo "0\n";
            else
                echo "inf\n";
        } else if($b1) {
            $ans=0;
            while(abs($b1)<=$l) {
                if(!array_key_exists($b1,$a))$ans++;
                $b1 = $b1 * $q;
            }
            echo "$ans\n";
        } else {
            if(array_key_exists(0,$a)){
                if(isset($a[$b1]))echo "0\n";
                else echo "1\n";
            }
            else echo "inf\n";
        }
    }
};
$caseNum=1;
for($caseId = 1;$caseId <= $caseNum; $caseId++){
    //echo 'Case #' . $caseId . ': ';
        $sol = new Solution(true);
            $sol->run();
           }

<?php
$input = [];
function readLine(){
    global $input;
    while(empty($input))$input=explode(' ',fgets(STDIN));
    $res = $input;
    $input = [];
    return array_map(function($x){return (int)$x;},$res);
}
function readToken(){
    global $input;
    while(empty($input))$input=explode(' ',fgets(STDIN));
    return array_shift($input);
}
function readInt(){
    return (int)readToken();
}
class Solution{
    public $debugMode;
    function __construct($flag){
        $this->debugMode = $flag;

    }
    function D($x){
        if($this->debugMode)var_dump($x);
    }
    function run(){
        list($b1,$q,$l,$m) = readLine();
        $a = readLine();
        $a = array_flip($a);
        if($q == 0) {
            if(array_key_exists(0,$a)) echo "inf\n";
            else {
                if(array_key_exists($b1,$a)) echo "0\n";
                else echo "1\n";
            }

        } else if ($q == -1){
            if(array_key_exists($b1,$a) && array_key_exists(-$b1,$a))
                echo "0\n";
            else
                echo "inf\n";
        } else if ($q == 1){
            if(array_key_exists($b1,$a))
                echo "0\n";
            else
                echo "inf\n";
        } else if($b1) {
            $ans=0;
            while(abs($b1)<=$l) {
                if(!array_key_exists($b1,$a))$ans++;
                $b1 = $b1 * $q;
            }
            echo "$ans\n";
        } else {
            if(array_key_exists(0,$a))echo "0\n";
            else echo "inf\n";
        }
    }
};
$caseNum=1;
for($caseId = 1;$caseId <= $caseNum; $caseId++){
    //echo 'Case #' . $caseId . ': ';
    $sol = new Solution(true);
    $sol->run();
}

<?php
$input = [];
function readLine(){
    global $input;
    while(empty($input))$input=explode(' ',fgets(STDIN));
    $res = $input;
    $input = [];
    return array_map(function($x){return (int)$x;},$res);
}
function readToken(){
    global $input;
    while(empty($input))$input=explode(' ',fgets(STDIN));
    return array_shift($input);
}
function readInt(){
    return (int)readToken();
}
class Solution{
    public $debugMode;
    function __construct($flag){
        $this->debugMode = $flag;

    }
    function D($x){
        if($this->debugMode)var_dump($x);
    }
    function run(){
        list($b1,$q,$l,$m) = readLine();
        $a = readLine();
        $a = array_flip($a);
        if($q == 0) {
            if(!array_key_exists(0,$a)) echo "inf\n";
            else {
                if(array_key_exists($b1,$a)|| abs($b1) > $m) echo "0\n";
                else echo "1\n";
            }

        } else if ($q == -1){
            if(abs($b1)>$m)echo "0\n";
            else if(array_key_exists($b1,$a) && array_key_exists(-$b1,$a))
                echo "0\n";
            else
                echo "inf\n";
        } else if ($q == 1){
            if(array_key_exists($b1,$a)||abs($b1)>$m)
                echo "0\n";
            else
                echo "inf\n";
        } else if($b1) {
            $ans=0;
            while(abs($b1)<=$l) {
                if(!array_key_exists($b1,$a))$ans++;
                $b1 = $b1 * $q;
            }
            echo "$ans\n";
        } else {
            if(array_key_exists(0,$a)){
                if(isset($a[$b1])||abs($b1)>$m)echo "0\n";
                else echo "1\n";
            }
            else echo "inf\n";
        }
    }
};
$caseNum=1;
for($caseId = 1;$caseId <= $caseNum; $caseId++){
    //echo 'Case #' . $caseId . ': ';
        $sol = new Solution(true);
            $sol->run();
           }

<?php
$input = [];
function readLine(){
    global $input;
    while(empty($input))$input=explode(' ',fgets(STDIN));
    $res = $input;
    $input = [];
    return array_map(function($x){return (int)$x;},$res);
}
function readToken(){
    global $input;
    while(empty($input))$input=explode(' ',fgets(STDIN));
    return array_shift($input);
}
function readInt(){
    return (int)readToken();
}
class Solution{
    public $debugMode;
    function __construct($flag){
        $this->debugMode = $flag;

    }
    function D($x){
        if($this->debugMode)var_dump($x);
    }
    function run(){
        list($b1,$q,$l,$m) = readLine();
        $a = readLine();
        $a = array_flip($a);
        if($q == 0) {
            if(array_key_exists(0,$a)) echo "inf\n";
            else {
                if(array_key_exists($b1,$a)) echo "0\n";
                else echo "1\n";
            }

        } else if (abs($q) == 1){
            if(array_key_exists($b1,$a) && array_key_exists(-$b1,$a))
                echo "0\n";
            else
                echo "inf\n";
        } else if($b1) {
            $ans=0;
            while(abs($b1)<=$l) {
                if(!array_key_exists($b1,$a))$ans++;
                $b1 = $b1 * $q;
            }
            echo "$ans\n";
        } else {
            if(array_key_exists(0,$a))echo "0\n";
            else echo "inf\n";
        }
    }
};
$caseNum=1;
for($caseId = 1;$caseId <= $caseNum; $caseId++){
    //echo 'Case #' . $caseId . ': ';
    $sol = new Solution(true);
    $sol->run();
}


<?php
$data = [];
while($line = fgets(STDIN)) {
    $data[] = $line;
}
$count = array_shift($data);
for($i = 0; $i < $count; $i++) {
    list($a, $b) = sscanf($data[$i], '%d %d');
    $ans = (int)(($a + $b) / 3);
    if($a === 0 || $b === 0) {
        echo 0 . PHP_EOL;
        continue;
    }
    if($ans > $a) {
        $ans = $a;
    }
    if($ans > $b) {
        $ans = $b;
    }
    echo $ans . PHP_EOL;

}

<?php
    define("MAXN", 1024) ;
    define("GAP_VAL", 1e-7) ;

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

    $t = intval(trim(fgets(STDIN))) ;
    while($t --)
    {
        $in_str = explode(" ", trim(fgets(STDIN))) ;

        $a = $in_str[0] ;
        $b = $in_str[1] ;

        if($a < $b) list($a, $b) = array($b, $a) ;

        if($a >= $b * 2) echo $b . "\n" ;
        else
        {
            $ans = $a - $b ;
            $a -= 2 * $ans ;

            $ans += intval($a / 3) * 2 ;

            $a = $a - 3 * intval($a / 3) ;
            if($a == 2) $ans ++ ;

            echo $ans . "\n" ;
        }
    }
?>

<?php
    define("MAXN", 1024) ;
    define("GAP_VAL", 1e-7) ;

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

    $t = intval(trim(fgets(STDIN))) ;
    while($t --)
    {
        $in_str = explode(" ", trim(fgets(STDIN))) ;

        $a = $in_str[0] ;
        $b = $in_str[1] ;

        if($a * 2 <= $b) echo $a . "\n" ;
        else if($a >= $b * 2) echo $b . "\n" ;
        else
        {
            $ans = min(intval($a/3), intval($b/3)) * 2 ;

            $a = $a - 3 * $ans / 2 ;
            $b = $b - 3 * $ans / 2 ;

            if($a >= 1 && $b >= 2) $ans ++ ;
            else if($a >= 2 && $b >= 1) $ans ++ ;

            echo $ans . "\n" ;
        }
    }
?>

<?php
    define("MAXN", 1024) ;
    define("GAP_VAL", 1e-7) ;

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

    $t = intval(trim(fgets(STDIN))) ;
    while($t --)
    {
        $in_str = explode(" ", trim(fgets(STDIN))) ;

        $a = $in_str[0] ;
        $b = $in_str[1] ;

        if($a < $b) list($a, $b) = array($b, $a) ;

        if($a >= $b * 2) echo $b . "\n" ;
        else
        {
            $ans = $a - $b ;

            $a -= 2 * $ans ;

            $ans += intval($a / 3) * 2 ;

            echo $ans . "\n" ;
        }
    }
?>

$data = [];
while($line = fgets(STDIN)) {
    $data[] = $line
}

var_dump($data);

<?php
$data = [];
while($line = fgets(STDIN)) {
    $data[] = $line;
}
$count = array_shift($data);
for($i = 0; $i < $count; $i++) {
    list($a, $b) = sscanf($data[$i], '%d %d');
    $ans = (int)(($a + $b) / 3);
    if($a === 0 || $b === 0) {
        echo 0;
        continue;
    }
    if($ans > $a) {
        $ans = $a;
    }
    if($ans > $b) {
        $ans = $b;
    }
    echo $ans;

}


<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     list($b, $c) = explode(" ", trim(fgets(STDIN)));
     if($b == 1)
     {
          if($b == $c)
          {
               print "YES\n";
          }
          else
          {
               print "NO\n";
          }
     }
     elseif(($b == 2) || ($b == 3))
     {
          if($c <= 3)
          {
               print "YES\n";
          }
          else
          {
               print "NO\n";
          }
     }
     elseif($b >= 4)
     {
          print "YES\n";
     }
}
?>

<?php
list($c) = explode(' ', trim(fgets(STDIN)));
for ($i = 0; $i < $c; $i++) {
    list($x, $y) = explode(' ', trim(fgets(STDIN)));
    //echo  ' input =' . $x . '   ' . $y . "\n";
    $bFlag = false;
    if (($x <= 1 && $x < $y) || ($y < 1 || $x < 1)) {
        $bFlag = false;
    } else {
        if ($x == $y) {
            $bFlag = true;
        } elseif ($x < $y) {
            if ($x % 2 == 0) {
                $d = $x;
                while (true) {
                    $x = $x * 1.5;
                    if($x >= $y){
                        $bFlag = true;
                        break;
                    }
                    $x = $x - 1;
                    if($d == $x) {
                        $bFlag = false;
                        break;
                    }

                }
            } else {
                $d = $x;
                while (true) {
                    $x = $x - 1;
                    $x = $x * 1.5;
                    if($x >= $y){
                        $bFlag = true;
                        break;
                    }
                    if($d == $x) {
                        $bFlag = false;
                        break;
                    }
                }
            }
        } else {
            $bFlag = true;
        }
    }

    //echo $x . '   ' . $y . "\n";
    if($bFlag){
        echo "YES"."\n";
    } else {
        echo "NO"."\n";
    }
}

<?php

function gcd($t,$l)
{
    $a=$t;
    while($a<$l && $a>3)
    {
        $a=(3*$a)/2;
    }
    if($a==2)$a=3;
    if($a>=$l)
    {
        echo "YES\n";
    }else
    {
        echo "NO\n";
    }

}
function solve()
{
    $server="php://stdin";
    $local="input.txt";
    $handle = fopen($server, "r");
    $s="";$d=0;$t=0;
    $x=trim(fgets($handle));
    for($i=0;$i<$x;$i++)
    {

        $str=trim(fgets($handle));
        $str1=explode(" ",$str);
        $w=$str1[0];$y=$str1[1];
        if($w==$y)
        {
            echo "YES\n";
        }else
        {
            gcd($w,$y);
        }

    }

}

solve();
?>

<?php
list($c) = explode(' ', trim(fgets(STDIN)));
for ($i = 0; $i < $c; $i++) {
    list($x, $y) = explode(' ', trim(fgets(STDIN)));
    //echo  ' input =' . $x . '   ' . $y . "\n";
    $bFlag = false;
    if ($x <= 1 && $x < $y) {
        $bFlag = false;
    } else {
        if ($x == $y) {
            $bFlag = true;
        } elseif ($x < $y) {
            if ($x % 2 == 0) {
                if ($x * 1.5 >= $y) {
                    $bFlag = true;
                } else {
                    $bFlag = false;
                }
            } else {
                $d = $x;
                while (true) {
                    $x = $x - 1;
                    $x = $x * 1.5;
                    if($x >= $y){
                        $bFlag = true;
                        break;
                    }
                    if($d == $x) {
                        $bFlag = false;
                        break;
                    }
                }
            }
        } else {
            $bFlag = true;
        }
    }

    if($bFlag){
        echo "YES"."\n";
    } else {
        echo "NO"."\n";
    }
}

<?php
list($c) = explode(' ', trim(fgets(STDIN)));
for ($i = 0; $i < $c; $i++) {
    list($x, $y) = explode(' ', trim(fgets(STDIN)));
    //echo  ' input =' . $x . '   ' . $y . "\n";
    $bFlag = false;
    if (($x <= 1 && $x < $y) || ($y < 1 || $x < 1)) {
        $bFlag = false;
    } else {
        if ($x == $y) {
            $bFlag = true;
        } elseif ($x < $y) {
            if ($x % 2 == 0) {
                if ($x * 1.5 >= $y) {
                    $bFlag = true;
                } else {
                    $bFlag = false;
                }
            } else {
                $d = $x;
                while (true) {
                    $x = $x - 1;
                    $x = $x * 1.5;
                    if($x >= $y){
                        $bFlag = true;
                        break;
                    }
                    if($d == $x) {
                        $bFlag = false;
                        break;
                    }
                }
            }
        } else {
            $bFlag = true;
        }
    }

    //echo $x . '   ' . $y . "\n";
    if($bFlag){
        echo "YES"."\n";
    } else {
        echo "NO"."\n";
    }
}

<?php

function gcd( $a, $b)
{
    if ($a == 0)
        return $b;
    return gcd($b % $a, $a);
}

// Function to return LCM
// of two numbers
function lcm( $a, $b)
{
    return ($a * $b) / gcd($a, $b);
}
function solve()
{
    $server="php://stdin";
    $local="input.txt";
    $handle = fopen($server, "r");
    $s="";$d=0;$t=0;
    $x=trim(fgets($handle));
    for($i=0;$i<$x;$i++)
    {

        $str=trim(fgets($handle));
        $str1=explode(" ",$str);
        $w=$str1[0];$y=$str1[1];
        if($w>=$y)
        {
            echo "Yes \n";
        }else
        {
            if(lcm($w,$y)!=$y)
                echo "Yes\n";
            else
                echo "No\n";
        }
    }

}

solve();
?>

<?php

function gcd($t,$l)
{
    $a=$t;
    if($a%2!=0 && $a!=1)$a=$a-1;
    while(true)
    {
        $a=(3*$a)/2;
        if($a>=$l)break;
    }
    if(is_float($a))
    {
        if($a-1<$l)
        echo "No\n";
        else
            echo "Yes\n";
    }else
    {
        echo "Yes\n";
    }

}
function solve()
{
    $server="php://stdin";
    $local="input.txt";
    $handle = fopen($server, "r");
    $s="";$d=0;$t=0;
    $x=trim(fgets($handle));
    for($i=0;$i<$x;$i++)
    {

        $str=trim(fgets($handle));
        $str1=explode(" ",$str);
        $w=$str1[0];$y=$str1[1];
        if($w==$y || $w>$y)
        {
            echo "Yes \n";
        }else
        {
            gcd($w,$y);
        }

    }

}

solve();
?>

<?php

function gcd($a,$b)
{
    while($b)
    {
        $a%=$b;
        $tmp=$b;
        $b=$a;
        $a=$tmp;

    }
    return $a;
}
function solve()
{
    $server="php://stdin";
    $local="input.txt";
    $handle = fopen($server, "r");
    $s="";$d=0;$t=0;
    $x=trim(fgets($handle));
    for($i=0;$i<$x;$i++)
    {

        $str=trim(fgets($handle));
        $str1=explode(" ",$str);
        $w=$str1[0];$y=$str1[1];
        if($w==$y)
        {
            echo "Yes \n";
        }
        else{
           if($w<=$y)
           {

               if (is_float((($w*3)/2)))
               {

                   if(is_float(((($w-1)*3)/2))||((($w-1)*3)/2)==$w||$w-1==0)
                   {
                       echo "No\n";
                   }else
                   {
                       echo "Yes\n";
                   }

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

    }

}

solve();
?>

<?php

function gcd($t,$l)
{
    $a=$t;
    if($a%2!=0 && $a!=1)$a=$a-1;
    while(true)
    {
        $a=(3*$a)/2;
        if($a>=$l)break;
    }
    if(is_float($a))
    {
        if($a-1<$l)
        echo "No\n";
        else
            echo "Yes\n";
    }else
    {
        echo "Yes\n";
    }

}
function solve()
{
    $server="php://stdin";
    $local="input.txt";
    $handle = fopen($server, "r");
    $s="";$d=0;$t=0;
    $x=trim(fgets($handle));
    for($i=0;$i<$x;$i++)
    {

        $str=trim(fgets($handle));
        $str1=explode(" ",$str);
        $w=$str1[0];$y=$str1[1];
        if($w==$y || $w>$y)
        {
            echo "Yes \n";
        }else
        {
            if($y%$w>0)
                echo "Yes\n";
            else
                echo "No\n";
        }
    }

}

solve();
?>

<?php

function gcd($t,$l)
{
    $a=$t;
    if($a%2!=0 && $a!=1)$a=$a-1;
    while(true)
    {
        $a=(3*$a)/2;
        if($a>=$l)break;
    }
    if(is_float($a))
    {
        if($a-1<$l)
        echo "No\n";
        else
            echo "Yes\n";
    }else
    {
        echo "Yes\n";
    }

}
function solve()
{
    $server="php://stdin";
    $local="input.txt";
    $handle = fopen($server, "r");
    $s="";$d=0;$t=0;
    $x=trim(fgets($handle));
    for($i=0;$i<$x;$i++)
    {

        $str=trim(fgets($handle));
        $str1=explode(" ",$str);
        $w=$str1[0];$y=$str1[1];
        if($w==$y)
        {
            echo "Yes \n";
        }else
        {
            gcd($w,$y);
        }

    }

}

solve();
?>


<?php
$stdin = explode(' ', trim(fgets(STDIN)));
$type = $stdin[0];
$rub = $stdin[1];

$types = explode(' ', trim(fgets(STDIN)));
sort($types);
$t = 0;
$count_types = count($types);

$k = 1;
$n = 0;
$list = array();
while ($rub >= 0) {
    if ($rub - $k < 0) {
        break;
    }

    if (($t+1) == $count_types) {

    }

    if ($k < $types[$t] || is_null($types[$t]) ) {
        $rub -= $k;
        if ($rub >= 0) {
            $list[] = $k;
            $n++;
        }
    } else {
        $t++;
    }

    $k++;
}

echo $n."\n".implode(' ', $list);

?>

<?php
$stdin = explode(' ', trim(fgets(STDIN)));
$type = $stdin[0];
$rub = $stdin[1];

$types = explode(' ', trim(fgets(STDIN)));
$t = 0;
$count_types = count($types);

$k = 1;
$n = 0;
$list = array();
while ($rub >= 0) {
    if ($rub - $k < 0) {
        break;
    }

    if (($t+1) == $count_types) {

    }

    if ($k < $types[$t] || is_null($types[$t]) ) {
        $rub -= $k;
        if ($rub >= 0) {
            $list[] = $k;
            $n++;
        }
    } else {
        $t++;
    }

    $k++;
}

echo $n."\n".implode(' ', $list);

?>

<?php
$stdin = explode(' ', trim(fgets(STDIN)));
$type = $stdin[0];
$rub = $stdin[1];

$types = explode(' ', trim(fgets(STDIN)));
$t = 0;

$k = 1;
$n = 0;
$list = array();
while ($rub >= 0) {
    if ($rub - $k < 0) {
        break;
    }

    if ($k < $types[$t]) {
        $rub -= $k;
        if ($rub >= 0) {
            $list[] = $k;
            $n++;
        }
    } else {
        $t++;
    }

    $k++;
}

echo $n."\n".implode(' ', $list);

?>


<?php
$a = explode(" ", trim(fgets(STDIN)));
$n = explode(" ", trim(fgets(STDIN)));
foreach($n as $sK => $aV){
    $b = true;
    for($i = 1; $i <= $a[1]; $i++){
        $f = $sK - $i;
        if(isset($n[$f])){
            if($n[$f] < $aV){
                $b = false;
            }
        }
    }
    if($b == false && $sK < sizeof($n) - 1){
        continue;
    }
    for($j = 1; $j <= $a[2]; $j++){
        $f = $sK + $j;
        if(isset($n[$f])){
            if($n[$f] < $aV){
                $b = false;
            }
        }
    }
    if($b == true){
        echo $sK+1;
        exit;
    } else {
        continue;
    }
}

?>


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

    return $iSum;
}

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

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

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

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

}

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

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

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

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

    /**
     * @param string $bigint1
     * @param string $bigint2
     * @return string $bigint1 % $bigint2
     */
    public static function mod($bigint1, $bigint2) {
        return bcmod($bigint1, $bigint2);
    }

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

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

class Algorithms
{

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

        return $a;
    }

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

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

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

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

        return $a[$n];
    }

}

?>

<?php

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

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

    return $iSum;
}

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

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

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

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

}

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

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

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

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

    /**
     * @param string $bigint1
     * @param string $bigint2
     * @return string $bigint1 % $bigint2
     */
    public static function mod($bigint1, $bigint2) {
        return bcmod($bigint1, $bigint2);
    }

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

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

class Algorithms
{

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

        return $a;
    }

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

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

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

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

        return $a[$n];
    }

}

?>

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

    return $iSum;
}

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

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

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

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

}

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

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

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

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

    /**
     * @param string $bigint1
     * @param string $bigint2
     * @return string $bigint1 % $bigint2
     */
    public static function mod($bigint1, $bigint2) {
        return bcmod($bigint1, $bigint2);
    }

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

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

class Algorithms
{

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

        return $a;
    }

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

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

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

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

        return $a[$n];
    }

}

?>

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

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

    return $iSum;
}

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

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

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

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

}

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

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

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

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

    /**
     * @param string $bigint1
     * @param string $bigint2
     * @return string $bigint1 % $bigint2
     */
    public static function mod($bigint1, $bigint2) {
        return bcmod($bigint1, $bigint2);
    }

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

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

class Algorithms
{

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

        return $a;
    }

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

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

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

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

        return $a[$n];
    }

}

?>

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

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

    echo $iCounter."\n";

    return $iSum;
}

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

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

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

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

}

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

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

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

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

    /**
     * @param string $bigint1
     * @param string $bigint2
     * @return string $bigint1 % $bigint2
     */
    public static function mod($bigint1, $bigint2) {
        return bcmod($bigint1, $bigint2);
    }

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

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

class Algorithms
{

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

        return $a;
    }

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

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

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

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

        return $a[$n];
    }

}

?>

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

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

    echo $iSum;
}

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

<?php

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

<?php

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


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

<?php

function FindFlowers($flowers,$beauty){

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

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

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

}

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

FindFlowers($flowers,$beauty);

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

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

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

sort($arr);

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

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

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

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

echo "$max $col";

?>

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

<?php

function FindFlowers($flowers,$beauty){

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

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

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

}

function factorial($number) {

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

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

FindFlowers($flowers,$beauty);

<?php

function FindFlowers($flowers,$beauty){

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

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

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

}

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

FindFlowers($flowers,$beauty);

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

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

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

sort($arr);

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

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

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

$col = $colfirst * $colend;

echo "$max $col";

?>

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

echo $col;

?>

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

<?php

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

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

// solution
$burning_calories = 0;

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

echo "$burning_calories\n";

<?php

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

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

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

$result = 0 ;

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

fwrite($stdout);

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

?>

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

<?php

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

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

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

	$s = fgets($temp);

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

	echo $sum;

?>

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

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

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

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

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

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

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

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


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

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

/** Module for retrieving input **/

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

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

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

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

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

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

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

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

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

<?php

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

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

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

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

$answer = 0;
$output = PHP_EOL;

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

print $answer;
print $output;

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

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


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

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

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

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

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

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

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

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

        return $a[$n];
    }

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

    $hasOdd= false;

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

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

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