Alperen has two strings, $s$ and $t$ which are both initially equal to "a".

He will perform $q$ operations of two types on the given strings:

$1 \;\; k \;\; x$ — Append the string $x$ exactly $k$ times at the end of string $s$. In other words, $s := s + \underbrace{x + \dots + x}_{k \text{ times}}$.

$2 \;\; k \;\; x$ — Append the string $x$ exactly $k$ times at the end of string $t$. In other words, $t := t + \underbrace{x + \dots + x}_{k \text{ times}}$.

After each operation, determine if it is possible to rearrange the characters of $s$ and $t$ such that $s$ is lexicographically smaller$^{\dagger}$ than $t$.

Note that the strings change after performing each operation and don't go back to their initial states.

$^{\dagger}$ Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. A formal definition is as follows: string $p$ is lexicographically smaller than string $q$ if there exists a position $i$ such that $p_i < q_i$, and for all $j < i$, $p_j = q_j$. If no such $i$ exists, then $p$ is lexicographically smaller than $q$ if the length of $p$ is less than the length of $q$. For example, ${abdc} < {abe}$ and ${abc} < {abcd}$, where we write $p < q$ if $p$ is lexicographically smaller than $q$.


-----Input-----

The first line of the input contains an integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.

The first line of each test case contains an integer $q$ $(1 \leq q \leq 10^5)$ — the number of operations Alperen will perform.

Then $q$ lines follow, each containing two positive integers $d$ and $k$ ($1 \leq d \leq 2$; $1 \leq k \leq 10^5$) and a non-empty string $x$ consisting of lowercase English letters — the type of the operation, the number of times we will append string $x$ and the string we need to append respectively.

It is guaranteed that the sum of $q$ over all test cases doesn't exceed $10^5$ and that the sum of lengths of all strings $x$ in the input doesn't exceed $5 \cdot 10^5$.


-----Output-----

For each operation, output "YES", if it is possible to arrange the elements in both strings in such a way that $s$ is lexicographically smaller than $t$ and "NO" otherwise.


-----Examples-----

Input
3
5
2 1 aa
1 2 a
2 3 a
1 2 b
2 3 abca
2
1 5 mihai
2 2 buiucani
3
1 5 b
2 3 a
2 4 paiu
Output
YES
NO
YES
NO
YES
NO
YES
NO
NO
YES


-----Note-----

In the first test case, the strings are initially $s = $ "a" and $t = $ "a".

After the first operation the string $t$ becomes "aaa". Since "a" is already lexicographically smaller than "aaa", the answer for this operation should be "YES".

After the second operation string $s$ becomes "aaa", and since $t$ is also equal to "aaa", we can't arrange $s$ in any way such that it is lexicographically smaller than $t$, so the answer is "NO".

After the third operation string $t$ becomes "aaaaaa" and $s$ is already lexicographically smaller than it so the answer is "YES".

After the fourth operation $s$ becomes "aaabb" and there is no way to make it lexicographically smaller than "aaaaaa" so the answer is "NO".

After the fifth operation the string $t$ becomes "aaaaaaabcaabcaabca", and we can rearrange the strings to: "bbaaa" and "caaaaaabcaabcaabaa" so that $s$ is lexicographically smaller than $t$, so we should answer "YES".

Source: codeforces | Difficulty: MEDIUM
Tags: Greedy algorithms

String algorithms

Constructive algorithms


Solutions:
import sys
from math import *
input = lambda : sys.stdin.readline().rstrip()
for _ in [0] * int(input()):
	n = int(input())
	ls = 1
	lt = 1
	tna = 0
	sna = 0
	for _ in [0] * n:
		(c, k, x) = input().split()
		k = int(k)
		if int(c) == 1:
			ls += k * len(x)
			sna |= len(x) - x.count('a')
		else:
			lt += k * len(x)
			tna |= len(x) - x.count('a')
		if tna:
			print('YES')
		elif sna:
			print('NO')
		else:
			print(['NO', 'YES'][ls < lt])


