You and Fredrick are good friends. Yesterday, Fredrick received $N$ credit cards from ABCD Bank. He wants to verify whether his credit card numbers are valid or not. You happen to be great at regex so he is asking for your help!

A valid credit card from ABCD Bank has the following characteristics: 

► It must start with a $4$, $5$ or $6$. 

► It must contain exactly $\mbox{16}$ digits. 

► It must only consist of digits ($\mbox{0}$-$\mbox{9}$). 

► It may have digits in groups of $4$, separated by one hyphen "-". 

► It must NOT use any other separator like ' ' , '_', etc. 

► It must NOT have $4$ or more consecutive repeated digits.    

Examples:  

Valid Credit Card Numbers  

4253625879615786
4424424424442444
5122-2368-7954-3214

Invalid Credit Card Numbers  

42536258796157867       #17 digits in card number → Invalid 
4424444424442444        #Consecutive digits are repeating 4 or more times → Invalid
5122-2368-7954 - 3214   #Separators other than '-' are used → Invalid
44244x4424442444        #Contains non digit characters → Invalid
0525362587961578        #Doesn't start with 4, 5 or 6 → Invalid

Input Format

The first line of input contains an integer $N$. 

The next $N$ lines contain credit card numbers.  

Constraints  

$0<N<100$

Output Format

Print 'Valid' if the credit card number is valid. Otherwise, print 'Invalid'. Do not print the quotes.

Sample Input
6
4123456789123456
5123-4567-8912-3456
61234-567-8912-3456
4123356789123456
5133-3367-8912-3456
5123 - 3567 - 8912 - 3456

Sample Output
Valid
Valid
Invalid
Valid
Invalid
Invalid

Explanation

4123456789123456 : Valid 

5123-4567-8912-3456  : Valid 

61234-567-8912-3456  : Invalid, because the card number is not divided into equal groups of 4. 

4123356789123456  : Valid 

5133-3367-8912-3456  : Invalid, consecutive digits $3333$ is repeating 4 times. 

5123-4567-8912-3456 :  Invalid, because space '  ' and -  are used as separators.

Source: hackerrank | Difficulty: MEDIUM


Solutions:
import re
for _ in range(int(input())):
	number = input().strip()
	if len(number) != 16 and len(number) != 19:
		print('Invalid')
		continue
	if number[0] not in '456':
		print('Invalid')
		continue
	if re.search('[^0-9\\-]', number):
		print('Invalid')
		continue
	if len(number) == 19 and (not bool(re.search('\\d{4}-\\d{4}-\\d{4}', number))):
		print('Invalid')
		continue
	pure = re.sub('\\-', '', number)
	if re.search('.*(\\d)\\1{3,}.*', pure):
		print('Invalid')
		continue
	print('Valid')


import re
for _ in range(int(input())):
	s = input()
	print('Valid' if re.match('^[4-6]\\d{3}(-)?\\d{4}\\1?\\d{4}\\1?\\d{4}$', s) and (not re.search('(.)\\1{3,}', re.sub('-', '', s))) else 'Invalid')


def is_valid(s):
	if s[0] not in ['4', '5', '6']:
		return False
	if len(s) != 16 and len(s) != 19:
		return False
	if s[4] == '-' and s[9] == '-' and s[14]:
		s = s[:14] + s[15:]
		s = s[:9] + s[10:]
		s = s[:4] + s[5:]
	if not s.isdigit():
		return False
	for i in range(1, 10):
		if str(i) * 4 in s:
			return False
	return True

def main():
	n = int(input())
	for _ in range(n):
		s = input()
		print('Valid' if is_valid(s) else 'Invalid')
main()


valid = 'Valid'
invalid = 'Invalid'
allowed = '1234567890'
tests = int(input())
for test in range(tests):
	res = True
	s = input()
	nums = list(filter(lambda c: c in allowed, s))

	def fourConseq(i):
		return nums[i:i + 4].count(nums[i]) == 4

	def dashesOK():
		if '-' in s:
			return s[4:15:5].count('-') == 3 and len(s) == 19
		return True
	res &= len(nums) == 16
	res &= nums[0] in '456'
	four = list(filter(fourConseq, range(12)))
	res &= len(four) == 0
	res &= dashesOK()
	if res:
		print(valid)
	else:
		print(invalid)


import re

def credit(ls):
	xs = list(ls)
	if xs[0] in '456':
		test = False
		i = 0
		while i < 13 and (not test):
			v = xs[i]
			test = v == xs[i + 1] and v == xs[i + 2] and (v == xs[i + 3])
			i += 1
		if not test:
			return 'Valid'
	return 'Invalid'

def bank(card):
	valid1 = re.compile('^\\d{16}$')
	valid2 = re.compile('^\\d{4}-\\d{4}-\\d{4}-\\d{4}$')
	if valid1.match(card):
		return credit(card)
	elif valid2.match(card):
		return credit(''.join(card.split('-')))
	else:
		return 'Invalid'
for i in range(int(input())):
	print(bank(input()))


import re
cc_pattern = re.compile('^[4-6]\\d\\d\\d-?\\d\\d\\d\\d-?\\d\\d\\d\\d-?\\d\\d\\d\\d$')
repeat_pattern = re.compile('^.*(?P<x>\\d)((-?(?P=x)(?P=x)(?P=x))|((?P=x)-(?P=x)(?P=x))|((?P=x)(?P=x)-(?P=x))).*$')
n = int(input())
for i in range(0, n):
	cc = input()
	if cc_pattern.match(cc) and (not repeat_pattern.match(cc)):
		print('Valid')
	else:
		print('Invalid')


import re
t = int(input())
for j in range(0, t):
	n = list(input())
	l = len(n)
	c = 0
	if l == 16 or l == 19:
		c += 1
		if n[0] == '4' or n[0] == '5' or n[0] == '6':
			c += 1
			if l == 19:
				if n[4] == '-' and n[9] == '-' and (n[14] == '-'):
					c += 1
				n[4] = n[9] = n[14] = 'x'
			else:
				c += 1
			d = 0
			p = []
			for i in range(0, l):
				if n[i].isnumeric():
					p.append(n[i])
			c += 1
			for i in range(1, len(p)):
				if p[i] == p[i - 1]:
					d += 1
				else:
					d = 0
				if d == 3:
					c = 0
				c += 1
	if c == 19:
		print('Valid')
	else:
		print('Invalid')


import re

def fun(x):
	l = []
	m = 0
	l = x.split('-')
	r = ''.join(l)
	if len(r) == 16:
		count = 0
		if re.match('^[4-6]{1}[0-9]{3}[-]?[0-9]{4}[-]?[0-9]{4}[-]?[0-9]{4}[-]?', x):
			for i in range(0, len(r) - 1):
				if r[i] == r[i + 1]:
					count += 1
					if count == 3:
						m = 1
						break
					else:
						continue
				else:
					count = 0
		else:
			m = 1
	else:
		m = 1
	if m == 1:
		print('Invalid')
	else:
		print('Valid')
t = int(input())
for i in range(0, t):
	x = input()
	fun(x)


import re
for _ in range(int(input())):
	s = input()
	if re.match('^[456]\\d{3}(-?)\\d{4}(\\1\\d{4}){2}$', s) and (not re.search('(\\d)(-?\\1-?){3}', s)):
		print('Valid')
	else:
		print('Invalid')


import re
num_cc = int(input())
for x in range(num_cc):
	cc = input()
	if not cc[0] in '456':
		print('Invalid')
		continue
	if re.match('(\\d{4}-\\d{4}-\\d{4}-\\d{4}$)|\\d{16}$', cc):
		if re.search('1{4}|2{4}|3{4}|4{4}|5{4}|6{4}|7{4}|8{4}|9{4}', cc.replace('-', '')):
			print('Invalid')
		else:
			print('Valid')
	else:
		print('Invalid')


