#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int n, m, cnt;
vector<long long> ve[63];
long long la[105], ra[105], lb[105], rb[105];
pair<long long, long long> f[8001000];
inline void insert(long long l, long long r, long long L, long long R,
                   long long val, int k) {
  ve[k].push_back(val);
  if (L >= l && R <= r) return;
  const long long mid = L + R >> 1;
  if (l <= mid) insert(l, r, L, mid, val, k - 1);
  if (r > mid) insert(l, r, mid + 1, R, val | (1ll << k - 1), k - 1);
}
inline void Insert(long long l, long long r, long long L, long long R,
                   long long val, int k) {
  if (L >= l && R <= r) {
    for (long long y : ve[k])
      f[++cnt] = make_pair(val ^ y, (val ^ y) + (1ll << k) - 1);
    return;
  }
  const long long mid = L + R >> 1;
  if (l <= mid) Insert(l, r, L, mid, val, k - 1);
  if (r > mid) Insert(l, r, mid + 1, R, val | (1ll << k - 1), k - 1);
}
inline void calc(int n, int m, long long *l, long long *r, long long *L,
                 long long *R) {
  for (int i = 0; i <= 60; ++i) ve[i].clear();
  for (int i = 1; i <= n; ++i) insert(l[i], r[i], 0, (1ll << 60) - 1, 0, 60);
  for (int i = 1; i <= m; ++i) Insert(L[i], R[i], 0, (1ll << 60) - 1, 0, 60);
}
inline long long solve(long long l, long long r) {
  return (r - l + 1) % mod * ((l + r) % mod) % mod * 499122177 % mod;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%lld%lld", &la[i], &ra[i]);
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) scanf("%lld%lld", &lb[i], &rb[i]);
  calc(n, m, la, ra, lb, rb);
  calc(m, n, lb, rb, la, ra);
  sort(f + 1, f + cnt + 1);
  long long ans = 0, l = 0, r = 0;
  for (int i = 1; i <= cnt; ++i)
    if (f[i].first > r)
      l = f[i].first, r = f[i].second, ans += solve(l, r);
    else if (r < f[i].second)
      ans += solve(r + 1, f[i].second), r = f[i].second;
  return printf("%lld\n", ans % mod), 0;
}
