You are given an integer $N$ followed by $N$ email addresses. Your task is to print a list containing only valid email addresses in lexicographical order.

Valid email addresses must follow these rules:

It must have the username@websitename.extension format type.
The username can only contain letters, digits, dashes and underscores $[a-z],[A-Z],[0-9],[\text{_}-]$. 
The website name can only have letters and digits $[a-z],[A-Z],[0-9]$. 
The extension can only contain letters $[a-z],[A-Z]$.   
The maximum length of the extension is $3$.

Concept

A filter takes a function returning True or False and applies it to a sequence, returning a list of only those members of the sequence where the function returned True. A Lambda function can be used with filters.

Let's say you have to make a list of the squares of integers from $\mbox{0}$ to $\mbox{9}$ (both included).

>> l = list(range(10))
>> l = list(map(lambda x:x*x, l))

Now, you only require those elements that are greater than $10$ but less than $80$.

>> l = list(filter(lambda x: x > 10 and x < 80, l))

Easy, isn't it?

Example   

Complete the function fun in the editor below.   

fun has the following paramters:   

string s: the string to test   

Returns   

boolean: whether the string is a valid email or not   

Input Format

The first line of input is the integer $N$, the number of email addresses. 

$N$ lines follow, each containing a string.

Constraints

Each line is a non-empty string.

Sample Input
3
lara@hackerrank.com
brian-23@hackerrank.com
britts_54@hackerrank.com

Sample Output
['brian-23@hackerrank.com', 'britts_54@hackerrank.com', 'lara@hackerrank.com']

Difficulty: MEDIUM

Starter code:

Solutions:
import re
data = []
pattern = re.compile('^[a-zA-Z0-9-_]+@[a-zA-Z0-9]+\\.[a-z]{1,3}$')
N = int(input())
while N > 0:
	data.append(input().strip())
	N -= 1
print(sorted(list(filter(lambda x: re.match(pattern, x), data))))


import re

def validate_email(email):
	pat = re.compile('^[a-z0-9-_]+@[a-z0-9]+\\.[a-z0-9]{,3}$')
	return pat.match(email)
i = int(input())
mails = [input() for n in range(i)]
valid_mails = list(filter(lambda mail: validate_email(mail), mails))
valid_mails.sort()
print(valid_mails)


import re
N = int(input())
lis = []
for i in range(N):
	lis.append(input())
reg = '^[a-zA-Z0-9\\-_]+@[a-zA-Z0-9]+\\.[a-zA-Z]{1,3}$'
lis = list(filter(lambda x: re.match(reg, x), lis))
lis.sort()
print(lis)


import re

def cmp_to_key(mycmp):

	class K(object):

		def __init__(self, obj, *args):
			self.obj = obj

		def __lt__(self, other):
			return mycmp(self.obj, other.obj) < 0

		def __gt__(self, other):
			return mycmp(self.obj, other.obj) > 0

		def __eq__(self, other):
			return mycmp(self.obj, other.obj) == 0

		def __le__(self, other):
			return mycmp(self.obj, other.obj) <= 0

		def __ge__(self, other):
			return mycmp(self.obj, other.obj) >= 0

		def __ne__(self, other):
			return mycmp(self.obj, other.obj) != 0
	return K

def CmpNumbers(x, y):
	l1 = len(x)
	l2 = len(y)
	if l1 < l2:
		ml = l1
	else:
		ml = l2
	for i in range(ml):
		if x[i] < y[i]:
			return -1
		if x[i] > y[i]:
			return 1
	if l1 < l2:
		return -1
	elif l1 > l2:
		return 1
	else:
		return 0

def IsValid(x):
	if re.search('^[a-zA-Z0-9-_]+\\@([a-zA-Z]|\\d)+\\.[a-zA-Z0-9]{1,3}$', x):
		return True
	return False
n = int(input())
emails = [input() for i in range(0, n)]
emails = list(filter(lambda x: IsValid(x), emails))
emails.sort(key=cmp_to_key(CmpNumbers))
print(emails)


import re
lines = input()
email_list = []
for i in range(int(lines)):
	email = input()
	email_list.append(email)

def validity(email_addr):
	if re.search('[@]', email_addr) == None:
		return False
	else:
		(username, domain) = email_addr.rsplit('@', 1)
		validUsername = True
		validWebsite = True
		validExtension = True
		if re.search('[^a-zA-Z0-9_-]', username) != None or len(username) == 0:
			validUsername = False
		if re.search('[.]', domain) == None:
			return False
		else:
			(website, extension) = domain.rsplit('.', 1)
			if website.isalnum() == False or len(website) == 0:
				validWebsite = False
			if len(extension) > 3 or len(extension) == 0:
				validExtension = False
			if validUsername and validWebsite and validExtension:
				return True
			else:
				return False
email_list = list(filter(validity, email_list))
email_list.sort()
print(email_list)


import re
n = int(input())
ls = []

def f(x):
	parts = x.split('@')
	if len(parts) != 2 or len(parts[1].split('.')) != 2:
		return False
	username = parts[0]
	website = parts[1].split('.')[0]
	extension = parts[1].split('.')[1]
	return re.match('^[-_a-zA-Z0-9]+$', username) and re.match('^[a-zA-Z0-9]+$', website) and (1 <= len(extension) <= 3)