import re
n = int(input())
for t in range(n):
	credit = input().strip()
	credit_removed_hiphen = credit.replace('-', '')
	valid = True
	length_16 = bool(re.match('^[4-6]\\d{15}$', credit))
	length_19 = bool(re.match('^[4-6]\\d{3}-\\d{4}-\\d{4}-\\d{4}$', credit))
	consecutive = bool(re.findall('(?=(\\d)\\1\\1\\1)', credit_removed_hiphen))
	if length_16 == True or length_19 == True:
		if consecutive == True:
			valid = False
	else:
		valid = False
	if valid == True:
		print('Valid')
	else:
		print('Invalid')


import re
input_size = int(input())
input_cards = []
for i in range(0, input_size):
	input_cards.append(input())
pattern = '^(4|5|6)\\d{3}-?\\d{4}-?\\d{4}-?\\d{4}$'
result = re.compile(pattern)
for card_number in input_cards:
	if result.match(card_number):
		if re.search('(\\d)-?\\1-?\\1-?\\1', card_number):
			print('Invalid')
		else:
			print('Valid')
	else:
		print('Invalid')


import re
N = int(input())
for each in range(N):
	number = input().strip()
	if number[0] != '4' and number[0] != '5' and (number[0] != '6'):
		print('Invalid')
		continue
	if len(number) == 16 and bool(re.search('^\\d{16}$', number)) or bool(re.search('^\\d{4}\\-\\d{4}\\-\\d{4}\\-\\d{4}$', number)):
		pass
	else:
		print('Invalid')
		continue
	if bool(re.search('^\\d{4}\\-\\d{4}\\-\\d{4}\\-\\d{4}$', number)):
		n = number.split('-')
		number = n[0] + n[1] + n[2] + n[3]
	if bool(re.search('(.)\\1{3}', number)):
		print('Invalid')
		continue
	print('Valid')


for _ in range(int(input())):
	card_num = input()
	if card_num[0] != '4' and card_num[0] != '5' and (card_num[0] != '6'):
		print('Invalid')
		continue
	dig_count = 0
	consec = 1
	prev = card_num[0]
	loop_broken = False
	for i in range(len(card_num)):
		if card_num[i] != '-':
			if i != 0 and card_num[i] == prev:
				consec += 1
			else:
				consec = 1
			if card_num[i] != '-':
				prev = card_num[i]
		if consec >= 4:
			print('Invalid')
			loop_broken = True
			break
		if ord(card_num[i]) < 48 or ord(card_num[i]) > 57:
			if i != 4 and i != 9 and (i != 14) or card_num[i] != '-':
				print('Invalid')
				loop_broken = True
				break
		else:
			dig_count += 1
	if not loop_broken:
		if dig_count != 16:
			print('Invalid')
		else:
			print('Valid')


import re
n = int(input())
regex_for_valid_credit_card_number_ignoring_repeats = re.compile('^[456][0-9]{3}\\-?[0-9]{4}\\-?[0-9]{4}\\-?[0-9]{4}$')
regex_for_our_consecutive_characters = re.compile('(.)\\1\\1\\1')
for _ in range(n):
	number = input()
	if regex_for_valid_credit_card_number_ignoring_repeats.match(number):
		number_without_hyphens = number.replace('-', '')
		if regex_for_our_consecutive_characters.search(number_without_hyphens):
			print('Invalid')
		else:
			print('Valid')
	else:
		print('Invalid')


n = int(input())
for j in range(n):
	text = input()
	noDigits = 0
	noConseqDigits = 0
	noHyphonCount = 0
	noHyphonConseqCount = 0
	invalid = False
	if not (text[0] == '4' or text[0] == '5' or text[0] == '6'):
		invalid = True
	noConseqNumber = 0
	stripText = ''
	for i in range(len(text)):
		if text[i].isdigit():
			noDigits += 1
			noConseqDigits += 1
			noHyphonConseqCount = 0
			stripText += text[i]
		elif text[i] == '-':
			noHyphonConseqCount += 1
			noHyphonCount += 1
			if noConseqDigits != 4:
				invalid = True
				break
			noConseqDigits = 0
		else:
			invalid = True
			break
		if noHyphonConseqCount > 1:
			invalid = True
			break
	if not text[len(text) - 1].isdigit() or len(stripText) != 16:
		invalid = True
	if (noConseqDigits != 4 and noConseqDigits != 0) and noHyphonCount > 0:
		invalid = True
	for i in range(len(stripText) - 1):
		noConseqNumber += 1
		if stripText[i] != stripText[i + 1]:
			if noConseqNumber > 3:
				invalid = True
				break
			noConseqNumber = 0
	if len(stripText) > 2:
		if stripText[len(stripText) - 2] != stripText[len(stripText) - 1]:
			if noConseqNumber > 3:
				invalid = True
	if invalid:
		print('Invalid')
	else:
		print('Valid')


import time
t = int(input().strip())
for z in range(t):
	s = input().strip()
	flag = 0
	if True:
		for z in s:
			if z not in '0123456789-':
				flag = 1
				break
		if flag == 1:
			print('Invalid')
			continue
	try:
		count = 0
		for z in s:
			if ord(z) >= 48 and ord(z) <= 59:
				count += 1
		if count != 16:
			print('Invalid')
			continue
	except:
		None
	try:
		if s[0] not in '456':
			print('Invalid')
			continue
	except:
		None
	sd = s
	try:
		sd = s.split('-')
	except:
		None
	flag = 0
	for z in sd:
		if len(z) == 4 or len(z) == 16:
			None
		else:
			flag = 1
			break
	if flag == 1:
		print('Invalid')
		continue
	kl = 0
	try:
		kl = s[0]
	except:
		None
	count = 1
	flag = 0
	try:
		for z in range(1, len(s)):
			if s[z] in '0123456789':
				if s[z] == kl:
					count += 1
				else:
					count = 1
				if count >= 4:
					flag = 1
					break
				kl = s[z]
			else:
				continue
	except:
		None
	if flag == 1:
		print('Invalid')
		continue
	print('Valid')


import re
pattern = re.compile('^[4|5|6][0-9]{3}' + '[-]?[0-9]{4}' * 3 + '$')
rep = re.compile('([0-9])\\1{3,}')
for _ in range(int(input())):
	s = input()
	result = pattern.match(s) and len([m.group() for m in rep.finditer(re.sub('-', '', s))]) == 0
	print('Valid' if result else 'Invalid')


t = int(input())
a = []
for i in range(0, t):
	flag = 0
	a = list(input())
	if a[0] != '4' and a[0] != '5' and (a[0] != '6'):
		print('Invalid')
		flag = 1
		continue
	elif (len(a) == 16 or (len(a) == 19 and (a[4] == '-' and a[9] == '-' and (a[14] == '-')))) == 0:
		print('Invalid')
		flag = 1
		continue
	else:
		if len(a) == 19:
			for j in range(0, 4):
				if ord(a[j]) < 48 or ord(a[j]) > 57:
					print('Invalid')
					flag = 1
					continue
			for j in range(5, 9):
				if ord(a[j]) < 48 or ord(a[j]) > 57:
					print('Invalid')
					flag = 1
					continue
			for j in range(10, 14):
				if ord(a[j]) < 48 or ord(a[j]) > 57:
					print('Invalid')
					flag = 1
					continue
			for j in range(15, 19):
				if ord(a[j]) < 48 or ord(a[j]) > 57:
					print('Invalid')
					flag = 1
					continue
			b = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
			b[0] = 1
			for j in range(1, 4):
				if a[j] == a[j - 1]:
					b[j] = b[j - 1] + 1
				else:
					b[j] = 1
			if a[5] == a[3]:
				b[5] = b[3] + 1
			else:
				b[5] = 1
			for j in range(6, 9):
				if a[j] == a[j - 1]:
					b[j] = b[j - 1] + 1
				else:
					b[j] = 1
			if a[10] == a[8]:
				b[10] = b[8] + 1
			else:
				b[10] = 1
			for j in range(11, 14):
				if a[j] == a[j - 1]:
					b[j] = b[j - 1] + 1
				else:
					b[j] = 1
			if a[15] == a[13]:
				b[15] = b[13] + 1
			else:
				b[15] = 1
			for j in range(16, 19):
				if a[j] == a[j - 1]:
					b[j] = b[j - 1] + 1
				else:
					b[j] = 1
			for j in range(0, 19):
				if b[j] == 4:
					print('Invalid')
					flag = 1
					continue
		else:
			for j in range(0, 16):
				if ord(a[j]) < 48 or ord(a[j]) > 57:
					print('Invalid')
					flag = 1
					continue
			b = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
			b[0] = 1
			for j in range(1, 16):
				if a[j] == a[j - 1]:
					b[j] = b[j - 1] + 1
				else:
					b[j] = 1
			for j in range(0, 16):
				if b[j] == 4:
					print('Invalid')
					flag = 1
					continue
		if flag == 1:
			continue
		print('Valid')


