#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Seg {
  ll start;
  ll finish;
  ll size() const { return finish - start; }
  bool contains(const Seg &s) const {
    return start <= s.start && s.finish <= finish;
  }
};
struct Node {
  Node *l = nullptr;
  Node *r = nullptr;
  bool full = false;
};
void update(Node *n, const Seg &sn, const Seg &su) {
  if (su.contains(sn)) {
    n->full = true;
    return;
  }
  ll mid = (sn.start + sn.finish) >> 1;
  if (su.start < mid) {
    if (n->l == nullptr) {
      n->l = new Node;
    }
    update(n->l, Seg{sn.start, mid}, su);
  }
  if (mid < su.finish) {
    if (n->r == nullptr) {
      n->r = new Node;
    }
    update(n->r, Seg{mid, sn.finish}, su);
  }
}
const ll M = 998244353ll;
ll segSum(const Seg &s) {
  ll x = s.start + s.finish - 1;
  ll y = s.size();
  if (y & 1) {
    x >>= 1;
  } else {
    y >>= 1;
  }
  x %= M;
  y %= M;
  return x * y % M;
}
using pnn = pair<Node *, Node *>;
using vpnn = vector<pnn>;
ll calc(const vpnn &v, const Seg &s) {
  for (auto &p : v) {
    if (p.first->full || p.second->full) {
      return segSum(s);
    }
  }
  ll sol = 0;
  ll mid = (s.start + s.finish) >> 1;
  {
    vpnn vl;
    for (const pnn &p : v) {
      if (p.first->l && p.second->l) {
        vl.emplace_back(p.first->l, p.second->l);
      }
      if (p.first->r && p.second->r) {
        vl.emplace_back(p.first->r, p.second->r);
      }
    }
    if (!vl.empty()) {
      sol += calc(vl, Seg{s.start, mid});
    }
  }
  {
    vpnn vr;
    for (const pnn &p : v) {
      if (p.first->l && p.second->r) {
        vr.emplace_back(p.first->l, p.second->r);
      }
      if (p.first->r && p.second->l) {
        vr.emplace_back(p.first->r, p.second->l);
      }
    }
    if (!vr.empty()) {
      sol += calc(vr, Seg{mid, s.finish});
    }
  }
  return sol % M;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  auto inputSet = []() {
    auto *n = new Node;
    int ct;
    cin >> ct;
    while (ct--) {
      ll i, j;
      cin >> i >> j;
      update(n, Seg{0, 1ll << 60}, Seg{i, j + 1});
    }
    return n;
  };
  Node *a = inputSet();
  Node *b = inputSet();
  vpnn v = {{a, b}};
  cout << calc(v, Seg{0, 1ll << 60}) << endl;
  return 0;
}
