#include <bits/stdc++.h>
using namespace std;
const int INF = 1000 * 1000 * 1000;
const int mod = 998244353;
const int dwa = (mod + 1) / 2;
string s[1005];
int add(long long a, long long b) {
  a %= mod;
  b %= mod;
  return a + b >= mod ? a + b - mod : a + b;
}
int mult(long long a, long long b) {
  a %= mod;
  b %= mod;
  return a * (long long)b % mod;
}
int sum(long long l, long long r) {
  return mult(l + r, mult((r - l + 1), dwa));
}
vector<pair<long long, long long> > seg[2], seg3;
void split(long long tl, long long tr, long long l, long long r, int it,
           vector<pair<long long, long long> > &v) {
  if (l > r) return;
  if (tl == l && tr == r) {
    v.push_back({tl, it});
    return;
  }
  long long tm = (tl + tr) / 2;
  split(tl, tm, l, min(r, tm), it - 1, v);
  split(tm + 1, tr, max(l, tm + 1), r, it - 1, v);
}
bool cmp(pair<long long, long long> a, pair<long long, long long> b) {
  if (a.first != b.first) return a.first < b.first;
  return a.second > b.second;
}
vector<pair<long long, long long> > reb(vector<pair<long long, long long> > v) {
  sort(v.begin(), v.end(), cmp);
  long long r = -1;
  vector<pair<long long, long long> > w;
  for (auto first : v)
    if (first.first + (1LL << first.second) - 1 > r) {
      w.push_back(first);
      r = first.first + (1LL << first.second) - 1;
    }
  return w;
}
int n[2];
int main() {
  for (int i = 0; i < 2; i++) {
    cin >> n[i];
    while (n[i]--) {
      long long l, r;
      cin >> l >> r;
      split(0, (1LL << 60) - 1, l, r, 60, seg[i]);
    }
    seg[i] = reb(seg[i]);
  }
  set<pair<long long, long long> > s;
  for (auto S1 : seg[0])
    for (auto s2 : seg[1]) {
      auto s1 = S1;
      int mx = max(s1.second, s2.second);
      s1.first >>= mx;
      s1.first <<= mx;
      s2.first >>= mx;
      s2.first <<= mx;
      long long xl = s1.first ^ s2.first;
      long long xr = xl + (1LL << mx) - 1;
      auto it = s.lower_bound({xl, -INF});
      if (it != s.begin()) {
        auto it2 = --it;
        if (it2->first <= xl && it2->second >= xr) continue;
      }
      while (true) {
        auto it = s.lower_bound({xl, -INF});
        if (it == s.end()) break;
        if (it->first >= xl && it->second <= xr) {
          s.erase(it);
        } else
          break;
      }
      it = s.lower_bound({xl, -INF});
      if (it != s.end()) {
        if (it->first - 1 <= xr) {
          xr = it->second;
          s.erase(it);
        }
      }
      it = s.lower_bound({xl, -INF});
      if (it != s.begin()) {
        it--;
        if (it->second + 1 >= xl) {
          xl = it->first;
          s.erase(it);
        }
      }
      s.insert({xl, xr});
    }
  long long ans = 0;
  for (auto first : s) {
    ans = add(ans, sum(first.first, first.second));
  }
  cout << ans;
  return 0;
}