import re

def credit(ls):
	if 16 == len(ls):
		rs = re.findall('\\d', ls)
		if 16 == len(rs):
			xs = list(ls)
			if xs[0] in '456':
				test = False
				i = 0
				while i < 13 and (not test):
					v = xs[i]
					test = v == xs[i + 1] and v == xs[i + 2] and (v == xs[i + 3])
					i += 1
				if not test:
					return 'Valid'
	return 'Invalid'

def bank(card):
	valid1 = re.compile('^\\d{16}$')
	valid2 = re.compile('^\\d{4}-\\d{4}-\\d{4}-\\d{4}$')
	if valid1.match(card):
		return credit(card)
	elif valid2.match(card):
		return credit(''.join(card.split('-')))
	else:
		return 'Invalid'
for i in range(int(input())):
	print(bank(input()))


import re
__author__ = 'namii'
patterni = '^[456][0-9]{15}$|^[456][0-9]{3}-[0-9]{4}-[0-9]{4}-[0-9]{4}$'

def no_four_consecutives(word):
	only_ints = word.replace('-', '')
	for i in range(len(only_ints) - 3):
		substring = only_ints[i:i + 4]
		if substring == len(substring) * substring[0]:
			return False
	return True
N = int(input())
for _ in range(N):
	current = input()
	if re.match(patterni, current) == None:
		print('Invalid')
	elif no_four_consecutives(current):
		print('Valid')
	else:
		print('Invalid')


import operator
import string

def read_f(f):
	return f(input())

def v(cc):
	if cc[0] not in '456':
		return False
	ccnoh = ''.join(list(filter(lambda x: x != '-', cc)))
	if len(ccnoh) != 16:
		return False
	if any(map(lambda x: x not in string.digits, ccnoh)):
		return False
	for i in range(10):
		if str(i) * 4 in ccnoh:
			return False
	if '-' in cc:
		ps = cc.split('-')
		if any(map(lambda x: len(x) != 4, ps)):
			return False
	return True
n = read_f(int)
for i in range(n):
	cc = input()
	if v(cc):
		print('Valid')
	else:
		print('Invalid')


p = print
n = int(input().strip())
for __ in range(n):
	s = input()
	if s[0] not in ['4', '5', '6'] or not (len(s) == 16 or len(s) == 19):
		flag = 1
	else:
		flag = 0
		for i in s:
			if i not in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '-']:
				flag = 1
				break
		if '-' in s and flag == 0:
			if len(s) != 19:
				flag = 1
			elif s[4] != '-' or s[9] != '-' or s[14] != '-':
				flag = 1
			else:
				s = s[0:4] + s[5:9] + s[10:14] + s[15:19]
				if '-' in s:
					flag = 1
		if flag == 0:
			for i in range(1, 9 + 1):
				if str(i) * 4 in s:
					flag = 1
					break
	if flag:
		print('Invalid')
	else:
		print('Valid')


import re

def chk1(a):
	if a[0] != '4' and a[0] != '5' and (a[0] != '6'):
		return False
	r = a.split('-')
	if len(r) == 1 and r[0].isnumeric() and (len(r[0]) == 16):
		return True
	if len(r) != 4:
		return False
	if len(r[0]) != 4 or len(r[1]) != 4 or len(r[2]) != 4 or (len(r[3]) != 4):
		return False
	if not ''.join(r).isnumeric():
		return False
	return True

def chk2(a):
	a = a.replace('-', '')
	for (a, b) in re.findall('((\\w)\\2*)', a):
		if len(a) > 3:
			return False
	return True
n = int(input())
for i in range(n):
	a = input()
	if chk1(a) and chk2(a):
		print('Valid')
	else:
		print('Invalid')


import re
for _ in range(int(input())):
	cc = input().strip()
	if '-' in cc:
		if re.match('(\\d{4}-){3}\\d{4}', cc):
			cc = re.sub('-', '', cc)
		else:
			print('Invalid')
			continue
	if len(cc) == 16 and re.match('[456]\\d{15}', cc) and (not re.search('.*(.)\\1{3}', cc)):
		print('Valid')
	else:
		print('Invalid')


import re
k = int(input())
for i in range(k):
	s = input()
	if re.match('^([4-6]{1}\\d{3})([\\s-]?)(\\d{4})([\\s-]?)(\\d{4})([\\s-]?)(\\d{4})$', s):
		if re.match('^.*(.)(\\1)(\\1)(\\1).*$', s.strip().replace('-', '')):
			print('Invalid')
		else:
			print('Valid')
	else:
		print('Invalid')


import re
prog = re.compile('^([456]\\d{3})-?(\\d{4})-?(\\d{4})-?(\\d{4})$')
ncases = int(input())
for _ in range(ncases):
	s = input()
	result = prog.match(s)
	repeats = False
	if result:
		s2 = re.sub('-', '', s)
		lastchar = 'x'
		count = 0
		for c in s2:
			if c == lastchar:
				count += 1
			else:
				lastchar = c
				count = 0
			if count == 3:
				repeats = True
				break
	if result != None and (not repeats):
		out = 'Valid'
	else:
		out = 'Invalid'
	print(out)


N = int(input())

def check(n):
	if n.isdigit():
		if n[0] in ['4', '5', '6'] and len(n) == 16 and all([not str(i) * 4 in n for i in range(0, 10)]):
			return True
		else:
			return False
	else:
		return False
for _ in range(N):
	s = input()
	if check(s) or (check(''.join(s.split('-'))) and all([len(i) == 4 for i in s.split('-')])):
		print('Valid')
	else:
		print('Invalid')


import re
first = '^[456][\\d]{3}\\-?[\\d]{4}\\-?[\\d]{4}\\-?[\\d]{4}$'
second = '(.)\\1{3}'
for _ in range(int(input())):
	card = input()
	if re.match(first, card) is not None:
		card = ''.join((c for c in card if c.isdigit()))
		if re.search(second, card) is None:
			print('Valid')
			continue
	print('Invalid')


import re
N = int(input())
for i in range(N):
	number = input()
	valid = True
	check = re.match('^[456]', number)
	if not check:
		valid = False
	check1 = re.match('^\\d{16}$', number)
	check2 = re.match('^\\d{4}-\\d{4}-\\d{4}-\\d{4}$', number)
	if not check1 and (not check2):
		valid = False
	repeatCnt = 0
	prev = '\x00'
	for i in range(len(number)):
		if number[i] == '-':
			continue
		elif number[i] == prev:
			repeatCnt += 1
		elif number[i] != prev:
			prev = number[i]
			repeatCnt = 1
		if repeatCnt == 4:
			break
	if repeatCnt == 4:
		valid = False
	if valid:
		print('Valid')
	else:
		print('Invalid')


import re
for _ in range(int(input())):
	s = input()
	print('Valid' if all([re.search(r, s) for r in ['^[456]', '^(\\d{4}-?){4}$']]) and (not re.search('(\\d)(-|\\1){4}', s)) else 'Invalid')


