/*
 * Sample solution to budget / Mikael Goldmann
 *
 */

import java.io.*;
import java.util.*;

class budget 
{
    
    final static boolean debug=false;

    
    static StreamTokenizer tokens = new StreamTokenizer(
      new BufferedReader(    
        new InputStreamReader(System.in)));    

    static int[][] f = new int[100][100]; /* flow */
    static int[][] c = new int[100][100]; /* capacity */

    static int getInt() throws IOException
    {
	tokens.nextToken();
	return (int)Math.round(tokens.nval);
    }

    static int min(int a, int b)     { return a>b ? b : a;  }    
	
    static int rc(int i,int j) {return c[i][j] - f[i][j];} /* residual capacity */

    final static int infty = 1<<30;

    static void init(int m, int n)
    {
	int nv = n+m+2;	
	if (nv > f.length) {
	    f = new int[nv][nv];
	    c = new int[nv][nv];
	}
	    
	for (int i = 0; i < nv; ++i) 
	    for (int j = 0; j < nv; ++j) {
		f[i][j] = 0;      
		c[i][j] =  (i > 0 && i <= m && j > m && j <= n+m) ? infty : 0;
	    }
    }

    static boolean bfs(int s, int t, int nv, int[] p) 
    {
	int qh = 0, qt = 0;
	boolean[] v = new boolean[nv];
	int[] q = new int[nv];
  
	q[qt++] = s;
	p[s] = -1;
	v[s] = true;  
	while (qh < qt) {
	    int u = q[qh++];
	    if (u == t) return true;
	    for (int w = 0; w < nv; ++w)
		if (!v[w] && rc(u,w)>0) {
		    v[w] = true;
		    q[qt++] = w;
		    p[w] = u;
		}
	}
	return false;
    }

    static int augment(int t, int[] p)
    {
	int slack = infty;
	for (int u = t; p[u] != -1; u = p[u]) 
	    slack = min(slack, rc(p[u],u));
	for (int u = t; p[u] != -1; u = p[u]) 
	    f[u][p[u]] = -(f[p[u]][u] += slack);
	return slack;
    }
  
    

    static int maxf(int s, int t, int nv)
    {
	int[] p = new int[nv];
	int fl = 0;
	for (int i = 1; i < nv; ++i)  fl += f[s][i];
	while(bfs(s,t,nv,p)) fl += augment(t,p);
	return fl;
    }
    
    static int impossible(String s)
    {
	if(debug) System.out.println(s);
  
	System.out.println("IMPOSSIBLE");
	return 0;  
    }

    static int solve(int caseno) throws IOException
    {
        //	System.err.println("Case " + caseno);
	
	int n,m,nc;
	int x,y,a;
	int ilo, ihi, jlo, jhi;
	char cmp;
	int s,t;
	int rsum = 0;
	int csum = 0;
	
	if (caseno != 1) System.out.println();  
	m = getInt();
	n = getInt();

	init(m,n);
	s = 0;
	t = m+n+1;  
	
	/* read m row sums */
	for (int i = 1; i <= m; ++i)  { 
	    c[s][i] = getInt();
	    rsum += c[s][i];
	}
  

	/* read n column sums */
	for (int i = 1; i <= n; ++i) {
	    c[m+i][t] = getInt();
	    csum += c[m+i][t];
	}

	/* get constraints */
	nc = getInt();
	while(nc-- > 0) {
	    x = getInt();
	    y = getInt();
	    tokens.nextToken();
	    cmp = (char)tokens.ttype;
	    a = getInt();
	    ilo = x!=0 ? x : 1;
	    ihi = x!=0 ? x : m;
	    jlo = y!=0 ? y : 1;
	    jhi = y!=0 ? y : n;
	    for (int i = ilo; i <= ihi; ++i)
		for (int j = jlo; j <= jhi; ++j)
		    switch (cmp) {
		    case '=':
			c[i][m+j] = min(c[i][m+j],a);
			c[m+j][i] = min(c[m+j][i],-a);
			break;
		    case '>':
			c[m+j][i] = min(c[m+j][i],-(1+a));
			break;
		    case '<':
			c[i][m+j] = min(c[i][m+j],a-1);
			break;
		    default:System.err.println("OP ERROR!!!");
		    }
	}
	
	if (csum != rsum) return impossible("csum != rsum");
	
	
	/* find feasible flow */
	for (int i = 1; i <= m; ++i)
	    for (int j = m+1; j <= n+m; ++j)
		if ((x=rc(j,i)) < 0) {  /* try to fix all > constraints */
		    f[i][j] = -(f[j][i] += x);
		    f[s][i] = -(f[i][s] += x);
		    f[j][t] = -(f[t][j] += x);
		}  
	for (int i = 0; i <= m+n+1; ++i) /* check that we succeded */
	    for (int j = 0; j <= n+m+1; ++j)
		if (rc(i,j) < 0) 	return impossible("No legal initial flow");
	
	/* Check that max flow = total romsums */
	if (maxf(s,t,m+n+2) != rsum) return impossible("max flow != rowsum");
	
	/* print result */
	for (int i = 1; i <= m; ++i) 
	    for (int j = m+1; j <= n+m; ++j) {
		if (j < m+n) System.out.print(f[i][j] + " ");
		else  System.out.println(f[i][j] );
	    }
	
	return 1;
	
    }
    
    public static  void  main(String[] ss) throws IOException
    {
	int ncases;
	ncases = getInt();	
        //	System.err.println("ncases="+ncases);
	
	for (int i=1; i <=ncases; ++i) solve(i);
    }
    
}