for i in range(n):
	ls.append(input())
ls = list(filter(f, ls))
ls.sort()
print(ls)


import re
valid_emails = []
for email in [input() for i in range(int(input()))]:
	if re.match('^[a-zA-Z0-9-_]+@[a-zA-Z0-9]+\\.[a-z]{1,3}$', email):
		valid_emails.append(email)
ve = list(valid_emails)
ve.sort()
print(ve)


import sys
filein = sys.stdin
filein.readline()
a = []
for line in filein:
	a.append(line.rstrip())

def check(s):
	spot = 0
	if not '@' in s:
		return False
	for i in range(len(s)):
		if s[i] == '@':
			if i == 0:
				return False
			spot = i
			break
		if not s[i].isalnum() and (not s[i] == '-') and (not s[i] == '_'):
			return False
	for i in range(spot + 1, len(s)):
		if s[i] == '.':
			if i == len(s) - 1:
				return False
			spot = i
			break
		if not s[i].isalnum():
			return False
	if len(s) - spot > 4:
		return False
	return True
a = list(filter(check, a))
a.sort()
print(a)


import re

def main():
	noemails = int(input())
	emails = []
	for email in range(noemails):
		emails.append(input())
	email_matcher = '^[A-Za-z0-9-_]+@[A-Za-z0-9]+\\.[A-Za-z0-9]{0,3}$'
	emails = list(filter(lambda email: re.match(email_matcher, email) != None, emails))
	print(sorted(emails))
main()


import re

def validEmail(addr):
	return re.match('^[\\w_-]+@([A-Za-z0-9]+\\.)*([A-Za-z0-9]{1,3})$', addr)
num = int(input())
l = []
for i in range(num):
	l.append(input())
l = filter(validEmail, l)
print(sorted(l))


n = int(input())
emails = [input() for _ in range(n)]
import re
pattern = re.compile('^[\\w\\-]+@[a-zA-Z0-9]+\\.\\w{1,3}$')
valid_emails = sorted(filter(pattern.match, emails))
print(valid_emails)


import re
i = int(input())
l = []
pattern = '^(([a-zA-Z0-9]|(-)|(_))+)(@)([a-zA-Z0-9]+)(\\.)(([a-zA-Z]{1,3})$)'
for x in range(0, i):
	l += [input()]
l = list(filter(lambda y: re.search(pattern, y), l))
l.sort()
print(l)


import re
pattern = re.compile('^[-a-zA-Z0-9_]+@[a-zA-Z0-9]+\\.[a-zA-Z]{1,3}$')
n = int(input())
lst = []
for x in range(n):
	temp = input()
	if pattern.match(temp):
		lst.append(temp)
lst.sort()
print(lst)


def foo(x):
	import re
	email = re.compile('[\\w\\-]+@[a-zA-Z0-9]+\\.[a-zA-Z]{1,3}$')
	l = []
	for i in range(x):
		l.append(input())
	print(sorted(list(filter(lambda y: email.match(y), l))))
foo(int(input()))


import re
n = int(input())
p = re.compile('^[a-zA-Z0-9\\-_]+@[a-zA-Z0-9]+\\.[a-zA-Z]{1,3}$')
l = list(filter(lambda x: p.match(x), (input() for _ in range(n))))
print(sorted(l))


def check(str):
	try:
		l1 = str.split('.')
		l2 = l1[0].split('@')
		if len(l1) != 2 or len(l2) != 2:
			return False
		if len(l1[1]) > 3 or len(l2[0]) < 1 or len(l2[1]) < 1:
			return False
		for x in l2[0]:
			if not (x >= 'a' and x <= 'z' or (x >= 'A' and x <= 'Z') or (x >= '0' and x <= '9') or (x == '-') or (x == '_')):
				return False
		for x in l2[1]:
			if not (x >= 'a' and x <= 'z' or (x >= 'A' and x <= 'Z') or (x >= '0' and x <= '9')):
				return False
	except IndexError:
		return False
	return True
test_case = int(input())
wo_mails = []
for x in range(0, test_case):
	wo_mails.append(input())
wo_mails = list(filter(lambda x: check(x), wo_mails))
wo_mails.sort()
print(wo_mails)


import re
n = int(input())
emails = []
for i in range(n):
	emails.append(input())
valid_emails = filter(lambda email: re.search('^[a-zA-Z0-9_-]+@[a-zA-Z0-9]+\\.[a-zA-Z]{1,3}$', email), emails)
print(sorted(valid_emails))


import re
n = int(input())
list = []
for i in range(n):
	s = input()
	if re.match('([a-z0-9\\-_]+)@([a-z0-9]+)\\.([a-z]{1,3})$', s):
		list.append(s)
list.sort()
print(list)


import sys, re
regex = re.compile('[\\w-]+@[^\\W_]+\\.[a-z]{1,3}$', re.ASCII)
next(sys.stdin)
valid_emails = sorted([email.strip() for email in sys.stdin if regex.match(email)])
print(valid_emails)


import string
N = int(input(''))
emailList = []
for i in range(0, N):
	emailList.append(input(''))
validEmail = []

