#include #include #include #include #include #include #include #include #include using namespace std; #define rep(i, a, b) for(int i = (a); i < (b); i++) #define per(i, a, b) for(int i = (b) - 1; i >= (a); i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define sz(x) ((int)(x).size()) typedef vector vi; typedef long long ll; typedef pair pii; int main() { int t; cin >> t; while (t--) { int k; cin >> k; vi s(k); rep(i, 0, k) cin >> s[i]; if (k < 4) cout << "invalid\n"; else { vi st(10, 0); int f = 0; rep(i, 0, k) { if (s[i] > 9 || s[i] == 0 || st[s[i]]) f = 1; else { if (i > 0) { switch (s[i]) { case 1: if ((s[i - 1] == 3 && st[2] == 0) || (s[i - 1] == 7 && st[4] == 0) || (s[i - 1] == 9 && st[5] == 0)) f = 1; break; case 2: if (s[i - 1] == 8 && st[5] == 0) f = 1; break; case 3: if ((s[i - 1] == 1 && st[2] == 0) || (s[i - 1] == 7 && st[5] == 0) || (s[i - 1] == 9 && st[6] == 0)) f = 1; break; case 4: if (s[i - 1] == 6 && st[5] == 0) f = 1; break; case 6: if (s[i - 1] == 4 && st[5] == 0) f = 1; break; case 7: if ((s[i - 1] == 1 && st[4] == 0) || (s[i - 1] == 3 && st[5] == 0) || (s[i - 1] == 9 && st[8] == 0)) f = 1; break; case 8: if (s[i - 1] == 2 && st[5] == 0) f = 1; break; case 9: if ((s[i - 1] == 1 && st[5] == 0) || (s[i - 1] == 3 && st[6] == 0) || (s[i - 1] == 7 && st[8] == 0)) f = 1; break; } } st[s[i]] = 1; } } if (f) cout << "invalid\n"; else cout << "valid\n"; } } return 0; }