#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0;
  bool f = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar()) f = (ch == '-');
  for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
  return f ? -x : x;
}
const long long inf = 1ll << 60;
const long long mod = 998244353;
const long long inv2 = (mod + 1) / 2;
inline long long calc(long long l, long long r) {
  l %= mod;
  r %= mod;
  return (l + r) * (r - l + 1 + mod) % mod * inv2 % mod;
}
long long na, nb, ans;
struct node {
  long long w, a;
  node(long long x, long long y) {
    w = x;
    a = y;
  }
  inline bool friend operator<(node a, node b) {
    return a.w == b.w ? a.a < b.a : a.w < b.w;
  }
  inline bool friend operator!=(node a, node b) {
    return a.w != b.w || a.a != b.a;
  }
};
map<long long, long long> mp;
vector<node> a[70], b[70], A[70], B[70], c;
void solve(vector<node>* a, vector<node>* b) {
  for (int i = 0; i <= 60; ++i) {
    sort(a[i].begin(), a[i].end());
    sort(b[i].begin(), b[i].end());
    if (!a[i].size() || !b[i].size()) continue;
    for (int j = 0; j < a[i].size(); ++j)
      if (!j || a[i][j] != a[i][j - 1])
        for (int k = 0; k < b[i].size(); ++k)
          if (!k || b[i][k] != b[i][k - 1]) {
            long long x = a[i][j].a ^ b[i][k].a;
            c.push_back(node(x << i, ((x + 1) << i) - 1));
          }
  }
}
void cover(long long l, long long r, long long ql, long long qr,
           vector<node>* a, vector<node>* A) {
  int w = mp[r - l + 1];
  A[w].push_back(node(w, l >> w));
  if (ql <= l && r <= qr) return a[w].push_back(node(w, l >> w));
  long long mid = (l + r) >> 1;
  if (ql <= mid) cover(l, mid, ql, qr, a, A);
  if (qr > mid) cover(mid + 1, r, ql, qr, a, A);
}
int main() {
  for (int i = 0; i <= 60; ++i) mp[1ll << i] = i;
  na = read();
  for (int i = 1; i <= na; ++i) {
    long long l = read(), r = read();
    cover(0, inf - 1, l, r, a, A);
  }
  nb = read();
  for (int i = 1; i <= nb; ++i) {
    long long l = read(), r = read();
    cover(0, inf - 1, l, r, b, B);
  }
  solve(a, B);
  solve(b, A);
  sort(c.begin(), c.end());
  for (int i = 0; i < c.size(); ++i)
    ;
  long long L = c[0].w, R = c[0].a;
  for (int i = 1; i < c.size(); ++i) {
    long long l = c[i].w, r = c[i].a;
    if (l <= R)
      R = max(R, r);
    else
      ans += calc(L, R), L = l, R = r;
  }
  ans += calc(L, R);
  printf("%lld\n", ans % mod);
  return 0;
}
