include bits stdc using namespace std define MAXN 1000000 define WL wh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
#define MAXN 1000000
#define WL(t) while(t--)
#define FOR(i,s,e) for(int i=s; i<e; i++)
vector<int> adj[MAXN];
vector<bool> visited;
int s[MAXN];
int n;
class fastio {
public:
fastio() {
ios_base::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
}
} __fastio;
bool dfs(int v) {
visited[v] = true;
if(v == 2*n+1 && visited[v]) return true;
for(int u : adj[v]) {
if(!visited[u])
dfs(u);
}
return false;
}
void solve() {
cin >> n;
FOR(i,1,2*n+1) {
int r; cin >> r;
if(r == 1 || r == 2) s[i] = 1;
else s[i] = 3;
}
FOR(i,1,2*n+1+1) {
if(s[i] == 1 && i != n) adj[i].push_back(i+1);
if(i <= n){
if(s[i] == 3 && s[i+n] == 3)
adj[i].push_back(i+n+1);
} else {
if(s[i] == 3 && s[i-n] == 3 && i != 2*n)
adj[i].push_back(i-n+1);
}
}
if(dfs(1)) cout << "YES" << endl;
else cout << "NO" << endl;
}
int main() {
__fastio;
int q; cin >> q;
WL(q) {
solve();
}
return 0;
}