#include <bits/stdc++.h>
using namespace std;
namespace mine {
long long qread() {
  long long ans = 0, f = 1;
  char c = getchar();
  while (c < '0' or c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while ('0' <= c and c <= '9') ans = ans * 10 + c - '0', c = getchar();
  return ans * f;
}
void write(long long num) {
  if (num < 0) putchar('-'), num = -num;
  if (num >= 10) write(num / 10);
  putchar('0' + num % 10);
}
void write1(long long num) {
  write(num);
  putchar(' ');
}
void write2(long long num) {
  write(num);
  putchar('\n');
}
template <typename T>
void chmax(T &x, const T y) {
  x = (x > y ? x : y);
}
template <typename T>
void chmin(T &x, const T y) {
  x = (x < y ? x : y);
}
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
int mm(const int x) { return x >= MOD ? x - MOD : x; }
template <typename T>
void add(T &x, const int &y) {
  x = (x + y >= MOD ? x + y - MOD : x + y);
}
long long qpower(long long x, long long e, int mod = MOD) {
  long long ans = 1;
  while (e) {
    if (e & 1) ans = ans * x % mod;
    x = x * x % mod;
    e >>= 1;
  }
  return ans;
}
long long invm(long long x) { return qpower(x, MOD - 2); }
const int N = 2e5 + 10;
vector<pair<long long, long long> > real[2];
vector<long long> go[2][100];
void split(int op, long long fl, long long fr, long long l = 0,
           long long r = (1ll << (60)) - 1, int i = 60) {
  go[op][i].push_back(l);
  if (l == fl and r == fr) {
    real[op].push_back(make_pair(l, r));
    return;
  }
  if (fr <= (l + r) / 2)
    split(op, fl, fr, l, (l + r) / 2, i - 1);
  else if (fl > (l + r) / 2)
    split(op, fl, fr, (l + r) / 2 + 1, r, i - 1);
  else
    split(op, fl, (l + r) / 2, l, (l + r) / 2, i - 1),
        split(op, (l + r) / 2 + 1, fr, (l + r) / 2 + 1, r, i - 1);
}
vector<pair<long long, long long> > all;
void solve(long long al, long long ar, long long bl, long long br) {
  for (int op = (0), I = (1); op <= I; op++) {
    real[op].clear();
    for (int i = (0), I = (60); i <= I; i++) go[op][i].clear();
  }
  split(0, al, ar);
  split(1, bl, br);
  for (int op = (0), I = (1); op <= I; op++)
    for (auto x : real[op]) {
      int i = log2(x.second - x.first + 1);
      for (auto y : go[op ^ 1][i])
        all.push_back(make_pair(x.first ^ y, (x.first ^ y) + (1ll << (i)) - 1));
    }
}
void main() {
  int n = qread();
  vector<pair<long long, long long> > A(n);
  for (int i = (0), I = (n - 1); i <= I; i++)
    A[i].first = qread(), A[i].second = qread();
  n = qread();
  vector<pair<long long, long long> > B(n);
  for (int i = (0), I = (n - 1); i <= I; i++)
    B[i].first = qread(), B[i].second = qread();
  for (auto a : A)
    for (auto b : B) solve(a.first, a.second, b.first, b.second);
  sort((all).begin(), (all).end());
  long long R = -1, ans = 0;
  for (auto t : all)
    if (R < t.second) {
      long long l = max(R + 1, t.first) % MOD, r = t.second % MOD;
      (ans += (r + 1) * r - l * (l - 1)) %= MOD;
      R = t.second;
    }
  write((ans * invm(2) % MOD + MOD) % MOD);
}
};  // namespace mine
signed main() {
  srand(time(0));
  mine::main();
}
