#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005;
const int mod = 998244353, inv2 = (mod + 1) / 2;
struct Tree {
  int rt, tot, ch[MAXN][2];
  bool tag[MAXN];
  void ins(int &i, long long l, long long r, long long x, long long y) {
    if (!i) i = ++tot;
    if (x <= l && r <= y) {
      tag[i] = 1;
      return;
    }
    long long mid = (l + r) >> 1;
    if (x < mid) ins(ch[i][0], l, mid, x, y);
    if (y > mid) ins(ch[i][1], mid, r, x, y);
  }
} A, B;
int n, m;
vector<pair<int, int> > q[61];
int bfs(int d, long long s) {
  for (auto x : q[d])
    if (A.tag[x.first] || B.tag[x.second])
      return (s + ((1ll << d) - 1) % mod * inv2) % mod * ((1ll << d) % mod) %
             mod;
  int re = 0;
  for (int i = 0; i < 2; ++i) {
    q[d - 1].clear();
    for (auto x : q[d])
      for (int j = 0; j < 2; ++j)
        if (A.ch[x.first][j] && B.ch[x.second][j ^ i])
          q[d - 1].push_back(
              pair<int, int>(A.ch[x.first][j], B.ch[x.second][j ^ i]));
    if (q[d - 1].size())
      re = (re + bfs(d - 1, s | ((long long)i << d - 1))) % mod;
  }
  return re;
}
int main() {
  scanf("%d", &n);
  long long x, y;
  for (int i = 1; i <= n; ++i) {
    scanf("%lld%lld", &x, &y);
    A.ins(A.rt, 0, 1ll << 60, x, y + 1);
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%lld%lld", &x, &y);
    B.ins(B.rt, 0, 1ll << 60, x, y + 1);
  }
  q[60].push_back(pair<int, int>(A.rt, B.rt));
  printf("%d\n", bfs(60, 0));
}