import sys
R = sys.stdin.readline
for _ in range(int(R())):
	a0 = a1 = 1
	for _ in range(int(R())):
		(t, l, s) = R().split()
		(t, l) = (int(t), int(l))
		if not a1:
			print('YES')
		elif not all((c == 'a' for c in s)):
			if t == 1:
				a0 = 0
				print('NO')
			else:
				a1 = 0
				print('YES')
		else:
			if not a0:
				print('NO')
				continue
			if t == 1:
				a0 += l * len(s)
			else:
				a1 += l * len(s)
			print('YES' if a1 > a0 else 'NO')


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
input_values = lambda : map(int, input().split())
for _ in range(int(input())):
	(f, f1, c, d) = (0, 0, 1, 1)
	for __ in range(int(input())):
		[p, k, s] = list(input().split())
		p = int(p)
		k = int(k)
		if p == 1:
			r = 0
			for j in s:
				if j == 'a':
					r += 1
				else:
					f = 1
			c = c + r * k
		else:
			r = 0
			for j in s:
				if j == 'a':
					r += 1
				else:
					f1 = 1
			d = d + r * k
		if f1:
			print('YES')
		elif f == 0:
			if c >= d:
				print('NO')
			else:
				print('YES')
		else:
			print('NO')


import sys
from heapq import heapify, heappush, heappop
from math import ceil, floor, sqrt, gcd
from collections import defaultdict, deque
input = lambda : sys.stdin.readline().rstrip('\r\n')

def int_arr():
	return list(map(int, input().split()))

def str_arr():
	return list(map(str, input().split()))

def get_str():
	return map(str, input().split())

def get_int():
	return map(int, input().split())

def lcm(a, b):
	return a * b // gcd(a, b)
mod = 1000000007

def f(arr):
	for i in range(25, -1, -1):
		if arr[i] > 0:
			return i

def solve():
	n = int(input())
	s = [0 for i in range(26)]
	t = [0 for i in range(26)]
	s[0] += 1
	t[0] += 1
	for i in range(n):
		(num, k, x) = get_str()
		k = int(k)
		if num == '1':
			for j in x:
				s[ord(j) - 97] += k
		else:
			for j in x:
				t[ord(j) - 97] += k
		(a, b) = (f(s), f(t))
		if b != 0:
			print('YES')
		elif a == b == 0 and s[a] < t[b]:
			print('YES')
		else:
			print('NO')
T = 1
T = int(input())
while T:
	solve()
	T -= 1


def solve(fs, ft, sc, tc):
	if ft:
		return 'YES'
	elif fs:
		return 'NO'
	return 'YES' if sc < tc else 'NO'

def main():
	for _ in range(int(input())):
		q = int(input())
		(sa, ta) = (0, 0)
		(fs, ft) = (False, False)
		for _ in range(q):
			(d, k, x) = input().split()
			(d, k) = (int(d), int(k))
			if d == 1:
				for e in x:
					if e != 'a':
						fs = True
					else:
						sa += k
			else:
				for e in x:
					if e != 'a':
						ft = True
					else:
						ta += k
			print(solve(fs, ft, sa, ta))
main()


def lexicographical():
	t1 = int(input())
	for i in range(t1):
		q = int(input())
		sa = sb = la = lb = 'a'
		lena = lenb = 1
		for j in range(q):
			(d, k, x) = input().split()
			k = int(k)
			if d == '1':
				if max(x) > la:
					la = max(x)
				lena += k * len(x)
			else:
				if max(x) > lb:
					lb = max(x)
				lenb += k * len(x)
			if sa < lb:
				print('YES')
			elif la == lb:
				if lena < lenb:
					print('YES')
				else:
					print('NO')
			else:
				print('NO')
lexicographical()


import sys
input = sys.stdin.readline
for t in range(int(input())):
	res = 'abcdefghijklmnopqrstuvwxyz'
	dct1 = {i: 0 for i in res}
	dct2 = {i: 0 for i in res}
	(dct1['a'], dct2['a']) = (1, 1)
	for i in range(int(input())):
		(a, b, c) = input().split(' ')
		c = c[:-1]
		if a == '1':
			for i in c:
				dct1[i] += int(b)
		else:
			for i in c:
				dct2[i] += int(b)
		for i in res:
			if dct2[i] != 0 and i != 'a':
				print('YES')
				break
		else:
			if dct1['a'] >= dct2['a']:
				print('NO')
			else:
				for i in dct1:
					if dct1[i] != 0 and i != 'a':
						print('NO')
						break
				else:
					print('YES')


import sys
from typing import List, Tuple, Dict
from collections import defaultdict, deque, namedtuple
from heapq import heappush as push, heappop as pop
MOD = int(1000000000.0 + 7)
INF = float('inf')
INT_MAX = sys.maxsize
sys.setrecursionlimit(MOD)

