#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <functional>
#include <string>
#include <iostream>
#include <cctype>
#include <set>
#include <climits>
#include <iomanip>
#include <cassert>
using namespace std;
#define D(x) cout<<#x " = "<<(x)<<endl
#define un(x) x.erase(unique(x.begin(),x.end()), x.end())
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pb push_back
#define pi 2*acos(0.0)
#define mp make_pair
#define xx first
#define yy second
#define hp (ll) 999983
#define mx 100000
#define mod 1000000007
#define endl '\n'
typedef long long int ll;
ll F[mx+7];
vector<pair<int, char> > A, B;
void failure_function( char *pattern, ll len ) {
F[0] = F[1] = 0;
ll i,j;
for( i=2; i<=len; i++ ) {
j = F[i-1];
while( 1 ) {
if( pattern[j] == pattern[i-1] ) {
F[i] = j + 1;
break;
}
if( j==0) {
F[i] = 0;
break;
}
j = F[j];
}
}
}
ll KMP( char *text, ll len_t, char *pattern, ll len_p ) {
failure_function( pattern, len_p );
ll i = 0,j = 0, ret = 0, h, k, m;
deque<ll> v;
while( j<len_t ) {
if( text[j] == pattern[i] ) {
i++;
j++;
v.pb(j - 1);
if( i == len_p ) {
if(v.size() == 1) {
cout << A[j - 1].first << " " << B[i - 1].first << endl;
if(A[j - 1].first >= B[i - 1].first) {
ret += A[j - 1].first - B[i - 1].first;
}
if(A[j - 1].first == B[i - 1].first) {
ret ++;
}
} else {
m = 0;
for(h = 0; h < v.size(); h ++) {
k = v[h];
if(h == 0 || h == v.size() - 1) {
if(A[k].first >= B[k].first) m ++;
else break;
} else if(A[k].first == B[k].first) m ++;
else break;
//cout << A[k].first << " " << B[k].first << endl;
}
if(m == len_p) {
ret ++;
}
}
}
} else if( i > 0 ) i = F[i], v.clear();
else {
j++;
if(!v.empty()) v.pop_front();
}
}
return ret;
}
int main() {
int i, j, k, n, m, l, a, b;
char t[mx + 10], s[mx + 10], ch;
sff(n, m);
for(i = 1, j = 0; i <= n; i ++) {
scanf("%d-%c", &l, &ch);
if(A.size()) {
if(A.back().second == ch) {
A.back().first += l;
continue;
}
}
A.pb(make_pair(l, ch));
t[j++] = ch;
}
t[j] = '\0';
for(i = 1, j = 0; i <= m; i ++) {
scanf("%d-%c", &l, &ch);
if(B.size()) {
if(B.back().second == ch) {
B.back().first += l;
continue;
}
}
B.pb(make_pair(l, ch));
s[j++] = ch;
}
s[j++] = '\0';
a = strlen(t), b = strlen(s);
cout << s << " " << t << endl;
printf("%d\n", KMP(t, a, s, b));
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPG1hcD4KI2luY2x1ZGUgPHF1ZXVlPgojaW5jbHVkZSA8c3RhY2s+CiNpbmNsdWRlIDx2ZWN0b3I+CiNpbmNsdWRlIDxkZXF1ZT4KI2luY2x1ZGUgPGZ1bmN0aW9uYWw+CiNpbmNsdWRlIDxzdHJpbmc+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGNjdHlwZT4KI2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPGNsaW1pdHM+CiNpbmNsdWRlIDxpb21hbmlwPgojaW5jbHVkZSA8Y2Fzc2VydD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIEQoeCkgICAgICAgIGNvdXQ8PCN4ICIgPSAiPDwoeCk8PGVuZGwKI2RlZmluZSB1bih4KSAgICAgICB4LmVyYXNlKHVuaXF1ZSh4LmJlZ2luKCkseC5lbmQoKSksIHguZW5kKCkpCiNkZWZpbmUgc2YobikgICAgICAgc2NhbmYoIiVkIiwgJm4pCiNkZWZpbmUgc2ZmKGEsYikgICAgc2NhbmYoIiVkICVkIiwgJmEsICZiKQojZGVmaW5lIHNmZmYoYSxiLGMpIHNjYW5mKCIlZCAlZCAlZCIsICZhLCAmYiwgJmMpCiNkZWZpbmUgcGIgICAgICAgICAgcHVzaF9iYWNrCiNkZWZpbmUgcGkgICAgICAgICAgMiphY29zKDAuMCkKI2RlZmluZSBtcCAgICAgICAgICBtYWtlX3BhaXIKI2RlZmluZSB4eCAgICAgICAgICBmaXJzdAojZGVmaW5lIHl5ICAgICAgICAgIHNlY29uZAojZGVmaW5lIGhwICAgICAgICAgIChsbCkgOTk5OTgzCiNkZWZpbmUgbXggICAgICAgICAgMTAwMDAwCiNkZWZpbmUgbW9kICAgICAgICAgMTAwMDAwMDAwNwojZGVmaW5lIGVuZGwgICAgICAgICdcbicKCnR5cGVkZWYgbG9uZyBsb25nIGludCBsbDsKCmxsIEZbbXgrN107CnZlY3RvcjxwYWlyPGludCwgY2hhcj4gPiBBLCBCOwp2b2lkIGZhaWx1cmVfZnVuY3Rpb24oIGNoYXIgKnBhdHRlcm4sIGxsIGxlbiApIHsKICAgIEZbMF0gPSBGWzFdID0gMDsKICAgIGxsIGksajsKICAgIGZvciggaT0yOyBpPD1sZW47IGkrKyApIHsKICAgICAgICBqID0gRltpLTFdOwogICAgICAgIHdoaWxlKCAxICkgewogICAgICAgICAgICBpZiggcGF0dGVybltqXSA9PSBwYXR0ZXJuW2ktMV0gKSB7CiAgICAgICAgICAgICAgICBGW2ldID0gaiArIDE7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiggaj09MCkgewogICAgICAgICAgICAgICAgRltpXSA9IDA7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgICBqID0gRltqXTsKICAgICAgICB9CiAgICB9Cn0KCmxsIEtNUCggY2hhciAqdGV4dCwgbGwgbGVuX3QsIGNoYXIgKnBhdHRlcm4sIGxsIGxlbl9wICApIHsKICAgIGZhaWx1cmVfZnVuY3Rpb24oIHBhdHRlcm4sIGxlbl9wICk7CiAgICBsbCBpID0gMCxqID0gMCwgcmV0ID0gMCwgaCwgaywgbTsKICAgIGRlcXVlPGxsPiB2OwogICAgd2hpbGUoIGo8bGVuX3QgKSB7CiAgICAgICAgaWYoIHRleHRbal0gPT0gcGF0dGVybltpXSApIHsKICAgICAgICAgICAgaSsrOwogICAgICAgICAgICBqKys7CiAgICAgICAgICAgIHYucGIoaiAtIDEpOwogICAgICAgICAgICBpZiggaSA9PSBsZW5fcCApIHsKICAgICAgICAgICAgICAgIGlmKHYuc2l6ZSgpID09IDEpIHsKICAgICAgICAgICAgICAgICAgICBjb3V0IDw8IEFbaiAtIDFdLmZpcnN0IDw8ICIgIiA8PCBCW2kgLSAxXS5maXJzdCA8PCBlbmRsOwogICAgICAgICAgICAgICAgICAgIGlmKEFbaiAtIDFdLmZpcnN0ID49IEJbaSAtIDFdLmZpcnN0KSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHJldCArPSBBW2ogLSAxXS5maXJzdCAtIEJbaSAtIDFdLmZpcnN0OwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZihBW2ogLSAxXS5maXJzdCA9PSBCW2kgLSAxXS5maXJzdCkgewogICAgICAgICAgICAgICAgICAgICAgICByZXQgKys7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBtID0gMDsKICAgICAgICAgICAgICAgICAgICBmb3IoaCA9IDA7IGggPCB2LnNpemUoKTsgaCArKykgewogICAgICAgICAgICAgICAgICAgICAgICBrID0gdltoXTsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoaCA9PSAwIHx8IGggPT0gdi5zaXplKCkgLSAxKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZihBW2tdLmZpcnN0ID49IEJba10uZmlyc3QpIG0gKys7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBlbHNlIGJyZWFrOwogICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgaWYoQVtrXS5maXJzdCA9PSBCW2tdLmZpcnN0KSBtICsrOwogICAgICAgICAgICAgICAgICAgICAgICBlbHNlIGJyZWFrOwogICAgICAgICAgICAgICAgICAgICAgICAvL2NvdXQgPDwgQVtrXS5maXJzdCA8PCAiICIgPDwgQltrXS5maXJzdCA8PCBlbmRsOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZihtID09IGxlbl9wKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHJldCArKzsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9IGVsc2UgaWYoIGkgPiAwICkgaSA9IEZbaV0sIHYuY2xlYXIoKTsKICAgICAgICBlbHNlIHsKICAgICAgICAgICAgaisrOwogICAgICAgICAgICBpZighdi5lbXB0eSgpKSB2LnBvcF9mcm9udCgpOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiByZXQ7Cn0KCmludCBtYWluKCkgewogICAgaW50IGksIGosIGssIG4sIG0sIGwsIGEsIGI7CiAgICBjaGFyIHRbbXggKyAxMF0sIHNbbXggKyAxMF0sIGNoOwogICAgc2ZmKG4sIG0pOwogICAgZm9yKGkgPSAxLCBqID0gMDsgaSA8PSBuOyBpICsrKSB7CiAgICAgICAgc2NhbmYoIiVkLSVjIiwgJmwsICZjaCk7CiAgICAgICAgaWYoQS5zaXplKCkpIHsKICAgICAgICAgICAgaWYoQS5iYWNrKCkuc2Vjb25kID09IGNoKSB7CiAgICAgICAgICAgICAgICBBLmJhY2soKS5maXJzdCArPSBsOwogICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgQS5wYihtYWtlX3BhaXIobCwgY2gpKTsKICAgICAgICB0W2orK10gPSBjaDsKICAgIH0KICAgIHRbal0gPSAnXDAnOwoKICAgIGZvcihpID0gMSwgaiA9IDA7IGkgPD0gbTsgaSArKykgewogICAgICAgIHNjYW5mKCIlZC0lYyIsICZsLCAmY2gpOwogICAgICAgIGlmKEIuc2l6ZSgpKSB7CiAgICAgICAgICAgIGlmKEIuYmFjaygpLnNlY29uZCA9PSBjaCkgewogICAgICAgICAgICAgICAgQi5iYWNrKCkuZmlyc3QgKz0gbDsKICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIEIucGIobWFrZV9wYWlyKGwsIGNoKSk7CiAgICAgICAgc1tqKytdID0gY2g7CiAgICB9CiAgICBzW2orK10gPSAnXDAnOwogICAgYSA9IHN0cmxlbih0KSwgYiA9IHN0cmxlbihzKTsKICAgIGNvdXQgPDwgcyA8PCAiICIgPDwgdCA8PCBlbmRsOwogICAgcHJpbnRmKCIlZFxuIiwgS01QKHQsIGEsIHMsIGIpKTsKfQo=