def validate(email):
	details = email.split('@')
	if len(details) < 2 or not details[0] or (not details[1]):
		return False
	for ch in details[0]:
		if ch in string.ascii_letters or ch in string.digits or ch in '_-':
			continue
		return False
	details = details[1].split('.')
	for ch in details[0]:
		if ch in string.ascii_letters or ch in string.digits:
			continue
		return False
	if len(details) < 2 or len(details[1]) > 3:
		return False
	return True
for email in emailList:
	if validate(email):
		validEmail.append(email)
validEmail.sort()
print(validEmail)


import re
n = int(input())
a = []
for i in range(n):
	a.append(input())
x = list(filter(lambda x: re.match('^[a-zA-Z0-9_-]+@[a-zA-Z0-9]+\\.[a-zA-Z]{1,3}$', x), a))
x.sort()
print(x)


import re
N = int(input())
emails = []
for i in range(N):
	emails.append(input())
chkEml = lambda x: re.search('^[\\w\\-]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]{1,3}$', x)
emails = list(filter(chkEml, emails))
print(sorted(emails))


import re

def is_valid(email):
	return re.search('^[\\w-]+@[a-zA-Z0-9]+\\.[a-z]{1,3}$', email) != None
n = int(input())
emails = [input() for _ in range(n)]
ok_emails = sorted(filter(is_valid, emails))
print(ok_emails)


import re
n = int(input())
l1 = []
for i in range(n):
	e = input()
	if e.find('.') != -1 and e.find('@') != -1 and (e.find('.') != 0) and (e.find('.') != len(e) - 1) and (e.find('@') != 0) and (e.find('@') != len(e) - 1) and (e.count('.') == 1) and (e.count('@') == 1):
		(f, s1) = e.split('@')
		(s, t) = s1.split('.')
		if re.match('^[A-Za-z0-9_-]*$', f):
			if re.match('^[A-Za-z0-9]*$', s):
				if len(t) > 0 and len(t) <= 3:
					l1.append(e)
l1.sort()
print(l1)


import re
n = int(input())
x = []
for i in range(n):
	a = input()
	x.append(a)
b = list(filter(lambda a: re.search('^[0-9A-Za-z_-]+\\@[0-9A-Za-z]+\\.[0-9A-Za-z]{2,3}$', a), x))
print(sorted(b))


import re

def isemailvalid(email):
	if re.search('^[\\w\\-]+@[a-zA-Z0-9]+\\..{1,3}$', email) != None:
		return True
	else:
		return False
t = int(input())
lst = []
for i in range(0, t):
	lst.append(input())
lst = sorted(lst)
print(list(filter(isemailvalid, lst)))


import re
r = re.compile('^[a-zA-Z0-9_-]+@[a-zA-Z0-9]+\\.[a-zA-Z]{1,3}$')
n = int(input())
l = []
for i in range(n):
	l.append(input())
l = sorted(list(filter(lambda x: r.match(x), l)))
print(l)


import re
N = int(input())
str = []
a = re.compile('^[a-zA-Z0-9\\_\\-]+\\@[a-zA-Z0-9]+\\.[a-z]{2,3}$')
for i in range(0, N):
	str.append(input())
str1 = []
for email_id in str:
	if len(email_id.split('@')) == 2 and len(email_id.split('.')) == 2 and a.match(email_id):
		str1.append(email_id)
sorted_email = sorted(str1)
print(sorted_email)


N = int(input())
emails = []
emails_ext = []
emails_name = []
emails_web = []
emails_fin = []
allowed = list(map(chr, range(65, 91)))
allowed += list(map(chr, range(97, 123)))
allowed += ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
allowed += ['-', '_']
allowed_web = list(map(chr, range(97, 123)))
allowed_web += list(map(chr, range(65, 91)))
allowed_web += ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
allowed_com = list(map(chr, range(97, 123)))
for i in range(0, N):
	emails.append(input())
for i in range(0, N):
	if emails[i].count('@') == 1:
		if emails[i].count('.') == 1:
			if '.' in emails[i][-4:-1]:
				index_at = emails[i].index('@')
				index_dot = emails[i].index('.')
				if index_at != 0 and index_at - index_dot != 1:
					emails_ext.append(emails[i])
	else:
		pass
emails = emails_ext
emails_ext = []
for i in range(0, len(emails)):
	emails_name.append(emails[i][:emails[i].index('@')])
	emails_web.append(emails[i][emails[i].index('@') + 1:-4])
for i in range(0, len(emails)):
	if set(emails_name[i]).issubset(set(allowed)):
		if set(emails_web[i]).issubset(set(allowed_web)):
			emails_fin.append(emails[i])
emails_fin.sort()
print(emails_fin)


import re
valid_emails = set()
for email in [input() for i in range(int(input()))]:
	if re.match('^[a-zA-Z0-9-_]+@[a-zA-Z0-9]+\\.[a-z]{1,3}$', email):
		valid_emails.add(email)
ve = list(valid_emails)
ve.sort()
print(ve)


import re

def main():
	n = int(input())
	mails = []
	for i in range(n):
		mails.append(str(input()))
	expr = '^[\\w-]+@[a-zA-Z0-9]+\\.[a-zA-Z]{1,3}$'
	p = re.compile(expr)
	mails = filter(lambda x: None != p.match(x), mails)
	mails = sorted(mails)
	print(mails)
