#include <bits/stdc++.h>
using namespace std;
const long long maxlog = 61, P = 998244353, inv2 = (P + 1) / 2;
vector<pair<long long, long long> > V[4][maxlog + 1], res;
void adda(long long x, long long L, long long R, long long l, long long r,
          long long dep) {
  if (L <= l && r <= R) {
    V[x][dep].push_back(make_pair(l, r));
    return;
  }
  V[x ^ 1][dep].push_back(make_pair(l, r));
  long long m = (l + r) >> 1;
  if (L <= m) adda(x, L, R, l, m, dep + 1);
  if (R > m) adda(x, L, R, m + 1, r, dep + 1);
}
signed main() {
  long long na, nb;
  cin >> na;
  long long l, r;
  for (long long i = 0; i < na; ++i) {
    cin >> l >> r;
    adda(0, l, r, 0, (1ll << maxlog) - 1, 0);
  }
  cin >> nb;
  for (long long i = 0; i < nb; ++i) {
    cin >> l >> r;
    adda(2, l, r, 0, (1ll << maxlog) - 1, 0);
  }
  for (long long d = 0; d <= maxlog; ++d) {
    for (auto pa : V[0][d])
      for (auto pb : V[2][d]) {
        res.push_back(make_pair(pa.first ^ pb.first, 1));
        res.push_back(make_pair((pa.first ^ pb.second) + 1, -1));
      }
    for (auto pa : V[0][d])
      for (auto pb : V[3][d]) {
        res.push_back(make_pair(pa.first ^ pb.first, 1));
        res.push_back(make_pair((pa.first ^ pb.second) + 1, -1));
      }
    for (auto pa : V[1][d])
      for (auto pb : V[2][d]) {
        res.push_back(make_pair(pa.first ^ pb.first, 1));
        res.push_back(make_pair((pa.first ^ pb.second) + 1, -1));
      }
  }
  sort(res.begin(), res.end());
  long long last = 0, cur = 0;
  long long ans = 0;
  for (auto p : res) {
    if (cur > 0) {
      (ans += ((p.first - last) % P) * ((p.first + last - 1) % P) % P * inv2) %=
          P;
    }
    last = p.first;
    cur += p.second;
  }
  cout << ans << endl;
}
