#include <bits/stdc++.h>
using namespace std;
const long long MX = (1LL << 60), MOD = 998244353;
long long n, INV;
long long bm;
vector<pair<long long, long long> > segments;
struct Seg {
  vector<long long> x[61], y[61];
  void add(long long l, long long r, long long i = 0, long long j = MX - 1,
           long long d = 60) {
    if (r < i || l > j) return;
    y[d].push_back(i);
    if (l <= i && r >= j) {
      x[d].push_back(i);
    } else {
      long long m = (i + j) / 2;
      add(l, r, i, m, d - 1);
      add(l, r, m + 1, j, d - 1);
    }
  }
} segA, segB;
long long modpow(long long x_, long long N_) {
  if (N_ == 0) return 1;
  long long a = modpow(x_, N_ / 2);
  a = (a * a) % MOD;
  if (N_ % 2) a = (a * x_) % MOD;
  return a;
}
long long inv(long long x_) { return modpow(x_, MOD - 2); }
long long calcSum(long long x) {
  return (x % MOD) * (x % MOD + 1) % MOD * INV % MOD;
}
long long calcSum(long long x, long long y) {
  return (calcSum(y) - calcSum(x) + MOD) % MOD;
}
long long addSeg(long long a, long long b) {
  long long c = ((a ^ b) | bm);
  long long d = (c ^ bm);
  segments.push_back({d, c});
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  INV = inv(2);
  for (int i = int(0); i < int(n); i++) {
    long long l, r;
    cin >> l >> r;
    segA.add(l, r);
  }
  cin >> n;
  for (int i = int(0); i < int(n); i++) {
    long long l, r;
    cin >> l >> r;
    segB.add(l, r);
  }
  for (int i = int(0); i < int(61); i++) {
    bm = ((1LL << i) - 1);
    ;
    for (long long a : segA.x[i])
      for (long long b : segB.x[i]) addSeg(a, b);
    for (long long a : segA.x[i])
      for (long long b : segB.y[i]) addSeg(a, b);
    for (long long a : segA.y[i])
      for (long long b : segB.x[i]) addSeg(a, b);
  }
  sort(segments.begin(), segments.end());
  long long mostR = 0, ans = 0;
  for (pair<long long, long long> p : segments) {
    mostR = max(mostR, p.first - 1);
    if (mostR > p.second) continue;
    ans += calcSum(mostR, p.second);
    mostR = p.second;
  }
  ans %= MOD;
  cout << ans << endl;
}
