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 Bit = 60;
vector<pair<long long, long long> > A, B;
inline void maketrie(long long l, long long r,
                     vector<pair<long long, long long> >& rngs) {
  for (int i = 60; ~i; i--) {
    long long last = (1ll << (i + 1)) - 1;
    if (l - (l & last) == r - (r & last)) continue;
    if (r & (1ll << i)) {
      rngs.push_back(pair<long long, long long>(i, r - (1ll << i)));
    }
  }
  for (int i = 60; ~i; i--) {
    long long last = (1ll << (i + 1)) - 1;
    if (l - (l & last) == r - (r & last)) continue;
    if (!(l & (1ll << i))) {
      rngs.push_back(pair<long long, long long>(i, l + (1ll << i)));
    }
  }
}
vector<pair<long long, int> > op;
void merge(pair<long long, long long> a, pair<long long, long long> b) {
  long long Xor = a.second ^ b.second;
  Xor -= Xor & ((1ll << max(a.first, b.first)) - 1);
  long long rnglen = 1ll << max(a.first, b.first);
  op.push_back(pair<long long, int>(Xor, 1)),
      op.push_back(pair<long long, int>(Xor + rnglen, -1));
}
const int mod = 998244353;
const int inv2 = (mod + 1) >> 1;
long long S(long long n) {
  return n % mod * ((n + 1) % mod) % mod * inv2 % mod;
}
int main() {
  int sa, sb;
  cin >> sa;
  for (int i = 1; i <= sa; i++) {
    long long l, r;
    scanf("%lld%lld", &l, &r);
    maketrie(l - 1, r + 1, A);
  }
  cin >> sb;
  for (int i = 1; i <= sb; i++) {
    long long l, r;
    scanf("%lld%lld", &l, &r);
    maketrie(l - 1, r + 1, B);
  }
  for (size_t i = 0; i < A.size(); i++) {
    long long LastXor = -1;
    for (size_t j = 0; j < B.size(); j++)
      if (A[i].first > B[j].first) {
        if (LastXor !=
            (A[i].second ^ B[j].second) -
                ((A[i].second ^ B[j].second) & ((1ll << A[i].first) - 1))) {
          LastXor = (A[i].second ^ B[j].second) -
                    ((A[i].second ^ B[j].second) & ((1ll << A[i].first) - 1));
          merge(A[i], B[j]);
        }
      }
  }
  for (size_t j = 0; j < B.size(); j++) {
    long long LastXor = -1;
    for (size_t i = 0; i < A.size(); i++)
      if (A[i].first <= B[j].first) {
        if (LastXor !=
            (A[i].second ^ B[j].second) -
                ((A[i].second ^ B[j].second) & ((1ll << B[j].first) - 1))) {
          LastXor = (A[i].second ^ B[j].second) -
                    ((A[i].second ^ B[j].second) & ((1ll << B[j].first) - 1));
          merge(A[i], B[j]);
        }
      }
  }
  sort(op.begin(), op.end());
  int ans = 0, t = 0;
  for (size_t i = 0, j; i < op.size(); i = j + 1) {
    j = i;
    while (j + 1 < op.size() && op[j + 1].first == op[j].first) ++j;
    for (size_t k = i; k <= j; k++) t += op[k].second;
    if (t > 0) {
      ans += (S(op[j + 1].first - 1) - S(op[j].first - 1)) % mod;
      ans %= mod;
      ans += mod;
      ans %= mod;
    }
  }
  cout << ans << endl;
  return 0;
}