import re
N = int(input())
for _ in range(N):
	number = input()
	ismatch = re.match('^(4|5|6)[0-9]{3}(-?[0-9]{4}){3}$', number) != None
	if not ismatch:
		print('Invalid')
		continue
	digits = list('1234567890')
	for digit in digits:
		if re.search('(-?' + digit + '-?){4}', number) != None:
			print('Invalid')
			break
	else:
		print('Valid')


import re
for _ in range(int(input())):
	cc = input()
	valid = True
	if re.fullmatch('[4-6]\\d{3}(-?\\d{4}){3}', cc) == None or re.search('(\\d)(-?\\1){3}', cc):
		valid = False
	if valid:
		print('Valid')
	else:
		print('Invalid')


import re
t = int(input())
for i in range(0, t):
	x = input()
	if x.count('-') > 0:
		a = x.split('-')
		p = 1
		if len(a) != 4:
			p = None
			a = []
		for b in a:
			if len(b) != 4:
				p = None
				break
	else:
		p = re.search('[456][0-9]{15}', x)
	x = x.replace('-', '')
	q = re.search('.*([0-9])\\1{3}.*', x)
	if p != None and q == None:
		print('Valid')
	else:
		print('Invalid')


def remove_values_from_list(the_list, val):
	while val in the_list:
		the_list.remove(val)
	return the_list
T = int(input())
digits = '0123456789'
digits1 = '-0123456789'
firsts = '456'
for i in range(0, T):
	code = input()
	if len(code) == 16:
		if code[0] not in firsts:
			print('Invalid')
		else:
			flag = 0
			for i in code:
				if i not in digits:
					flag = 1
					break
			if flag == 1:
				print('Invalid')
			else:
				flag = 0
				for i in range(0, len(code)):
					key = code[i]
					cnt = 0
					for j in range(i, len(code)):
						if code[j] == key:
							cnt += 1
						else:
							break
					if cnt >= 4:
						flag = 1
						break
				if flag == 1:
					print('Invalid')
				else:
					print('Valid')
	elif len(code) == 19:
		if code[0] not in firsts:
			print('Invalid')
		elif code[4] == '-' and code[9] == '-' and (code[14] == '-'):
			flag = 0
			for i in code:
				if i not in digits1:
					flag = 1
					break
			if flag == 1:
				print('Invalid')
			else:
				mylist = list(code)
				newlist = remove_values_from_list(mylist, '-')
				code = ''
				for i in newlist:
					code += i
				flag = 0
				for i in range(0, len(code)):
					key = code[i]
					cnt = 0
					for j in range(i, len(code)):
						if code[j] == key:
							cnt += 1
						else:
							break
					if cnt >= 4:
						flag = 1
						break
				if flag == 1:
					print('Invalid')
				else:
					print('Valid')
		else:
			print('Invalid')
	else:
		print('Invalid')


def ok(s):
	import re
	if not s[0] in '456':
		return False
	if not re.match('[0-9-]+', s):
		return False
	if sum([1 if '0' <= _ and _ <= '9' else 0 for _ in s]) != 16:
		return False
	if not (len(s) == 16 or (len(s) == 19 and s[4] == '-' and (s[9] == '-') and (s[14] == '-'))):
		return False
	s = s.replace('-', '')
	for i in range(len(s) - 3):
		if s[i] == s[i + 1] and s[i] == s[i + 2] and (s[i] == s[i + 3]):
			return False
	return True
import sys
stdin = sys.stdin
t = int(stdin.readline())
import re
for z in range(t):
	line = stdin.readline().rstrip()
	if ok(line):
		print('Valid')
	else:
		print('Invalid')


import re

def validCard(card):
	if not re.match('^[0-9]{16}$', card) and (not re.match('^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}$', card)):
		return False
	card = card.replace('-', '')
	if not card or card[0] not in ['4', '5', '6']:
		return False
	pre = 0
	for i in range(1, len(card) + 1):
		if i == len(card) or card[i] != card[pre]:
			if i - pre >= 4:
				return False
			pre = i
	return True
N = int(input())
for i in range(N):
	card = input()
	if validCard(card):
		print('Valid')
	else:
		print('Invalid')


import re
noOfTest = int(input())
for i in range(0, noOfTest):
	str1 = input()
	lower = re.findall('(^[456]\\d{15}|^[456]\\d{3}\\-\\d{4}-\\d{4}-\\d{4})$', str1)
	str1 = str1.replace('-', '')
	status = 'Invalid'
	if len(lower) == 1:
		lower1 = re.findall('(.)\\1{3,}', str1)
		if len(lower1) == 0:
			status = 'Valid'
	print(status)


import re

def isValid(creditCardNumber):
	return re.match('[456][0-9]{3}(-?[0-9]{4}){3}$', creditCardNumber) and (not re.search('([0-9])\\1{3}', re.sub('-', '', creditCardNumber)))
for _ in range(int(input())):
	if isValid(input()):
		print('Valid')
	else:
		print('Invalid')


import re
n = int(input())
pattern = '^[4-6]{1}[0-9]{3}([-]{0,1}[0-9]{4}){3}$'
pattern_rep = '(\\d)\x01{3,16}'
for i in range(n):
	mystring = input().strip()
	mystring2 = ''.join(mystring.split('-'))
	prog = re.compile(pattern)
	prog2 = re.compile(pattern_rep)
	result = None
	result = prog.match(mystring)
	result2 = re.search('(\\d)\\1{3,}', mystring2)
	if result == None or result2 != None:
		print('Invalid')
	else:
		print('Valid')


import re

def check(s):
	if re.match('^[456]', s) is None:
		return 'Invalid'
	if re.match('[^0-9\\-]', s):
		return 'Invalid'
	if len(re.findall('[0-9]', s)) != 16:
		return 'Invalid'
	dashes = len(re.findall('\\-', s))
	if dashes != 0 and dashes != 3:
		return 'Invalid'
	if dashes == 3:
		for i in range(len(s))[4::5]:
			if s[i] != '-':
				return 'Invalid'
	if re.findall('([0-9])[-]*\\1[-]*\\1[-]*\\1', s):
		return 'Invalid'
	return 'Valid'
n = int(input())
for i in range(n):
	test = input()
	print(check(test))


import re
n = int(input())
c1 = re.compile('^(?=.{16,16})(?!.*([0-9])\\1{3})[4-6][0-9]+$')
c2 = re.compile('^(?=.{19,19})(?!.*([0-9])\\1{3})[4-6][0-9]{3}(\\-[0-9]{4}){3}$')
for _ in range(n):
	s = input()
	s_new = s.replace('-', '')
	if c1.match(s) != None:
		print('Valid')
	elif c2.match(s) != None:
		if c1.match(s_new) != None:
			print('Valid')
		else:
			print('Invalid')
	else:
		print('Invalid')


import re

def __starting_point():
	t = int(input().strip())
	for _ in range(t):
		num = ''.join(input())
		if re.match('^[456]', num) and (re.match('([\\d]{4}-){3}[\\d]{4}$', num) or re.match('[\\d]{16}', num)) and (not re.search('(\\d)\\1{3,}', num.replace('-', ''))):
			print('Valid')
		else:
			print('Invalid')
__starting_point()


import re
n = int(input())
l = [input() for x in range(n)]
for crn in l:
	x = re.findall('^([4-6][0-9]{3}-?)([0-9]{4}-?)([0-9]{4}-?)([0-9]{4})$', crn)
	if x:
		z = ''.join(x[0])
		h = re.findall('-', z)
		if len(h) == 3 or len(h) == 0:
			z = z.replace('-', '')
			rep = re.findall('[1]{4,}|[2]{4,}|[3]{4,}|[4]{4,}|[5]{4,}|[6]{4,}|[7]{4,}|[8]{4,}|[9]{4,}]', z)
			if rep:
				print('Invalid')
			else:
				print('Valid')
		else:
			print('Invalid')
	else:
		print('Invalid')