def solve():
	q = int(input())
	s = 'a'
	t = 'a'
	sflag = True
	tflag = True
	sCnt = 1
	tCnt = 1
	while q:
		(d, k, x) = input().split()
		d = int(d)
		k = int(k)
		for char in x:
			if d == 1:
				if char != 'a':
					sflag = False
				sCnt += k
			elif d == 2:
				if char != 'a':
					tflag = False
				tCnt += k
		if not tflag:
			print('YES')
		elif sflag and sCnt < tCnt:
			print('YES')
		else:
			print('NO')
		q -= 1
t = int(input())
while t:
	solve()
	t -= 1


for _ in range(int(input())):
	q = int(input())
	s_a = 1
	t_a = 1
	t_has_other = False
	s_len = 1
	for _ in range(q):
		(to, k, x) = input().split()
		if t_has_other:
			print('YES')
			continue
		k = int(k)
		a_count = x.count('a')
		if to == '1':
			s_a += k * a_count
			s_len += k * len(x)
		else:
			if a_count != len(x):
				t_has_other = True
				print('YES')
				continue
			t_a += k * a_count
		if s_len == s_a and s_a >= t_a or s_len != s_a:
			print('NO')
		else:
			print('YES')


for _ in range(int(input())):
	(a_in_s, a_in_t) = (1, 1)
	ck = False
	(nota_in_s, nota_in_t) = (False, False)
	for __ in range(int(input())):
		(d, k, x) = input().split()
		no_a = x.count('a') * int(k)
		x = x.replace('a', '')
		if int(d) == 1:
			if x != '':
				nota_in_s = True
			a_in_s += no_a
		else:
			if x != '':
				ck = True
				nota_in_t = True
			a_in_t += no_a
		if ck:
			print('YES')
			continue
		if nota_in_s and (not nota_in_t):
			print('NO')
			continue
		if a_in_s < a_in_t:
			print('YES')
		else:
			print('NO')


tests = int(input())
for _ in range(tests):
	q = int(input())
	s = t = 1
	talla = True
	salla = True
	for _ in range(q):
		(d, k, x) = input().split()
		(d, k) = (int(d), int(k))
		if not talla:
			print('YES')
			continue
		for c in x:
			if c != 'a':
				if d == 1:
					salla = False
				else:
					talla = False
				break
		else:
			if d == 1:
				s += k * len(x)
			else:
				t += k * len(x)
		if not talla:
			print('YES')
			continue
		if not salla:
			print('NO')
			continue
		if s < t:
			print('YES')
		else:
			print('NO')


test = int(input())
for j in range(test):
	s = 'a'
	t = 'a'
	maxg = 'a'
	maxf = 'a'
	l1 = 1
	l2 = 1
	r = int(input())
	alwaysTrue = False
	for i in range(r):
		if alwaysTrue:
			input()
			print('YES')
		else:
			ch = input()
			tab = ch.split()
			w = tab[0]
			k = int(tab[1])
			x = tab[2]
			newmax = max(x)
			if w == '1':
				if newmax == 'a':
					l1 = l1 + len(x) * k
				if newmax > maxf:
					maxf = newmax
			else:
				if newmax == 'a':
					l2 = l2 + len(x) * k
				if newmax > maxg:
					maxg = newmax
			if maxg > 'a':
				alwaysTrue = True
				print('YES')
			elif maxg == 'a' and 'a' == maxf:
				if l2 > l1:
					print('YES')
				else:
					print('NO')
			else:
				print('NO')


for _ in range(int(input())):
	n = int(input())
	(s, t) = (False, False)
	(ls, lt) = (0, 0)
	for _ in range(n):
		(d, k, x) = input().split()
		(d, k) = (int(d), int(k))
		for i in x:
			if d == 1:
				if i != 'a':
					s = True
				else:
					ls += k
			elif i != 'a':
				t = True
			else:
				lt += k
		if t:
			print('YES')
		elif not s and ls < lt:
			print('YES')
		else:
			print('NO')


testcase = int(input())
for i in range(testcase):
	numQuery = int(input())
	size = [1, 1]
	onlyA = [True, True]
	for j in range(numQuery):
		query = input().split()
		index = int(query[0]) - 1
		times = int(query[1])
		size[index] += times * len(query[2])
		onlyA[index] &= query[2].count('a') == len(query[2])
		if not onlyA[1] or (onlyA[0] and size[0] < size[1]):
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	f1 = 0
	f2 = 0
	cn1 = 0
	cn2 = 0
	for i in range(n):
		l = input().split()
		if l[0] == '1':
			c1 = 0
			for x in l[2]:
				if x == 'a':
					c1 += 1
				else:
					f1 = 1
			cn1 += c1 * int(l[1])
		else:
			c2 = 0
			for x in l[2]:
				if x == 'a':
					c2 += 1
				else:
					f2 = 1
			cn2 += c2 * int(l[1])
		if f2:
			print('YES')
		elif f1:
			print('NO')
		elif cn1 >= cn2:
			print('NO')
		else:
			print('YES')


