#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define x first
#define y second
#define bg begin()
#define ed end()
#define pb push_back
#define mp make_pair
#define sz(a) int((a).size())
#define R(i,n) for(int i(0);i<(n);++i)
#define L(i,n) for(int i((n)-1);~i;--i)
const int iinf=0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f;
//Data
const int N=500,M=3000;
int ns[N],kv[N],et[M][2];
//Flows
const int fN=N+2;
int fn,s,t;
vector<int> e[fN],to,fw;
void adde(int u,int v,int w){
e[u].pb(sz(to)),to.pb(v),fw.pb(w);
e[v].pb(sz(to)),to.pb(u),fw.pb(0);
}
int pre[fN];
bool bfs(){
R(u,fn) pre[u]=-1;
static queue<int> q; q.push(pre[s]=s);
while(sz(q)){
int u=q.front(); q.pop();
for(int v:e[u])if(fw[v]&&!~pre[to[v]])
pre[to[v]]=v,q.push(to[v]);
}
return ~pre[t];
}
int flow(){
int res=0;
while(bfs()){
int f=iinf;
for(int u=t;u^s;u=to[pre[u]^1])
f=min(f,fw[pre[u]]);
for(int u=t;u^s;u=to[pre[u]^1])
fw[pre[u]]-=f,fw[pre[u]^1]+=f;
res+=f;
}
return res;
}
//Main
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int T; cin>>T;
while(T--){
int n,m,k,x; cin>>n>>m,fn=(t=(s=n)+1)+1;
R(i,m)R(j,2) cin>>et[i][j],--et[i][j];
R(i,n) kv[i]=-1,ns[i]=0;
cin>>k; R(i,k) cin>>x,--x,cin>>kv[x];
R(b,31){ // 这里打成了 R(b,30) 用了 4 个小时
R(u,fn) e[u].clear(); to.clear(),fw.clear();
R(i,m)R(j,2) adde(et[i][j],et[i][j^1],1);
R(i,n)if(~kv[i]&&(kv[i]>>b&1)) adde(s,i,m<<2);
R(i,n)if(~kv[i]&&!(kv[i]>>b&1)) adde(i,t,m<<2);
flow(); R(i,n)if(!~kv[i]&&~pre[i]) ns[i]|=1<<b;
}
R(i,n)if(~kv[i]) ns[i]=kv[i];
R(i,n) cout<<ns[i]<<'\n';
}
return 0;
}