open $in, '<', "shortest-accepted.in";
<>;
while(<>){
	chomp;
	s/\$+/()/g;
	
	1 while 0
	|| s/ \w \*+ //x
	|| s/ (?> (?<![)*]) (\w*) \| (\w*) ) (?![(]) / 
		$capture_1 = $1, $capture_2 = $2, 
		(
		sort { (length $a) <=> (length $b) || $a cmp $b } ( $capture_1, $capture_2 ) 
		)[0]
		/xe
	|| s/\( (\w*) \) \*+ //x
	|| s/\( (\w*) \) /$1/x ;
	
	(length) or $_ = q($);
	print $_, "\n"
	}