main()


import string
n = int(input())
output = []
for i in range(n):
	output.append(input())
output = list(filter(lambda x: (len(x.split('.')) == 2) & (len(x.split('@')) == 2), output))
output = list(filter(lambda x: len(x.split('.')[len(x.split('.')) - 1]) <= 3, output))
output = list(map(lambda x: x.replace('.', '@'), output))
output = list(map(lambda x: x.split('@'), output))
output = list(filter(lambda x: (len(x[0]) > 0) & (len(x[1]) > 0) & (len(x[2]) > 0), output))
intabUser = b'QAZXSWEDCVFRTGBNHYUJMKIOLPqazxswedcvfrtgbnhyujmkiolp1234567890-_'
outtabUser = b'                                                                '
trantabUser = bytes.maketrans(intabUser, outtabUser)
intabDomain = b'QAZXSWEDCVFRTGBNHYUJMKIOLPqazxswedcvfrtgbnhyujmkiolp1234567890'
outtabDomain = b'                                                              '
trantabDomain = bytes.maketrans(intabDomain, outtabDomain)
output = list(filter(lambda x: (len(x[0].translate(trantabUser).replace(' ', '')) == 0) & (len(x[1].translate(trantabDomain).replace(' ', '')) == 0) & (len(x[2].translate(trantabDomain).replace(' ', '')) == 0), output))
output = list(map(lambda x: x[0] + '@' + x[1] + '.' + x[2], output))
print(sorted(output))


import re
N = int(input())
emails = []
for i in range(N):
	emails.append(input())
pattern = '^[\\w\\-]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]{1,3}$'
print(sorted(filter(lambda x: re.search(pattern, x), emails)))


n = int(input())
A = []
for x in range(0, n):
	A.append(input())
loweralplist = list(range(97, 123))
upperalplist = list(range(65, 91))
digitslist = list(range(48, 58))
dash = 45
underbar = 95
atsign = 64
dot = 46
i = 0
wrong = []
for x in A:
	i = i + 1
	a = 0
	b = 0
	j = 0
	k = 0
	l = -1
	c = 0
	d = 0
	count = 0
	for y in x:
		if ord(y) == atsign:
			a = 1
			c = c + 1
		if ord(y) == dot:
			b = 1
			d = d + 1
		count = count + 1
		if a == 0 and (ord(y) not in loweralplist and ord(y) not in upperalplist and (ord(y) not in digitslist) and (ord(y) != dash) and (ord(y) != underbar)):
			j = 1
		if a == 1 and b == 0 and (ord(y) not in loweralplist and ord(y) not in upperalplist and (ord(y) not in digitslist) and (ord(y) != atsign)):
			k = 1
		if a == 1 and b == 1 and (d == 1):
			l = l + 1
		if count == 1 and a == 1:
			break
	if j == 1 or k == 1 or l > 3 or (l <= 0) or (b == 0) or (c > 1):
		wrong.append(i - 1)
wrong.sort(reverse=True)
wrongemails = []
for x in wrong:
	wrongemails.append(A[x])
l3 = [x for x in A if x not in wrongemails]
l3.sort()
print(l3)


import re
N = int(input())
el = []
for i in range(N):
	email = input()
	el.append(email)
p = re.compile('[\\w\\-]+@[a-zA-Z0-9]+\\.\\w{1,3}$')
print(sorted(list(filter(lambda a: p.match(a), el))))


import re
pat = '^[a-zA-Z0-9-_]+@[a-zA-Z0-9]+\\.[a-z]{1,3}$'
size = int(input())
valid = list()
for i in range(size):
	email = input()
	if re.match(pat, email):
		valid.append(email)
print(sorted(valid))


import re
pattern = re.compile('^[a-zA-Z\\d\\-_]+@[a-zA-Z\\d]+\\.[a-zA-Z]{1,3}$')
(T, mails) = (int(input()), [])
for _ in range(T):
	mails.append(input().strip())
print(sorted(list(filter(lambda x: pattern.match(x), mails))))


x = input()
d = []
for i in range(0, int(x)):
	y = input()
	d.append(y)
d = sorted(d)
import string
y = tuple(string.ascii_lowercase) + tuple(string.ascii_uppercase) + tuple('-') + tuple('_') + ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
z = tuple(string.ascii_lowercase) + tuple(string.ascii_uppercase) + ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
k = tuple(string.ascii_lowercase) + tuple(string.ascii_uppercase)

def f(l):
	if '@' in l[0]:
		return False
	if '@' not in l:
		return False
	if '.' not in l:
		return False
	if ('@' and '.' in l) and '@' not in l[0]:
		m = l[:l.index('@')]
		n = l[l.index('@') + 1:l.index('.')]
		p = l[l.index('.') + 1:]
		if len(p) > 3:
			return False
		for i in m:
			if i not in y:
				return False
		for j in n:
			if j not in z:
				return False
		for g in p:
			if g not in k:
				return False
		else:
			return True
	else:
		return True
print(list(filter(f, d)))


import re

