You are given two sets of integers: A and B. You need to output the sum of elements in the set C = \\{x | x = a ⊕ b, a ∈ A, b ∈ B\} modulo 998244353, where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR). Each number should be counted only once.

For example, if A = \{2, 3\} and B = \{2, 3\} you should count integer 1 only once, despite the fact that you can get it as 3 ⊕ 2 and as 2 ⊕ 3. So the answer for this case is equal to 1 + 0 = 1.

Let's call a segment [l; r] a set of integers \\{l, l+1, ..., r\}.

The set A is given as a union of n_A segments, the set B is given as a union of n_B segments.

Input

The first line contains a single integer n_A (1 ≤ n_A ≤ 100).

The i-th of the next n_A lines contains two integers l_i and r_i (1 ≤ l_i ≤ r_i ≤ 10^{18}), describing a segment of values of set A.

The next line contains a single integer n_B (1 ≤ n_B ≤ 100).

The i-th of the next n_B lines contains two integers l_j and r_j (1 ≤ l_j ≤ r_j ≤ 10^{18}), describing a segment of values of set B.

Note that segments in both sets may intersect.

Output

Print one integer — the sum of all elements in set C = \\{x | x = a ⊕ b, a ∈ A, b ∈ B\} modulo 998244353.

Examples

Input


2
3 5
5 8
3
1 2
1 9
2 9


Output


112


Input


1
1 9
2
2 4
2 10


Output


120

Note

In the second example, we can discover that the set C = \{0,1,...,15\}, which means that all numbers between 0 and 15 can be represented as a ⊕ b.
#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) & ~((1LL << bit) - 1));
    for (long long p : b_tree.prefixes[bit])
      for (long long c : a_tree.contained[bit])
        xor_tree.add_single(bit, (p ^ c) & ~((1LL << bit) - 1));
    uniquify(xor_tree.prefixes[bit]);
  }
  long long total = 0;
  for (int bit = BITS - 1; bit >= 0; bit--)
    for (long long prefix : xor_tree.prefixes[bit]) {
      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;
      }
    }
  total = (total % MOD + MOD) % MOD;
  cout << total << '\n';
}
