#include <bits/stdc++.h>
inline int read() {
  char c = getchar();
  int x = 0, ft = 1;
  for (; c < '0' || c > '9'; c = getchar())
    if (c == '-') ft = -1;
  for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
  return x * ft;
}
const int N = 105, M = 5e4 + 5, L = 4e6 + 5;
const int mod = 998244353, inv2 = (mod + 1) / 2;
int na, nb, n, ans;
long long l, r, m = (1ll << 60) - 1;
struct Segment_Tree {
  int rt, node_cnt, ch[M][2], dep[M], tag[M];
  long long num[M];
  void modify(long long ql, long long qr, int d, long long v, long long l,
              long long r, int& u) {
    if (!u) u = ++node_cnt, dep[u] = d, num[u] = v;
    if (ql <= l && r <= qr)
      tag[u] = 1;
    else {
      long long mid = (l + r) >> 1;
      if (ql <= mid) modify(ql, qr, d - 1, v, l, mid, ch[u][0]);
      if (qr > mid)
        modify(ql, qr, d - 1, v | (1ll << (d - 1)), mid + 1, r, ch[u][1]);
    }
  }
} a, b;
struct Elem {
  int dep;
  long long num;
} q[L];
inline bool operator<(Elem a, Elem b) {
  return a.num == b.num ? a.dep > b.dep : a.num < b.num;
}
int main() {
  na = read();
  while (na--) scanf("%lld%lld", &l, &r), a.modify(l, r, 60, 0, 0, m, a.rt);
  nb = read();
  while (nb--) scanf("%lld%lld", &l, &r), b.modify(l, r, 60, 0, 0, m, b.rt);
  for (int i = 1; i <= a.node_cnt; ++i)
    for (int j = 1; j <= b.node_cnt; ++j) {
      if (a.dep[i] == b.dep[j] && (a.tag[i] || b.tag[j])) {
        q[++n] = (Elem){a.dep[i], a.num[i] ^ b.num[j]};
      }
    }
  std::sort(q + 1, q + 1 + n);
  int las_d = -1;
  long long las_m = -1;
  for (int i = 1; i <= n; ++i) {
    if (las_d != -1 && las_d >= q[i].dep &&
        (las_m >> las_d) == (q[i].num >> las_d))
      continue;
    las_d = q[i].dep;
    las_m = q[i].num;
    ans = (las_m % mod * ((1ll << las_d) % mod) + ans) % mod;
    ans = (((1ll << las_d) % mod) * (((1ll << las_d) - 1) % mod) % mod * inv2 +
           ans) %
          mod;
  }
  printf("%d\n", ans);
  return 0;
}