def solve():
	t = int(input())
	for _ in range(t):
		n = int(input())
		only_a_in_s = True
		only_a_in_t = True
		a_count_in_t = 1
		a_count_in_s = 1
		for _ in range(n):
			(d, k, x) = input().split()
			d = int(d)
			k = int(k)
			if not only_a_in_t:
				print('YES')
				continue
			a_count = sum((char_ == 'a' for char_ in x))
			if d == 2:
				if a_count != len(x):
					only_a_in_t = False
					print('YES')
					continue
				a_count_in_t += a_count * k
			else:
				if a_count != len(x):
					only_a_in_s = False
				a_count_in_s += a_count * k
			if only_a_in_s:
				print('YES' if a_count_in_s < a_count_in_t else 'NO')
			else:
				print('NO')
solve()


import sys, math, bisect
input = sys.stdin.readline
for _ in range(int(input())):
	s = {}
	t = {}
	for i in range(97, 123):
		s[chr(i)] = 0
		t[chr(i)] = 0
	s['a'] = t['a'] = 1
	q = int(input())
	for i in range(q):
		(d, k, x) = [x for x in input().split()]
		if d == '1':
			for i in x:
				s[i] += int(k)
		else:
			for i in x:
				t[i] += int(k)

		def nextOK(i):
			for j in range(ord(i) + 1, 123):
				if t[chr(j)] != 0:
					return True
			return False

		def same(i):
			if s[i] < t[i]:
				return True
			return False

		def wearentsame(i):
			for j in range(ord(i) + 1, 123):
				if s[chr(j)] != 0:
					return False
			return True
		for i in s:
			if s[i] != 0:
				if nextOK(i):
					print('YES')
				elif same(i) and wearentsame(i):
					print('YES')
				else:
					print('NO')
				break


for _ in range(int(input())):
	n = int(input())
	cnt_s = 1
	cnt_t = 1
	check_s = False
	check_t = False
	for __ in range(n):
		(q, k, x) = input().split()
		k = int(k)
		q = int(q)
		for i in x:
			if q == 1:
				if i != 'a':
					check_s = True
				else:
					cnt_s += k
			elif i != 'a':
				check_t = True
			else:
				cnt_t += k
		if check_t or (not check_s and cnt_s < cnt_t):
			print('YES')
		else:
			print('NO')


import os
import math
import collections
import bisect
import functools
import itertools
import heapq
import string

def solve(a1, a2, c1, c2):
	if c2:
		return 'YES'
	if not c1:
		return 'YES' if a1 < a2 else 'NO'
	return 'NO'
t = int(input())
for _ in range(t):
	n = int(input())
	(a1, a2, c1, c2) = (1, 1, False, False)
	for __ in range(n):
		(i, k, x) = input().split()
		if i == '1':
			k = int(k)
			for c in x:
				if c != 'a':
					c1 = True
				else:
					a1 += k
		else:
			k = int(k)
			for c in x:
				if c != 'a':
					c2 = True
				else:
					a2 += k
		print(solve(a1, a2, c1, c2))


t = int(input())
for x in range(t):
	n = int(input())
	allYES = 0
	a1 = 1
	a2 = 1
	b = 0
	for y in range(n):
		S = input().split()
		if allYES == 1:
			print('YES')
		else:
			(S[0], S[1]) = (int(S[0]), int(S[1]))
			if S[0] == 2:
				for i in S[2]:
					if i != 'a':
						allYES = 1
						break
				a2 += len(S[2]) * S[1]
			if S[0] == 1:
				for i in S[2]:
					if i != 'a':
						b += S[1]
					else:
						a1 += S[1]
			if allYES == 1:
				print('YES')
			elif b == 0 and a1 < a2:
				print('YES')
			else:
				print('NO')


