#include <bits/stdc++.h>
using namespace std;
const int Bit = 60;
vector<pair<long long, long long> > A, B;
inline void maketrie(long long l, long long r,
                     vector<pair<long long, long long> >& rngs) {
  for (int i = 60; ~i; i--) {
    long long last = (1ll << (i + 1)) - 1;
    if (l - (l & last) == r - (r & last)) continue;
    if (r & (1ll << i)) {
      rngs.push_back(pair<long long, long long>(i, r - (1ll << i)));
    }
  }
  for (int i = 60; ~i; i--) {
    long long last = (1ll << (i + 1)) - 1;
    if (l - (l & last) == r - (r & last)) continue;
    if (!(l & (1ll << i))) {
      rngs.push_back(pair<long long, long long>(i, l + (1ll << i)));
    }
  }
}
vector<pair<long long, int> > op;
void merge(pair<long long, long long> a, pair<long long, long long> b) {
  long long Xor = a.second ^ b.second;
  Xor -= Xor & ((1ll << max(a.first, b.first)) - 1);
  long long rnglen = 1ll << max(a.first, b.first);
  op.push_back(pair<long long, int>(Xor, 1)),
      op.push_back(pair<long long, int>(Xor + rnglen, -1));
}
const int mod = 998244353;
const int inv2 = (mod + 1) >> 1;
long long S(long long n) {
  return n % mod * ((n + 1) % mod) % mod * inv2 % mod;
}
int main() {
  int sa, sb;
  cin >> sa;
  for (int i = 1; i <= sa; i++) {
    long long l, r;
    scanf("%lld%lld", &l, &r);
    maketrie(l - 1, r + 1, A);
  }
  cin >> sb;
  for (int i = 1; i <= sb; i++) {
    long long l, r;
    scanf("%lld%lld", &l, &r);
    maketrie(l - 1, r + 1, B);
  }
  for (size_t i = 0; i < A.size(); i++) {
    long long LastXor = -1;
    for (size_t j = 0; j < B.size(); j++)
      if (A[i].first > B[j].first) {
        if (LastXor !=
            (A[i].second ^ B[j].second) -
                ((A[i].second ^ B[j].second) & ((1ll << A[i].first) - 1))) {
          LastXor = (A[i].second ^ B[j].second) -
                    ((A[i].second ^ B[j].second) & ((1ll << A[i].first) - 1));
          merge(A[i], B[j]);
        }
      }
  }
  for (size_t j = 0; j < B.size(); j++) {
    long long LastXor = -1;
    for (size_t i = 0; i < A.size(); i++)
      if (A[i].first <= B[j].first) {
        if (LastXor !=
            (A[i].second ^ B[j].second) -
                ((A[i].second ^ B[j].second) & ((1ll << B[j].first) - 1))) {
          LastXor = (A[i].second ^ B[j].second) -
                    ((A[i].second ^ B[j].second) & ((1ll << B[j].first) - 1));
          merge(A[i], B[j]);
        }
      }
  }
  sort(op.begin(), op.end());
  int ans = 0;
  int t = 0;
  for (size_t i = 0, j; i < op.size(); i = j + 1) {
    j = i;
    while (j + 1 < op.size() && op[j + 1].first == op[j].first) ++j;
    for (size_t k = i; k <= j; k++) t += op[k].second;
    if (t > 0) {
      ans += (S(op[j + 1].first - 1) - S(op[j].first - 1)) % mod;
      ans %= mod;
      ans += mod;
      ans %= mod;
    }
  }
  cout << ans << endl;
  return 0;
}