import re
from itertools import groupby
for _ in range(int(input())):
	l = input()
	r = l.replace('-', '')
	if not re.match('^[4-6]', l):
		print('Invalid')
	elif len(r) != 16:
		print('Invalid')
	elif not re.match('^\\d+$', r):
		print('Invalid')
	elif len(l.split('-')) not in (1, 4):
		print('Invalid')
	elif len([1 for s in l.split('-') if len(s) not in (4, 16)]):
		print('Invalid')
	elif [1 for (k, v) in groupby(r) if len(list(v)) >= 4]:
		print('Invalid')
	else:
		print('Valid')


import re

def validCard(line):
	grouped = line.split('-')
	if len(grouped) == 1:
		if len(line) != 16:
			return 'Invalid'
	elif len(grouped) == 4:
		if len(grouped[0]) != 4 or len(grouped[1]) != 4 or len(grouped[2]) != 4 or (len(grouped[3]) != 4):
			return 'Invalid'
		line = ''.join(grouped)
	else:
		return 'Invalid'
	if not re.search('^[456]', line):
		return 'Invalid'
	if re.search('\\D', line):
		return 'Invalid'
	if re.search('(.)\\1{3,}', line):
		return 'Invalid'
	return 'Valid'
N = int(input())
print(*[validCard(input()) for _ in range(N)], sep='\n')


def judge(s):
	if len(s) < 16:
		return False
	if s[0] not in '456':
		return False
	s = s.split('-')
	if len(s) not in [1, 4]:
		return False
	if len(s) == 4:
		for i in s:
			if len(i) not in [1, 4]:
				return False
	s = ''.join(s)
	if len(s) != 16:
		return False
	count = 1
	pre = s[0]
	for i in s[1:]:
		if i not in '0123456789':
			return False
		if i == pre:
			count += 1
			if count == 4:
				return False
		else:
			pre = i
			count = 1
	return True
n = int(input())
for i in range(n):
	if judge(input()):
		print('Valid')
	else:
		print('Invalid')


def isValid(x):
	if len(x) not in [16, 19]:
		return False
	if len(x) == 19:
		if x[4] != '-' or x[9] != '-' or x[14] != '-':
			return False
	if x[0] not in ['4', '5', '6']:
		return False
	if '-' in x:
		x = ''.join(x.split('-'))
	if any((ord(x[i]) not in range(ord('0'), ord('9') + 1) for i in range(16))):
		return False
	if any((str(i) * 4 in x for i in range(10))):
		return False
	return True
for _ in range(int(input())):
	x = input()
	if isValid(x):
		print('Valid')
	else:
		print('Invalid')


import re

def is_valid(s):
	if not re.match('^\\d{16}$', s) and (not re.match('^(\\d{4}-){3}\\d{4}$', s)):
		return 'Invalid'
	s = s.replace('-', '')
	if not re.match('^[456]', s):
		return 'Invalid'
	if re.search('(\\d)\\1{3}', s):
		return 'Invalid'
	return 'Valid'
n_tests = int(input())
for _ in range(n_tests):
	print(is_valid(input().strip()))


import re

def validate(s):
	a = ''.join(s.split('-'))
	return (re.match('\\d{16,16}$', s) or re.match('\\d{4,4}-\\d{4,4}-\\d{4,4}-\\d{4,4}$', s)) and re.match('[456]', s) and (not re.search('(\\d)\\1{3,}', a))
n = int(input())
for i in range(n):
	if validate(input().strip()):
		print('Valid')
	else:
		print('Invalid')


import re
n = int(input())
for i in range(n):
	s = input()
	if len(''.join(s.split('-'))) != 16:
		print('Invalid')
	else:
		repeat = ''.join(s.split('-'))
		if re.search('1111|2222|3333|4444|5555|6666|7777|8888|9999|0000', repeat):
			print('Invalid')
			continue
		ss = re.search('^[4|5|6]{1}[0-9]{15}$|^[4|5|6]{1}[0-9]{3}-[0-9]{4}-[0-9]{4}-[0-9]{4}$', s)
		if ss:
			print('Valid')
		else:
			print('Invalid')


import re

def valid(s):
	if not re.match('^[456]\\d{3}-?\\d{4}-?\\d{4}-?\\d{4}$', s):
		return False
	s = s.replace('-', '')
	if re.search('(?P<repeated>\\d)(?P=repeated){3}', s):
		return False
	return True
N = int(input())
for _ in range(N):
	if valid(input()):
		print('Valid')
	else:
		print('Invalid')


import re

def is_valid(s):
	f = True
	x = s.split('-')
	for i in x:
		if len(i) != 4 and len(i) != 16:
			f = False
	s = s.replace('-', '')
	p = re.compile('^[456][0-9]{15}$', re.M)
	a2 = re.findall(p, s)
	a = s[0]
	flag = True
	count = 0
	for i in s:
		if a == i:
			count += 1
		else:
			a = i
			count = 1
		if count > 3:
			flag = False
	if a2 and flag and f:
		return 'Valid'
	else:
		return 'Invalid'
for _ in range(int(input().rstrip())):
	print(is_valid(input().rstrip('\n')))


def isValid(s):
	if s[0] not in ['4', '5', '6']:
		return False
	elif len(s) != 16 and len(s) != 19:
		return False
	else:
		ccount = 1
		numcount = 1
		current = s[0]
		for c in s[1:]:
			if not c.isdigit() and c != '-':
				return False
			elif c.isdigit():
				if c == current:
					ccount += 1
					if ccount > 3:
						return False
				else:
					current = c
					ccount = 1
				numcount += 1
			else:
				if numcount != 4:
					return False
				numcount = 0
	return True
N = int(input())
for i in range(0, N):
	if isValid(input()):
		print('Valid')
	else:
		print('Invalid')


import re
for _ in range(int(input().strip())):
	cc = input()
	if re.match('^[0-9]{4}\\-[0-9]{4}\\-[0-9]{4}\\-[0-9]{4}$', cc) != None:
		cc = ''.join(cc.split('-'))
	if re.match('^[456][0-9]{15}$', cc) != None:
		if re.search('([0-9]{1})\\1{3}', cc) != None:
			print('Invalid')
		else:
			print('Valid')
	else:
		print('Invalid')


def check(s):
	ss = ''.join(s.split('-'))
	for i in range(len(ss) - 3):
		if ss[i] == ss[i + 1] and ss[i] == ss[i + 2] and (ss[i] == ss[i + 3]):
			return False
	return True
import re
p1 = re.compile('^[4-6][0-9]{15}$')
p2 = re.compile('^[4-6][0-9]{3}(-[0-9]{4}){3}$')
for _ in range(int(input())):
	s = input()
	if (p1.match(s) or p2.match(s)) and check(s):
		print('Valid')
	else:
		print('Invalid')


import re

def cr(L):
	for k in range(len(L) - 4):
		if L[k] == L[k + 1] and L[k + 1] == L[k + 2] and (L[k + 2] == L[k + 3]):
			return False
	return True

def h(L):
	for j in L:
		if re.match('[0-9]', j):
			pass
		else:
			return False
	return True
for k in range(int(input())):
	N = input()
	if N[0] in ['4', '5', '6']:
		if len(N) == 16:
			L = list(N)
			if cr(L):
				if h(L):
					print('Valid')
				else:
					print('Invalid')
			else:
				print('Invalid')
		elif len(N) == 19:
			L2 = []
			for k in range(len(N)):
				if k == 4 or k == 9 or k == 14:
					pass
				else:
					L2.append(N[k])
			if cr(L2):
				if h(L2):
					print('Valid')
				else:
					print('Invalid')
			else:
				print('Invalid')
		else:
			print('Invalid')
	else:
		print('Invalid')


