using namespace std;
#include <iostream>
#include <vector>


void presipaj(int *a, int *b)
{
    if (*a <= *b)
    {
        *(b) -= *(a);
        *(a) = 0;
    }
    else
    {
        *(a) -= *(b);
        *(b) = 0;
    }

}
int main()
{
    int N, M;
    //cout << "Unesi N i M\n";

    cin >> N;
    cin >> M;
    
    int *NiMin = new int(N);
    int *NiMax = new int(N);

    int* MjMin = new int(M);
    int* MjMax = new int(M);

    int maxN = 0, maxM = 0;
    int minN = 0, minM = 0;

    int* farma = new int[M * N];
    
    

    //cout << "Unesi ogranicenja za redove\n";
    for (int i = 0; i < N; i++)
    {
        cin >> NiMin[i];
        cin >> NiMax[i];
        maxN += NiMax[i];
        minN += NiMin[i];
    }
    
   /* cout << "PROVERA:\n";
    for (int i = 0; i < N; i++)
    {
        cout << NiMin[i];
        cout << " ";
        cout << NiMax[i];
        cout << endl;
    }*/

    //cout << "Unesi ogranicenja za kolone\n";
    for (int i = 0; i < M; i++)
    {
        cin >> MjMin[i];
        cin >> MjMax[i];
        maxM += MjMax[i];
        minM += MjMin[i];
    }
    /*cout << "PROVERA:\n";
    for (int i = 0; i < N; i++)
    {
        cout << MjMin[i];
        cout << " ";
        cout << MjMax[i];
        cout << endl;
    }*/
    int K = 0;

    if (maxN < maxM)
    {
        


        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            {
                if (NiMax[i] < MjMax[j])
                {
                    
                    farma[i * M + j] = NiMax[i];
                   
                    MjMax[j] -= NiMax[i];
                    NiMax[i] = 0;
                    
                }
                else
                {
                    farma[i * M + j] = MjMax[j];
                    NiMax[i] -= MjMax[j];
                    MjMax[j] = 0;
                    
                }
                
            }
        }
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            {
                cout << farma[i * M + j] << " ";
            }
            cout << endl;
        }
    }
    else
    {
        cout << maxM;
    }


}