t = int(input())
while t > 0:
	t -= 1
	q = int(input())
	s_a = 1
	s_not_a = 0
	t_a = 1
	t_not_a = 0
	for i in range(q):
		st = input().split()
		d = int(st[0])
		k = int(st[1])
		x = st[2]
		x_a = 0
		x_not_a = 0
		for simb in x:
			if simb == 'a':
				x_a += 1
			else:
				x_not_a += 1
		x_a *= k
		x_not_a *= k
		if d == 1:
			s_a += x_a
			s_not_a += x_not_a
		else:
			t_a += x_a
			t_not_a += x_not_a
		if t_not_a > 0 or (s_a < t_a and s_not_a == 0):
			print('YES')
		else:
			print('NO')


def fnd_min_max(lst_1, sz_1, lst_2, sz_2):
	if len(lst_1) == len(lst_2) and len(lst_1) == 1:
		if sz_1 < sz_2:
			return 'YES'
		else:
			return 'NO'
	elif len(lst_2) == 1 and len(lst_1) > 1:
		return 'NO'
	return 'YES'

def parsing():
	n = int(input())
	while n > 0:
		kk = int(input())
		first = set('a')
		len_a = 1
		second = set('a')
		len_b = 1
		while kk > 0:
			arr = input().split(' ')
			(d, k, st) = (int(arr[0]), int(arr[1]), arr[2])
			if d == 1:
				first = first.union(set(st))
				len_a += len(st) * k
			else:
				second = second.union(set(st))
				len_b += len(st) * k
			print(fnd_min_max(first, len_a, second, len_b))
			kk -= 1
		n -= 1
parsing()


for _ in range(int(input())):
	q = int(input())
	(s, t) = (1, 1)
	ss = 0
	tt = 0
	for i in range(q):
		(d, k, x) = map(str, input().split())
		d = int(d)
		k = int(k)
		if d == 1 and tt != 1:
			s += len(x) * k
			if len(x) - x.count('a') > 0:
				ss = 1
		else:
			t += len(x) * k
			if len(x) - x.count('a') > 0:
				tt = 1
		if tt == 1:
			print('YES')
		elif s >= t or ss == 1:
			print('NO')
		else:
			print('YES')


for _ in range(int(input())):
	mxa = 'a'
	mxb = 'a'
	mxac = 1
	mxbc = 1
	mna = 'a'
	mnb = 'a'
	mnac = 1
	mnbc = 1
	for __ in range(int(input())):
		a = input().split()
		mx = max(a[2])
		mn = min(a[2])
		n = int(a[1])
		if a[0] == '1':
			if mx > mxa:
				mxa = mx
				mxac = n * a[2].count(mx)
			elif mx == mxa:
				mxac += n * a[2].count(mx)
			if mn < mna:
				mna = mn
				mnac = n * a[2].count(mn)
			elif mn == mna:
				mnac += n * a[2].count(mn)
		if a[0] == '2':
			if mx > mxb:
				mxb = mx
				mxbc = n * a[2].count(mx)
			elif mx == mxb:
				mxbc += n * a[2].count(mx)
			if mn < mnb:
				mnb = mn
				mnbc = n * a[2].count(mn)
			elif mn == mnb:
				mnbc += n * a[2].count(mn)
		if mna < mxb:
			print('YES')
		elif mna == mxb and mxa == mxb and (mnac < mxbc):
			print('YES')
		else:
			print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	s = [1, False]
	t = [1, False]
	for i in range(n):
		(num, count, sym) = map(str, input().split())
		num = int(num)
		count = int(count)
		if sym != len(sym) * 'a':
			if num == 1:
				s[1] = True
			else:
				t[1] = True
		elif num == 1:
			s[0] += len(sym) * count
		else:
			t[0] += len(sym) * count
		if t[1] == True:
			print('YES')
		elif s[0] >= t[0]:
			print('NO')
		elif s[1]:
			print('NO')
		else:
			print('YES')


from math import gcd
for _ in range(int(input())):
	s = {'a': 1}
	t = {'a': 1}
	for __ in range(int(input())):
		(d, k, x) = input().split()
		(d, k) = (int(d), int(k))
		for c in x:
			if d == 1:
				s[c] = s.get(c, 0) + k
			else:
				t[c] = t.get(c, 0) + k
		print('YES' if len(t) > 1 or (len(s) == 1 and s['a'] < t['a']) else 'NO')


import string
from collections import Counter

def solve(fs, ft, sc, tc):
	if ft:
		return 'YES'
	elif fs:
		return 'NO'
	return 'YES' if sc < tc else 'NO'