def main():
	address_list = []
	num_addresses = int(input())
	for i in range(num_addresses):
		address_list.append(str(input()).strip())
	print(sorted(filter(lambda x: re.match('^[\\w\\-]+@([a-zA-Z0-9]+\\.){1,2}[a-zA-Z0-9]{1,3}$', x), address_list)))
main()


import sys
import re
(_, emails) = (input(), sys.stdin.read().split('\n'))
emailre = re.compile('^[a-zA-Z0-9-_]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]{1,3}$')
print(sorted(filter(lambda x: emailre.match(x), emails)))


l = []
for i in range(int(input())):
	l.append(input())
l = list(filter(lambda x: '@' in x and '.' in x.split('@')[1] and (len(x.split('.')[1]) <= 3) and x.split('@')[1].split('.')[0].isalnum() and x.split('@')[0].replace('-', '1').replace('_', '1').isalnum(), l))
print(sorted(l))


import sys, re
if sys.version_info[0] >= 3:
	raw_input = input

def check(email):
	return re.match('^[0-9a-zA-Z\\-_]+@[0-9a-zA-Z]+\\.[a-zA-Z]{1,3}$', email)
print(sorted((e for e in [raw_input().rstrip() for i in range(int(raw_input()))] if check(e))))


import re
N = int(input())
A = [input() for _ in range(N)]
print(sorted((a for a in A if re.match('^[a-zA-Z0-9-_]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]{1,3}$', a))))


import re

def valid(a):
	return re.search('^[A-Za-z0-9-_]+@[A-Za-z0-9]+\\.[A-Za-z0-9]{1,3}$', a)
l = []
for i in range(int(input())):
	l.append(input())
print(sorted(filter(valid, l)))


import re

def isCorrect(email):
	emailRe = '[\\w-]+@([a-zA-Z0-9])+(\\.([a-zA-Z0-9]{1,3}))'
	if re.fullmatch(emailRe, email):
		return True
	else:
		return False
n = int(input())
emailList = []
for i in range(n):
	emailList.append(input())
emailList = list(filter(lambda email: isCorrect(email), emailList))
emailList.sort()
print(emailList)


import re
ptrn = re.compile('^[0-9a-zA-Z\\-_]+@[0-9a-zA-Z]+\\.[a-zA-Z]{1,3}$')
N = int(input())
E = sorted([input() for e in range(N)])
print(list(filter(ptrn.match, E)))


import re
n = int(input())
L = []
for i in range(n):
	x = input()
	match = re.search('^[\\w-]+@[A-Za-z0-9]+\\..{1,3}$', x)
	if match:
		L.append(match.group())
L = sorted(L)
print(L)


def is_valid(s):
	if s.count('@') != 1:
		return False
	if s.count('.') != 1:
		return False
	ext = s.split('.')[-1]
	usr = s.split('@')[0]
	web = s.split('@')[-1].split('.')[0]
	if len(ext) < 1 or len(usr) < 1 or len(web) < 1:
		return False
	if len(ext) > 3:
		return False
	if web.isalnum() == False:
		return False
	for char in usr:
		if char.isalnum() == False and char not in '_-':
			return False
	return True
E = []
N = int(input())
for _ in range(N):
	E.append(input())
E = list(filter(is_valid, E))
E.sort()
print(E)


lista = []
n = int(input())
for i in range(n):
	tekst = input()
	lista.append(tekst)
znaki = set('QWERTYUIOPASDFGHJKLZXCVBNM_-0123456789')
litcyf = set('QWERTYUIOPASDFGHJKLZXCVBNM0123456789')

def funkcja(aa):
	malpa = aa.find('@')
	if malpa != -1:
		if len(set(aa[:malpa].upper()).difference(znaki)) == 0 and len(aa[:malpa]) > 0:
			kropka = aa.find('.')
			if len(set(aa[malpa + 1:kropka].upper()).difference(litcyf)) == 0 and len(aa[malpa + 1:kropka]) > 0:
				if 0 < len(aa[kropka + 1:]) < 4:
					return True
lista = list(filter(lambda x: funkcja(x), lista))
lista = sorted(lista)
print(lista)


import re
addresses = []
for _ in range(int(input())):
	addresses.insert(0, input())
print(sorted(filter(lambda x: re.match('[\\w-]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]{0,3}$', x), addresses)))


import sys
import re
input()
print(sorted(list(filter(lambda x: re.search('^[a-z0-9_\\-]+@[a-z0-9]+\\..{1,3}$', x), sys.stdin.read().split('\n')))))


import re
number = int(input())
emails = [input() for x in range(number)]
emails.sort()
pattern = '^[a-zA-Z0-9_\\-]+[@][a-zA-Z0-9]+[.][a-zA-Z0-9]{1,3}$'
l = list(filter(lambda y: re.match(pattern, y), emails))
print(l)


import re
addresses = []
for _ in range(0, int(input())):
	addresses.append(input())
print(sorted(list(filter(lambda a: re.match('\\A[a-zA-Z0-9-_]+@([a-zA-Z0-9]+\\.)+[a-zA-Z0-9]{2,3}\\Z', a), addresses))))


import re
pattern = re.compile('[a-z0-9\\-_]+@[a-z0-9]+\\..{1,3}$')
N = int(input())
emails = []
for n in range(N):
	emails.append(input())
