#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  return x * f;
}
inline void write(long long x) {
  if (x < 0) x = -x, putchar('-');
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
inline void writeln(long long x) {
  write(x);
  putchar('\n');
}
inline void wri(long long x) {
  write(x);
  putchar(' ');
}
const int maxn = 60 * 100 * 8 + 233;
const int mod = 998244353, inv2 = (mod + 1) / 2;
struct T {
  int rt, tot, c[maxn][2];
  bool tag[maxn];
  void insert(int &o, long long l, long long r, long long x, long long y) {
    if (!o) o = ++tot;
    if (x <= l && r <= y) {
      tag[o] = 1;
      return;
    }
    if (x < ((l + r) >> 1)) insert(c[o][0], l, ((l + r) >> 1), x, y);
    if (y > ((l + r) >> 1)) insert(c[o][1], ((l + r) >> 1), r, x, y);
  }
} A, B;
int nA, nB;
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 res = 0;
  for (int i = (0); i < int(2); ++i) {
    q[d - 1].clear();
    for (auto x : q[d])
      for (int j = (0); j < int(2); ++j)
        if (A.c[x.first][j] && B.c[x.second][j ^ i])
          q[d - 1].push_back({A.c[x.first][j], B.c[x.second][i ^ j]});
    if (q[d - 1].size())
      res = (res + bfs(d - 1, s | ((long long)i << (d - 1)))) % mod;
  }
  return res;
}
int main() {
  nA = read();
  for (int i = (1); i <= int(nA); ++i) {
    long long l = read(), r = read() + 1;
    A.insert(A.rt, 0, 1ll << 60, l, r);
  }
  nB = read();
  for (int i = (1); i <= int(nB); ++i) {
    long long l = read(), r = read() + 1;
    B.insert(B.rt, 0, 1ll << 60, l, r);
  }
  q[60].push_back({A.rt, B.rt});
  writeln(bfs(60, 0));
  return 0;
}
