#!/usr/bin/perl -l

use warnings;
use strict;

<>;

while(<>){
	my( undef, $K ) = split;
	
	my $A = <>;
	chomp $A;
	my $B = <>;
	chomp $B;
	
	my $ok;
	my $c = 27;
	
	while( $c -- ){
		$A ++;
		last if $A eq $B;
		last if $A =~ $K and $ok = $A;
		}
	
	print defined $ok ? $ok : 'NO';
	}