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 maxn = 100 + 10, lg = 61, M = 998244353;
struct GoodSeg {
  long long x;
  char powerSize;
  GoodSeg(long long x, char powerSize) {
    this->x = x;
    this->powerSize = powerSize;
  }
  long long getSize() { return (1ll << powerSize); }
  long long getY() { return x + getSize(); }
  bool operator<(GoodSeg goodSeg) const { return x < goodSeg.x; }
};
struct Seg {
  long long l, r;
  Seg() {}
  Seg(long long l, long long r) {
    this->l = l;
    this->r = r;
  }
  bool operator<(Seg seg) const { return l < seg.l; }
};
vector<GoodSeg> segToGoodSegs(Seg seg) {
  vector<GoodSeg> answer;
  for (int j = 0; j < lg; j++) {
    if ((((seg.l) >> (j)) & 1) && seg.l + (1ll << j) <= seg.r) {
      answer.push_back(GoodSeg(seg.l, j));
      seg.l += (1ll << j);
    }
  }
  for (int j = 0; j < lg; j++) {
    if ((((seg.r) >> (j)) & 1) && seg.l + (1ll << j) <= seg.r) {
      seg.r -= (1ll << j);
      answer.push_back(GoodSeg(seg.r, j));
    }
  }
  return answer;
}
vector<GoodSeg> xorSegment(Seg seg, long long xr, long long j = 0) {
  seg.l >>= j;
  seg.r = ((seg.r - 1) >> j) + 1;
  xr >>= j;
  vector<GoodSeg> answer = segToGoodSegs(seg);
  for (GoodSeg &goodSeg : answer) {
    long long xrr = (xr >> goodSeg.powerSize);
    xrr <<= goodSeg.powerSize;
    goodSeg.x ^= xrr;
    goodSeg.x <<= j;
    goodSeg.powerSize += j;
  }
  return answer;
}
vector<GoodSeg> multiply(Seg seg0, Seg seg1) {
  vector<GoodSeg> goodSegs = segToGoodSegs(seg0);
  vector<GoodSeg> answer, temporaryVector;
  for (GoodSeg goodSeg : goodSegs) {
    temporaryVector = xorSegment(seg1, goodSeg.x, goodSeg.powerSize);
    answer.insert(answer.end(), temporaryVector.begin(), temporaryVector.end());
  }
  return answer;
}
int segmentSum(long long l, long long r) {
  l %= M;
  r = (r - 1ll + M) % M;
  return ((1ll * (l + r) * (r - l + 1ll) / 2ll) % M + M) % M;
}
vector<Seg> unionOfGoodSegs(vector<GoodSeg> goodSegs) {
  long long l = 0, r = 0;
  sort(goodSegs.begin(), goodSegs.end());
  vector<Seg> segs;
  for (GoodSeg goodSeg : goodSegs) {
    if (goodSeg.x > r) {
      segs.push_back(Seg(l, r));
      l = goodSeg.x;
    }
    r = max(r, goodSeg.getY());
  }
  segs.push_back(Seg(l, r));
  return segs;
}
int sumofUnion(vector<Seg> Segs) {
  int answer = 0;
  long long l = 0, r = 0;
  sort(Segs.begin(), Segs.end());
  for (Seg seg : Segs) {
    if (seg.l > r) {
      answer = (answer + segmentSum(l, r)) % M;
      l = seg.l;
    }
    r = max(r, seg.r);
  }
  answer = (answer + segmentSum(l, r)) % M;
  return answer;
}
int n, m;
Seg A[maxn], B[maxn];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i].l >> A[i].r;
    A[i].r++;
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> B[i].l >> B[i].r;
    B[i].r++;
  }
  vector<Seg> answer, temporaryVector;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      temporaryVector = unionOfGoodSegs(multiply(A[i], B[j]));
      answer.insert(answer.end(), temporaryVector.begin(),
                    temporaryVector.end());
    }
  }
  cout << sumofUnion(answer) << endl;
}