import re
hyphens = re.compile('-')
dblhyp = re.compile('--')
hypchck = re.compile('\\d{4,4}')
spaces = re.compile('\\s')
digits = re.compile('\\D')
startn = re.compile('[^456]')
chkrep = re.compile('(\\d)\\1\\1\\1')
for k in range(int(input())):
	s = input().strip()
	if len(hypchck.findall(s)) != 4 or dblhyp.search(s):
		print('Invalid')
	else:
		s = hyphens.sub('', s)
		if len(s) != 16 or spaces.search(s) or startn.match(s) or digits.search(s) or chkrep.search(s):
			print('Invalid')
		else:
			print('Valid')


import re
N = int(input())
cards = []
for n in range(N):
	cards.append(input())
cond1 = re.compile('^[456]')
cond2 = re.compile('^(\\d{4}){4}$')
cond22 = re.compile('^(\\d{4}-){3}(\\d{4})$')
cond3 = re.compile('[^\\d-]')
for c in cards:
	tests = []
	if cond1.search(c) is not None:
		tests.append(True)
	else:
		tests.append(False)
	if cond2.search(c) is not None or cond22.search(c) is not None:
		tests.append(True)
	else:
		tests.append(False)
	if cond3.search(c) is None:
		tests.append(True)
	else:
		tests.append(False)
	if [match[0] for match in re.findall('((\\d)\\2{3,})', c)] == []:
		tests.append(True)
	else:
		tests.append(False)
	if [match[0] for match in re.findall('((\\d)\\2{1,}-\\2{2,})', c)] == []:
		tests.append(True)
	else:
		tests.append(False)
	if [match[0] for match in re.findall('((\\d)\\2{2,}-\\2{1,})', c)] == []:
		tests.append(True)
	else:
		tests.append(False)
	if [match[0] for match in re.findall('((\\d)-\\2{3,})', c)] == []:
		tests.append(True)
	else:
		tests.append(False)
	if all(tests):
		print('Valid')
	else:
		print('Invalid')


import re
for i in range(int(input())):
	s = input()
	if re.match('^[456][0-9]{3}-?[0-9]{4}-?[0-9]{4}-?[0-9]{4}$', s) and (not re.search('([0-9])-?\\1-?\\1-?\\1', s)):
		print('Valid')
	else:
		print('Invalid')


import sys, re
lines = int(input().strip())
for i in range(lines):
	CCN = str(input().strip())
	if re.search('[^\\d\\-]', CCN) or CCN[0] not in ['4', '5', '6'] or len(re.findall('[\\d]', CCN)) != 16 or re.search('([\\d])\\1{3}', re.sub('-', '', CCN)):
		print('Invalid')
	elif '-' in CCN:
		if len(re.findall('-', CCN)) != 3:
			print('Invalid')
		elif CCN[4] == '-' and CCN[9] == '-' and (CCN[14] == '-'):
			print('Valid')
		else:
			print('Invalid')
	else:
		print('Valid')


def valid(car):
	serie = []
	if len(car) < 16 or car[0] not in '456':
		return False
	serie = car.split('-')
	if len(serie) != 1 and len(serie) != 4:
		return False
	if len(serie) == 4:
		for i in serie:
			if len(i) != 4:
				return False
			if not i.isdigit():
				return False
		car = ''.join(serie)
	if not uni(car):
		return False
	return True

def uni(card):
	if not card.isdigit():
		return False
	r = ''
	cont = 0
	for i in card:
		if r == i:
			cont += 1
		else:
			r = i
			cont = 1
		if cont == 4:
			return False
	return True
for _ in range(int(input())):
	print('Valid' if valid(input()) else 'Invalid')


import re
times = int(input())
p1 = re.compile('[456][0-9]{3}-{0,1}[0-9]{4}-{0,1}[0-9]{4}-{0,1}[0-9]{4}$')
p2 = re.compile('.*([0-9])\\1\\1\\1+')
for i in range(times):
	t1 = input()
	if re.match(p1, t1) == None:
		print('Invalid')
	elif re.match(p2, str.replace(t1, '-', '')) == None:
		print('Valid')
	else:
		print('Invalid')


T = int(input())
(V, INV) = ('Valid', 'Invalid')
for j in range(T):
	s = [str(i) for i in input().split('-')]
	flag = True
	card = ''
	if len(s) == 4 and ord(s[0][0]) > 51 and (ord(s[0][0]) < 55):
		for i in range(len(s)):
			if len(s[i]) == 4:
				card += s[i]
			else:
				print(INV)
				flag = False
				break
	elif len(s) == 1 and ord(s[0][0]) > 51 and (ord(s[0][0]) < 55):
		if len(s[0]) == 16:
			card = s[0]
		else:
			print(INV)
			flag = False
	else:
		print(INV)
		flag = False
	for i in range(len(card)):
		if ord(card[i]) < 48 or ord(card[i]) > 57:
			flag = False
			break
		if i < len(card) - 3:
			kol = 0
			for i in range(i, i + 3):
				if card[i] == card[i + 1]:
					kol += 1
				else:
					kol = 0
				if kol >= 3:
					flag = False
					break
		if flag == False:
			print(INV)
			break
	if flag != False:
		print(V)


n = int(input())
digits = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']

def check(s, i):
	for j in range(i - 4, i):
		if s[j].isdigit() == False:
			return 0
	for j in range(i + 1, i + 4):
		if s[j].isdigit() == False:
			return 0
	return 1
for i in range(0, n):
	s = input()
	l = len(s)
	if s[0] not in ['4', '5', '6']:
		print('Invalid')
		continue
	count = 0
	for i in range(0, l):
		if s[i] in digits:
			count += 1
	if count != 16:
		print('Invalid')
		continue
	val = 0
	for i in range(0, l):
		if s[i] not in digits and s[i] != '-':
			val = 1
			break
	if val == 1:
		print('Invalid')
		continue
	val = 0
	for i in range(0, l):
		if s[i] == '-':
			if check(s, i) == 0:
				val = 1
				break
			if i < 4 or i > l - 4:
				val = 1
				break
	if val == 1:
		print('Invalid')
		continue
	s = s.replace('-', '')
	l = len(s)
	val = 0
	for i in range(0, l - 3):
		if s[i] == s[i + 1] and s[i + 1] == s[i + 2] and (s[i + 2] == s[i + 3]):
			val = 1
			break
	if val == 1:
		print('Invalid')
		continue
	print('Valid')


import re
pattern = re.compile('^[456]{1}(\\d{15}|\\d{3}-\\d{4}-\\d{4}-\\d{4})$')
for _ in range(int(input())):
	res = pattern.search(input())
	if res:
		s = res.group().replace('-', '')
		(x, last) = (1, s[0])
		for ch in s[1:]:
			if ch == last:
				x += 1
			else:
				(x, last) = (1, ch)
			if x == 4:
				break
		print('Invalid' if x == 4 else 'Valid')
	else:
		print('Invalid')


N = int(input())
import re
x = re.compile('^[4,5,6]{1}[0-9]{3}[0-9]{4}[0-9]{4}[0-9]{4}$')
y = re.compile('^[4,5,6]{1}[0-9]{3}-[0-9]{4}-[0-9]{4}-[0-9]{4}$')
for _ in range(N):
	S = input()
	if '-' in S:
		valid = re.match(y, S)
	else:
		valid = re.match(x, S)
	if valid:
		valid = True
		if '-' in S:
			S = ''.join(S.split('-'))
		c = 1
		d = S[0]
		for digit in S[1:]:
			if digit == d:
				c += 1
				if c == 4:
					valid = False
					break
			else:
				c = 1
				d = digit
		if valid:
			print('Valid')
		else:
			print('Invalid')
	else:
		print('Invalid')


import re
p = re.compile('(?=^(4|5|6))^((([0-9]{4}\\-){3}[0-9]{4})|([0-9]{16}))$')
for _ in range(int(input())):
	c = input()
	if p.match(c) and (not re.findall('((\\d)\\2{3,})', ''.join(c.split('-')))):
		print('Valid')
	else:
		print('Invalid')


