#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const int LIM = 60;
long long sum(long long l, long long r) {
  long long sum1 = l + r;
  long long sum2 = r - l + 1;
  if (sum1 % 2 == 0) {
    sum1 /= 2;
  } else {
    sum2 /= 2;
  }
  sum1 %= mod, sum2 %= mod;
  return (sum1 * sum2) % mod;
}
struct State {
  long long path;
  long long vid;
  long long l, r;
  State() {}
  State(long long p, long long v, long long l, long long r)
      : path(p), vid(v), l(l), r(r) {}
  long long len() { return r - l + 1; }
  pair<long long, pair<long long, long long> > get_len(long long targetlen) {
    State cur(path, vid, l, r);
    while (cur.len() != targetlen) {
      cur = cur.parent();
    }
    return make_pair(cur.path, make_pair(cur.l, cur.r));
  }
  State parent() {
    long long nvid = vid / 2;
    long long npath = path / 2;
    long long clen = len();
    if (vid % 2 == 0) {
      return State(npath, nvid, l, r + clen);
    } else {
      return State(npath, nvid, l - clen, r);
    }
  }
  int dep() {
    long long clen = len();
    int ret = 0;
    while (clen < (1LL << LIM)) {
      ++ret;
      clen *= 2;
    }
    return ret;
  }
  pair<long long, long long> merge(long long nl, long long nr,
                                   long long npath) {
    long long merged = path ^ (npath);
    long long extra = merged << (LIM - dep());
    return make_pair(extra, extra + len() - 1);
  }
};
vector<State> current;
void fill_state(long long v, long long l, long long r, long long path,
                long long tl, long long tr) {
  if (l == tl && r == tr) {
    current.push_back(State(path, v, l, r));
    return;
  }
  long long mid = (l + r) / 2;
  if (tl > mid) {
    fill_state(2 * v + 1, mid + 1, r, path * 2 + 1, tl, tr);
  } else if (tr <= mid) {
    fill_state(2 * v, l, mid, path * 2, tl, tr);
  } else {
    fill_state(2 * v, l, mid, path * 2, tl, mid);
    fill_state(2 * v + 1, mid + 1, r, path * 2 + 1, mid + 1, tr);
  }
}
bool cmp(pair<long long, long long>& a, pair<long long, long long>& b) {
  return a.second < b.second;
}
vector<pair<long long, long long> > ret;
void merge(vector<State>& a, vector<State>& b) {
  for (long long i = LIM; i >= 0; --i) {
    vector<State> cur;
    for (auto& s : a) {
      if (s.len() == (1LL << i)) {
        cur.push_back(s);
      }
    }
    set<pair<long long, pair<long long, long long> > > st;
    for (auto& s : b) {
      if (s.len() > (1LL << i)) {
        continue;
      }
      auto v = s.get_len(1LL << i);
      st.insert(v);
    }
    for (auto& s : cur) {
      for (auto& p : st) {
        ret.push_back(s.merge(p.second.first, p.second.second, p.first));
      }
    }
  }
}
void solve() {
  int n;
  cin >> n;
  current.clear();
  int x1 = -1, y1 = -1;
  for (int i = 0; i < n; ++i) {
    long long x, y;
    cin >> x >> y;
    x1 = x, y1 = y;
    fill_state(1, 0, (1LL << LIM) - 1, 0, x, y);
  }
  vector<State> a = current;
  current.clear();
  int m;
  cin >> m;
  int x2 = -1, y2 = -1;
  for (int i = 0; i < m; ++i) {
    long long x, y;
    cin >> x >> y;
    x2 = x, y2 = y;
    fill_state(1, 0, (1LL << LIM) - 1, 0, x, y);
  }
  vector<State> b = current;
  merge(a, b);
  merge(b, a);
  ret.reserve(5000000);
  sort(ret.begin(), ret.end());
  long long res = 0;
  long long cl = ret[0].first;
  long long cr = ret[0].second;
  for (int i = 1; i < ret.size(); ++i) {
    if (ret[i].first > cr) {
      res += sum(cl, cr);
      res %= mod;
      cl = ret[i].first;
      cr = ret[i].second;
    } else {
      cr = max(cr, ret[i].second);
    }
  }
  res += sum(cl, cr);
  res %= mod;
  cout << res << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  t = 1;
  for (int tt = 0; tt < t; ++tt) {
    solve();
  }
  return 0;
}
