#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e7 + 10, mod = 998244353, I = (mod + 1) >> 1;
const long long lim = (1ll << 60) - 1;
int na, nb, cnt, ans;
int ch[maxN + 1][2];
int ra[105], rb[105];
inline int build(long long l, long long r, long long x, long long y) {
  if (x <= l && r <= y) return -1;
  int node = ++cnt;
  long long mid = (l + r) >> 1;
  if (x <= mid) ch[node][0] = build(l, mid, x, y);
  if (y > mid) ch[node][1] = build(mid + 1, r, x, y);
  return node;
}
inline int merge(int u, int v) {
  if (!u || !v) return u | v;
  if (u == -1 || v == -1) return -1;
  int L = merge(ch[u][0], ch[v][0]);
  int R = merge(ch[u][1], ch[v][1]);
  if (L == -1 && R == -1) return -1;
  int node = ++cnt;
  ch[node][0] = L, ch[node][1] = R;
  return node;
}
inline int XOR(int u, int v) {
  if (!u || !v) return 0;
  if (u == -1 || v == -1) return -1;
  int L = merge(XOR(ch[u][0], ch[v][0]), XOR(ch[u][1], ch[v][1]));
  int R = merge(XOR(ch[u][0], ch[v][1]), XOR(ch[u][1], ch[v][0]));
  if (L == -1 && R == -1) return -1;
  int node = ++cnt;
  ch[node][0] = L, ch[node][1] = R;
  return node;
}
inline void calc(int node, long long l, long long r) {
  if (!node) return;
  if (node == -1) {
    int x = l % mod, y = r % mod;
    int t = 1ll * (x + y) * (y + mod - x + 1) % mod * I % mod;
    ans = (ans + t) % mod;
    return;
  }
  long long mid = (l + r) >> 1;
  calc(ch[node][0], l, mid);
  calc(ch[node][1], mid + 1, r);
}
int main() {
  scanf("%d", &na);
  for (int i = 1; i <= na; i++) {
    long long l, r;
    scanf("%lld %lld", &l, &r);
    ra[i] = build(0, lim, l, r);
  }
  scanf("%d", &nb);
  for (int i = 1; i <= nb; i++) {
    long long l, r;
    scanf("%lld %lld", &l, &r);
    rb[i] = build(0, lim, l, r);
  }
  int rt = 0;
  for (int i = 1; i <= na; i++)
    for (int j = 1; j <= nb; j++) rt = merge(rt, XOR(ra[i], rb[j]));
  calc(rt, 0, lim);
  printf("%d\n", ans);
  return 0;
}