N = int(input())
import re
cons = re.compile('(\\d)(\\1){3}')
f = re.compile('\\d{4}\\-?\\d{4}\\-?\\d{4}\\-?\\d{4}')
for _ in range(N):
	n = input()
	if len(n) != 16 and len(n) != 19:
		print('Invalid')
		continue
	if n[0] not in '456':
		print('Invalid')
		continue
	if not f.match(n):
		print('Invalid')
		continue
	n = n.replace('-', '')
	if cons.search(n):
		print('Invalid')
		continue
	print('Valid')


import re
CARD_REGEX = re.compile('^(?=[\\d-]{16,19}$)(?=(([\\d]{4}-){3}\\d{4})|(^[4,5,6][\\d]{15}))^((([\\d])\\6{0,2}-?(?!\\6))+$)')
for _ in range(int(input())):
	res = 'Valid' if CARD_REGEX.match(input()) else 'Invalid'
	print(res)


import sys
import re
n = int(sys.stdin.readline())
for line in sys.stdin.read().splitlines():
	if len(line) > 16:
		if all([line[w] == '-' for w in [4, 9, 14]]):
			line = line[:4] + line[5:9] + line[10:14] + line[15:]
	if len(line) != 16:
		print('Invalid')
	elif line.isnumeric() == False:
		print('Invalid')
	elif len(str(int(line))) != 16:
		print('Invalid')
	elif line[0] not in ['4', '5', '6']:
		print('Invalid')
	elif re.findall('((\\w)\\2{3,})', line) != []:
		print('Invalid')
	else:
		print('Valid')


import re
times = int(input())
for i in range(times):
	test = str(input())
	if re.match('^[456]\\d{3}-?\\d{4}-?\\d{4}-?\\d{4}$', test):
		test2 = test.replace('-', '')
		if not re.match('^.*(\\d)-?\\1-?\\1-?\\1.*$', test2):
			print('Valid')
		else:
			print('Invalid')
	else:
		print('Invalid')


import re
__author__ = 'gauth_000'
for test_cases in range(int(input())):
	string = input()
	valid = True
	if string.find(' ') != -1:
		valid = False
	if valid and (string[0] == '4' or string[0] == '5' or string[0] == '6'):
		if valid and string.find('-') != -1:
			if len(string.split('-')) != 4:
				valid = False
			if valid:
				for num in string.split('-'):
					if len(num) != 4:
						valid = False
						break
		if valid:
			for c in string:
				if c != '-':
					if c.isalpha():
						valid = False
						break
		if valid and len(string.replace('-', '')) != 16:
			valid = False
		if valid:
			numbers = re.findall('(\\d)(\\1+)', string.replace('-', ''))
			for num in numbers:
				if len(num[1]) + 1 > 3:
					valid = False
					break
	else:
		valid = False
	print('Valid' if valid else 'Invalid')


import re
n = int(input())
p_simple = '(?:^[456]\\d{15}$)'
p_grouped = '(?:^[456]\\d{3}(?:-\\d{4}){3}$)'
for _ in range(n):
	card_no = input()
	obj = re.findall(p_simple + '|' + p_grouped, card_no)
	if obj:
		card_no = card_no.replace('-', '')
		prev = card_no[0]
		matched = 0
		inv = False
		for c in card_no:
			if prev == c:
				matched += 1
				if matched == 3:
					inv = True
					break
			else:
				matched = 0
			prev = c
		if inv:
			print('Invalid')
		else:
			print('Valid')
	else:
		print('Invalid')


import re
import sys
import itertools

def valid(number):
	pat = re.compile('^[456][0-9]{3}-?[0-9]{4}-?[0-9]{4}-?[0-9]{4}$')
	if pat.match(number) is None:
		return False
	for g in itertools.groupby([v for v in number if v != '-']):
		if len(list(g[1])) >= 4:
			return False
	return True
data = sys.stdin.readlines()[1:]
for d in data:
	print('Valid' if valid(d) else 'Invalid')


import re
pattern = re.compile('^\\d{4}-\\d{4}-\\d{4}-\\d{4}$')
pattern_digits = re.compile('^\\d{16}$')
N = int(input())
C = [input() for _ in range(N)]

def is_valid(c):
	if not c:
		return False
	if c[0] not in '456':
		return False
	if '-' in c and (not pattern.match(c)):
		return False
	c = ''.join(filter(lambda x: '' if x == '-' else x, c))
	if not pattern_digits.match(c):
		return False
	for x in [str(i) * 4 for i in range(10)]:
		if x in c:
			return False
	return True
Cv = map(is_valid, C)
for cv in Cv:
	print('Valid' if cv else 'Invalid')


import re
flag = 0
creditRegex = re.compile('^([456]\\d{3})-?(\\d{4})-?(\\d{4})-?(\\d{4})$')
repeRegex = re.compile('([0-9])\\1{3,}')
for i in range(int(input())):
	num = input()
	if len(num) == 16 or len(num) == 19:
		t1 = creditRegex.search(num)
		if t1:
			flag = 1
			t1 = list(t1.groups())
			s = ''.join(t1)
			t2 = repeRegex.search(s)
			if t2:
				flag = 0
			if flag == 1:
				print('Valid')
			else:
				print('Invalid')
		else:
			print('Invalid')
	else:
		print('Invalid')


from itertools import groupby
import re
for _ in range(int(input())):
	card_no = input()
	bad = list(filter(lambda x: x >= 4, [len(list(g)) for (k, g) in groupby(filter(str.isnumeric, card_no))]))
	if re.match('^[456][\\d]{3}-?[\\d]{4}-?[\\d]{4}-?[\\d]{4}$', card_no) and len(bad) == 0:
		print('Valid')
	else:
		print('Invalid')


import re
for x in range(0, int(input())):
	card = input()
	if re.fullmatch('[456]\\d\\d\\d-\\d\\d\\d\\d-\\d\\d\\d\\d-\\d\\d\\d\\d|[456]\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d', card):
		if re.findall('(.)\\1\\1\\1', ''.join(card.split('-'))):
			print('Invalid')
		else:
			print('Valid')
	else:
		print('Invalid')


import re
r = '(?!.*(.)-?\\1-?\\1-?\\1)(?=[4-6])(?=[0-9]{4}-?[0-9]{4}-?[0-9]{4}-?[0-9]{4}$)'
for _ in range(int(input())):
	t = input()
	print('Valid' if re.match(r, t) else 'Invalid')


import re
for _ in range(int(input())):
	isvalid = 'Invalid'
	creditcardnumber = input()
	validsingle = re.search('^([0-9]{16})$', creditcardnumber)
	validgroup = re.search('^([0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4})$', creditcardnumber)
	if validsingle != None or validgroup != None:
		numbers = ''.join(creditcardnumber.split('-'))
		if numbers[0] == '4' or numbers[0] == '5' or numbers[0] == '6':
			if re.search('([0-9])\\1{3}', numbers) == None:
				isvalid = 'Valid'
	print(isvalid)


import re
for i in range(int(input())):
	s = input().strip()
	valid = True
	if re.findall('\\A[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}\\Z', s):
		s = re.sub('-', '', s)
	if len(re.findall('\\A[4-6][0-9]{15}\\Z', s)) == 0:
		valid = False
	if len(re.findall('0{4}|1{4}|2{4}|3{4}|4{4}|5{4}|6{4}|7{4}|8{4}|9{4}', s)):
		valid = False
	if valid:
		print('Valid')
	else:
		print('Invalid')


import sys
import re

def is_valid(s):
	if not re.search('^[4-6]', s):
		sys.stdout.write('Invalid\n')
		return
	if not re.search('^[\\d]{16,16}$|^([\\d]{4,4}-){3,3}[\\d]{4,4}$', s):
		sys.stdout.write('Invalid\n')
		return
	if re.search('(?P<c>\\d)(-*(?P=c)){3,}', s):
		sys.stdout.write('Invalid\n')
		return
	sys.stdout.write('Valid\n')
	return

def vn():
	num = int(sys.stdin.readline())
	while num:
		s = sys.stdin.readline()
		is_valid(s)
		num -= 1
	return

