#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
int main(){
int t,m;
int *result;//The test result array
scanf("%d",&t);//Get the test case number
result=(int*)malloc(sizeof(int)*t);//allocate memory
for (int i = 0; i < t; i++)
{
result[i]=1;//intialise with 1
int n,*guest;
string chain;//binary string
scanf("%d",&n);// the number of hats
if(n>15){
std::cout<<"invalid";
return 0;
}
guest=(int*)malloc(sizeof(int)*n);//allocate
m+=n;
int current;//current means the first hat number
for (int j = 0; j < n; j++)
{
scanf("%d",&guest;[j]);//get the input of hat number
}
current=guest[0];
std::cin>>chain;//get the binary string
for (int j = 0; j < n; j++)
{
if (chain[j]=='1')
{
//check if it goes with the current guest
if (current==(j+1))
{
// if it does then shift the next hat
current=guest[j+1];
}else{
// if it doen't then put 0 for this test
result[i]=0;
}
}else{
if (chain[current-1]!='1')
{
//distribute if the current hat is not necessary for the upcoming customer, required to be happy
current=guest[j+1];
}
}
}
free(guest);
}
if(m>1000){
std::cout<<"Invalid";
return 0;
}
for (int r = 0; r < t; r++)
{
std::cout<<(result[r]==0?"NO":"YES");//print the test case result
}
free(result);
return 0;
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}