print(sorted(filter(lambda s: pattern.match(s), emails)))


import re
n = int(input())
addressesbefore = []
for i in range(n):
	addressesbefore.append(input())
addressesafter = list(filter(lambda address: re.match('[-a-z\\d_]+@[a-z\\d]+\\.[a-z\\d]{1,3}$', address, re.I), addressesbefore))
addressesafter.sort()
print(addressesafter)


import re
EMAIL = re.compile('^[\\w\\d-]+@[A-Za-z\\d]+\\.[a-zA-Z]{2,3}$')

def valid(email):
	return re.match(EMAIL, email)
l = [input() for _ in range(int(input()))]
print(sorted(filter(valid, l)))


from re import match
l = sorted([input() for _ in range(int(input()))])
print(list(filter(lambda i: match('^[a-zA-Z0-9_-]+\\@[a-zA-Z0-9]+\\.[a-zA-Z]{1,3}$', i), l)))


import re
n = int(input())
ar = []
for i in range(0, n):
	s = input()
	t = re.search('^[a-zA-Z][\\w-]*@[a-zA-Z0-9]+\\.[a-zA-Z]{1,3}$', s)
	if t:
		ar.append(s)
ar.sort()
print(ar)


import re
n = int(input())
adrs = []
for i in range(n):
	adrs.append(input())
adrs = list(filter(lambda x: re.match('^[\\w\\-]+@[a-zA-Z0-9]+\\.[a-zA-Z]{1,3}$', x), adrs))
print(sorted(adrs))


import re
email = re.compile('[\\w-]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]{1,3}$')
lines = [input() for i in range(int(input()))]
print(sorted(filter(email.match, lines)))


from sys import stdin
import re

def main():
	N = int(stdin.readline())
	ids = []
	for i in range(N):
		ids.append(stdin.readline().strip())
	l = sorted(filter(lambda x: re.fullmatch('[\\w-]+@[a-zA-Z0-9]+\\.[a-zA-Z]{1,3}', x), ids))
	print(l)
main()


import re
N = int(input())
emails = []
for i in range(N):
	email = input()
	if re.search('^(\\w|-)+@[a-z0-9]+\\.\\w{1,3}$', email):
		emails.append(email)
print(sorted(emails))


import re
N = int(input())
a = []
for i in range(N):
	a.append(input())
l = list(filter(lambda str: re.search('^([\\w-]+)@[0-9A-Za-z]+\\.[0-9A-Za-z]?[0-9A-Za-z]?[0-9A-Za-z]?$', str), a))
l.sort(key=lambda str: re.search('^([\\w-]+)@[0-9A-Za-z]+\\.\\w+$', str).group(1))
print(l)


import re
max = int(input())
emaillist = []
for i in range(0, max):
	emaillist.append(input())
finallist = list(filter(lambda x: re.match('^([A-Za-z0-9-_]+@[A-Za-z0-9]+\\.[A-Za-z]{2,3})$', x), emaillist))
finallist.sort()
print(finallist)


import re

def validEmail(email):
	if email[len(email) - 1] == ' ':
		return False
	return True if re.search('^(\\w|\\-)+@[A-Za-z0-9]+\\.[A-Za-z0-9]?[A-Za-z0-9]?[A-Za-z0-9]$', email) else False
e = []
for i in range(int(input())):
	e.append(input())
e = list(filter(lambda x: validEmail(x), e))
print(sorted(e))


import sys, re
n = int(input())
a = [input() for i in range(n)]
print(sorted([s.strip() for s in a if re.match('^[a-zA-Z0-9_-]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]{1,3}$', s)]))


import re
MAIL = re.compile('^[-_A-Za-z0-9]+@[A-Za-z0-9]+\\.[a-zA-Z]{2,3}$')
print(sorted(filter(lambda x: MAIL.match(x), (input() for _ in range(int(input()))))))


import re
n = int(input())
l = [input() for _ in range(n)]
print(sorted(filter(lambda s: re.match('^[a-zA-Z0-9_-]+@[a-zA-Z0-9]+\\.[a-z]{1,3}$', s), l)))


import re
lst = []
for i in range(int(input())):
	lst.append(input())
regex = re.compile('^[a-z\\d_-]+@[a-z\\d]+\\.[a-z]{2,3}$', re.I)
lst = list(filter(lambda s: regex.match(s), lst))
lst.sort()
print(lst)


import re
n = int(input())
p = re.compile('^[\\w\\-]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]{1,3}$')
emails = sorted(filter(lambda email: p.match(email), [input() for i in range(n)]))
print(emails)


import re

def valid(email):
	reg = re.compile('(^[a-zA-Z0-9][a-zA-Z0-9\\-_\\)\\(]*\\@[a-zA-Z0-9\\-\\)\\(]+\\.[a-zA-Z0-9]{2,3}$)')
	return reg.match(email)
n = int(input())
emails = []
for i in range(0, n):
	emails.append(input())
print(sorted(filter(lambda email: valid(email), emails)))


import re
ex = re.compile('^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\\.)+[a-zA-Z0-9]{1,3}$')
print(sorted(filter(ex.match, (input() for i in range(int(input()))))))


