#include<iostream>
#include<vector>
#include<queue>
using namespace std;
struct node{
queue<int> from;
queue<int> to;
bool output=-1;
int gate=-1;
};
int main(){
cin.sync_with_stdio(0);
cin.tie(0);
int p,q,r,m;
cin>>p>>q>>r>>m;
node arr[p+q+r];
queue<int> bfs;
int target_size(0),now_size(0),next_size(0),delay(0);
for(int i=0;i<p;i++){
cin>>arr[i].output;
bfs.push(i);
}
target_size=p;
for(int i=0;i<q;i++){
cin>>arr[i+p].gate;
}
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
arr[a-1].to.push(b-1);
}
while(bfs.size()!=0){
if(now_size==target_size){
now_size=0;
target_size=next_size;
next_size=0;
delay++;
}
int now=bfs.front();
bfs.pop();
while(arr[now].to.size()!=0){
int tonext=arr[now].to.front();
arr[now].to.pop();
arr[tonext].from.push(now);
if( (arr[tonext].gate ==4 && arr[tonext].from.size()==1) || (arr[tonext].from.size()==2) ){
bfs.push(tonext);
next_size++;
if(arr[tonext].gate == 1){
if(arr[arr[tonext].from.front()].output ==1 && arr[arr[tonext].from.back()].output ==1){
arr[tonext].output = 1;
}
else{
arr[tonext].output = 0;
}
}
else if(arr[tonext].gate == 2){
if(arr[arr[tonext].from.front()].output ==1 || arr[arr[tonext].from.back()].output ==1){
arr[tonext].output = 1;
}
else{
arr[tonext].output = 0;
}
}
else if(arr[tonext].gate == 3){
if(arr[arr[tonext].from.front()].output != arr[arr[tonext].from.back()].output){
arr[tonext].output = 1;
}
else{
arr[tonext].output = 0;
}
}
else{
if(arr[arr[tonext].from.front()].output == 0){
arr[tonext].output = 1;
}
else{
arr[tonext].output = 0;
}
}
}
}
now_size++;
}
cout<<delay<<endl;
for(int i=0;i<r;i++){
cout<<arr[arr[p+q+i].from.front()].output<<" ";
}
return 0;
}