t = int(input())
for _ in range(t):
x = int(input())
arr = list(map(int, input().split()))
a = arr[x - 1]
if a == 0:
print("NO")
else:
b = arr[a - 1]
if b == 0:
print("NO")
else:
print("YES")
#include <bits/stdc++.h>
// #include <bits/stdc++.h>
#define ll long long int
#define dis(a) cout<<a<<"\n";
#define ret(a) {dis(a); return;}
ll expo1(ll a, ll b) {ll res = 1;while (b > 0){if(b & 1)res=(res*a);a=(a*a);b = b >> 1;} return res;}
ll expo(ll a,ll b,ll MOD=1e9+7) {ll res = 1; a%=MOD; while(b>0){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b=b>>1;} return res;}
using namespace std;
void solve(){
int n;
cin>>n;
int a[3];
cin>>a[0]>>a[1]>>a[2];
if(a[n-1]!=0 && a[a[n-1]-1]!=0 && a[a[a[n-1]-1]-1]==0) ret("YES")
ret("NO")
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int t=1;
cin>>t;
while(t--){
solve();
}
return 0;
}
// 1 2 1 3 4
1920. Build Array from Permutation | 494. Target Sum |
797. All Paths From Source to Target | 1547B - Alphabetical Strings |
1550A - Find The Array | 118B - Present from Lena |
27A - Next Test | 785. Is Graph Bipartite |
90. Subsets II | 1560A - Dislike of Threes |
36. Valid Sudoku | 557. Reverse Words in a String III |
566. Reshape the Matrix | 167. Two Sum II - Input array is sorted |
387. First Unique Character in a String | 383. Ransom Note |
242. Valid Anagram | 141. Linked List Cycle |
21. Merge Two Sorted Lists | 203. Remove Linked List Elements |
733. Flood Fill | 206. Reverse Linked List |
83. Remove Duplicates from Sorted List | 116. Populating Next Right Pointers in Each Node |
145. Binary Tree Postorder Traversal | 94. Binary Tree Inorder Traversal |
101. Symmetric Tree | 77. Combinations |
46. Permutations | 226. Invert Binary Tree |