#include <bits/stdc++.h>
using namespace std;
inline long long gi() {
  char c;
  long long num = 0, flg = 1;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') flg = -1;
  while (c >= '0' && c <= '9') {
    num = num * 10 + 1ll * c - 48;
    c = getchar();
  }
  return num * flg;
}
const int mod = 998244353;
const int inv2 = 499122177;
struct SegTrie {
  int ch[1000005][2], tot, rt;
  bool flg[1000005];
  void insert(int &i, long long l, long long r, long long ql, long long qr) {
    if (!i) i = ++tot;
    if (ql == l && r == qr) {
      flg[i] = 1;
      return;
    }
    long long mid = (l + r) >> 1;
    if (ql < mid) insert(ch[i][0], l, mid, ql, qr < mid ? qr : mid);
    if (qr > mid) insert(ch[i][1], mid, r, ql > mid ? ql : mid, qr);
  }
} A, B;
vector<pair<int, int> > q[60 + 5];
long long BFS(int d, long long sum) {
  for (int i = 0; i < (int)q[d].size(); i++)
    if (A.flg[q[d][i].first] || B.flg[q[d][i].second])
      return (sum + 1ll * ((1ll << d) - 1) % mod * inv2) % mod *
             ((1ll << d) % mod) % mod;
  long long ret = 0;
  for (int x = 0; x <= 1; x++) {
    q[d - 1].clear();
    for (int i = 0; i < (int)q[d].size(); i++)
      for (int y = 0; y <= 1; y++)
        if (A.ch[q[d][i].first][y] && B.ch[q[d][i].second][x ^ y])
          q[d - 1].push_back(
              make_pair(A.ch[q[d][i].first][y], B.ch[q[d][i].second][x ^ y]));
    if (q[d - 1].size())
      ret = (ret + 1ll * BFS(d - 1, sum | (1ll * x << (d - 1)))) % mod;
  }
  return ret;
}
int main() {
  int na, nb, i;
  long long l, r;
  na = gi();
  for (i = 1; i <= na; i++) {
    l = gi();
    r = gi() + 1;
    A.insert(A.rt, 0, 1ll << 60, l, r);
  }
  nb = gi();
  for (i = 1; i <= nb; i++) {
    l = gi();
    r = gi() + 1;
    B.insert(B.rt, 0, 1ll << 60, l, r);
  }
  q[60].push_back(make_pair(A.rt, B.rt));
  printf("%lld\n", BFS(60, 0ll));
}
