from collections import defaultdict, deque
def find_max_length(t, test_cases):
results = []
for n, a in test_cases:
b = [a[i] + i for i in range(n)] # b[i] = a[i] + (i + 1)
c = [b[i] + i for i in range(n)] # c[i] = b[i] + (i + 1)
u = [(b[i], i) for i in range(n)] # Pair (b[i], i)
v = [(c[i], i) for i in range(n)] # Pair (c[i], i)
# Sort both u and v based on the first element of the pair
u.sort()
v.sort()
# Use two pointers to find pairs (i, j) such that u[i].first == v[j].first
graph = defaultdict(list)
i, j = 0, 0
while i < n and j < n:
if u[i][0] == v[j][0]: # Found a match
graph[u[i][1]].append(v[j][1])
i += 1
elif u[i][0] < v[j][0]:
i += 1
else:
j += 1
# Initialize d array and perform BFS/DFS to find reachable nodes
d = [0] * n
visited = [False] * n
def bfs(start):
queue = deque([start])
reachable_values = []
while queue:
node = queue.popleft()
if visited[node]:
continue
visited[node] = True
reachable_values.append(c[node])
for neighbor in graph[node]:
if not visited[neighbor]:
queue.append(neighbor)
return max(reachable_values, default=0)
# Compute d[i] for each vertex
for i in range(n):
if not visited[i]:
max_c = bfs(i)
d[i] = max_c
count = 0
# Count valid positions
for i in range(n):
if b[i] == n:
count = max(count, d[i])
# The answer for this test case
results.append(count)
return results
# Read input
t = int(input())
test_cases = []
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
test_cases.append((n, a))
# Get results
results = find_max_length(t, test_cases)
# Print results
for res in results:
print(res)
ZnJvbSBjb2xsZWN0aW9ucyBpbXBvcnQgZGVmYXVsdGRpY3QsIGRlcXVlCgpkZWYgZmluZF9tYXhfbGVuZ3RoKHQsIHRlc3RfY2FzZXMpOgogICAgcmVzdWx0cyA9IFtdCiAgICAKICAgIGZvciBuLCBhIGluIHRlc3RfY2FzZXM6CiAgICAgICAgYiA9IFthW2ldICsgaSBmb3IgaSBpbiByYW5nZShuKV0gICMgYltpXSA9IGFbaV0gKyAoaSArIDEpCiAgICAgICAgYyA9IFtiW2ldICsgaSBmb3IgaSBpbiByYW5nZShuKV0gICMgY1tpXSA9IGJbaV0gKyAoaSArIDEpCiAgICAgICAgCiAgICAgICAgdSA9IFsoYltpXSwgaSkgZm9yIGkgaW4gcmFuZ2UobildICAjIFBhaXIgKGJbaV0sIGkpCiAgICAgICAgdiA9IFsoY1tpXSwgaSkgZm9yIGkgaW4gcmFuZ2UobildICAjIFBhaXIgKGNbaV0sIGkpCgogICAgICAgICMgU29ydCBib3RoIHUgYW5kIHYgYmFzZWQgb24gdGhlIGZpcnN0IGVsZW1lbnQgb2YgdGhlIHBhaXIKICAgICAgICB1LnNvcnQoKQogICAgICAgIHYuc29ydCgpCgogICAgICAgICMgVXNlIHR3byBwb2ludGVycyB0byBmaW5kIHBhaXJzIChpLCBqKSBzdWNoIHRoYXQgdVtpXS5maXJzdCA9PSB2W2pdLmZpcnN0CiAgICAgICAgZ3JhcGggPSBkZWZhdWx0ZGljdChsaXN0KQogICAgICAgIAogICAgICAgIGksIGogPSAwLCAwCiAgICAgICAgd2hpbGUgaSA8IG4gYW5kIGogPCBuOgogICAgICAgICAgICBpZiB1W2ldWzBdID09IHZbal1bMF06ICAjIEZvdW5kIGEgbWF0Y2gKICAgICAgICAgICAgICAgIGdyYXBoW3VbaV1bMV1dLmFwcGVuZCh2W2pdWzFdKQogICAgICAgICAgICAgICAgaSArPSAxCiAgICAgICAgICAgIGVsaWYgdVtpXVswXSA8IHZbal1bMF06CiAgICAgICAgICAgICAgICBpICs9IDEKICAgICAgICAgICAgZWxzZToKICAgICAgICAgICAgICAgIGogKz0gMQoKICAgICAgICAjIEluaXRpYWxpemUgZCBhcnJheSBhbmQgcGVyZm9ybSBCRlMvREZTIHRvIGZpbmQgcmVhY2hhYmxlIG5vZGVzCiAgICAgICAgZCA9IFswXSAqIG4KICAgICAgICB2aXNpdGVkID0gW0ZhbHNlXSAqIG4KCiAgICAgICAgZGVmIGJmcyhzdGFydCk6CiAgICAgICAgICAgIHF1ZXVlID0gZGVxdWUoW3N0YXJ0XSkKICAgICAgICAgICAgcmVhY2hhYmxlX3ZhbHVlcyA9IFtdCiAgICAgICAgICAgIHdoaWxlIHF1ZXVlOgogICAgICAgICAgICAgICAgbm9kZSA9IHF1ZXVlLnBvcGxlZnQoKQogICAgICAgICAgICAgICAgaWYgdmlzaXRlZFtub2RlXToKICAgICAgICAgICAgICAgICAgICBjb250aW51ZQogICAgICAgICAgICAgICAgdmlzaXRlZFtub2RlXSA9IFRydWUKICAgICAgICAgICAgICAgIHJlYWNoYWJsZV92YWx1ZXMuYXBwZW5kKGNbbm9kZV0pCiAgICAgICAgICAgICAgICBmb3IgbmVpZ2hib3IgaW4gZ3JhcGhbbm9kZV06CiAgICAgICAgICAgICAgICAgICAgaWYgbm90IHZpc2l0ZWRbbmVpZ2hib3JdOgogICAgICAgICAgICAgICAgICAgICAgICBxdWV1ZS5hcHBlbmQobmVpZ2hib3IpCiAgICAgICAgICAgIHJldHVybiBtYXgocmVhY2hhYmxlX3ZhbHVlcywgZGVmYXVsdD0wKQoKICAgICAgICAjIENvbXB1dGUgZFtpXSBmb3IgZWFjaCB2ZXJ0ZXgKICAgICAgICBmb3IgaSBpbiByYW5nZShuKToKICAgICAgICAgICAgaWYgbm90IHZpc2l0ZWRbaV06CiAgICAgICAgICAgICAgICBtYXhfYyA9IGJmcyhpKQogICAgICAgICAgICAgICAgZFtpXSA9IG1heF9jCgogICAgICAgIGNvdW50ID0gMAogICAgICAgICMgQ291bnQgdmFsaWQgcG9zaXRpb25zCiAgICAgICAgZm9yIGkgaW4gcmFuZ2Uobik6CiAgICAgICAgICAgIGlmIGJbaV0gPT0gbjoKICAgICAgICAgICAgICAgIGNvdW50ID0gbWF4KGNvdW50LCBkW2ldKQoKICAgICAgICAjIFRoZSBhbnN3ZXIgZm9yIHRoaXMgdGVzdCBjYXNlCiAgICAgICAgcmVzdWx0cy5hcHBlbmQoY291bnQpCiAgICAKICAgIHJldHVybiByZXN1bHRzCgojIFJlYWQgaW5wdXQKdCA9IGludChpbnB1dCgpKQp0ZXN0X2Nhc2VzID0gW10KCmZvciBfIGluIHJhbmdlKHQpOgogICAgbiA9IGludChpbnB1dCgpKQogICAgYSA9IGxpc3QobWFwKGludCwgaW5wdXQoKS5zcGxpdCgpKSkKICAgIHRlc3RfY2FzZXMuYXBwZW5kKChuLCBhKSkKCiMgR2V0IHJlc3VsdHMKcmVzdWx0cyA9IGZpbmRfbWF4X2xlbmd0aCh0LCB0ZXN0X2Nhc2VzKQoKIyBQcmludCByZXN1bHRzCmZvciByZXMgaW4gcmVzdWx0czoKICAgIHByaW50KHJlcykK