#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long data = 0, w = 1;
  char ch = 0;
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') w = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') data = data * 10 + ch - '0', ch = getchar();
  return data * w;
}
long long m;
struct ival {
  long long l, r;
} w[5000005];
struct Con {
  long long n;
  ival a[105];
} A, B;
vector<long long> v[65];
const long long M = 60;
void dicon(long long l, long long r, long long ql, long long qr, long long d) {
  v[d].push_back(l);
  if (ql <= l && r <= qr) return;
  long long mid = l + r >> 1;
  if (ql <= mid) dicon(l, mid, ql, qr, d + 1);
  if (mid < qr) dicon(mid + 1, r, ql, qr, d + 1);
}
const long long mod = 998244353;
long long Sum(long long l, long long r) {
  l %= mod;
  r %= mod;
  return (r * (r + 1) - (l - 1) * l) % mod * (mod + 1) / 2 % mod;
}
void Solve(long long l, long long r, long long ql, long long qr, long long d) {
  if (ql <= l && r <= qr) {
    for (auto p : v[d]) w[++m] = (ival){l ^ p, (l ^ p) + (1ll << M - d) - 1};
    return;
  }
  long long mid = l + r >> 1;
  if (ql <= mid) Solve(l, mid, ql, qr, d + 1);
  if (mid < qr) Solve(mid + 1, r, ql, qr, d + 1);
}
void clac() {
  for (long long i = 0; i <= M; i++) v[i].clear();
  for (long long i = 1; i <= B.n; i++)
    dicon(0, (1ll << M) - 1, B.a[i].l, B.a[i].r, 0);
  for (long long i = 1; i <= A.n; i++)
    Solve(0, (1ll << M) - 1, A.a[i].l, A.a[i].r, 0);
}
int main() {
  A.n = read();
  for (long long i = 1; i <= A.n; i++) A.a[i].l = read(), A.a[i].r = read();
  B.n = read();
  for (long long i = 1; i <= B.n; i++) B.a[i].l = read(), B.a[i].r = read();
  clac();
  swap(A, B), clac();
  sort(w + 1, w + m + 1, [](ival A, ival B) { return A.l < B.l; });
  ival now;
  now = w[1];
  long long ans = 0;
  for (long long i = 2; i <= m; i++) {
    if (now.r < w[i].l)
      ans = (ans + Sum(now.l, now.r)) % mod, now = w[i];
    else
      now.r = max(now.r, w[i].r);
  }
  ans = (ans + Sum(now.l, now.r)) % mod;
  cout << (ans % mod + mod) % mod << '\n';
  return 0;
}
