#include <bits/stdc++.h>
using namespace std;
const long long INF = (1LL << 60) - 1;
const long long mo = 998244353;
const long long inv2 = 499122177;
template <typename _T>
void read(_T &x) {
  _T f = 1;
  x = 0;
  char s = getchar();
  while ('0' > s || '9' < s) {
    if (s == '-') f = -1;
    s = getchar();
  }
  while ('0' <= s && s <= '9') {
    x = (x << 3) + (x << 1) + (s ^ 48);
    s = getchar();
  }
  x *= f;
}
struct ming {
  long long l, r;
} a[105], b[105];
int na, nb, tot, ch[100000][2], root;
vector<ming> tpa[65], tpb[65], ans;
void work1(int &rt, long long l, long long r, long long al, long long ar,
           int id) {
  if (!rt)
    rt = ++tot, ch[rt][0] = ch[rt][1] = 0, tpa[id].push_back((ming){l, r});
  if (al <= l && r <= ar) return;
  long long mid = l + r >> 1;
  if (al <= mid) work1(ch[rt][0], l, mid, al, ar, id + 1);
  if (ar > mid) work1(ch[rt][1], mid + 1LL, r, al, ar, id + 1);
}
void work2(long long l, long long r, long long al, long long ar, int id) {
  if (al <= l && r <= ar) {
    tpb[id].push_back((ming){l, r});
    return;
  }
  long long mid = l + r >> 1;
  if (al <= mid) work2(l, mid, al, ar, id + 1);
  if (ar > mid) work2(mid + 1LL, r, al, ar, id + 1);
}
void solve() {
  tot = root = 0;
  for (int i = 0; i <= 60; i++) tpa[i].clear(), tpb[i].clear();
  for (int i = 1; i <= na; i++) work1(root, 0, INF, a[i].l, a[i].r, 0);
  for (int i = 1; i <= nb; i++) work2(0, INF, b[i].l, b[i].r, 0);
  for (int i = 1; i <= 60; i++) {
    int siz1 = tpa[i].size(), siz2 = tpb[i].size();
    for (int j = 0; j < siz1; j++)
      for (int k = 0; k < siz2; k++) {
        ming u = tpa[i][j], v = tpb[i][k];
        long long tmp = v.l ^ v.r, num = u.l ^ v.l;
        ans.push_back((ming){num & (~tmp), num | tmp});
      }
  }
}
int cmp(ming x, ming y) { return x.l < y.l; }
signed main() {
  read(na);
  for (int i = 1; i <= na; i++) read(a[i].l), read(a[i].r);
  read(nb);
  for (int i = 1; i <= nb; i++) read(b[i].l), read(b[i].r);
  solve();
  swap(a, b);
  swap(na, nb);
  solve();
  int siz = ans.size();
  sort(ans.begin(), ans.end(), cmp);
  long long tl = 0, tr = 0, answer = 0;
  for (int i = 0; i < siz; i++) {
    ming t = ans[i];
    if (t.l <= tr + 1)
      tr = max(tr, t.r);
    else
      answer =
          (answer + (tr + tl) % mo * ((tr - tl + 1) % mo) % mo * inv2 % mo) %
          mo,
      tl = t.l, tr = t.r;
  }
  answer =
      (answer + (tr + tl) % mo * ((tr - tl + 1) % mo) % mo * inv2 % mo) % mo;
  printf("%lld\n", answer);
  return 0;
}
