#include <iostream>
#include <fstream>

using namespace std;

ifstream reader("DATA3.txt");
ofstream writer("OUT3.txt");

int main(){
    for(int a = 0; a < 5; ++a){
        int n, m;
        reader >> n >> m;
        string lines[n], lines2[m];
        int numbers[n], numbers2[m];
        int sum = 0, sum2 = 0, idents = 0;
        
        for(int b = 0; b < n; ++b){
            reader >> lines[b] >> numbers[b];
        }
        for(int c = 0; c < m; ++c){
            reader >> lines2[c] >> numbers2[c];
        }        
        for(int b = 0; b < n; ++b){
            for(int c = 0; c < m; ++c){
                if(lines[b] == lines2[c]){
                    lines[b] = "";
                    lines2[c] = "";
                    sum += numbers[b];
                    sum2 += numbers2[c];
                }
            }
        }
        
        for(int b = 0; b < n; ++b){
            if(lines[b] != "")
                ++idents;
        }
        for(int c = 0; c < m; ++c){
            if(lines2[c] != "")
                ++idents;
        }
        
        writer << idents << " " << abs(sum - sum2) << endl;
        
        string waste = "";
        reader >> waste; // the --- line
    }
}

