#define _CRT_SECURE_NO_WARNINGS
//#define _USE_MATH_DEFINES
#include<stdio.h>
//#include<stdlib.h>
//#include<math.h>
//#include<string.h>
//#include<time.h>
//#include<ctype.h>
//#define pi acos(-1.0)
#define P(type,x) fprintf(stdout,"%"#type"\n",x)
#define S(type,x) fscanf(stdin,"%"#type,&x)
//#define MAX(a,b) a>b?a:b
//#define MIN(a,b) a<b?a:b

int main() {
	int w, d,ans=0;
	for (; ~fscanf(stdin, "%d%d", &w, &d), w, d;P(d,ans)) {
		int a[21] = {},
			i=0,b;
		ans = 0;
		for (; i < w; i++) {
			S(d, b);
			a[b]++;
			ans += b;
		}
		for (i = 0; i < d; i++) {
			S(d, b);
			if (a[b]) a[b]--;
			else ans += b;
		}
	}
	return 0;
}