#include <bits/stdc++.h>
using namespace std;
inline bool isEq(double a, double b) { return abs(a - b) < 1e-9; }
const double PI = acos(-1.0);
int relation[105][105];
int findRep(int c, int n) {
  if (n == relation[c][n])
    return n;
  else
    return relation[c][n] = findRep(c, relation[c][n]);
}
void makeUnion(int color, int x, int y) {
  int a = findRep(color, x);
  int b = findRep(color, y);
  if (a != b) relation[color][a] = b;
}
bool isSame(int color, int x, int y) {
  return findRep(color, x) == findRep(color, y);
}
vector<int> cols[105];
int count(int x, int y) {
  bitset<105> bs;
  bs.reset();
  if (cols[x].size() > cols[y].size()) swap(x, y);
  int d = cols[x].size();
  int cnt = 0;
  for (int i = 0; i < d; i++) {
    if (!bs[cols[x][i]]) {
      cnt += isSame(cols[x][i], x, y);
      bs[cols[x][i]] = 1;
    }
  }
  return cnt;
}
int main(int argc, char* argv[]) {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int j = 1; j <= m; j++)
    for (int i = 1; i <= n; i++) relation[j][i] = i;
  int a, b, c;
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", &a, &b, &c);
    makeUnion(c, a, b);
    cols[a].push_back(c);
    cols[b].push_back(c);
  }
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &a, &b);
    printf("%d\n", count(a, b));
  }
  return 0;
}