def main():
	for _ in range(int(input())):
		q = int(input())
		(s, t) = (Counter('a'), Counter('a'))
		(fs, ft) = (False, False)
		for _ in range(q):
			(d, k, x) = input().split()
			(d, k) = (int(d), int(k))
			if d == 1:
				for e in x:
					if e != 'a':
						fs = True
					s[e] += k
			else:
				for e in x:
					if e != 'a':
						ft = True
					t[e] += k
			print(solve(fs, ft, s['a'], t['a']))
main()


t = int(input())
for t1 in range(t):
	q = int(input())
	ind = 0
	n2 = 1
	n1 = 1
	c1 = 1
	c2 = 1
	for q1 in range(q):
		si = list(input().split())
		(d, k) = (int(si[0]), int(si[1]))
		s = si[2]
		if ind == 0:
			if d == 2:
				for i in range(len(s)):
					if s[i] != 'a':
						ind = 1
						break
					else:
						n2 += k
				c2 += len(s) * k
			if d == 1:
				for i in range(len(s)):
					if s[i] == 'a':
						n1 += k
				c1 += len(s) * k
		if ind == 0:
			if c1 > c2:
				print('NO')
			elif c1 == n1 and c1 < c2:
				print('YES')
			else:
				print('NO')
		else:
			print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	(alist, olist) = ([1, 1], [0, 0])
	for __ in range(n):
		(q, k, s) = input().split()
		for c in s:
			if c == 'a':
				alist[int(q) - 1] += int(k)
			else:
				olist[int(q) - 1] += int(k)
		if olist[1]:
			print('YES')
		elif olist[0] == 0 and alist[0] < alist[1]:
			print('YES')
		else:
			print('NO')


t = int(input())
for _ in range(t):
	q = int(input())
	s_a_count = 1
	t_a_count = 1
	other_than_a_t = False
	other_than_a_s = False
	for _ in range(q):
		line = list(map(str, input().rstrip().split()))
		if line[0] == '2':
			if other_than_a_t:
				pass
			else:
				for char in line[2]:
					if char != 'a':
						other_than_a_t = True
						break
					else:
						t_a_count += int(line[1])
		elif other_than_a_t:
			pass
		else:
			for char in line[2]:
				if char != 'a':
					other_than_a_s = True
				else:
					s_a_count += int(line[1])
		print('YES' if other_than_a_t or (not other_than_a_s and s_a_count < t_a_count) else 'NO')


import collections
LETTERS = 'abcdefghijklmnopqrstuvwxyz'

def solve():
	t = int(input())
	for _ in range(t):
		q = int(input())
		s = collections.defaultdict(int)
		t = collections.defaultdict(int)
		s['a'] = 1
		t['a'] = 1
		for _ in range(q):
			(d, k, x) = input().split()
			d = int(d)
			k = int(k)
			if d == 1:
				for letter in x:
					s[letter] += k
			else:
				for letter in x:
					t[letter] += k
			s_overall_count = 0
			last_diff = None
			for (index, letter) in enumerate(LETTERS):
				s_count = s[letter]
				t_count = t[letter]
				if not s_count and (not t_count):
					continue
				if t_count > 0 and s_overall_count > 0:
					print('YES')
					break
				last_diff = t_count - s_count
				s_overall_count += s_count
			else:
				print('YES' if last_diff > 0 else 'NO')
solve()


for _ in range(int(input())):
	n = int(input())
	(s, tot) = (1, 1)
	t = 1
	p = False
	for i in range(n):
		(op, k, x) = map(str, input().split())
		k = int(k)
		for j in x:
			if p:
				break
			if op == '1':
				if ord(j) == ord('a'):
					s += k
					tot += k
				else:
					tot += k
			elif ord(j) - ord('a') != 0:
				p = True
			else:
				t += k
		if p:
			print('YES')
		elif t <= tot:
			print('NO')
		elif tot == s:
			print('YES')
		else:
			print('NO')


from collections import Counter

def cal(dics, dict):
	if len(dict) > 1:
		return 'YES'
	if dict['a'] <= dics['a']:
		return 'NO'
	if len(dics) > 1:
		return 'NO'
	return 'YES'
for jj in range(int(input())):
	dics = {'a': 1}
	dict = {'a': 1}
	n = int(input())
	for _ in range(n):
		arr = input().split()
		if arr[0] == '1':
			p = dics
		else:
			p = dict
		k = int(arr[1])
		ck = Counter(arr[2])
		for x in ck.keys():
			if x not in p:
				p[x] = ck[x] * k
			else:
				p[x] += ck[x] * k
		print(cal(dics, dict))