def main():
	vn()
	return
main()


import re
fmt_reg = re.compile('^[456][0-9]{3}(-?[0-9]{4}){3}$')
rpt_reg = re.compile('([0-9])\\1{3}')

def check(s):
	return fmt_reg.match(s) and (not rpt_reg.search(s.replace('-', '')))
N = int(input())
for _ in range(N):
	if check(input()):
		print('Valid')
	else:
		print('Invalid')


import re
p = '^((?:4|5|6)[\\d]{3})-?([\\d]{4})-?([\\d]{4})-?([\\d]{4})$'
p2 = '([0-9])\\1{3,}'
t = int(input())
for _ in range(t):
	m = re.match(p, input().strip())
	if m:
		if re.search(p2, ''.join(m.groups())):
			print('Invalid')
		else:
			print('Valid')
	else:
		print('Invalid')


import re

def validate(number):
	if re.search('(\\d)\\-?\\1\\-?\\1\\-?\\1', number):
		return 'Invalid'
	if not re.match('^[456]\\d{3}\\-?\\d{4}\\-?\\d{4}\\-?\\d{4}$', number):
		return 'Invalid'
	return 'Valid'
for _ in range(int(input())):
	print(validate(input()))


import re
for s in range(int(input())):
	s = input()
	if re.search('^([456]\\d{3})(((\\d{4}){3})|(\\-\\d{4}){3})$', s) and (not re.search('(\\d)\\1{3,}', s.replace('-', ''))):
		print('Valid')
	else:
		print('Invalid')


import re
n = int(input())
for test in range(n):
	txt = input()
	matches = 0
	if re.match('^[456](\\d{15}|\\d{3}-\\d{4}-\\d{4}-\\d{4})$', txt) != None:
		txt = re.sub('-', '', txt)
		cnt = 0
		matches = 1
		for i in range(1, len(txt)):
			if txt[i] == txt[i - 1]:
				cnt = cnt + 1
				if cnt >= 3:
					matches = 0
			else:
				cnt = 0
	print('Valid' if matches else 'Invalid')


import re
num_of_cards = int(input())
for i in range(num_of_cards):
	card_number = input()
	if not re.match('((4|5|6)\\d\\d\\d-\\d\\d\\d\\d-\\d\\d\\d\\d-\\d\\d\\d\\d)|((4|5|6)\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d)', card_number):
		print('Invalid')
		continue
	if not len(card_number) == 19 and (not len(card_number) == 16):
		print('Invalid')
		continue
	card_number = ''.join(card_number.split('-'))
	current = card_number[0]
	count = 0
	is_invalid = False
	for c in card_number:
		if c == current:
			count += 1
			if count > 3:
				is_invalid = True
				print('Invalid')
				break
		else:
			current = c
			count = 1
	if not is_invalid:
		print('Valid')


a = int(input())
for c in range(a):
	isValid = True
	d = input()
	e = d.split('-')
	for f in range(len(e) - 1):
		if len(e[f]) < 4:
			isValid = False
	g = ''.join(e)
	if len(g) != 16 or not g.isdigit():
		isValid = False
	else:
		for i in range(13):
			if int(g[i:i + 4]) % 1111 == 0:
				isValid = False
	first = int(g[0])
	if first != 4 and first != 5 and (first != 6):
		isValid = False
	if isValid:
		print('Valid')
	else:
		print('Invalid')


import re
for _ in range(int(input())):
	n = input()
	check = bool(re.search('^[456]\\d{3}-?\\d{4}-?\\d{4}-?\\d{4}$', n)) and (not re.search('(\\d)\\1{3,}', re.sub('-', '', n)))
	if check:
		print('Valid')
	else:
		print('Invalid')


import re

def is_valid(credit_card):
	return looks_like_a(credit_card) and max_number_of_consecutive_digits(credit_card) < 4

def looks_like_a(credit_card):
	FIRST_DIGITS_GROUP = '[456]\\d{3}'
	DIGITS_GROUP = '-?\\d{4}'
	CREDIT_CARD_REGEX = FIRST_DIGITS_GROUP + DIGITS_GROUP + DIGITS_GROUP + DIGITS_GROUP
	return not re.search('^' + CREDIT_CARD_REGEX + '$', credit_card) is None

def max_number_of_consecutive_digits(string):
	digit = None
	max_count_so_far = 0
	count = 0
	for c in string:
		if not c.isdigit():
			continue
		if c == digit:
			count += 1
		else:
			max_count_so_far = max(max_count_so_far, count)
			digit = c
			count = 1
	max_count_so_far = max(max_count_so_far, count)
	return max_count_so_far
T = int(input())
for t in range(T):
	credit_card = input().strip()
	result = 'Valid' if is_valid(credit_card) else 'Invalid'
	print(result)


import re

def result(credit):
	if re.compile('[1-9]{4}\\-[1-9]{4}\\-[1-9]{4}\\-[1-9]{4}').match(credit) and len(credit) == 19:
		credit = credit[0:4] + credit[5:9] + credit[10:14] + credit[15:19]
	if re.compile('[456][1-9]{15}').match(credit) and len(credit) == 16:
		for i in range(13):
			if credit[i:i + 4] == credit[i] * 4:
				return 'Invalid'
		return 'Valid'
	else:
		return 'Invalid'
n = int(input())
for _ in range(n):
	penis = input()
	print(result(penis))


import re
for i in range(int(input())):
	n = input().strip()
	if not re.search('([0-9])\\1{3}', n.replace('-', '')) and (re.match('([0-9]{4}-){3}[0-9]{4}$', n) or re.match('[0-9]{16}', n)) and re.match('[4-6].*', n):
		print('Valid')
	else:
		print('Invalid')


import re
N = int(input())
for _ in range(N):
	t = input().strip()
	if re.search('^[456]', t) != None and (re.search('^\\d{16}$', t) != None or re.search('^((\\d{4}-){3})\\d{4}$', t)) and (re.search('(\\d)-?\\1-?\\1-?\\1', t) == None):
		print('Valid')
	else:
		print('Invalid')


for _ in range(int(input())):
	number = input()
	digits = 0
	if number[0] not in '456':
		print('Invalid')
	else:
		for x in number:
			if x in '0123456789':
				digits += 1
			elif x != '-':
				print('Invalid')
				break
		else:
			if digits != 16:
				print('Invalid')
			else:
				groups = number.split('-')
				for g in groups:
					if len(g) != 4 and g != number:
						print('Invalid')
						break
				else:
					s = number.replace('-', '')
					while len(s) >= 4:
						if s[0] * 4 == s[:4]:
							print('Invalid')
							break
						s = s[1:]
					else:
						print('Valid')


import re
nTests = int(input())
for i in range(nTests):
	s = input()
	n = len(s)
	sHyphenRemoved = s.replace('-', '')
	valid = n == 16 or n == 16 + 3
	if valid:
		valid = s[0] in '456' and sHyphenRemoved.isdigit()
		if valid:
			if n == 16 + 3:
				valid = s[4] == '-' and s[9] == '-' and (s[14] == '-')
				prev = ' '
	for i in range(len(sHyphenRemoved)):
		if i == 0:
			cnt = 1
		elif sHyphenRemoved[i] == sHyphenRemoved[i - 1]:
			cnt += 1
		else:
			cnt = 1
		if cnt > 3:
			valid = False
	print('Valid' if valid else 'Invalid')


import re
n = int(input())
pat = '^[456][0-9][0-9][0-9](-?)[0-9][0-9][0-9][0-9]\\1[0-9][0-9][0-9][0-9]\\1[0-9][0-9][0-9][0-9]$'
for _ in range(n):
	card = input()
	if re.match(pat, card):
		past = card[0]
		cont = 0
		for d in card[1:]:
			if d == '-':
				continue
			if d == past:
				cont += 1
			else:
				cont = 1
				past = d
			if cont == 4:
				print('Invalid')
				break
		else:
			print('Valid')
	else:
		print('Invalid')