def a1(a):
	x = True
	if len(a) == 0:
		return False
	for i in range(len(a)):
		if (((a[i] >= 'a' and a[i] <= 'z' or (a[i] >= 'A' and a[i] <= 'Z')) or (a[i] >= '0' and a[i] <= '9')) or a[i] == '_') or a[i] == '-':
			continue
		else:
			x = False
	return x

def a2(a):
	x = True
	if len(a) == 0:
		return False
	for i in range(len(a)):
		if (a[i] >= 'a' and a[i] <= 'z' or (a[i] >= 'A' and a[i] <= 'Z')) or (a[i] >= '0' and a[i] <= '9'):
			continue
		else:
			x = False
	return x

def a3(a):
	x = True
	if len(a) > 3 or len(a) == 0:
		return False
	for i in range(len(a)):
		if a[i] >= 'a' and a[i] <= 'z' or (a[i] >= 'A' and a[i] <= 'Z'):
			continue
		else:
			x = False
	return x
n = int(input())
s = [''] * n
z = 0
p = []
for i in range(n):
	try:
		s[i] = input()
		s[i] = s[i].split('@')
		s[i][1] = s[i][1].split('.')
		a = True
		if (a1(s[i][0]) and a2(s[i][1][0])) and a3(s[i][1][1]):
			a = True
		else:
			a = False
		if a:
			p.append(s[i][0] + '@' + s[i][1][0] + '.' + s[i][1][1])
	except:
		continue
p.sort()
print(p)


def ans(s):
	address = [c for c in s]
	if '@' not in address:
		return False
	a = address.index('@')
	if '.' not in address:
		return False
	d = address.index('.')
	username = address[:a]
	sitename = address[a + 1:d]
	extname = address[d + 1:]
	if len(username) == 0:
		return False
	if len(sitename) == 0:
		return False
	if len(extname) == 0 or len(extname) > 3:
		return False
	ai = ord('a')
	Ai = ord('A')
	oi = ord('0')
	di = ord('-')
	ui = ord('_')
	c_username = []
	c_username += [i + ai for i in range(26)]
	c_username += [i + Ai for i in range(26)]
	c_username += [i + oi for i in range(10)]
	for c in sitename:
		if ord(c) not in c_username:
			return False
	for c in extname:
		if ord(c) not in c_username:
			return False
	c_username += [di] + [ui]
	for c in username:
		if ord(c) not in c_username:
			return False
	return True
input_list = []
for i in range(int(input())):
	s = input().strip()
	input_list.append(s)
input_list.sort()
input_list = list(filter(ans, input_list))
print(input_list)


import re
n = int(input())
l = []
p = re.compile('[a-zA-Z0-9\\-_]+@([a-zA-Z0-9]+\\.)+[a-zA-Z0-9]{1,3}')
for i in range(n):
	s = input()
	if p.fullmatch(s):
		l.append(s)
l.sort()
print(l)


import re
num = int(input())
emails = []
for i in range(num):
	emails.append(str(input()))
if len(emails) != 0:
	print(sorted(list(filter(lambda x: re.match('^([\\w-]+?){1}\\@{1}([a-zA-Z0-9]+?){1}\\.{1}([a-zA-Z]{1,3})$', x), emails))))


import string
N = int(input().strip())
emails = []
for i in range(N):
	emails.append(input().strip())

def validator(email):
	if email.count('@') != 1:
		return False
	(user, web) = email.split('@')
	if not user or not web:
		return False
	valid_web = string.ascii_letters + string.digits + '.'
	valid_user = string.ascii_letters + string.digits + '-_'
	for c in user:
		if c not in valid_user:
			return False
	for c in web:
		if c not in valid_web:
			return False
	if web.count('.') != 1:
		return False
	if not web.split('.')[0]:
		return False
	ext = web.split('.')[-1]
	if not 1 <= len(ext) <= 3:
		return False
	return True
filtered_emails = list(filter(validator, emails))
filtered_emails.sort()
print(filtered_emails)


import re
l = []
for _ in range(int(input())):
	s = str(input())
	if re.match('^[a-zA-Z0-9._%+-]+\\@[a-zA-Z0-9.-]+\\.[a-zA-Z]{3,}$', s) != None:
		l += [s]
l.sort()
print(l)


import sys
import re

def valid(str):
	if re.match('((\\w|\\-){1,})(\\@)([a-z|0-9]{1,})(\\.)([a-z]{1,3})$', str) == None:
		return False
	return True
result = []
n = int(sys.stdin.readline())
for i in range(0, n):
	str = sys.stdin.readline()
	result.append(str.rstrip('\n'))
result = list(filter(lambda x: valid(x) == True, result))
result.sort()
print(result)


import re

def check(s):
	one = s.split('@')
	if len(one) != 2:
		return False
	two = one[1].split('.')
	if len(two) != 2:
		return False
	return len(one[0]) > 0 and re.match('^[A-Za-z0-9_-]*$', one[0]) and (len(two[0]) > 0) and re.match('^[A-Za-z0-9]*$', two[0]) and two[1].isalpha() and (len(two[1]) > 0 and len(two[1]) < 4)
N = int(input())
emails = list()
for n in range(N):
	emails.append(input())
emails = list(filter(lambda x: check(x), emails))
emails.sort()
print(emails)