for _ in range(int(input())):
	n = int(input())
	one = [0] * 26
	two = [0] * 26
	one[0] = two[0] = 1
	oc = 1
	for i in range(n):
		(d, k, x) = input().split()
		d = int(d)
		k = int(k)
		if d == 1:
			for i in x:
				one[ord(i) - 97] += k
				oc += k
		else:
			for i in x:
				two[ord(i) - 97] += k
		f = False
		for i in range(1, 26):
			if two[i] > 0:
				f = True
				break
		if f:
			print('YES')
		elif oc - one[0] > 0:
			print('NO')
		elif one[0] >= two[0]:
			print('NO')
		else:
			print('YES')


o = int(input())
while o:
	o -= 1
	n = int(input())
	s = [1] + [0] * 25
	t = [1] + [0] * 25
	while n:
		n -= 1
		(d, k, x) = input().split()
		d = int(d)
		k = int(k)
		for c in x:
			if d == 1:
				s[ord(c) - ord('a')] += k
			else:
				t[ord(c) - ord('a')] += k
		if sum(s) == s[0] and sum(t) == t[0]:
			print('YES' if s[0] < t[0] else 'NO')
			continue
		for i in range(26):
			if s[i] > 0:
				if sum(t[i + 1:]) > 0:
					print('YES')
					break
		else:
			print('NO')


for _ in range(int(input())):
	len1 = len2 = 1
	letters1 = [0] * 26
	letters2 = [0] * 26
	letters1[0] = letters2[0] = 1
	for _ in range(int(input())):
		(d, k, s) = input().split()
		(d, k) = map(int, (d, k))
		if d == 1:
			len1 += len(s) * k
		elif d == 2:
			len2 += len(s) * k
		for x in s:
			if d == 1:
				letters1[ord(x) - ord('a')] += k
			elif d == 2:
				letters2[ord(x) - ord('a')] += k
		if any(letters2[1:]):
			print('YES')
		else:
			k = 0
			i = 0
			while i < 26 and letters1[i] == letters2[i]:
				k += letters1[i]
				i += 1
			if i == 26:
				print('NO')
			elif letters1[i] < letters2[i] and len1 - k - letters1[i]:
				print('NO')
			elif letters1[i] < letters2[i] and (not len1 - k - letters1[i]):
				print('YES')
			elif letters1[i] > letters2[i] and len2 - k - letters2[i]:
				print('YES')
			else:
				print('NO')


import sys, os
if os.getlogin() == 'arun':
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
from collections import defaultdict
from math import comb
import math
t = int(input())
for _ in range(t):
	n = int(input())
	d1 = defaultdict(lambda : 0)
	d2 = defaultdict(lambda : 0)
	d1['a'] += 1
	d2['a'] += 1
	sm1 = sm2 = 1
	for i in range(n):
		(a, b, st) = input().split()
		a = int(a)
		b = int(b)
		if a == 1:
			for x in st:
				d1[x] += b
				sm1 += b
		else:
			for x in st:
				d2[x] += b
				sm2 += b
		k1 = sorted(list(d1.keys()))
		k2 = sorted(list(d2.keys()), key=lambda x: -ord(x))
		if k1[0] < k2[0]:
			print('YES')
		elif k1[0] == k2[0] and d1[k1[0]] < d2[k2[0]] and (sm1 - d1[k1[0]] == 0):
			print('YES')
		elif k1[0] == k2[0] and d1[k1[0]] < d2[k2[0]]:
			print('NO')
		elif k1[0] > k2[0]:
			print('NO')
		else:
			print('NO')


t = int(input())
for tt in range(t):
	q = int(input())
	s1 = [1] + [0] * 25
	t1 = [1] + [0] * 25
	for qq in range(q):
		(x, y, s) = input().split()
		(x, y) = (int(x), int(y))
		if x == 1:
			for r in s:
				s1[ord(r) - ord('a')] += y
		else:
			for rr in s:
				t1[ord(rr) - ord('a')] += y
		i = 0
		j = 25
		while s1[i] == 0:
			i += 1
		while t1[j] == 0:
			j -= 1
		if i < j or (sum(s1) == s1[i] and sum(t1) == t1[j] and (s1[i] < t1[j])) or (sum(s1) != s1[i] and sum(t1) != t1[j] and (s1[i] > t1[j])):
			print('YES')
		else:
			print('NO')


