#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int BITS = 60;
template <typename T>
void uniquify(vector<T> &v) {
  sort(v.begin(), v.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
}
struct tree {
  vector<long long> prefixes[BITS + 1];
  vector<long long> contained[BITS + 1];
  void recursive_add(int bit, long long prefix, long long start,
                     long long end) {
    long long after = prefix + (1LL << bit);
    if (start <= prefix && after <= end) {
      prefixes[bit].push_back(prefix);
      return;
    }
    if (bit <= 0) return;
    long long mid = (prefix + after) / 2;
    if (start < mid) recursive_add(bit - 1, prefix, start, end);
    if (end > mid) recursive_add(bit - 1, mid, start, end);
  }
  void add_range(long long L, long long R) { recursive_add(BITS, 0, L, R); }
  void add_single(int bit, long long prefix) {
    prefixes[bit].push_back(prefix);
  }
  bool contains(int bit, long long prefix) {
    return binary_search(prefixes[bit].begin(), prefixes[bit].end(), prefix);
  }
  void build() {
    for (int bit = 0; bit < BITS; bit++) {
      uniquify(prefixes[bit]);
      contained[bit] = bit == 0 ? vector<long long>() : contained[bit - 1];
      for (long long &x : contained[bit]) x &= ~((1LL << bit) - 1);
      contained[bit].insert(contained[bit].end(), prefixes[bit].begin(),
                            prefixes[bit].end());
      uniquify(contained[bit]);
    }
  }
  void print() {
    cerr << endl;
    for (int bit = 0; bit < BITS; bit++) {
      if (prefixes[bit].empty()) continue;
      cerr << bit << ':';
      for (long long x : prefixes[bit]) cerr << ' ' << x;
      cerr << endl;
    }
  }
};
int main() {
  int A, B;
  tree a_tree, b_tree;
  cin >> A;
  for (int i = 0; i < A; i++) {
    long long L, R;
    cin >> L >> R;
    R++;
    a_tree.add_range(L, R);
  }
  cin >> B;
  for (int i = 0; i < B; i++) {
    long long L, R;
    cin >> L >> R;
    R++;
    b_tree.add_range(L, R);
  }
  a_tree.build();
  b_tree.build();
  tree xor_tree;
  for (int bit = 0; bit < BITS; bit++) {
    for (long long p : a_tree.prefixes[bit])
      for (long long c : b_tree.contained[bit]) xor_tree.add_single(bit, p ^ c);
    for (long long p : b_tree.prefixes[bit])
      for (long long c : a_tree.contained[bit]) xor_tree.add_single(bit, p ^ c);
    uniquify(xor_tree.prefixes[bit]);
  }
  long long total = 0;
  long long prefix_count = 0;
  for (int bit = BITS - 1; bit >= 0; bit--)
    for (long long prefix : xor_tree.prefixes[bit]) {
      prefix_count++;
      bool covered = false;
      for (int b = bit + 1; b < BITS; b++)
        if (xor_tree.contains(b, prefix & ~((1LL << b) - 1))) {
          covered = true;
          break;
        }
      if (!covered) {
        long long count = 1LL << bit;
        long long prefix_sum = (prefix % MOD) * (count % MOD);
        long long bit_sum = (count / 2 % MOD) * ((count - 1) % MOD);
        total += prefix_sum + bit_sum;
        total %= MOD;
      }
    }
  cerr << prefix_count << " prefixes" << endl;
  total = (total % MOD + MOD) % MOD;
  cout << total << '\n';
}