import re
N = input()
lst = []
for i in range(0, int(N)):
	lst.append(input())
lst = list(filter(lambda x: x.count('@') == 1 and x.count('.') == 1, lst))
lst = list(filter(lambda x: len(x.split('.')[1]) <= 3 and x.split('.')[1].isalnum() and x.split('.')[0].split('@')[1].isalnum(), lst))
flst = []
for email in lst:
	match = re.search('[\\w\\-]+', email.split('@')[0])
	if match and len(match.group(0)) == len(email.split('@')[0]):
		flst.append(email)
print(sorted(flst))


import re
num = int(input())
lis = []
op = []
for i in range(0, num):
	x = input()
	try:
		if re.match('^[A-Za-z0-9_-]*$', x.split('@')[0]):
			if re.match('^[A-Za-z0-9]*$', x.split('@')[1].split('.')[0]):
				lis.append(x)
	except:
		bob = 0
op = list(filter(lambda x: len(x.split('.')) == 2 and len(x.split('@')) == 2 and (len(x.split('.')[1]) <= 3) and (len(x.split('.')[0]) > 0) and (len(x.split('@')[0]) > 0), lis))
print(sorted(op))


import re
input_list = []
filtered_list = []
total_email_addresses = int(input())
for i in range(total_email_addresses):
	input_list.append(input())
for email in input_list:
	if re.match('[-A-Za-z0-9_]+@[A-Za-z0-9.]+\\.[A-Za-z][A-Za-z][A-Za-z]?$', email):
		filtered_list.append(email)
print(sorted(filtered_list))


import re
pattern = re.compile('^[a-z0-9-_]+@[a-z0-9]+\\.((\\w\\w\\w)|(\\w\\w)|(\\w))$')
N = int(input())
list_of_email = list()
for _ in range(N):
	list_of_email.append(input())
list_of_email = sorted(list(filter(lambda x: pattern.match(x), list_of_email)))
print(list_of_email)


import re

def is_valid_email(s):
	if re.search('^(\\w|\\-)+\\@[a-zA-Z0-9]+\\.[a-zA-Z0-9]{1,3}$', s) != None:
		return True
	else:
		return False
N = int(input())
ar = []
for i in range(N):
	ar += [input()]
print(sorted(filter(is_valid_email, ar)))


import re
n = int(input())
list = []
while n > 0:
	n -= 1
	address = input()
	match = re.search('^[-\\w]+@[A-Za-z0-9]+\\.[A-Za-z]{0,3}$', address)
	if match:
		list.append(address)
list.sort()
print(list)


import re
email_list = [input() for i in range(int(input()))]
v_pattern = '^[\\w-]+@[a-zA-Z0-9.]+\\.[a-zA-Z0-9]{1,3}$'
validation = re.compile(v_pattern)

def validate_email(email):
	if validation.match(email):
		return True
	else:
		return False
validated_list = sorted(list(filter(validate_email, email_list)))
print(validated_list)


import re
N = int(input())
l = []
for i in range(N):
	s = input()
	if re.match('^(\\w|-)+@[A-Za-z0-9]+\\.[A-Za-z0-9]{1,3}$', s) != None:
		l.append(s)
l.sort()
print(l)


import re
N = int(input())
lst = []
for i in range(N):
	lst.append(input())
print(sorted(list(filter(lambda x: re.match('^[a-zA-Z0-9_-]+\\@[a-zA-Z0-9]+\\.[a-zA-Z]{,3}$', x), lst))))


import re
test = int(input())
lst = []
for i in range(test):
	email = input()
	match = re.match('^[-0-9a-zA-Z_]+@[-0-9a-zA-Z]+\\.[a-zA-Z]{1,3}$', email)
	if match:
		lst.append(email)
lst.sort()
print(lst)


import re

def isValidEmail(s):
	return None != re.match('^[\\w_-]+@[a-zA-Z0-9]+\\.[a-zA-Z]{1,3}$', s)
print(sorted(list(filter(isValidEmail, [input() for i in range(int(input()))]))))


import re
n = int(input())
a = []
for i in range(n):
	a.append(input())
r = re.compile('^[A-z0-9_-]+@[A-Za-z0-9]+\\.[A-z]{,3}$')
print(list(sorted(filter(r.match, a))))


import re
xs = [input() for _ in range(int(input()))]
xs = [x for x in xs if re.match('^[\\w\\-]+@[a-zA-Z0-9]*\\.[a-zA-Z]{1,3}$', x)]
xs.sort()
print(xs)


import re
from operator import itemgetter
n = int(input())
emails = []
while n != 0:
	email = input()
	if re.match('^[a-zA-Z0-9_-]+\\@[a-zA-Z0-9]+\\.[a-zA-Z0-9]{2,3}$', email):
		username = email.split(sep='@')
		domain = username[1]
		username = username[0]
		website = domain.split('.')
		tld = website[1]
		website = website[0]
		emails.append([username, website, tld])
	n -= 1
emails.sort(key=itemgetter(2))
emails.sort(key=itemgetter(1))
emails.sort(key=itemgetter(0))
final = ['{0}@{1}.{2}'.format(a[0], a[1], a[2]) for a in emails]
print(final)