iter = int(input())
for _ in range(iter):
	q = int(input())
	ass = [0] * 26
	ass[0] = 1
	at = [0] * 26
	at[0] = 1
	for i in range(q):
		inp = input().split(' ')
		x = inp[2]
		c = inp[0]
		k = int(inp[1])
		if c == '1':
			for char in x:
				ass[ord(char) - 97] += k
		else:
			for char in x:
				at[ord(char) - 97] += k
		ss = 0
		while ass[ss] == 0:
			ss += 1
		tf = 25
		while at[tf] == 0:
			tf -= 1
		if ss < tf:
			print('YES')
		elif tf < ss:
			print('NO')
		elif ass[ss] < at[tf]:
			ss += 1
			while ss != 26 and ass[ss] == 0:
				ss += 1
			if ss == 26:
				print('YES')
			else:
				print('NO')
		else:
			print('NO')


def f():
	s = [1] + 25 * [0]
	t = [1] + 25 * [0]
	q = int(input())
	for i in range(q):
		(a, b, d) = map(str, input().split())
		b = int(b)
		if a == '1':
			for i in d:
				s[ord(i) - ord('a')] += b
		else:
			for i in d:
				t[ord(i) - ord('a')] += b
		ans = True
		for i in range(26):
			if ans and s[i] > t[i] and (sum(t[i + 1:]) > 0):
				print('YES')
				ans = False
			if ans and s[i] != 0 and (sum(t[i + 1:]) > 0):
				print('YES')
				ans = False
			if ans and s[i] < t[i] and (sum(t[i + 1:]) == sum(s[i + 1:]) == 0):
				print('YES')
				ans = False
		if ans:
			print('NO')
t = int(input())
for i in range(t):
	f()


for _ in range(int(input())):
	a1 = [0] * 26
	a2 = [0] * 26
	a1[0] = 1
	a2[0] = 1
	a1_len = 1
	a2_len = 1
	for __ in range(int(input())):
		(d, k, q) = input().split()
		k = int(k)
		ans = 'NO'
		if d == '1':
			a1_len += k * len(q)
			for x in range(len(q)):
				a1[ord(q[x]) - 97] += k
		if d == '2':
			a2_len += k * len(q)
			for x in range(len(q)):
				a2[ord(q[x]) - 97] += k
		a1_letters = 0
		a2_letters = 0
		for x in range(26):
			if a1[x] != 0:
				a1_letters += 1
			if a2[x] != 0:
				a2_letters += 1
		if a1_letters > 1 and a2_letters > 1:
			ans = 'YES'
		elif a1_letters == a2_letters:
			if a1_letters == 1:
				if a1[0] < a2[0]:
					ans = 'YES'
			else:
				ans = 'YES'
		elif a1_letters == 1 and a2_letters != 1:
			ans = 'YES'
		print(ans)


for _ in range(int(input())):
	s = {chr(ord('a') + i): 0 for i in range(26)}
	s['a'] = 1
	t = {chr(ord('a') + i): 0 for i in range(26)}
	t['a'] = 1
	ls = 1
	lt = 1
	for u in range(int(input())):
		(d, k, h) = input().split()
		k = int(k)
		if d == '1':
			for r in h:
				s[r] += k
				ls += k
		elif d == '2':
			for r in h:
				t[r] += k
				lt += k
		h = ''
		for i in range(26):
			if h != '' and t[chr(ord('a') + i)] > 0:
				print('YES')
				break
			if h == '' and s[chr(ord('a') + i)] > 0:
				h = chr(ord('a') + i)
		else:
			if s[h] == ls and (t[h] > s[h] or (t[h] == s[h] and lt > ls)):
				print('YES')
			else:
				print('NO')


from collections import Counter
t = int(input())
for __ in range(t):
	q = int(input())
	s = [1] + [0] * 26
	t = [1] + [0] * 26
	for __ in range(q):
		(d, k, cs) = input().split(' ')
		d = int(d)
		k = int(k)
		cd = Counter(cs)
		if d == 1:
			for (c, cc) in cd.items():
				s[ord(c) - ord('a')] += cc * k
		else:
			for (c, cc) in cd.items():
				t[ord(c) - ord('a')] += cc * k
		rptr = 25
		isv = 'YES'
		nt = t.copy()
		for lptr in range(26):
			if s[lptr] == 0:
				continue
			while rptr >= 0 and nt[rptr] == 0:
				rptr -= 1
			if lptr < rptr:
				break
			elif lptr > rptr:
				isv = 'NO'
				break
			elif s[lptr] < nt[rptr]:
				nt[rptr] -= s[lptr]
			else:
				isv = 'NO'
				break
		print(isv)